Skip to content

Commit e0643fa

Browse files
r-barnesfacebook-github-bot
authored andcommitted
use irange for loops 5 (pytorch#66744)
Summary: Pull Request resolved: pytorch#66744 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: ngimel Differential Revision: D31705358 fbshipit-source-id: d6ea350cbaa8f452fc78f238160e5374be637a48
1 parent bceb1db commit e0643fa

31 files changed

+423
-393
lines changed

test/cpp/api/operations.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>
@@ -11,7 +12,8 @@ struct OperationTest : torch::test::SeedingFixture {
1112
};
1213

1314
TEST_F(OperationTest, Lerp) {
14-
for (auto i = 0; i < TEST_AMOUNT; i++) {
15+
for (const auto i : c10::irange(TEST_AMOUNT)) {
16+
(void)i; // Suppress unused variable warning
1517
// test lerp_kernel_scalar
1618
auto start = torch::rand({3, 5});
1719
auto end = torch::rand({3, 5});
@@ -35,13 +37,14 @@ TEST_F(OperationTest, Lerp) {
3537
}
3638

3739
TEST_F(OperationTest, Cross) {
38-
for (auto i = 0; i < TEST_AMOUNT; i++) {
40+
for (const auto i : c10::irange(TEST_AMOUNT)) {
41+
(void)i; // Suppress unused variable warning
3942
// input
4043
auto a = torch::rand({10, 3});
4144
auto b = torch::rand({10, 3});
4245
// expected
4346
auto exp = torch::empty({10, 3});
44-
for (int j = 0; j < 10; j++) {
47+
for (const auto j : c10::irange(10)) {
4548
auto u1 = a[j][0], u2 = a[j][1], u3 = a[j][2];
4649
auto v1 = b[j][0], v2 = b[j][1], v3 = b[j][2];
4750
exp[j][0] = u2 * v3 - v2 * u3;

test/cpp/api/optim.cpp

+8-6
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/optim_baseline.h>
@@ -36,7 +37,7 @@ bool test_optimizer_xor(Options options) {
3637
while (running_loss > 0.1) {
3738
auto inputs = torch::empty({kBatchSize, 2});
3839
auto labels = torch::empty({kBatchSize});
39-
for (size_t i = 0; i < kBatchSize; i++) {
40+
for (const auto i : c10::irange(kBatchSize)) {
4041
inputs[i] = torch::randint(2, {2}, torch::kInt64);
4142
labels[i] = inputs[i][0].item<int64_t>() ^ inputs[i][1].item<int64_t>();
4243
}
@@ -112,7 +113,7 @@ void check_exact_values(
112113
torch::Tensor input =
113114
torch::tensor({0.1, 0.2, 0.3, 0.4, 0.5, 0.6}, torch::kFloat64).reshape({3, 2});
114115

115-
for (size_t i = 0; i < kIterations; ++i) {
116+
for (const auto i : c10::irange(kIterations)) {
116117
optimizer.zero_grad();
117118
auto output = model->forward(input);
118119
auto loss = output.sum();
@@ -124,7 +125,7 @@ void check_exact_values(
124125
if (i % kSampleEvery == 0) {
125126
ASSERT_TRUE(
126127
expected_parameters.at(i / kSampleEvery).size() == parameters.size());
127-
for (size_t p = 0; p < parameters.size(); ++p) {
128+
for (const auto p : c10::irange(parameters.size())) {
128129
ASSERT_TRUE(parameters[p]->defined());
129130
// Always compare using double dtype, regardless of the original dtype of the tensors
130131
auto computed = parameters[p]->flatten().to(torch::kFloat64);
@@ -143,7 +144,8 @@ void check_exact_values(
143144
TEST(OptimTest, OptimizerAccessors) {
144145
auto options = AdagradOptions(1.0);
145146
std::vector<torch::Tensor> params;
146-
for (size_t i = 0; i < 3; i++) {
147+
for (const auto i : c10::irange(3)) {
148+
(void)i; // Suppress unused variable warning
147149
params.push_back(torch::randn(10));
148150
}
149151
auto optimizer = Adagrad(params, options);
@@ -155,7 +157,7 @@ TEST(OptimTest, OptimizerAccessors) {
155157
// NOLINTNEXTLINE(modernize-use-emplace)
156158
params_groups.push_back(OptimizerParamGroup(params));
157159
auto& params_1 = params_groups[1].params();
158-
for (size_t i = 0; i < params_1.size(); i++) {
160+
for (const auto i : c10::irange(params_1.size())) {
159161
torch::equal(params[i], params_1[i]);
160162
}
161163

@@ -225,7 +227,7 @@ TEST(OptimTest, OldInterface) {
225227

226228
std::vector<torch::Tensor> params_;
227229
OLD_INTERFACE_WARNING_CHECK(params_ = optimizer.parameters());
228-
for (size_t p = 0; p < size; ++p) {
230+
for (const auto p : c10::irange(size)) {
229231
ASSERT_TRUE(params_[p].allclose(parameters[p]));
230232
}
231233
}

test/cpp/api/parallel.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/csrc/autograd/functions/comm.h>
45
#include <torch/nn/module.h>
56
#include <torch/nn/modules/conv.h>
@@ -86,7 +87,7 @@ TEST_F(ParallelTest, Replicate_MultiCUDA) {
8687
}
8788
replicas[0]->to(torch::kCPU);
8889
ASSERT_EQ(replica1_parameters.size(), original_parameters.size());
89-
for (size_t i = 0; i < original_parameters.size(); ++i) {
90+
for (const auto i : c10::irange(original_parameters.size())) {
9091
ASSERT_TRUE(replica1_parameters[i].allclose(original_parameters[i]));
9192
ASSERT_TRUE(
9293
replica1_parameters[i].data_ptr<float>() !=
@@ -99,7 +100,7 @@ TEST_F(ParallelTest, Replicate_MultiCUDA) {
99100
}
100101
replicas[1]->to(torch::kCPU);
101102
ASSERT_EQ(replica2_parameters.size(), original_parameters.size());
102-
for (size_t i = 0; i < original_parameters.size(); ++i) {
103+
for (const auto i : c10::irange(original_parameters.size())) {
103104
ASSERT_TRUE(replica2_parameters[i].allclose(original_parameters[i]));
104105
ASSERT_TRUE(
105106
replica2_parameters[i].data_ptr<float>() !=
@@ -222,7 +223,7 @@ TEST_F(ParallelTest, DataParallelUsesAllAvailableCUDADevices_CUDA) {
222223
auto output = parallel::data_parallel(m, input);
223224

224225
ASSERT_EQ(output.numel(), device_count);
225-
for (size_t i = 0; i < device_count; ++i) {
226+
for (const auto i : c10::irange(device_count)) {
226227
ASSERT_EQ(output[i].item<int32_t>(), i);
227228
}
228229
}
@@ -258,7 +259,7 @@ TEST_F(ParallelTest, DataParallelNumericalEquivalence_MultiCUDA) {
258259
auto model_dp = std::dynamic_pointer_cast<M>(model->clone());
259260

260261
// run 3 training iterations
261-
for (int i = 0; i < 3; ++i) {
262+
for (const auto i : c10::irange(3)) {
262263
input += i;
263264
input_dp += i;
264265

test/cpp/api/parameterlist.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 <algorithm>
@@ -78,11 +79,11 @@ TEST_F(ParameterListTest, AccessWithAt) {
7879
ASSERT_EQ(list->size(), 4);
7980

8081
// returns the correct module for a given index
81-
for (size_t i = 0; i < params.size(); ++i) {
82+
for (const auto i : c10::irange(params.size())) {
8283
ASSERT_TRUE(torch::all(torch::eq(list->at(i), params[i])).item<bool>());
8384
}
8485

85-
for (size_t i = 0; i < params.size(); ++i) {
86+
for (const auto i : c10::irange(params.size())) {
8687
ASSERT_TRUE(torch::all(torch::eq(list[i], params[i])).item<bool>());
8788
}
8889

test/cpp/api/sequential.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 <algorithm>
@@ -172,7 +173,7 @@ TEST_F(SequentialTest, AccessWithAt) {
172173
ASSERT_EQ(sequential->size(), 3);
173174

174175
// returns the correct module for a given index
175-
for (size_t i = 0; i < modules.size(); ++i) {
176+
for (const auto i : c10::irange(modules.size())) {
176177
ASSERT_EQ(&sequential->at<M>(i), modules[i].get());
177178
}
178179

@@ -201,7 +202,7 @@ TEST_F(SequentialTest, AccessWithPtr) {
201202
ASSERT_EQ(sequential->size(), 3);
202203

203204
// returns the correct module for a given index
204-
for (size_t i = 0; i < modules.size(); ++i) {
205+
for (const auto i : c10::irange(modules.size())) {
205206
ASSERT_EQ(sequential->ptr(i).get(), modules[i].get());
206207
ASSERT_EQ(sequential[i].get(), modules[i].get());
207208
ASSERT_EQ(sequential->ptr<M>(i).get(), modules[i].get());

test/cpp/api/serialize.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <c10/util/tempfile.h>
44
#include <c10/util/flat_hash_map.h>
5+
#include <c10/util/irange.h>
56

67
#include <torch/torch.h>
78

@@ -41,7 +42,7 @@ void is_optimizer_param_group_equal(const OptimizerParamGroup& lhs, const Optimi
4142
const auto& rhs_params = rhs.params();
4243

4344
ASSERT_TRUE(lhs_params.size() == rhs_params.size());
44-
for (size_t j = 0; j < lhs_params.size(); j++) {
45+
for (const auto j : c10::irange(lhs_params.size())) {
4546
ASSERT_TRUE(torch::equal(lhs_params[j], rhs_params[j]));
4647
}
4748
ASSERT_TRUE(static_cast<const DerivedOptions&>(lhs.options()) == static_cast<const DerivedOptions&>(rhs.options()));
@@ -136,7 +137,7 @@ void test_serialize_optimizer(DerivedOptimizerOptions options, bool only_has_glo
136137
ASSERT_TRUE(optim3_2_state.size() == optim3_state.size());
137138

138139
// checking correctness of serialization logic for optimizer.param_groups_ and optimizer.state_
139-
for (int i = 0; i < optim3_2_param_groups.size(); i++) {
140+
for (const auto i : c10::irange(optim3_2_param_groups.size())) {
140141
is_optimizer_param_group_equal<DerivedOptimizerOptions>(
141142
optim3_2_param_groups[i], optim3_param_groups[i]);
142143
is_optimizer_state_equal<DerivedOptimizerParamState>(optim3_2_state, optim3_state);
@@ -173,7 +174,7 @@ void write_tensors_to_archive(
173174
const BufferContainer& buffers) {
174175
archive.write(
175176
key + "/size", torch::tensor(static_cast<int64_t>(buffers.size())));
176-
for (size_t index = 0; index < buffers.size(); ++index) {
177+
for (const auto index : c10::irange(buffers.size())) {
177178
archive.write(
178179
key + "/" + c10::to_string(index), buffers[index], /*is_buffer=*/true);
179180
}
@@ -203,23 +204,23 @@ void write_step_buffers(
203204
TEST(SerializeTest, KeysFunc) {
204205
auto tempfile = c10::make_tempfile();
205206
torch::serialize::OutputArchive output_archive;
206-
for (size_t i = 0; i < 3; i++) {
207+
for (const auto i : c10::irange(3)) {
207208
output_archive.write("element/" + c10::to_string(i), c10::IValue(static_cast<int64_t>(i)));
208209
}
209210
output_archive.save_to(tempfile.name);
210211
torch::serialize::InputArchive input_archive;
211212
input_archive.load_from(tempfile.name);
212213
std::vector<std::string> keys = input_archive.keys();
213214
ASSERT_EQ(keys.size(), 3);
214-
for (size_t i = 0; i < keys.size(); i++) {
215+
for (const auto i : c10::irange(keys.size())) {
215216
ASSERT_EQ(keys[i], "element/" + c10::to_string(i));
216217
}
217218
}
218219

219220
TEST(SerializeTest, TryReadFunc) {
220221
auto tempfile = c10::make_tempfile();
221222
torch::serialize::OutputArchive output_archive;
222-
for (size_t i = 0; i < 3; i++) {
223+
for (const auto i : c10::irange(3)) {
223224
output_archive.write("element/" + c10::to_string(i), c10::IValue(static_cast<int64_t>(i)));
224225
}
225226
output_archive.save_to(tempfile.name);
@@ -363,7 +364,7 @@ TEST(SerializeTest, XOR) {
363364
auto getLoss = [](Sequential model, uint32_t batch_size) {
364365
auto inputs = torch::empty({batch_size, 2});
365366
auto labels = torch::empty({batch_size});
366-
for (size_t i = 0; i < batch_size; i++) {
367+
for (const auto i : c10::irange(batch_size)) {
367368
inputs[i] = torch::randint(2, {2}, torch::kInt64);
368369
labels[i] = inputs[i][0].item<int64_t>() ^ inputs[i][1].item<int64_t>();
369370
}
@@ -533,7 +534,7 @@ TEST(SerializeTest, Optim_SGD) {
533534
int64_t iteration_{0};
534535
const auto& params_ = optim1.param_groups()[0].params();
535536
const auto& optim1_state = optim1.state();
536-
for (size_t i = 0; i < params_.size(); i++) {
537+
for (const auto i : c10::irange(params_.size())) {
537538
if(i != (params_.size() - 1)) {
538539
auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl());
539540
const SGDParamState& curr_state_ = static_cast<const SGDParamState&>(*(optim1_state.at(key_).get()));
@@ -577,7 +578,7 @@ TEST(SerializeTest, Optim_Adam) {
577578
std::vector<at::Tensor> max_exp_average_sq_buffers;
578579
const auto& params_ = optim1.param_groups()[0].params();
579580
const auto& optim1_state = optim1.state();
580-
for (size_t i = 0; i < params_.size(); i++) {
581+
for (const auto i : c10::irange(params_.size())) {
581582
if(i != (params_.size() - 1)) {
582583
auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl());
583584
const AdamParamState& curr_state_ = static_cast<const AdamParamState&>(*(optim1_state.at(key_).get()));
@@ -627,7 +628,7 @@ TEST(SerializeTest, Optim_AdamW) {
627628
std::vector<at::Tensor> max_exp_average_sq_buffers;
628629
const auto& params_ = optim1.param_groups()[0].params();
629630
const auto& optim1_state = optim1.state();
630-
for (size_t i = 0; i < params_.size(); i++) {
631+
for (const auto i : c10::irange(params_.size())) {
631632
if(i != (params_.size() - 1)) {
632633
auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl());
633634
const AdamWParamState& curr_state_ = static_cast<const AdamWParamState&>(*(optim1_state.at(key_).get()));
@@ -678,7 +679,7 @@ TEST(SerializeTest, Optim_RMSprop) {
678679
std::vector<at::Tensor> grad_average_buffers;
679680
const auto& params_ = optim1.param_groups()[0].params();
680681
const auto& optim1_state = optim1.state();
681-
for (size_t i = 0; i < params_.size(); i++) {
682+
for (const auto i : c10::irange(params_.size())) {
682683
if(i != (params_.size() - 1)) {
683684
auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl());
684685
const RMSpropParamState& curr_state_ = static_cast<const RMSpropParamState&>(*(optim1_state.at(key_).get()));
@@ -703,7 +704,7 @@ TEST(SerializeTest, Optim_RMSprop) {
703704
const auto& params1_2_ = optim1_2.param_groups()[0].params();
704705
auto& optim1_2_state = optim1_2.state();
705706
// old RMSprop didn't track step value
706-
for (size_t i = 0; i < params1_2_.size(); i++) {
707+
for (const auto i : c10::irange(params1_2_.size())) {
707708
if(i != (params1_2_.size() - 1)) {
708709
auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl());
709710
auto key1_2_ = c10::guts::to_string(params1_2_[i].unsafeGetTensorImpl());
@@ -788,7 +789,7 @@ TEST(SerializeTest, XOR_CUDA) {
788789
inputs = inputs.cuda();
789790
labels = labels.cuda();
790791
}
791-
for (size_t i = 0; i < batch_size; i++) {
792+
for (const auto i : c10::irange(batch_size)) {
792793
inputs[i] = torch::randint(2, {2}, torch::kInt64);
793794
labels[i] = inputs[i][0].item<int64_t>() ^ inputs[i][1].item<int64_t>();
794795
}
@@ -879,7 +880,7 @@ TEST(SerializeTest, VectorOfTensors) {
879880
std::vector<torch::Tensor> y_vec;
880881
torch::load(y_vec, stream);
881882

882-
for (int64_t i = 0; i < x_vec.size(); i++) {
883+
for (const auto i : c10::irange(x_vec.size())) {
883884
auto& x = x_vec[i];
884885
auto& y = y_vec[i];
885886
ASSERT_TRUE(y.defined());

test/cpp/api/static.cpp

+2-1
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/detail/static.h>
45
#include <torch/csrc/utils/variadic.h>
56
#include <torch/torch.h>
@@ -95,7 +96,7 @@ TEST(TestStatic, Apply) {
9596
std::vector<int> v;
9697
torch::apply([&v](int x) { v.push_back(x); }, 1, 2, 3, 4, 5);
9798
ASSERT_EQ(v.size(), 5);
98-
for (size_t i = 0; i < v.size(); ++i) {
99+
for (const auto i : c10::irange(v.size())) {
99100
ASSERT_EQ(v.at(i), i + 1);
100101
}
101102
}

test/cpp/api/tensor.cpp

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

4+
#include <c10/util/irange.h>
45
#include <torch/torch.h>
56

67
#include <cmath>
@@ -263,15 +264,15 @@ TEST(TensorTest, AtTensorCtorSingleDim) {
263264
tensor = at::tensor(v);
264265
ASSERT_EQ(tensor.numel(), v.size());
265266
ASSERT_EQ(tensor.dtype(), at::kInt);
266-
for (size_t i = 0; i < v.size(); ++i) {
267+
for (const auto i : c10::irange(v.size())) {
267268
ASSERT_TRUE(exactly_equal(tensor[i], v.at(i)));
268269
}
269270

270271
std::vector<double> w = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
271272
tensor = at::tensor(w);
272273
ASSERT_EQ(tensor.numel(), w.size());
273274
ASSERT_EQ(tensor.dtype(), at::kDouble);
274-
for (size_t i = 0; i < w.size(); ++i) {
275+
for (const auto i : c10::irange(w.size())) {
275276
ASSERT_TRUE(almost_equal(tensor[i], w.at(i)));
276277
}
277278

@@ -282,7 +283,7 @@ TEST(TensorTest, AtTensorCtorSingleDim) {
282283
tensor = at::tensor(x);
283284
ASSERT_EQ(tensor.numel(), x.size());
284285
ASSERT_EQ(tensor.dtype(), at::kComplexDouble);
285-
for (size_t i = 0; i < x.size(); ++i) {
286+
for (const auto i : c10::irange(x.size())) {
286287
ASSERT_TRUE(almost_equal(tensor[i], x.at(i)));
287288
}
288289
}
@@ -913,8 +914,8 @@ TEST(TensorTest, FromBlobWithStrides) {
913914
ASSERT_EQ(tensor.numel(), 9);
914915
const std::vector<int64_t> expected_strides = {1, 3};
915916
ASSERT_EQ(tensor.strides(), expected_strides);
916-
for (int64_t i = 0; i < tensor.size(0); ++i) {
917-
for (int64_t j = 0; j < tensor.size(1); ++j) {
917+
for (const auto i : c10::irange(tensor.size(0))) {
918+
for (const auto j : c10::irange(tensor.size(1))) {
918919
// NOTE: This is column major because the strides are swapped.
919920
EXPECT_EQ(tensor[i][j].item<int32_t>(), 1 + (j * tensor.size(1)) + i);
920921
}

test/cpp/c10d/ProcessGroupGlooTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void checkProfiledEvents(
242242
auto match = !strcmp(evt.name(), expected_profile_str);
243243
if (verify_shapes && match) {
244244
auto shapesVec = evt.shapes();
245-
for (int i = 0; i < expected_count; i++) {
245+
for (const auto i : c10::irange(expected_count)) {
246246
// Assumptions: no two expected shapes are the same
247247
if (shapesVec[0] == expected_shapes[i]) {
248248
matched_shapes[i] = true;

test/cpp/c10d/ProcessGroupNCCLTest.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ class ReduceScatterBaseNCCLTest : public NCCLTest {
312312
: NCCLTest(path, worldSize) {
313313
output_tensor_ = at::empty({1}, at::kCUDA);
314314
input_tensor_ = at::empty({worldSize}, at::kCUDA);
315-
for(int i = 0; i < worldSize; i++)
316-
{
315+
for (const auto i : c10::irange(worldSize)) {
317316
input_tensor_[i] = i;
318317
}
319318
}

0 commit comments

Comments
 (0)