mxnet
pad.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_PAD_H_
26 #define MSHADOW_EXTENSION_PAD_H_
27 #include "../extension.h"
28 namespace mshadow {
29 namespace expr {
36 template<typename SrcExp, typename DType, int srcdim>
37 struct PaddingExp:
38  public MakeTensorExp<PaddingExp<SrcExp, DType, srcdim>,
39  SrcExp, srcdim, DType> {
41  const SrcExp &src_;
51  PaddingExp(const SrcExp &src, index_t pad_y, index_t pad_x)
52  : src_(src), pad_y_(pad_y), pad_x_(pad_x) {
54  src_height_ = this->shape_[srcdim - 2];
55  src_width_ = this->shape_[srcdim - 1];
56  this->shape_[srcdim - 2] += pad_y * 2; // height
57  this->shape_[srcdim - 1] += pad_x * 2; // width
58  }
59 };
69 template<typename SrcExp, typename DType, int etype>
70 inline PaddingExp<SrcExp, DType, ExpInfo<SrcExp>::kDim>
73  ::Error_Expression_Does_Not_Meet_Dimension_Req();
75 }
86 template<typename SrcExp, typename DType, int etype>
87 inline PaddingExp<SrcExp, DType, ExpInfo<SrcExp>::kDim>
88 pad(const Exp<SrcExp, DType, etype> &src, index_t pad_y, index_t pad_x) {
90  ::Error_Expression_Does_Not_Meet_Dimension_Req();
92  (src.self(), pad_y, pad_x);
93 }
94 //----------------------
95 // Execution plan
96 //----------------------
97 template<typename SrcExp, typename DType, int srcdim>
98 struct Plan<PaddingExp<SrcExp, DType, srcdim>, DType> {
99  public:
101  : src_(MakePlan(e.src_)),
102  pad_y_(e.pad_y_), pad_x_(e.pad_x_),
103  new_height_(e.shape_[srcdim - 2]),
104  src_height_(e.src_height_), src_width_(e.src_width_) {}
105  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
106  const index_t x = j;
107  const index_t y = i % new_height_;
108  const index_t c = i / new_height_;
109  if (y < pad_y_ || x < pad_x_) return static_cast<DType>(0);
110  const index_t h = y - pad_y_;
111  const index_t w = x - pad_x_;
112  if (h < src_height_ && w < src_width_) {
113  return src_.Eval(c * src_height_ + h, w);
114  } else {
115  return static_cast<DType>(0);
116  }
117  }
118 
119  private:
120  Plan<SrcExp, DType> src_;
121  const index_t pad_y_;
122  const index_t pad_x_;
123  const index_t new_height_;
124  const index_t src_height_;
125  const index_t src_width_;
126 };
127 } // namespace expr
128 } // namespace mshadow
129 #endif // MSHADOW_EXTENSION_PAD_H_
mshadow::expr::PaddingExp::pad_x_
index_t pad_x_
pad size in x
Definition: pad.h:45
mshadow::expr::PaddingExp::src_width_
index_t src_width_
source tensor width
Definition: pad.h:49
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::TypeCheckPass
used to help static type check
Definition: expr_engine-inl.h:330
mshadow::expr::PaddingExp::pad_y_
index_t pad_y_
pad size in y
Definition: pad.h:43
mshadow::expr::PaddingExp::src_
const SrcExp & src_
source operand
Definition: pad.h:41
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::Plan< PaddingExp< SrcExp, DType, srcdim >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: pad.h:105
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::expr::MakeTensorExp< PaddingExp< SrcExp, DType, srcdim >, SrcExp, srcdim, DType >::shape_
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:47
mshadow::expr::PaddingExp
padding expression, pad a image with zeros
Definition: pad.h:37
mshadow::expr::Plan< PaddingExp< SrcExp, DType, srcdim >, DType >::Plan
Plan(const PaddingExp< SrcExp, DType, srcdim > &e)
Definition: pad.h:100
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::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
mshadow::expr::pad
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],...
Definition: pad.h:71
mshadow
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::expr::MakeTensorExp
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:43
mshadow::expr::PaddingExp::src_height_
index_t src_height_
source tensor height
Definition: pad.h:47
mshadow::expr::PaddingExp::PaddingExp
PaddingExp(const SrcExp &src, index_t pad_y, index_t pad_x)
constructor
Definition: pad.h:51