mxnet
transpose.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_TRANSPOSE_H_
26 #define MSHADOW_EXTENSION_TRANSPOSE_H_
27 #include <algorithm>
28 #include "../extension.h"
29 namespace mshadow {
30 namespace expr {
42 template<typename SrcExp, typename DType, int dimsrc>
44  public MakeTensorExp<TransposeExExp<SrcExp, DType, dimsrc>,
45  SrcExp, dimsrc, DType> {
47  const SrcExp &src_;
49  Shape<dimsrc> dst_in_src_stride_; // Holds the corresponding stride of the dst axes in src
52  explicit TransposeExExp(const SrcExp &src, Shape<dimsrc> axes) : src_(src), axes_(axes) {
54  src_stride_ = src_shape[dimsrc - 1];
55  Shape<dimsrc> src_stride;
56  src_stride[dimsrc-1] = 1;
57  for (int i = dimsrc-2; i >= 0; --i) src_stride[i] = src_shape[i+1]*src_stride[i+1];
58  for (int i = 0; i < dimsrc; ++i) {
59  dst_in_src_stride_[i] = src_stride[axes[i]];
60  this->shape_[i] = src_shape[axes[i]];
61  }
62  }
63 };
74 template<typename SrcExp, typename DType, int etype>
75 inline TransposeExExp<SrcExp, DType, ExpInfo<SrcExp>::kDim>
78 }
79 
80 template<typename SrcExp, typename DType, int dimsrc>
81 struct Plan<TransposeExExp<SrcExp, DType, dimsrc>, DType> {
82  public:
84  : src_(MakePlan(e.src_)),
85  src_stride_(e.src_stride_),
86  dst_in_src_stride_(e.dst_in_src_stride_),
87  dst_shape_(e.shape_) {}
88  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
89  index_t idx = j * dst_in_src_stride_[dimsrc - 1];
90  #pragma unroll
91  for (int k = dimsrc-2; k >= 0; --k) {
92  idx += (i % dst_shape_[k]) * dst_in_src_stride_[k];
93  i /= dst_shape_[k];
94  }
95  return src_.Eval(idx/src_stride_, idx%src_stride_);
96  }
97 
98  private:
100  const index_t src_stride_;
101  const Shape<dimsrc> dst_in_src_stride_, dst_shape_;
102 };
103 
114 template<typename SrcExp, typename DType, int dimsrc, int etype>
116  public Exp<TransposeIndicesExp<SrcExp, DType, dimsrc, etype>, DType, etype> {
118  const SrcExp &src_indices_; // Expression of the source indices
119  Shape<dimsrc> src_shape_; // Holds the corresponding stride of the source axes in dst
120  const Shape<dimsrc> axes_; // The transpose axes
121  Shape<dimsrc> src_in_dst_stride_; // Holds the corresponding stride of the source axes in dst
123  explicit TransposeIndicesExp(const SrcExp &src_indices,
124  Shape<dimsrc> src_shape,
125  Shape<dimsrc> axes) : src_indices_(src_indices),
126  src_shape_(src_shape), axes_(axes) {
127  Shape<dimsrc> dst_shape_;
128  Shape<dimsrc> dst_stride_;
129  bool axes_checking_flag[dimsrc] = { 0 };
130  for (int i = 0; i < dimsrc; ++i) {
131  CHECK_LT(static_cast<int>(axes[i]), dimsrc)
132  << "Invalid axes input! All elements of axes must be between 0 and " << dimsrc
133  << ", find axes=" << axes;
134  dst_shape_[i] = src_shape[axes[i]];
135  axes_checking_flag[axes[i]] = true;
136  }
137  // check if the input axes is valid
138  for (int i = 0; i < dimsrc; ++i) {
139  CHECK_EQ(axes_checking_flag[i], true)
140  << "Invalid axes input! All elements of axes must be between 0 and " << dimsrc
141  << ", find axes=" << axes;
142  }
143  dst_stride_[dimsrc - 1] = 1;
144  for (int i = dimsrc - 2; i >= 0; --i) dst_stride_[i] = dst_shape_[i+1] * dst_stride_[i+1];
145  for (int i = 0; i < dimsrc; ++i) {
146  src_in_dst_stride_[axes[i]] = dst_stride_[i];
147  }
148  }
149 };
150 
161 template<typename SrcExp, typename DType, int dimsrc, int etype>
162 inline TransposeIndicesExp<SrcExp, DType, dimsrc, etype>
164  Shape<dimsrc> src_shape,
165  Shape<dimsrc> axes) {
166  return TransposeIndicesExp<SrcExp, DType, dimsrc, etype>(src_indices.self(), src_shape, axes);
167 }
168 
169 template<typename SrcExp, typename DType, int dimsrc, int etype>
170 struct Plan<TransposeIndicesExp<SrcExp, DType, dimsrc, etype>, DType> {
171  public:
173  : src_indices_(MakePlan(e.src_indices_)),
174  src_in_dst_stride_(e.src_in_dst_stride_),
175  src_shape_(e.src_shape_) {}
176  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
177  index_t src_idx = static_cast<index_t>(src_indices_.Eval(i, j));
178  index_t dst_idx = 0;
179  #pragma unroll
180  for (int k = dimsrc - 1; k >= 0; --k) {
181  dst_idx += (src_idx % src_shape_[k]) * src_in_dst_stride_[k];
182  src_idx /= src_shape_[k];
183  }
184  return static_cast<DType>(dst_idx);
185  }
186 
187  private:
188  Plan<SrcExp, DType> src_indices_;
189  const Shape<dimsrc> src_in_dst_stride_, src_shape_;
190 };
191 
192 //----------------------
193 // Execution plan
194 //----------------------
196 template<typename SrcExp, typename DType, int dimsrc, int etype>
197 inline Plan<TransposeIndicesExp<SrcExp, DType, dimsrc, etype>, DType>
200 }
201 
202 template<int dim, typename SrcExp, typename DType, int dimsrc, int etype>
203 struct ShapeCheck<dim, TransposeIndicesExp<SrcExp, DType, dimsrc, etype> > {
204  inline static Shape<dim>
207  return s;
208  }
209 };
210 
211 template<typename SrcExp, typename DType, int dimsrc, int etype>
212 struct ExpInfo<TransposeIndicesExp<SrcExp, DType, dimsrc, etype> > {
213  static const int kDim = ExpInfo<SrcExp>::kDim;
215 };
216 } // namespace expr
217 } // namespace mshadow
218 #endif // MSHADOW_EXTENSION_TRANSPOSE_H_
mshadow::expr::ExpInfo::kDevMask
static const int kDevMask
Definition: expr_engine-inl.h:264
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::TransposeIndicesExp::axes_
const Shape< dimsrc > axes_
Definition: transpose.h:120
mshadow::expr::Plan< TransposeIndicesExp< SrcExp, DType, dimsrc, etype >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: transpose.h:176
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::TransposeIndicesExp::src_in_dst_stride_
Shape< dimsrc > src_in_dst_stride_
Definition: transpose.h:121
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::TransposeIndicesExp::TransposeIndicesExp
TransposeIndicesExp(const SrcExp &src_indices, Shape< dimsrc > src_shape, Shape< dimsrc > axes)
constructor
Definition: transpose.h:123
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::Plan< TransposeExExp< SrcExp, DType, dimsrc >, DType >::Plan
Plan(const TransposeExExp< SrcExp, DType, dimsrc > &e)
Definition: transpose.h:83
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< TransposeExExp< SrcExp, DType, dimsrc >, SrcExp, dimsrc, DType >::shape_
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:47
mshadow::expr::Plan< TransposeExExp< SrcExp, DType, dimsrc >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: transpose.h:88
mshadow::expr::ExpInfo::kDim
static const int kDim
Definition: expr_engine-inl.h:263
mshadow::expr::TransposeIndicesExp::src_shape_
Shape< dimsrc > src_shape_
Definition: transpose.h:119
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::Plan< TransposeIndicesExp< SrcExp, DType, dimsrc, etype >, DType >::Plan
Plan(const TransposeIndicesExp< SrcExp, DType, dimsrc, etype > &e)
Definition: transpose.h:172
mshadow::expr::transpose_indices
TransposeIndicesExp< SrcExp, DType, dimsrc, etype > transpose_indices(const Exp< SrcExp, DType, etype > &src_indices, Shape< dimsrc > src_shape, Shape< dimsrc > axes)
a expression that reshapes a tensor to another shape
Definition: transpose.h:163
mshadow::expr::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
mshadow::expr::TransposeExExp::TransposeExExp
TransposeExExp(const SrcExp &src, Shape< dimsrc > axes)
constructor
Definition: transpose.h:52
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::transpose
TransposeExExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > transpose(const Exp< SrcExp, DType, etype > &src, Shape< ExpInfo< SrcExp >::kDim > axes)
a expression that reshapes a tensor to another shape
Definition: transpose.h:76
mshadow::expr::TransposeExExp
transpose axes of a tensor input: Tensor<Device,dim>: ishape output: Tensor<Device,...
Definition: transpose.h:43
mshadow::expr::TransposeExExp::axes_
const Shape< dimsrc > axes_
Definition: transpose.h:48
mshadow::Shape< dimsrc >
mshadow::expr::TransposeIndicesExp
transform contiguous indices of the source tensor to indices of the transposed tensor....
Definition: transpose.h:115
mshadow::expr::TransposeIndicesExp::src_indices_
const SrcExp & src_indices_
source expression
Definition: transpose.h:118
mshadow::expr::TransposeExExp::dst_in_src_stride_
Shape< dimsrc > dst_in_src_stride_
Definition: transpose.h:49
mshadow::expr::ShapeCheck< dim, TransposeIndicesExp< SrcExp, DType, dimsrc, etype > >::Check
static Shape< dim > Check(const TransposeIndicesExp< SrcExp, DType, dimsrc, etype > &t)
Definition: transpose.h:205
mshadow::expr::TransposeExExp::src_
const SrcExp & src_
source expression
Definition: transpose.h:47
mshadow::expr::TransposeExExp::src_stride_
index_t src_stride_
Definition: transpose.h:50