Skip to content

Conversation

@ProGamerGov
Copy link
Contributor

This change will make it easier to use these helpers for testing other models.

import torch


def _check_layer_in_model(
Copy link
Contributor

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 _

Copy link
Contributor Author

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!



def _check_layer_in_model(
self,
Copy link
Contributor

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.

Copy link
Contributor Author

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.

model: torch.nn.Module,
layer: Type[torch.nn.Module],
) -> None:
def check_for_layer_in_model(model, layer) -> bool:
Copy link
Contributor

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 False

Copy link
Contributor Author

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.

self.assertTrue(check_for_layer_in_model(model, layer))


def _check_layer_not_in_model(
Copy link
Contributor

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)

Copy link
Contributor Author

@ProGamerGov ProGamerGov Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aobo-y

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.

@aobo-y aobo-y merged commit aae34d0 into meta-pytorch:optim-wip Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants