Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix disabled grads after call to predict #6657

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed comparing required versions ([#6434](https://github.com/PyTorchLightning/pytorch-lightning/pull/6434))


- Fixed a bug where gradients were disabled after calling `Trainer.predict` ([#6657](https://github.com/PyTorchLightning/pytorch-lightning/pull/6657))


## [1.2.4] - 2021-03-16

### Changed
Expand Down
4 changes: 4 additions & 0 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,10 @@ def run_predict(self):

results = self.predict_loop.on_predict_epoch_end()
self.predict_loop.on_predict_end()

# re-enable grads
torch.set_grad_enabled(True)

return results

def run_sanity_check(self, ref_model):
Expand Down
13 changes: 13 additions & 0 deletions tests/trainer/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,19 @@ def predict_step(self, batch, batch_idx, dataloader_idx=None):
predict(tmpdir, None, None, 1, model=CustomBoringModel())


def test_trainer_predict_grad(tmpdir):
class CustomBoringModel(BoringModel):

def predict_step(self, batch, batch_idx, dataloader_idx=None):
assert batch.expand_as(batch).grad_fn is None
return super().predict_step(batch, batch_idx, dataloader_idx)

predict(tmpdir, None, None, 1, model=CustomBoringModel())

x = torch.zeros(1, requires_grad=True)
assert x.expand_as(x).grad_fn is not None


@pytest.mark.parametrize('datamodule', [False, True])
def test_trainer_predict_cpu(tmpdir, datamodule):
predict(tmpdir, None, None, 1, datamodule=datamodule)
Expand Down