-
Notifications
You must be signed in to change notification settings - Fork 24.3k
asarray
: device does not propagate from input to output after set_default_device
#150199<
8000
/span>
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
Comments
Hey! The function In [1]: import torch
In [2]: torch.get_default_device()
Out[2]: device(type='cpu')
In [3]: x = torch.asarray(0, device=torch.device('cuda'))
In [4]: torch.asarray(x)
Out[4]: tensor(0, device='cuda:0') # OK
In [5]: torch.set_default_device('cpu')
In [6]: torch.asarray(x)
Out[6]: tensor(0)
In [6]: torch.asarray(x).device
Out[6]: device(type='cpu') // Since the index of cpu is -1, the function repr of torch.device has some special logic to not display the index |
I'm sorry there must be some misunderstanding. The torch documentation states:(emphasis mine)
What is actually happening in the code snippets above:The default |
Sorry, I misunderstood. As the Array API standard says, I have excerpted a part of it and pasted some relevant content as follows:
So, I think |
Hello, Let me quote the same page, 3 lines above:
Also, I need to reiterate that torch respects this guidance, but only if nobody called |
Hey, @crusaderky We can try to simplify this problem and break it into three parts:
Any advices are welcome :D |
Correct.
It should be the same as the input tensor's, following the principle of device propagation from input to output:
If the argument is a pure-python array-like (e.g. a list or scalar), it should be the same as the default device.
Correct. |
I see, thanks for your explanation. But I have two questions about this:
|
All functions should behave the same.
If you would rather retain the current behaviour due to backwards compatibility concerns, we can wrap |
I went on and tested all the functions you listed.
These work fine today, as there is no option to pass an input array:
These work fine today, as the input's device is correctly propagated to the output:
This DOES NOT propagate the input's device when the user has set the default device:
These are not covered by the Array API standard so I don't have an opinion:
|
As the title stated. Related Issue: pytorch/pytorch#150199 ghstack-source-id: 1000e3d Pull Request resolved: pytorch/pytorch#150385
Uh oh!
There was an error while loading. Please reload this page.
🐛 Describe the bug
The documentation of
asarray
states:The described behaviour is coherent with the specification of the Array API standard.
This works as expected in practice:
asarray(x, device=None)
, propagates the device ofx
to the output - unless the user sets the default device (even if just to explicitly state the current value). After that,asarray(x, device=None)
disregards the device ofx
and converts everything to the default device.Versions
pytorch 2.6.0 conda-forge linux intel
cc @mruberry @rgommers @asmeurer @leofang @AnirudhDagar @asi1024 @emcastillo @kmaehashi @albanD
The text was updated successfully, but these errors were encountered: