mxnet
concat.h
Go to the documentation of this file.
1 
6 #ifndef MSHADOW_EXTENSION_CONCAT_H_
7 #define MSHADOW_EXTENSION_CONCAT_H_
8 
9 #include "../extension.h"
10 
11 namespace mshadow {
12 namespace expr {
21 template<typename LhsExp, typename RhsExp,
22  typename Device, typename DType,
23  int srcdim, int dimsrc_m_cat>
24 struct ConcatExp : public TRValue<ConcatExp<LhsExp, RhsExp,
25  Device, DType,
26  srcdim, dimsrc_m_cat>,
27  Device, srcdim, DType> {
28  static const int dimcat = srcdim - dimsrc_m_cat;
29  const LhsExp &src1_;
30  const RhsExp &src2_;
34  ConcatExp(const LhsExp &src1, const RhsExp &src2) : src1_(src1), src2_(src2) {
37  #pragma unroll
38  for (int i = 0; i < srcdim; ++i) {
39  if (i != dimcat) {
40  CHECK_EQ(sshape1[i], sshape2[i]) << "ConcatExp: shape mismatch";
41  }
42  }
43  this->shape_ = sshape1;
44  this->shape_[dimcat] = sshape1[dimcat] + sshape2[dimcat];
45  this->dcat_src1_ = sshape1[dimcat];
46  this->dcat_src2_ = sshape2[dimcat];
47  }
48  template<typename E, int etype>
49  inline void
51  this->__assign(exp);
52  }
53  inline void
54  operator=(const DType &exp) {
55  this->__assign(exp);
56  }
57 }; // struct ConcatExp
68 template<int cdim, typename LhsExp, typename RhsExp,
69  typename Device, typename DType, int srcdim>
70 inline ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, srcdim - cdim>
74  ::Error_Expression_Does_Not_Meet_Dimension_Req();
76  ::Error_Expression_Does_Not_Meet_Dimension_Req();
77  return ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, srcdim - cdim>
78  (src1.self(), src2.self());
79 }
80 //------------------------
81 // engine plugin
82 //------------------------
83 // runtime shapecheck
84 template<typename LhsExp, typename RhsExp,
85  typename Device, typename DType,
86  int srcdim, int dimsrc_m_cat>
87 struct ShapeCheck<srcdim, ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat> >{
88  inline static Shape<srcdim> Check(const ConcatExp<LhsExp, RhsExp,
89  Device, DType, srcdim, dimsrc_m_cat> &t) {
90  return t.shape_;
91  }
92 };
93 template<typename LhsExp, typename RhsExp,
94  typename Device, typename DType,
95  int srcdim, int dimsrc_m_cat>
96 struct StreamInfo<Device, ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat> >{
97  inline static Stream<Device> *
101  if (lhs != rhs) return NULL;
102  return lhs;
103  }
104 };
105 // static typecheck
106 template<typename LhsExp, typename RhsExp,
107  typename Device, typename DType,
108  int srcdim, int dimsrc_m_cat>
109 struct ExpInfo<ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat> >{
110  static const int kDimLhs = ExpInfo<LhsExp>::kDim;
111  static const int kDimRhs = ExpInfo<RhsExp>::kDim;
112  // copy from binarymap
113  static const int kDim = (kDimLhs >= 0 && kDimRhs >= 0) ?\
114  (kDimLhs == 0 ?\
115  kDimRhs :\
116  ((kDimRhs == 0 || kDimLhs == kDimRhs) ? kDimLhs : -1)) : -1;
118 };
119 //----------------------
120 // Execution plan
121 //---------------------
122 template<typename LhsExp, typename RhsExp,
123  typename Device, typename DType,
124  int srcdim, int dimsrc_m_cat>
125 struct Plan<ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat>, DType> {
126  public:
127  static const int dimcat = srcdim - dimsrc_m_cat;
129  : src1_(MakePlan(e.src1_)), src2_(MakePlan(e.src2_)),
130  height_(e.shape_.ProdShape(dimcat + 1, srcdim - 1)),
131  ch_src1_(e.dcat_src1_), ch_src2_(e.dcat_src2_), ch_(e.shape_[dimcat]) {}
132  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
133  const index_t y = i % height_;
134  i /= height_;
135  const index_t c = i % ch_;
136  const index_t b = i / ch_;
137  const index_t x = j;
138  if (c < ch_src1_) {
139  return src1_.Eval((b * ch_src1_ + c) * height_ + y, x);
140  } else {
141  return src2_.Eval((b * ch_src2_ + c - ch_src1_) * height_ + y, x);
142  }
143  }
145  const index_t y = i % height_;
146  i /= height_;
147  const index_t c = i % ch_;
148  const index_t b = i / ch_;
149  const index_t x = j;
150  if (c < ch_src1_) {
151  return src1_.REval((b * ch_src1_ + c) * height_ + y, x);
152  } else {
153  return src2_.REval((b * ch_src2_ + c - ch_src1_) * height_ + y, x);
154  }
155  }
156 
157  private:
160  const index_t height_, ch_src1_, ch_src2_, ch_;
161 }; // struct Plan
162 
163 // specialize for concat in x
164 template<typename LhsExp, typename RhsExp,
165  typename Device, typename DType,
166  int srcdim>
167 struct Plan<ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, 1>, DType> {
168  public:
170  : src1_(MakePlan(e.src1_)), src2_(MakePlan(e.src2_)),
171  width_src1_(e.dcat_src1_) {}
172  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
173  if (x < width_src1_) {
174  return src1_.Eval(y, x);
175  } else {
176  return src2_.Eval(y, x - width_src1_);
177  }
178  }
180  if (x < width_src1_) {
181  return src1_.REval(y, x);
182  } else {
183  return src2_.REval(y, x - width_src1_);
184  }
185  }
186 
187  private:
190  const index_t width_src1_;
191 };
192 } // namespace expr
193 } // namespace mshadow
194 #endif // MSHADOW_EXTENSION_CONCAT_H_
static Stream< Device > * Get(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > &t)
Definition: concat.h:98
index_t dcat_src2_
Definition: concat.h:32
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: concat.h:172
Tensor RValue, this is the super type of all kinds of possible tensors.
Definition: tensor.h:391
Definition: expr_engine-inl.h:40
used to help static type check
Definition: expr_engine-inl.h:312
static const int dimcat
Definition: concat.h:28
ConcatExp(const LhsExp &src1, const RhsExp &src2)
Definition: concat.h:34
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: concat.h:132
void operator=(const expr::Exp< E, DType, etype > &exp)
Definition: concat.h:50
const LhsExp & src1_
Definition: concat.h:29
index_t dcat_src1_
Definition: concat.h:31
static Shape< dim > Check(const E &t)
#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
Definition: expr_engine-inl.h:327
int32_t index_t
type that will be used for index
Definition: base.h:291
Plan(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > &e)
Definition: concat.h:128
Plan(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, 1 > &e)
Definition: concat.h:169
const RhsExp & src2_
Definition: concat.h:30
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:57
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:346
MSHADOW_XINLINE DType & REval(index_t i, index_t j)
Definition: concat.h:144
static Stream< Device > * Get(const E &t)
Shape< 4 > shape_
Definition: concat.h:33
void operator=(const DType &exp)
Definition: concat.h:54
ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, srcdim-cdim > concat(const TRValue< LhsExp, Device, srcdim, DType > &src1, const TRValue< RhsExp, Device, srcdim, DType > &src2)
concat two 4D tensor
Definition: concat.h:71
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
const Container & 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
namespace for mshadow
Definition: base.h:282
concat expression, concat two tensor&#39;s channel
Definition: concat.h:24
ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > & __assign(DType s)
operator overload
Definition: expression.h:160
MSHADOW_XINLINE DType & REval(index_t y, index_t x)
Definition: concat.h:179
static Shape< srcdim > Check(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > &t)
Definition: concat.h:88
computaion stream structure, used for asynchronous computations
Definition: tensor.h:365