mxnet
reduce_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 
26 #ifndef MSHADOW_EXTENSION_REDUCE_WITH_AXIS_H_
27 #define MSHADOW_EXTENSION_REDUCE_WITH_AXIS_H_
28 
29 #include "../extension.h"
30 
31 namespace mshadow {
32 namespace expr {
33 
39 template<typename Reducer, typename SrcExp, typename DType, int dimsrc, bool mask, int dimdst>
41  public MakeTensorExp<ReduceWithAxisExp<Reducer, SrcExp, DType, dimsrc, mask, dimdst>,
42  SrcExp, dimdst, DType> {
44  const SrcExp &src_;
54  explicit ReduceWithAxisExp(const SrcExp &src, int axis)
55  : src_(src) {
56  bool keepdim = (dimsrc == dimdst);
57  CHECK(dimsrc > axis) << "reduce axis out of bound";
59  for (int i = 0; i < axis; ++i) {
60  this->shape_[i] = src_shape[i];
61  }
62  this->size_ = src_shape[axis];
63  this->trailing_ = 1;
64  if (!keepdim) {
65  for (int i = axis + 1; i < dimsrc; ++i) {
66  this->trailing_ *= src_shape[i];
67  this->shape_[i - 1] = src_shape[i];
68  }
69  } else {
70  this->shape_[axis] = 1;
71  for (index_t i = axis + 1; i < dimsrc; ++i) {
72  this->trailing_ *= src_shape[i];
73  this->shape_[i] = src_shape[i];
74  }
75  }
76 
77  this->last_ = src_shape[dimsrc - 1];
78  this->last_dst_dim_ = this->shape_[dimdst - 1];
79  }
80 }; // struct ReduceWithAxisExp
81 
90 template<typename Reducer, bool mask, typename SrcExp, typename DType, int etype>
94  return ReduceWithAxisExp<Reducer, SrcExp, DType, ExpInfo<SrcExp>::kDim, mask,
95  ExpInfo<SrcExp>::kDim- 1>(src.self(), axis);
96 }
97 
106 template<typename Reducer, bool mask, typename SrcExp, typename DType, int etype>
107 inline ReduceWithAxisExp<Reducer, SrcExp, DType, ExpInfo<SrcExp>::kDim, mask,
110  return ReduceWithAxisExp<Reducer, SrcExp, DType, ExpInfo<SrcExp>::kDim, mask,
111  ExpInfo<SrcExp>::kDim>(src.self(), axis);
112 }
113 
114 //----------------------
115 // Execution plan
116 //----------------------
117 template<typename Reducer, typename SrcExp, typename DType, int dimsrc, bool mask, int dimdst>
118 struct Plan<ReduceWithAxisExp<Reducer, SrcExp, DType, dimsrc, mask, dimdst>, DType> {
119  public:
122  size_(e.size_), last_(e.last_) {}
123  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
124  index_t x = (i*last_dst_dim_ + j)/trailing_;
125  index_t y = (i*last_dst_dim_ + j)%trailing_;
126 
127  if (mask) {
128  index_t idx = 0;
129  DType res; Reducer::SetInitValue(res);
130  for (index_t k = 0; k < size_; ++k) {
131  index_t z = (x*size_+k)*trailing_+y;
132  DType tmp = res;
133  Reducer::Reduce(res, src_.Eval(z/last_, z%last_));
134  if (tmp != res && !isnan_typed::IsNan(tmp)) {
135  idx = k;
136  }
137  }
138  return static_cast<DType>(static_cast<int>(idx));
139  } else {
140  DType res; Reducer::SetInitValue(res);
141  for (index_t k = 0; k < size_; ++k) {
142  index_t z = (x*size_+k)*trailing_+y;
143  Reducer::Reduce(res, src_.Eval(z/last_, z%last_));
144  }
145  return res;
146  }
147  }
148 
149  private:
152 };
153 } // namespace expr
154 } // namespace mshadow
155 #endif // MSHADOW_EXTENSION_REDUCE_WITH_AXIS_H_
index_t last_
size of last src dimension
Definition: reduce_with_axis.h:52
Definition: expr_engine-inl.h:59
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
MSHADOW_XINLINE bool IsNan(volatile DType val)
Definition: base.h:675
MaskExp< IndexExp, SrcExp, DType > mask(const Exp< IndexExp, DType, e1 > &index, const Exp< SrcExp, DType, e2 > &src)
Definition: mask.h:58
const SrcExp & src_
source oprand
Definition: reduce_with_axis.h:44
int32_t index_t
type that will be used for index
Definition: base.h:336
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: reduce_with_axis.h:123
index_t size_
size of axis dimension
Definition: reduce_with_axis.h:50
ReduceWithAxisExp< Reducer, SrcExp, DType, ExpInfo< SrcExp >::kDim, mask, ExpInfo< SrcExp >::kDim-1 > reduce_with_axis(const Exp< SrcExp, DType, etype > &src, int axis)
reduce out the dimension of src labeled by axis.
Definition: reduce_with_axis.h:93
ReduceWithAxisExp(const SrcExp &src, int axis)
Definition: reduce_with_axis.h:54
reduce out the dimension of src labeled by axis.
Definition: reduce_with_axis.h:40
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
const SubType & 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
ReduceWithAxisExp< Reducer, SrcExp, DType, ExpInfo< SrcExp >::kDim, mask, ExpInfo< SrcExp >::kDim > reduce_keepdim(const Exp< SrcExp, DType, etype > &src, int axis)
reduce out the dimension of src labeled by axis, keepdim turned on.
Definition: reduce_with_axis.h:109
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:44
overloaded + operator between half_t and bf16_t
Definition: base.h:327
Plan(const ReduceWithAxisExp< Reducer, SrcExp, DType, dimsrc, mask, dimdst > &e)
Definition: reduce_with_axis.h:120
index_t trailing_
size of trailing dimensions
Definition: reduce_with_axis.h:48
index_t last_dst_dim_
size of last destination dimension
Definition: reduce_with_axis.h:46