mxnet
reshape.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_RESHAPE_H_
26 #define MSHADOW_EXTENSION_RESHAPE_H_
27 #include "../extension.h"
28 namespace mshadow {
29 namespace expr {
38 template<typename SrcExp, typename DType, int dimdst, int dimsrc>
39 struct ReshapeExp:
40  public MakeTensorExp<ReshapeExp<SrcExp, DType, dimdst, dimsrc>,
41  SrcExp, dimdst, DType> {
43  const SrcExp &src_;
47  ReshapeExp(const SrcExp &src, Shape<dimdst> shape)
48  : src_(src) {
50  CHECK_EQ(ishape.Size(), shape.Size()) << "reshape size must match";
51  ishapex_ = ishape[dimsrc - 1];
52  this->shape_ = shape;
53  }
54 };
64 template<typename SrcExp, typename DType, int etype, int dimdst>
65 inline ReshapeExp<SrcExp, DType, dimdst, ExpInfo<SrcExp>::kDim>
68  (src.self(), oshape);
69 }
70 //----------------------
71 // Execution plan
72 //----------------------
73 template<typename SrcExp, typename DType, int dimdst, int dimsrc>
74 struct Plan<ReshapeExp<SrcExp, DType, dimdst, dimsrc>, DType> {
75  public:
77  : src_(MakePlan(e.src_)),
78  oshapex_(e.shape_[dimdst - 1]), ishapex_(e.ishapex_) {}
79  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
80  const index_t idx = y * oshapex_ + x;
81  return src_.Eval(idx / ishapex_, idx % ishapex_);
82  }
83 
84  private:
86  const index_t oshapex_, ishapex_;
87 };
88 // special work plan for 1 dimensional data
89 template<typename SrcExp, typename DType, int dimdst>
90 struct Plan<ReshapeExp<SrcExp, DType, dimdst, 1>, DType> {
91  public:
93  : src_(MakePlan(e.src_)), oshapex_(e.shape_[dimdst - 1]) {
94  }
95  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
96  return src_.Eval(0, y * oshapex_ + x);
97  }
98 
99  private:
100  Plan<SrcExp, DType> src_;
101  const index_t oshapex_;
102 };
103 } // namespace expr
104 } // namespace mshadow
105 #endif // MSHADOW_EXTENSION_RESHAPE_H_
mshadow::Shape::Size
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:158
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::Plan< ReshapeExp< SrcExp, DType, dimdst, dimsrc >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: reshape.h:79
mshadow::expr::Plan< ReshapeExp< SrcExp, DType, dimdst, dimsrc >, DType >::Plan
Plan(const ReshapeExp< SrcExp, DType, dimdst, dimsrc > &e)
Definition: reshape.h:76
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::ReshapeExp::src_
const SrcExp & src_
source expression
Definition: reshape.h:43
mshadow::expr::Plan< ReshapeExp< SrcExp, DType, dimdst, 1 >, DType >::Plan
Plan(const ReshapeExp< SrcExp, DType, dimdst, 1 > &e)
Definition: reshape.h:92
mshadow::expr::ReshapeExp::ReshapeExp
ReshapeExp(const SrcExp &src, Shape< dimdst > shape)
constructor
Definition: reshape.h:47
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
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< ReshapeExp< SrcExp, DType, dimdst, dimsrc >, SrcExp, dimdst, DType >::shape_
Shape< dim > shape_
the shape of this expression
Definition: expr_engine-inl.h:47
mshadow::expr::reshape
ReshapeExp< SrcExp, DType, dimdst, ExpInfo< SrcExp >::kDim > reshape(const Exp< SrcExp, DType, etype > &src, Shape< dimdst > oshape)
a expression that reshapes a tensor to another shape
Definition: reshape.h:66
mshadow::expr::ReshapeExp
reshape the content to another shape input: Tensor<Device,dimsrc>: ishape output: Tensor<Device,...
Definition: reshape.h:39
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< ReshapeExp< SrcExp, DType, dimdst, 1 >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: reshape.h:95
mshadow::expr::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
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::Shape
shape of a tensor
Definition: tensor.h:64
mshadow::expr::ReshapeExp::ishapex_
index_t ishapex_
smallest dimension of input
Definition: reshape.h:45