Caffe2 - C++ API
A deep learning, cross platform ML framework
cos_op.cc
1 #include "caffe2/operators/elementwise_op.h"
2 #include "caffe2/utils/math.h"
3 
4 namespace caffe2 {
5 
6 struct CosCPUFunctor {
7  template <typename T>
8  inline void
9  operator()(const int n, const T* x, T* y, CPUContext* device_context) {
10  math::Cos<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 * sin(xM);
21  }
22 };
23 
24 REGISTER_CPU_OPERATOR(
25  Cos,
27 REGISTER_CPU_OPERATOR(
28  CosGradient,
31  CPUContext,
33 
34 OPERATOR_SCHEMA(Cos)
35  .NumInputs(1)
36  .NumOutputs(1)
37  .IdenticalTypeAndShape()
38  .SetDoc(R"DOC(
39 Calculates the cosine of the given input tensor, element-wise.
40 )DOC")
41  .Input(0, "input", "Input tensor")
42  .Output(
43  0,
44  "output",
45  "The cosine of the input tensor computed element-wise");
46 
47 OPERATOR_SCHEMA(CosGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
48 
50  using GradientMakerBase::GradientMakerBase;
51  vector<OperatorDef> GetGradientDefs() override {
52  return SingleGradientDef(
53  "CosGradient",
54  "",
55  std::vector<string>{I(0), GO(0)},
56  std::vector<string>{GI(0)});
57  }
58 };
59 REGISTER_GRADIENT(Cos, GetCosGradient);
60 } // 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.