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
Description
Array API spec stipulates that array creation functions (
asarray,arange,onesetc) have adevice=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:
Mixing the notion of a global default device with per-function arguments leads to issues like
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:No response