mxnet
fill.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_FILL_H_
26 #define MSHADOW_EXTENSION_FILL_H_
27 
28 #include "../extension.h"
29 
30 
31 namespace mshadow {
32 namespace expr {
40 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
42  public Exp<MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType>,
43  DType, type::kChainer> {
45  const SrcExp &src_;
46  const ValExp &val_;
48  const IndexExp &index_;
50  MatFillRowElementExp(const SrcExp &src, const ValExp &val, const IndexExp &index)
51  : src_(src), val_(val), index_(index) {}
52 };
53 
54 template<typename SrcExp, typename ValExp, typename IndexExp,
55  typename SDType, typename VDType, typename IDType, int e1, int e2, int e3>
56 inline MatFillRowElementExp<SrcExp, ValExp, IndexExp, SDType>
58  const Exp<ValExp, VDType, e2> &val,
59  const Exp<IndexExp, IDType, e3> &index) {
61  && ExpInfo<IndexExp>::kDim == 1>::Error_Expression_Does_Not_Meet_Dimension_Req();
63  val.self(), index.self());
64 }
65 
66 //----------------------
67 // Execution plan
68 //----------------------
69 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
70 struct Plan<MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType>, DType> {
71  public:
73  : src_(MakePlan(e.src_)),
74  val_(MakePlan(e.val_)),
75  index_(MakePlan(e.index_)) {
76  }
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  if (idx == x) {
80  return static_cast<DType>(val_.Eval(0, y));
81  } else {
82  return static_cast<DType>(src_.Eval(y, x));
83  }
84  }
85 
86  private:
90 };
91 
92 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
93 inline Plan<MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType>, DType>
96 }
97 
98 template<int dim, typename SrcExp, typename ValExp, typename IndexExp, typename DType>
99 struct ShapeCheck<dim, MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType> > {
100  inline static Shape<dim>
102  CHECK(dim == 2)
103  << "MatFillRowElementExp only support 2 dimension output";
107  CHECK((shape_src[0] == shape_index[0]) && (shape_index[0] == shape_val[0]))
108  << "mat_fill_row_element index length, val length and number of rows in matrix";
109  return shape_src;
110  }
111 };
112 
113 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
114 struct ExpInfo<MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType> > {
115  static const int kDim = 2;
116  static const int kDevMask =
118 };
119 } // namespace expr
120 } // namespace mshadow
121 #endif // MSHADOW_EXTENSION_FILL_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_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::MatFillRowElementExp
Set value of a specific element in each line of the data matrix.
Definition: fill.h:41
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::MatFillRowElementExp::MatFillRowElementExp
MatFillRowElementExp(const SrcExp &src, const ValExp &val, const IndexExp &index)
constructor
Definition: fill.h:50
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::MatFillRowElementExp::src_
const SrcExp & src_
src operand
Definition: fill.h:45
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::MatFillRowElementExp::val_
const ValExp & val_
Definition: fill.h:46
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::Plan< MatFillRowElementExp< SrcExp, ValExp, IndexExp, DType >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: fill.h:77
mshadow::expr::Plan< MatFillRowElementExp< SrcExp, ValExp, IndexExp, DType >, DType >::Plan
Plan(const MatFillRowElementExp< SrcExp, ValExp, IndexExp, DType > &e)
Definition: fill.h:72
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::ShapeCheck< dim, MatFillRowElementExp< SrcExp, ValExp, IndexExp, DType > >::Check
static Shape< dim > Check(const MatFillRowElementExp< SrcExp, ValExp, IndexExp, DType > &t)
Definition: fill.h:101
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::mat_fill_row_element
MatFillRowElementExp< SrcExp, ValExp, IndexExp, SDType > mat_fill_row_element(const Exp< SrcExp, SDType, e1 > &src, const Exp< ValExp, VDType, e2 > &val, const Exp< IndexExp, IDType, e3 > &index)
Definition: fill.h:57
mshadow::expr::MatFillRowElementExp::index_
const IndexExp & index_
index operand
Definition: fill.h:48