Skip to content

ENH: Add device= arguments to array creation functions #9848

@ev-br

Description

@ev-br

Description

Array API spec stipulates that array creation functions (asarray, arange, ones etc) have a device= argument to specify the target device. CuPy is currently incompatible with the Array API spec in that CuPy functions do not have this argument.
Instead, the user guide recommends context managers:

>>> with cupy.cuda.Device(1):
...    x = cupy.arange(3)
>>> x.device
<CUDa Device 1>

Mixing the notion of a global default device with per-function arguments leads to issues like

with cp.cuda.Device(1):
    y = cp.asarray(1, device=cp.cuda.Device(0))  # y should be on Device(0)
    z = y + 1  # device of z?

There were several discussions in the Array API spaces: [1], [2] and [3]. With [1] hinting that potentially CuPy could favor the per-function device arguments.

[1] data-apis/array-api-compat#293 (comment)
[2] data-apis/array-api-compat#337
[3] https://hackmd.io/zn5bvdZTQIeJmb3RW1B-8g#Meeting-minutes-17-April-2025

Additional Information

Prior art: pytorch allows context managers, too, and explicit device= arguments take precedence:

In [7]: with torch.device("cuda"):
   ...:     y = torch.arange(3, device="cpu")
   ...:     z = y + 1

In [8]: z.device
Out[8]: device(type='cpu')

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions