mxnet
swapaxis.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_SWAPAXIS_H_
8 #define MSHADOW_EXTENSION_SWAPAXIS_H_
9 #include <algorithm>
10 #include <utility>
11 #include "../extension.h"
12 namespace mshadow {
13 namespace expr {
25 template<typename SrcExp, typename DType, int dimsrc, int m_a1, int a2>
26 struct SwapAxisExp:
27  public MakeTensorExp<SwapAxisExp<SrcExp, DType, dimsrc, m_a1, a2>,
28  SrcExp, dimsrc, DType> {
29  // decode the a1, a2
30  static const int a1 = dimsrc - m_a1;
32  const SrcExp &src_;
34  explicit SwapAxisExp(const SrcExp &src) : src_(src) {
36  std::swap(this->shape_[a1], this->shape_[a2]);
37  }
38 };
49 template<int a1, int a2, typename SrcExp, typename DType, int etype>
53  typedef ExpInfo<SrcExp> Info;
54  TypeCheckPass<Info::kDim >= a1 + 1 && Info::kDim >= a2 + 1 &&
55  a2 < a1>::Error_Expression_Does_Not_Meet_Dimension_Req();
56  return SwapAxisExp<SrcExp, DType, ExpInfo<SrcExp>::kDim,
57  ExpInfo<SrcExp>::kDim - a1, a2>(src.self());
58 }
59 template<typename SrcExp, typename DType, int dimsrc, int m_a1, int a2>
60 struct Plan<SwapAxisExp<SrcExp, DType, dimsrc, m_a1, a2>, DType> {
61  public:
62  // decode the a1
63  static const int a1 = dimsrc - m_a1;
65  : src_(MakePlan(e.src_)),
66  shapey_(e.shape_.ProdShape(a1 + 1, dimsrc - 1)),
67  shapez_(e.shape_[a1]),
68  shapec_(e.shape_.ProdShape(a2 + 1, a1)),
69  shapen_(e.shape_[a2]) {}
70  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
71  const index_t y = i % shapey_;
72  i /= shapey_;
73  const index_t z = i % shapez_;
74  i /= shapez_;
75  const index_t c = i % shapec_;
76  i /= shapec_;
77  const index_t n = i % shapen_;
78  // swap z and n
79  return src_.Eval(((((i / shapen_) * shapez_ + z) * shapec_ +
80  c) * shapen_ + n) * shapey_ + y, j);
81  }
82 
83  private:
85  const index_t shapey_, shapez_, shapec_, shapen_;
86 };
87 template<typename SrcExp, typename DType, int dimsrc, int a2>
88 struct Plan<SwapAxisExp<SrcExp, DType, dimsrc, 1, a2>, DType> {
89  public:
91  : src_(MakePlan(e.src_)),
92  shapex_(e.shape_[dimsrc - 1]),
93  shapey_(e.shape_.ProdShape(a2 + 1, dimsrc - 1)),
94  shapez_(e.shape_[a2]) {}
95  MSHADOW_XINLINE DType Eval(index_t i, index_t x) const {
96  // swap x and z
97  const index_t y = i % shapey_;
98  i /= shapey_;
99  const index_t z = i % shapez_;
100  const index_t n = i / shapez_;
101  return src_.Eval((n * shapex_ + x) * shapey_ + y , z);
102  }
103 
104  private:
106  const index_t shapex_, shapey_, shapez_;
107 };
108 } // namespace expr
109 } // namespace mshadow
110 #endif // MSHADOW_EXTENSION_SWAPAXIS_H_
Definition: expr_engine-inl.h:40
used to help static type check
Definition: expr_engine-inl.h:312
SwapAxisExp< SrcExp, DType, ExpInfo< SrcExp >::kDim, ExpInfo< SrcExp >::kDim-a1, a2 > swapaxis(const Exp< SrcExp, DType, etype > &src)
a expression that reshapes a tensor to another shape
Definition: swapaxis.h:52
static Shape< dim > Check(const E &t)
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: swapaxis.h:70
#define MSHADOW_XINLINE
Definition: base.h:204
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:244
Plan(const SwapAxisExp< SrcExp, DType, dimsrc, m_a1, a2 > &e)
Definition: swapaxis.h:64
int32_t index_t
type that will be used for index
Definition: base.h:291
const SrcExp & src_
source expression
Definition: swapaxis.h:32
static const int a1
Definition: swapaxis.h:30
Plan(const SwapAxisExp< SrcExp, DType, dimsrc, 1, a2 > &e)
Definition: swapaxis.h:90
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
const SubType & self(void) const
Definition: expression.h:64
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:221
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:25
namespace for mshadow
Definition: base.h:282
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29
SwapAxisExp(const SrcExp &src)
constructor
Definition: swapaxis.h:34
swap two axis of a tensor input: Tensor<Device,dim>: ishape output: Tensor<Device,dimdst> oshape[a1],oshape[a2] = ishape[a2],oshape[a1]
Definition: swapaxis.h:26
MSHADOW_XINLINE DType Eval(index_t i, index_t x) const
Definition: swapaxis.h:95