Skip to content

asarray: device does not propagate from input to output after set_default_deviceΒ #150199

@crusaderky

Description

@crusaderky

πŸ› Describe the bug

The documentation of asarray states:

device (torch.device, optional) – the device of the returned tensor. Default: None, which causes the device of obj to be used. Or, if obj is a Python sequence, the current default device will be used.

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 of x 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 of x and converts everything to the default device.

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).get_device()
Out[4]: 0  # OK

In [5]: torch.set_default_device('cpu')

In [6]: torch.asarray(x).get_device()
Out[6]: -1  # KO

Versions

pytorch 2.6.0 conda-forge linux intel

cc @mruberry @rgommers @asmeurer @leofang @AnirudhDagar @asi1024 @emcastillo @kmaehashi @albanD

Metadata

Metadata

Assignees

Labels

module: python array apiIssues related to the Python Array APImodule: python frontendFor issues relating to PyTorch's Python frontendtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions