mxnet
swapaxis.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_SWAPAXIS_H_
26 #define MSHADOW_EXTENSION_SWAPAXIS_H_
27 #include <algorithm>
28 #include <utility>
29 #include "../extension.h"
30 namespace mshadow {
31 namespace expr {
43 template<typename SrcExp, typename DType, int dimsrc, int m_a1, int a2>
44 struct SwapAxisExp:
45  public MakeTensorExp<SwapAxisExp<SrcExp, DType, dimsrc, m_a1, a2>,
46  SrcExp, dimsrc, DType> {
47  // decode the a1, a2
48  static const int a1 = dimsrc - m_a1;
50  const SrcExp &src_;
52  explicit SwapAxisExp(const SrcExp &src) : src_(src) {
54  std::swap(this->shape_[a1], this->shape_[a2]);
55  }
56 };
67 template<int a1, int a2, typename SrcExp, typename DType, int etype>
68 inline SwapAxisExp<SrcExp, DType, ExpInfo<SrcExp>::kDim,
69  ExpInfo<SrcExp>::kDim - a1, a2>
71  typedef ExpInfo<SrcExp> Info;
72  TypeCheckPass<Info::kDim >= a1 + 1 && Info::kDim >= a2 + 1 &&
73  a2 < a1>::Error_Expression_Does_Not_Meet_Dimension_Req();
75  ExpInfo<SrcExp>::kDim - a1, a2>(src.self());
76 }
77 template<typename SrcExp, typename DType, int dimsrc, int m_a1, int a2>
78 struct Plan<SwapAxisExp<SrcExp, DType, dimsrc, m_a1, a2>, DType> {
79  public:
80  // decode the a1
81  static const int a1 = dimsrc - m_a1;
83  : src_(MakePlan(e.src_)),
84  shapey_(e.shape_.ProdShape(a1 + 1, dimsrc - 1)),
85  shapez_(e.shape_[a1]),
86  shapec_(e.shape_.ProdShape(a2 + 1, a1)),
87  shapen_(e.shape_[a2]) {}
88  MSHADOW_XINLINE DType Eval(index_t i, index_t j) const {
89  const index_t y = i % shapey_;
90  i /= shapey_;
91  const index_t z = i % shapez_;
92  i /= shapez_;
93  const index_t c = i % shapec_;
94  i /= shapec_;
95  const index_t n = i % shapen_;
96  // swap z and n
97  return src_.Eval(((((i / shapen_) * shapez_ + z) * shapec_ +
98  c) * shapen_ + n) * shapey_ + y, j);
99  }
100 
101  private:
102  Plan<SrcExp, DType> src_;
103  const index_t shapey_, shapez_, shapec_, shapen_;
104 };
105 template<typename SrcExp, typename DType, int dimsrc, int a2>
106 struct Plan<SwapAxisExp<SrcExp, DType, dimsrc, 1, a2>, DType> {
107  public:
109  : src_(MakePlan(e.src_)),
110  shapex_(e.shape_[dimsrc - 1]),
111  shapey_(e.shape_.ProdShape(a2 + 1, dimsrc - 1)),
112  shapez_(e.shape_[a2]) {}
113  MSHADOW_XINLINE DType Eval(index_t i, index_t x) const {
114  // swap x and z
115  const index_t y = i % shapey_;
116  i /= shapey_;
117  const index_t z = i % shapez_;
118  const index_t n = i / shapez_;
119  return src_.Eval((n * shapex_ + x) * shapey_ + y , z);
120  }
121 
122  private:
123  Plan<SrcExp, DType> src_;
124  const index_t shapex_, shapey_, shapez_;
125 };
126 } // namespace expr
127 } // namespace mshadow
128 #endif // MSHADOW_EXTENSION_SWAPAXIS_H_
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::Plan< SwapAxisExp< SrcExp, DType, dimsrc, m_a1, a2 >, DType >::Plan
Plan(const SwapAxisExp< SrcExp, DType, dimsrc, m_a1, a2 > &e)
Definition: swapaxis.h:82
mshadow::expr::SwapAxisExp
swap two axis of a tensor input: Tensor<Device,dim>: ishape output: Tensor<Device,...
Definition: swapaxis.h:44
mshadow::expr::TypeCheckPass
used to help static type check
Definition: expr_engine-inl.h:330
mshadow::expr::Plan< SwapAxisExp< SrcExp, DType, dimsrc, 1, a2 >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t x) const
Definition: swapaxis.h:113
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::SwapAxisExp::src_
const SrcExp & src_
source expression
Definition: swapaxis.h:50
mshadow::expr::SwapAxisExp::SwapAxisExp
SwapAxisExp(const SrcExp &src)
constructor
Definition: swapaxis.h:52
mshadow::expr::SwapAxisExp::a1
static const int a1
Definition: swapaxis.h:48
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
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::Plan< SwapAxisExp< SrcExp, DType, dimsrc, m_a1, a2 >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t i, index_t j) const
Definition: swapaxis.h:88
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< SwapAxisExp< SrcExp, DType, dimsrc, m_a1, a2 >, SrcExp, dimsrc, DType >::shape_
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:47
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::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::swapaxis
SwapAxisExp< SrcExp, DType, ExpInfo< SrcExp >::kDim, ExpInfo< SrcExp >::kDim - a1, a2 > swapaxis(const Exp< SrcExp, DType, etype > &src)
a expression that reshapes a tensor to another shape
Definition: swapaxis.h:70
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::Plan< SwapAxisExp< SrcExp, DType, dimsrc, 1, a2 >, DType >::Plan
Plan(const SwapAxisExp< SrcExp, DType, dimsrc, 1, a2 > &e)
Definition: swapaxis.h:108