Skip to content

Commit b5867a1

Browse files
r-barnesfacebook-github-bot
authored andcommitted
irange-ify 7 (pytorch#62117)
Summary: Pull Request resolved: pytorch#62117 Test Plan: Sandcastle Reviewed By: ngimel Differential Revision: D29879640 fbshipit-source-id: 189578a57301747a3421742e145bbcdf2ad75c49
1 parent 59bb4f2 commit b5867a1

File tree

6 files changed

+17
-12
lines changed

6 files changed

+17
-12
lines changed

torch/csrc/jit/mobile/import.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <ATen/core/ivalue.h>
44
#include <c10/util/ScopeExit.h>
5+
#include <c10/util/irange.h>
56
#include <caffe2/serialize/inline_container.h>
67
#include <torch/csrc/jit/api/compilation_unit.h>
78
#include <torch/csrc/jit/mobile/interpreter.h>
@@ -169,7 +170,7 @@ c10::intrusive_ptr<c10::ivalue::Object> objLoaderMobile(
169170
size_t ndict = dict.size();
170171
auto obj = c10::ivalue::Object::create(type, ndict);
171172
auto it = dict.begin();
172-
for (size_t i = 0; i < ndict; ++i) {
173+
for (const auto i : c10::irange(ndict)) {
173174
std::stringstream name;
174175
name << it->key();
175176
cls->addOrCheckAttribute(name.str(), it->key().type());
@@ -323,7 +324,7 @@ void BytecodeDeserializer::parseMethods(
323324
}
324325

325326
// Process all methods in this mobile module.
326-
for (size_t i = method_i_start; i < vals.size(); ++i) {
327+
for (const auto i : c10::irange(method_i_start, vals.size())) {
327328
const auto& element = vals[i];
328329
const auto& m_tuple = element.toTuple()->elements();
329330
const std::string& function_name = m_tuple[0].toStringRef();
@@ -380,8 +381,8 @@ void BytecodeDeserializer::parseMethods(
380381
"The numbers of instructions and debug handles strings do not match.");
381382
}
382383

383-
for (size_t i = 0; i < ins_list.size(); ++i) {
384-
auto ins_item = ins_list[i].toTuple()->elements();
384+
for (const auto j : c10::irange(ins_list.size())) {
385+
auto ins_item = ins_list[j].toTuple()->elements();
385386
TORCH_CHECK(
386387
ins_item.size() == 3,
387388
"There should be three parts in an instruction. The function name is ",
@@ -390,7 +391,7 @@ void BytecodeDeserializer::parseMethods(
390391
int X = ins_item[1].toInt();
391392
int N = ins_item[2].toInt();
392393
if (has_debug_handles) {
393-
int64_t debug_handle = debug_handles_list[i].toInt();
394+
int64_t debug_handle = debug_handles_list[j].toInt();
394395
function->append_instruction(op_code, X, N, debug_handle);
395396
} else {
396397
function->append_instruction(op_code, X, N);

torch/csrc/jit/mobile/import_data.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <torch/csrc/jit/mobile/import_data.h>
22

33
#include <ATen/core/ivalue.h>
4+
#include <c10/util/irange.h>
45
#include <caffe2/serialize/inline_container.h>
56
#include <torch/csrc/jit/api/compilation_unit.h>
67
#include <torch/csrc/jit/mobile/observer.h>
@@ -127,7 +128,7 @@ c10::IValue BytecodeDeserializer::readArchive(
127128
size_t ndict = dict.size();
128129
auto obj = c10::ivalue::Object::create(type, ndict);
129130
auto it = dict.begin();
130-
for (size_t i = 0; i < ndict; ++i) {
131+
for (const auto i : c10::irange(ndict)) {
131132
std::stringstream name;
132133
name << it->key();
133134
cls->addOrCheckAttribute(name.str(), it->key().type());

torch/csrc/jit/mobile/interpreter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <torch/csrc/jit/runtime/vararg_functions.h>
99

1010
#include <ATen/record_function.h>
11+
#include <c10/util/irange.h>
1112
#include <torch/csrc/jit/mobile/observer.h>
1213

1314
#include <torch/csrc/jit/backends/backend_exception.h>
@@ -169,7 +170,7 @@ bool InterpreterState::run(Stack& stack) {
169170
++pc;
170171
} else {
171172
size_t n_loop_carried = inst.N - 2;
172-
for (size_t i = 0; i < n_loop_carried; ++i) {
173+
for (const auto i : c10::irange(n_loop_carried)) {
173174
frame[i] = std::move(frame[i + 3]);
174175
}
175176
drop(stack, 3); // iteration_count, max_iter, cond

torch/csrc/jit/mobile/model_compatibility.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <ATen/core/ivalue.h>
2+
#include <c10/util/irange.h>
23
#include <caffe2/serialize/file_adapter.h>
34
#include <caffe2/serialize/inline_container.h>
45
#include <torch/csrc/jit/api/compilation_unit.h> // removed after using simple type_resolver/obj_loader
@@ -148,8 +149,7 @@ std::unordered_map<std::string, OperatorInfo> _get_model_ops_and_info(
148149
return result;
149150
}
150151
// loop over all the functions in the bytecode
151-
// NOLINTNEXTLINE(clang-diagnostic-sign-compare)
152-
for (int i = 1; i < bytecode_ivalues.size(); i++) {
152+
for (const auto i : c10::irange(1, bytecode_ivalues.size())) {
153153
// descend to the operators list
154154
auto method_tuple = bytecode_ivalues.at(i).toTuple()->elements();
155155
auto operators_tuple = method_tuple.at(1).toTuple()->elements()[1];

torch/csrc/jit/mobile/module.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <exception>
88

99
#include <ATen/record_function.h>
10+
#include <c10/util/irange.h>
1011

1112
namespace torch {
1213
namespace jit {
@@ -76,7 +77,7 @@ void slot_named_params_recurse(
7677
const std::string& parent_name) {
7778
auto slots = obj->slots();
7879
size_t nslots = slots.size();
79-
for (size_t i = 0; i < nslots; ++i) {
80+
for (const auto i : c10::irange(nslots)) {
8081
auto slot = slots[i];
8182
std::string name =
8283
parent_name.size() == 0 ? parent_name : parent_name + ".";

torch/csrc/jit/mobile/nnc/context.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ATen/Functions.h>
44
#include <ATen/core/functional.h>
55
#include <c10/core/CPUAllocator.h>
6+
#include <c10/util/irange.h>
67

78
#include <torch/csrc/jit/mobile/nnc/registry.h>
89

@@ -197,7 +198,7 @@ c10::impl::GenericList Function::run(
197198
input_specs_.size(),
198199
" actual: ",
199200
inputs.size());
200-
for (size_t i = 0; i < inputs.size(); ++i) {
201+
for (const auto i : c10::irange(inputs.size())) {
201202
const c10::IValue& input = inputs[i];
202203
const auto& input_tensor = input.toTensor();
203204
TORCH_CHECK(
@@ -208,7 +209,7 @@ c10::impl::GenericList Function::run(
208209
// Preallocate and fill in output tensors.
209210
c10::List<at::Tensor> outputs;
210211
outputs.reserve(output_specs_.size());
211-
for (size_t i = 0; i < output_specs_.size(); ++i) {
212+
for (const auto i : c10::irange(output_specs_.size())) {
212213
at::Tensor output = output_specs_[i].allocate();
213214
outputs.emplace_back(output);
214215
args[inputs.size() + i] = output.data_ptr();

0 commit comments

Comments
 (0)