Caffe2 - C++ API
A deep learning, cross platform ML framework
sin_op.cc
1 
17 #include "caffe2/operators/elementwise_op.h"
18 #include "caffe2/utils/math.h"
19 
20 namespace caffe2 {
21 
22 struct SinCPUFunctor {
23  template <typename T>
24  inline void
25  operator()(const int n, const T* x, T* y, CPUContext* device_context) {
26  math::Sin<T, CPUContext>(n, x, y, device_context);
27  }
28 };
29 
31  template <typename T>
32  inline void
33  Run(const int n, const T* x, const T* dy, T* dx, CPUContext* /* unused */) {
34  ConstEigenVectorArrayMap<T> dyM(dy, n);
35  ConstEigenVectorArrayMap<T> xM(x, n);
36  EigenVectorMap<T>(dx, n) = dyM * cos(xM);
37  }
38 };
39 
40 REGISTER_CPU_OPERATOR(
41  Sin,
43 REGISTER_CPU_OPERATOR(
44  SinGradient,
47  CPUContext,
49 
50 OPERATOR_SCHEMA(Sin)
51  .NumInputs(1)
52  .NumOutputs(1)
53  .IdenticalTypeAndShape()
54  .SetDoc(R"DOC(
55 Calculates the sine of the given input tensor, element-wise.
56 )DOC")
57  .Input(0, "input", "Input tensor")
58  .Output(0, "output", "The sine of the input tensor computed element-wise");
59 
60 OPERATOR_SCHEMA(SinGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
61 
63  using GradientMakerBase::GradientMakerBase;
64  vector<OperatorDef> GetGradientDefs() override {
65  return SingleGradientDef(
66  "SinGradient",
67  "",
68  std::vector<string>{I(0), GO(0)},
69  std::vector<string>{GI(0)});
70  }
71 };
72 REGISTER_GRADIENT(Sin, GetSinGradient);
73 } // namespace caffe2
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:82
Copyright (c) 2016-present, Facebook, Inc.
Performs a binary operation (e.g.