[5/N] Remove unused loop variables in tests (#166716)

This PR removes unused loop variables in tests.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/166716
Approved by: https://github.com/Lucaskabela, https://github.com/Skylion007
This commit is contained in:
Yuanyuan Chen 2025-10-31 20:47:54 +00:00 committed by PyTorch MergeBot
parent e4043884c7
commit d97144d31e
18 changed files with 59 additions and 65 deletions

View File

@ -266,7 +266,7 @@ class TestFullyShardPostAccGradHookMultiThread(FSDPTestMultiThread):
model(inp).sum().backward() model(inp).sum().backward()
param_names = {param_name for param_name, _ in model.named_parameters()} param_names = {param_name for param_name, _ in model.named_parameters()}
self.assertEqual(param_names, set(param_name_to_hook_count.keys())) self.assertEqual(param_names, set(param_name_to_hook_count.keys()))
for param_name, count in param_name_to_hook_count.items(): for count in param_name_to_hook_count.values():
self.assertEqual(count, 1) self.assertEqual(count, 1)

View File

@ -195,7 +195,7 @@ if not TEST_WITH_DEV_DBG_ASAN:
for i, t in enumerate(tensors): for i, t in enumerate(tensors):
self.assertEqual(t, torch.ones(5, 5, device=device) + i) self.assertEqual(t, torch.ones(5, 5, device=device) + i)
elif self.rank == 0: elif self.rank == 0:
for i, t in enumerate(tensors): for t in tensors:
zeros = torch.zeros(5, 5, device=device) zeros = torch.zeros(5, 5, device=device)
self.assertEqual(t, zeros) self.assertEqual(t, zeros)
y = torch.sum(torch.stack(tensors), axis=0) y = torch.sum(torch.stack(tensors), axis=0)

View File

@ -11795,7 +11795,7 @@ class TestONNXRuntime(onnx_test_common._TestONNXRuntime):
elem = torch.matmul(x[0], y) elem = torch.matmul(x[0], y)
for i in range(x.size(0)): for i in range(x.size(0)):
res.append(torch.matmul(x[i], y)) res.append(torch.matmul(x[i], y))
for i in range(x.size(0)): for _ in range(x.size(0)):
elem = res.pop() elem = res.pop()
for i in range(x.size(0)): for i in range(x.size(0)):
res.append(torch.matmul(x[i], y)) res.append(torch.matmul(x[i], y))
@ -11815,7 +11815,7 @@ class TestONNXRuntime(onnx_test_common._TestONNXRuntime):
elem = torch.matmul(x[0], y) elem = torch.matmul(x[0], y)
for i in range(x.size(0)): for i in range(x.size(0)):
res.append(torch.matmul(x[i], y)) res.append(torch.matmul(x[i], y))
for i in range(x.size(0)): for _ in range(x.size(0)):
del res[0] del res[0]
for i in range(x.size(0)): for i in range(x.size(0)):
res.append(torch.matmul(x[i], y)) res.append(torch.matmul(x[i], y))

View File

@ -109,7 +109,7 @@ class TestProfilerCUDA(TestCase):
t = torch.rand(1, 1).cuda() t = torch.rand(1, 1).cuda()
p = psutil.Process() p = psutil.Process()
last_rss = collections.deque(maxlen=5) last_rss = collections.deque(maxlen=5)
for outer_idx in range(10): for _ in range(10):
with _profile(use_cuda=True): with _profile(use_cuda=True):
for _ in range(1024): for _ in range(1024):
t = torch.mm(t, t) t = torch.mm(t, t)
@ -1054,7 +1054,7 @@ class TestProfiler(TestCase):
schedule=torch.profiler.schedule(wait=1, warmup=1, active=2), schedule=torch.profiler.schedule(wait=1, warmup=1, active=2),
on_trace_ready=trace_handler, on_trace_ready=trace_handler,
) as p: ) as p:
for idx in range(8): for _ in range(8):
self.payload(use_cuda=use_cuda) self.payload(use_cuda=use_cuda)
p.step() p.step()
@ -1144,14 +1144,14 @@ class TestProfiler(TestCase):
# See https://github.com/pytorch/pytorch/issues/88446 # See https://github.com/pytorch/pytorch/issues/88446
optimizer_step() optimizer_step()
for idx in range(niters): for _ in range(niters):
run_batch() run_batch()
with profile( with profile(
activities=supported_activities(), activities=supported_activities(),
schedule=torch.profiler.schedule(wait=1, warmup=1, active=2), schedule=torch.profiler.schedule(wait=1, warmup=1, active=2),
) as p: ) as p:
for idx in range(niters): for _ in range(niters):
run_batch() run_batch()
p.step() p.step()
@ -1508,7 +1508,7 @@ class TestProfiler(TestCase):
) )
inputs = torch.randn(40, 16, 18, 260) inputs = torch.randn(40, 16, 18, 260)
uint32_max = 2**32 - 1 uint32_max = 2**32 - 1
for i in range(5): for _ in range(5):
with profile() as prof: with profile() as prof:
model(inputs) model(inputs)
for event in prof.profiler.kineto_results.events(): for event in prof.profiler.kineto_results.events():
@ -2023,7 +2023,7 @@ assert KinetoStepTracker.current_step() == initial_step + 2 * niters
WAIT_TIME = 10 WAIT_TIME = 10
with profile() as p: with profile() as p:
with torch.profiler.record_function("test_span"): with torch.profiler.record_function("test_span"):
for i in range(WAIT_TIME): for _ in range(WAIT_TIME):
torch.rand(4, 4) torch.rand(4, 4)
time.sleep(1) time.sleep(1)
events = p.events() events = p.events()
@ -2072,7 +2072,7 @@ assert KinetoStepTracker.current_step() == initial_step + 2 * niters
), ),
acc_events=acc_events, acc_events=acc_events,
) as prof: ) as prof:
for i in range(100): for _ in range(100):
torch.add(1, 2) torch.add(1, 2)
prof.step() prof.step()
# print(prof.key_averages()) # print(prof.key_averages())
@ -3161,7 +3161,7 @@ aten::mm""",
r.seed(1) r.seed(1)
text_sections = get_text_sections() text_sections = get_text_sections()
addrs = [] addrs = []
for i in range(200): for _ in range(200):
s = r.randrange(0, len(text_sections)) s = r.randrange(0, len(text_sections))
start, size = text_sections[s] start, size = text_sections[s]
addr = r.randrange(start, start + size) addr = r.randrange(start, start + size)

View File

@ -154,7 +154,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in weight_dict.values(): for v in weight_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
model_list = [SingleLayerLinearDynamicModel(qengine)] model_list = [SingleLayerLinearDynamicModel(qengine)]
for model in model_list: for model in model_list:
@ -178,7 +178,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in weight_dict.values(): for v in weight_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
model_list = [LSTMwithHiddenDynamicModel(qengine)] model_list = [LSTMwithHiddenDynamicModel(qengine)]
for model in model_list: for model in model_list:
@ -200,7 +200,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in ob_dict.values(): for v in ob_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
model_list = [ model_list = [
AnnotatedConvModel(qengine), AnnotatedConvModel(qengine),
@ -235,7 +235,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in ob_dict.values(): for v in ob_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
linear_data = self.calib_data[0][0] linear_data = self.calib_data[0][0]
module_swap_list = [nn.Linear] module_swap_list = [nn.Linear]
@ -260,7 +260,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in ob_dict.values(): for v in ob_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
linear_data = self.calib_data[0][0] linear_data = self.calib_data[0][0]
module_swap_list = [nn.Linear] module_swap_list = [nn.Linear]
@ -314,7 +314,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in ob_dict.values(): for v in ob_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
@override_qengines @override_qengines
def test_compare_model_stub_linear_dynamic(self): def test_compare_model_stub_linear_dynamic(self):
@ -328,7 +328,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in ob_dict.values(): for v in ob_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
linear_data = self.calib_data[0][0] linear_data = self.calib_data[0][0]
@ -357,7 +357,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in ob_dict.values(): for v in ob_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
lstm_input = torch.rand((1, 1, 2)) lstm_input = torch.rand((1, 1, 2))
lstm_hidden = (torch.rand(1, 1, 2), torch.rand(1, 1, 2)) lstm_hidden = (torch.rand(1, 1, 2), torch.rand(1, 1, 2))
@ -411,7 +411,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in act_compare_dict.values(): for v in act_compare_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
linear_data = self.calib_data[0][0] linear_data = self.calib_data[0][0]
model_list = [AnnotatedSingleLayerLinearModel(qengine)] model_list = [AnnotatedSingleLayerLinearModel(qengine)]
@ -447,7 +447,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in act_compare_dict.values(): for v in act_compare_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
@override_qengines @override_qengines
def test_compare_model_outputs_linear_dynamic(self): def test_compare_model_outputs_linear_dynamic(self):
@ -464,7 +464,7 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in act_compare_dict.values(): for v in act_compare_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(v["float"][i].shape == v["quantized"][i].shape) self.assertTrue(v["float"][i].shape == val.shape)
linear_data = self.calib_data[0][0] linear_data = self.calib_data[0][0]
@ -493,18 +493,12 @@ class TestNumericSuiteEager(QuantizationTestCase):
for v in act_compare_dict.values(): for v in act_compare_dict.values():
self.assertTrue(len(v["float"]) == len(v["quantized"])) self.assertTrue(len(v["float"]) == len(v["quantized"]))
for i, val in enumerate(v["quantized"]): for i, val in enumerate(v["quantized"]):
self.assertTrue(len(v["float"][i]) == len(v["quantized"][i])) self.assertTrue(len(v["float"][i]) == len(val))
if i == 0: if i == 0:
self.assertTrue( self.assertTrue(v["float"][i][0].shape == val[0].shape)
v["float"][i][0].shape == v["quantized"][i][0].shape
)
else: else:
self.assertTrue( self.assertTrue(v["float"][i][0].shape == val[0].shape)
v["float"][i][0].shape == v["quantized"][i][0].shape self.assertTrue(v["float"][i][1].shape == val[1].shape)
)
self.assertTrue(
v["float"][i][1].shape == v["quantized"][i][1].shape
)
lstm_input = torch.rand((1, 1, 2)) lstm_input = torch.rand((1, 1, 2))
lstm_hidden = (torch.rand(1, 1, 2), torch.rand(1, 1, 2)) lstm_hidden = (torch.rand(1, 1, 2), torch.rand(1, 1, 2))

View File

@ -525,7 +525,7 @@ class TestFxModelReportObserver(QuantizationTestCase):
def run_model_and_common_checks(self, model, ex_input, num_epochs, batch_size): def run_model_and_common_checks(self, model, ex_input, num_epochs, batch_size):
# split up data into batches # split up data into batches
split_up_data = torch.split(ex_input, batch_size) split_up_data = torch.split(ex_input, batch_size)
for epoch in range(num_epochs): for _epoch in range(num_epochs):
# reset all model report obs # reset all model report obs
model.apply( model.apply(
lambda module: module.reset_batch_and_epoch_values() lambda module: module.reset_batch_and_epoch_values()
@ -952,7 +952,7 @@ class TestFxModelReportClass(QuantizationTestCase):
# see whether observers properly in regular nn.Module # see whether observers properly in regular nn.Module
# there should be 4 observers present in this case # there should be 4 observers present in this case
modules_observer_cnt = 0 modules_observer_cnt = 0
for fqn, module in prepared_for_callibrate_model.named_modules(): for module in prepared_for_callibrate_model.modules():
if isinstance(module, ModelReportObserver): if isinstance(module, ModelReportObserver):
modules_observer_cnt += 1 modules_observer_cnt += 1
@ -999,7 +999,7 @@ class TestFxModelReportClass(QuantizationTestCase):
""" """
# get the number of observers stored as modules # get the number of observers stored as modules
modules_observer_cnt = 0 modules_observer_cnt = 0
for fqn, module in callibrated_fx_module.named_modules(): for module in callibrated_fx_module.modules():
if isinstance(module, ModelReportObserver): if isinstance(module, ModelReportObserver):
modules_observer_cnt += 1 modules_observer_cnt += 1
@ -1058,7 +1058,7 @@ class TestFxModelReportClass(QuantizationTestCase):
# now calibrate the two models # now calibrate the two models
num_iterations = 10 num_iterations = 10
for i in range(num_iterations): for _ in range(num_iterations):
example_input = torch.tensor(torch.randint(100, (1, 3, 3, 3)), dtype=torch.float) example_input = torch.tensor(torch.randint(100, (1, 3, 3, 3)), dtype=torch.float)
prepared_for_callibrate_model_full(example_input) prepared_for_callibrate_model_full(example_input)
prepared_for_callibrate_model_single(example_input) prepared_for_callibrate_model_single(example_input)
@ -1324,7 +1324,7 @@ class TestFxDetectInputWeightEqualization(QuantizationTestCase):
fused = self._get_prepped_for_calibration_model(self.TwoBlockComplexNet(), detector_set, fused=True) fused = self._get_prepped_for_calibration_model(self.TwoBlockComplexNet(), detector_set, fused=True)
# reporter should still give same counts even for fused model # reporter should still give same counts even for fused model
for prepared_for_callibrate_model, mod_report in [non_fused, fused]: for prepared_for_callibrate_model, _mod_report in [non_fused, fused]:
# supported modules to check # supported modules to check
mods_to_check = {nn.Linear, nn.Conv2d} mods_to_check = {nn.Linear, nn.Conv2d}
@ -1345,7 +1345,7 @@ class TestFxDetectInputWeightEqualization(QuantizationTestCase):
self.assertEqual(number_of_obs_found, correct_number_of_obs_inserted) self.assertEqual(number_of_obs_found, correct_number_of_obs_inserted)
# assert that each of the desired modules have the observers inserted # assert that each of the desired modules have the observers inserted
for fqn, module in prepared_for_callibrate_model.named_modules(): for module in prepared_for_callibrate_model.modules():
# check if module is a supported module # check if module is a supported module
is_in_include_list = sum(isinstance(module, x) for x in mods_to_check) > 0 is_in_include_list = sum(isinstance(module, x) for x in mods_to_check) > 0
@ -1569,7 +1569,7 @@ class TestFxDetectOutliers(QuantizationTestCase):
self.assertEqual(number_of_obs_found, correct_number_of_obs_inserted) self.assertEqual(number_of_obs_found, correct_number_of_obs_inserted)
# assert that each of the desired modules have the observers inserted # assert that each of the desired modules have the observers inserted
for fqn, module in prepared_for_callibrate_model.named_modules(): for module in prepared_for_callibrate_model.modules():
# check if module is a supported module # check if module is a supported module
is_in_include_list = isinstance(module, tuple(mods_to_check)) is_in_include_list = isinstance(module, tuple(mods_to_check))

View File

@ -2252,7 +2252,7 @@ class TestFXNumericSuiteNShadows(FXNumericSuiteQuantizationTestCase):
msp(*example_input) msp(*example_input)
def _check_logger_count(model, exp_count_stats, exp_count_comparisons): def _check_logger_count(model, exp_count_stats, exp_count_comparisons):
for name, mod in model.named_modules(): for mod in model.modules():
if isinstance(mod, OutputLogger): if isinstance(mod, OutputLogger):
self.assertTrue( self.assertTrue(
len(mod.stats) == exp_count_stats, len(mod.stats) == exp_count_stats,

View File

@ -4678,7 +4678,7 @@ class TestQuantizeFx(QuantizationTestCase):
self.assertEqual(actpp_module_count, 2) self.assertEqual(actpp_module_count, 2)
actpp_module_count = 0 actpp_module_count = 0
for name, module in m.named_modules(): for module in m.modules():
if isinstance(module, actpp_module_class): if isinstance(module, actpp_module_class):
actpp_module_count += 1 actpp_module_count += 1
self.assertEqual(actpp_module_count, 1) self.assertEqual(actpp_module_count, 1)
@ -5732,7 +5732,7 @@ class TestQuantizeFx(QuantizationTestCase):
m = M().eval() m = M().eval()
qconfig_dict = func(backend) qconfig_dict = func(backend)
m = prepare_fx(m, qconfig_dict, example_inputs=(torch.randn(1, 1, 1, 1))) m = prepare_fx(m, qconfig_dict, example_inputs=(torch.randn(1, 1, 1, 1)))
for name, mod in m.named_modules(): for mod in m.modules():
if _is_activation_post_process(mod) and mod.dtype == torch.quint8: if _is_activation_post_process(mod) and mod.dtype == torch.quint8:
if backend == "fbgemm": if backend == "fbgemm":
lower_bnd = 0 lower_bnd = 0

View File

@ -242,14 +242,14 @@ class TestDatasetRandomSplit(TestCase):
dataset = CustomDataset(self, x) dataset = CustomDataset(self, x)
dataset = random_split(dataset, [5])[0] dataset = random_split(dataset, [5])[0]
data_loader = DataLoader(dataset) data_loader = DataLoader(dataset)
for batch in data_loader: for _batch in data_loader:
pass pass
# fractional splitting # fractional splitting
dataset = CustomDataset(self, x) dataset = CustomDataset(self, x)
dataset = random_split(dataset, [1.0])[0] dataset = random_split(dataset, [1.0])[0]
data_loader = DataLoader(dataset) data_loader = DataLoader(dataset)
for batch in data_loader: for _batch in data_loader:
pass pass
def test_splits_reproducibility(self): def test_splits_reproducibility(self):
@ -1155,7 +1155,7 @@ class TestMultiEpochDataset(IterableDataset):
worker_info = torch.utils.data.get_worker_info() worker_info = torch.utils.data.get_worker_info()
assert worker_info is not None assert worker_info is not None
worker_id = worker_info.id worker_id = worker_info.id
for idx in range(self.length // worker_info.num_workers): for _ in range(self.length // worker_info.num_workers):
yield worker_id yield worker_id
def __len__(self): def __len__(self):
@ -2000,7 +2000,7 @@ except RuntimeError as e:
dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers
) )
for ind in range(num_epochs): for _ in range(num_epochs):
for batch_idx, sample in enumerate(dataloader): for batch_idx, sample in enumerate(dataloader):
self.assertEqual( self.assertEqual(
sample.tolist(), [batch_idx % num_workers] * batch_size sample.tolist(), [batch_idx % num_workers] * batch_size
@ -3018,7 +3018,7 @@ class IntegrationTestDataLoaderDataPipe(TestCase):
# Same seeds # Same seeds
dl_res = [] dl_res = []
for epoch in range(2): for _epoch in range(2):
torch.manual_seed(123) torch.manual_seed(123)
dl_res.append(list(dl)) dl_res.append(list(dl))
self.assertEqual(dl_res[0], dl_res[1]) self.assertEqual(dl_res[0], dl_res[1])
@ -3238,7 +3238,7 @@ except RuntimeError as e:
) )
dataset.start = 0 dataset.start = 0
for i in range(10): for i in range(10):
for x in dataloader: for _ in dataloader:
pass pass
# Changing the start value here doesn't have any effect in the dataset # Changing the start value here doesn't have any effect in the dataset
# cached by the workers. since they are not recreated between epochs # cached by the workers. since they are not recreated between epochs

View File

@ -270,7 +270,7 @@ class TestFlopCounter(TestCase):
model = torch.nn.ConvTranspose2d(4, 8, (2, 2), stride=2) model = torch.nn.ConvTranspose2d(4, 8, (2, 2), stride=2)
with FlopCounterMode() as mode: with FlopCounterMode() as mode:
for i in range(50): for _ in range(50):
out = model(x) out = model(x)
out.sum().backward() out.sum().backward()
self.assertExpectedInline(str(mode.get_total_flops()), """1536000""") self.assertExpectedInline(str(mode.get_total_flops()), """1536000""")

View File

@ -262,7 +262,7 @@ class TestFXExperimental(JitTestCase):
self.embedding_layers = torch.nn.ModuleList() self.embedding_layers = torch.nn.ModuleList()
el = torch.nn.EmbeddingBag(500000, 4, mode="sum", sparse=True) el = torch.nn.EmbeddingBag(500000, 4, mode="sum", sparse=True)
self.embedding_layers.append(el) self.embedding_layers.append(el)
for i in range(3): for _ in range(3):
el = torch.nn.EmbeddingBag(1000000, 4, mode="sum", sparse=True) el = torch.nn.EmbeddingBag(1000000, 4, mode="sum", sparse=True)
self.embedding_layers.append(el) self.embedding_layers.append(el)
el = torch.nn.EmbeddingBag(500000, 4, mode="sum", sparse=True) el = torch.nn.EmbeddingBag(500000, 4, mode="sum", sparse=True)

View File

@ -94,7 +94,7 @@ def strip_profiling_nodes(nodes):
def warmup_forward(f, *args, profiling_count=2): def warmup_forward(f, *args, profiling_count=2):
for i in range(profiling_count): for _ in range(profiling_count):
results = f(*args) results = f(*args)
return results return results
@ -2284,7 +2284,7 @@ class TestTEFuser(JitTestCase):
x = torch.arange(-10, 10, dtype=torch.float32, requires_grad=True) x = torch.arange(-10, 10, dtype=torch.float32, requires_grad=True)
xs = torch.arange(-10, 10, dtype=torch.float32, requires_grad=True) xs = torch.arange(-10, 10, dtype=torch.float32, requires_grad=True)
script = torch.jit.script(fn) script = torch.jit.script(fn)
for i in range(11): for _ in range(11):
y = fn(x) y = fn(x)
g0 = torch.rand_like(y) g0 = torch.rand_like(y)
y.backward(g0) y.backward(g0)
@ -2514,7 +2514,7 @@ class TestTEFuser(JitTestCase):
x, y, z = gen(n), gen(n), gen(n) x, y, z = gen(n), gen(n), gen(n)
func_s(x, y, z) func_s(x, y, z)
for incr in range(3): for _incr in range(3):
func_s(*[gen(n + 1) for _ in range(3)]) func_s(*[gen(n + 1) for _ in range(3)])
g = torch.jit.last_executed_optimized_graph() g = torch.jit.last_executed_optimized_graph()
@ -2678,7 +2678,7 @@ class TestTEFuser(JitTestCase):
f_traced = torch.jit.trace(f, (x, y)) f_traced = torch.jit.trace(f, (x, y))
for i in range(4): for _ in range(4):
# make sure this doesn't error out # make sure this doesn't error out
res = f_traced(x, y) res = f_traced(x, y)
@ -2697,7 +2697,7 @@ class TestTEFuser(JitTestCase):
ref = fn(x) ref = fn(x)
script_fn = torch.jit.script(fn) script_fn = torch.jit.script(fn)
for i in range(4): for _ in range(4):
res = script_fn(x) res = script_fn(x)
self.assertEqual(ref, res) self.assertEqual(ref, res)

View File

@ -130,7 +130,7 @@ class MpsMemoryLeakCheck:
discrepancy_detected = True discrepancy_detected = True
# Query memory multiple items to ensure leak was not transient # Query memory multiple items to ensure leak was not transient
for n in range(3): for _ in range(3):
caching_allocator_mem_allocated = torch.mps.current_allocated_memory() caching_allocator_mem_allocated = torch.mps.current_allocated_memory()
driver_mem_allocated = torch.mps.driver_allocated_memory() driver_mem_allocated = torch.mps.driver_allocated_memory()
@ -4984,7 +4984,7 @@ class TestMPS(TestCaseMPS):
input_xs.append(torch.ones(prod, dtype=torch.int).reshape(shape).bool()) input_xs.append(torch.ones(prod, dtype=torch.int).reshape(shape).bool())
input_xs.append(torch.zeros(prod, dtype=torch.int).reshape(shape).bool()) input_xs.append(torch.zeros(prod, dtype=torch.int).reshape(shape).bool())
for i, cpu_x in enumerate(input_xs): for cpu_x in input_xs:
x = cpu_x.detach().clone().to('mps') x = cpu_x.detach().clone().to('mps')
y = torch.all(x) y = torch.all(x)
ref_y = torch.all(cpu_x) ref_y = torch.all(cpu_x)
@ -9601,7 +9601,7 @@ class TestSDPA(TestCaseMPS):
key = torch.randn(batch_size, num_heads, seq_len, head_dim, device="mps", dtype=torch.float32) key = torch.randn(batch_size, num_heads, seq_len, head_dim, device="mps", dtype=torch.float32)
value = torch.randn(batch_size, num_heads, seq_len, head_dim, device="mps", dtype=torch.float32) value = torch.randn(batch_size, num_heads, seq_len, head_dim, device="mps", dtype=torch.float32)
memory_footprints = [] memory_footprints = []
for i in range(100): for _ in range(100):
output = F.scaled_dot_product_attention(query, key, value) output = F.scaled_dot_product_attention(query, key, value)
current_mem, driver_mem = get_mps_memory_usage() current_mem, driver_mem = get_mps_memory_usage()
memory_footprints.append((current_mem, driver_mem)) memory_footprints.append((current_mem, driver_mem))

View File

@ -2040,7 +2040,7 @@ class TestCompositeCompliance(TestCase):
# Convert strided tensor inputs to COW tensors and make copies of # Convert strided tensor inputs to COW tensors and make copies of
# all inputs # all inputs
for idx, arg in enumerate(args_raw): for arg in args_raw:
if is_strided_tensor(arg): if is_strided_tensor(arg):
args_copy.append(arg.detach().clone()) args_copy.append(arg.detach().clone())
args.append(torch._lazy_clone(arg)) args.append(torch._lazy_clone(arg))

View File

@ -1433,7 +1433,7 @@ class TestSparse(TestSparseBase):
def test_shape(num_mats, dim_i, dim_j, dim_k, nnz): def test_shape(num_mats, dim_i, dim_j, dim_k, nnz):
a_list = [] a_list = []
b_list = [] b_list = []
for mat_idx in range(num_mats): for _ in range(num_mats):
a_mat = self._gen_sparse(2, nnz, [dim_i, dim_j], dtype, device, coalesced)[0] a_mat = self._gen_sparse(2, nnz, [dim_i, dim_j], dtype, device, coalesced)[0]
b_mat = torch.randn([dim_j, dim_k], dtype=dtype, device=device) b_mat = torch.randn([dim_j, dim_k], dtype=dtype, device=device)
a_list.append(a_mat) a_list.append(a_mat)
@ -3558,7 +3558,7 @@ class TestSparse(TestSparseBase):
values = torch.ones((indices.shape[1],) + shape[sparse_dim:], dtype=dtype, device=device) values = torch.ones((indices.shape[1],) + shape[sparse_dim:], dtype=dtype, device=device)
else: else:
ranges = [] ranges = []
for j, sz in enumerate(shape[:sparse_dim]): for sz in shape[:sparse_dim]:
ranges.append(list(range(sz))) ranges.append(list(range(sz)))
indices = torch.tensor(list(itertools.product(*ranges)), dtype=torch.long, device=device).t() indices = torch.tensor(list(itertools.product(*ranges)), dtype=torch.long, device=device).t()
values = torch.zeros((indices.shape[1],) + shape[sparse_dim:], dtype=dtype, device=device) values = torch.zeros((indices.shape[1],) + shape[sparse_dim:], dtype=dtype, device=device)

View File

@ -46,7 +46,7 @@ class TestThroughputBenchmark(TestCase):
inputs = [] inputs = []
for i in range(NUM_INPUTS): for _ in range(NUM_INPUTS):
inputs.append([torch.randn(B, D_in), torch.randn(B, D_in)]) inputs.append([torch.randn(B, D_in), torch.randn(B, D_in)])
bench = ThroughputBenchmark(module) bench = ThroughputBenchmark(module)

View File

@ -2012,7 +2012,7 @@ class TestTorchDeviceType(TestCase):
res = x.scatter_add(dim, idx, src) res = x.scatter_add(dim, idx, src)
# Checking if scatter_add is deterministic # Checking if scatter_add is deterministic
for i in range(5): for _ in range(5):
res_next = x.scatter_add(dim, idx, src) res_next = x.scatter_add(dim, idx, src)
self.assertEqual(res, res_next, atol=0, rtol=0) self.assertEqual(res, res_next, atol=0, rtol=0)
res = res_next res = res_next

View File

@ -6131,7 +6131,7 @@ class TestArrayCreationCopyArgument(TestCase):
assert res is not base_arr assert res is not base_arr
for copy in self.false_vals: for copy in self.false_vals:
res = np.array(arr, copy=False) res = np.array(arr, copy=copy)
assert_array_equal(res, base_arr) assert_array_equal(res, base_arr)
assert res is base_arr # numpy trusts the ArrayLike assert res is base_arr # numpy trusts the ArrayLike