8000 torch.nn.Linear wrong type annotation for bias · Issue #68179 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

torch.nn.Linear wrong type annotation for bias #68179

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

Open
gao462 opened this issue Nov 11, 2021 · 4 comments
Open

torch.nn.Linear wrong type annotation for bias #68179

gao462 opened this issue Nov 11, 2021 · 4 comments
Labels
module: nn Related to torch.nn module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@gao462
Copy link
gao462 commented Nov 11, 2021

🐛 Bug

It seems that torch.nn.Linear gives wrong type annotation for bias:
Expect NoneType, but get torch.nn.parameter.Parameter.

To Reproduce

I am using PyTorch 1.9.0.

import torch
x = torch.nn.Linear(3, 5, bias=False)
# `reveal_type(x.bias)` gives `torch.nn.parameter.Parameter` which is wrong.
# But printing will give `NoneType` which is correct.
print(type(x.bias))

Expected behavior

torch.nn.Linear.bias should be Union[types.NoneType, torch.nn.parameter.Parameter], or have a torch.nn.LinearNoBias as a subclass?

Environment

Please copy and paste the output from our
environment collection script
(or fill out the checklist below manually).

You can get the script and run it with:

wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
  • PyTorch Version (e.g., 1.0): 1.9.0
  • OS (e.g., Linux): Linux
  • How you installed PyTorch (conda, pip, source): pip
  • Build command you used (if compiling from source):
  • Python version: 3.9.7
  • CUDA/cuDNN version: 10.2
  • GPU models and configuration:
  • Any other relevant information:

cc @albanD @mruberry @jbschlosser @walterddr @ezyang @malfet @rgommers @xuzhao9 @gramster

@vinnamkim
Copy link
Contributor

I ran your code in the latest master (PyTorch version: 1.11.0a0+git08d08d9). But, it seems no problem.

import torch
x = torch.nn.Linear(3, 5, bias=False)
# `reveal_type(x.bias)` gives `torch.nn.parameter.Parameter` which is wrong.
# But printing will give `NoneType` which is correct.
print(type(x.bias))

print

<class 'NoneType'>

@saketh-are saketh-are added module: nn Related to torch.nn triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Nov 12, 2021
@jbschlosser jbschlosser added the module: typing Related to mypy type annotations label Nov 15, 2021
@jbschlosser
Copy link
Contributor

Printing is correct, but reveal_type outputs torch.nn.parameter.Parameter on master.

@gao462
Copy link
Author
gao462 commented Nov 15, 2021

@jbschlosser is correct. The problem I noticed is reveal_type(x.bias) (which is in the comment, since reveal_type is only for mypy, and is not runnable).

I expect mypy (or other static typing toolkits) to hint x.bias (not print output) as Union[torch.nn.parameter.Parameter, None] (since it is None when bias=False), but it seems it will always be torch.nn.parameter.Parameter which is wrong.

@ezyang
Copy link
Contributor
ezyang commented Jan 6, 2022

IIRC there is some interaction with TorchScript that makes it difficult to put the correct annotation here. Backstory in PR desc at #38211

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: nn Related to torch.nn module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

5 participants
0