-
Notifications
You must be signed in to change notification settings - Fork 547
Optim-wip: Move inception test helpers to separate file #863
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
Optim-wip: Move inception test helpers to separate file #863
Conversation
tests/optim/helpers/models.py
Outdated
| import torch | ||
|
|
||
|
|
||
| def _check_layer_in_model( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leading _ is a convention for private. As we are exporting these 2 functions to be used by others, we can remove this leading _
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, yeah I'm not sure how I missed removing the underscore!
tests/optim/helpers/models.py
Outdated
|
|
||
|
|
||
| def _check_layer_in_model( | ||
| self, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passing self here is a little weird, as these functions are not class method. How about updating these 2 functions to simply return the bool and doing the assert in the tests? This also makes the 2 utils more generic to other use cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original idea was to follow after assertTensorAlmostEqual's input variable setup, but it does appear simpler to return bool outputs instead.
tests/optim/helpers/models.py
Outdated
| model: torch.nn.Module, | ||
| layer: Type[torch.nn.Module], | ||
| ) -> None: | ||
| def check_for_layer_in_model(model, layer) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you agree with the above suggestion, we don't need another nested inner function here.
def check_layer_in_model(model, layer):
for _, child in model._modules.items():
if child is None:
continue
if isinstance(child, layer) or check_layer_in_model(child, layer):
return True
return FalseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we can just use the one function with bool outputs to test for the presence and absence of certain layers.
tests/optim/helpers/models.py
Outdated
| self.assertTrue(check_for_layer_in_model(model, layer)) | ||
|
|
||
|
|
||
| def _check_layer_not_in_model( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we may not even need this layer_not_in_model function, just not check_layer_in_model(model, layer)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to using not, we can also use self.assertFalse
self.assertFalse(check_layer_in_model(model, layer))
self.assertTrue(check_layer_in_model(model, layer))
But yeah, it looks like we don't need _check_layer_not_in_model after switching to bool outputs.
This change will make it easier to use these helpers for testing other models.