This repository was archived by the owner on Aug 21, 2025. It is now read-only.
Fixing tensor.numpy on wrapped tensors #627
Open
vfdev-5 wants to merge 2 commits intopytorch:mainfrom
Open
Conversation
Fixes pytorch#626 Description: - Fixing tensor.numpy on wrapped tensors
zou3519
reviewed
Mar 31, 2022
Comment on lines
+109
to
+128
| level = _C.maybe_get_level(tensor) | ||
| if level == -1: | ||
| return _old_numpy(tensor) | ||
|
|
||
| if _C.is_functionaltensor(tensor): | ||
| # Since we're unwrapping the FunctionalTensorWrapper, we need to make sure | ||
| # that it's up to date first | ||
| torch._sync(tensor) | ||
|
|
||
| value = _C.get_unwrapped(tensor) | ||
| dl_enabled = _C.tls_set_is_included() | ||
| try: | ||
| # Disable temporarily kDynamicLayerFrontModeKey/kDynamicLayerBackModeKey as included dispatch keys | ||
| if (dl_enabled): | ||
| _C._set_dynamic_layer_keys_included(False) | ||
| return value.numpy() | ||
| finally: | ||
| # Reenable kDynamicLayerFrontModeKey/kDynamicLayerBackModeKey as included dispatch keys | ||
| if (dl_enabled): | ||
| _C._set_dynamic_layer_keys_included(True) |
Contributor
There was a problem hiding this comment.
Hmm, so this is a little more complicated than this I think.
When someone calls .numpy() under vmap, we probably want to error out. Otherwise some weird things might happen:
def f(x):
return torch.tensor(x.numpy())
x = torch.randn(B)
vmap(f)(x) # returns a Tensor of size B, B -- is that what we want?
Contributor
There was a problem hiding this comment.
When someone calls .numpy() under the grad transform then we should support this (as long as there are no vmaps involved). I'm not sure what the best way to support this is... one thing we can do is keep unwrapping the Tensor and seeing that no BatchedTensors are involved.
In the long-term we want a better fix for this that perhaps involves making the pytorch dispatcher recognize .numpy() as an operation
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #626
Description: