Caffe2 - C++ API
A deep learning, cross platform ML framework
abs_op.cc
1 
17 #include "caffe2/operators/elementwise_op.h"
18 #include "caffe2/utils/math.h"
19 
20 namespace caffe2 {
21 
22 struct AbsCPUFunctor {
23  template <typename T>
24  inline void
25  operator()(const int n, const T* x, T* y, CPUContext* device_context) {
26  math::Abs<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) =
37  (xM == T(0)).select(T(0), (xM > T(0)).select(dyM, -dyM));
38  }
39 };
40 
41 REGISTER_CPU_OPERATOR(
42  Abs,
44 REGISTER_CPU_OPERATOR(
45  AbsGradient,
48  CPUContext,
50 
51 OPERATOR_SCHEMA(Abs)
52  .NumInputs(1)
53  .NumOutputs(1)
54  .IdenticalTypeAndShape()
55  .SetDoc(R"DOC(
56 Calculates the absolute value of the given input tensor, element-wise.
57 )DOC")
58  .Input(0, "input", "Input tensor")
59  .Output(
60  0,
61  "output",
62  "The absolute value of the input tensor computed element-wise");
63 
64 OPERATOR_SCHEMA(AbsGradient).NumInputs(2).NumOutputs(1).IdenticalTypeAndShape();
65 
67  using GradientMakerBase::GradientMakerBase;
68  vector<OperatorDef> GetGradientDefs() override {
69  return SingleGradientDef(
70  "AbsGradient",
71  "",
72  std::vector<string>{I(0), GO(0)},
73  std::vector<string>{GI(0)});
74  }
75 };
76 REGISTER_GRADIENT(Abs, GetAbsGradient);
77 } // 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.