Go to the documentation of this file.
25 #ifndef MSHADOW_EXTENSION_TRANSPOSE_H_
26 #define MSHADOW_EXTENSION_TRANSPOSE_H_
28 #include "../extension.h"
42 template<
typename SrcExp,
typename DType,
int dimsrc>
45 SrcExp, dimsrc, DType> {
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) {
60 this->
shape_[i] = src_shape[axes[i]];
74 template<
typename SrcExp,
typename DType,
int etype>
75 inline TransposeExExp<SrcExp, DType, ExpInfo<SrcExp>::kDim>
80 template<
typename SrcExp,
typename DType,
int dimsrc>
85 src_stride_(e.src_stride_),
86 dst_in_src_stride_(e.dst_in_src_stride_),
87 dst_shape_(e.shape_) {}
89 index_t idx = j * dst_in_src_stride_[dimsrc - 1];
91 for (
int k = dimsrc-2; k >= 0; --k) {
92 idx += (i % dst_shape_[k]) * dst_in_src_stride_[k];
95 return src_.Eval(idx/src_stride_, idx%src_stride_);
114 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
116 public Exp<TransposeIndicesExp<SrcExp, DType, dimsrc, etype>, DType, etype> {
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;
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;
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) {
161 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
162 inline TransposeIndicesExp<SrcExp, DType, dimsrc, etype>
169 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
173 : src_indices_(
MakePlan(e.src_indices_)),
174 src_in_dst_stride_(e.src_in_dst_stride_),
175 src_shape_(e.src_shape_) {}
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];
184 return static_cast<DType
>(dst_idx);
196 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
197 inline Plan<TransposeIndicesExp<SrcExp, DType, dimsrc, etype>, DType>
202 template<
int dim,
typename SrcExp,
typename DType,
int dimsrc,
int etype>
211 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
218 #endif // MSHADOW_EXTENSION_TRANSPOSE_H_
static const int kDevMask
Definition: expr_engine-inl.h:264
const SubType & self(void) const
Definition: expression.h:82
const Shape< dimsrc > axes_
Definition: transpose.h:120
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: transpose.h:176
#define MSHADOW_XINLINE
Definition: base.h:228
Shape< dimsrc > src_in_dst_stride_
Definition: transpose.h:121
runtime shape checking template get the shape of an expression, report error if shape mismatch
Definition: expr_engine-inl.h:364
TransposeIndicesExp(const SrcExp &src_indices, Shape< dimsrc > src_shape, Shape< dimsrc > axes)
constructor
Definition: transpose.h:123
static Shape< dim > Check(const E &t)
Plan(const TransposeExExp< SrcExp, DType, dimsrc > &e)
Definition: transpose.h:83
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:262
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:239
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:47
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: transpose.h:88
static const int kDim
Definition: expr_engine-inl.h:263
Shape< dimsrc > src_shape_
Definition: transpose.h:119
int32_t index_t
type that will be used for index
Definition: base.h:328
Definition: expr_engine-inl.h:58
Plan(const TransposeIndicesExp< SrcExp, DType, dimsrc, etype > &e)
Definition: transpose.h:172
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
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
TransposeExExp(const SrcExp &src, Shape< dimsrc > axes)
constructor
Definition: transpose.h:52
overloaded + operator between half_t and bf16_t
Definition: base.h:319
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:43
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
transpose axes of a tensor input: Tensor<Device,dim>: ishape output: Tensor<Device,...
Definition: transpose.h:43
const Shape< dimsrc > axes_
Definition: transpose.h:48
transform contiguous indices of the source tensor to indices of the transposed tensor....
Definition: transpose.h:115
const SrcExp & src_indices_
source expression
Definition: transpose.h:118
Shape< dimsrc > dst_in_src_stride_
Definition: transpose.h:49
static Shape< dim > Check(const TransposeIndicesExp< SrcExp, DType, dimsrc, etype > &t)
Definition: transpose.h:205
const SrcExp & src_
source expression
Definition: transpose.h:47
index_t src_stride_
Definition: transpose.h:50