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 
25 #ifndef MSHADOW_EXTENSION_SLICE_H_
26 #define MSHADOW_EXTENSION_SLICE_H_
27 
28 #include "../extension.h"
29 
30 namespace mshadow {
31 namespace expr {
39 template<typename SrcExp,
40  typename Device, typename DType,
41  int srcdim, int dimsrc_m_slice>
42 struct SliceExp : public TRValue<SliceExp<SrcExp,
43  Device, DType,
44  srcdim, dimsrc_m_slice>,
45  Device, srcdim, DType> {
46  static const int dimslice = srcdim - dimsrc_m_slice;
47  const SrcExp &src_;
51  SliceExp(const SrcExp &src, index_t begin, index_t end)
52  : src_(src), ch_begin_(begin) {
53  shape_ = ShapeCheck<srcdim, SrcExp>::Check(src_);
54  ch_old_ = shape_[dimslice];
55  CHECK(begin <= shape_[dimslice] && end <= shape_[dimslice])
56  << "The slice went out of range. ";
57  shape_[dimslice] = end - begin;
58  }
59  template<typename E, int etype>
60  inline void
62  this->__assign(exp);
63  }
64  inline void
65  operator=(const DType &exp) {
66  this->__assign(exp);
67  }
68 }; // struct Slice
69 
81 template<int sdim, typename SrcExp,
82  typename Device, typename DType, int srcdim>
83 inline SliceExp<SrcExp, Device, DType, srcdim, srcdim - sdim>
86  ::Error_Expression_Does_Not_Meet_Dimension_Req();
87  return SliceExp<SrcExp, Device, DType, srcdim, srcdim - sdim>(src.self(), begin, end);
88 }
89 //------------------------
90 // engine plugin
91 //------------------------
92 // runtime shapecheck
93 template<typename SrcExp,
94  typename Device, typename DType,
95  int srcdim, int dimsrc_m_slice>
96 struct ShapeCheck<srcdim, SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice> >{
97  inline static Shape<srcdim> Check(const SliceExp<SrcExp,
98  Device, DType, srcdim, dimsrc_m_slice> &t) {
99  return t.shape_;
100  }
101 };
102 template<typename SrcExp,
103  typename Device, typename DType,
104  int srcdim, int dimsrc_m_slice>
105 struct StreamInfo<Device, SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice> >{
106  inline static Stream<Device> *
109  }
110 };
111 // static typecheck
112 template<typename SrcExp,
113  typename Device, typename DType,
114  int srcdim, int dimsrc_m_slice>
115 struct ExpInfo<SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice> >{
116  static const int kDim = ExpInfo<SrcExp>::kDim;
117  static const int kDevMask = ExpInfo<SrcExp>::kDevMask;
118 };
119 //----------------------
120 // Execution plan
121 //---------------------
122 template<typename SrcExp,
123  typename Device, typename DType,
124  int srcdim, int dimsrc_m_slice>
125 struct Plan<SliceExp<SrcExp, Device, DType, srcdim, dimsrc_m_slice>, DType> {
126  public:
127  static const int dimslice = srcdim - dimsrc_m_slice;
129  : src_(MakePlan(e.src_)),
130  height_(e.shape_.ProdShape(dimslice + 1, srcdim - 1)),
131  ch_begin_(e.ch_begin_), ch_old_(e.ch_old_), ch_(e.shape_[dimslice]) {}
132  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
133  const index_t y = i % height_;
134  i /= height_;
135  const index_t c = i % ch_ + ch_begin_;
136  const index_t b = i / ch_;
137  const index_t x = j;
138  return src_.Eval((b * ch_old_ + c) * height_ + y, x);
139  }
141  const index_t y = i % height_;
142  i /= height_;
143  const index_t c = i % ch_ + ch_begin_;
144  const index_t b = i / ch_;
145  const index_t x = j;
146  return src_.REval((b * ch_old_ + c) * height_ + y, x);
147  }
148 
149  private:
151  const index_t height_, ch_begin_, ch_old_, ch_;
152 }; // struct Plan
153 
154 template<typename SrcExp,
155  typename Device, typename DType,
156  int srcdim>
157 struct Plan<SliceExp<SrcExp, Device, DType, srcdim, 1>, DType> {
158  public:
160  : src_(MakePlan(e.src_)),
161  ch_begin_(e.ch_begin_) {}
162  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
163  return src_.Eval(y, x + ch_begin_);
164  }
166  return src_.REval(y, x + ch_begin_);
167  }
168 
169  private:
171  const index_t ch_begin_;
172 };
173 } // namespace expr
174 } // namespace mshadow
175 #endif // MSHADOW_EXTENSION_SLICE_H_
Tensor RValue, this is the super type of all kinds of possible tensors.
Definition: tensor.h:410
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
static const int dimslice
Definition: slice.h:46
void operator=(const expr::Exp< E, DType, etype > &exp)
Definition: slice.h:61
Plan(const SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > &e)
Definition: slice.h:128
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: slice.h:132
static Shape< dim > Check(const E &t)
index_t ch_old_
Definition: slice.h:49
MSHADOW_XINLINE DType & REval(index_t y, index_t x)
Definition: slice.h:165
#define MSHADOW_XINLINE
Definition: base.h:223
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:84
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:263
Definition: expr_engine-inl.h:346
static Shape< srcdim > Check(const SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > &t)
Definition: slice.h:97
int32_t index_t
type that will be used for index
Definition: base.h:336
void operator=(const DType &exp)
Definition: slice.h:65
const SrcExp & src_
Definition: slice.h:47
Plan(const SliceExp< SrcExp, Device, DType, srcdim, 1 > &e)
Definition: slice.h:159
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:76
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:365
Shape< srcdim > shape_
Definition: slice.h:50
static Stream< Device > * Get(const E &t)
MSHADOW_XINLINE DType & REval(index_t i, index_t j)
Definition: slice.h:140
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: slice.h:162
SliceExp(const SrcExp &src, index_t begin, index_t end)
Definition: slice.h:51
const Container & self(void) const
Definition: expression.h:83
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:240
overloaded + operator between half_t and bf16_t
Definition: base.h:327
index_t ch_begin_
Definition: slice.h:48
slice expression, slice a tensor&#39;s channel
Definition: slice.h:42
SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > & __assign(DType s)
operator overload
Definition: expression.h:179
static Stream< Device > * Get(const SliceExp< SrcExp, Device, DType, srcdim, dimsrc_m_slice > &t)
Definition: slice.h:107
computaion stream structure, used for asynchronous computations
Definition: tensor.h:384