mxnet
pad.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_PAD_H_
8 #define MSHADOW_EXTENSION_PAD_H_
9 #include "../extension.h"
10 namespace mshadow {
11 namespace expr {
18 template<typename SrcExp, typename DType, int srcdim>
19 struct PaddingExp:
20  public MakeTensorExp<PaddingExp<SrcExp, DType, srcdim>,
21  SrcExp, srcdim, DType> {
23  const SrcExp &src_;
33  PaddingExp(const SrcExp &src, index_t pad_y, index_t pad_x)
34  : src_(src), pad_y_(pad_y), pad_x_(pad_x) {
36  src_height_ = this->shape_[srcdim - 2];
37  src_width_ = this->shape_[srcdim - 1];
38  this->shape_[srcdim - 2] += pad_y * 2; // height
39  this->shape_[srcdim - 1] += pad_x * 2; // width
40  }
41 };
51 template<typename SrcExp, typename DType, int etype>
55  ::Error_Expression_Does_Not_Meet_Dimension_Req();
57 }
68 template<typename SrcExp, typename DType, int etype>
70 pad(const Exp<SrcExp, DType, etype> &src, index_t pad_y, index_t pad_x) {
72  ::Error_Expression_Does_Not_Meet_Dimension_Req();
74  (src.self(), pad_y, pad_x);
75 }
76 //----------------------
77 // Execution plan
78 //----------------------
79 template<typename SrcExp, typename DType, int srcdim>
80 struct Plan<PaddingExp<SrcExp, DType, srcdim>, DType> {
81  public:
83  : src_(MakePlan(e.src_)),
84  pad_y_(e.pad_y_), pad_x_(e.pad_x_),
85  new_height_(e.shape_[srcdim - 2]),
87  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
88  const index_t x = j;
89  const index_t y = i % new_height_;
90  const index_t c = i / new_height_;
91  if (y < pad_y_ || x < pad_x_) return static_cast<DType>(0);
92  const index_t h = y - pad_y_;
93  const index_t w = x - pad_x_;
94  if (h < src_height_ && w < src_width_) {
95  return src_.Eval(c * src_height_ + h, w);
96  } else {
97  return static_cast<DType>(0);
98  }
99  }
100 
101  private:
103  const index_t pad_y_;
104  const index_t pad_x_;
105  const index_t new_height_;
106  const index_t src_height_;
107  const index_t src_width_;
108 };
109 } // namespace expr
110 } // namespace mshadow
111 #endif // MSHADOW_EXTENSION_PAD_H_
const SrcExp & src_
source operand
Definition: pad.h:23
PaddingExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > pad(const Exp< SrcExp, DType, etype > &src, index_t pad)
padding expression, pad a image with zeros on boundaries, padding affects shape[0], and shape[1]
Definition: pad.h:53
Definition: expr_engine-inl.h:40
used to help static type check
Definition: expr_engine-inl.h:312
index_t pad_y_
pad size in y
Definition: pad.h:25
padding expression, pad a image with zeros
Definition: pad.h:19
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:204
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: pad.h:87
int32_t index_t
type that will be used for index
Definition: base.h:291
index_t src_height_
source tensor height
Definition: pad.h:29
index_t src_width_
source tensor width
Definition: pad.h:31
index_t pad_x_
pad size in x
Definition: pad.h:27
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
Plan(const PaddingExp< SrcExp, DType, srcdim > &e)
Definition: pad.h:82
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29
PaddingExp(const SrcExp &src, index_t pad_y, index_t pad_x)
constructor
Definition: pad.h:33