Caffe2 - C++ API
A deep learning, cross platform ML framework
rsqrt_op.cc
1 #include "caffe2/operators/rsqrt_op.h"
2 
3 #include "caffe2/utils/eigen_utils.h"
4 
5 #include <algorithm>
6 #include <functional>
7 #include <string>
8 
9 namespace caffe2 {
10 
11 template <>
12 template <typename T>
13 bool RsqrtGradientFunctor<CPUContext>::Forward(
14  const std::vector<int>& dY_dims,
15  const std::vector<int>& /* Y_dims */,
16  const T* dY,
17  const T* Y,
18  T* dX,
19  CPUContext* /* context */) const {
20  const int size = std::accumulate(
21  dY_dims.cbegin(), dY_dims.cend(), 1, std::multiplies<int>());
22  EigenVectorMap<T>(dX, size) = ConstEigenVectorMap<T>(dY, size).array() *
23  ConstEigenVectorMap<T>(Y, size).array().cube() * static_cast<T>(-0.5);
24  return true;
25 }
26 
27 REGISTER_CPU_OPERATOR(
28  Rsqrt,
29  UnaryElementwiseOp<
30  TensorTypes<float>,
31  CPUContext,
32  RsqrtFunctor<CPUContext>>);
33 REGISTER_CPU_OPERATOR(
34  RsqrtGradient,
35  BinaryElementwiseOp<
36  TensorTypes<float>,
37  CPUContext,
38  RsqrtGradientFunctor<CPUContext>>);
39 
40 OPERATOR_SCHEMA(Rsqrt)
41  .NumInputs(1)
42  .NumOutputs(1)
43  .AllowInplace({{0, 0}})
44  .IdenticalTypeAndShape()
45  .SetDoc("Computes the element-wise rsqrt of the input.")
46  .Input(0, "X", "ND input tensor")
47  .Output(0, "Y", "ND output tensor");
48 
49 OPERATOR_SCHEMA(RsqrtGradient)
50  .NumInputs(2)
51  .NumOutputs(1)
52  .AllowInplace({{0, 0}});
53 
54 namespace {
55 
56 class GetRsqrtGradient final : public GradientMakerBase {
57  using GradientMakerBase::GradientMakerBase;
58 
59  std::vector<OperatorDef> GetGradientDefs() override {
60  return SingleGradientDef(
61  "RsqrtGradient",
62  "",
63  std::vector<std::string>{GO(0), O(0)},
64  std::vector<std::string>{GI(0)});
65  }
66 };
67 
68 } // namespace
69 
70 REGISTER_GRADIENT(Rsqrt, GetRsqrtGradient);
71 
72 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13