Skip to content

Commit 1c471fc

Browse files
authored
Fix skip of test_training_gradient_checkpointing (#34723)
19d58d3 has introduced a context manager to manage subtests of test_training_gradient_checkpointing. However, test body was not moved under "with" statement. Thus, while tests are correctly marked as skipped, test bodies were still executed. In some cases, as with llama this caused attribute errors. Fixes: #34722 Fixes: 19d58d3 ("Add MLLama (#33703)") Signed-off-by: Dmitry Rogozhkin <[email protected]>
1 parent c772d4d commit 1c471fc

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

tests/test_modeling_common.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -849,29 +849,29 @@ def check_training_gradient_checkpointing(self, gradient_checkpointing_kwargs=No
849849
):
850850
self.skipTest(reason=f"`supports_gradient_checkpointing` is False for {model_class.__name__}.")
851851

852-
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
853-
config.use_cache = False
854-
config.return_dict = True
855-
model = model_class(config)
852+
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
853+
config.use_cache = False
854+
config.return_dict = True
855+
model = model_class(config)
856856

857-
model.to(torch_device)
858-
model.gradient_checkpointing_enable(gradient_checkpointing_kwargs=gradient_checkpointing_kwargs)
859-
model.train()
857+
model.to(torch_device)
858+
model.gradient_checkpointing_enable(gradient_checkpointing_kwargs=gradient_checkpointing_kwargs)
859+
model.train()
860860

861-
# unfreeze additional layers
862-
for p in model.parameters():
863-
p.requires_grad_(True)
861+
# unfreeze additional layers
862+
for p in model.parameters():
863+
p.requires_grad_(True)
864864

865-
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
865+
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
866866

867-
inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)
868-
loss = model(**inputs).loss
869-
loss.backward()
870-
optimizer.step()
867+
inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)
868+
loss = model(**inputs).loss
869+
loss.backward()
870+
optimizer.step()
871871

872-
for k, v in model.named_parameters():
873-
if v.requires_grad:
874-
self.assertTrue(v.grad is not None, f"{k} in {model_class.__name__} has no gradient!")
872+
for k, v in model.named_parameters():
873+
if v.requires_grad:
874+
self.assertTrue(v.grad is not None, f"{k} in {model_class.__name__} has no gradient!")
875875

876876
def test_training(self):
877877
if not self.model_tester.is_training:

0 commit comments

Comments
 (0)