mxnet
reshape.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_RESHAPE_H_
8 #define MSHADOW_EXTENSION_RESHAPE_H_
9 #include "../extension.h"
10 namespace mshadow {
11 namespace expr {
20 template<typename SrcExp, typename DType, int dimdst, int dimsrc>
21 struct ReshapeExp:
22  public MakeTensorExp<ReshapeExp<SrcExp, DType, dimdst, dimsrc>,
23  SrcExp, dimdst, DType> {
25  const SrcExp &src_;
29  ReshapeExp(const SrcExp &src, Shape<dimdst> shape)
30  : src_(src) {
32  CHECK_EQ(ishape.Size(), shape.Size()) << "reshape size must match";
33  ishapex_ = ishape[dimsrc - 1];
34  this->shape_ = shape;
35  }
36 };
46 template<typename SrcExp, typename DType, int etype, int dimdst>
50  (src.self(), oshape);
51 }
52 //----------------------
53 // Execution plan
54 //----------------------
55 template<typename SrcExp, typename DType, int dimdst, int dimsrc>
56 struct Plan<ReshapeExp<SrcExp, DType, dimdst, dimsrc>, DType> {
57  public:
59  : src_(MakePlan(e.src_)),
60  oshapex_(e.shape_[dimdst - 1]), ishapex_(e.ishapex_) {}
61  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
62  const index_t idx = y * oshapex_ + x;
63  return src_.Eval(idx / ishapex_, idx % ishapex_);
64  }
65 
66  private:
68  const index_t oshapex_, ishapex_;
69 };
70 // special work plan for 1 dimensional data
71 template<typename SrcExp, typename DType, int dimdst>
72 struct Plan<ReshapeExp<SrcExp, DType, dimdst, 1>, DType> {
73  public:
75  : src_(MakePlan(e.src_)), oshapex_(e.shape_[dimdst - 1]) {
76  }
77  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
78  return src_.Eval(0, y * oshapex_ + x);
79  }
80 
81  private:
83  const index_t oshapex_;
84 };
85 } // namespace expr
86 } // namespace mshadow
87 #endif // MSHADOW_EXTENSION_RESHAPE_H_
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: reshape.h:61
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:126
Definition: expr_engine-inl.h:40
shape of a tensor
Definition: tensor.h:35
Plan(const ReshapeExp< SrcExp, DType, dimdst, dimsrc > &e)
Definition: reshape.h:58
Plan(const ReshapeExp< SrcExp, DType, dimdst, 1 > &e)
Definition: reshape.h:74
static Shape< dim > Check(const E &t)
const SrcExp & src_
source expression
Definition: reshape.h:25
#define MSHADOW_XINLINE
Definition: base.h:204
index_t ishapex_
smallest dimension of input
Definition: reshape.h:27
int32_t index_t
type that will be used for index
Definition: base.h:291
ReshapeExp< SrcExp, DType, dimdst, ExpInfo< SrcExp >::kDim > reshape(const Exp< SrcExp, DType, etype > &src, Shape< dimdst > oshape)
a expression that reshapes a tensor to another shape
Definition: reshape.h:48
reshape the content to another shape input: Tensor<Device,dimsrc>: ishape output: Tensor<Device...
Definition: reshape.h:21
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
ReshapeExp(const SrcExp &src, Shape< dimdst > shape)
constructor
Definition: reshape.h:29
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
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: reshape.h:77
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29