-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Enable torch.tensor typechecks #45077
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
Conversation
💊 CI failures summary and remediationsAs of commit c78539d (more details on the Dr. CI page):
Extra GitHub checks: 1 failed
codecov.io: 1 failed
This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group. This comment has been revised 33 times. |
e6fae89
to
c23ca86
Compare
c23ca86
to
f4e8106
Compare
several ignores are put in due to limitation on mypy
f4e8106
to
8c82951
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@walterddr has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Codecov Report
@@ Coverage Diff @@
## master #45077 +/- ##
==========================================
- Coverage 67.83% 67.83% -0.01%
==========================================
Files 384 384
Lines 50050 50060 +10
==========================================
+ Hits 33953 33958 +5
- Misses 16097 16102 +5
Continue to review full report at Codecov.
|
tools/pyi/gen_pyi.py
Outdated
@@ -536,6 +531,7 @@ def gen_pyi(declarations_path, out): | |||
'def __init__(self, other: Tensor) -> None: ...', | |||
'def __init__(self, size: {}, *, {}) -> None: ...'.format(type_to_python('IntArrayRef'), DEVICE_PARAM), | |||
], | |||
'as_subclass': ["def as_subclass(self, cls) -> Tensor: ..."], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to have a more accurate type here but I won't block this PR on it.
tools/pyi/gen_pyi.py
Outdated
@@ -576,6 +573,10 @@ def gen_pyi(declarations_path, out): | |||
], | |||
'item': ["def item(self) -> Number: ..."], | |||
'copy_': ["def copy_(self, src: Tensor, non_blocking: _bool=False) -> Tensor: ..."], | |||
'set_': ['def set_(self, storage: Storage, offset: _int, size: Size, stride: Tuple[_int]) -> Tensor: ...', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean Tuple[_int, ...]
here; otherwise it's a single element tuple
tools/pyi/gen_pyi.py
Outdated
@@ -576,6 +573,10 @@ def gen_pyi(declarations_path, out): | |||
], | |||
'item': ["def item(self) -> Number: ..."], | |||
'copy_': ["def copy_(self, src: Tensor, non_blocking: _bool=False) -> Tensor: ..."], | |||
'set_': ['def set_(self, storage: Storage, offset: _int, size: Size, stride: Tuple[_int]) -> Tensor: ...', | |||
'def set_(self, storage: Storage) -> Tensor: ...'], | |||
'split': ['def split(self, split_size: _int, dim: _int=0) -> Union[Tuple[Tensor, ...], List[Tensor]]: ...', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't want to specify what the return type is, just say Sequence[Tensor]
. But I think we guarantee that we return a list here.
tools/pyi/gen_pyi.py
Outdated
'set_': ['def set_(self, storage: Storage, offset: _int, size: Size, stride: Tuple[_int]) -> Tensor: ...', | ||
'def set_(self, storage: Storage) -> Tensor: ...'], | ||
'split': ['def split(self, split_size: _int, dim: _int=0) -> Union[Tuple[Tensor, ...], List[Tensor]]: ...', | ||
'def split(self, split_size: Tuple[_int], dim: _int=0) -> Union[Tuple[Tensor, ...], List[Tensor]]: ...'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here
Some minor errors but it LGTM. There's also some small extra trailing whitespace. Thanks for doing this! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the review @ezyang. I will clean them up asap
@@ -528,7 +534,7 @@ def __format__(self, format_spec): | |||
return self.item().__format__(format_spec) | |||
return object.__format__(self, format_spec) | |||
|
|||
def __ipow__(self, other): | |||
def __ipow__(self, other): # type: ignore[misc] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is probably the only issue I dont know what's the right way to fix:
apparently __ipow__
is not defined in the generated _C/__init__.pyi
and in previous line __pow__ = _C._TensorBase.pow
instead of _C._TensorBase.__pow__
(which is defined BTW). it got me a bit confused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, reading the code here, it sounds like we don't implement __ipow__
(that's why we raise NotImplemented
here). So it isn't unexpected that it's not defined in _C
. What's the problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops. forgot to mention the problem - mypy expects identical signature between __pow__
and __ipow__
since a **= b
is equivalent to a = a ** b
. Yes I think we are expected to not implemented this function so I will just leave it as ignore for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@walterddr has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
torch/tensor.py
Outdated
@@ -154,7 +160,7 @@ def __setstate__(self, state): | |||
raise RuntimeError('__setstate__ can be only called on leaf Tensors') | |||
if len(state) == 4: | |||
# legacy serialization of Tensor | |||
self.set_(*state) | |||
self.set_(*state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: try to avoid unnecessary trailing space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can configure your editor to tell you about this. On vim I use set listchars=tab:>-,trail:·
tools/pyi/gen_pyi.py
Outdated
@@ -576,6 +573,10 @@ def gen_pyi(declarations_path, out): | |||
], | |||
'item': ["def item(self) -> Number: ..."], | |||
'copy_': ["def copy_(self, src: Tensor, non_blocking: _bool=False) -> Tensor: ..."], | |||
'set_': ['def set_(self, storage: Storage, offset: _int, size: Size, stride: Tuple[_int, ...]) -> Tensor: ...', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading this again, I don't think Tuple
is the right type here, because I'm pretty sure we accept lists too here. I think using the _size
type for size
and stride
will be more correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_size
is defined in torch.types
... | ||
|
||
def _new_shared(self, int) -> 'Storage': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you deleting _new_shared() here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was reordering them based on alphabetical order :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@walterddr has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
@walterddr merged this pull request in bea7901. |
this fixes #42983.