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 
26 #ifndef MSHADOW_EXTENSION_FILL_H_
27 #define MSHADOW_EXTENSION_FILL_H_
28 
29 #include "../extension.h"
30 
31 
32 namespace mshadow {
33 namespace expr {
41 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
43  public Exp<MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType>,
44  DType, type::kChainer> {
46  const SrcExp &src_;
47  const ValExp &val_;
49  const IndexExp &index_;
51  MatFillRowElementExp(const SrcExp &src, const ValExp &val, const IndexExp &index)
52  : src_(src), val_(val), index_(index) {}
53 };
54 
55 template<typename SrcExp, typename ValExp, typename IndexExp,
56  typename SDType, typename VDType, typename IDType, int e1, int e2, int e3>
59  const Exp<ValExp, VDType, e2> &val,
60  const Exp<IndexExp, IDType, e3> &index) {
62  && ExpInfo<IndexExp>::kDim == 1>::Error_Expression_Does_Not_Meet_Dimension_Req();
64  val.self(), index.self());
65 }
66 
67 //----------------------
68 // Execution plan
69 //----------------------
70 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
71 struct Plan<MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType>, DType> {
72  public:
74  : src_(MakePlan(e.src_)),
75  val_(MakePlan(e.val_)),
76  index_(MakePlan(e.index_)) {
77  }
78  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
79  index_t idx = static_cast<index_t>(index_.Eval(0, y));
80  if (idx == x) {
81  return static_cast<DType>(val_.Eval(0, y));
82  } else {
83  return static_cast<DType>(src_.Eval(y, x));
84  }
85  }
86 
87  private:
91 };
92 
93 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
97 }
98 
99 template<int dim, typename SrcExp, typename ValExp, typename IndexExp, typename DType>
100 struct ShapeCheck<dim, MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType> > {
101  inline static Shape<dim>
103  CHECK(dim == 2)
104  << "MatFillRowElementExp only support 2 dimension output";
108  CHECK((shape_src[0] == shape_index[0]) && (shape_index[0] == shape_val[0]))
109  << "mat_fill_row_element index length, val length and number of rows in matrix";
110  return shape_src;
111  }
112 };
113 
114 template<typename SrcExp, typename ValExp, typename IndexExp, typename DType>
115 struct ExpInfo<MatFillRowElementExp<SrcExp, ValExp, IndexExp, DType> > {
116  static const int kDim = 2;
117  static const int kDevMask =
119 };
120 } // namespace expr
121 } // namespace mshadow
122 #endif // MSHADOW_EXTENSION_FILL_H_
MatFillRowElementExp(const SrcExp &src, const ValExp &val, const IndexExp &index)
constructor
Definition: fill.h:51
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
const IndexExp & index_
index operand
Definition: fill.h:49
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: fill.h:78
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
int32_t index_t
type that will be used for index
Definition: base.h:336
const ValExp & val_
Definition: fill.h:47
Plan(const MatFillRowElementExp< SrcExp, ValExp, IndexExp, DType > &e)
Definition: fill.h:73
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:365
const SrcExp & src_
src operand
Definition: fill.h:46
Set value of a specific element in each line of the data matrix.
Definition: fill.h:42
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
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:58
static Shape< dim > Check(const MatFillRowElementExp< SrcExp, ValExp, IndexExp, DType > &t)
Definition: fill.h:102