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