Caffe2 - C++ API
A deep learning, cross platform ML framework
elementwise_add_op.h
1 #ifndef CAFFE2_OPERATORS_ELEMENTWISE_ADD_OP_H_
2 #define CAFFE2_OPERATORS_ELEMENTWISE_ADD_OP_H_
3 
4 #include <algorithm>
5 #include <functional>
6 #include <vector>
7 
8 #include "caffe2/operators/elementwise_ops.h"
9 #include "caffe2/operators/elementwise_ops_utils.h"
10 #include "caffe2/utils/math.h"
11 
12 namespace caffe2 {
13 
14 template <class Context>
15 struct AddFunctor {
16  template <typename TIn, typename TOut>
17  bool Forward(
18  const std::vector<int>& A_dims,
19  const std::vector<int>& B_dims,
20  const TIn* A,
21  const TIn* B,
22  TOut* C,
23  Context* context) const {
24  math::Add(
25  A_dims.size(),
26  A_dims.data(),
27  B_dims.size(),
28  B_dims.data(),
29  A,
30  B,
31  C,
32  context);
33  return true;
34  }
35 
36  template <typename TGrad, typename TIn, typename TOut>
37  bool Backward(
38  const std::vector<int>& A_dims,
39  const std::vector<int>& B_dims,
40  const TGrad* dC,
41  const TIn* /* A */,
42  const TIn* /* B */,
43  const TOut* /* C */,
44  TGrad* dA,
45  TGrad* dB,
46  Context* context) const {
47  const std::vector<int> C_dims =
48  elementwise_ops_utils::ComputeBinaryBroadcastForwardDims(
49  A_dims, B_dims);
50  std::vector<int> A_back_dims;
51  std::vector<int> B_back_dims;
52  elementwise_ops_utils::ComputeBinaryBroadcastBackwardDims(
53  A_dims, B_dims, &A_back_dims, &B_back_dims);
54  math::ReduceSum(
55  C_dims.size(),
56  C_dims.data(),
57  A_back_dims.data(),
58  TGrad(1),
59  dC,
60  dA,
61  context);
62  math::ReduceSum(
63  C_dims.size(),
64  C_dims.data(),
65  B_back_dims.data(),
66  TGrad(1),
67  dC,
68  dB,
69  context);
70  return true;
71  }
72 };
73 
74 } // namespace caffe2
75 
76 #endif // CAFFE2_OPERATORS_ELEMENTWISE_ADD_OP_H_
Definition: static.cpp:52
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
Definition: static.cpp:64
Definition: static.cpp:58