-
-
Notifications
You must be signed in to change notification settings - Fork 1k
cupy.array_api.linalg.vecdot deviates from the standard Array API specification #8747
Copy link
Copy link
Closed
Labels
Description
Description
cupy.array_api.linalg.vecdot does not behave the same as np.linalg.vecdot or jax.numpy.linalg.vecdot (or even cupy.dot) even when identical arguments are passed to these functions. The cupy.array_api function throws a shape error (ValueError) in the example below, whereas the other implementations don't.
I am aware that there are subtle differences between NumPy and CuPy, so I am not sure if this is a bug or if this is intended. It looks like a bug as passing the same arguments to cupy.dot works, but cupy.array_api.linalg.vecdot(..., axis=1) (should behave as dot) doesn't.
To Reproduce
Array API compatible NumPy implementation (works)
import numpy as np
rng = np.random.default_rng(seed=42)
a = rng.standard_normal((100, 20))
b = rng.standard_normal((100,))
_, n = a.shape
x = np.zeros(n)
w = np.linalg.vecdot(b - a @ x, a, axis=0) # worksArray API compatible JAX implementation (works)
import numpy as np
import jax.numpy as jnp
rng = np.random.default_rng(seed=42)
a = rng.standard_normal((100, 20))
b = rng.standard_normal((100,))
a_j = jnp.array(a)
b_j = jnp.array(b)
_, n_j = a_j.shape
x_j = jnp.zeros(n_j)
w_j = jnp.linalg.vecdot(b_j - a_j @ x_j, a_j, axis=0) # worksArray API non-compatible CuPy (works)
Using dot instead of linalg.vecdot(..., axis=1) as the latter does not exist -
import numpy as np
import cupy
rng = np.random.default_rng(seed=42)
a = rng.standard_normal((100, 20))
b = rng.standard_normal((100,))
a_cu = cupy.asarray(a)
b_cu = cupy.asarray(b)
_, n_cu = a_cu.shape
x_cu = cupy.zeros(n_cu)
w_cu = cupy.dot(b_cu - a_cu @ x_cu, a_cu) # worksArray API compatible CuPy (does not work)
import numpy as np
import cupy.array_api as cp
rng = np.random.default_rng(seed=42)
a = rng.standard_normal((100, 20))
b = rng.standard_normal((100,))
a_c = cp.asarray(a)
b_c = cp.asarray(b)
_, n_c = a_c.shape
x_c = cp.zeros(n_c)
w_c = cp.linalg.vecdot(b_c - a_c @ x_c, a_c, axis=0) # does not work
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[48], line 1
----> 1 w_c = cp.linalg.vecdot(b_c - a_c @ x_c, a_c, axis=0)
File /mnt/c/Users/Saransh/Saransh_softwares/OpenSource/Python/glass/.env/lib/python3.10/site-packages/cupy/array_api/linalg.py:385, in vecdot(x1, x2, axis)
383 x2_shape = (1,)*(ndim - x2.ndim) + tuple(x2.shape)
384 if x1_shape[axis] != x2_shape[axis]:
--> 385 raise ValueError("x1 and x2 must have the same size along the given axis")
387 x1_, x2_ = np.broadcast_arrays(x1._array, x2._array)
388 x1_ = np.moveaxis(x1_, axis, -1)
ValueError: x1 and x2 must have the same size along the given axisInstallation
None
Environment
OS : Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python Version : 3.10.12
CuPy Version : 13.3.0
CuPy Platform : NVIDIA CUDA
NumPy Version : 2.1.3
SciPy Version : 1.14.1
Cython Build Version : 0.29.36
Cython Runtime Version : None
CUDA Root : /usr/local/cuda
nvcc PATH : /usr/local/cuda/bin/nvcc
CUDA Build Version : 12060
CUDA Driver Version : 12050
CUDA Runtime Version : 12060 (linked to CuPy) / 12060 (locally installed)
CUDA Extra Include Dirs : []
cuBLAS Version : (available)
cuFFT Version : 11300
cuRAND Version : 10307
cuSOLVER Version : (11, 7, 1)
cuSPARSE Version : (available)
NVRTC Version : (12, 6)
Thrust Version : 200600
CUB Build Version : 200600
Jitify Build Version : <unknown>
cuDNN Build Version : (not loaded; try `import cupy.cuda.cudnn` first)
cuDNN Version : (not loaded; try `import cupy.cuda.cudnn` first)
NCCL Build Version : (not loaded; try `import cupy.cuda.nccl` first)
NCCL Runtime Version : (not loaded; try `import cupy.cuda.nccl` first)
cuTENSOR Version : None
cuSPARSELt Build Version : None
Device 0 Name : NVIDIA GeForce GTX 1650 Ti
Device 0 Compute Capability : 75
Device 0 PCI Bus ID : 0000:01:00.0
Additional Information
No response
Reactions are currently unavailable