diff --git a/test/cpp/api/operations.cpp b/test/cpp/api/operations.cpp index 49c205ae403e..bf1643ae1e79 100644 --- a/test/cpp/api/operations.cpp +++ b/test/cpp/api/operations.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -11,7 +12,8 @@ struct OperationTest : torch::test::SeedingFixture { }; TEST_F(OperationTest, Lerp) { - for (auto i = 0; i < TEST_AMOUNT; i++) { + for (const auto i : c10::irange(TEST_AMOUNT)) { + (void)i; // Suppress unused variable warning // test lerp_kernel_scalar auto start = torch::rand({3, 5}); auto end = torch::rand({3, 5}); @@ -35,13 +37,14 @@ TEST_F(OperationTest, Lerp) { } TEST_F(OperationTest, Cross) { - for (auto i = 0; i < TEST_AMOUNT; i++) { + for (const auto i : c10::irange(TEST_AMOUNT)) { + (void)i; // Suppress unused variable warning // input auto a = torch::rand({10, 3}); auto b = torch::rand({10, 3}); // expected auto exp = torch::empty({10, 3}); - for (int j = 0; j < 10; j++) { + for (const auto j : c10::irange(10)) { auto u1 = a[j][0], u2 = a[j][1], u3 = a[j][2]; auto v1 = b[j][0], v2 = b[j][1], v3 = b[j][2]; exp[j][0] = u2 * v3 - v2 * u3; diff --git a/test/cpp/api/optim.cpp b/test/cpp/api/optim.cpp index 026e7eba2a38..8bc2e6ce4479 100644 --- a/test/cpp/api/optim.cpp +++ b/test/cpp/api/optim.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -36,7 +37,7 @@ bool test_optimizer_xor(Options options) { while (running_loss > 0.1) { auto inputs = torch::empty({kBatchSize, 2}); auto labels = torch::empty({kBatchSize}); - for (size_t i = 0; i < kBatchSize; i++) { + for (const auto i : c10::irange(kBatchSize)) { inputs[i] = torch::randint(2, {2}, torch::kInt64); labels[i] = inputs[i][0].item() ^ inputs[i][1].item(); } @@ -112,7 +113,7 @@ void check_exact_values( torch::Tensor input = torch::tensor({0.1, 0.2, 0.3, 0.4, 0.5, 0.6}, torch::kFloat64).reshape({3, 2}); - for (size_t i = 0; i < kIterations; ++i) { + for (const auto i : c10::irange(kIterations)) { optimizer.zero_grad(); auto output = model->forward(input); auto loss = output.sum(); @@ -124,7 +125,7 @@ void check_exact_values( if (i % kSampleEvery == 0) { ASSERT_TRUE( expected_parameters.at(i / kSampleEvery).size() == parameters.size()); - for (size_t p = 0; p < parameters.size(); ++p) { + for (const auto p : c10::irange(parameters.size())) { ASSERT_TRUE(parameters[p]->defined()); // Always compare using double dtype, regardless of the original dtype of the tensors auto computed = parameters[p]->flatten().to(torch::kFloat64); @@ -143,7 +144,8 @@ void check_exact_values( TEST(OptimTest, OptimizerAccessors) { auto options = AdagradOptions(1.0); std::vector params; - for (size_t i = 0; i < 3; i++) { + for (const auto i : c10::irange(3)) { + (void)i; // Suppress unused variable warning params.push_back(torch::randn(10)); } auto optimizer = Adagrad(params, options); @@ -155,7 +157,7 @@ TEST(OptimTest, OptimizerAccessors) { // NOLINTNEXTLINE(modernize-use-emplace) params_groups.push_back(OptimizerParamGroup(params)); auto& params_1 = params_groups[1].params(); - for (size_t i = 0; i < params_1.size(); i++) { + for (const auto i : c10::irange(params_1.size())) { torch::equal(params[i], params_1[i]); } @@ -225,7 +227,7 @@ TEST(OptimTest, OldInterface) { std::vector params_; OLD_INTERFACE_WARNING_CHECK(params_ = optimizer.parameters()); - for (size_t p = 0; p < size; ++p) { + for (const auto p : c10::irange(size)) { ASSERT_TRUE(params_[p].allclose(parameters[p])); } } diff --git a/test/cpp/api/parallel.cpp b/test/cpp/api/parallel.cpp index bec553ed7662..1789fe500087 100644 --- a/test/cpp/api/parallel.cpp +++ b/test/cpp/api/parallel.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -86,7 +87,7 @@ TEST_F(ParallelTest, Replicate_MultiCUDA) { } replicas[0]->to(torch::kCPU); ASSERT_EQ(replica1_parameters.size(), original_parameters.size()); - for (size_t i = 0; i < original_parameters.size(); ++i) { + for (const auto i : c10::irange(original_parameters.size())) { ASSERT_TRUE(replica1_parameters[i].allclose(original_parameters[i])); ASSERT_TRUE( replica1_parameters[i].data_ptr() != @@ -99,7 +100,7 @@ TEST_F(ParallelTest, Replicate_MultiCUDA) { } replicas[1]->to(torch::kCPU); ASSERT_EQ(replica2_parameters.size(), original_parameters.size()); - for (size_t i = 0; i < original_parameters.size(); ++i) { + for (const auto i : c10::irange(original_parameters.size())) { ASSERT_TRUE(replica2_parameters[i].allclose(original_parameters[i])); ASSERT_TRUE( replica2_parameters[i].data_ptr() != @@ -222,7 +223,7 @@ TEST_F(ParallelTest, DataParallelUsesAllAvailableCUDADevices_CUDA) { auto output = parallel::data_parallel(m, input); ASSERT_EQ(output.numel(), device_count); - for (size_t i = 0; i < device_count; ++i) { + for (const auto i : c10::irange(device_count)) { ASSERT_EQ(output[i].item(), i); } } @@ -258,7 +259,7 @@ TEST_F(ParallelTest, DataParallelNumericalEquivalence_MultiCUDA) { auto model_dp = std::dynamic_pointer_cast(model->clone()); // run 3 training iterations - for (int i = 0; i < 3; ++i) { + for (const auto i : c10::irange(3)) { input += i; input_dp += i; diff --git a/test/cpp/api/parameterlist.cpp b/test/cpp/api/parameterlist.cpp index d4c52133ce6d..d5b464c6ec2f 100644 --- a/test/cpp/api/parameterlist.cpp +++ b/test/cpp/api/parameterlist.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -78,11 +79,11 @@ TEST_F(ParameterListTest, AccessWithAt) { ASSERT_EQ(list->size(), 4); // returns the correct module for a given index - for (size_t i = 0; i < params.size(); ++i) { + for (const auto i : c10::irange(params.size())) { ASSERT_TRUE(torch::all(torch::eq(list->at(i), params[i])).item()); } - for (size_t i = 0; i < params.size(); ++i) { + for (const auto i : c10::irange(params.size())) { ASSERT_TRUE(torch::all(torch::eq(list[i], params[i])).item()); } diff --git a/test/cpp/api/sequential.cpp b/test/cpp/api/sequential.cpp index ed245963c7fd..cac719af6cde 100644 --- a/test/cpp/api/sequential.cpp +++ b/test/cpp/api/sequential.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -172,7 +173,7 @@ TEST_F(SequentialTest, AccessWithAt) { ASSERT_EQ(sequential->size(), 3); // returns the correct module for a given index - for (size_t i = 0; i < modules.size(); ++i) { + for (const auto i : c10::irange(modules.size())) { ASSERT_EQ(&sequential->at(i), modules[i].get()); } @@ -201,7 +202,7 @@ TEST_F(SequentialTest, AccessWithPtr) { ASSERT_EQ(sequential->size(), 3); // returns the correct module for a given index - for (size_t i = 0; i < modules.size(); ++i) { + for (const auto i : c10::irange(modules.size())) { ASSERT_EQ(sequential->ptr(i).get(), modules[i].get()); ASSERT_EQ(sequential[i].get(), modules[i].get()); ASSERT_EQ(sequential->ptr(i).get(), modules[i].get()); diff --git a/test/cpp/api/serialize.cpp b/test/cpp/api/serialize.cpp index 46ede0f156ac..b422662aa362 100644 --- a/test/cpp/api/serialize.cpp +++ b/test/cpp/api/serialize.cpp @@ -2,6 +2,7 @@ #include #include +#include #include @@ -41,7 +42,7 @@ void is_optimizer_param_group_equal(const OptimizerParamGroup& lhs, const Optimi const auto& rhs_params = rhs.params(); ASSERT_TRUE(lhs_params.size() == rhs_params.size()); - for (size_t j = 0; j < lhs_params.size(); j++) { + for (const auto j : c10::irange(lhs_params.size())) { ASSERT_TRUE(torch::equal(lhs_params[j], rhs_params[j])); } ASSERT_TRUE(static_cast(lhs.options()) == static_cast(rhs.options())); @@ -136,7 +137,7 @@ void test_serialize_optimizer(DerivedOptimizerOptions options, bool only_has_glo ASSERT_TRUE(optim3_2_state.size() == optim3_state.size()); // checking correctness of serialization logic for optimizer.param_groups_ and optimizer.state_ - for (int i = 0; i < optim3_2_param_groups.size(); i++) { + for (const auto i : c10::irange(optim3_2_param_groups.size())) { is_optimizer_param_group_equal( optim3_2_param_groups[i], optim3_param_groups[i]); is_optimizer_state_equal(optim3_2_state, optim3_state); @@ -173,7 +174,7 @@ void write_tensors_to_archive( const BufferContainer& buffers) { archive.write( key + "/size", torch::tensor(static_cast(buffers.size()))); - for (size_t index = 0; index < buffers.size(); ++index) { + for (const auto index : c10::irange(buffers.size())) { archive.write( key + "/" + c10::to_string(index), buffers[index], /*is_buffer=*/true); } @@ -203,7 +204,7 @@ void write_step_buffers( TEST(SerializeTest, KeysFunc) { auto tempfile = c10::make_tempfile(); torch::serialize::OutputArchive output_archive; - for (size_t i = 0; i < 3; i++) { + for (const auto i : c10::irange(3)) { output_archive.write("element/" + c10::to_string(i), c10::IValue(static_cast(i))); } output_archive.save_to(tempfile.name); @@ -211,7 +212,7 @@ TEST(SerializeTest, KeysFunc) { input_archive.load_from(tempfile.name); std::vector keys = input_archive.keys(); ASSERT_EQ(keys.size(), 3); - for (size_t i = 0; i < keys.size(); i++) { + for (const auto i : c10::irange(keys.size())) { ASSERT_EQ(keys[i], "element/" + c10::to_string(i)); } } @@ -219,7 +220,7 @@ TEST(SerializeTest, KeysFunc) { TEST(SerializeTest, TryReadFunc) { auto tempfile = c10::make_tempfile(); torch::serialize::OutputArchive output_archive; - for (size_t i = 0; i < 3; i++) { + for (const auto i : c10::irange(3)) { output_archive.write("element/" + c10::to_string(i), c10::IValue(static_cast(i))); } output_archive.save_to(tempfile.name); @@ -363,7 +364,7 @@ TEST(SerializeTest, XOR) { auto getLoss = [](Sequential model, uint32_t batch_size) { auto inputs = torch::empty({batch_size, 2}); auto labels = torch::empty({batch_size}); - for (size_t i = 0; i < batch_size; i++) { + for (const auto i : c10::irange(batch_size)) { inputs[i] = torch::randint(2, {2}, torch::kInt64); labels[i] = inputs[i][0].item() ^ inputs[i][1].item(); } @@ -533,7 +534,7 @@ TEST(SerializeTest, Optim_SGD) { int64_t iteration_{0}; const auto& params_ = optim1.param_groups()[0].params(); const auto& optim1_state = optim1.state(); - for (size_t i = 0; i < params_.size(); i++) { + for (const auto i : c10::irange(params_.size())) { if(i != (params_.size() - 1)) { auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl()); const SGDParamState& curr_state_ = static_cast(*(optim1_state.at(key_).get())); @@ -577,7 +578,7 @@ TEST(SerializeTest, Optim_Adam) { std::vector max_exp_average_sq_buffers; const auto& params_ = optim1.param_groups()[0].params(); const auto& optim1_state = optim1.state(); - for (size_t i = 0; i < params_.size(); i++) { + for (const auto i : c10::irange(params_.size())) { if(i != (params_.size() - 1)) { auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl()); const AdamParamState& curr_state_ = static_cast(*(optim1_state.at(key_).get())); @@ -627,7 +628,7 @@ TEST(SerializeTest, Optim_AdamW) { std::vector max_exp_average_sq_buffers; const auto& params_ = optim1.param_groups()[0].params(); const auto& optim1_state = optim1.state(); - for (size_t i = 0; i < params_.size(); i++) { + for (const auto i : c10::irange(params_.size())) { if(i != (params_.size() - 1)) { auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl()); const AdamWParamState& curr_state_ = static_cast(*(optim1_state.at(key_).get())); @@ -678,7 +679,7 @@ TEST(SerializeTest, Optim_RMSprop) { std::vector grad_average_buffers; const auto& params_ = optim1.param_groups()[0].params(); const auto& optim1_state = optim1.state(); - for (size_t i = 0; i < params_.size(); i++) { + for (const auto i : c10::irange(params_.size())) { if(i != (params_.size() - 1)) { auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl()); const RMSpropParamState& curr_state_ = static_cast(*(optim1_state.at(key_).get())); @@ -703,7 +704,7 @@ TEST(SerializeTest, Optim_RMSprop) { const auto& params1_2_ = optim1_2.param_groups()[0].params(); auto& optim1_2_state = optim1_2.state(); // old RMSprop didn't track step value - for (size_t i = 0; i < params1_2_.size(); i++) { + for (const auto i : c10::irange(params1_2_.size())) { if(i != (params1_2_.size() - 1)) { auto key_ = c10::guts::to_string(params_[i].unsafeGetTensorImpl()); auto key1_2_ = c10::guts::to_string(params1_2_[i].unsafeGetTensorImpl()); @@ -788,7 +789,7 @@ TEST(SerializeTest, XOR_CUDA) { inputs = inputs.cuda(); labels = labels.cuda(); } - for (size_t i = 0; i < batch_size; i++) { + for (const auto i : c10::irange(batch_size)) { inputs[i] = torch::randint(2, {2}, torch::kInt64); labels[i] = inputs[i][0].item() ^ inputs[i][1].item(); } @@ -879,7 +880,7 @@ TEST(SerializeTest, VectorOfTensors) { std::vector y_vec; torch::load(y_vec, stream); - for (int64_t i = 0; i < x_vec.size(); i++) { + for (const auto i : c10::irange(x_vec.size())) { auto& x = x_vec[i]; auto& y = y_vec[i]; ASSERT_TRUE(y.defined()); diff --git a/test/cpp/api/static.cpp b/test/cpp/api/static.cpp index df90d1fc0376..36bb5deb00cf 100644 --- a/test/cpp/api/static.cpp +++ b/test/cpp/api/static.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -95,7 +96,7 @@ TEST(TestStatic, Apply) { std::vector v; torch::apply([&v](int x) { v.push_back(x); }, 1, 2, 3, 4, 5); ASSERT_EQ(v.size(), 5); - for (size_t i = 0; i < v.size(); ++i) { + for (const auto i : c10::irange(v.size())) { ASSERT_EQ(v.at(i), i + 1); } } diff --git a/test/cpp/api/tensor.cpp b/test/cpp/api/tensor.cpp index 362cc6af1285..eac3a04205dd 100644 --- a/test/cpp/api/tensor.cpp +++ b/test/cpp/api/tensor.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -263,7 +264,7 @@ TEST(TensorTest, AtTensorCtorSingleDim) { tensor = at::tensor(v); ASSERT_EQ(tensor.numel(), v.size()); ASSERT_EQ(tensor.dtype(), at::kInt); - for (size_t i = 0; i < v.size(); ++i) { + for (const auto i : c10::irange(v.size())) { ASSERT_TRUE(exactly_equal(tensor[i], v.at(i))); } @@ -271,7 +272,7 @@ TEST(TensorTest, AtTensorCtorSingleDim) { tensor = at::tensor(w); ASSERT_EQ(tensor.numel(), w.size()); ASSERT_EQ(tensor.dtype(), at::kDouble); - for (size_t i = 0; i < w.size(); ++i) { + for (const auto i : c10::irange(w.size())) { ASSERT_TRUE(almost_equal(tensor[i], w.at(i))); } @@ -282,7 +283,7 @@ TEST(TensorTest, AtTensorCtorSingleDim) { tensor = at::tensor(x); ASSERT_EQ(tensor.numel(), x.size()); ASSERT_EQ(tensor.dtype(), at::kComplexDouble); - for (size_t i = 0; i < x.size(); ++i) { + for (const auto i : c10::irange(x.size())) { ASSERT_TRUE(almost_equal(tensor[i], x.at(i))); } } @@ -913,8 +914,8 @@ TEST(TensorTest, FromBlobWithStrides) { ASSERT_EQ(tensor.numel(), 9); const std::vector expected_strides = {1, 3}; ASSERT_EQ(tensor.strides(), expected_strides); - for (int64_t i = 0; i < tensor.size(0); ++i) { - for (int64_t j = 0; j < tensor.size(1); ++j) { + for (const auto i : c10::irange(tensor.size(0))) { + for (const auto j : c10::irange(tensor.size(1))) { // NOTE: This is column major because the strides are swapped. EXPECT_EQ(tensor[i][j].item(), 1 + (j * tensor.size(1)) + i); } diff --git a/test/cpp/c10d/ProcessGroupGlooTest.cpp b/test/cpp/c10d/ProcessGroupGlooTest.cpp index 5c490fe28e2e..b278bfd35187 100644 --- a/test/cpp/c10d/ProcessGroupGlooTest.cpp +++ b/test/cpp/c10d/ProcessGroupGlooTest.cpp @@ -242,7 +242,7 @@ void checkProfiledEvents( auto match = !strcmp(evt.name(), expected_profile_str); if (verify_shapes && match) { auto shapesVec = evt.shapes(); - for (int i = 0; i < expected_count; i++) { + for (const auto i : c10::irange(expected_count)) { // Assumptions: no two expected shapes are the same if (shapesVec[0] == expected_shapes[i]) { matched_shapes[i] = true; diff --git a/test/cpp/c10d/ProcessGroupNCCLTest.cpp b/test/cpp/c10d/ProcessGroupNCCLTest.cpp index 410e7470642d..65b9c00de2b7 100644 --- a/test/cpp/c10d/ProcessGroupNCCLTest.cpp +++ b/test/cpp/c10d/ProcessGroupNCCLTest.cpp @@ -312,8 +312,7 @@ class ReduceScatterBaseNCCLTest : public NCCLTest { : NCCLTest(path, worldSize) { output_tensor_ = at::empty({1}, at::kCUDA); input_tensor_ = at::empty({worldSize}, at::kCUDA); - for(int i = 0; i < worldSize; i++) - { + for (const auto i : c10::irange(worldSize)) { input_tensor_[i] = i; } } diff --git a/test/cpp/rpc/test_tensorpipe_serialization.cpp b/test/cpp/rpc/test_tensorpipe_serialization.cpp index 1de13fd353c2..b561e71aec99 100644 --- a/test/cpp/rpc/test_tensorpipe_serialization.cpp +++ b/test/cpp/rpc/test_tensorpipe_serialization.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -64,7 +65,7 @@ TEST(TensorpipeSerialize, Base) { // Mimic tensorpipe data transfer EXPECT_EQ( recvingTpAllocation.payloads.size(), sendingTpMessage.payloads.size()); - for (int i = 0; i < recvingTpAllocation.payloads.size(); i++) { + for (const auto i : c10::irange(recvingTpAllocation.payloads.size())) { tensorpipe::Message::Payload& srcPayload = sendingTpMessage.payloads[i]; tensorpipe::Allocation::Payload& dstPayload = recvingTpAllocation.payloads[i]; @@ -76,7 +77,7 @@ TEST(TensorpipeSerialize, Base) { } EXPECT_EQ( recvingTpAllocation.tensors.size(), sendingTpMessage.tensors.size()); - for (int i = 0; i < recvingTpAllocation.tensors.size(); i++) { + for (const auto i : c10::irange(recvingTpAllocation.tensors.size())) { tensorpipe::Message::Tensor& srcTensor = sendingTpMessage.tensors[i]; tensorpipe::Allocation::Tensor& dstTensor = recvingTpAllocation.tensors[i]; memcpy( diff --git a/test/cpp/rpc/test_wire_serialization.cpp b/test/cpp/rpc/test_wire_serialization.cpp index 9c949dd94c03..ed96b52fa18b 100644 --- a/test/cpp/rpc/test_wire_serialization.cpp +++ b/test/cpp/rpc/test_wire_serialization.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -27,7 +28,7 @@ TEST(WireSerialize, Base) { EXPECT_TRUE( memcmp(deser.first.data(), payload.data(), payload.size()) == 0); } - for (size_t i = 0; i < tensors.size(); ++i) { + for (const auto i : c10::irange(tensors.size())) { EXPECT_TRUE(torch::equal(tensors[i], deser.second[i])); } }; diff --git a/test/cpp/tensorexpr/padded_buffer.cpp b/test/cpp/tensorexpr/padded_buffer.cpp index a78e7c29436b..2b6202557fdb 100644 --- a/test/cpp/tensorexpr/padded_buffer.cpp +++ b/test/cpp/tensorexpr/padded_buffer.cpp @@ -1,6 +1,7 @@ #include "test/cpp/tensorexpr/padded_buffer.h" #include +#include #include namespace torch { @@ -10,7 +11,7 @@ namespace tensorexpr { int PaddedBufferBase::Index(const std::vector& indices) const { DCHECK_EQ(dims_.size(), indices.size()); int total_index = 0; - for (size_t i = 0; i < dims_.size(); i++) { + for (const auto i : c10::irange(dims_.size())) { total_index += indices[i] * strides_[i]; } return total_index; diff --git a/test/cpp/tensorexpr/padded_buffer.h b/test/cpp/tensorexpr/padded_buffer.h index ab050d5c8f1a..b3e5227ae7e6 100644 --- a/test/cpp/tensorexpr/padded_buffer.h +++ b/test/cpp/tensorexpr/padded_buffer.h @@ -3,6 +3,7 @@ #include #include +#include #include "torch/csrc/jit/tensorexpr/eval.h" namespace torch { @@ -168,7 +169,7 @@ class PaddedBuffer : public PaddedBufferBase { // Verify the watermarks in the paddings are intact. void ValidateWatermark() const { - for (int i = 0; i < kPaddingSize; i++) { + for (const auto i : c10::irange(kPaddingSize)) { ASSERT_EQ(data_[i], kPaddingValue); ASSERT_EQ(data_[i + total_size_ + kPaddingSize], kPaddingValue); } @@ -178,7 +179,7 @@ class PaddedBuffer : public PaddedBufferBase { ValidateWatermark(); DCHECK(backup_data_.size() == data_.size()) << "Please make sure you have call Backup() before calling CheckBackup()"; - for (int i = 0; i < total_size_; i++) { + for (const auto i : c10::irange(total_size_)) { ASSERT_EQ(data_[i + kPaddingSize], backup_data_[i + kPaddingSize]); } } @@ -214,7 +215,7 @@ void ExpectAllEqual(const PaddedBuffer& f1, const PaddedBuffer& f2) { ASSERT_EQ(v1.size(), v2.size()); f1.ValidateWatermark(); f2.ValidateWatermark(); - for (int i = 0; i < total_size; i++) { + for (const auto i : c10::irange(total_size)) { ASSERT_EQ(v1[kPaddingSize + i], v2[kPaddingSize + i]); } } @@ -231,7 +232,7 @@ void ExpectAllNear( ASSERT_EQ(v1.size(), v2.size()); f1.ValidateWatermark(); f2.ValidateWatermark(); - for (int i = 0; i < total_size; i++) { + for (const auto i : c10::irange(total_size)) { ASSERT_NEAR(v1[kPaddingSize + i], v2[kPaddingSize + i], abs_error); } } diff --git a/test/cpp/tensorexpr/test_aten.cpp b/test/cpp/tensorexpr/test_aten.cpp index ecc6365a79f6..34ce2bd069d5 100644 --- a/test/cpp/tensorexpr/test_aten.cpp +++ b/test/cpp/tensorexpr/test_aten.cpp @@ -5,6 +5,7 @@ #include #include +#include #include "test/cpp/tensorexpr/padded_buffer.h" #include "test/cpp/tensorexpr/test_base.h" #include "torch/csrc/jit/tensorexpr/ir_printer.h" @@ -28,14 +29,14 @@ TEST(ATen, _cast_Float) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), static_cast(i)); } @@ -55,14 +56,14 @@ TEST(ATen, negInt) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), -static_cast(i)); } @@ -82,14 +83,14 @@ TEST(ATen, negFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), -i); } @@ -114,7 +115,7 @@ TEST(ATen, addInt) { PaddedBuffer c_v(kTotalSize); PaddedBuffer d_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; c_v(i) = 3 * i + 2; @@ -123,7 +124,7 @@ TEST(ATen, addInt) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf, d_buf}); ir_eval(a_v, b_v, c_v, d_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), 3 * i + 2); @@ -150,7 +151,7 @@ TEST(ATen, addFloat) { PaddedBuffer c_v(kTotalSize); PaddedBuffer d_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; c_v(i) = 3 * i + 2; @@ -159,7 +160,7 @@ TEST(ATen, addFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf, d_buf}); ir_eval(a_v, b_v, c_v, d_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), 3 * i + 2); @@ -186,7 +187,7 @@ TEST(ATen, subInt) { PaddedBuffer c_v(kTotalSize); PaddedBuffer d_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; c_v(i) = 3 * i + 2; @@ -195,7 +196,7 @@ TEST(ATen, subInt) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf, d_buf}); ir_eval(a_v, b_v, c_v, d_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), 3 * i + 2); @@ -222,7 +223,7 @@ TEST(ATen, subFloat) { PaddedBuffer c_v(kTotalSize); PaddedBuffer d_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; c_v(i) = 3 * i + 2; @@ -231,7 +232,7 @@ TEST(ATen, subFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf, d_buf}); ir_eval(a_v, b_v, c_v, d_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), 3 * i + 2); @@ -258,7 +259,7 @@ TEST(ATen, lerp) { PaddedBuffer c_v(kTotalSize); PaddedBuffer d_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; c_v(i) = 3 * i + 2; @@ -267,7 +268,7 @@ TEST(ATen, lerp) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf, d_buf}); ir_eval(a_v, b_v, c_v, d_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), 3 * i + 2); @@ -297,7 +298,7 @@ TEST(ATen, addcmulInt) { PaddedBuffer d_v(kTotalSize); PaddedBuffer e_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; c_v(i) = 3 * i + 2; @@ -307,7 +308,7 @@ TEST(ATen, addcmulInt) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf, d_buf, e_buf}); ir_eval(a_v, b_v, c_v, d_v, e_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), 3 * i + 2); @@ -338,7 +339,7 @@ TEST(ATen, addcmulFloat) { PaddedBuffer d_v(kTotalSize); PaddedBuffer e_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; c_v(i) = 3 * i + 2; @@ -348,7 +349,7 @@ TEST(ATen, addcmulFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf, d_buf, e_buf}); ir_eval(a_v, b_v, c_v, d_v, e_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), 3 * i + 2); @@ -373,7 +374,7 @@ TEST(ATen, mulInt) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; } @@ -381,7 +382,7 @@ TEST(ATen, mulInt) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), a_v(i) * b_v(i)); @@ -404,7 +405,7 @@ TEST(ATen, mulFloat) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; } @@ -412,7 +413,7 @@ TEST(ATen, mulFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), a_v(i) * b_v(i)); @@ -435,7 +436,7 @@ TEST(ATen, divInt) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = 2 * i + 1; b_v(i) = i + 1; } @@ -443,7 +444,7 @@ TEST(ATen, divInt) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), 2 * i + 1); ASSERT_EQ(b_v(i), i + 1); ASSERT_EQ(c_v(i), a_v(i) / b_v(i)); @@ -466,7 +467,7 @@ TEST(ATen, divFloat) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = 2 * i + 1; b_v(i) = i + 1; } @@ -474,7 +475,7 @@ TEST(ATen, divFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), 2 * i + 1); ASSERT_EQ(b_v(i), i + 1); ASSERT_EQ(c_v(i), a_v(i) / b_v(i)); @@ -497,7 +498,7 @@ TEST(ATen, maxInt) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; } @@ -505,7 +506,7 @@ TEST(ATen, maxInt) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), std::max(a_v(i), b_v(i))); @@ -528,7 +529,7 @@ TEST(ATen, maxFloat) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; } @@ -536,7 +537,7 @@ TEST(ATen, maxFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), std::fmax(a_v(i), b_v(i))); @@ -559,7 +560,7 @@ TEST(ATen, minInt) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; } @@ -567,7 +568,7 @@ TEST(ATen, minInt) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), std::min(a_v(i), b_v(i))); @@ -590,7 +591,7 @@ TEST(ATen, minFloat) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; b_v(i) = 2 * i + 1; } @@ -598,7 +599,7 @@ TEST(ATen, minFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf, c_buf}); ir_eval(a_v, b_v, c_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 2 * i + 1); ASSERT_EQ(c_v(i), std::fmin(a_v(i), b_v(i))); @@ -618,14 +619,14 @@ void __ubsan_ignore_float_divide_by_zero__ testATenreciprocal() { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i); ASSERT_EQ(b_v(i), 1.0f / i); } @@ -644,14 +645,14 @@ TEST(ATen, reluInt) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i - 64; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i - 64); ASSERT_EQ(b_v(i), std::max(a_v(i), 0)); } @@ -672,14 +673,14 @@ TEST(ATen, reluFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i - 64; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i - 64); ASSERT_EQ(b_v(i), std::fmax(a_v(i), 0)); } @@ -698,14 +699,14 @@ TEST(ATen, logFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i + 10; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i + 10); ASSERT_EQ(b_v(i), std::log(a_v(i))); } @@ -724,14 +725,14 @@ TEST(ATen, fastLogFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = at::randn({1}).item().to(); } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { auto test = b_v(i); auto ref = std::log(a_v(i)); if (std::isnan(ref)) { @@ -755,14 +756,14 @@ TEST(ATen, fastTanhFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = at::randn({1}).item().to(); } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { auto test = b_v(i); auto ref = std::tanh(a_v(i)); if (std::isnan(ref)) { @@ -786,14 +787,14 @@ TEST(ATen, fastSigmoidFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = at::randn({1}).item().to(); } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { auto test = b_v(i); at::Tensor t = at::ones({1}) * a_v(i); float ref = at::sigmoid(t).item().to(); @@ -818,14 +819,14 @@ TEST(ATen, log10Float) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i + 10; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i + 10); ASSERT_EQ(b_v(i), std::log10(a_v(i))); } @@ -844,14 +845,14 @@ TEST(ATen, log2Float) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i + 10; } SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i + 10); ASSERT_EQ(b_v(i), std::log2(a_v(i))); } @@ -870,7 +871,7 @@ TEST(ATen, expFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions) a_v(i) = i / 10.0f; } @@ -878,7 +879,7 @@ TEST(ATen, expFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i / 10.0f); ASSERT_EQ(b_v(i), std::exp(a_v(i))); } @@ -897,7 +898,7 @@ TEST(ATen, erfFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions) a_v(i) = i / 10.0f; } @@ -905,7 +906,7 @@ TEST(ATen, erfFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i / 10.0f); ASSERT_EQ(b_v(i), std::erf(a_v(i))); } @@ -924,7 +925,7 @@ TEST(ATen, cosFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions) a_v(i) = i / 10.0f; } @@ -932,7 +933,7 @@ TEST(ATen, cosFloat) { SimpleIREvaluator ir_eval(stmt, {a_buf, b_buf}); ir_eval(a_v, b_v); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { ASSERT_EQ(a_v(i), i / 10.0f); ASSERT_EQ(b_v(i), std::cos(a_v(i))); } diff --git a/test/cpp/tensorexpr/test_boundsinference.cpp b/test/cpp/tensorexpr/test_boundsinference.cpp index 3a1645147f0a..7cabee0ce55e 100644 --- a/test/cpp/tensorexpr/test_boundsinference.cpp +++ b/test/cpp/tensorexpr/test_boundsinference.cpp @@ -5,6 +5,7 @@ #include +#include #include #include #include @@ -26,7 +27,7 @@ static void verifyConstBounds( size_t ndim = ref.size(); ASSERT_EQ(access_info.start.size(), ndim); ASSERT_EQ(access_info.stop.size(), ndim); - for (size_t i = 0; i < ndim; i++) { + for (const auto i : c10::irange(ndim)) { if (ref[i].first >= 0) { // Negative values are used to skip the check ASSERT_TRUE(access_info.start[i]->isConstant()); int start_i = immediateAs(access_info.start[i]); @@ -524,14 +525,14 @@ TEST(BoundsInference, CacheReads) { // Same number of TensorAccessBoundInfos. ASSERT_EQ(pair.second.size(), beforeIt->second.size()); - for (size_t i = 0; i < pair.second.size(); ++i) { + for (const auto i : c10::irange(pair.second.size())) { TensorAccessBoundsInfo& after = pair.second[i]; TensorAccessBoundsInfo& before = beforeIt->second[i]; // Same number of dimensions. ASSERT_EQ(before.start.size(), after.start.size()); // Bounds are equal. - for (size_t j = 0; j < before.start.size(); ++j) { + for (const auto j : c10::irange(before.start.size())) { ASSERT_TRUE(exprEquals(before.start[j], after.start[j])); ASSERT_TRUE(exprEquals(before.stop[j], after.stop[j])); } @@ -550,7 +551,7 @@ TEST(BoundsInference, CacheReads) { ASSERT_EQ(first.start.size(), 2); // bounds for load and store are equal. - for (size_t j = 0; j < first.start.size(); ++j) { + for (const auto j : c10::irange(first.start.size())) { ASSERT_TRUE(exprEquals(first.start[j], second.start[j])); ASSERT_TRUE(exprEquals(first.stop[j], second.stop[j])); } @@ -713,10 +714,10 @@ TEST(BoundsInference, GetPotentialHazardsLoopSplit) { TEST(BoundsInference, HasConflictingOverlapSameBufferWithPartialOverlap) { // Input IR: - // for (int j = 10; j < 100; j++) { + // for (const auto j : c10::irange(10, 100)) { // A[j] = 10 * j; // } - // for (int k = 10; k < 100; k++) { + // for (const auto k : c10::irange(10, 100)) { // A[k-1] = 20 * k; // } BufHandle a_buf("A", {200}, kInt); @@ -735,10 +736,10 @@ TEST(BoundsInference, HasConflictingOverlapSameBufferWithPartialOverlap) { TEST(BoundsInference, HasConflictingOverlapSameBufferWithFullOverlap) { // Input IR: - // for (int j = 10; j < 100; j++) { + // for (const auto j : c10::irange(10, 100)) { // A[j] = 10 * j; // } - // for (int k = 10; k < 100; k++) { + // for (const auto k : c10::irange(10, 100)) { // A[k] = 20 * k; // } BufHandle a_buf("A", {200}, kInt); @@ -756,10 +757,10 @@ TEST(BoundsInference, HasConflictingOverlapSameBufferWithFullOverlap) { TEST(BoundsInference, HasConflictingOverlapSameBufferWithFullOverlapRAW) { // Input IR: - // for (int j = 10; j < 100; j++) { + // for (const auto j : c10::irange(10, 100)) { // A[j] = 10 * j; // } - // for (int k = 10; k < 100; k++) { + // for (const auto k : c10::irange(10, 100)) { // B[k] = A[k]; // } BufHandle a_buf("A", {200}, kInt); @@ -779,10 +780,10 @@ TEST(BoundsInference, HasConflictingOverlapSameBufferWithFullOverlapRAW) { TEST(BoundsInference, HasConflictingOverlapSameBufferNotOverlapping) { // Input IR: - // for (int j = 10; j < 100; j++) { + // for (const auto j : c10::irange(10, 100)) { // A[j] = 10 * j; // } - // for (int k = 10; k < 100; k++) { + // for (const auto k : c10::irange(10, 100)) { // A[k+100] = 20 * k; // } BufHandle a_buf("A", {200}, kInt); @@ -801,13 +802,13 @@ TEST(BoundsInference, HasConflictingOverlapSameBufferNotOverlapping) { TEST(BoundsInference, HasConflictingOverlap2DBufferWithOverlap) { // Input IR: - // for (int i = 0; i < 20; i++) { - // for (int j = 0; j < 100; j++) { + // for (const auto i : c10::irange(20)) { + // for (const auto j : c10::irange(100)) { // A[i,j] = i * j * 500; // } // } - // for (int m = 0; m < 20; m++) { - // for (int n = 0; n < 50; n++) { + // for (const auto m : c10::irange(20)) { + // for (const auto n : c10::irange(50)) { // A[m+1,n] = m + n * 100; // } // } @@ -840,13 +841,13 @@ TEST(BoundsInference, HasConflictingOverlap2DBufferWithOverlap) { TEST(BoundsInference, HasConflictingOverlap2DBufferWithNoOverlap) { // Input IR: - // for (int i = 0; i < 20; i++) { - // for (int j = 0; j < 100; j++) { + // for (const auto i : c10::irange(20)) { + // for (const auto j : c10::irange(100)) { // A[i,j] = i * j * 500; // } // } - // for (int m = 0; m < 20; m++) { - // for (int n = 0; n < 50; n++) { + // for (const auto m : c10::irange(20)) { + // for (const auto n : c10::irange(50)) { // A[m+20,n+100] = m + n * 100; // } // } @@ -879,13 +880,13 @@ TEST(BoundsInference, HasConflictingOverlap2DBufferWithNoOverlap) { TEST(BoundsInference, HasConflictingOverlapDifferentBuffers) { // Input IR: - // for (int i = 0; i < 20; i++) { - // for (int j = 0; j < 100; j++) { + // for (const auto i : c10::irange(20)) { + // for (const auto j : c10::irange(100)) { // A[i,j] = i * j * 500; // } // } - // for (int m = 0; m < 20; m++) { - // for (int n = 0; n < 50; n++) { + // for (const auto m : c10::irange(20)) { + // for (const auto n : c10::irange(50)) { // B[m,n] = m + n * 100; // } // } @@ -917,10 +918,10 @@ TEST(BoundsInference, HasConflictingOverlapDifferentBuffers) { TEST(BoundsInference, HasConflictingOverlapDueToRAWDependence) { // Input IR: - // for (int j = 0; j < 100; j++) { + // for (const auto j : c10::irange(100)) { // A[j] = 10 * j; // } - // for (int k = 0; k < 100; k++) { + // for (const auto k : c10::irange(100)) { // B[k] = 20 * A[99-k]; // } BufHandle a_buf("A", {100}, kInt); @@ -944,10 +945,10 @@ TEST(BoundsInference, HasConflictingOverlapDueToRAWDependence) { TEST(BoundsInference, HasConflictingOverlapDueToWARDependence) { // Input IR: - // for (int k = 0; k < 100; k++) { + // for (const auto k : c10::irange(100)) { // B[k] = 20 * A[99-k]; // } - // for (int j = 0; j < 100; j++) { + // for (const auto j : c10::irange(100)) { // A[j] = 10 * j; // } BufHandle a_buf("A", {100}, kInt); @@ -971,10 +972,10 @@ TEST(BoundsInference, HasConflictingOverlapDueToWARDependence) { TEST(BoundsInference, HasConflictingOverlapWithLoads) { // Input IR: - // for (int k = 10; k < 100; k++) { + // for (const auto k : c10::irange(10, 100)) { // B[k] = 20 * A[99-k]; // } - // for (int j = 10; j < 100; j++) { + // for (const auto j : c10::irange(10, 100)) { // C[j] = 10 * A[j]; // } BufHandle a_buf("A", {100}, kInt); @@ -1003,7 +1004,7 @@ TEST(BoundsInference, HasConflictingOverlapWithLoads) { TEST(BoundsInference, IsOverlapping) { // Input IR: - // for (int i = 0; i < 100; i++) { + // for (const auto i : c10::irange(100)) { // A[i] = i * 10; // storeA1 // B[i] = A[99-i] * 20; // loadA1 // C[i] = A[i + 100] * 10; // loadA2 diff --git a/test/cpp/tensorexpr/test_cpp_codegen.cpp b/test/cpp/tensorexpr/test_cpp_codegen.cpp index 2603611ec5a4..ed7679053637 100644 --- a/test/cpp/tensorexpr/test_cpp_codegen.cpp +++ b/test/cpp/tensorexpr/test_cpp_codegen.cpp @@ -2,6 +2,7 @@ #include "test/cpp/tensorexpr/test_base.h" +#include #include #include #include @@ -207,7 +208,7 @@ TEST(CppPrinter, Cond) { TEST(CppPrinter, Intrinsics) { const std::unordered_set> unsupported_ops{ kRand, kSigmoid}; - for (int i = 0; i < kMaxIntrinsicsOp; i++) { + for (const auto i : c10::irange(static_cast(kMaxIntrinsicsOp))) { IntrinsicsOp op = static_cast(i); if (unsupported_ops.count(op)) { continue; diff --git a/test/cpp/tensorexpr/test_cuda.cpp b/test/cpp/tensorexpr/test_cuda.cpp index 6c1a11aa87b7..bc267d9158a5 100644 --- a/test/cpp/tensorexpr/test_cuda.cpp +++ b/test/cpp/tensorexpr/test_cuda.cpp @@ -19,6 +19,7 @@ #include #include +#include namespace torch { namespace jit { @@ -56,7 +57,7 @@ static void testCudaTestVectorAdd01_impl() { PaddedBuffer c_v(N); PaddedBuffer c_ref(N); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { a_v(i) = ctype(i); b_v(i) = ctype(i * 3 + 7); c_ref(i) = a_v(i) + b_v(i); @@ -119,7 +120,7 @@ TEST(Cuda, Sigmoid_CUDA) { PaddedBuffer c_v(N); PaddedBuffer c_ref(N); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { a_v(i) = float(i); c_ref(i) = sigmoid(sigmoid(a_v(i))); } @@ -182,7 +183,7 @@ static void testCudaTestVectorAdd02_impl(int N, int block_size) { PaddedBuffer c_v(N); PaddedBuffer c_ref(N); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { a_v(i) = i; b_v(i) = i * 3 + 7; c_ref(i) = a_v(i) + b_v(i); @@ -356,7 +357,7 @@ TEST(Cuda, TestRand01_CUDA) { float sum1 = 0; float sum2 = 0; float sum3 = 0; - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { float v = c_v.data()[i]; sum1 += v; sum2 += v * v; @@ -431,10 +432,10 @@ TEST(Cuda, OneBlockOneThreadGlobalReduce1_CUDA) { BufHandle output_buf("output", {1}, kFloat); // The test adds the following code for trivial reduction: - // for (int bidx = 0; bidx < 1; bidx++) { // blockIdx.x - // for (int tidx = 0; tidx < 1; tidx++) { // threadIdx.x + // for (const auto bidx : c10::irange(1)) { // blockIdx.x + // for (const auto tidx : c10::irange(1)) { // threadIdx.x // output[0] = 0.f; - // for (int i1 = 0; i1 < 1024; i1++) { + // for (const auto i1 : c10::irange(1024)) { // output[0] = output[0] + data[i1]; // } // } @@ -465,7 +466,7 @@ TEST(Cuda, OneBlockOneThreadGlobalReduce1_CUDA) { PaddedBuffer output_ref(1, "output_ref"); output_ref(0) = 0; - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { data_v(i) = i; output_ref(0) += data_v(i); } @@ -544,7 +545,7 @@ TEST(Cuda, OneBlockMultiThreadGlobalReduce1_CUDA) { PaddedBuffer b_ref(1, "b_ref"); b_ref(0) = 0; - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { a_v(i) = i; b_ref(0) += a_v(i); } @@ -634,11 +635,11 @@ TEST(Cuda, NoThreadIdxWrite_1_CUDA) { PaddedBuffer b_ref(N, "b_ref"); a_ref(0) = 0; - for (int i = 0; i < 2; i++) { + for (const auto i : c10::irange(2)) { a_ref(0) += i; } a_ref(1) = a_ref(0) + 1; - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { b_ref(i) = i; } @@ -772,8 +773,8 @@ TEST(Cuda, SharedMemReduce_1_CUDA) { PaddedBuffer b_ref(1, "b_ref"); b_ref(0) = 0; - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { int v = i + j; a_v(0, i, j) = v; b_ref(0) += v; @@ -882,8 +883,8 @@ TEST(Cuda, LocalMemReduce_1_CUDA) { PaddedBuffer b_ref(1, "b_ref"); b_ref(0) = 0; - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { int v = i + j; a_v(0, i, j) = v; b_ref(0) += v; @@ -1083,7 +1084,7 @@ TEST(Cuda, PrioritizeDependents_CUDA) { VarHandle j("j", kInt); /* - * for (int i = 0; i < 12; ++i) { + * for (const auto i : c10::irange(12)) { * c[i] = (i < 10 ? a[i] + b[i] : b[i]); * } */ @@ -1102,13 +1103,13 @@ TEST(Cuda, PrioritizeDependents_CUDA) { PaddedBuffer c_v(12, "c_v"); PaddedBuffer c_ref(12, "c_ref"); - for (int i = 0; i < 10; ++i) { + for (const auto i : c10::irange(10)) { a_v(i) = i * 100; b_v(i) = i; c_v(i) = 0; } - for (int i = 10; i < 12; ++i) { + for (const auto i : c10::irange(10, 12)) { b_v(i) = i; c_v(i) = 0; } @@ -1131,7 +1132,7 @@ TEST(Cuda, PrioritizeDependents_CUDA) { cudaMemcpy(c_v.data(), c_dev, 12 * sizeof(float), cudaMemcpyDeviceToHost); cudaDeviceSynchronize(); - for (int i = 0; i < 12; ++i) { + for (const auto i : c10::irange(12)) { if (i < 10) { c_ref(i) = i + i * 100; } else { @@ -1193,12 +1194,12 @@ TEST(Cuda, MaskBlockDim_CUDA) { PaddedBuffer c_ref(A_SIZE); PaddedBuffer d_ref(B_SIZE); - for (int i = 0; i < A_SIZE; i++) { + for (const auto i : c10::irange(A_SIZE)) { a_v(i) = (float)i; c_ref(i) = (float)(i + 10); } - for (int i = 0; i < B_SIZE; i++) { + for (const auto i : c10::irange(B_SIZE)) { b_v(i) = (float)(B_SIZE - i); d_ref(i) = a_v(i) + b_v(i); } @@ -1285,12 +1286,12 @@ TEST(Cuda, MaskThreadDim_CUDA) { PaddedBuffer c_ref(A_SIZE); PaddedBuffer d_ref(B_SIZE); - for (int i = 0; i < A_SIZE; i++) { + for (const auto i : c10::irange(A_SIZE)) { a_v(i) = (float)i; c_ref(i) = (float)(i + 10); } - for (int i = 0; i < B_SIZE; i++) { + for (const auto i : c10::irange(B_SIZE)) { b_v(i) = (float)(B_SIZE - i); d_ref(i) = a_v(i / 2) + b_v(i); } @@ -1378,12 +1379,12 @@ TEST(Cuda, MaskMultiBlockDim_CUDA) { PaddedBuffer c_ref(A_SIZE); PaddedBuffer d_ref(B_SIZE); - for (int i = 0; i < A_SIZE; i++) { + for (const auto i : c10::irange(A_SIZE)) { a_v(i) = (float)i; c_ref(i) = (float)(i + 10); } - for (int i = 0; i < B_SIZE; i++) { + for (const auto i : c10::irange(B_SIZE)) { b_v(i) = (float)(B_SIZE - i); d_ref(i) = a_v(i) + b_v(i); } @@ -1471,12 +1472,12 @@ TEST(Cuda, MaskBlockAndThreadDim_CUDA) { PaddedBuffer c_ref(A_SIZE); PaddedBuffer d_ref(B_SIZE); - for (int i = 0; i < A_SIZE; i++) { + for (const auto i : c10::irange(A_SIZE)) { a_v(i) = (float)i; c_ref(i) = (float)(i + 10); } - for (int i = 0; i < B_SIZE; i++) { + for (const auto i : c10::irange(B_SIZE)) { b_v(i) = (float)(B_SIZE - i); d_ref(i) = a_v(i) + b_v(i); } @@ -1572,15 +1573,15 @@ TEST(Cuda, MaskMultiDim_CUDA) { PaddedBuffer c_ref(OUTER_SIZE, A_SIZE); PaddedBuffer d_ref(OUTER_SIZE, B_SIZE); - for (int o = 0; o < OUTER_SIZE; ++o) { - for (int i = 0; i < A_SIZE; i++) { + for (const auto o : c10::irange(OUTER_SIZE)) { + for (const auto i : c10::irange(A_SIZE)) { a_v(o, i) = (float)i; c_ref(o, i) = (float)(i * 2); } } - for (int o = 0; o < OUTER_SIZE; ++o) { - for (int i = 0; i < B_SIZE; i++) { + for (const auto o : c10::irange(OUTER_SIZE)) { + for (const auto i : c10::irange(B_SIZE)) { b_v(o, i) = (float)(B_SIZE - i); d_ref(o, i) = c_ref(o, i * 2) + b_v(o, i); } @@ -1706,15 +1707,15 @@ TEST(Cuda, MaskMultiDimSymbolic_CUDA) { PaddedBuffer c_ref(OUTER_EXTENT, A_EXTENT); PaddedBuffer d_ref(OUTER_EXTENT, B_EXTENT); - for (int o = 0; o < OUTER_EXTENT; ++o) { - for (int i = 0; i < A_EXTENT; i++) { + for (const auto o : c10::irange(OUTER_EXTENT)) { + for (const auto i : c10::irange(A_EXTENT)) { a_v(o, i) = (float)i; c_ref(o, i) = (float)(i * 2); } } - for (int o = 0; o < OUTER_EXTENT; ++o) { - for (int i = 0; i < B_EXTENT; i++) { + for (const auto o : c10::irange(OUTER_EXTENT)) { + for (const auto i : c10::irange(B_EXTENT)) { b_v(o, i) = (float)(B_EXTENT - i); d_ref(o, i) = c_ref(o, i * 2) + b_v(o, i); } @@ -1847,12 +1848,12 @@ TEST(Cuda, MaskCompoundInnerLoop_CUDA) { PaddedBuffer c_ref(OUTER_SIZE, A_SIZE); PaddedBuffer d_ref(OUTER_SIZE, B_SIZE); - for (int o = 0; o < OUTER_SIZE; ++o) { - for (int i = 0; i < A_SIZE; i++) { + for (const auto o : c10::irange(OUTER_SIZE)) { + for (const auto i : c10::irange(A_SIZE)) { a_v(o, i) = (float)i; c_ref(o, i) = (float)(i * 2); } - for (int i = 0; i < B_SIZE; i++) { + for (const auto i : c10::irange(B_SIZE)) { b_v(o, i) = (float)(B_SIZE - i); d_ref(o, i) = c_ref(o, i * 2) + b_v(o, i); } @@ -1985,12 +1986,12 @@ TEST(Cuda, MaskInnerLoopOneBlock_CUDA) { PaddedBuffer c_ref(OUTER_SIZE, A_SIZE); PaddedBuffer d_ref(OUTER_SIZE, B_SIZE); - for (int o = 0; o < OUTER_SIZE; ++o) { - for (int i = 0; i < A_SIZE; i++) { + for (const auto o : c10::irange(OUTER_SIZE)) { + for (const auto i : c10::irange(A_SIZE)) { a_v(o, i) = (float)i; c_ref(o, i) = (float)(i * 2); } - for (int i = 0; i < B_SIZE; i++) { + for (const auto i : c10::irange(B_SIZE)) { b_v(o, i) = (float)(B_SIZE - i); d_ref(o, i) = c_ref(o, i * 2) + b_v(o, i); } @@ -2112,15 +2113,15 @@ TEST(Cuda, MaskMultiDimMultiAxis_CUDA) { PaddedBuffer c_ref(OUTER_SIZE, A_SIZE); PaddedBuffer d_ref(OUTER_SIZE, B_SIZE); - for (int o = 0; o < OUTER_SIZE; ++o) { - for (int i = 0; i < A_SIZE; i++) { + for (const auto o : c10::irange(OUTER_SIZE)) { + for (const auto i : c10::irange(A_SIZE)) { a_v(o, i) = (float)i; c_ref(o, i) = (float)(i * 2); } } - for (int o = 0; o < OUTER_SIZE; ++o) { - for (int i = 0; i < B_SIZE; i++) { + for (const auto o : c10::irange(OUTER_SIZE)) { + for (const auto i : c10::irange(B_SIZE)) { b_v(o, i) = (float)(B_SIZE - i); d_ref(o, i) = c_ref(o, i * 2) + b_v(o, i); } @@ -2243,15 +2244,15 @@ TEST(Cuda, MaskMultiDimMultiLevel_CUDA) { PaddedBuffer c_ref(OUTER_A_SIZE, A_SIZE); PaddedBuffer d_ref(OUTER_B_SIZE, B_SIZE); - for (int o = 0; o < OUTER_A_SIZE; ++o) { - for (int i = 0; i < A_SIZE; i++) { + for (const auto o : c10::irange(OUTER_A_SIZE)) { + for (const auto i : c10::irange(A_SIZE)) { a_v(o, i) = (float)i; c_ref(o, i) = (float)(i * 2); } } - for (int o = 0; o < OUTER_B_SIZE; ++o) { - for (int i = 0; i < B_SIZE; i++) { + for (const auto o : c10::irange(OUTER_B_SIZE)) { + for (const auto i : c10::irange(B_SIZE)) { b_v(o, i) = (float)(B_SIZE - i); d_ref(o, i) = c_ref(o, i * 2) + b_v(o, i); } diff --git a/test/cpp/tensorexpr/test_expr.cpp b/test/cpp/tensorexpr/test_expr.cpp index b7389786766e..520fca4f40d1 100644 --- a/test/cpp/tensorexpr/test_expr.cpp +++ b/test/cpp/tensorexpr/test_expr.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -163,7 +164,7 @@ TEST(Expr, VectorAdd01) { /* Build the following: - for (int index = 0; index < kVectorCount; index++) { + for (const auto index : c10::irange(kVectorCount)) { store(c_buf, ramp(index * 8, 1, 8), load(a_buf, ramp(index * 8, 1, 8) + load(b_buf, ramp(index * 8, 1, 8)))) @@ -187,7 +188,7 @@ TEST(Expr, VectorAdd01) { PaddedBuffer b_v(kTotalSize); PaddedBuffer c_v(kTotalSize); PaddedBuffer c_ref(kTotalSize); - for (int i = 0; i < kTotalSize; i++) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = i * i; b_v(i) = i * i * 4; c_ref(i) = a_v(i) + b_v(i); @@ -568,7 +569,7 @@ void testCond01() { SimpleIREvaluator(for_stmt, {a_buf})(a_v); PaddedBuffer a_ref(N); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { if (i % 2 == 0) { a_ref(i) = i * 2; } else { diff --git a/test/cpp/tensorexpr/test_kernel.cpp b/test/cpp/tensorexpr/test_kernel.cpp index abb211b3bf11..f05269f20867 100644 --- a/test/cpp/tensorexpr/test_kernel.cpp +++ b/test/cpp/tensorexpr/test_kernel.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -416,13 +417,13 @@ TEST_F(Kernel, DISABLED_Shape_Inference) { // Check sizes CHECK_EQ(o.sizes().size(), ref.sizes().size()); size_t num_el = 1; - for (size_t idx = 0; idx < ref.sizes().size(); idx++) { + for (const auto idx : c10::irange(ref.sizes().size())) { CHECK_EQ(o.sizes()[idx], ref.sizes()[idx]); num_el *= ref.sizes()[idx]; } // Check the contents - for (size_t i = 0; i < num_el; i++) { + for (const auto i : c10::irange(num_el)) { CHECK_EQ(((float*)o.data_ptr())[i], ((float*)ref.data_ptr())[i]); } } @@ -469,13 +470,13 @@ TEST_F(Kernel, DISABLED_Shape_Inference) { // Check sizes CHECK_EQ(o.sizes().size(), ref.sizes().size()); size_t num_el = 1; - for (size_t idx = 0; idx < ref.sizes().size(); idx++) { + for (const auto idx : c10::irange(ref.sizes().size())) { CHECK_EQ(o.sizes()[idx], ref.sizes()[idx]); num_el *= ref.sizes()[idx]; } // Check the contents - for (size_t i = 0; i < num_el; i++) { + for (const auto i : c10::irange(num_el)) { CHECK_EQ(((float*)o.data_ptr())[i], ((float*)ref.data_ptr())[i]); } } @@ -569,13 +570,13 @@ TEST_F(Kernel, CatInputTypesPromotion) { CHECK_EQ(o.sizes().size(), ref.sizes().size()); CHECK_EQ(o.dtype(), ref.dtype()); size_t num_el = 1; - for (size_t idx = 0; idx < ref.sizes().size(); idx++) { + for (const auto idx : c10::irange(ref.sizes().size())) { CHECK_EQ(o.sizes()[idx], ref.sizes()[idx]); num_el *= ref.sizes()[idx]; } // Check the contents - for (size_t i = 0; i < num_el; i++) { + for (const auto i : c10::irange(num_el)) { CHECK_EQ(((double*)o.data_ptr())[i], ((double*)ref.data_ptr())[i]); } } @@ -658,13 +659,13 @@ TEST_F(Kernel, CatWoConditionals) { CHECK_EQ(o.sizes().size(), ref.sizes().size()); CHECK_EQ(o.dtype(), ref.dtype()); size_t num_el = 1; - for (size_t idx = 0; idx < ref.sizes().size(); idx++) { + for (const auto idx : c10::irange(ref.sizes().size())) { CHECK_EQ(o.sizes()[idx], ref.sizes()[idx]); num_el *= ref.sizes()[idx]; } // Check the contents - for (size_t i = 0; i < num_el; i++) { + for (const auto i : c10::irange(num_el)) { CHECK_EQ(((float*)o.data_ptr())[i], ((float*)ref.data_ptr())[i]); } getCatWoConditionals() = false; @@ -723,13 +724,13 @@ TEST_F(Kernel, OptimizeConditionals) { CHECK_EQ(o.sizes().size(), ref.sizes().size()); CHECK_EQ(o.dtype(), ref.dtype()); size_t num_el = 1; - for (size_t idx = 0; idx < ref.sizes().size(); idx++) { + for (const auto idx : c10::irange(ref.sizes().size())) { CHECK_EQ(o.sizes()[idx], ref.sizes()[idx]); num_el *= ref.sizes()[idx]; } // Check the contents - for (size_t i = 0; i < num_el; i++) { + for (const auto i : c10::irange(num_el)) { CHECK_EQ(((float*)o.data_ptr())[i], ((float*)ref.data_ptr())[i]); } getOptConditionals() = old_opt_conditionals; @@ -904,7 +905,7 @@ TEST_F(Kernel, SumMultipleAxes) { // Only iterate over positive values of axes to keep the running time // reasonable, since the number of pairs is quadratic. - for (int dim1 = 0; dim1 < a.dim(); ++dim1) { + for (const auto dim1 : c10::irange(a.dim())) { for (int dim2 = dim1 + 1; dim2 < a.dim(); ++dim2) { for (bool keepdim : {false, true}) { TemplateEnv env; @@ -977,7 +978,7 @@ TEST_F(Kernel, Softmax2D) { # CHECK-NEXT: aten_softmax)IR"; for (auto log_softmax : {false, true}) { - for (int softmax_dim = 0; softmax_dim < a.dim(); ++softmax_dim) { + for (const auto softmax_dim : c10::irange(a.dim())) { auto softmax_dim_size = a.sizes()[softmax_dim]; auto other_dim = (softmax_dim + 1) % a.dim(); auto ref = @@ -1046,10 +1047,10 @@ TEST_F(Kernel, Softmax3D) { # CHECK-NEXT: aten_softmax)IR"; for (auto log_softmax : {false, true}) { - for (int softmax_dim = 0; softmax_dim < a.dim(); ++softmax_dim) { + for (const auto softmax_dim : c10::irange(a.dim())) { auto softmax_dim_size = a.sizes()[softmax_dim]; std::vector other_dims; - for (int i = 0; i < a.dim(); ++i) { + for (const auto i : c10::irange(a.dim())) { if (i != softmax_dim) { other_dims.push_back(i); } @@ -1127,10 +1128,10 @@ TEST_F(Kernel, Softmax4D) { # CHECK-NEXT: aten_softmax)IR"; for (auto log_softmax : {false, true}) { - for (int softmax_dim = 0; softmax_dim < a.dim(); ++softmax_dim) { + for (const auto softmax_dim : c10::irange(a.dim())) { auto softmax_dim_size = a.sizes()[softmax_dim]; std::vector other_dims; - for (int i = 0; i < a.dim(); ++i) { + for (const auto i : c10::irange(a.dim())) { if (i != softmax_dim) { other_dims.push_back(i); } diff --git a/test/cpp/tensorexpr/test_llvm.cpp b/test/cpp/tensorexpr/test_llvm.cpp index 61748b02a054..489c9c85e063 100644 --- a/test/cpp/tensorexpr/test_llvm.cpp +++ b/test/cpp/tensorexpr/test_llvm.cpp @@ -3,6 +3,7 @@ #include +#include #include #include #include @@ -242,14 +243,14 @@ TEST(LLVM, fastLogFloat) { PaddedBuffer a_v(kTotalSize); PaddedBuffer b_v(kTotalSize); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { a_v(i) = at::randn({1}).item().to(); } LLVMCodeGen ir_eval(stmt, {a_buf, b_buf}); ir_eval.call({a_v, b_v}); - for (int i = 0; i < kTotalSize; ++i) { + for (const auto i : c10::irange(kTotalSize)) { auto test = b_v(i); auto ref = std::log(a_v(i)); if (std::isnan(ref)) { @@ -516,7 +517,7 @@ TEST(LLVM, VecLoadStoreTest) { LLVMCodeGen cg(store, {a, b}); \ std::vector args({a_buffer.data(), b_buffer.data()}); \ ASSERT_EQ(cg.value(args), 0); \ - for (int i = 0; i < Lanes; i++) { \ + for (const auto i : c10::irange(Lanes)) { \ ASSERT_FLOAT_EQ(a_buffer[i], val); \ } \ } // namespace jit @@ -554,7 +555,7 @@ FLOAT_INTRINSICS_TEST(lgamma, 8) LLVMCodeGen cg(store, {a, b}); \ std::vector args({a_buffer.data(), b_buffer.data()}); \ ASSERT_EQ(cg.value(args), 0); \ - for (int i = 0; i < Lanes; i++) { \ + for (const auto i : c10::irange(Lanes)) { \ ASSERT_FLOAT_EQ(a_buffer[i], val); \ } \ } // namespace jit @@ -619,7 +620,7 @@ TEST(LLVM, VectorizeBitCast) { std::vector a_vec(128); std::vector c_vec(128); - for (auto i = 0; i < 128; ++i) { + for (const auto i : c10::irange(128)) { a_vec[i] = raw_bitcast(1337.f); } std::vector args({a_vec.data(), c_vec.data()}); @@ -985,7 +986,7 @@ TEST(LLVM, CompareSelectIntEQ) { ASSERT_EQ(c_buffer.size(), N); assertAllEqual(a_buffer, 1); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { ASSERT_EQ(c_ref[i], c_buffer[i]); } } @@ -1058,7 +1059,7 @@ TEST(LLVM, CompareSelectByteGT) { ASSERT_EQ(c_buffer.size(), N); assertAllEqual(b_buffer, uint8_t(0)); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { ASSERT_EQ(c_ref[i], c_buffer[i]); } } @@ -1093,7 +1094,7 @@ TEST(LLVM, CompareSelectByteGE) { ASSERT_EQ(c_buffer.size(), N); assertAllEqual(b_buffer, uint8_t(0)); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { ASSERT_EQ(c_ref[i], c_buffer[i]); } } @@ -1133,7 +1134,7 @@ TEST(LLVM, CompareSelectByteLT) { ASSERT_EQ(c_buffer.size(), N); assertAllEqual(b_buffer, uint8_t(128)); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { ASSERT_EQ(c_ref[i], c_buffer[i]); } } @@ -1168,7 +1169,7 @@ TEST(LLVM, CompareSelectByteLE) { ASSERT_EQ(c_buffer.size(), N); assertAllEqual(b_buffer, uint8_t(128)); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { ASSERT_EQ(c_ref[i], c_buffer[i]); } } @@ -1198,7 +1199,7 @@ TEST(LLVM, SimpleMath01) { int value = cg.value(args); ASSERT_EQ(value, 0); PaddedBuffer f_ref(N, "f_ref"); - for (int i = 0; i < N; i++) { + for (const auto i : c10::irange(N)) { f_ref(i) = i * i + 1; } ExpectAllNear(f_v, f_ref, 1e-5); @@ -1251,8 +1252,8 @@ TEST(LLVM, BroadcastAdd) { std::vector args({av.data(), bv.data(), cv.data()}); ASSERT_EQ(cg.value(args), 0); - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { ASSERT_EQ(cv[i * N + j], av[i * N + j] + bv[j]); } } @@ -1421,8 +1422,8 @@ TEST(LLVM, SimpleReduction) { PaddedBuffer b_ref(1, "b_ref"); b_ref(0) = 0; - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { int v = i + j; a_v(0, i, j) = v; b_ref(0) += v; @@ -1469,8 +1470,8 @@ TEST(LLVM, RFactorReduction) { PaddedBuffer b_ref(1, "b_ref"); b_ref(0) = 0; - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { int v = i + j; a_v(0, i, j) = v; b_ref(0) += v; @@ -1516,8 +1517,8 @@ TEST(LLVM, RFactorVectorizedReduction) { PaddedBuffer b_ref(1, "b_ref"); b_ref(0) = 0; - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { int v = i + j; a_v(0, i, j) = v; b_ref(0) += v; @@ -1558,8 +1559,8 @@ static void testSimpleParallel() { int value = cg.value(args); ASSERT_EQ(value, 0); PaddedBuffer f_ref(M, N, "f_ref"); - for (int m = 0; m < M; m++) { - for (int n = 0; n < N; n++) { + for (const auto m : c10::irange(M)) { + for (const auto n : c10::irange(N)) { f_ref(m, n) = m + n; } } @@ -1584,7 +1585,7 @@ TEST(LLVM, CompositeParallel) { int test_count = 1 << loop_count; // Compute a composite operation, and try all loop-axis combination to be // parallel or sequential. - for (int test_cfg = 0; test_cfg < test_count; test_cfg++) { + for (const auto test_cfg : c10::irange(test_count)) { int M = 5; int N = 7; Tensor t1 = @@ -1624,7 +1625,7 @@ TEST(LLVM, CompositeParallel) { loop_list.push_back(loops[1]); } ASSERT_EQ(loop_list.size(), loop_count); - for (int i = 0; i < loop_count; i++) { + for (const auto i : c10::irange(loop_count)) { if (test_cfg & (1 << i)) { loop_list[i]->set_parallel(); } @@ -1638,8 +1639,8 @@ TEST(LLVM, CompositeParallel) { int value = cg.value(args); ASSERT_EQ(value, 0); PaddedBuffer t4_ref(M, N, "t4_ref"); - for (int m = 0; m < M; m++) { - for (int n = 0; n < N; n++) { + for (const auto m : c10::irange(M)) { + for (const auto n : c10::irange(N)) { t4_ref(m, n) = (m + 1) * (n + 2) + m + n; } } @@ -1715,10 +1716,10 @@ TEST(LLVM, VectorizedGEMM) { PaddedBuffer c_v(M, N, "c_v"); PaddedBuffer c_ref(M, N, "c_ref"); - for (int m = 0; m < M; m++) { - for (int n = 0; n < N; n++) { + for (const auto m : c10::irange(M)) { + for (const auto n : c10::irange(N)) { c_ref(m, n) = 0.f; - for (int k = 0; k < K; k++) { + for (const auto k : c10::irange(K)) { c_ref(m, n) += a_v(m, k) * b_v(k, n); } } @@ -1754,8 +1755,8 @@ TEST(LLVM, CallRaw) { LLVMCodeGen cg(s, {a, b, BufHandle(c.buf()), N}); cg.call_raw(args); - for (int i = 0; i < M; i++) { - for (int j = 0; j < N_value; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N_value)) { ASSERT_EQ(cv[i * N_value + j], av[i * N_value + j] + bv[j]); } } @@ -1763,8 +1764,8 @@ TEST(LLVM, CallRaw) { SimpleIREvaluator eval(s, {a, b, BufHandle(c.buf()), N}); eval.call_raw(args); - for (int i = 0; i < M; i++) { - for (int j = 0; j < N_value; j++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N_value)) { ASSERT_EQ(cv[i * N_value + j], av[i * N_value + j] + bv[j]); } } diff --git a/test/cpp/tensorexpr/test_reductions.cpp b/test/cpp/tensorexpr/test_reductions.cpp index d86f0463f112..27f1dcc9ab2d 100644 --- a/test/cpp/tensorexpr/test_reductions.cpp +++ b/test/cpp/tensorexpr/test_reductions.cpp @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -28,7 +29,7 @@ TEST(Reductions, ReduceSum0D_1) { BufHandle b("b", {M}, kFloat); std::vector in(M); - for (int j = 0; j < M; ++j) { + for (const auto j : c10::irange(M)) { in[j] = j; } @@ -43,7 +44,7 @@ TEST(Reductions, ReduceSum0D_1) { SimpleIREvaluator cg(s, {b, c}); cg.call({in, out}); - for (int i = 0; i < M; ++i) { + for (const auto i : c10::irange(M)) { ASSERT_EQ(out[i], in[i]); } } @@ -73,7 +74,7 @@ TEST(Reductions, ReduceSum0D_2) { TEST(Reductions, ReduceSum1D) { BufHandle b("b", {10}, kFloat); std::vector in(10); - for (int j = 0; j < 10; ++j) { + for (const auto j : c10::irange(10)) { in[j] = j; } @@ -100,8 +101,8 @@ TEST(Reductions, ReduceSum2D) { BufHandle b("b", {m, n}, kFloat); std::vector in(M * N); - for (int i = 0; i < M; ++i) { - for (int j = 0; j < N; ++j) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { in[i * N + j] = j; } } @@ -119,12 +120,12 @@ TEST(Reductions, ReduceSum2D) { cg.call({in, out, 5, 7}); float expected = 0; - for (int i = 0; i < N; ++i) { + for (const auto i : c10::irange(N)) { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) expected += i; } - for (int i = 0; i < M; ++i) { + for (const auto i : c10::irange(M)) { ASSERT_EQ(out[i], expected); } } @@ -151,14 +152,14 @@ TEST(Reductions, ReduceSum3D) { std::vector eData(2, 1.0f); for (int i = 0; i < 2 * 3; ++i) { - for (int j = 0; j < M; ++j) { + for (const auto j : c10::irange(M)) { bData[i * M + j] = j; } } cg.call({bData, cData, M}); float expected = 0; - for (int i = 0; i < M; ++i) { + for (const auto i : c10::irange(M)) { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) expected += i; } @@ -179,7 +180,7 @@ TEST(Reductions, ReduceSum3D) { // We're combining an additional dimension of 3, so the sum is 3x. expected = expected * 3; - for (int i = 0; i < 2; ++i) { + for (const auto i : c10::irange(2)) { ASSERT_EQ(dData[i], expected); } @@ -194,7 +195,7 @@ TEST(Reductions, ReduceSum3D) { SimpleIREvaluator cg3(s3, {c, e}); cg3.call({cData, eData}); - for (int i = 0; i < 2; ++i) { + for (const auto i : c10::irange(2)) { ASSERT_EQ(eData[i], expected); } } @@ -226,7 +227,7 @@ TEST(Reductions, ReduceSum10D) { // NOLINTNEXTLINE(bugprone-integer-division) float expected = InputSize / OutputSize; - for (int i = 0; i < OutputSize; ++i) { + for (const auto i : c10::irange(OutputSize)) { ASSERT_EQ(out[i], expected); } } @@ -238,8 +239,8 @@ TEST(Reductions, ReduceProduct) { BufHandle b("b", {M, N}, kFloat); std::vector in(M * N); - for (int i = 0; i < M; ++i) { - for (int j = 0; j < N; ++j) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { in[i * N + j] = 2 + j; } } @@ -260,12 +261,12 @@ TEST(Reductions, ReduceProduct) { cg.call({in, out}); float expected = 1; - for (int i = 0; i < N; ++i) { + for (const auto i : c10::irange(N)) { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) expected *= 2 + i; } - for (int i = 0; i < M; ++i) { + for (const auto i : c10::irange(M)) { ASSERT_EQ(out[i], expected); } } @@ -276,7 +277,7 @@ TEST(Reductions, ReduceMax) { std::vector in(10); std::vector out(1, -1.f); - for (int j = 0; j < 10; ++j) { + for (const auto j : c10::irange(10)) { in[j] = j; } @@ -316,7 +317,7 @@ TEST(Reductions, ReduceMinCustomInitializer) { std::vector in(10); std::vector out(1, -1.f); - for (int j = 0; j < 10; ++j) { + for (const auto j : c10::irange(10)) { in[j] = 10 + j; } @@ -374,7 +375,7 @@ TEST(Reductions, ReduceAnyAll) { std::vector out(4, 0); // input has 0-39 in 4 rows. - for (int i = 0; i < 40; ++i) { + for (const auto i : c10::irange(40)) { in[i] = i; } cg.call({in, out, 1}); @@ -438,8 +439,8 @@ TEST(Reductions, ReduceMatmul2D) { std::vector tB_(6); std::vector out(9, -1.f); - for (int i = 0; i < 3; ++i) { - for (int j = 0; j < 2; ++j) { + for (const auto i : c10::irange(3)) { + for (const auto j : c10::irange(2)) { tA_[i * 2 + j] = i * 2 + j; tB_[j * 3 + i] = i * 2 + j; } @@ -465,7 +466,7 @@ TEST(Reductions, ReduceMatmul2D) { std::vector expected( {1.f, 3.f, 5.f, 3.f, 13.f, 23.f, 5.f, 23.f, 41.f}); - for (int i = 0; i < 9; ++i) { + for (const auto i : c10::irange(9)) { ASSERT_EQ(out[i], expected[i]); } } @@ -473,7 +474,7 @@ TEST(Reductions, ReduceMatmul2D) { TEST(Reductions, ReduceRfactorLike) { BufHandle in("in", {10, 10}, kFloat); std::vector in_(100); - for (int i = 0; i < 100; ++i) { + for (const auto i : c10::irange(100)) { in_[i] = i; } std::vector in_rf_(10, -2.f); @@ -522,14 +523,14 @@ TEST(Reductions, ReduceAsProducer) { for (int i = 0; i < 2 * 3; ++i) { aData[i] = 6 - i; - for (int j = 0; j < M; ++j) { + for (const auto j : c10::irange(M)) { bData[i * M + j] = j; } } cg.call({aData, bData, dData, M}); float expected = 0; - for (int i = 0; i < M; ++i) { + for (const auto i : c10::irange(M)) { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) expected += i; } @@ -564,7 +565,7 @@ TEST(Reductions, ReduceAsConsumer) { std::vector dData(2, 6.0f); for (int i = 0; i < 2 * 3; ++i) { - for (int j = 0; j < M; ++j) { + for (const auto j : c10::irange(M)) { bData[i * M + j] = j + 1; aData[i * M + j] = 6 - i; } @@ -573,16 +574,16 @@ TEST(Reductions, ReduceAsConsumer) { cg.call({aData, bData, dData, M}); // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) float expected[2] = {0, 0}; - for (int i = 0; i < 2; ++i) { - for (int j = 0; j < 3; ++j) { - for (int k = 0; k < M; ++k) { + for (const auto i : c10::irange(2)) { + for (const auto j : c10::irange(3)) { + for (const auto k : c10::irange(M)) { // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) expected[i] += (k + 1) * (6 - (i * 3 + j)); } } } - for (int i = 0; i < 2; ++i) { + for (const auto i : c10::irange(2)) { ASSERT_EQ(dData[i], expected[i]); } } @@ -591,8 +592,8 @@ TEST(Reductions, SplitReduceAxis) { BufHandle in("in", {16, 8}, kFloat); std::vector in_(16 * 8); - for (int i = 0; i < 16; ++i) { - for (int j = 0; j < 8; ++j) { + for (const auto i : c10::irange(16)) { + for (const auto j : c10::irange(8)) { in_[i * 8 + j] = i; } } @@ -611,7 +612,7 @@ TEST(Reductions, SplitReduceAxis) { SimpleIREvaluator cg(s, {in, tensor}); cg.call({in_, out}); - for (int i = 0; i < 16; ++i) { + for (const auto i : c10::irange(16)) { ASSERT_EQ(out[i], i * 8); } } @@ -620,8 +621,8 @@ TEST(Reductions, SplitNonReduceAxis) { BufHandle in("in", {16, 8}, kFloat); std::vector in_(16 * 8); - for (int i = 0; i < 16; ++i) { - for (int j = 0; j < 8; ++j) { + for (const auto i : c10::irange(16)) { + for (const auto j : c10::irange(8)) { in_[i * 8 + j] = i; } } @@ -640,7 +641,7 @@ TEST(Reductions, SplitNonReduceAxis) { SimpleIREvaluator cg(s, {in, tensor}); cg.call({in_, out}); - for (int i = 0; i < 16; ++i) { + for (const auto i : c10::irange(16)) { ASSERT_EQ(out[i], i * 8); } } @@ -689,7 +690,7 @@ TEST(Reductions, ReorderedReductionInitializer) { SimpleIREvaluator cg2(s, {in, tensor}); cg2.call({in_, out2}); - for (int i = 0; i < 16; ++i) { + for (const auto i : c10::irange(16)) { ASSERT_EQ(out1[i], out2[i]); } } @@ -807,7 +808,7 @@ TEST(Reductions, ReduceRepeatedInternalRfactor) { LoopNest orig_loop({c}); // Try rfactoring N outer loops - for (int rfac_number = 1; rfac_number < 5; rfac_number++) { + for (const auto rfac_number : c10::irange(1, 5)) { LoopNest refloop(orig_loop); LoopNest loop(orig_loop); refloop.prepareForCodegen(); @@ -817,7 +818,7 @@ TEST(Reductions, ReduceRepeatedInternalRfactor) { BufPtr tmp_buf = c.buf(); - for (int idx = 0; idx < rfac_number; idx++) { + for (const auto idx : c10::irange(rfac_number)) { auto reduce = loop.getAllWritesToBuf(tmp_buf)[1]; ASSERT_TRUE(loop.rfactor( reduce, loop.getLoopStmtsFor(tmp_buf).at(idx), &tmp_buf)); @@ -846,7 +847,7 @@ TEST(Reductions, ReduceSplitTail) { in[j] = j; } - for (int i = 0; i < 3; ++i) { + for (const auto i : c10::irange(3)) { std::vector out(M, -1.f); Tensor c = Reduce("sum", {{M, "m"}}, Sum(), b, {{N, "n"}, {K, "k"}}); @@ -876,7 +877,7 @@ TEST(Reductions, ReduceSplitNoTail) { in[j] = j; } - for (int i = 0; i < 3; ++i) { + for (const auto i : c10::irange(3)) { std::vector out(M, -1.f); Tensor c = Reduce("sum", {{M, "m"}}, Sum(), b, {{N, "n"}, {K, "k"}}); @@ -908,7 +909,7 @@ TEST(Reductions, ReduceOverSplitTail) { in[j] = j; } - for (int i = 0; i < 3; ++i) { + for (const auto i : c10::irange(3)) { std::vector out(M, -1.f); Tensor c = Reduce("sum", {{M, "m"}}, Sum(), b, {{N, "n"}, {K, "k"}}); @@ -939,7 +940,7 @@ TEST(Reductions, ReduceSplitMask) { in[j] = j; } - for (int i = 0; i < 3; ++i) { + for (const auto i : c10::irange(3)) { std::vector out(M, -1.f); Tensor c = Reduce("sum", {{M, "m"}}, Sum(), b, {{N, "n"}, {K, "k"}}); @@ -969,7 +970,7 @@ TEST(Reductions, ReduceSplitNoMask) { in[j] = j; } - for (int i = 0; i < 3; ++i) { + for (const auto i : c10::irange(3)) { std::vector out(M, -1.f); Tensor c = Reduce("sum", {{M, "m"}}, Sum(), b, {{N, "n"}, {K, "k"}}); @@ -1000,7 +1001,7 @@ TEST(Reductions, ReduceOverSplitMask) { in[j] = j; } - for (int i = 0; i < 3; ++i) { + for (const auto i : c10::irange(3)) { std::vector out(M, -1.f); Tensor c = Reduce("sum", {{M, "m"}}, Sum(), b, {{N, "n"}, {K, "k"}}); @@ -1029,7 +1030,7 @@ TEST(Reductions, ReduceSplitRfactor) { BufHandle b("b", {M, N, K}, kFloat); std::vector in(M * N * K); - for (int m = 0; m < M; ++m) { + for (const auto m : c10::irange(M)) { for (int j = 0; j < N * K; ++j) { in[m * N * K + j] = j; } @@ -1056,7 +1057,8 @@ TEST(Reductions, ReduceSplitRfactor) { SimpleIREvaluator cg(s, {b, c}); cg.call({in, out}); - for (int i = 0; i < M; ++i) { + for (const auto i : c10::irange(M)) { + (void)i; // Suppress unused variable warning ASSERT_EQ(out[0], 4950); } } @@ -1134,12 +1136,12 @@ TEST(Reductions, ReduceInlineReduction) { PaddedBuffer a_v(M); PaddedBuffer b_v(M, N, K); - for (int i = 0; i < M; i++) { + for (const auto i : c10::irange(M)) { a_v(i) = i * i; } - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { - for (int k = 0; k < K; k++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { + for (const auto k : c10::irange(K)) { b_v(i, j, k) = j * j * k; } } @@ -1169,9 +1171,9 @@ TEST(Reductions, ReduceInlineConsumer) { PaddedBuffer a_v(M, N, K); PaddedBuffer b_v(M, N, K); - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { - for (int k = 0; k < K; k++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { + for (const auto k : c10::irange(K)) { a_v(i, j, k) = i * i + k; b_v(i, j, k) = j * j + k; } @@ -1226,9 +1228,9 @@ TEST(Reductions, ReduceInlineReducerInternal) { PaddedBuffer a_v(M, N, K); PaddedBuffer b_v(M, N, K); - for (int i = 0; i < M; i++) { - for (int j = 0; j < N; j++) { - for (int k = 0; k < K; k++) { + for (const auto i : c10::irange(M)) { + for (const auto j : c10::irange(N)) { + for (const auto k : c10::irange(K)) { a_v(i, j, k) = i * i + k; b_v(i, j, k) = j * j + k; } @@ -1319,9 +1321,9 @@ TEST(Reductions, ReductionCacheAccessesOperatorAxis) { PaddedBuffer e_before(L, "e_before"); PaddedBuffer e_after(L, "e_after"); - for (int l = 0; l < L; l++) { - for (int m = 0; m < M; m++) { - for (int n = 0; n < N; n++) { + for (const auto l : c10::irange(L)) { + for (const auto m : c10::irange(M)) { + for (const auto n : c10::irange(N)) { a_v(l, m, n) = at::randn({1}).item().to(); b_v(l, m, n) = at::randn({1}).item().to(); } @@ -1392,9 +1394,9 @@ TEST(Reductions, ReductionCacheAccessesOuterReduceAxis) { PaddedBuffer e_before(L, "e_before"); PaddedBuffer e_after(L, "e_after"); - for (int l = 0; l < L; l++) { - for (int m = 0; m < M; m++) { - for (int n = 0; n < N; n++) { + for (const auto l : c10::irange(L)) { + for (const auto m : c10::irange(M)) { + for (const auto n : c10::irange(N)) { a_v(l, m, n) = at::randn({1}).item().to(); b_v(l, m, n) = at::randn({1}).item().to(); } @@ -1465,9 +1467,9 @@ TEST(Reductions, ReductionCacheAccessesInnerReduceAxis) { PaddedBuffer e_before(L, "e_before"); PaddedBuffer e_after(L, "e_after"); - for (int l = 0; l < L; l++) { - for (int m = 0; m < M; m++) { - for (int n = 0; n < N; n++) { + for (const auto l : c10::irange(L)) { + for (const auto m : c10::irange(M)) { + for (const auto n : c10::irange(N)) { a_v(l, m, n) = at::randn({1}).item().to(); b_v(l, m, n) = at::randn({1}).item().to(); } @@ -1782,8 +1784,8 @@ TEST(Reductions, ReductionRfactorCacheTempInner) { TEST(Reductions, ReductionVectorize) { std::vector in_(8 * 8); - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + for (const auto i : c10::irange(8)) { + for (const auto j : c10::irange(8)) { in_[i * 8 + j] = i; } } @@ -1820,7 +1822,7 @@ TEST(Reductions, ReductionVectorize) { s = IRSimplifier::simplify(l.root_stmt()); SimpleIREvaluator cg_after(s, {in, tensor}); cg_after.call({in_, out_after}); - for (int i = 0; i < 8; ++i) { + for (const auto i : c10::irange(8)) { ASSERT_EQ(out_before[i], out_after[i]); } } @@ -1836,8 +1838,8 @@ TEST(Reductions, ReductionVectorizeInner) { TEST(Reductions, ReductionVectorizeRfactor) { std::vector in_(8 * 8); - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + for (const auto i : c10::irange(8)) { + for (const auto j : c10::irange(8)) { in_[i * 8 + j] = i; } } diff --git a/test/cpp/tensorexpr/test_simplify.cpp b/test/cpp/tensorexpr/test_simplify.cpp index f71f907c2732..21f85ce160af 100644 --- a/test/cpp/tensorexpr/test_simplify.cpp +++ b/test/cpp/tensorexpr/test_simplify.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -1132,7 +1133,7 @@ TEST(Simplify, SimplifyDivWithLoopContext0) { TEST(Simplify, SimplifyDivWithLoopContext1) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { + // for (const auto i : c10::irange(6)) { // A[i] = (i + 24) / 6; //} VarHandle i("i", kInt); @@ -1153,7 +1154,7 @@ TEST(Simplify, SimplifyDivWithLoopContext1) { TEST(Simplify, SimplifyDivWithLoopContext2) { // Stmt to simplify: - // for (int i = 0; i < 5; i++) { + // for (const auto i : c10::irange(5)) { // A[i] = (i + 25) / 6; //} VarHandle i("i", kInt); @@ -1174,7 +1175,7 @@ TEST(Simplify, SimplifyDivWithLoopContext2) { TEST(Simplify, SimplifyDivWithLoopContext3) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { + // for (const auto i : c10::irange(6)) { // A[i] = (i + 24) / (-6); //} VarHandle i("i", kInt); @@ -1195,7 +1196,7 @@ TEST(Simplify, SimplifyDivWithLoopContext3) { TEST(Simplify, SimplifyDivWithLoopContext4) { // Stmt to simplify: - // for (int i = 0; i < 5; i++) { + // for (const auto i : c10::irange(5)) { // A[i] = (i - 5) / 6; //} VarHandle i("i", kInt); @@ -1216,8 +1217,8 @@ TEST(Simplify, SimplifyDivWithLoopContext4) { TEST(Simplify, SimplifyDivWithLoopContext5) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { - // for (int j = 0; j < 10; j++) { + // for (const auto i : c10::irange(6)) { + // for (const auto j : c10::irange(10)) { // A[i, j] = (i + 6*j) / 6; // } //} @@ -1242,7 +1243,7 @@ TEST(Simplify, SimplifyDivWithLoopContext5) { TEST(Simplify, SimplifyDivWithLoopContext6) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { + // for (const auto i : c10::irange(6)) { // for (int j = -1; j < 9; j++) { // A[i, j+1] = (i + 6*j) / 6; // } @@ -1269,8 +1270,8 @@ TEST(Simplify, SimplifyDivWithLoopContext6) { TEST(Simplify, SimplifyDivWithLoopContext7) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { - // for (int j = 0; j < 10; j++) { + // for (const auto i : c10::irange(6)) { + // for (const auto j : c10::irange(10)) { // A[i, j] = (i + 6*j) / (-6); // } //} @@ -1296,7 +1297,7 @@ TEST(Simplify, SimplifyDivWithLoopContext7) { TEST(Simplify, SimplifyModWithLoopContext0) { // Stmt to simplify: - // for (int i = 0; i < 100; i++) { + // for (const auto i : c10::irange(100)) { // A[i] = i % 100; //} VarHandle i("i", kInt); @@ -1317,7 +1318,7 @@ TEST(Simplify, SimplifyModWithLoopContext0) { TEST(Simplify, SimplifyModWithLoopContext1) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { + // for (const auto i : c10::irange(6)) { // A[i] = (i + 24) % 6; //} VarHandle i("i", kInt); @@ -1338,7 +1339,7 @@ TEST(Simplify, SimplifyModWithLoopContext1) { TEST(Simplify, SimplifyModWithLoopContext2) { // Stmt to simplify: - // for (int i = 0; i < 5; i++) { + // for (const auto i : c10::irange(5)) { // A[i] = (i + 25) % 6; //} VarHandle i("i", kInt); @@ -1359,7 +1360,7 @@ TEST(Simplify, SimplifyModWithLoopContext2) { TEST(Simplify, SimplifyModWithLoopContext3) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { + // for (const auto i : c10::irange(6)) { // A[i] = (i + 24) % (-6); //} VarHandle i("i", kInt); @@ -1380,7 +1381,7 @@ TEST(Simplify, SimplifyModWithLoopContext3) { TEST(Simplify, SimplifyModWithLoopContext4) { // Stmt to simplify: - // for (int i = 0; i < 5; i++) { + // for (const auto i : c10::irange(5)) { // A[i] = (i - 5) % 6; //} VarHandle i("i", kInt); @@ -1401,8 +1402,8 @@ TEST(Simplify, SimplifyModWithLoopContext4) { TEST(Simplify, SimplifyModWithLoopContext5) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { - // for (int j = 0; j < 10; j++) { + // for (const auto i : c10::irange(6)) { + // for (const auto j : c10::irange(10)) { // A[i, j] = (i + 6*j) % 6; // } //} @@ -1427,7 +1428,7 @@ TEST(Simplify, SimplifyModWithLoopContext5) { TEST(Simplify, SimplifyModWithLoopContext6) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { + // for (const auto i : c10::irange(6)) { // for (int j = -1; j < 9; j++) { // A[i, j+1] = (i + 6*j) % 6; // } @@ -1454,8 +1455,8 @@ TEST(Simplify, SimplifyModWithLoopContext6) { TEST(Simplify, SimplifyModWithLoopContext7) { // Stmt to simplify: - // for (int i = 0; i < 6; i++) { - // for (int j = 0; j < 10; j++) { + // for (const auto i : c10::irange(6)) { + // for (const auto j : c10::irange(10)) { // A[i, j] = (i + 6*j) % (-6); // } //} @@ -3884,7 +3885,8 @@ TEST(Simplify, SimplifyEliminateEmptyFor) { { // Flatten many layers around an empty block to an empty block. StmtPtr last = alloc(std::vector({})); - for (int i = 0; i < 11; ++i) { + for (const auto i : c10::irange(11)) { + (void)i; // Suppress unused variable warning VarHandle loopVar("loopVar", kInt); last = For::make(loopVar, 0, 10, last); } @@ -3968,7 +3970,8 @@ TEST(Simplify, SimplifyFlattenBlock) { { // Flatten many layers around an empty block to an empty block. StmtPtr last = alloc(std::vector({})); - for (int i = 0; i < 11; ++i) { + for (const auto i : c10::irange(11)) { + (void)i; // Suppress unused variable warning last = alloc(std::vector({last})); } @@ -4817,18 +4820,18 @@ TEST(Simplify, SimplifyBroadcastTermExpander) { SimpleIREvaluator eval(store, {buf}); std::vector output(num_lanes); eval(output); - for (int i = 0; i < num_lanes; ++i) { + for (const auto i : c10::irange(num_lanes)) { ASSERT_EQ(output[i], 2); } } TEST(Simplify, DISABLED_CompareSelectCondAlwaysInLoopBounds) { // Before: - // for (int n = 1; n < N; n++) { + // for (const auto n : c10::irange(1, N)) { // b[n] = n < 1 ? 0.f : 1.f; // } // After: - // for (int n = 1; n < N; n++) { + // for (const auto n : c10::irange(1, N)) { // b[n] = 1.f; // } constexpr int N = 8; @@ -4848,11 +4851,11 @@ TEST(Simplify, DISABLED_CompareSelectCondAlwaysInLoopBounds) { TEST(Simplify, DISABLED_IfThenCondAlwaysInLoopBounds) { // Before: - // for (int n = 1; n < N; n++) { + // for (const auto n : c10::irange(1, N)) { // b[n] = IfThenElse(n < 1 ? 1 : 0, 0.f, 1.f); // } // After: - // for (int n = 1; n < N; n++) { + // for (const auto n : c10::irange(1, N)) { // b[n] = 1.f; // } constexpr int N = 8; @@ -4875,13 +4878,13 @@ TEST(Simplify, DISABLED_MultiClauseCondAlwaysInLoopBounds) { // conditional that is provably satisfied (or unsatisfied) by the entire loop // range. // Before: - // for (int i = 1; i < 7; i++) { - // for (int j = 1; j < 7; j++) { + // for (const auto i : c10::irange(1, 7)) { + // for (const auto j : c10::irange(1, 7)) { // b[i, j] = IfThenElse( // j>=7 ? 1 : (i>=7 ? 1 : (j<1 ? 1 : (i<1 ? 1 : 0))), 0.f, 1.f); // After: - // for (int i = 1; i < 7; i++) { - // for (int j = 1; j < 7; j++) { + // for (const auto i : c10::irange(1, 7)) { + // for (const auto j : c10::irange(1, 7)) { // b[i, j] = 1.f; constexpr int N = 8; BufHandle b("b", {N, N}, kFloat); @@ -4910,13 +4913,13 @@ TEST(Simplify, DISABLED_SimplifyLoopBounds) { // could be solved by peeling, and applying the range-based conditional // simplification in the previous tests. // Before: - // for (int i = 0; i < 3; i++) { - // for (int j = 0; j < 3; j++) { + // for (const auto i : c10::irange(3)) { + // for (const auto j : c10::irange(3)) { // b[i, j] = (b[i, j]) + (IfThenElse( // j>=7 ? 1 : (i>=7 ? 1 : (j<1 ? 1 : (i<1 ? 1 : 0))), 0.f, a[i, j])); // After: - // for (int i = 1; i < 3; i++) { - // for (int j = 1; j < 3; j++) { + // for (const auto i : c10::irange(1, 3)) { + // for (const auto j : c10::irange(1, 3)) { // b[i, j] = (b[i, j]) + 1.f; constexpr int N = 8; constexpr int K = 3; @@ -4937,8 +4940,8 @@ TEST(Simplify, DISABLED_SimplifyLoopBounds) { oss << *s; torch::jit::testing::FileCheck().run( R"IR( -# CHECK: for (int i = 1; i < 3; i++) { -# CHECK: for (int j = 1; j < 3; j++) { +# CHECK: for (const auto i : c10::irange(1, 3)) { +# CHECK: for (const auto j : c10::irange(1, 3)) { # CHECK-NOT: IfThenElse )IR", oss.str()); diff --git a/test/cpp/tensorexpr/tutorial.cpp b/test/cpp/tensorexpr/tutorial.cpp index 0ec0968bebf8..b89fcc3396df 100644 --- a/test/cpp/tensorexpr/tutorial.cpp +++ b/test/cpp/tensorexpr/tutorial.cpp @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -146,8 +147,8 @@ int main(int argc, char* argv[]) { std::cout << "Nested for loops: " << std::endl << *loop_i_a << std::endl; // Prints: // Nested for loops: - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // A[i, j] = i + j; // } // } @@ -166,13 +167,13 @@ int main(int argc, char* argv[]) { // Prints: // Compound Block statement: // { - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // A[i, j] = i + j; // } // } - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // B[i, j] = A[i, j]; // } // } @@ -193,8 +194,8 @@ int main(int argc, char* argv[]) { << *C.stmt() << std::endl; // Prints: // Stmt produced by 'Compute' API: - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // C[i, j] = i * j; // } // } @@ -239,13 +240,13 @@ int main(int argc, char* argv[]) { // Prints: // Stmt produced by 'Compute' API: // { - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // C[i, j] = i * (j + 1); // } // } - // for (int i_1 = 0; i_1 < 64; i_1++) { - // for (int j_1 = 0; j_1 < 32; j_1++) { + // for (const auto i_1 : c10::irange(64)) { + // for (const auto j_1 : c10::irange(32)) { // D[i_1, j_1] = (C[i_1, j_1]) - i_1; // } // } @@ -265,13 +266,13 @@ int main(int argc, char* argv[]) { // Prints: // LoopNest root stmt: // { - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // C[i, j] = i * (j + 1); // } // } - // for (int i_1 = 0; i_1 < 64; i_1++) { - // for (int j_1 = 0; j_1 < 32; j_1++) { + // for (const auto i_1 : c10::irange(64)) { + // for (const auto j_1 : c10::irange(32)) { // D[i_1, j_1] = (C[i_1, j_1]) - i_1; // } // } @@ -284,8 +285,8 @@ int main(int argc, char* argv[]) { // Prints: // Stmt after inlining: // { - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // D[i, j] = i * (j + 1) - i; // } // } @@ -298,8 +299,8 @@ int main(int argc, char* argv[]) { // Prints: // Stmt after simplification: // { - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // D[i, j] = i * j; // } // } @@ -319,15 +320,15 @@ int main(int argc, char* argv[]) { // Prints: // Stmt after splitWithTail: // { - // for (int i_outer = 0; i_outer < 4; i_outer++) { - // for (int i_inner = 0; i_inner < 13; i_inner++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i_outer : c10::irange(4)) { + // for (const auto i_inner : c10::irange(13)) { + // for (const auto j : c10::irange(32)) { // D[i_inner + 13 * i_outer, j] = i_inner * j + 13 * (i_outer * j); // } // } // } - // for (int i_tail = 0; i_tail < 12; i_tail++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i_tail : c10::irange(12)) { + // for (const auto j : c10::irange(32)) { // D[i_tail + 52, j] = i_tail * j + 52 * j; // } // } @@ -365,8 +366,8 @@ int main(int argc, char* argv[]) { std::cout << *loopnest.root_stmt() << std::endl; // Prints: // { - // for (int i = 0; i < 64; i++) { - // for (int j = 0; j < 32; j++) { + // for (const auto i : c10::irange(64)) { + // for (const auto j : c10::irange(32)) { // X[i, j] = (A[i, j]) + (B[i, j]); // } // } @@ -469,8 +470,8 @@ int main(int argc, char* argv[]) { // Prints: // TE Stmt constructed from TorchScript: // { - // for (int v = 0; v < 5; v++) { - // for (int _tail_tail = 0; _tail_tail < 3; _tail_tail++) { + // for (const auto v : c10::irange(5)) { + // for (const auto _tail_tail : c10::irange(3)) { // aten_add[_tail_tail + 3 * v] = (tA[_tail_tail + 3 * v]) * // ((tA[_tail_tail + 3 * v]) * (tB[_tail_tail + 3 * v])) + // (tB[_tail_tail + 3 * v]); diff --git a/test/custom_operator/op.cpp b/test/custom_operator/op.cpp index dd8ca4344bc1..94dd4f0a5225 100644 --- a/test/custom_operator/op.cpp +++ b/test/custom_operator/op.cpp @@ -1,3 +1,4 @@ +#include #include #include "op.h" @@ -11,7 +12,8 @@ torch::List custom_op( int64_t repeat) { torch::List output; output.reserve(repeat); - for (int64_t i = 0; i < repeat; ++i) { + for (const auto i : c10::irange(repeat)) { + (void)i; // Suppress unused variable warning output.push_back(tensor * scalar); } return output; diff --git a/test/custom_operator/test_custom_ops.cpp b/test/custom_operator/test_custom_ops.cpp index ec22568c5a3e..4cedd7ed0367 100644 --- a/test/custom_operator/test_custom_ops.cpp +++ b/test/custom_operator/test_custom_ops.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -44,7 +45,7 @@ void get_operator_from_registry_and_execute() { const auto manual = custom_op(torch::ones(5), 2.0, 3); TORCH_INTERNAL_ASSERT(output.size() == 3); - for (size_t i = 0; i < output.size(); ++i) { + for (const auto i : c10::irange(output.size())) { TORCH_INTERNAL_ASSERT(output[i].allclose(torch::ones(5) * 2)); TORCH_INTERNAL_ASSERT(output[i].allclose(manual[i])); } diff --git a/test/mobile/custom_build/predictor.cpp b/test/mobile/custom_build/predictor.cpp index aaae9062f777..fc9786b82a1b 100644 --- a/test/mobile/custom_build/predictor.cpp +++ b/test/mobile/custom_build/predictor.cpp @@ -4,6 +4,7 @@ #include #include +#include #include using namespace std; @@ -40,7 +41,7 @@ int main(int argc, const char* argv[]) { }(); std::cout << std::setprecision(3) << std::fixed; - for (int i = 0; i < 5; i++) { + for (const auto i : c10::irange(5)) { std::cout << output.data_ptr()[i] << std::endl; } return 0; diff --git a/torch/csrc/autograd/functions/basic_ops.h b/torch/csrc/autograd/functions/basic_ops.h index 2ff2ce8e499d..8dfe0c3ab1fa 100644 --- a/torch/csrc/autograd/functions/basic_ops.h +++ b/torch/csrc/autograd/functions/basic_ops.h @@ -4,8 +4,6 @@ #include #include -#include - #include #include #include diff --git a/torch/csrc/autograd/python_function.cpp b/torch/csrc/autograd/python_function.cpp index 76fbd8fb3346..01b0eae6ab43 100644 --- a/torch/csrc/autograd/python_function.cpp +++ b/torch/csrc/autograd/python_function.cpp @@ -589,7 +589,7 @@ static void _trace_post_record( // to the original tuple type. if (!unpack_output) { std::vector new_tuple_values; - for (int i = 0; i < num_outputs; ++i) { + for (const auto i : c10::irange(num_outputs)) { TypePtr ptr = node->outputs()[i]->type(); new_tuple_values.push_back(ptr); } diff --git a/torch/csrc/autograd/python_variable.cpp b/torch/csrc/autograd/python_variable.cpp index d566586de851..415c49e4ec31 100644 --- a/torch/csrc/autograd/python_variable.cpp +++ b/torch/csrc/autograd/python_variable.cpp @@ -1706,7 +1706,7 @@ void concrete_dispatch_fn( } // Find overloaded tensors - for (int64_t idx = 0; idx < arguments.size(); idx++) { + for (const auto idx : c10::irange(arguments.size())) { const auto& ivalue = arguments[idx]; if (ivalue.isTensor()) { const auto& tensor = ivalue.toTensor(); @@ -1728,12 +1728,12 @@ void concrete_dispatch_fn( } // Populate positional arguments - for (int64_t idx = 0; idx < positional_default_start; idx++) { + for (const auto idx : c10::irange(positional_default_start)) { PyTuple_SET_ITEM(args.ptr(), idx, torch::jit::toPyObject(std::move(arguments[idx])).release().ptr()); } // Populate keyword arguments - for (int64_t idx = kwarg_only_start; idx < arguments.size(); idx++) { + for (const auto idx : c10::irange(kwarg_only_start, arguments.size())) { // But don't populate default keyword arguments if (is_default(idx)) continue; const auto& arg = schema.arguments()[idx]; diff --git a/torch/csrc/deploy/loader.cpp b/torch/csrc/deploy/loader.cpp index a95cf6a272cc..a9519865028f 100644 --- a/torch/csrc/deploy/loader.cpp +++ b/torch/csrc/deploy/loader.cpp @@ -54,6 +54,7 @@ #include #include +#include #include #include @@ -152,7 +153,7 @@ size_t phdr_table_get_load_size( Elf64_Addr max_vaddr = 0; bool found_pt_load = false; - for (size_t i = 0; i < phdr_count; ++i) { + for (const auto i : c10::irange(phdr_count)) { const Elf64_Phdr* phdr = &phdr_table[i]; if (phdr->p_type != PT_LOAD) { @@ -383,7 +384,7 @@ std::pair> load_needed_from_elf_file( auto program_headers = (Elf64_Phdr*)(data + header_->e_phoff); auto n_program_headers = header_->e_phnum; const Elf64_Dyn* dynamic = nullptr; - for (size_t i = 0; i < n_program_headers; ++i) { + for (const auto i : c10::irange(n_program_headers)) { const Elf64_Phdr* phdr = &program_headers[i]; if (phdr->p_type == PT_DYNAMIC) { dynamic = reinterpret_cast(data + phdr->p_offset); @@ -405,7 +406,7 @@ std::pair> load_needed_from_elf_file( const char* segment_string_table = data + segment_headers[header_->e_shstrndx].sh_offset; - for (size_t i = 0; i < n_segments; ++i) { + for (const auto i : c10::irange(n_segments)) { const Elf64_Shdr* shdr = &segment_headers[i]; if (shdr->sh_type == SHT_STRTAB && strcmp(".dynstr", segment_string_table + shdr->sh_name) == 0) { @@ -641,7 +642,7 @@ struct AlreadyLoadedSymTable { const Elf64_Phdr* program_headers, size_t n_program_headers) { Elf64_Dyn* dynamic = nullptr; - for (size_t i = 0; i < n_program_headers; ++i) { + for (const auto i : c10::irange(n_program_headers)) { const Elf64_Phdr* phdr = &program_headers[i]; // Segment addresses in memory. @@ -871,7 +872,7 @@ struct __attribute__((visibility("hidden"))) CustomLibraryImpl void load_segments() { // from bionic - for (size_t i = 0; i < n_program_headers_; ++i) { + for (const auto i : c10::irange(n_program_headers_)) { const Elf64_Phdr* phdr = &program_headers_[i]; // Segment addresses in memory. @@ -1141,17 +1142,17 @@ struct __attribute__((visibility("hidden"))) CustomLibraryImpl } void relocate() { - for (size_t i = 0; i < dyninfo_.n_rela_; ++i) { + for (const auto i : c10::irange(dyninfo_.n_rela_)) { relocate_one(dyninfo_.rela_[i]); } - for (size_t i = 0; i < dyninfo_.n_plt_rela_; ++i) { + for (const auto i : c10::irange(dyninfo_.n_plt_rela_)) { relocate_one(dyninfo_.plt_rela_[i]); } } void initialize() { call_function(dyninfo_.init_func_); - for (size_t i = 0; i < dyninfo_.n_init_array_; ++i) { + for (const auto i : c10::irange(dyninfo_.n_init_array_)) { call_function(dyninfo_.init_array_[i]); } initialized_ = true;