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