mxnet
flip.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 
26 #ifndef MSHADOW_EXTENSION_FLIP_H_
27 #define MSHADOW_EXTENSION_FLIP_H_
28 
29 #include "../extension.h"
30 
31 namespace mshadow {
32 namespace expr {
40 template<typename SrcExp, typename Device,
41  typename DType, int srcdim>
42 struct FlipExp : public TRValue<FlipExp<SrcExp,
43  Device, DType,
44  srcdim>,
45  Device, srcdim, DType> {
46  const SrcExp &src_;
51  FlipExp(const SrcExp &src, int dim)
52  : src_(src) {
53  shape_ = ShapeCheck<srcdim, SrcExp>::Check(src_);
54  stride_ = shape_[dim];
55  stride_j_ = shape_[srcdim-1];
56  trailing_ = 1;
57  for (int i = dim + 1; i < srcdim; ++i) {
58  trailing_ *= shape_[i];
59  }
60  }
61  template<typename E, int etype>
62  inline void
64  this->__assign(exp);
65  }
66  inline void
67  operator=(const DType &exp) {
68  this->__assign(exp);
69  }
70 }; // struct Flip
71 
83 template<typename SrcExp, typename Device,
84  typename DType, int srcdim>
88 }
89 //------------------------
90 // engine plugin
91 //------------------------
92 // runtime shapecheck
93 template<typename SrcExp, typename Device,
94  typename DType, int srcdim>
95 struct ShapeCheck<srcdim, FlipExp<SrcExp, Device, DType, srcdim> >{
96  inline static Shape<srcdim> Check(const FlipExp<SrcExp,
97  Device, DType, srcdim> &t) {
98  return t.shape_;
99  }
100 };
101 template<typename SrcExp, typename Device,
102  typename DType, int srcdim>
103 struct StreamInfo<Device, FlipExp<SrcExp, Device, DType, srcdim> >{
104  inline static Stream<Device> *
107  }
108 };
109 // static typecheck
110 template<typename SrcExp, typename Device,
111  typename DType, int srcdim>
112 struct ExpInfo<FlipExp<SrcExp, Device, DType, srcdim> >{
113  static const int kDim = ExpInfo<SrcExp>::kDim;
114  static const int kDevMask = ExpInfo<SrcExp>::kDevMask;
115 };
116 //----------------------
117 // Execution plan
118 //---------------------
119 template<typename SrcExp, typename Device,
120  typename DType, int srcdim>
121 struct Plan<FlipExp<SrcExp, Device, DType, srcdim>, DType> {
122  public:
126  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
127  index_t idx = i*stride_j_+j;
128  const index_t low = idx%trailing_;
129  index_t high = idx/trailing_;
130  const index_t x = high%stride_;
131  high /= stride_;
132  idx = (high*stride_+stride_-1-x)*trailing_+low;
133  return src_.Eval(idx/stride_j_, idx%stride_j_);
134  }
135  MSHADOW_XINLINE DType &REval(index_t i, index_t j) const {
136  index_t idx = i*stride_j_+j;
137  const index_t low = idx%trailing_;
138  index_t high = idx/trailing_;
139  const index_t x = high%stride_;
140  high /= stride_;
141  idx = (high*stride_+stride_-1-x)*trailing_+low;
142  return src_.REval(idx/stride_j_, idx%stride_j_);
143  }
144 
145  private:
148 }; // struct Plan
149 } // namespace expr
150 } // namespace mshadow
151 #endif // MSHADOW_EXTENSION_FLIP_H_
index_t stride_
Definition: flip.h:48
MSHADOW_XINLINE DType & REval(index_t i, index_t j) const
Definition: flip.h:135
Tensor RValue, this is the super type of all kinds of possible tensors.
Definition: tensor.h:410
index_t trailing_
Definition: flip.h:47
Definition: expr_engine-inl.h:59
static Stream< Device > * Get(const FlipExp< SrcExp, Device, DType, srcdim > &t)
Definition: flip.h:105
FlipExp< SrcExp, Device, DType, srcdim > flip(const TRValue< SrcExp, Device, srcdim, DType > &src, int dim)
Flip a Tensor.
Definition: flip.h:86
static Shape< dim > Check(const E &t)
FlipExp(const SrcExp &src, int dim)
Definition: flip.h:51
#define MSHADOW_XINLINE
Definition: base.h:223
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
int32_t index_t
type that will be used for index
Definition: base.h:336
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: flip.h:126
const SrcExp & src_
Definition: flip.h:46
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
void operator=(const expr::Exp< E, DType, etype > &exp)
Definition: flip.h:63
static Stream< Device > * Get(const E &t)
Plan(const FlipExp< SrcExp, Device, DType, srcdim > &e)
Definition: flip.h:123
void operator=(const DType &exp)
Definition: flip.h:67
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
const Container & self(void) const
Definition: expression.h:83
slice expression, slice a tensor&#39;s channel
Definition: flip.h:42
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
FlipExp< SrcExp, Device, DType, srcdim > & __assign(DType s)
operator overload
Definition: expression.h:179
Shape< srcdim > shape_
Definition: flip.h:50
static Shape< srcdim > Check(const FlipExp< SrcExp, Device, DType, srcdim > &t)
Definition: flip.h:96
index_t stride_j_
Definition: flip.h:49
computaion stream structure, used for asynchronous computations
Definition: tensor.h:384