File: adagrad_op.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (257 lines) | stat: -rw-r--r-- 9,501 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "adagrad_op.h"
#include "caffe2/core/types.h"

namespace caffe2 {

static OpSchema::Cost CostInferenceForAdagrad(
    const OperatorDef& def,
    const vector<TensorShape>& inputs) {
  CAFFE_ENFORCE_GE(inputs.size(), 4, "Adagrad requires at least 4 inputs");

  const TensorShape param = inputs[0];
  const TensorShape moment = inputs[1];
  const TensorShape grad = inputs[2];
  const TensorShape lr = inputs[3];

  uint64_t grad_size = nElemFromDim(grad);
  int output_size = def.output_size();

  OpSchema::Cost c;
  // +2: applying weight decay and add to grads
  // +3: updading moments
  // +3: updating effective lr (including 1 sqrt)
  // +2: updating params
  // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
  c.flops = grad_size * 10;

  auto const& moment_element_size_byte =
      DataTypeToTypeMeta(moment.data_type()).itemsize();
  auto const& param_element_size_byte =
      DataTypeToTypeMeta(param.data_type()).itemsize();
  auto const& grad_element_size_byte =
      DataTypeToTypeMeta(grad.data_type()).itemsize();
  auto const& lr_element_size_byte =
      DataTypeToTypeMeta(lr.data_type()).itemsize();
  uint64_t bytes_written =
      grad_size * param_element_size_byte + moment_element_size_byte;

  if (output_size == 3) {
    // also need to output effective learning rate in this case
    // assume it's the same data type as lr
    bytes_written += grad_size * lr_element_size_byte;
  } else if (output_size == 4) {
    // also need to output effective learning rate and updates in this case
    // assume update is the same data type as param
    bytes_written +=
        grad_size * (lr_element_size_byte + param_element_size_byte);
  }
  c.bytes_written = bytes_written;
  c.bytes_read = c.bytes_written +
      grad_size * (grad_element_size_byte + lr_element_size_byte);

  return c;
}

REGISTER_CPU_OPERATOR(Adagrad, AdagradOp<CPUContext>);
// For backward compatibility
REGISTER_CPU_OPERATOR_WITH_ENGINE(Adagrad, SIMD, AdagradOp<CPUContext>);
OPERATOR_SCHEMA(Adagrad)
    .NumInputs(4)
    .NumOutputs(2, 4)
    .AllowInplace({{0, 0}, {1, 1}})
    .SetDoc(R"DOC(

Computes the AdaGrad update for an input gradient and accumulated
history. Concretely, given inputs (param, grad, moment, learning_rate),
computes

    new_moment = moment + square(grad)
    effective_lr = learning_rate / (sqrt(new_moment) + epsilon)
    update = learning_rate * grad / (sqrt(new_moment) + epsilon)
    new_param = param + update
and returns (new_param, new_moment).

Optionally returns effective_lr and update as well.

)DOC")
    .Input(0, "param", "Parameters to be updated")
    .Input(1, "moment", "Moment history")
    .Input(2, "grad", "Gradient computed")
    .Input(3, "lr", "learning rate")
    .Output(0, "output_param", "Updated parameters")
    .Output(1, "output_moment", "Updated moment")
    .Output(2, "output_effective_lr", "(optional) Effective learning rate")
    .Output(3, "output_update", "(optional) Actual update that is applied.")

    .Arg("epsilon", "Default 1e-5")
    .Arg(
        "decay",
        "Default 1. If it is in (0, 1), the gradient square sum "
        "is decayed by this factor.")
    .CostInferenceFunction(
        OpSchema::CostInferenceFunctionType(CostInferenceForAdagrad));

static OpSchema::Cost CostInferenceForSparseAdagrad(
    const OperatorDef& /* unused */,
    const vector<TensorShape>& inputs) {
  CAFFE_ENFORCE_GE(
      inputs.size(), 4, "SparseAdagrad requires at least 4 inputs");

  const TensorShape param = inputs[0];
  const TensorShape moment = inputs[1];
  const TensorShape indices = inputs[2];
  const TensorShape grad = inputs[3];

  uint64_t n = nElemFromDim(indices);
  uint64_t grad_size = nElemFromDim(grad);

  OpSchema::Cost c;
  // See adagrad_op.h (note that decay is 1 for SparseAdagrad).
  // 2 multiplications, 3 additions, 1 division, and 1 sqrt
  // (optimistically count sqrt as one flop).
  // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
  c.flops = grad_size * 7;
  auto const& param_element_size_byte =
      DataTypeToTypeMeta(param.data_type()).itemsize();
  auto const& moment_element_size_byte =
      DataTypeToTypeMeta(moment.data_type()).itemsize();
  c.bytes_written =
      grad_size * (param_element_size_byte + moment_element_size_byte);
  auto const& grad_element_size_byte =
      DataTypeToTypeMeta(grad.data_type()).itemsize();
  auto const& indices_element_size_byte =
      DataTypeToTypeMeta(indices.data_type()).itemsize();
  c.bytes_read = c.bytes_written + grad_size * grad_element_size_byte +
      n * indices_element_size_byte;

  return c;
}

REGISTER_CPU_OPERATOR(SparseAdagrad, SparseAdagradOp);
// For backward compatibility
REGISTER_CPU_OPERATOR_WITH_ENGINE(SparseAdagrad, SIMD, SparseAdagradOp);
OPERATOR_SCHEMA(SparseAdagrad)
    // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
    .NumInputs(5)
    .NumOutputs(2)
    .EnforceOneToOneInplace()
    .SetDoc(R"DOC(

Given inputs (param, moment, indices, grad, lr), runs the dense AdaGrad
update on (param, grad, moment[indices], lr), and returns (new_param,
new_moment) as in the dense case.

)DOC")
    .Input(0, "param", "Parameters to be updated")
    .Input(1, "moment", "Moment history")
    .Input(2, "indices", "Sparse indices")
    .Input(3, "grad", "Gradient computed")
    .Input(4, "lr", "learning rate")
    .Output(0, "output_param", "Updated parameters")
    .Output(1, "output_moment_1", "Updated moment")
    .Arg("epsilon", "Default 1e-5")
    .CostInferenceFunction(
        OpSchema::CostInferenceFunctionType(CostInferenceForSparseAdagrad));

static OpSchema::Cost CostInferenceForRowWiseSparseAdagrad(
    const OperatorDef& /* unused */,
    const vector<TensorShape>& inputs) {
  CAFFE_ENFORCE_GE(
      inputs.size(), 5, "RowWiseSparseAdagrad requires at least 4 inputs");

  const TensorShape param = inputs[0];
  const TensorShape moment = inputs[1];
  const TensorShape indices = inputs[2];
  const TensorShape grad = inputs[3];
  const TensorShape lr = inputs[4];

  uint64_t n = nElemFromDim(indices);
  uint64_t grad_size = nElemFromDim(grad);
  OpSchema::Cost c;

  if (n > 0) {
    auto const& param_element_size_byte =
        DataTypeToTypeMeta(param.data_type()).itemsize();
    auto const& moment_element_size_byte =
        DataTypeToTypeMeta(moment.data_type()).itemsize();
    auto const& grad_element_size_byte =
        DataTypeToTypeMeta(grad.data_type()).itemsize();
    auto const& indices_element_size_byte =
        DataTypeToTypeMeta(indices.data_type()).itemsize();
    auto const& lr_element_size_byte =
        DataTypeToTypeMeta(lr.data_type()).itemsize();
    auto block_size = grad_size / n;
    if (block_size == 1) {
      // +2: applying weight decay and add to grads
      // +2: updading moments
      // +5: updating params
      // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
      c.flops = n * 9;
      c.bytes_written =
          n * (param_element_size_byte + moment_element_size_byte);
      c.bytes_read = c.bytes_written +
          n *
              (grad_element_size_byte + indices_element_size_byte +
               lr_element_size_byte);
    } else {
      // 5 per block (not counting index transforms)
      // 8 for each value of a block
      // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
      c.flops = n * (5 + (block_size * 8));
      c.bytes_written = n * moment_element_size_byte +
          n * block_size * param_element_size_byte;

      c.bytes_read = c.bytes_written + n * lr_element_size_byte +
          2 * n * block_size *
              (grad_element_size_byte + param_element_size_byte);
    }
  }
  return c;
}

REGISTER_CPU_OPERATOR(RowWiseSparseAdagrad, RowWiseSparseAdagradOp<CPUContext>);
// For backward compatibility
REGISTER_CPU_OPERATOR_WITH_ENGINE(
    RowWiseSparseAdagrad,
    SIMD,
    RowWiseSparseAdagradOp<CPUContext>);
OPERATOR_SCHEMA(RowWiseSparseAdagrad)
    // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
    .NumInputs(5, 6)
    .NumOutputs(2)
    .EnforceOneToOneInplace()
    .SetDoc(R"DOC(

Given inputs (param, moment, indices, grad, lr), runs a modified sparse Adagrad
update on (param, grad, moment[indices], lr), and returns (new_param,
new_momwnr), where moment is a 1D tensor with length equal to the number of
rows in param: shape(moment) == shape(param)[0]. Each element of moment is
applied to an entire row of param, and the new moment is calculated by adding
the average squared sum of gradients across each row. Note that indices must
also be a 1D tensor indexing into the rows of param.

)DOC")
    .Input(0, "param", "Parameters to be updated")
    .Input(1, "moment", "Moment history")
    .Input(2, "indices", "Sparse indices")
    .Input(3, "grad", "Gradient computed")
    .Input(4, "lr", "learning rate")
    .Input(
        5,
        "counter",
        "Optional input when weight_decay is adjusted by frequency ignored "
        "when counter_halflife == -1")
    .Output(0, "output_param", "Updated parameters")
    .Output(1, "output_moment_1", "Updated moment")
    .Arg("epsilon", "Default 1e-5")
    .Arg("weight_decay", "Default 0")
    .Arg(
        "counter_halflife",
        "Optional arg when weight_decay is adjusted by frequency (default -1)")
    .CostInferenceFunction(OpSchema::CostInferenceFunctionType(
        CostInferenceForRowWiseSparseAdagrad));

SHOULD_NOT_DO_GRADIENT(Adagrad);
SHOULD_NOT_DO_GRADIENT(SparseAdagrad);
SHOULD_NOT_DO_GRADIENT(RowWiseSparseAdagrad);
} // namespace caffe2