Skip to content

Commit afb7423

Browse files
r-barnesfacebook-github-bot
authored andcommitted
use irange for loops 10 (pytorch#69394)
Summary: Pull Request resolved: pytorch#69394 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: D32837991 fbshipit-source-id: fc7c4f76d2f32a17a0faf329294b3fe7cb81df32
1 parent 2d5b310 commit afb7423

11 files changed

+103
-85
lines changed

test/cpp/api/dataloader.cpp

+25-22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <test/cpp/api/support.h>
66

77
#include <c10/util/ArrayRef.h>
8+
#include <c10/util/irange.h>
89
#include <c10/util/tempfile.h>
910

1011
#include <algorithm>
@@ -173,7 +174,7 @@ TEST(DataTest, InfiniteStreamDataset) {
173174
for (auto& batch : *data_loader) {
174175
ASSERT_LT(batch_index, 3);
175176
ASSERT_EQ(batch.size(), kBatchSize);
176-
for (size_t j = 0; j < kBatchSize; ++j) {
177+
for (const auto j : c10::irange(kBatchSize)) {
177178
ASSERT_EQ(batch.at(j), 1 + (batch_index * kBatchSize) + j);
178179
}
179180
batch_index += 1;
@@ -837,7 +838,7 @@ TEST(DataTest, CanUseCustomTypeAsIndexType) {
837838

838839
size_t i = 0;
839840
for (auto batch : *data_loader) {
840-
for (int j = 0; j < kBatchSize; ++j) {
841+
for (const auto j : c10::irange(kBatchSize)) {
841842
ASSERT_EQ(batch.at(j), 10 + j);
842843
}
843844
i += 1;
@@ -857,7 +858,7 @@ TEST(DataTest, DistributedRandomSamplerSingleReplicaProduceCorrectSamples) {
857858
ASSERT_EQ(res.size(), sample_count);
858859

859860
std::sort(res.begin(), res.end());
860-
for (size_t i = 0; i < res.size(); ++i) {
861+
for (const auto i : c10::irange(res.size())) {
861862
ASSERT_EQ(res[i], i);
862863
}
863864
}
@@ -872,14 +873,14 @@ TEST(DataTest, DistributedRandomSamplerMultiReplicaProduceCorrectSamples) {
872873
size_t batch_size) {
873874
std::vector<std::unique_ptr<samplers::DistributedRandomSampler>> samplers;
874875

875-
for (size_t i = 0; i < num_replicas; ++i) {
876+
for (const auto i : c10::irange(num_replicas)) {
876877
samplers.emplace_back(
877878
torch::make_unique<samplers::DistributedRandomSampler>(
878879
sample_count, num_replicas, i, allow_duplicates));
879880
}
880881

881882
std::vector<size_t> res;
882-
for (size_t i = 0; i < num_replicas; ++i) {
883+
for (const auto i : c10::irange(num_replicas)) {
883884
(*samplers[i]).reset();
884885
torch::optional<std::vector<size_t>> idx;
885886
while ((idx = (*samplers[i]).next(batch_size)).has_value()) {
@@ -953,7 +954,7 @@ TEST(DataTest, DistributedSequentialSamplerSingleReplicaProduceCorrectSamples) {
953954
ASSERT_EQ(res.size(), sample_count);
954955

955956
std::sort(res.begin(), res.end());
956-
for (size_t i = 0; i < res.size(); ++i) {
957+
for (const auto i : c10::irange(res.size())) {
957958
ASSERT_EQ(res[i], i);
958959
}
959960
}
@@ -969,14 +970,14 @@ TEST(DataTest, DistributedSequentialSamplerMultiReplicaProduceCorrectSamples) {
969970
std::vector<std::unique_ptr<samplers::DistributedSequentialSampler>>
970971
samplers;
971972

972-
for (size_t i = 0; i < num_replicas; ++i) {
973+
for (const auto i : c10::irange(num_replicas)) {
973974
samplers.emplace_back(
974975
torch::make_unique<samplers::DistributedSequentialSampler>(
975976
sample_count, num_replicas, i, allow_duplicates));
976977
}
977978

978979
std::vector<size_t> res;
979-
for (size_t i = 0; i < num_replicas; ++i) {
980+
for (const auto i : c10::irange(num_replicas)) {
980981
(*samplers[i]).reset();
981982
torch::optional<std::vector<size_t>> idx;
982983
while ((idx = (*samplers[i]).next(batch_size)).has_value()) {
@@ -1490,7 +1491,7 @@ TEST(DataLoaderTest, StatefulDatasetWithNoWorkers) {
14901491

14911492
auto data_loader = torch::data::make_data_loader(D{});
14921493

1493-
for (size_t i = 0; i < 10; ++i) {
1494+
for (const auto i : c10::irange(10)) {
14941495
const auto number_of_iterations =
14951496
std::distance(data_loader->begin(), data_loader->end());
14961497
ASSERT_EQ(
@@ -1531,7 +1532,7 @@ TEST(DataLoaderTest, StatefulDatasetWithManyWorkers) {
15311532
torch::data::datasets::make_shared_dataset<D>(),
15321533
DataLoaderOptions().workers(kNumberOfWorkers));
15331534

1534-
for (size_t i = 0; i < 10; ++i) {
1535+
for (const auto i : c10::irange(10)) {
15351536
const auto number_of_iterations =
15361537
std::distance(data_loader->begin(), data_loader->end());
15371538
ASSERT_EQ(
@@ -1574,7 +1575,7 @@ TEST(DataLoaderTest, StatefulDatasetWithMap) {
15741575
})),
15751576
DataLoaderOptions{});
15761577

1577-
for (size_t i = 0; i < 10; ++i) {
1578+
for (const auto i : c10::irange(10)) {
15781579
const auto number_of_iterations =
15791580
std::distance(data_loader->begin(), data_loader->end());
15801581
ASSERT_EQ(
@@ -1675,7 +1676,8 @@ TEST(DataLoaderTest, ChunkDataSetGetBatch) {
16751676
dataset,
16761677
DataLoaderOptions(batch_size).workers(dataloader_worker_count));
16771678

1678-
for (int epoch_index = 0; epoch_index < epoch_count; ++epoch_index) {
1679+
for (const auto epoch_index : c10::irange(epoch_count)) {
1680+
(void)epoch_index; // Suppress unused variable warning
16791681
std::vector<bool> result(total_example_count, false);
16801682
int iteration_count = 0;
16811683
for (auto iterator = data_loader->begin();
@@ -1687,11 +1689,11 @@ TEST(DataLoaderTest, ChunkDataSetGetBatch) {
16871689
// When prefetch_count is equal to 1 and no worker thread, the batch
16881690
// order is deterministic. So we can verify elements in each batch.
16891691
if (prefetch_count == 1 && dataloader_worker_count == 0) {
1690-
for (size_t j = 0; j < batch_size; ++j) {
1692+
for (const auto j : c10::irange(batch_size)) {
16911693
ASSERT_EQ(batch[j], iteration_count * batch_size + j);
16921694
}
16931695
}
1694-
for (size_t j = 0; j < batch_size; ++j) {
1696+
for (const auto j : c10::irange(batch_size)) {
16951697
result[batch[j]] = true;
16961698
}
16971699
}
@@ -1978,7 +1980,8 @@ TEST(DataLoaderTest, ChunkDatasetSave) {
19781980
dataset,
19791981
DataLoaderOptions(batch_size).workers(dataloader_worker_count));
19801982

1981-
for (int epoch_index = 0; epoch_index < epoch_count; ++epoch_index) {
1983+
for (const auto epoch_index : c10::irange(epoch_count)) {
1984+
(void)epoch_index; // Suppress unused variable warning
19821985
int iteration_count = 0;
19831986
for (auto iterator = data_loader->begin(); iterator != data_loader->end();
19841987
++iterator, ++iteration_count) {
@@ -2079,7 +2082,7 @@ TEST(DataLoaderTest, ChunkDatasetLoad) {
20792082
auto data_loader = torch::data::make_data_loader(
20802083
dataset, DataLoaderOptions(batch_size).workers(dataloader_worker_count));
20812084

2082-
for (int epoch_index = 0; epoch_index < epoch_count; ++epoch_index) {
2085+
for (const auto epoch_index : c10::irange(epoch_count)) {
20832086
int iteration_count = 0;
20842087

20852088
// For the first epoch, the returned batch should be returned from the
@@ -2128,7 +2131,7 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) {
21282131
size_t index = 0;
21292132

21302133
// Repeatly sample every 5 indices.
2131-
for (size_t i = 0; i < batch_size; ++i) {
2134+
for (const auto i : c10::irange(batch_size)) {
21322135
for (size_t j = 0; j < size_ / batch_size; ++j) {
21332136
indices_[index++] = i + batch_size * j;
21342137
}
@@ -2222,11 +2225,11 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) {
22222225
// construct expected result
22232226
int offset = 0;
22242227

2225-
for (int i = 0; i < (chunk_count + cross_chunk_shuffle_count - 1) /
2226-
cross_chunk_shuffle_count;
2227-
i++) {
2228-
for (int j = 0; j < chunk_size; ++j) {
2229-
for (int k = 0; k < cross_chunk_shuffle_count; ++k) {
2228+
for (const auto i : c10::irange((chunk_count + cross_chunk_shuffle_count - 1) /
2229+
cross_chunk_shuffle_count)) {
2230+
for (const auto j : c10::irange(chunk_size)) {
2231+
(void)j; // Suppress unused variable warning
2232+
for (const auto k : c10::irange(cross_chunk_shuffle_count)) {
22302233
if (i * cross_chunk_shuffle_count + k < chunk_count) {
22312234
expected_result.push_back(i * cross_chunk_shuffle_count + k);
22322235
}

test/cpp/api/dispatch.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <torch/torch.h>
44
#include <ATen/native/Pow.h>
5+
#include <c10/util/irange.h>
56
#include <torch/types.h>
67
#include <torch/utils.h>
78
#include <test/cpp/api/support.h>
@@ -24,7 +25,7 @@ TEST_F(DispatchTest, TestAVX2) {
2425
setenv("ATEN_CPU_CAPABILITY", "avx2", 1);
2526
#endif
2627
const auto actual_pow_avx2 = vals_tensor.pow(pows_tensor);
27-
for (int i = 0; i < 4; i++) {
28+
for (const auto i : c10::irange(4)) {
2829
ASSERT_EQ(result[i], actual_pow_avx2[i].item<int>());
2930
}
3031
}
@@ -40,7 +41,7 @@ TEST_F(DispatchTest, TestAVX512) {
4041
setenv("ATEN_CPU_CAPABILITY", "avx512", 1);
4142
#endif
4243
const auto actual_pow_avx512 = vals_tensor.pow(pows_tensor);
43-
for (int i = 0; i < 4; i++) {
44+
for (const auto i : c10::irange(4)) {
4445
ASSERT_EQ(result[i], actual_pow_avx512[i].item<int>());
4546
}
4647
}
@@ -56,7 +57,7 @@ TEST_F(DispatchTest, TestDefault) {
5657
setenv("ATEN_CPU_CAPABILITY", "default", 1);
5758
#endif
5859
const auto actual_pow_default = vals_tensor.pow(pows_tensor);
59-
for (int i = 0; i < 4; i++) {
60+
for (const auto i : c10::irange(4)) {
6061
ASSERT_EQ(result[i], actual_pow_default[i].item<int>());
6162
}
6263
}

test/cpp/api/expanding-array.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <gtest/gtest.h>
22

3+
#include <c10/util/irange.h>
34
#include <torch/torch.h>
45

56
#include <test/cpp/api/support.h>
@@ -13,31 +14,31 @@ struct ExpandingArrayTest : torch::test::SeedingFixture {};
1314
TEST_F(ExpandingArrayTest, CanConstructFromInitializerList) {
1415
torch::ExpandingArray<5> e({1, 2, 3, 4, 5});
1516
ASSERT_EQ(e.size(), 5);
16-
for (size_t i = 0; i < e.size(); ++i) {
17+
for (const auto i : c10::irange(e.size())) {
1718
ASSERT_EQ((*e)[i], i + 1);
1819
}
1920
}
2021

2122
TEST_F(ExpandingArrayTest, CanConstructFromVector) {
2223
torch::ExpandingArray<5> e(std::vector<int64_t>{1, 2, 3, 4, 5});
2324
ASSERT_EQ(e.size(), 5);
24-
for (size_t i = 0; i < e.size(); ++i) {
25+
for (const auto i : c10::irange(e.size())) {
2526
ASSERT_EQ((*e)[i], i + 1);
2627
}
2728
}
2829

2930
TEST_F(ExpandingArrayTest, CanConstructFromArray) {
3031
torch::ExpandingArray<5> e(std::array<int64_t, 5>({1, 2, 3, 4, 5}));
3132
ASSERT_EQ(e.size(), 5);
32-
for (size_t i = 0; i < e.size(); ++i) {
33+
for (const auto i : c10::irange(e.size())) {
3334
ASSERT_EQ((*e)[i], i + 1);
3435
}
3536
}
3637

3738
TEST_F(ExpandingArrayTest, CanConstructFromSingleValue) {
3839
torch::ExpandingArray<5> e(5);
3940
ASSERT_EQ(e.size(), 5);
40-
for (size_t i = 0; i < e.size(); ++i) {
41+
for (const auto i : c10::irange(e.size())) {
4142
ASSERT_EQ((*e)[i], 5);
4243
}
4344
}

test/cpp/api/fft.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <gtest/gtest.h>
22

3+
#include <c10/util/irange.h>
34
#include <torch/torch.h>
45
#include <test/cpp/api/support.h>
56

@@ -14,15 +15,15 @@ torch::Tensor naive_dft(torch::Tensor x, bool forward=true) {
1415
// Roots of unity, exp(-2*pi*j*n/N) for n in [0, N), reversed for inverse transform
1516
std::vector<c10::complex<double>> roots(len);
1617
const auto angle_base = (forward ? -2.0 : 2.0) * M_PI / len;
17-
for (int64_t i = 0; i < len; ++i) {
18+
for (const auto i : c10::irange(len)) {
1819
auto angle = i * angle_base;
1920
roots[i] = c10::complex<double>(std::cos(angle), std::sin(angle));
2021
}
2122

2223
const auto in = x.data_ptr<c10::complex<double>>();
2324
const auto out = out_tensor.data_ptr<c10::complex<double>>();
24-
for (int64_t i = 0; i < len; ++i) {
25-
for (int64_t j = 0; j < len; ++j) {
25+
for (const auto i : c10::irange(len)) {
26+
for (const auto j : c10::irange(len)) {
2627
out[i] += roots[(j * i) % len] * in[j];
2728
}
2829
}

test/cpp/api/functional.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <gtest/gtest.h>
22

3+
#include <c10/util/irange.h>
34
#include <torch/torch.h>
45

56
#include <test/cpp/api/support.h>
@@ -1127,7 +1128,7 @@ TEST_F(FunctionalTest, GumbelSoftmax) {
11271128
int dims[] = {1, -1};
11281129
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-magic-numbers)
11291130
int expected[] = {5*3, 5*4};
1130-
for(auto i=0; i<2; i++) {
1131+
for (const auto i : c10::irange(2)) {
11311132
auto logits = torch::randn({5, 4, 3});
11321133
int expected_count = expected[i];
11331134
auto y_draw = F::gumbel_softmax(logits, F::GumbelSoftmaxFuncOptions().hard(true).dim(dims[i]));
@@ -1149,7 +1150,8 @@ TEST_F(FunctionalTest, GumbelSoftmax) {
11491150

11501151
auto counts = torch::zeros_like(logits);
11511152
torch::Tensor y_draw;
1152-
for (auto i=0; i<num_draws; i++) {
1153+
for (const auto i : c10::irange(num_draws)) {
1154+
(void)i; // Suppress unused variable warning
11531155
y_draw = F::gumbel_softmax(logits, F::GumbelSoftmaxFuncOptions().hard(true));
11541156
counts += y_draw;
11551157
}
@@ -1175,7 +1177,7 @@ TEST_F(FunctionalTest, Softmax) {
11751177
auto output = F::softmax(input, /*dim=*/1);
11761178
auto sum = torch::sum(torch::exp(input), 1);
11771179

1178-
for (int i = 0; i < 2; i++) {
1180+
for (const auto i : c10::irange(2)) {
11791181
auto expected = torch::exp(input[i]) / sum[i];
11801182
ASSERT_TRUE(torch::allclose(output[i], expected));
11811183
}
@@ -1187,7 +1189,7 @@ TEST_F(FunctionalTest, Softmin) {
11871189
auto output = F::softmin(input, /*dim=*/1);
11881190
auto sum = torch::sum(torch::exp(-input), 1);
11891191

1190-
for (int i = 0; i < 2; i++) {
1192+
for (const auto i : c10::irange(2)) {
11911193
auto expected = torch::exp(-input[i]) / sum[i];
11921194
ASSERT_TRUE(torch::allclose(output[i], expected));
11931195
}
@@ -1199,7 +1201,7 @@ TEST_F(FunctionalTest, LogSoftmax) {
11991201
auto output = F::log_softmax(input, /*dim=*/1);
12001202
auto sum = torch::sum(torch::exp(input), 1);
12011203

1202-
for (int i = 0; i < 2; i++) {
1204+
for (const auto i : c10::irange(2)) {
12031205
auto expected = torch::log(torch::exp(input[i]) / sum[i]);
12041206
ASSERT_TRUE(torch::allclose(output[i], expected));
12051207
}

test/cpp/api/init.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <gtest/gtest.h>
22

3+
#include <c10/util/irange.h>
34
#include <torch/torch.h>
45

56
#include <test/cpp/api/init_baseline.h>
@@ -14,7 +15,7 @@ void check_exact_values(
1415
const std::vector<std::vector<torch::Tensor>>& expected_parameters) {
1516
ASSERT_EQ(parameters.size(), expected_parameters.size());
1617

17-
for (size_t i = 0; i < parameters.size(); i++) {
18+
for (const auto i : c10::irange(parameters.size())) {
1819
auto layerParameters = parameters[i];
1920
auto expectedLayerParameters = expected_parameters[i];
2021

@@ -27,7 +28,7 @@ void check_exact_values(
2728
ASSERT_TRUE(false);
2829
}
2930

30-
for (size_t p = 0; p < layerParameters.size(0); p++) {
31+
for (const auto p : c10::irange(layerParameters.size(0))) {
3132
// Always compare using double dtype, regardless of the original dtype of the tensors
3233
auto tensor = layerParameters[p].to(torch::kFloat64);
3334
auto expectedTensor = expectedLayerParameters[p].to(torch::kFloat64);

test/cpp/api/integration.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <gtest/gtest.h>
22

3+
#include <c10/util/irange.h>
34
#include <torch/torch.h>
45

56
#include <test/cpp/api/support.h>
@@ -122,10 +123,12 @@ bool test_mnist(
122123
torch::Device device(with_cuda ? torch::kCUDA : torch::kCPU);
123124
model->to(device);
124125

125-
for (size_t epoch = 0; epoch < number_of_epochs; epoch++) {
126+
for (const auto epoch : c10::irange(number_of_epochs)) {
127+
(void)epoch; // Suppress unused variable warning
126128
// NOLINTNEXTLINE(performance-for-range-copy)
127129
for (torch::data::Example<> batch : *data_loader) {
128-
auto data = batch.data.to(device), targets = batch.target.to(device);
130+
auto data = batch.data.to(device);
131+
auto targets = batch.target.to(device);
129132
torch::Tensor prediction = forward_op(std::move(data));
130133
// NOLINTNEXTLINE(performance-move-const-arg)
131134
torch::Tensor loss = torch::nll_loss(prediction, std::move(targets));
@@ -196,7 +199,7 @@ TEST_F(IntegrationTest, CartPole) {
196199

197200
std::vector<torch::Tensor> policy_loss;
198201
std::vector<torch::Tensor> value_loss;
199-
for (auto i = 0U; i < saved_log_probs.size(); i++) {
202+
for (const auto i : c10::irange(0U, saved_log_probs.size())) {
200203
auto advantage = r_t[i] - saved_values[i].item<float>();
201204
policy_loss.push_back(-advantage * saved_log_probs[i]);
202205
value_loss.push_back(

0 commit comments

Comments
 (0)