Caffe2 - C++ API
A deep learning, cross platform ML framework
sin_op.cc
1 #include "caffe2/operators/elementwise_op.h"
2 #include "caffe2/utils/math.h"
3 
4 namespace caffe2 {
5 
6 struct SinCPUFunctor {
7  template <typename T>
8  inline void
9  operator()(const int n, const T* x, T* y, CPUContext* device_context) {
10  math::Sin<T, CPUContext>(n, x, y, device_context);
11  }
12 };
13 
15  template <typename T>
16  inline void
17  Run(const int n, const T* x, const T* dy, T* dx, CPUContext* /* unused */) {
18  ConstEigenVectorArrayMap<T> dyM(dy, n);
19  ConstEigenVectorArrayMap<T> xM(x, n);
20  EigenVectorMap<T>(dx, n) = dyM * cos(xM);
21  }
22 };
23 
24 REGISTER_CPU_OPERATOR(
25  Sin,
27 REGISTER_CPU_OPERATOR(
28  SinGradient,
31  CPUContext,
33 
34 OPERATOR_SCHEMA(Sin)
35  .NumInputs(1)
36  .NumOutputs(1)
37  .IdenticalTypeAndShape()
38  .SetDoc(R"DOC(
39 Calculates the sine of the given input tensor, element-wise.
40 )DOC")
41  .Input(0, "input", "Input tensor")
42  .Output(0, "output", "The sine of the input tensor computed element-wise");
43 
44 OPERATOR_SCHEMA(SinGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
45 
47  using GradientMakerBase::GradientMakerBase;
48  vector<OperatorDef> GetGradientDefs() override {
49  return SingleGradientDef(
50  "SinGradient",
51  "",
52  std::vector<string>{I(0), GO(0)},
53  std::vector<string>{GI(0)});
54  }
55 };
56 REGISTER_GRADIENT(Sin, GetSinGradient);
57 } // namespace caffe2
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:66
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Performs a binary operation (e.g.