Caffe2 - C++ API
A deep learning, cross platform ML framework
softmax_op.cc
1 #include "caffe2/operators/softmax_op.h"
2 #include "caffe2/operators/softmax_shared.h"
3 
4 namespace caffe2 {
5 
6 // Implementation for the CPU context.
7 template <>
8 bool SoftmaxOp<float, CPUContext>::RunOnDevice() {
9  auto& X = Input(0);
10  auto* Y = Output(0);
11  const auto canonical_axis = X.canonical_axis_index(axis_);
12  const int N = X.size_to_dim(canonical_axis);
13  const int D = X.size_from_dim(canonical_axis);
14  Y->ResizeLike(X);
15  float* Ydata = Y->mutable_data<float>();
16  // First, get scales
17  if (scale_.size() != N) {
18  scale_.Resize(N);
19  }
20  if (rowmax_.size() != N) {
21  rowmax_.Resize(N);
22  }
23  if (sum_multiplier_.size() != D) {
24  sum_multiplier_.Resize(D);
25  math::Set<float, CPUContext>(D, 1.f, sum_multiplier_.mutable_data<float>(),
26  &context_);
27  }
28 
29  SoftmaxCPU(
30  context_,
31  N,
32  D,
33  X.data<float>(),
34  Ydata,
35  scale_.mutable_data<float>(),
36  sum_multiplier_.data<float>(),
37  false,
38  rowmax_.mutable_data<float>());
39  return true;
40 }
41 
42 // Implementation for the CPU context.
43 template <>
44 bool SoftmaxGradientOp<float, CPUContext>::RunOnDevice() {
45  auto& Y = Input(0);
46  auto& dY = Input(1);
47  auto* dX = Output(0);
48  const auto canonical_axis = Y.canonical_axis_index(axis_);
49  const int N = Y.size_to_dim(canonical_axis);
50  const int D = Y.size_from_dim(canonical_axis);
51  // First, get scales
52  if (scale_.size() != N) {
53  scale_.Resize(N);
54  }
55  if (sum_multiplier_.size() != D) {
56  sum_multiplier_.Resize(D);
57  math::Set<float, CPUContext>(D, 1.f, sum_multiplier_.mutable_data<float>(),
58  &context_);
59  }
60  dX->ResizeLike(Y);
61  const float* Ydata = Y.data<float>();
62  const float* dYdata = dY.data<float>();
63  float* dXdata = dX->mutable_data<float>();
64  context_.Copy<float, CPUContext, CPUContext>(Y.size(), dYdata, dXdata);
65  float* scaledata = scale_.mutable_data<float>();
66  for (int i = 0; i < N; ++i) {
67  math::Dot<float, CPUContext>(D, Ydata + i * D, dYdata + i * D,
68  scaledata + i, &context_);
69  }
70  math::Gemm<float, CPUContext>(CblasNoTrans, CblasNoTrans, N, D, 1, -1,
71  scaledata, sum_multiplier_.data<float>(), 1,
72  dXdata, &context_);
73  math::Mul<float, CPUContext>(Y.size(), dXdata, Ydata, dXdata,
74  &context_);
75  return true;
76 }
77 
78 REGISTER_CPU_OPERATOR(Softmax, SoftmaxOp<float, CPUContext>);
79 REGISTER_CPU_OPERATOR(SoftmaxGradient, SoftmaxGradientOp<float, CPUContext>);
80 
81 OPERATOR_SCHEMA(Softmax)
82  .NumInputs(1)
83  .NumOutputs(1)
84  .IdenticalTypeAndShape()
85  .SetDoc(R"DOC(
86 The operator computes the softmax normalized values for each layer in the batch
87  of the given input. The input is a 2-D tensor (Tensor<float>) of size
88 (batch_size x input_feature_dimensions). The output tensor has the same shape
89 and contains the softmax normalized values of the corresponding input.
90 
91 X does not need to explicitly be a 2D vector; rather, it will be
92 coerced into one. For an arbitrary n-dimensional tensor
93 X \in [a_0, a_1, ..., a_{k-1}, a_k, ..., a_{n-1}] and k is
94 the axis provided, then X will be coerced into a 2-dimensional tensor with
95 dimensions [a_0 * ... * a_{k-1}, a_k * ... * a_{n-1}]. For the default
96 case where axis=1, this means the X tensor will be coerced into a 2D tensor
97 of dimensions [a_0, a_1 * ... * a_{n-1}], where a_0 is often the batch size.
98 In this situation, we must have a_0 = N and a_1 * ... * a_{n-1} = D.
99 Each of these dimensions must be matched correctly, or else the operator
100 will throw errors.
101 )DOC")
102  .Arg("axis",
103  "(int) default to 1; describes the axis of the inputs when coerced "
104  "to 2D; defaults to one because the 0th axis most likely describes "
105  "the batch_size")
106  .Input(0, "input",
107  "The input tensor that's coerced into a 2D matrix of size (NxD) "
108  "as described above.")
109  .Output(0, "output", "The softmax normalized output values with the same "
110  "shape as input tensor.")
111  .InheritOnnxSchema("Softmax");
112 
113 // Input: Y, dY. Output: dX
114 OPERATOR_SCHEMA(SoftmaxGradient).NumInputs(2).NumOutputs(1);
115 
117  using GradientMakerBase::GradientMakerBase;
118  vector<OperatorDef> GetGradientDefs() override {
119  return SingleGradientDef(
120  def_.type() + "Gradient", "",
121  vector<string>{O(0), GO(0)},
122  vector<string>{GI(0)});
123  }
124 };
125 REGISTER_GRADIENT(Softmax, GetSoftmaxGradient);
126 REGISTER_GRADIENT(SoftmaxFp16, GetSoftmaxGradient);
127 
128 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...