mxnet
take.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 
26 #ifndef MSHADOW_EXTENSION_TAKE_H_
27 #define MSHADOW_EXTENSION_TAKE_H_
28 
29 #include "../extension.h"
30 
31 namespace mshadow {
32 namespace expr {
33 
39 template<typename IndexExp, typename SrcExp, typename DType>
40 struct TakeExp: public Exp<TakeExp<IndexExp, SrcExp, DType>,
41  DType, type::kChainer> {
43  const IndexExp &index_;
45  const SrcExp &src_;
47  TakeExp(const IndexExp &index, const SrcExp &src)
48  : index_(index), src_(src) {}
49 }; // struct TakeExp
50 
51 
52 
53 template<typename IndexExp,
54  typename SrcExp,
55  typename DType,
56  int e1, int e2>
59  const Exp<SrcExp, DType, e2> &src) {
60  return TakeExp<IndexExp, SrcExp, DType>(index.self(), src.self());
61 }
62 
63 
64 //----------------------
65 // Execution plan
66 //----------------------
67 
68 template<typename IndexExp, typename SrcExp, typename DType>
69 struct Plan<TakeExp<IndexExp, SrcExp, DType>, DType> {
70  public:
72  : index_(MakePlan(e.index_)), src_(MakePlan(e.src_)) {
73  }
74 
75  // TODO(xx): discuss W shape: in * out or out * in
76  // Now I use in * out
77  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
78  index_t idx = static_cast<index_t>(index_.Eval(0, y));
79  return static_cast<DType>(src_.Eval(idx, x));
80  }
81 
82  private:
85 }; // struct Plan
86 
87 template<typename IndexExp, typename SrcExp, typename DType>
90  return Plan<TakeExp<IndexExp, SrcExp, DType>, DType>(exp);
91 }
92 
93 template<int dim, typename IndexExp, typename SrcExp, typename DType>
94 struct ShapeCheck<dim, TakeExp<IndexExp, SrcExp, DType> > {
95  inline static Shape<dim>
97  CHECK(dim == 2)
98  << "TakeExp only support 2D output";
101  Shape<dim> ret;
102  ret[0] = dshape[0];
103  ret[1] = wshape[1];
104  return ret;
105  }
106 };
107 
108 
109 template<typename IndexExp, typename SrcExp, typename DType>
110 struct ExpInfo<TakeExp<IndexExp, SrcExp, DType> > {
111  static const int kDim = 2;
112  static const int kDevMask = ExpInfo<IndexExp>::kDevMask;
113 };
114 
115 } // namespace expr
116 } // namespace mshadow
117 
118 #endif // MSHADOW_EXTENSION_TAKE_H_
static Shape< dim > Check(const TakeExp< IndexExp, SrcExp, DType > &t)
Definition: take.h:96
Definition: expr_engine-inl.h:59
TakeExp< IndexExp, SrcExp, DType > take(const Exp< IndexExp, DType, e1 > &index, const Exp< SrcExp, DType, e2 > &src)
Definition: take.h:58
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: take.h:77
static Shape< dim > Check(const E &t)
TakeExp(const IndexExp &index, const SrcExp &src)
Definition: take.h:47
#define MSHADOW_XINLINE
Definition: base.h:223
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:263
int32_t index_t
type that will be used for index
Definition: base.h:336
Take a column from a matrix.
Definition: take.h:40
Plan(const TakeExp< IndexExp, SrcExp, DType > &e)
Definition: take.h:71
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:365
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
const SubType & self(void) const
Definition: expression.h:83
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:240
overloaded + operator between half_t and bf16_t
Definition: base.h:327
const IndexExp & index_
index oprand
Definition: take.h:43
const SrcExp & src_
embediing oprand
Definition: take.h:45