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