mxnet
take_grad.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_TAKE_GRAD_H_
26 #define MSHADOW_EXTENSION_TAKE_GRAD_H_
27 
28 #include "../extension.h"
29 
30 namespace mshadow {
31 namespace expr {
32 
39 template<typename IndexExp, typename SrcExp, typename DType>
40 struct TakeGradExp : public Exp<TakeGradExp<IndexExp, SrcExp, DType>,
41  DType, type::kChainer> {
43  const IndexExp &index_;
45  const SrcExp &src_;
49  TakeGradExp(const IndexExp &index, const SrcExp &src, const index_t input_dim)
50  : index_(index), src_(src), input_dim_(input_dim) {}
51 }; // struct TakeGradExp
52 
53 
54 template<typename IndexExp,
55  typename SrcExp,
56  typename DType,
57  int e1, int e2>
58 inline TakeGradExp<IndexExp, SrcExp, DType>
60  const Exp<SrcExp, DType, e2> &src,
61  const index_t input_dim) {
63  src.self(),
64  input_dim);
65 }
66 
67 //----------------------
68 // Execution plan
69 //----------------------
70 
71 template<typename IndexExp, typename SrcExp, typename DType>
72 struct Plan<TakeGradExp<IndexExp, SrcExp, DType>, DType> {
73  public:
75  : index_(MakePlan(e.index_)),
76  src_(MakePlan(e.src_)),
77  batch_size_(ShapeCheck<1, IndexExp>::Check(e.index_)[0]) {
78  }
79 
80  // now return shape: in * out
81  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
82  DType ret = 0.f;
83  for (index_t i = 0; i < batch_size_; ++i) {
84  index_t idx = static_cast<index_t>(index_.Eval(0, i));
85  if (idx == y) {
86  ret += static_cast<DType>(src_.Eval(i, x));
87  }
88  }
89  return ret;
90  }
91 
92  private:
95  const index_t batch_size_;
96 }; // struct Plan
97 
98 
99 template<typename IndexExp, typename SrcExp, typename DType>
100 inline Plan<TakeGradExp<IndexExp, SrcExp, DType>, DType>
102  return Plan<TakeGradExp<IndexExp, SrcExp, DType>, DType>(exp);
103 }
104 
105 template<int dim, typename IndexExp, typename SrcExp, typename DType>
106 struct ShapeCheck<dim, TakeGradExp<IndexExp, SrcExp, DType> > {
107  inline static Shape<dim>
109  CHECK(dim == 2)
110  << "TakeGradExp only support 2D output";
111  // Shape<1> dshape = ShapeCheck<1, IndexExp>::Check(t.index_);
113  Shape<dim> ret;
114  ret[0] = t.input_dim_;
115  ret[1] = gshape[1];
116  return ret;
117  }
118 }; // struct ShapeCheck
119 
120 template<typename IndexExp, typename SrcExp, typename DType>
121 struct ExpInfo<TakeGradExp<IndexExp, SrcExp, DType> > {
122  static const int kDim = 2;
124 };
125 
126 } // namespace expr
127 } // namespace mshadow
128 
129 #endif // MSHADOW_EXTENSION_TAKE_GRAD_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::TakeGradExp
Calculate embedding gradient.
Definition: take_grad.h:40
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< TakeGradExp< IndexExp, SrcExp, DType >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: take_grad.h:81
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::TakeGradExp::input_dim_
const index_t input_dim_
batch size
Definition: take_grad.h:47
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::ExpInfo::kDim
static const int kDim
Definition: expr_engine-inl.h:263
mshadow::expr::ShapeCheck< dim, TakeGradExp< IndexExp, SrcExp, DType > >::Check
static Shape< dim > Check(const TakeGradExp< IndexExp, SrcExp, DType > &t)
Definition: take_grad.h:108
mshadow::index_t
int32_t index_t
type that will be used for index
Definition: base.h:328
mshadow::expr::take_grad
TakeGradExp< IndexExp, SrcExp, DType > take_grad(const Exp< IndexExp, DType, e1 > &index, const Exp< SrcExp, DType, e2 > &src, const index_t input_dim)
Definition: take_grad.h:59
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::expr::Plan< TakeGradExp< IndexExp, SrcExp, DType >, DType >::Plan
Plan(const TakeGradExp< IndexExp, SrcExp, DType > &e)
Definition: take_grad.h:74
mshadow
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::expr::TakeGradExp::index_
const IndexExp & index_
index oprand
Definition: take_grad.h:43
mshadow::Shape< dim >
mshadow::expr::TakeGradExp::TakeGradExp
TakeGradExp(const IndexExp &index, const SrcExp &src, const index_t input_dim)
constructor
Definition: take_grad.h:49
mshadow::expr::TakeGradExp::src_
const SrcExp & src_
out gradient oprand
Definition: take_grad.h:45