array-api-compat icon indicating copy to clipboard operation
array-api-compat copied to clipboard

xp.eye(..., device=device) has not effect when xp is cupy

Open PascalCarrivain opened this issue 6 months ago • 3 comments

Dear all,

I run the following command when xp is cupy

# cupy
x = cupy.asarray([1.0] * 3)
xp = array_api_compat.array_namespace(x)
E = xp.eye(3, 3, k=0, dtype='float64', device=1)
print(E.device)
with xp.cuda.Device(1):
    E = xp.eye(3, 3, k=0, dtype='float64', device=1)
    print(E.device)
# torch
x = torch.as_tensor([1.0] * 3)
xp = array_api_compat.array_namespace(x)
E = xp.eye(3, 3, k=0, dtype=torch.float64, device='cuda:1')
print(E.device)

and I get the result:

<CUDA Device 0>
<CUDA Device 1>
cuda:1

It seems that device argument of xp.eye has no effect here when xp is cupy. It seems to work well when xp is torch. Did I miss something?

Thank you.

array-api-compat 1.12.0 cupy-cuda12x 13.4.1

PascalCarrivain avatar Jun 13 '25 09:06 PascalCarrivain

Yes, this is unfortunately a known issue with CuPy that it requires a context manager. NOt sure if there are plans to change that, maybe @leofang could weight in?

ev-br avatar Jun 16 '25 19:06 ev-br

Naively I'd have thought that array-api-compat is the place where we should be inserting that context manager for users when they pass device= as argument. Basically, to smooth out the differences between the different libraries. A bit like what is done in asarray

https://github.com/data-apis/array-api-compat/blob/6c708d13e826fb850161babf34f306fe80cae875/array_api_compat/cupy/_aliases.py#L65-L87

betatim avatar Jun 17 '25 08:06 betatim

Potentially, yes. The key question ATM is what's the cupy team plan. If this is something they plan to fix, we can either wait or potentially help materializing for cupy 14.x.

ev-br avatar Jun 17 '25 09:06 ev-br