-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Description
🐛 Bug
Mypy will report typing errors for some operations that should be allowed. I have encountered four different type check errors although I am only sure one of them is an actual bug. But I will list the others as well.
To Reproduce
Error 1 - in-place operations on tensor
t_1 = torch.rand(3)
t_2 = torch.ones_like(t_1)
t_1.add_(t_2)The error message:
error: "Tensor" has no attribute "add_"; maybe "addr_", "addmv_", or "addmm_"?
Error 2 - creating a parameter requires argument 'requires_grad' even though it has a default value
t_1 = torch.rand(3)
p_1 = torch.nn.Parameter(t_1)The error message:
error: Too few arguments for "Parameter"
Error 3 - torch.is_tensor() is not counted as a assertion for typing
from typing import Union
def scale_values(input: torch.Tensor, weight: Union[float, torch.Tensor]) -> torch.Tensor:
if not torch.is_tensor(weight):
weight = torch.tensor(weight)
# the following line is required for mypy to not report an error:
# assert isinstance(weight, torch.Tensor)
return input * weight.to(input)
The error message:
error: Item "float" of "Union[float, Tensor]" has no attribute "to"
Error 4 - torch.nn.Module.training is not a bool
class Dropout(torch.nn.Module):
def forward(self, input) -> torch.Tensor: # type: ignore
return torch.nn.functional.dropout(
input,
p=0.5,
training=self.training
)The error message:
error: Argument "training" to "dropout" has incompatible type "Union[Tensor, Module[Any]]"; expected "bool"
Expected behavior
I know that the first error is an actual bug but the others I am not sure of as there might be some internal design decision that create these errors and that it is intended.
Environment
PyTorch version: 1.4.0
Is debug build: No
CUDA used to build PyTorch: 10.1
OS: Ubuntu 16.04.6 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
CMake version: Could not collect
Python version: 3.7
Is CUDA available: Yes
CUDA runtime version: 9.1.85
GPU models and configuration:
GPU 0: GeForce GTX 1080 Ti
GPU 1: GeForce GTX 1080 Ti
Nvidia driver version: 418.56
cuDNN version: Could not collect
Versions of relevant libraries:
[pip] numpy==1.17.2
[pip] numpydoc==0.9.1
[pip] torch==1.4.0
[pip] torchvision==0.5.0
[conda] blas 1.0 mkl
[conda] mkl 2019.4 243
[conda] mkl-service 2.3.0 py37he904b0f_0
[conda] mkl_fft 1.0.14 py37ha843d7b_0
[conda] mkl_random 1.1.0 py37hd6b4f25_0
[conda] pytorch 1.4.0 py3.7_cuda10.1.243_cudnn7.6.3_0 pytorch
[conda] torchvision 0.5.0 py37_cu101 pytorch
Additional context
Using mypy 0.750
cc @ezyang