Skip to content

Commit 8b18864

Browse files
cyyeverpytorchmergebot
authored andcommitted
[2/N] Fix unused loop variables (pytorch#166500)
This PR removes unused loop variables. Pull Request resolved: pytorch#166500 Approved by: https://github.com/mlazos
1 parent 96b6184 commit 8b18864

File tree

13 files changed

+33
-35
lines changed

13 files changed

+33
-35
lines changed

test/distributed/_composable/test_composability/test_2d_composability.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def _shard_placement_fn(param: nn.Parameter) -> Optional[Shard]:
218218

219219
torch.manual_seed(42 + global_mesh.get_local_rank("dp"))
220220
inp = torch.randint(0, model_args.vocab_size, (2, 16), device=device_type)
221-
for iter_idx in range(5):
221+
for _ in range(5):
222222
ref_loss = ref_model(inp).sum()
223223
loss = model(inp).sum()
224224
self.assertEqual(ref_loss, loss)
@@ -238,9 +238,7 @@ def _shard_placement_fn(param: nn.Parameter) -> Optional[Shard]:
238238
# runs its reduce-scatter
239239
self.assertIsInstance(model.pos_embeddings.weight.placements[1], Shard)
240240
self.assertIsInstance(model.pos_embeddings.weight.grad.placements[1], Shard)
241-
for ref_param, (param_name, param) in zip(
242-
ref_model.parameters(), model.named_parameters()
243-
):
241+
for ref_param, param in zip(ref_model.parameters(), model.parameters()):
244242
full_grad = param.grad.full_tensor()
245243
self.assertEqual(ref_param.grad, full_grad)
246244

test/distributed/_composable/test_composability/test_pp_composability.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def apply_tp(
336336
for model in partial_models
337337
]
338338

339-
for train_step in range(5):
339+
for _train_step in range(5):
340340
for optimizer in optimizers:
341341
optimizer.zero_grad()
342342
inputs = torch.rand((num_microbatches, dim), device=self.device)
@@ -517,7 +517,7 @@ def apply_same_precision(partial_model):
517517
for model in ref_partial_models
518518
]
519519

520-
for train_step in range(5):
520+
for _train_step in range(5):
521521
for optimizer in optimizers:
522522
optimizer.zero_grad()
523523
for ref_optimizer in ref_optimizers:

test/quantization/core/test_quantized_op.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ def test_cat(self, X, num, dim, relu):
22022202
X = torch.from_numpy(X)
22032203
new_shape = np.array(X.shape)
22042204
new_shape[dim] = 0
2205-
for idx in range(num):
2205+
for _ in range(num):
22062206
tensors_q.append(torch.quantize_per_tensor(X, scale, zero_point,
22072207
torch_type))
22082208
tensors_ref.append(X)
@@ -3162,7 +3162,7 @@ def forward(
31623162
# Quantize
31633163
mha_quantized = torch.ao.quantization.convert(mha_prepared)
31643164

3165-
for name, param in mha_quantized.named_parameters():
3165+
for name, _param in mha_quantized.named_parameters():
31663166
self.assertTrue("in_proj_weight" not in name)
31673167

31683168
qy = mha_quantized(*q_data)
@@ -5991,7 +5991,7 @@ def trace_handler(p):
59915991
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
59925992
schedule=my_schedule,
59935993
on_trace_ready=trace_handler) as prof:
5994-
for i in range(30):
5994+
for _ in range(30):
59955995
conv_op(input, weight, None, stride, padding, dilation, groups)
59965996
prof.step()
59975997

@@ -6006,7 +6006,7 @@ def trace_handler(p):
60066006
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
60076007
schedule=my_schedule,
60086008
on_trace_ready=trace_handler) as prof:
6009-
for i in range(30):
6009+
for _ in range(30):
60106010
conv_op(input_fp16, weight_fp16, None, stride, padding, dilation, groups)
60116011
prof.step()
60126012

@@ -6023,7 +6023,7 @@ def trace_handler(p):
60236023
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
60246024
schedule=my_schedule,
60256025
on_trace_ready=trace_handler) as prof:
6026-
for i in range(30):
6026+
for _ in range(30):
60276027
conv_op(input_int8, weight_prepacked, scale, zero_point)
60286028
prof.step()
60296029

test/quantization/core/test_quantized_tensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def test_compare_per_channel_device_numerics(self):
586586
]
587587
axis = 1
588588
device = torch.device('cuda')
589-
for i in range(20):
589+
for _ in range(20):
590590
for dtype, zero_type in dtype_and_zero_types:
591591
r = torch.rand(2, 2) * 10
592592
r[0, 0] = 2.5
@@ -1113,7 +1113,7 @@ def _test_qtensor_index_put_non_accumulate_deterministic(self, device):
11131113
zero_point = 10
11141114
types = [torch.qint8, torch.quint8, torch.qint32]
11151115
for qtype in types:
1116-
for i in range(3):
1116+
for _ in range(3):
11171117
m = random.randint(10, 20)
11181118
elems = random.randint(20000, 30000)
11191119
values = torch.rand(elems, device=device)

test/quantization/core/test_workflow_module.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ def test_observers_preserve_buffers(self):
10281028
for observer_type in observer_types:
10291029
observer = observer_type()
10301030
buffer_ids_before = _get_buffer_ids(observer)
1031-
for _i in range(5):
1031+
for _ in range(5):
10321032
inputs = torch.rand((4, 4, 4))
10331033
observer(inputs)
10341034
buffer_ids_after = _get_buffer_ids(observer)
@@ -1046,7 +1046,7 @@ def test_fake_quant_preserves_buffers(self):
10461046
"""
10471047
model = torch.ao.quantization.FakeQuantize()
10481048
buffer_ids_before = _get_buffer_ids(model)
1049-
for _i in range(5):
1049+
for _ in range(5):
10501050
inputs = torch.rand((4, 4, 4))
10511051
model(inputs)
10521052
model.apply(torch.ao.quantization.enable_fake_quant)
@@ -1309,7 +1309,7 @@ def test_compare_fused_obs_fq_oss_module(self, device):
13091309
torch.ao.quantization.enable_observer(mod_ref)
13101310
mod_ref.to(device)
13111311

1312-
for i in range(10):
1312+
for _ in range(10):
13131313
x = torch.randn(5, 5, device=device)
13141314
out = mod(x)
13151315
out_ref = mod_ref(x)
@@ -1445,7 +1445,7 @@ def test_embedding_qat_config(self):
14451445

14461446
count_fake_quant = 0
14471447
count_activation_postproc = 0
1448-
for name, mod in quant_model.named_modules():
1448+
for name, _mod in quant_model.named_modules():
14491449
if name.endswith('weight_fake_quant'):
14501450
count_fake_quant += 1
14511451
if name.count('activation_post_process') == 1 and 'weight_fake_quant' not in name:

test/torch_np/numpy_tests/core/test_multiarray.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,7 +2702,7 @@ def test_diagonal_memleak(self):
27022702
a = np.zeros((100, 100))
27032703
if HAS_REFCOUNT:
27042704
assert_(sys.getrefcount(a) < 50)
2705-
for i in range(100):
2705+
for _ in range(100):
27062706
a.diagonal()
27072707
if HAS_REFCOUNT:
27082708
assert_(sys.getrefcount(a) < 50)
@@ -4457,7 +4457,7 @@ def test_none_shape(self):
44574457

44584458
def test_0d_shape(self):
44594459
# to it multiple times to test it does not break alloc cache gh-9216
4460-
for i in range(10):
4460+
for _ in range(10):
44614461
x = np.empty((1,))
44624462
x.resize(())
44634463
assert_equal(x.shape, ())
@@ -5081,7 +5081,7 @@ def test_dot_3args(self):
50815081
v = np.random.random_sample((16, 32))
50825082

50835083
r = np.empty((1024, 32))
5084-
for i in range(12):
5084+
for _ in range(12):
50855085
dot(f, v, r)
50865086
if HAS_REFCOUNT:
50875087
assert_equal(sys.getrefcount(r), 2)
@@ -6678,7 +6678,7 @@ def test_largedim(self):
66786678
np.random.seed(2)
66796679
array = np.random.rand(*shape)
66806680

6681-
for i in range(10):
6681+
for _ in range(10):
66826682
benchmark = array.nonzero()
66836683
result = array.nonzero()
66846684
assert_array_equal(benchmark, result)

test/torch_np/numpy_tests/fft/test_pocketfft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def worker(args, q):
366366

367367
[x.join() for x in t]
368368
# Make sure all threads returned the correct value
369-
for i in range(self.threads):
369+
for _ in range(self.threads):
370370
# under torch.dynamo `assert_array_equal` fails with relative errors of
371371
# about 1.5e-14. Hence replace it with `assert_allclose(..., rtol=2e-14)`
372372
assert_allclose(

test/torch_np/numpy_tests/lib/test_function_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,12 +2596,12 @@ def test_dtype_reference_leaks(self):
25962596
intp_refcount = sys.getrefcount(np.dtype(np.intp))
25972597
double_refcount = sys.getrefcount(np.dtype(np.double))
25982598

2599-
for j in range(10):
2599+
for _ in range(10):
26002600
np.bincount([1, 2, 3])
26012601
assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount)
26022602
assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount)
26032603

2604-
for j in range(10):
2604+
for _ in range(10):
26052605
np.bincount([1, 2, 3], [4, 5, 6])
26062606
assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount)
26072607
assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount)

test/torch_np/numpy_tests/linalg/test_linalg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1706,7 +1706,7 @@ def test_reduced_rank(self):
17061706
# Test matrices with reduced rank
17071707
# rng = np.random.RandomState(20120714)
17081708
np.random.seed(20120714)
1709-
for i in range(100):
1709+
for _ in range(100):
17101710
# Make a rank deficient matrix
17111711
X = np.random.normal(size=(40, 10))
17121712
X[:, 0] = X[:, 1] + X[:, 2]

torch/_dynamo/compiled_autograd.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@ def dce(self) -> None:
969969

970970
# Dynamo guards will error instead of creating aliasing guards unless we unpack them in the graph
971971
unpack_nodes: OrderedSet[torch.fx.Node] = OrderedSet()
972-
for i, node in enumerate(self.fx_tracer.graph.find_nodes(op="placeholder")):
972+
i: int | None = None
973+
for i, node in enumerate(self.fx_tracer.graph.find_nodes(op="placeholder")): # noqa: B007
973974
unpack_nodes.update(node.users.keys())
974975
assert i == len(_graph_placeholders) - 1
975976

0 commit comments

Comments
 (0)