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 
25 #ifndef MSHADOW_EXTENSION_CHOOSE_H_
26 #define MSHADOW_EXTENSION_CHOOSE_H_
27 
28 #include "../extension.h"
29 
30 namespace mshadow {
31 namespace expr {
38 template<typename SrcExp, typename IndexExp, typename DType>
40  public Exp<MatChooseRowElementExp<SrcExp, IndexExp, DType>,
41  DType, type::kChainer> {
43  const SrcExp &src_;
45  const IndexExp &index_;
47  MatChooseRowElementExp(const SrcExp &src, const IndexExp &index)
48  : src_(src), index_(index) {}
49 };
50 
51 template<typename SrcExp, typename IndexExp,
52  typename DType, typename IDType, int e1, int e2>
53 inline MatChooseRowElementExp<SrcExp, IndexExp, DType>
55  const Exp<IndexExp, IDType, e2> &index) {
57  ::Error_Expression_Does_Not_Meet_Dimension_Req();
59 }
60 
61 //----------------------
62 // Execution plan
63 //----------------------
64 template<typename SrcExp, typename IndexExp, typename DType>
65 struct Plan<MatChooseRowElementExp<SrcExp, IndexExp, DType>, DType> {
66  public:
68  : src_(MakePlan(e.src_)),
69  index_(MakePlan(e.index_)) {
70  }
71  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
72  index_t idx = static_cast<index_t>(index_.Eval(0, x));
73  return src_.Eval(x, idx);
74  }
75 
76  private:
79 };
80 
81 template<typename SrcExp, typename IndexExp, typename DType>
82 inline Plan<MatChooseRowElementExp<SrcExp, IndexExp, DType>, DType>
85 }
86 
87 template<int dim, typename SrcExp, typename IndexExp, typename DType>
88 struct ShapeCheck<dim, MatChooseRowElementExp<SrcExp, IndexExp, DType> > {
89  inline static Shape<dim>
91  CHECK(dim == 1)
92  << "MatChooseRowElementExp only support 1 dimension output";
95  CHECK_EQ(shape1[0], shape2[0])
96  << "mat_choose_row_element index length and number of rows in matrix";
97  return shape2;
98  }
99 };
100 
101 template<typename SrcExp, typename IndexExp, typename DType>
102 struct ExpInfo<MatChooseRowElementExp<SrcExp, IndexExp, DType> > {
103  static const int kDim = 1;
105 };
106 } // namespace expr
107 } // namespace mshadow
108 #endif // MSHADOW_EXTENSION_CHOOSE_H_
mshadow::expr::ExpInfo::kDevMask
static const int kDevMask
Definition: expr_engine-inl.h:264
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::TypeCheckPass
used to help static type check
Definition: expr_engine-inl.h:330
mshadow::expr::ShapeCheck< dim, MatChooseRowElementExp< SrcExp, IndexExp, DType > >::Check
static Shape< dim > Check(const MatChooseRowElementExp< SrcExp, IndexExp, DType > &t)
Definition: choose.h:90
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::ShapeCheck
runtime shape checking template get the shape of an expression, report error if shape mismatch
Definition: expr_engine-inl.h:364
mshadow::expr::Plan< MatChooseRowElementExp< SrcExp, IndexExp, DType >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: choose.h:71
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::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::mat_choose_row_element
MatChooseRowElementExp< SrcExp, IndexExp, DType > mat_choose_row_element(const Exp< SrcExp, DType, e1 > &src, const Exp< IndexExp, IDType, e2 > &index)
Definition: choose.h:54
mshadow::expr::Plan< MatChooseRowElementExp< SrcExp, IndexExp, DType >, DType >::Plan
Plan(const MatChooseRowElementExp< SrcExp, IndexExp, DType > &e)
Definition: choose.h:67
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
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::Shape< dim >
mshadow::expr::MatChooseRowElementExp::index_
const IndexExp & index_
index operand
Definition: choose.h:45
mshadow::expr::MatChooseRowElementExp
Make a choice of index in the lowest changing dimension.
Definition: choose.h:39
mshadow::expr::MatChooseRowElementExp::src_
const SrcExp & src_
source operand
Definition: choose.h:43
mshadow::expr::MatChooseRowElementExp::MatChooseRowElementExp
MatChooseRowElementExp(const SrcExp &src, const IndexExp &index)
constructor
Definition: choose.h:47