Skip to content

Commit 29d7599

Browse files
r-barnesfacebook-github-bot
authored andcommitted
use irange for loops 2 (pytorch#66746)
Summary: Pull Request resolved: pytorch#66746 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: D31705361 fbshipit-source-id: 33fd22eb03086d114e2c98e56703e8ec84460268
1 parent 91d16cb commit 29d7599

File tree

96 files changed

+19710
-19683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+19710
-19683
lines changed

Diff for: aten/src/ATen/native/quantized/cpu/qlinear.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ at::Tensor PackedLinearWeightsQnnp::apply_impl(
303303
w_zero_points[0]);
304304
auto* qnnp_w_data = qnnp_weight.data_ptr<c10::quint8>();
305305
auto wt_numel = weight_contig.numel();
306-
for (int i = 0; i < wt_numel; ++i) {
306+
for (const auto i : c10::irange(wt_numel)) {
307307
qnnp_w_data[i] = static_cast<c10::quint8>(w_data[i] + 128);
308308
}
309309
// Original bias was float, so we requantize it here.

Diff for: aten/src/ATen/native/quantized/cpu/qlinear_dynamic.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ at::Tensor PackedLinearWeightsQnnp::apply_dynamic_impl(
301301
auto* qnnp_w_data = qnnp_weight.data_ptr<c10::quint8>();
302302
int8_t* w_data = (int8_t*)weight_contig.data_ptr<c10::qint8>();
303303
auto wt_numel = weight_contig.numel();
304-
for (int i = 0; i < wt_numel; ++i) {
304+
for (const auto i : c10::irange(wt_numel)) {
305305
qnnp_w_data[i] = static_cast<c10::quint8>(w_data[i] + 128);
306306
}
307307

Diff for: aten/src/ATen/native/quantized/cpu/qpool.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ATen/native/quantized/cpu/quantized_ops.h>
1010
#include <ATen/native/quantized/cpu/init_qnnpack.h>
1111
#include <ATen/native/quantized/cpu/qnnpack_utils.h>
12+
#include <c10/util/irange.h>
1213
#include <caffe2/utils/threadpool/pthreadpool-cpp.h>
1314

1415
#include <algorithm>
@@ -43,7 +44,7 @@ void spatial_dilated_max_pooling(
4344
int64_t dW, // dilation
4445
T* oData) { // output arrays (data and max-index)
4546
at::parallel_for(0, iC, 0, [&](int64_t start, int64_t end) {
46-
for (auto p = start; p < end; ++p) {
47+
for (const auto p : c10::irange(start, end)) {
4748
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
4849
int64_t row, col;
4950
const T* i_p = iData + p * iW * iH;
@@ -195,7 +196,7 @@ Tensor q_maxpool_2d(
195196
oData);
196197
} else {
197198
at::parallel_for(0, nbatch, 0, [&](int64_t start, int64_t end) {
198-
for (auto p = start; p < end; ++p) {
199+
for (const auto p : c10::irange(start, end)) {
199200
auto* iData = qxd + p * iC * iW * iH;
200201
auto* oData = qyd + p * oC * oW * oH;
201202
spatial_dilated_max_pooling<Q>(

Diff for: aten/src/ATen/native/quantized/cpu/qrelu.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ATen/native/quantized/cpu/init_qnnpack.h>
77
#include <ATen/native/quantized/cpu/qnnpack_utils.h>
88
#include <ATen/native/quantized/cpu/quantized_ops.h>
9+
#include <c10/util/irange.h>
910
#include <caffe2/utils/threadpool/pthreadpool-cpp.h>
1011
#include <torch/library.h>
1112

@@ -30,7 +31,7 @@ Tensor qnnpack_relu(Tensor input) {
3031
initQNNPACK();
3132

3233
size_t num_elems = 1;
33-
for (int i = 1; i < input_contig.ndimension(); ++i) {
34+
for (const auto i : c10::irange(1, input_contig.ndimension())) {
3435
num_elems *= input_contig.size(i);
3536
}
3637

Diff for: aten/src/ATen/native/quantized/cpu/qsigmoid.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ATen/native/quantized/cpu/quantized_ops.h>
88
#include <ATen/native/quantized/cpu/init_qnnpack.h>
99
#include <ATen/native/quantized/cpu/qnnpack_utils.h>
10+
#include <c10/util/irange.h>
1011
#include <caffe2/utils/threadpool/pthreadpool-cpp.h>
1112

1213
#include <algorithm>
@@ -26,7 +27,7 @@ Tensor qnnpack_sigmoid(
2627

2728
Tensor input_contig = input.contiguous(input.suggest_memory_format());
2829
size_t num_elems = 1;
29-
for (int i = 1; i < input_contig.ndimension(); ++i) {
30+
for (const auto i : c10::irange(1, input_contig.ndimension())) {
3031
num_elems *= input_contig.size(i);
3132
}
3233

Diff for: aten/src/ATen/native/quantized/cpu/qtanh.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ATen/native/quantized/cpu/quantized_ops.h>
88
#include <ATen/native/quantized/cpu/init_qnnpack.h>
99
#include <ATen/native/quantized/cpu/qnnpack_utils.h>
10+
#include <c10/util/irange.h>
1011
#include <caffe2/utils/threadpool/pthreadpool-cpp.h>
1112

1213
#include <algorithm>
@@ -29,7 +30,7 @@ Tensor qnnpack_tanh(Tensor input) {
2930

3031
Tensor input_contig = input.contiguous(input.suggest_memory_format());
3132
size_t num_elems = 1;
32-
for (int i = 1; i < input_contig.ndimension(); ++i) {
33+
for (const auto i : c10::irange(1, input_contig.ndimension())) {
3334
num_elems *= input_contig.size(i);
3435
}
3536
const auto zero_point = input_contig.q_zero_point();

Diff for: aten/src/ATen/native/quantized/cpu/quant_utils.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <ATen/ATen.h>
4+
#include <c10/util/irange.h>
45
#include <algorithm>
56
#include <cmath>
67

@@ -193,7 +194,7 @@ static C10_UNUSED torch::List<int64_t> MakeArgForConv1d(const torch::List<int64_
193194
inline void HandleWeightsSaturation(int64_t N, float* weight) {
194195
const float kFp16Max = RawUint16ToFp16(0x7BFF);
195196
bool found_out_of_range = false;
196-
for (int64_t i = 0; i < N; ++i) {
197+
for (const auto i : c10::irange(N)) {
197198
bool saturate = CheckAndSaturate<float>(kFp16Max, weight + i);
198199
if (saturate) {
199200
found_out_of_range = true;

Diff for: aten/src/ATen/native/quantized/cpu/qupsample_bilinear2d.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <ATen/native/UpSample.h>
33
#include <ATen/native/quantized/affine_quantizer.h>
44
#include <ATen/native/quantized/cpu/quantized_ops.h>
5+
#include <c10/util/irange.h>
56

67
#include <algorithm>
78
#include <cmath>
@@ -57,7 +58,7 @@ static void upsample_bilinear2d_out_frame(
5758
const int64_t input_q_zero_point = input.q_zero_point();
5859
const int64_t output_q_zero_point = output.q_zero_point();
5960

60-
for (int64_t h2 = 0; h2 < output_height; ++h2) {
61+
for (const auto h2 : c10::irange(output_height)) {
6162
const auto h1r = area_pixel_compute_source_index<float>(
6263
rheight, h2, align_corners, /*cubic=*/false);
6364

@@ -67,7 +68,7 @@ static void upsample_bilinear2d_out_frame(
6768
const float h1lambda = h1r - h1;
6869
const float h0lambda = static_cast<float>(1.) - h1lambda;
6970

70-
for (int64_t w2 = 0; w2 < output_width; ++w2) {
71+
for (const auto w2 : c10::irange(output_width)) {
7172
const auto w1r = area_pixel_compute_source_index<float>(
7273
rwidth, w2, align_corners, /*cubic=*/false);
7374

@@ -79,7 +80,8 @@ static void upsample_bilinear2d_out_frame(
7980
const typename scalar_t::underlying* pos1 = i_p + h1 * input_width + w1;
8081
typename scalar_t::underlying* pos2 = o_p + h2 * output_width + w2;
8182

82-
for (int64_t c = 0; c < channels; ++c) {
83+
for (const auto c : c10::irange(channels)) {
84+
(void)c; //Suppress unused variable warning
8385
float result = h0lambda * (w0lambda * pos1[0] + w1lambda * pos1[w1p]) +
8486
h1lambda *
8587
(w0lambda * pos1[h1p * input_width] +

Diff for: aten/src/ATen/native/quantized/cpu/qupsample_nearest2d.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@ static void upsample_nearest2d_out_frame(
4444
return;
4545
}
4646

47-
for (int64_t h2 = 0; h2 < output_height; ++h2) {
47+
for (const auto h2 : c10::irange(output_height)) {
4848
const int64_t h1 =
4949
nn_compute_source_index_fn(height_scale, h2, input_height);
5050

51-
for (int64_t w2 = 0; w2 < output_width; ++w2) {
51+
for (const auto w2 : c10::irange(output_width)) {
5252
const int64_t w1 =
5353
nn_compute_source_index_fn(width_scale, w2, input_width);
5454

5555
const auto* pos1 = &i_p[h1 * input_width + w1];
5656
auto* pos2 = &o_p[h2 * output_width + w2];
5757

58-
for (int64_t c = 0; c < channels; ++c) {
58+
for (const auto c : c10::irange(channels)) {
59+
(void)c; //Suppress unused variable warning
5960
pos2[0] = pos1[0];
6061
pos1 += input_height * input_width;
6162
pos2 += output_height * output_width;
@@ -88,11 +89,11 @@ static void upsample_nearest2d_out_frame_nhwc(
8889
return;
8990
}
9091

91-
for (int64_t h2 = 0; h2 < output_height; ++h2) {
92+
for (const auto h2 : c10::irange(output_height)) {
9293
const int64_t h1 =
9394
nn_compute_source_index_fn(height_scale, h2, input_height);
9495

95-
for (int64_t w2 = 0; w2 < output_width; ++w2) {
96+
for (const auto w2 : c10::irange(output_width)) {
9697
const int64_t w1 =
9798
nn_compute_source_index_fn(width_scale, w2, input_width);
9899

Diff for: aten/src/ATen/native/quantized/cpu/qupsample_nearest3d.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,23 @@ static void upsample_nearest3d_out_frame(
4848
return;
4949
}
5050

51-
for (int64_t d2 = 0; d2 < output_depth; ++d2) {
51+
for (const auto d2 : c10::irange(output_depth)) {
5252
const int64_t d1 =
5353
nn_compute_source_index_fn(depth_scale, d2, input_depth);
5454

55-
for (int64_t h2 = 0; h2 < output_height; ++h2) {
55+
for (const auto h2 : c10::irange(output_height)) {
5656
const int64_t h1 =
5757
nn_compute_source_index_fn(height_scale, h2, input_height);
5858

59-
for (int64_t w2 = 0; w2 < output_width; ++w2) {
59+
for (const auto w2 : c10::irange(output_width)) {
6060
const int64_t w1 =
6161
nn_compute_source_index_fn(width_scale, w2, input_width);
6262

6363
const auto* pos1 = &i_p[d1 * input_height * input_width + h1 * input_width + w1];
6464
auto* pos2 = &o_p[d2 * output_height * output_width + h2 * output_width + w2];
6565

66-
for (int64_t c = 0; c < channels; ++c) {
66+
for (const auto c : c10::irange(channels)) {
67+
(void)c; //Suppress unused variable warning
6768
pos2[0] = pos1[0];
6869
pos1 += input_depth * input_height * input_width;
6970
pos2 += output_depth * output_height * output_width;
@@ -101,14 +102,14 @@ static void upsample_nearest3d_out_frame_nhwc(
101102
return;
102103
}
103104

104-
for (int64_t d2 = 0; d2 < output_depth; ++d2) {
105+
for (const auto d2 : c10::irange(output_depth)) {
105106
const int64_t d1 =
106107
nn_compute_source_index_fn(depth_scale, d2, input_depth);
107-
for (int64_t h2 = 0; h2 < output_height; ++h2) {
108+
for (const auto h2 : c10::irange(output_height)) {
108109
const int64_t h1 =
109110
nn_compute_source_index_fn(height_scale, h2, input_height);
110111

111-
for (int64_t w2 = 0; w2 < output_width; ++w2) {
112+
for (const auto w2 : c10::irange(output_width)) {
112113
const int64_t w1 =
113114
nn_compute_source_index_fn(width_scale, w2, input_width);
114115

Diff for: aten/src/ATen/native/quantized/fake_quant_per_channel_affine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ std::tuple<Tensor, Tensor, Tensor> _fake_quantize_learnable_per_channel_affine_b
218218
// into the same shapes as X along the channel axis.
219219
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
220220
int64_t* axis_mask = (int64_t *) calloc(numDimensions, sizeof(int64_t));
221-
for (int i = 0; i < numDimensions; ++i) {
221+
for (const auto i : c10::irange(numDimensions)) {
222222
axis_mask[i] = (i == axis) ? X.size(axis) : 1;
223223
}
224224
auto X_shape = X.sizes();

Diff for: aten/src/ATen/native/sparse/SoftMax.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ATen/Parallel.h>
88
#include <ATen/SparseTensorUtils.h>
99
#include <c10/util/accumulate.h>
10+
#include <c10/util/irange.h>
1011

1112
#include <map>
1213

@@ -71,9 +72,9 @@ std::vector<int64_t> get_offsets(const Tensor& indices, const IntArrayRef& sizes
7172
}
7273
}
7374

74-
for (int64_t i=0; i < nnz; i++) {
75+
for (const auto i : c10::irange(nnz)) {
7576
int64_t acc = 0;
76-
for (int64_t j=0; j < ndim; j++) {
77+
for (const auto j : c10::irange(ndim)) {
7778
auto indices_row = indices_accessor[j];
7879
auto stride = strides[j];
7980
if (j != dim) {
@@ -119,9 +120,9 @@ std::vector<std::vector<int64_t>> get_pools(const Tensor& indices, const IntArra
119120
}
120121
}
121122

122-
for (int64_t i=0; i < nnz; i++) {
123+
for (const auto i : c10::irange(nnz)) {
123124
int64_t pool_index = 0;
124-
for (int64_t j=0; j < ndim; j++) {
125+
for (const auto j : c10::irange(ndim)) {
125126
if (j != dim) {
126127
const auto indices_row = indices_accessor[j];
127128
const auto stride = strides[j];
@@ -315,7 +316,7 @@ void cpu_sparse_coo_softmax(Tensor output, const Tensor& input, const int64_t di
315316

316317
int64_t grain_size = 1;
317318
parallel_for(0, pools.size(), grain_size, [&](int64_t begin, int64_t end) {
318-
for (auto p = begin; p < end; p++) {
319+
for (const auto p : c10::irange(begin, end)) {
319320
auto pool_indices = pools[p];
320321

321322
// Skip empty pools
@@ -329,7 +330,7 @@ void cpu_sparse_coo_softmax(Tensor output, const Tensor& input, const int64_t di
329330
/* Compute mx */
330331
for (int64_t i : pool_indices) {
331332
auto values_row = values_accessor[i];
332-
for (int64_t j=0; j < nvalues; j++) {
333+
for (const auto j : c10::irange(nvalues)) {
333334
mx_row[j] = std::max(mx_row[j], values_row[j]);
334335
}
335336
}
@@ -338,7 +339,7 @@ void cpu_sparse_coo_softmax(Tensor output, const Tensor& input, const int64_t di
338339
for (int64_t i : pool_indices) {
339340
auto values_row = values_accessor[i];
340341
auto out_values_row = out_values_accessor[i];
341-
for (int64_t j=0; j < nvalues; j++) {
342+
for (const auto j : c10::irange(nvalues)) {
342343
auto v = std::exp(values_row[j] - mx_row[j]);
343344
if (!LogSoftMax) {
344345
out_values_row[j] = v;
@@ -347,7 +348,7 @@ void cpu_sparse_coo_softmax(Tensor output, const Tensor& input, const int64_t di
347348
}
348349
}
349350

350-
for (int64_t j=0; j < nvalues; j++) {
351+
for (const auto j : c10::irange(nvalues)) {
351352
if (LogSoftMax) {
352353
mx_row[j] += std::log(exp_sums_row[j]);
353354
} else {
@@ -359,7 +360,7 @@ void cpu_sparse_coo_softmax(Tensor output, const Tensor& input, const int64_t di
359360
for (int64_t i : pool_indices) {
360361
auto values_row = values_accessor[i];
361362
auto out_values_row = out_values_accessor[i];
362-
for (int64_t j=0; j < nvalues; j++) {
363+
for (const auto j : c10::irange(nvalues)) {
363364
if (LogSoftMax) {
364365
out_values_row[j] = values_row[j] - mx_row[j];
365366
} else {
@@ -421,7 +422,7 @@ void cpu_sparse_coo_softmax_backward(const Tensor& grad_input, const Tensor& gra
421422
values.set_(r);
422423
}
423424
} else {
424-
for(int64_t i=0; i<out_nnz; i++) {
425+
for (const auto i : c10::irange(out_nnz)) {
425426
auto low = std::lower_bound(grad_offsets.begin(), grad_offsets.end(), out_offsets[i]);
426427
auto j = low - grad_offsets.begin();
427428
if (j < grad_nnz && out_offsets[i] == grad_offsets[j]) {
@@ -456,7 +457,7 @@ void cpu_sparse_coo_softmax_backward(const Tensor& grad_input, const Tensor& gra
456457

457458
int64_t grain_size = 1;
458459
parallel_for(0, pools.size(), grain_size, [&](int64_t begin, int64_t end) {
459-
for (auto p = begin; p < end; p++) {
460+
for (const auto p : c10::irange(begin, end)) {
460461
auto pool_indices = pools[p];
461462

462463
// Skip empty pools
@@ -473,7 +474,7 @@ void cpu_sparse_coo_softmax_backward(const Tensor& grad_input, const Tensor& gra
473474

474475
if (j < grad_nnz && (out_offsets[i] == grad_offsets[j])) {
475476
auto grad_values_row = grad_values_accessor[j];
476-
for (int64_t k=0; k<nvalues; k++) {
477+
for (const auto k : c10::irange(nvalues)) {
477478
if (LogSoftMax) {
478479
tmp_row[k] -= grad_values_row[k];
479480
} else {
@@ -492,15 +493,15 @@ void cpu_sparse_coo_softmax_backward(const Tensor& grad_input, const Tensor& gra
492493

493494
if (j < grad_nnz && (out_offsets[i] == grad_offsets[j])) {
494495
auto grad_values_row = grad_values_accessor[j];
495-
for (int64_t k=0; k<nvalues; k++) {
496+
for (const auto k : c10::irange(nvalues)) {
496497
if (LogSoftMax) {
497498
values_row[k] = grad_values_row[k] + std::exp(out_values_row[k]) * tmp_row[k];
498499
} else {
499500
values_row[k] = out_values_row[k] * (grad_values_row[k] + tmp_row[k]);
500501
}
501502
}
502503
} else {
503-
for (int64_t k=0; k<nvalues; k++) {
504+
for (const auto k : c10::irange(nvalues)) {
504505
if (LogSoftMax) {
505506
values_row[k] = std::exp(out_values_row[k]) * tmp_row[k];
506507
} else {

Diff for: aten/src/ATen/native/sparse/SparseCsrTensorMath.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <ATen/native/Resize.h>
1414
#include <ATen/native/mkl/SparseBlasImpl.h>
1515
#include <ATen/native/sparse/SparseBlasImpl.h>
16+
#include <c10/util/irange.h>
1617

1718
#include <algorithm>
1819

@@ -60,7 +61,7 @@ void convert_indices_from_coo_to_csr_cpu(const Tensor& result, const Tensor& inp
6061

6162
at::parallel_for(0, numel - 1, GRAIN_SIZE, [&](int64_t start, int64_t end) {
6263
input_t curr_value = data_in[start], next_value;
63-
for (int64_t i = start; i < end; i++) {
64+
for (const auto i : c10::irange(start, end)) {
6465
next_value = data_in[i + 1];
6566
for (; curr_value < next_value; curr_value++)
6667
data_out[curr_value + 1] = static_cast<output_t>(i + 1);

0 commit comments

Comments
 (0)