mxnet
concat.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_CONCAT_H_
26 #define MSHADOW_EXTENSION_CONCAT_H_
27 
28 #include "../extension.h"
29 
30 namespace mshadow {
31 namespace expr {
40 template<typename LhsExp, typename RhsExp,
41  typename Device, typename DType,
42  int srcdim, int dimsrc_m_cat>
43 struct ConcatExp : public TRValue<ConcatExp<LhsExp, RhsExp,
44  Device, DType,
45  srcdim, dimsrc_m_cat>,
46  Device, srcdim, DType> {
47  static const int dimcat = srcdim - dimsrc_m_cat;
48  const LhsExp &src1_;
49  const RhsExp &src2_;
53  ConcatExp(const LhsExp &src1, const RhsExp &src2) : src1_(src1), src2_(src2) {
56  #pragma unroll
57  for (int i = 0; i < srcdim; ++i) {
58  if (i != dimcat) {
59  CHECK_EQ(sshape1[i], sshape2[i]) << "ConcatExp: shape mismatch";
60  }
61  }
62  this->shape_ = sshape1;
63  this->shape_[dimcat] = sshape1[dimcat] + sshape2[dimcat];
64  this->dcat_src1_ = sshape1[dimcat];
65  this->dcat_src2_ = sshape2[dimcat];
66  }
67  template<typename E, int etype>
68  inline void
70  this->__assign(exp);
71  }
72  inline void
73  operator=(const DType &exp) {
74  this->__assign(exp);
75  }
76 }; // struct ConcatExp
87 template<int cdim, typename LhsExp, typename RhsExp,
88  typename Device, typename DType, int srcdim>
89 inline ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, srcdim - cdim>
93  ::Error_Expression_Does_Not_Meet_Dimension_Req();
95  ::Error_Expression_Does_Not_Meet_Dimension_Req();
96  return ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, srcdim - cdim>
97  (src1.self(), src2.self());
98 }
99 //------------------------
100 // engine plugin
101 //------------------------
102 // runtime shapecheck
103 template<typename LhsExp, typename RhsExp,
104  typename Device, typename DType,
105  int srcdim, int dimsrc_m_cat>
106 struct ShapeCheck<srcdim, ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat> >{
107  inline static Shape<srcdim> Check(const ConcatExp<LhsExp, RhsExp,
108  Device, DType, srcdim, dimsrc_m_cat> &t) {
109  return t.shape_;
110  }
111 };
112 template<typename LhsExp, typename RhsExp,
113  typename Device, typename DType,
114  int srcdim, int dimsrc_m_cat>
115 struct StreamInfo<Device, ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat> >{
116  inline static Stream<Device> *
120  if (lhs != rhs) return NULL;
121  return lhs;
122  }
123 };
124 // static typecheck
125 template<typename LhsExp, typename RhsExp,
126  typename Device, typename DType,
127  int srcdim, int dimsrc_m_cat>
128 struct ExpInfo<ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat> >{
129  static const int kDimLhs = ExpInfo<LhsExp>::kDim;
130  static const int kDimRhs = ExpInfo<RhsExp>::kDim;
131  // copy from binarymap
132  static const int kDim = (kDimLhs >= 0 && kDimRhs >= 0) ?\
133  (kDimLhs == 0 ?\
134  kDimRhs :\
135  ((kDimRhs == 0 || kDimLhs == kDimRhs) ? kDimLhs : -1)) : -1;
137 };
138 //----------------------
139 // Execution plan
140 //---------------------
141 template<typename LhsExp, typename RhsExp,
142  typename Device, typename DType,
143  int srcdim, int dimsrc_m_cat>
144 struct Plan<ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat>, DType> {
145  public:
146  static const int dimcat = srcdim - dimsrc_m_cat;
148  : src1_(MakePlan(e.src1_)), src2_(MakePlan(e.src2_)),
149  height_(e.shape_.ProdShape(dimcat + 1, srcdim - 1)),
150  ch_src1_(e.dcat_src1_), ch_src2_(e.dcat_src2_), ch_(e.shape_[dimcat]) {}
151  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
152  const index_t y = i % height_;
153  i /= height_;
154  const index_t c = i % ch_;
155  const index_t b = i / ch_;
156  const index_t x = j;
157  if (c < ch_src1_) {
158  return src1_.Eval((b * ch_src1_ + c) * height_ + y, x);
159  } else {
160  return src2_.Eval((b * ch_src2_ + c - ch_src1_) * height_ + y, x);
161  }
162  }
164  const index_t y = i % height_;
165  i /= height_;
166  const index_t c = i % ch_;
167  const index_t b = i / ch_;
168  const index_t x = j;
169  if (c < ch_src1_) {
170  return src1_.REval((b * ch_src1_ + c) * height_ + y, x);
171  } else {
172  return src2_.REval((b * ch_src2_ + c - ch_src1_) * height_ + y, x);
173  }
174  }
175 
176  private:
179  const index_t height_, ch_src1_, ch_src2_, ch_;
180 }; // struct Plan
181 
182 // specialize for concat in x
183 template<typename LhsExp, typename RhsExp,
184  typename Device, typename DType,
185  int srcdim>
186 struct Plan<ConcatExp<LhsExp, RhsExp, Device, DType, srcdim, 1>, DType> {
187  public:
189  : src1_(MakePlan(e.src1_)), src2_(MakePlan(e.src2_)),
190  width_src1_(e.dcat_src1_) {}
191  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
192  if (x < width_src1_) {
193  return src1_.Eval(y, x);
194  } else {
195  return src2_.Eval(y, x - width_src1_);
196  }
197  }
199  if (x < width_src1_) {
200  return src1_.REval(y, x);
201  } else {
202  return src2_.REval(y, x - width_src1_);
203  }
204  }
205 
206  private:
209  const index_t width_src1_;
210 };
211 } // namespace expr
212 } // namespace mshadow
213 #endif // MSHADOW_EXTENSION_CONCAT_H_
static Stream< Device > * Get(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > &t)
Definition: concat.h:117
index_t dcat_src2_
Definition: concat.h:51
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: concat.h:191
Tensor RValue, this is the super type of all kinds of possible tensors.
Definition: tensor.h:410
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
static const int dimcat
Definition: concat.h:47
ConcatExp(const LhsExp &src1, const RhsExp &src2)
Definition: concat.h:53
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: concat.h:151
void operator=(const expr::Exp< E, DType, etype > &exp)
Definition: concat.h:69
const LhsExp & src1_
Definition: concat.h:48
index_t dcat_src1_
Definition: concat.h:50
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:223
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:263
Definition: expr_engine-inl.h:346
int32_t index_t
type that will be used for index
Definition: base.h:336
Plan(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > &e)
Definition: concat.h:147
Plan(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, 1 > &e)
Definition: concat.h:188
const RhsExp & src2_
Definition: concat.h:49
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:76
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:365
MSHADOW_XINLINE DType & REval(index_t i, index_t j)
Definition: concat.h:163
static Stream< Device > * Get(const E &t)
Shape< 4 > shape_
Definition: concat.h:52
void operator=(const DType &exp)
Definition: concat.h:73
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:90
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
const Container & self(void) const
Definition: expression.h:83
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:240
overloaded + operator between half_t and bf16_t
Definition: base.h:327
concat expression, concat two tensor&#39;s channel
Definition: concat.h:43
ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > & __assign(DType s)
operator overload
Definition: expression.h:179
MSHADOW_XINLINE DType & REval(index_t y, index_t x)
Definition: concat.h:198
static Shape< srcdim > Check(const ConcatExp< LhsExp, RhsExp, Device, DType, srcdim, dimsrc_m_cat > &t)
Definition: concat.h:107
computaion stream structure, used for asynchronous computations
Definition: tensor.h:384