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

"example_input_array" depends on ordering of modules #1556

Closed
awaelchli opened this issue Apr 22, 2020 · 0 comments · Fixed by #1773
Closed

"example_input_array" depends on ordering of modules #1556

awaelchli opened this issue Apr 22, 2020 · 0 comments · Fixed by #1773
Labels
bug Something isn't working help wanted Open to be worked on

Comments

@awaelchli
Copy link
Contributor

awaelchli commented Apr 22, 2020

🐛 Bug

To Reproduce

  1. Go to the pl_examples/basic_examples/LightningTemplateModel.py
  2. Change the order of modules in the __build_model method from
    def __build_model(self):
        self.c_d1 = nn.Linear(in_features=self.hparams.in_features,
                              out_features=self.hparams.hidden_dim)
        self.c_d1_bn = nn.BatchNorm1d(self.hparams.hidden_dim)
        self.c_d1_drop = nn.Dropout(self.hparams.drop_prob)

        self.c_d2 = nn.Linear(in_features=self.hparams.hidden_dim,
                              out_features=self.hparams.out_features)

to:

    def __build_model(self):
        self.c_d1 = nn.Linear(in_features=self.hparams.in_features,
                              out_features=self.hparams.hidden_dim)
        # move the layer definition up here
        self.c_d2 = nn.Linear(in_features=self.hparams.hidden_dim,  
                              out_features=self.hparams.out_features)

        self.c_d1_bn = nn.BatchNorm1d(self.hparams.hidden_dim)
        self.c_d1_drop = nn.Dropout(self.hparams.drop_prob)

We get an error message because input size does not match (for this order).

Expected behavior

Input output sizes are computed in order of execution, not definition. This is important because PyTorch graphs are dynamically built on each forward, so order of execution of each layer is not known beforehand.

Proposed Fix

I propose to install a forward hook on each submodule and compute the sizes that way.
I have started to validate the fix already and would like to submit a PR very soon if you agree.

Additional Context

It could be confusing to a user to see this error, they might think something is wrong with their code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Open to be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant