-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Simplify _NO_WRAPPING_EXCEPTIONS #7806
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,14 +36,9 @@ def _to_tensor( | |
def wrap_like(cls: Type[D], other: D, tensor: torch.Tensor) -> D: | ||
return tensor.as_subclass(cls) | ||
|
||
_NO_WRAPPING_EXCEPTIONS = { | ||
torch.Tensor.clone: lambda cls, input, output: cls.wrap_like(input, output), | ||
torch.Tensor.to: lambda cls, input, output: cls.wrap_like(input, output), | ||
torch.Tensor.detach: lambda cls, input, output: cls.wrap_like(input, output), | ||
# We don't need to wrap the output of `Tensor.requires_grad_`, since it is an inplace operation and thus | ||
# retains the type automatically | ||
torch.Tensor.requires_grad_: lambda cls, input, output: output, | ||
} | ||
# The ops in this set are those that should *preserve* the Datapoint type, | ||
# i.e. they are exceptions to the "no wrapping" rule. | ||
_NO_WRAPPING_EXCEPTIONS = {torch.Tensor.clone, torch.Tensor.to, torch.Tensor.detach, torch.Tensor.requires_grad_} | ||
|
||
@classmethod | ||
def __torch_function__( | ||
|
@@ -79,22 +74,21 @@ def __torch_function__( | |
with DisableTorchFunctionSubclass(): | ||
output = func(*args, **kwargs or dict()) | ||
|
||
wrapper = cls._NO_WRAPPING_EXCEPTIONS.get(func) | ||
# Apart from `func` needing to be an exception, we also require the primary operand, i.e. `args[0]`, to be | ||
if func in cls._NO_WRAPPING_EXCEPTIONS and isinstance(args[0], cls): | ||
# We also require the primary operand, i.e. `args[0]`, to be | ||
# an instance of the class that `__torch_function__` was invoked on. The __torch_function__ protocol will | ||
# invoke this method on *all* types involved in the computation by walking the MRO upwards. For example, | ||
# `torch.Tensor(...).to(datapoints.Image(...))` will invoke `datapoints.Image.__torch_function__` with | ||
# `args = (torch.Tensor(), datapoints.Image())` first. Without this guard, the original `torch.Tensor` would | ||
# be wrapped into a `datapoints.Image`. | ||
if wrapper and isinstance(args[0], cls): | ||
return wrapper(cls, args[0], output) | ||
return cls.wrap_like(args[0], output) | ||
|
||
# Inplace `func`'s, canonically identified with a trailing underscore in their name like `.add_(...)`, | ||
# will retain the input type. Thus, we need to unwrap here. | ||
if isinstance(output, cls): | ||
return output.as_subclass(torch.Tensor) | ||
if isinstance(output, cls): | ||
# DisableTorchFunctionSubclass is ignored by inplace ops like `.add_(...)`, | ||
# so for those, the output is still a Datapoint. Thus, we need to manually unwrap. | ||
Comment on lines
+83
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also move most of the content of the |
||
return output.as_subclass(torch.Tensor) | ||
|
||
return output | ||
return output | ||
|
||
def _make_repr(self, **kwargs: Any) -> str: | ||
# This is a poor man's implementation of the proposal in https://github.com/pytorch/pytorch/issues/76532. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
the deepcopy isn't a leaf anymore because it went through
wrap_like()
, so it's got an "ancestor".I don't think
is_leaf
is part of thedeepcopy
contract anyway? I don't think we really need to enforce 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.
I don't think it is specified anywhere, so I'm ok with removing this check. Might be surprising to users though if they bank on this. Let's find out though 🤷