mxnet
slice.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 #ifndef MSHADOW_EXTENSION_SLICE_H_
25 #define MSHADOW_EXTENSION_SLICE_H_
26 
27 #include "../extension.h"
28 
29 namespace mshadow {
30 namespace expr {
38 template<typename SrcExp,
39  typename Device, typename DType,
40  int srcdim, int dimsrc_m_slice>
41 struct SliceExp : public TRValue<SliceExp<SrcExp,
42  Device, DType,
43  srcdim, dimsrc_m_slice>,
44  Device, srcdim, DType> {
45  static const int dimslice = srcdim - dimsrc_m_slice;
46  const SrcExp &src_;
50  SliceExp(const SrcExp &src, index_t begin, index_t end)
51  : src_(src), ch_begin_(begin) {
54  CHECK(begin <= shape_[dimslice] && end <= shape_[dimslice])
55  << "The slice went out of range. ";
56  shape_[dimslice] = end - begin;
57  }
58  template<typename E, int etype>
59  inline void
61  this->__assign(exp);
62  }
63  inline void
64  operator=(const DType &exp) {
65  this->__assign(exp);
66  }
67 }; // struct Slice
68 
80 template<int sdim, typename SrcExp,
81  typename Device, typename DType, int srcdim>
82 inline SliceExp<SrcExp, Device, DType, srcdim, srcdim - sdim>
85  ::Error_Expression_Does_Not_Meet_Dimension_Req();
86  return SliceExp<SrcExp, Device, DType, srcdim, srcdim - sdim>(src.self(), begin, end);
87 }
88 //------------------------
89 // engine plugin
90 //------------------------
91 // runtime shapecheck
92 template<typename SrcExp,
93  typename Device, typename DType,
94  int srcdim, int dimsrc_m_slice>
95 struct ShapeCheck<srcdim, SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice> >{
96  inline static Shape<srcdim> Check(const SliceExp<SrcExp,
97  Device, DType, srcdim, dimsrc_m_slice> &t) {
98  return t.shape_;
99  }
100 };
101 template<typename SrcExp,
102  typename Device, typename DType,
103  int srcdim, int dimsrc_m_slice>
104 struct StreamInfo<Device, SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice> >{
105  inline static Stream<Device> *
108  }
109 };
110 // static typecheck
111 template<typename SrcExp,
112  typename Device, typename DType,
113  int srcdim, int dimsrc_m_slice>
114 struct ExpInfo<SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice> >{
115  static const int kDim = ExpInfo<SrcExp>::kDim;
117 };
118 //----------------------
119 // Execution plan
120 //---------------------
121 template<typename SrcExp,
122  typename Device, typename DType,
123  int srcdim, int dimsrc_m_slice>
124 struct Plan<SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice>, DType> {
125  public:
126  static const int dimslice = srcdim - dimsrc_m_slice;
128  : src_(MakePlan(e.src_)),
129  height_(e.shape_.ProdShape(dimslice + 1, srcdim - 1)),
130  ch_begin_(e.ch_begin_), ch_old_(e.ch_old_), ch_(e.shape_[dimslice]) {}
131  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
132  const index_t y = i % height_;
133  i /= height_;
134  const index_t c = i % ch_ + ch_begin_;
135  const index_t b = i / ch_;
136  const index_t x = j;
137  return src_.Eval((b * ch_old_ + c) * height_ + y, x);
138  }
140  const index_t y = i % height_;
141  i /= height_;
142  const index_t c = i % ch_ + ch_begin_;
143  const index_t b = i / ch_;
144  const index_t x = j;
145  return src_.REval((b * ch_old_ + c) * height_ + y, x);
146  }
147 
148  private:
149  Plan<SrcExp, DType> src_;
150  const index_t height_, ch_begin_, ch_old_, ch_;
151 }; // struct Plan
152 
153 template<typename SrcExp,
154  typename Device, typename DType,
155  int srcdim>
156 struct Plan<SliceExp<SrcExp, Device, DType, srcdim, 1>, DType> {
157  public:
159  : src_(MakePlan(e.src_)),
160  ch_begin_(e.ch_begin_) {}
161  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
162  return src_.Eval(y, x + ch_begin_);
163  }
165  return src_.REval(y, x + ch_begin_);
166  }
167 
168  private:
169  Plan<SrcExp, DType> src_;
170  const index_t ch_begin_;
171 };
172 } // namespace expr
173 } // namespace mshadow
174 #endif // MSHADOW_EXTENSION_SLICE_H_
mshadow::expr::Plan< SliceExp< SrcExp, Device, DType, srcdim, 1 >, DType >::REval
MSHADOW_XINLINE DType & REval(index_t y, index_t x)
Definition: slice.h:164
mshadow::expr::ExpInfo::kDevMask
static const int kDevMask
Definition: expr_engine-inl.h:264
mshadow::expr::Exp< Container, DType, type::kRValue >::self
const Container & self(void) const
Definition: expression.h:82
mshadow::Stream
computaion stream structure, used for asynchronous computations
Definition: tensor.h:488
mshadow::expr::slice
SliceExp< SrcExp, Device, DType, srcdim, srcdim - sdim > slice(const TRValue< SrcExp, Device, srcdim, DType > &src, index_t begin, index_t end)
Slice a Tensor.
Definition: slice.h:83
mshadow::expr::SliceExp::shape_
Shape< srcdim > shape_
Definition: slice.h:49
mshadow::TRValue
Tensor RValue, this is the super type of all kinds of possible tensors.
Definition: tensor.h:514
mshadow::expr::TypeCheckPass
used to help static type check
Definition: expr_engine-inl.h:330
mshadow::expr::Plan< SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice >, DType >::REval
MSHADOW_XINLINE DType & REval(index_t i, index_t j)
Definition: slice.h:139
mshadow::expr::SliceExp::dimslice
static const int dimslice
Definition: slice.h:45
mshadow::expr::Plan< SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice >, DType >::Plan
Plan(const SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > &e)
Definition: slice.h:127
mshadow::expr::StreamInfo::Get
static Stream< Device > * Get(const E &t)
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::ShapeCheck
runtime shape checking template get the shape of an expression, report error if shape mismatch
Definition: expr_engine-inl.h:364
mshadow::expr::StreamInfo
Definition: expr_engine-inl.h:345
mshadow::expr::Plan< SliceExp< SrcExp, Device, DType, srcdim, 1 >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: slice.h:161
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::ExpInfo
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:262
mshadow::expr::MakePlan
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:239
mshadow::Shape::shape_
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:86
mshadow::expr::ShapeCheck< srcdim, SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > >::Check
static Shape< srcdim > Check(const SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > &t)
Definition: slice.h:96
mshadow::expr::SliceExp
slice expression, slice a tensor's channel
Definition: slice.h:41
mshadow::expr::StreamInfo< Device, SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > >::Get
static Stream< Device > * Get(const SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > &t)
Definition: slice.h:106
mshadow::expr::ExpInfo::kDim
static const int kDim
Definition: expr_engine-inl.h:263
mshadow::expr::SliceExp::operator=
void operator=(const DType &exp)
Definition: slice.h:64
mshadow::index_t
int32_t index_t
type that will be used for index
Definition: base.h:328
mshadow::expr::Plan
Definition: expr_engine-inl.h:58
mshadow::expr::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
mshadow::expr::SliceExp::src_
const SrcExp & src_
Definition: slice.h:46
mshadow::expr::SliceExp::operator=
void operator=(const expr::Exp< E, DType, etype > &exp)
Definition: slice.h:60
mshadow::expr::Plan< SliceExp< SrcExp, Device, DType, srcdim, 1 >, DType >::Plan
Plan(const SliceExp< SrcExp, Device, DType, srcdim, 1 > &e)
Definition: slice.h:158
mshadow
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::expr::Plan< SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: slice.h:131
mshadow::expr::SliceExp::ch_old_
index_t ch_old_
Definition: slice.h:48
mshadow::Shape< srcdim >
mshadow::expr::RValueExp< SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice >, DType >::__assign
SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > & __assign(DType s)
operator overload
Definition: expression.h:178
mshadow::expr::SliceExp::ch_begin_
index_t ch_begin_
Definition: slice.h:47
mshadow::expr::SliceExp::SliceExp
SliceExp(const SrcExp &src, index_t begin, index_t end)
Definition: slice.h:50