-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support inspecting the signature of decorated hooks (#17507)
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import torch | ||
|
||
from lightning.pytorch.utilities.signature_utils import is_param_in_hook_signature | ||
|
||
|
||
def test_param_in_hook_signature(): | ||
class LightningModule: | ||
def validation_step(self, dataloader_iter, batch_idx): | ||
... | ||
|
||
model = LightningModule() | ||
assert is_param_in_hook_signature(model.validation_step, "dataloader_iter", explicit=True) | ||
|
||
class LightningModule: | ||
@torch.no_grad() | ||
def validation_step(self, dataloader_iter, batch_idx): | ||
... | ||
|
||
model = LightningModule() | ||
assert is_param_in_hook_signature(model.validation_step, "dataloader_iter", explicit=True) | ||
|
||
class LightningModule: | ||
def validation_step(self, *args): | ||
... | ||
|
||
model = LightningModule() | ||
assert not is_param_in_hook_signature(model.validation_step, "dataloader_iter", explicit=True) | ||
assert is_param_in_hook_signature(model.validation_step, "dataloader_iter", explicit=False) | ||
|
||
class LightningModule: | ||
def validation_step(self, a, b): | ||
... | ||
|
||
model = LightningModule() | ||
assert not is_param_in_hook_signature(model.validation_step, "dataloader_iter", min_args=3) | ||
assert is_param_in_hook_signature(model.validation_step, "dataloader_iter", min_args=2) |