mxnet
mask.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_MASK_H_
26 #define MSHADOW_EXTENSION_MASK_H_
27 
28 #include "../extension.h"
29 
30 namespace mshadow {
31 namespace expr {
32 
38 template<typename IndexExp, typename SrcExp, typename DType>
39 struct MaskExp: public Exp<MaskExp<IndexExp, SrcExp, DType>,
40  DType, type::kChainer> {
42  const IndexExp &index_;
44  const SrcExp &src_;
46  MaskExp(const IndexExp &index, const SrcExp &src)
47  : index_(index), src_(src) {}
48 }; // struct MaskExp
49 
50 
51 
52 template<typename IndexExp,
53  typename SrcExp,
54  typename DType,
55  int e1, int e2>
56 inline MaskExp<IndexExp, SrcExp, DType>
58  const Exp<SrcExp, DType, e2> &src) {
59  return MaskExp<IndexExp, SrcExp, DType>(index.self(), src.self());
60 }
61 
62 
63 //----------------------
64 // Execution plan
65 //----------------------
66 
67 template<typename IndexExp, typename SrcExp, typename DType>
68 struct Plan<MaskExp<IndexExp, SrcExp, DType>, DType> {
69  public:
71  : index_(MakePlan(e.index_)), src_(MakePlan(e.src_)) {
72  }
73 
74  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
75  return static_cast<DType>(src_.Eval(y, x) * index_.Eval(0, y));
76  }
77 
78  private:
81 }; // struct Plan
82 
83 template<typename IndexExp, typename SrcExp, typename DType>
84 inline Plan<MaskExp<IndexExp, SrcExp, DType>, DType>
86  return Plan<MaskExp<IndexExp, SrcExp, DType>, DType>(exp);
87 }
88 
89 template<int dim, typename IndexExp, typename SrcExp, typename DType>
90 struct ShapeCheck<dim, MaskExp<IndexExp, SrcExp, DType> > {
91  inline static Shape<dim>
93  CHECK(dim == 2)
94  << "MaskExp only support 2D output";
97  CHECK_EQ(dshape[0], wshape[0]) << "MaskExp require inputs in same first dimention";
98  Shape<dim> ret;
99  ret[0] = wshape[0];
100  ret[1] = wshape[1];
101  return ret;
102  }
103 };
104 
105 
106 template<typename IndexExp, typename SrcExp, typename DType>
107 struct ExpInfo<MaskExp<IndexExp, SrcExp, DType> > {
108  static const int kDim = 2;
110 };
111 
112 } // namespace expr
113 } // namespace mshadow
114 
115 #endif // MSHADOW_EXTENSION_MASK_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_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< MaskExp< IndexExp, SrcExp, DType >, DType >::Plan
Plan(const MaskExp< IndexExp, SrcExp, DType > &e)
Definition: mask.h:70
mshadow::expr::MaskExp::index_
const IndexExp & index_
index oprand
Definition: mask.h:42
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::mask
MaskExp< IndexExp, SrcExp, DType > mask(const Exp< IndexExp, DType, e1 > &index, const Exp< SrcExp, DType, e2 > &src)
Definition: mask.h:57
mshadow::expr::MaskExp
Broadcast a mask and do element-wise multiplication.
Definition: mask.h:39
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, MaskExp< IndexExp, SrcExp, DType > >::Check
static Shape< dim > Check(const MaskExp< IndexExp, SrcExp, DType > &t)
Definition: mask.h:92
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::MaskExp::src_
const SrcExp & src_
matrix oprand
Definition: mask.h:44
mshadow::expr::Plan< MaskExp< IndexExp, SrcExp, DType >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: mask.h:74
mshadow::expr::MaskExp::MaskExp
MaskExp(const IndexExp &index, const SrcExp &src)
Definition: mask.h:46