mxnet
choose.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_CHOOSE_H_
27 #define MSHADOW_EXTENSION_CHOOSE_H_
28 
29 #include "../extension.h"
30 
31 namespace mshadow {
32 namespace expr {
39 template<typename SrcExp, typename IndexExp, typename DType>
41  public Exp<MatChooseRowElementExp<SrcExp, IndexExp, DType>,
42  DType, type::kChainer> {
44  const SrcExp &src_;
46  const IndexExp &index_;
48  MatChooseRowElementExp(const SrcExp &src, const IndexExp &index)
49  : src_(src), index_(index) {}
50 };
51 
52 template<typename SrcExp, typename IndexExp,
53  typename DType, typename IDType, int e1, int e2>
56  const Exp<IndexExp, IDType, e2> &index) {
58  ::Error_Expression_Does_Not_Meet_Dimension_Req();
60 }
61 
62 //----------------------
63 // Execution plan
64 //----------------------
65 template<typename SrcExp, typename IndexExp, typename DType>
66 struct Plan<MatChooseRowElementExp<SrcExp, IndexExp, DType>, DType> {
67  public:
69  : src_(MakePlan(e.src_)),
70  index_(MakePlan(e.index_)) {
71  }
72  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
73  index_t idx = static_cast<index_t>(index_.Eval(0, x));
74  return src_.Eval(x, idx);
75  }
76 
77  private:
80 };
81 
82 template<typename SrcExp, typename IndexExp, typename DType>
86 }
87 
88 template<int dim, typename SrcExp, typename IndexExp, typename DType>
89 struct ShapeCheck<dim, MatChooseRowElementExp<SrcExp, IndexExp, DType> > {
90  inline static Shape<dim>
92  CHECK(dim == 1)
93  << "MatChooseRowElementExp only support 1 dimension output";
96  CHECK_EQ(shape1[0], shape2[0])
97  << "mat_choose_row_element index length and number of rows in matrix";
98  return shape2;
99  }
100 };
101 
102 template<typename SrcExp, typename IndexExp, typename DType>
103 struct ExpInfo<MatChooseRowElementExp<SrcExp, IndexExp, DType> > {
104  static const int kDim = 1;
106 };
107 } // namespace expr
108 } // namespace mshadow
109 #endif // MSHADOW_EXTENSION_CHOOSE_H_
Make a choice of index in the lowest changing dimension.
Definition: choose.h:40
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
const IndexExp & index_
index operand
Definition: choose.h:46
static Shape< dim > Check(const E &t)
#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
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: choose.h:72
const SrcExp & src_
source operand
Definition: choose.h:44
int32_t index_t
type that will be used for index
Definition: base.h:336
static Shape< dim > Check(const MatChooseRowElementExp< SrcExp, IndexExp, DType > &t)
Definition: choose.h:91
MatChooseRowElementExp< SrcExp, IndexExp, DType > mat_choose_row_element(const Exp< SrcExp, DType, e1 > &src, const Exp< IndexExp, IDType, e2 > &index)
Definition: choose.h:55
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
MatChooseRowElementExp(const SrcExp &src, const IndexExp &index)
constructor
Definition: choose.h:48
overloaded + operator between half_t and bf16_t
Definition: base.h:327
Plan(const MatChooseRowElementExp< SrcExp, IndexExp, DType > &e)
Definition: choose.h:68