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