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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

len of ModuleList is incorrect for jitted models #51018

Open
twoertwein opened this issue Jan 24, 2021 · 3 comments
Open

len of ModuleList is incorrect for jitted models #51018

twoertwein opened this issue Jan 24, 2021 · 3 comments
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects

Comments

@twoertwein
Copy link
Contributor

twoertwein commented Jan 24, 2021

馃悰 Bug

from typing import Final

import torch


class Test(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.models: Final = torch.nn.modules.container.ModuleList([])

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        if len(self.models) != 0:
            x = self.models[0](x)
        return x


print(torch.__version__)  # 1.7.1+cu101
model = Test()
torch.jit.script(model)
Traceback (most recent call last):
  File "test_pytorch.py", line 19, in <module>
    torch.jit.script(model)
  File "/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-XWCSm5JO-py3.8/lib/python3.8/site-packages/torch/jit/_script.py", line 897, in script
    return torch.jit._recursive.create_script_module(
  File "/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-XWCSm5JO-py3.8/lib/python3.8/site-packages/torch/jit/_recursive.py", line 352, in create_script_module
    return create_script_module_impl(nn_module, concrete_type, stubs_fn)
  File "/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-XWCSm5JO-py3.8/lib/python3.8/site-packages/torch/jit/_recursive.py", line 410, in create_script_module_impl
    create_methods_and_properties_from_stubs(concrete_type, method_stubs, property_stubs)
  File "/home/twoertwe/.cache/pypoetry/virtualenvs/python-tools-XWCSm5JO-py3.8/lib/python3.8/site-packages/torch/jit/_recursive.py", line 304, in create_methods_and_properties_from_stubs
    concrete_type._create_methods_and_properties(property_defs, property_rcbs, method_defs, method_rcbs, method_defaults)
RuntimeError:
Index 0 out of range of length 0:
  File "test_pytorch.py", line 13
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        if len(self.models) != 0:
            x = self.models[0](x)
                ~~~~~~~~~~~~~~ <--- HERE
        return x

cc @gmagogsfm

@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Jan 24, 2021
@github-actions github-actions bot added this to Need triage in JIT Triage Jan 24, 2021
@gmagogsfm
Copy link
Contributor

Note: this fails during compilation not during forward invocation. We should probably meta-compile condition of if to avoid this.

@SplitInfinity SplitInfinity self-assigned this Jan 27, 2021
@gmagogsfm gmagogsfm moved this from Need triage to Pending in JIT Triage Jan 27, 2021
@twoertwein
Copy link
Contributor Author

I assume this issue can be closed: #43942 and #49728 describe the same underlying issue and they seem to be fixed for 1.8 :)

@twoertwein
Copy link
Contributor Author

nope, still doesn't work on 1.8.0 :( but the issues linked above suggest that it should work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects
JIT Triage
  
Pending
Development

No branches or pull requests

4 participants