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

aten/src/ATen/native/quantized/cpu/qlinear.cpp

Lines changed: 1 addition & 1 deletion
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.

aten/src/ATen/native/quantized/cpu/qlinear_dynamic.cpp

Lines changed: 1 addition & 1 deletion
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

aten/src/ATen/native/quantized/cpu/qpool.cpp

Lines changed: 3 additions & 2 deletions
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>(

aten/src/ATen/native/quantized/cpu/qrelu.cpp

Lines changed: 2 additions & 1 deletion
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

aten/src/ATen/native/quantized/cpu/qsigmoid.cpp

Lines changed: 2 additions & 1 deletion
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

aten/src/ATen/native/quantized/cpu/qtanh.cpp

Lines changed: 2 additions & 1 deletion
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();

aten/src/ATen/native/quantized/cpu/quant_utils.h

Lines changed: 2 additions & 1 deletion
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;

aten/src/ATen/native/quantized/cpu/qupsample_bilinear2d.cpp

Lines changed: 5 additions & 3 deletions
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] +

aten/src/ATen/native/quantized/cpu/qupsample_nearest2d.cpp

Lines changed: 6 additions & 5 deletions
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

aten/src/ATen/native/quantized/cpu/qupsample_nearest3d.cpp

Lines changed: 8 additions & 7 deletions
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

0 commit comments

Comments
 (0)