7 #ifndef MSHADOW_EXTENSION_TRANSPOSE_H_ 8 #define MSHADOW_EXTENSION_TRANSPOSE_H_ 10 #include "../extension.h" 24 template<
typename SrcExp,
typename DType,
int dimsrc>
27 SrcExp, dimsrc, DType> {
36 src_stride_ = src_shape[dimsrc - 1];
38 src_stride[dimsrc-1] = 1;
39 for (
int i = dimsrc-2; i >= 0; --i) src_stride[i] = src_shape[i+1]*src_stride[i+1];
40 for (
int i = 0; i < dimsrc; ++i) {
41 dst_in_src_stride_[i] = src_stride[axes[i]];
42 this->
shape_[i] = src_shape[axes[i]];
56 template<
typename SrcExp,
typename DType,
int etype>
62 template<
typename SrcExp,
typename DType,
int dimsrc>
73 for (
int k = dimsrc-2; k >= 0; --k) {
74 idx += (i % dst_shape_[k]) * dst_in_src_stride_[k];
96 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
98 public Exp<TransposeIndicesExp<SrcExp, DType, dimsrc, etype>, DType, etype> {
108 src_shape_(src_shape), axes_(axes) {
111 bool axes_checking_flag[dimsrc] = { 0 };
112 for (
int i = 0; i < dimsrc; ++i) {
113 CHECK_LT(static_cast<int>(axes[i]), dimsrc)
114 <<
"Invalid axes input! All elements of axes must be between 0 and " << dimsrc
115 <<
", find axes=" << axes;
116 dst_shape_[i] = src_shape[axes[i]];
117 axes_checking_flag[axes[i]] =
true;
120 for (
int i = 0; i < dimsrc; ++i) {
121 CHECK_EQ(axes_checking_flag[i],
true)
122 <<
"Invalid axes input! All elements of axes must be between 0 and " << dimsrc
123 <<
", find axes=" << axes;
125 dst_stride_[dimsrc - 1] = 1;
126 for (
int i = dimsrc - 2; i >= 0; --i) dst_stride_[i] = dst_shape_[i+1] * dst_stride_[i+1];
127 for (
int i = 0; i < dimsrc; ++i) {
128 src_in_dst_stride_[axes[i]] = dst_stride_[i];
143 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
151 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
155 : src_indices_(
MakePlan(e.src_indices_)),
156 src_in_dst_stride_(e.src_in_dst_stride_),
157 src_shape_(e.src_shape_) {}
162 for (
int k = dimsrc - 1; k >= 0; --k) {
163 dst_idx += (src_idx % src_shape_[k]) * src_in_dst_stride_[k];
164 src_idx /= src_shape_[k];
166 return static_cast<DType
>(dst_idx);
178 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
181 return Plan<TransposeIndicesExp<SrcExp, DType, dimsrc, etype>, DType>(e);
184 template<
int dim,
typename SrcExp,
typename DType,
int dimsrc,
int etype>
193 template<
typename SrcExp,
typename DType,
int dimsrc,
int etype>
200 #endif // MSHADOW_EXTENSION_TRANSPOSE_H_ TransposeExExp(const SrcExp &src, Shape< dimsrc > axes)
constructor
Definition: transpose.h:34
Definition: expr_engine-inl.h:40
static Shape< dim > Check(const TransposeIndicesExp< SrcExp, DType, dimsrc, etype > &t)
Definition: transpose.h:187
Plan(const TransposeExExp< SrcExp, DType, dimsrc > &e)
Definition: transpose.h:65
const Shape< dimsrc > axes_
Definition: transpose.h:30
transpose axes of a tensor input: Tensor<Device,dim>: ishape output: Tensor<Device,dimdst> oshape[a1],oshape[a2] = ishape[a2],oshape[a1]
Definition: transpose.h:25
index_t src_stride_
Definition: transpose.h:32
Shape< dimsrc > dst_in_src_stride_
Definition: transpose.h:31
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:204
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:244
const SrcExp & src_
source expression
Definition: transpose.h:29
int32_t index_t
type that will be used for index
Definition: base.h:291
Shape< dimsrc > src_in_dst_stride_
Definition: transpose.h:103
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: transpose.h:70
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:346
Plan(const TransposeIndicesExp< SrcExp, DType, dimsrc, etype > &e)
Definition: transpose.h:154
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:145
transform contiguous indices of the source tensor to indices of the transposed tensor. input: Tensor<Device, k>: ishape output: Tensor<Device, k>: oshape = ishape
Definition: transpose.h:97
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
const SubType & self(void) const
Definition: expression.h:64
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:221
TransposeIndicesExp(const SrcExp &src_indices, Shape< dimsrc > src_shape, Shape< dimsrc > axes)
constructor
Definition: transpose.h:105
a general class that allows extension that makes tensors of some shape
Definition: expr_engine-inl.h:25
const Shape< dimsrc > axes_
Definition: transpose.h:102
namespace for mshadow
Definition: base.h:282
Shape< dimsrc > src_shape_
Definition: transpose.h:101
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:58
const SrcExp & src_indices_
source expression
Definition: transpose.h:100
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:29
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: transpose.h:158