Skip to content

Commit 2d38d37

Browse files
r-barnesfacebook-github-bot
authored andcommitted
use irange for loops (pytorch#69533)
Summary: Pull Request resolved: pytorch#69533 Modified loops in files under fbsource/fbcode/caffe2/ from the format ``` for(TYPE var=x0;var<x_max;x++) ``` to the format ``` for(const auto var: irange(xmax)) ``` This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand. Test Plan: Sandcastle Reviewed By: malfet Differential Revision: D32837942 fbshipit-source-id: 8663037a38ade8f81bd5e983a614d197ea11f0d1
1 parent 8a975c0 commit 2d38d37

14 files changed

+75
-71
lines changed

Diff for: caffe2/sgd/adagrad_op.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void adagrad_update_output_effective_lr(
3939
const float* lr,
4040
Context* /*context*/,
4141
float weight_decay = 0.f) {
42-
for (auto i = 0; i < N; ++i) {
42+
for (const auto i : c10::irange(N)) {
4343
float grad = std::fma(weight_decay, paramIn[i], gradIn[i]);
4444
float moment = momentOut[i] = decay * momentIn[i] + grad * grad;
4545
float effective_lr = effectiveLROut[i] =
@@ -63,7 +63,7 @@ void adagrad_update_output_effective_lr_and_update(
6363
const float* lr,
6464
Context* /*context*/,
6565
float weight_decay = 0.f) {
66-
for (auto i = 0; i < N; ++i) {
66+
for (const auto i : c10::irange(N)) {
6767
float grad = std::fma(weight_decay, paramIn[i], gradIn[i]);
6868
float moment = momentOut[i] = decay * momentIn[i] + grad * grad;
6969
float effective_lr = effectiveLROut[i] =
@@ -300,7 +300,7 @@ class SparseAdagradOp final : public Operator<CPUContext> {
300300
const auto* momentIn = Input(MOMENT_1).template data<float>();
301301

302302
std::vector<float> grad(block_size);
303-
for (auto i = 0; i < n; ++i) {
303+
for (const auto i : c10::irange(n)) {
304304
auto idx = indices[i];
305305
auto offsetI = i * block_size;
306306
auto offsetIdx = idx * block_size;
@@ -504,7 +504,7 @@ class RowWiseSparseAdagradOp final : public Operator<Context> {
504504
#else
505505
VLOG(1) << "using plain adagrad updates in RowWiseSparseAdagradOp";
506506

507-
for (auto i = 0; i < n; ++i) {
507+
for (const auto i : c10::irange(n)) {
508508
auto idx = indices[i];
509509
float freq = (counter_halflife_ > 0 && count[idx] > 0)
510510
? counter_halflife_ / count[idx]
@@ -542,13 +542,13 @@ class RowWiseSparseAdagradOp final : public Operator<Context> {
542542
const float* g = gradIn + offsetI;
543543
float* h = moment + idx;
544544
float hs = 0.;
545-
for (auto j = 0; j < block_size; ++j) {
545+
for (const auto j : c10::irange(block_size)) {
546546
float gj = std::fma(weight_decay_ * freq, w[j], g[j]);
547547
hs += gj * gj;
548548
}
549549
float hi = h[0] = h[0] + hs / block_size;
550550
float step = lr[0] / (std::sqrt(hi) + epsilon_);
551-
for (auto j = 0; j < block_size; ++j) {
551+
for (const auto j : c10::irange(block_size)) {
552552
float gj = std::fma(weight_decay_ * freq, w[j], g[j]);
553553
w[j] = w[j] + gj * step;
554554
}

Diff for: caffe2/sgd/adam_op.h

+16-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void adam_update(
2121
float correction,
2222
const float* lr,
2323
Context* /*context*/) {
24-
for (auto i = 0; i < N; ++i) {
24+
for (const auto i : c10::irange(N)) {
2525
float gi = g[i];
2626
float mi = nm[i] = m[i] * beta1 + gi * (1 - beta1);
2727
float vi = nv[i] = v[i] * beta2 + gi * gi * (1 - beta2);
@@ -45,7 +45,7 @@ void adam_compute(
4545
float correction,
4646
const float* lr,
4747
Context* /*context*/) {
48-
for (auto i = 0; i < N; ++i) {
48+
for (const auto i : c10::irange(N)) {
4949
float gi = g[i];
5050
float mi = nm[i] = m[i] * beta1 + gi * (1 - beta1);
5151
float vi = nv[i] = v[i] * beta2 + gi * gi * (1 - beta2);
@@ -74,7 +74,7 @@ void adam_compute_smart_decay(
7474
Context* /*context*/) {
7575
float k = (float)(t - lastSeenIn[0]);
7676
lastSeenOut[0] = t;
77-
for (auto i = 0; i < N; ++i) {
77+
for (const auto i : c10::irange(N)) {
7878
float gi = g[i];
7979
// The number of steps since this param was last seen.
8080
// We don't need integer precision for k. Float is fine and it's faster to convert here.
@@ -107,7 +107,7 @@ void adam_compute_output_grad(
107107
float correction,
108108
const float* lr,
109109
Context* /*context*/) {
110-
for (auto i = 0; i < N; ++i) {
110+
for (const auto i : c10::irange(N)) {
111111
float gi = g[i];
112112
float mi = nm[i] = m[i] * beta1 + gi * (1 - beta1);
113113
float vi = nv[i] = v[i] * beta2 + gi * gi * (1 - beta2);
@@ -135,7 +135,7 @@ void radam_update(
135135
float r_correction,
136136
const float* lr,
137137
Context* /*context*/) {
138-
for (auto i = 0; i < N; ++i) {
138+
for (const auto i : c10::irange(N)) {
139139
float gi = g[i];
140140
float mi = nm[i] = m[i] * beta1 + gi * (1 - beta1);
141141
float vi = nv[i] = v[i] * beta2 + gi * gi * (1 - beta2);
@@ -169,7 +169,7 @@ void radam_compute(
169169
float r_correction,
170170
const float* lr,
171171
Context* /*context*/) {
172-
for (auto i = 0; i < N; ++i) {
172+
for (const auto i : c10::irange(N)) {
173173
float gi = g[i];
174174
float mi = nm[i] = m[i] * beta1 + gi * (1 - beta1);
175175
float vi = nv[i] = v[i] * beta2 + gi * gi * (1 - beta2);
@@ -204,7 +204,7 @@ void radam_compute_output_grad(
204204
float r_correction,
205205
const float* lr,
206206
Context* /*context*/) {
207-
for (auto i = 0; i < N; ++i) {
207+
for (const auto i : c10::irange(N)) {
208208
float gi = g[i];
209209
float mi = nm[i] = m[i] * beta1 + gi * (1 - beta1);
210210
float vi = nv[i] = v[i] * beta2 + gi * gi * (1 - beta2);
@@ -350,7 +350,7 @@ class SparseAdamOp final : public Operator<Context> {
350350
auto* moment2Out = Output(OUTPUT_MOMENT_2)->template mutable_data<T>();
351351

352352
if (OutputSize() == 3) {
353-
for (auto i = 0; i < n; ++i) {
353+
for (const auto i : c10::irange(n)) {
354354
auto idx = indices[i];
355355

356356
if (block_size == 1) {
@@ -444,7 +444,7 @@ class SparseAdamOp final : public Operator<Context> {
444444
} else {
445445
Output(OUTPUT_GRAD)->ResizeLike(Input(GRAD));
446446
auto* gradOut = Output(OUTPUT_GRAD)->template mutable_data<T>();
447-
for (auto i = 0; i < n; ++i) {
447+
for (const auto i : c10::irange(n)) {
448448
auto idx = indices[i];
449449

450450
if (block_size == 1) {
@@ -593,7 +593,7 @@ class SmartDecaySparseAdamOp final : public Operator<Context> {
593593
auto* moment2Out = Output(OUTPUT_MOMENT_2)->template mutable_data<T>();
594594
int64_t* lastSeenOut = Output(OUTPUT_LAST_SEEN)->template mutable_data<int64_t>();
595595

596-
for (auto i = 0; i < n; ++i) {
596+
for (const auto i : c10::irange(n)) {
597597
auto idx = indices[i];
598598
auto offsetI = i * block_size;
599599
auto offsetIdx = idx * block_size;
@@ -673,7 +673,7 @@ class RowWiseSparseAdamOp final : public Operator<Context> {
673673
auto* moment2Out = Output(OUTPUT_MOMENT_2)->template mutable_data<T>();
674674

675675
if (OutputSize() == 3) {
676-
for (auto i = 0; i < n; ++i) {
676+
for (const auto i : c10::irange(n)) {
677677
auto idx = indices[i];
678678

679679
if (block_size == 1) {
@@ -719,13 +719,13 @@ class RowWiseSparseAdamOp final : public Operator<Context> {
719719
float* nm2 = moment2Out + idx;
720720

721721
float m2_sum = 0.;
722-
for (auto j = 0; j < block_size; ++j) {
722+
for (const auto j : c10::irange(block_size)) {
723723
float gj = g[j];
724724
m2_sum += gj * gj;
725725
}
726726
float vi = nm2[0] =
727727
m2[0] * beta2_ + (m2_sum / block_size) * (1 - beta2_);
728-
for (auto j = 0; j < block_size; ++j) {
728+
for (const auto j : c10::irange(block_size)) {
729729
float mi = nm1[j] = m1[j] * beta1_ + g[j] * (1 - beta1_);
730730
nw[j] = w[j] + lr[0] * correction * mi / (std::sqrt(vi) + epsilon_);
731731
}
@@ -734,7 +734,7 @@ class RowWiseSparseAdamOp final : public Operator<Context> {
734734
} else {
735735
Output(OUTPUT_GRAD)->ResizeLike(Input(GRAD));
736736
auto* gradOut = Output(OUTPUT_GRAD)->template mutable_data<T>();
737-
for (auto i = 0; i < n; ++i) {
737+
for (const auto i : c10::irange(n)) {
738738
auto idx = indices[i];
739739

740740
if (block_size == 1) {
@@ -781,13 +781,13 @@ class RowWiseSparseAdamOp final : public Operator<Context> {
781781
float* ng = gradOut + offsetI;
782782

783783
float m2_sum = 0.;
784-
for (auto j = 0; j < block_size; ++j) {
784+
for (const auto j : c10::irange(block_size)) {
785785
float gj = g[j];
786786
m2_sum += gj * gj;
787787
}
788788
float vi = nm2[0] =
789789
m2[0] * beta2_ + (m2_sum / block_size) * (1 - beta2_);
790-
for (auto j = 0; j < block_size; ++j) {
790+
for (const auto j : c10::irange(block_size)) {
791791
float mi = nm1[j] = m1[j] * beta1_ + g[j] * (1 - beta1_);
792792
float ngi = ng[j] = correction * mi / (std::sqrt(vi) + epsilon_);
793793
nw[j] = w[j] + lr[0] * ngi;

Diff for: caffe2/sgd/learning_rate_adaption_op.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void lr_update(
2121
float x = 0;
2222
float y = 0, z = 0;
2323
const float kEps = 1e-12f;
24-
for (auto i = 0; i < n; i++) {
24+
for (const auto i : c10::irange(n)) {
2525
x += grad[i] * effgrad[i];
2626
if (normalized_lr_adaption) {
2727
y += grad[i] * grad[i];

Diff for: caffe2/sgd/learning_rate_op.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cmath>
66
#include "caffe2/core/context.h"
77
#include "caffe2/core/export_caffe2_op_to_c10.h"
8+
#include <c10/util/irange.h>
89
#include "caffe2/core/operator.h"
910
#include "caffe2/sgd/learning_rate_functors.h"
1011

@@ -162,7 +163,7 @@ class LearningRateOp final : public Operator<Context> {
162163
sub_policy_num_iters.size(),
163164
0,
164165
"Must specify at least one sub learning rate policy.");
165-
for (size_t i = 0; i < sub_policy_num_iters.size(); ++i) {
166+
for (const auto i : c10::irange(sub_policy_num_iters.size())) {
166167
CAFFE_ENFORCE_GT(
167168
sub_policy_num_iters[i],
168169
0,

Diff for: caffe2/sgd/momentum_sgd_op.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void momentum_sgd_update(
1717
float* param,
1818
Context* /*context*/) {
1919
const float LR = lr[0];
20-
for (auto i = 0; i < N; ++i) {
20+
for (const auto i : c10::irange(N)) {
2121
if (!nesterov) {
2222
const float adjusted_gradient = LR * g[i] + momentum * m[i];
2323
nm[i] = adjusted_gradient;
@@ -154,7 +154,7 @@ class SparseMomentumSGDUpdateOp final : public Operator<Context> {
154154
auto* momentumOut = Output(OUTPUT_MOMENTUM)->template mutable_data<T>();
155155
auto* paramOut = Output(OUTPUT_PARAM)->template mutable_data<T>();
156156

157-
for (auto i = 0; i < n; ++i) {
157+
for (const auto i : c10::irange(n)) {
158158
auto idx = indices[i];
159159
auto offsetI = i * block_size;
160160
auto offsetIdx = idx * block_size;

Diff for: caffe2/sgd/rowwise_adagrad_fused.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ class RowWiseSparseAdagradFusedWithSparseLengthsSumGradientOp final
217217
auto* grad_buffer_data =
218218
is_mean ? grad_buffer_.template mutable_data<T>() : NULL;
219219
if (is_mean) {
220-
for (auto rangeIndex = 0; rangeIndex < numSegments; ++rangeIndex) {
221-
for (auto tmpIndex = 0; tmpIndex < block_size; ++tmpIndex) {
220+
for (const auto rangeIndex : c10::irange(numSegments)) {
221+
for (const auto tmpIndex : c10::irange(block_size)) {
222222
auto offsetI = rangeIndex * block_size;
223223
grad_buffer_data[offsetI + tmpIndex] = lengths[rangeIndex] > 0
224224
? gradIn[offsetI + tmpIndex] / lengths[rangeIndex]
@@ -269,7 +269,7 @@ class RowWiseSparseAdagradFusedWithSparseLengthsSumGradientOp final
269269
T counter_halflife,
270270
rowWiseAdagradT& kernel) {
271271
int dataIndex = 0;
272-
for (auto rangeIndex = 0; rangeIndex < numSegments; ++rangeIndex) {
272+
for (const auto rangeIndex : c10::irange(numSegments)) {
273273
auto offsetI = rangeIndex * block_size;
274274
const float* g = gradIn + offsetI;
275275

@@ -557,7 +557,7 @@ class RowWiseSparseAdagradFusedWithSparseLengthsWeightedSumGradientOp final
557557
// ignores this dependency and fuses these two loops.
558558
std::vector<T> temp_grad(block_size);
559559
int dataIndex = 0;
560-
for (auto rangeIndex = 0; rangeIndex < numSegments; ++rangeIndex) {
560+
for (const auto rangeIndex : c10::irange(numSegments)) {
561561
for (auto start = dataIndex; dataIndex < start + lengths[rangeIndex];
562562
++dataIndex) {
563563
std::size_t idx = indices[dataIndex];
@@ -591,7 +591,7 @@ class RowWiseSparseAdagradFusedWithSparseLengthsWeightedSumGradientOp final
591591
CAFFE_ENFORCE_EQ(dataIndex, n);
592592

593593
dataIndex = 0;
594-
for (auto rangeIndex = 0; rangeIndex < numSegments; ++rangeIndex) {
594+
for (const auto rangeIndex : c10::irange(numSegments)) {
595595
auto offsetI = rangeIndex * block_size;
596596
const float* g = gradIn + offsetI;
597597

@@ -606,7 +606,7 @@ class RowWiseSparseAdagradFusedWithSparseLengthsWeightedSumGradientOp final
606606
auto offsetIdx = idx * block_size;
607607
auto localOffset = dataIndex - start;
608608

609-
for (int i = 0; i < block_size; ++i) {
609+
for (const auto i : c10::irange(block_size)) {
610610
temp_grad[i] = auxParamIn[localOffset] * g[i];
611611
}
612612

@@ -839,7 +839,7 @@ class RowWiseSparseAdagradFusedWithSparseLengthsWeightedSumGradientApproxOp
839839

840840
std::vector<T> temp_grad(block_size);
841841
int dataIndex = 0;
842-
for (auto rangeIndex = 0; rangeIndex < numSegments; ++rangeIndex) {
842+
for (const auto rangeIndex : c10::irange(numSegments)) {
843843
auto offsetI = rangeIndex * block_size;
844844
const float* g = gradIn + offsetI;
845845

@@ -902,7 +902,7 @@ class RowWiseSparseAdagradFusedWithSparseLengthsWeightedSumGradientApproxOp
902902

903903
alignas(64) float temp[VLEN];
904904
_mm256_store_ps(temp, acc_v);
905-
for (int j = 0; j < VLEN; ++j) {
905+
for (const auto j : c10::irange(VLEN)) {
906906
acc += temp[j];
907907
}
908908
#endif

Diff for: caffe2/sgd/rowwise_counter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RowWiseCounterOp final : public Operator<CPUContext> {
4040
return true;
4141
}
4242

43-
for (auto i = 0; i < n; ++i) {
43+
for (const auto i : c10::irange(n)) {
4444
const std::size_t idx = indices[i];
4545
CAFFE_ENFORCE_GE(
4646
Input(COUNTER).numel(),

Diff for: caffe2/sgd/storm_op.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ void storm_update(
1919
const float beta,
2020
Context* /*context*/) {
2121
float gradSqSumTmp = 0.0;
22-
for (auto i = 0; i < N; ++i) {
22+
for (const auto i : c10::irange(N)) {
2323
const float gi = gradIn[i];
2424
gradSqSumTmp += gi * gi;
2525
}
2626
gradSqSumOut[0] = gradSqSumIn[0] + gradSqSumTmp;
2727

2828
const float nlr = lr[0] * std::pow(beta + gradSqSumOut[0], -1.0 / 3.0);
2929
const float alpha = momentum * nlr * nlr;
30-
for (auto i = 0; i < N; ++i) {
30+
for (const auto i : c10::irange(N)) {
3131
const float gi = gradIn[i];
3232
const float mi = momentIn[i];
3333
float new_mi = momentOut[i] = gi + (1.0 - alpha) * (mi - gi);
@@ -120,7 +120,7 @@ class SparseStormOp final : public Operator<Context> {
120120
}
121121

122122
float gradSqSumTmp = 0.0;
123-
for (auto i = 0; i < Input(GRAD).numel(); ++i) {
123+
for (const auto i : c10::irange(Input(GRAD).numel())) {
124124
const float gi = gradIn[i];
125125
gradSqSumTmp += gi * gi;
126126
}
@@ -130,7 +130,7 @@ class SparseStormOp final : public Operator<Context> {
130130
const float alpha = momentum_ * nlr * nlr;
131131
const auto block_size = Input(GRAD).numel() / n;
132132

133-
for (auto i = 0; i < n; ++i) {
133+
for (const auto i : c10::irange(n)) {
134134
auto idx = indices[i];
135135
if (block_size == 1) {
136136
const float gi = gradIn[i];
@@ -162,7 +162,7 @@ class SparseStormOp final : public Operator<Context> {
162162
i);
163163
#endif
164164

165-
for (auto j = 0; j < block_size; ++j) {
165+
for (const auto j : c10::irange(block_size)) {
166166
const float gi = gradIn[offsetI + j];
167167
const float mi = momentIn[offsetIdx + j];
168168
float new_mi = momentOut[offsetIdx + j] =

0 commit comments

Comments
 (0)