Skip to content

Can't use cupy.ndarray's __setitem__ when the value is an array-like object that implements __cuda_array_interface__ or __cupy_get_ndarray__ but also implements __eq__. #9089

@ikrommyd

Description

@ikrommyd

Description

Can't use cupy.ndarray's __setitem__ when the value is an array-like object that implements __cuda_array_interface__ or __cupy_get_ndarray__ but also implements __eq__.

The reproducer below works with numpy

x = Array(np.array([1, 1, 2]))
y = np.array([1, 2, 3])
y[:] = x
y

So I expect it to work for cupy as well and not raise

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[33], line 28
     26 x = Array(cp.array([1, 1, 2]))
     27 y = cp.array([1, 2, 3])
---> 28 y[:] = x
     29 y

File cupy/_core/core.pyx:1641, in cupy._core.core._ndarray_base.__setitem__()

File cupy/_core/_routines_indexing.pyx:51, in cupy._core._routines_indexing._ndarray_setitem()

File cupy/_core/_routines_indexing.pyx:1017, in cupy._core._routines_indexing._scatter_op()

File cupy/_core/core.pyx:789, in cupy._core.core._ndarray_base.fill()

File cupy/_core/core.pyx:1293, in cupy._core.core._ndarray_base.__nonzero__()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

To Reproduce

import numpy as np
import cupy as cp

class Array:
    def __init__(self,  data):
        self.data = data

    def __array__(self, *args, **kwargs):
        return self.data.__array__(*args, **kwargs)

    def __cupy_get_ndarray__(self, *args, **kwargs):
        return self.data

    @property
    def __array_interface__(self):
        return self.data.__array_interface__

    @property
    def __cuda_array_interface__(self):
        return self.data.__cuda_array_interface__

    def __eq__(self, other):
        return self.data == other


x = Array(cp.array([1, 1, 2]))
y = cp.array([1, 2, 3])
y[:] = x
y

Deleting the __eq__ implementation works fine

import numpy as np
import cupy as cp

class Array:
    def __init__(self,  data):
        self.data = data

    def __array__(self, *args, **kwargs):
        return self.data.__array__(*args, **kwargs)

    def __cupy_get_ndarray__(self, *args, **kwargs):
        return self.data

    @property
    def __array_interface__(self):
        return self.data.__array_interface__

    @property
    def __cuda_array_interface__(self):
        return self.data.__cuda_array_interface__


x = Array(cp.array([1, 1, 2]))
y = cp.array([1, 2, 3])
y[:] = x
y

and returns array([1, 1, 2])

Installation

Wheel (pip install cupy-***)

Environment

OS                           : Linux-6.11.0-21-generic-x86_64-with-glibc2.39
Python Version               : 3.12.9
CuPy Version                 : 13.4.1
CuPy Platform                : NVIDIA CUDA
NumPy Version                : 2.2.4
SciPy Version                : 1.15.2
Cython Build Version         : 3.0.12
Cython Runtime Version       : None
CUDA Root                    : /usr/local/cuda
nvcc PATH                    : /usr/local/cuda/bin/nvcc
CUDA Build Version           : 12080
CUDA Driver Version          : 12080
CUDA Runtime Version         : 12080 (linked to CuPy) / 12080 (locally installed)
CUDA Extra Include Dirs      : []
cuBLAS Version               : 120804
cuFFT Version                : 11303
cuRAND Version               : 10309
cuSOLVER Version             : (11, 7, 3)
cuSPARSE Version             : 12508
NVRTC Version                : (12, 8)
Thrust Version               : 200800
CUB Build Version            : 200800
Jitify Build Version         : <unknown>
cuDNN Build Version          : None
cuDNN Version                : None
NCCL Build Version           : None
NCCL Runtime Version         : None
cuTENSOR Version             : 20200
cuSPARSELt Build Version     : None
Device 0 Name                : NVIDIA GeForce RTX 4080 SUPER
Device 0 Compute Capability  : 89
Device 0 PCI Bus ID          : 0000:01:00.0

Additional Information

No response

Metadata

Metadata

Assignees

Labels

cat:enhancementImprovements to existing featuresprio:highto-be-backportedPull-requests to be backported to stable branch

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions