mxnet
broadcast_with_axis.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_BROADCAST_WITH_AXIS_H_
26 #define MSHADOW_EXTENSION_BROADCAST_WITH_AXIS_H_
27 
28 #include <vector>
29 #include "../extension.h"
30 
31 namespace mshadow {
32 namespace expr {
33 
41 template<typename SrcExp, typename DType, int dimsrc, int dimdst>
43  public MakeTensorExp<BroadcastWithAxisExp<SrcExp, DType, dimsrc, dimdst>,
44  SrcExp, dimdst, DType> {
46  const SrcExp &src_;
56  BroadcastWithAxisExp(const SrcExp &src, const int axis, const index_t size)
57  : src_(src), size_(size) {
58  bool keepdim = (dimsrc == dimdst);
60  this->trailing_ = 1;
61 
62  if (!keepdim) {
63  CHECK(dimsrc > axis && axis >= -1) << "broadcast axis (no keepdim) out of bound, " <<
64  "axis must be between -1 and" << dimsrc - 1 << ", given=" << axis << ".";
65  for (int i = 0; i <= axis; ++i) {
66  this->shape_[i] = src_shape[i];
67  }
68  this->shape_[axis + 1] = size_;
69  for (int i = axis + 1; i < dimsrc; ++i) {
70  this->trailing_ *= src_shape[i];
71  this->shape_[i + 1] = src_shape[i];
72  }
73  } else {
74  CHECK(dimdst > axis && axis >= 0) << "broadcast axis (keepdim) out of bound, " <<
75  "axis must be between 0 and" << dimdst - 1 << ", given=" << axis << ".";
76  CHECK_EQ(src_shape[axis], 1U) << "Size of the dimension of the broadcasting axis must be 1" <<
77  " when keepdim is on, src_shape[" << axis << "]=" << src_shape[axis] << ".";
78  for (int i = 0; i <= axis - 1; ++i) {
79  this->shape_[i] = src_shape[i];
80  }
81  this->shape_[axis] = size_;
82  for (int i = axis + 1; i < dimdst; ++i) {
83  this->trailing_ *= src_shape[i];
84  this->shape_[i] = src_shape[i];
85  }
86  }
87 
88  this->last_ = src_shape[dimsrc - 1];
89  this->dst_last_ = this->shape_[dimdst - 1];
90  }
91 }; // struct BroadcastWithAxisExp
92 
99 template<typename SrcExp, typename DType, int etype>
100 inline BroadcastWithAxisExp<SrcExp, DType, ExpInfo<SrcExp>::kDim,
102 broadcast_with_axis(const Exp<SrcExp, DType, etype> &src, const int axis, const index_t size) {
104  ExpInfo<SrcExp>::kDim + 1>(src.self(), axis, size);
105 }
106 
113 template<typename SrcExp, typename DType, int etype>
114 inline BroadcastWithAxisExp<SrcExp, DType, ExpInfo<SrcExp>::kDim,
116  broadcast_keepdim(const Exp<SrcExp, DType, etype> &src, const int axis, const index_t size) {
118  ExpInfo<SrcExp>::kDim>(src.self(), axis, size);
119 }
120 
129 template<typename SrcExp, typename DType, int dimsrc>
131  public MakeTensorExp<BroadcastWithMultiAxesExp<SrcExp, DType, dimsrc>,
132  SrcExp, dimsrc, DType> {
134  const SrcExp &src_;
146  template<typename TShape>
147  BroadcastWithMultiAxesExp(const SrcExp &src, const TShape& axes, const TShape& sizes)
148  : src_(src) {
150  CHECK(axes.ndim() == sizes.ndim()) << "ndim of axes and sizes must be equal.";
151  this->axesnum_ = axes.ndim();
152  CHECK(this->axesnum_ <= dimsrc) << "Number of broadcasting axes must be smaller than"
153  "the source ndim, number of axes=" << this->axesnum_ << " dimsrc=" << dimsrc;
154  for (index_t i = 0; i < this->axesnum_; i++) {
155  CHECK(dimsrc > axes[i]) << "broadcast axis (keepdim) out of bound, " <<
156  "all axes must be between 0 and" << dimsrc - 1 << ", given axes[" << i << "] = " << axes[i]
157  << ".";
158  CHECK_EQ(src_shape[axes[i]], 1U) << "Size of the dimension of the broadcasting axis must be 1"
159  << ", src_shape[" << axes[i] << "]=" << src_shape[axes[i]] << ".";
160  if (i < this->axesnum_ - 1) {
161  CHECK(axes[i] < axes[i + 1]) << "The given axes must be in increasing order.";
162  }
163  }
164  for (index_t i = 0; i < dimsrc; i++) {
165  this->shape_[i] = src_shape[i];
166  this->sizes_[i] = 1;
167  this->trailings_[i] = 1;
168  }
169  for (index_t i = 0; i < this->axesnum_; i++) {
170  this->shape_[axes[i]] = sizes[i];
171  this->sizes_[i] = sizes[i];
172  }
173  for (index_t i = 0; i < this->axesnum_; i++) {
174  this->trailings_[i] = 1;
175  for (index_t j = axes[i] + 1; j < dimsrc; ++j) {
176  this->trailings_[i] *= this->shape_[j];
177  }
178  }
179  this->last_ = src_shape[dimsrc - 1];
180  this->dst_last_ = this->shape_[dimsrc - 1];
181  }
182 }; // struct BroadcastWithMultiAxesExp
183 
194 template<typename SrcExp, typename DType, int etype, typename TShape>
195 inline BroadcastWithMultiAxesExp<SrcExp, DType, ExpInfo<SrcExp>::kDim>
197 const TShape &axes, const TShape &sizes) {
199 }
200 
211 template<typename SrcExp, typename DType, int etype, typename TShape>
212 inline BroadcastWithMultiAxesExp<SrcExp, DType, ExpInfo<SrcExp>::kDim>
213 broadcast_to(const Exp<SrcExp, DType, etype> &src, const TShape &target_shape) {
214  static const size_t dimsrc = ExpInfo<SrcExp>::kDim;
215  CHECK_EQ(target_shape.ndim(), dimsrc);
216  std::vector<index_t> axes_vec, sizes_vec;
218  for (size_t i = 0; i < dimsrc; ++i) {
219  if (src_shape[i] != target_shape[i]) {
220  CHECK_EQ(src_shape[i], 1U) << "broadcasting axis must have size 1, received shape="
221  << src_shape << " target_shape=" << target_shape;
222  axes_vec.push_back(i);
223  sizes_vec.push_back(target_shape[i]);
224  }
225  }
226  TShape axes = TShape(axes_vec.begin(), axes_vec.end());
227  TShape sizes = TShape(sizes_vec.begin(), sizes_vec.end());
229 }
230 
231 //----------------------
232 // Execution plan
233 //----------------------
234 template<typename SrcExp, typename DType, int dimsrc, int dimdst>
235 struct Plan<BroadcastWithAxisExp<SrcExp, DType, dimsrc, dimdst>, DType> {
236  public:
238  : src_(MakePlan(e.src_)), dst_last_(e.dst_last_),
239  trailing_(e.trailing_), size_(e.size_), last_(e.last_) {}
240  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
241  index_t x = (i * dst_last_ + j) / trailing_ / size_;
242  index_t y = (i * dst_last_ + j) % trailing_;
243  index_t z = x * trailing_ + y;
244  return src_.Eval(z / last_, z % last_);
245  }
246 
247  private:
248  Plan<SrcExp, DType> src_;
249  const index_t dst_last_, trailing_, size_, last_;
250 };
251 
252 template<typename SrcExp, typename DType, int dimsrc>
253 struct Plan<BroadcastWithMultiAxesExp<SrcExp, DType, dimsrc>, DType> {
254  public:
256  : src_(MakePlan(e.src_)), dst_last_(e.dst_last_), last_(e.last_), axesnum_(e.axesnum_),
257  trailings_(e.trailings_), sizes_(e.sizes_) {}
258  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
259  index_t indx = i * dst_last_ + j;
260  for (index_t p = 0; p < dimsrc; ++p) {
261  if (p >= axesnum_) {
262  break;
263  }
264  indx = (indx / trailings_[p] / sizes_[p]) * trailings_[p] + (indx % trailings_[p]);
265  }
266  return src_.Eval(indx / last_, indx % last_);
267  }
268 
269  private:
270  Plan<SrcExp, DType> src_;
271  const index_t dst_last_, last_, axesnum_;
272  const Shape<dimsrc> trailings_, sizes_;
273 };
274 } // namespace expr
275 } // namespace mshadow
276 #endif // MSHADOW_EXTENSION_BROADCAST_WITH_AXIS_H_
mshadow::expr::BroadcastWithMultiAxesExp::dst_last_
index_t dst_last_
size of the last dimension of dst
Definition: broadcast_with_axis.h:136
mshadow::expr::Plan< BroadcastWithAxisExp< SrcExp, DType, dimsrc, dimdst >, DType >::Plan
Plan(const BroadcastWithAxisExp< SrcExp, DType, dimsrc, dimdst > &e)
Definition: broadcast_with_axis.h:237
mshadow::expr::BroadcastWithAxisExp
Broadcasting the tensor in the given axis. If keepdim is off, insert the broadcasting dim after axis....
Definition: broadcast_with_axis.h:42
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::BroadcastWithAxisExp::size_
index_t size_
new dimension of the broadcasting axis
Definition: broadcast_with_axis.h:52
mshadow::expr::broadcast_to
BroadcastWithMultiAxesExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > broadcast_to(const Exp< SrcExp, DType, etype > &src, const TShape &target_shape)
Broadcasting the tensor to the target shape, dimension of different sizes must be 1 in the original t...
Definition: broadcast_with_axis.h:213
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::BroadcastWithAxisExp::BroadcastWithAxisExp
BroadcastWithAxisExp(const SrcExp &src, const int axis, const index_t size)
Definition: broadcast_with_axis.h:56
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::BroadcastWithMultiAxesExp::BroadcastWithMultiAxesExp
BroadcastWithMultiAxesExp(const SrcExp &src, const TShape &axes, const TShape &sizes)
Definition: broadcast_with_axis.h:147
mshadow::expr::BroadcastWithAxisExp::last_
index_t last_
size of the last dimension of src
Definition: broadcast_with_axis.h:54
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::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< BroadcastWithAxisExp< SrcExp, DType, dimsrc, dimdst >, SrcExp, dimdst, DType >::shape_
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:47
mshadow::expr::BroadcastWithMultiAxesExp::src_
const SrcExp & src_
data oprand
Definition: broadcast_with_axis.h:134
mshadow::expr::BroadcastWithMultiAxesExp::last_
index_t last_
size of the last dimension of src
Definition: broadcast_with_axis.h:144
mshadow::expr::BroadcastWithMultiAxesExp
Broadcasting the tensor in multiple axes. The dimension of the source tensor in the given axes must b...
Definition: broadcast_with_axis.h:130
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::BroadcastWithAxisExp::src_
const SrcExp & src_
data oprand
Definition: broadcast_with_axis.h:46
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::Plan< BroadcastWithAxisExp< SrcExp, DType, dimsrc, dimdst >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: broadcast_with_axis.h:240
mshadow::expr::BroadcastWithAxisExp::trailing_
index_t trailing_
product of the dimensions after the broadcasting axis
Definition: broadcast_with_axis.h:50
mshadow::expr::BroadcastWithMultiAxesExp::trailings_
Shape< dimsrc > trailings_
product of the dimensions after the broadcasting axses
Definition: broadcast_with_axis.h:140
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::broadcast_with_axis
BroadcastWithAxisExp< SrcExp, DType, ExpInfo< SrcExp >::kDim, ExpInfo< SrcExp >::kDim+1 > broadcast_with_axis(const Exp< SrcExp, DType, etype > &src, const int axis, const index_t size)
Broadcasting the tensor after given axis.
Definition: broadcast_with_axis.h:102
mshadow::Shape< dimsrc >
mshadow::expr::BroadcastWithMultiAxesExp::sizes_
Shape< dimsrc > sizes_
new dimension of the broadcasting axes
Definition: broadcast_with_axis.h:142
mshadow::expr::broadcast_keepdim
BroadcastWithAxisExp< SrcExp, DType, ExpInfo< SrcExp >::kDim, ExpInfo< SrcExp >::kDim > broadcast_keepdim(const Exp< SrcExp, DType, etype > &src, const int axis, const index_t size)
Broadcasting the tensor in the given axis (keepdim turned on)
Definition: broadcast_with_axis.h:116
mshadow::expr::Plan< BroadcastWithMultiAxesExp< SrcExp, DType, dimsrc >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: broadcast_with_axis.h:258
mshadow::expr::BroadcastWithAxisExp::dst_last_
index_t dst_last_
size of the last dimension of dst
Definition: broadcast_with_axis.h:48
mshadow::expr::BroadcastWithMultiAxesExp::axesnum_
index_t axesnum_
number of broadcasting axes
Definition: broadcast_with_axis.h:138
mshadow::expr::Plan< BroadcastWithMultiAxesExp< SrcExp, DType, dimsrc >, DType >::Plan
Plan(const BroadcastWithMultiAxesExp< SrcExp, DType, dimsrc > &e)
Definition: broadcast_with_axis.h:255
mshadow::expr::broadcast_multi_axes
BroadcastWithMultiAxesExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > broadcast_multi_axes(const Exp< SrcExp, DType, etype > &src, const TShape &axes, const TShape &sizes)
Broadcasting the tensor in the given axis (keepdim turned on)
Definition: broadcast_with_axis.h:196