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

Request: Support for cuPyNumeric

Open TimothyEDawson opened this issue 4 months ago • 6 comments

cuPyNumeric aims to be a drop-in replacement for NumPy, with full API compatibility achieved by automatically falling back to NumPy for any functionality which isn't yet implemented.

I suspect this should make it a trivial addition to array-api-compat, although the way it falls back to NumPy may need to be explicitly handled as it returns NumPy ndarrays from any function marked with a dot in this table: https://docs.nvidia.com/cupynumeric/latest/api/comparison.html

For example:

>>> import cupynumeric as xp
>>> from array_api_compat import is_numpy_array
>>> 
>>> a = xp.linspace(0.0, 1.0, 21)
>>> is_numpy_array(a)
False
>>> b = xp.acos(a)
<cupynumeric internals>:241: RuntimeWarning: cuPyNumeric has not implemented numpy.acos and is falling back to canonical NumPy. You may notice significantly decreased performance for this function call.

Set CUPYNUMERIC_FALLBACK_STACKTRACE=1 and re-run to include a full stack trace with this warning.
>>> is_numpy_array(b)
True
>>>

I don't know if there's a preferred method to do so, but it's not difficult to programmatically check which functions do not yet have a native implementation. E.g.

>>> xp.acos.__module__
'numpy'
>>> xp.cos.__module__
'cupynumeric._ufunc.ufunc'

TimothyEDawson avatar Sep 05 '25 16:09 TimothyEDawson

@seberg any thoughts?

lucascolley avatar Sep 05 '25 17:09 lucascolley

I think this has been discussed in cupynumeric, but not sure of the current status/priority. I'll bring it up with the cupynumeric team. Not sure what is best, in practice cupynumeric should be easy since it is basically NumPy compatible (I suppose there will be some subtleties when actually testing, although I doubt they will be big issues in practice).

In principle we could of course also do it here as it should be effectively a copy paste of the NumPy version.

seberg avatar Sep 05 '25 20:09 seberg

I believe this is a bug rather than unimplemented feature. There was a fix for that in 25.08.rc1 package, however we reverted it due to the performance regression. We will discuss how to resolve this internally and update here

ipdemes avatar Sep 05 '25 21:09 ipdemes

I believe this is a bug rather than unimplemented feature. There was a fix for that in 25.08.rc1 package, however we reverted it due to the performance regression. We will discuss how to resolve this internally and update here

What bug are you referring to, specifically? Returning numpy arrays instead of cuPyNumeric arrays?

I would also note that the array objects in cuPyNumeric 25.8.0 (i.e. the latest I was able to obtain from PyPI) do not have an __array_namespace__ attribute, but perhaps they are otherwise Array API compliant? I haven't looked into that, personally.

TimothyEDawson avatar Sep 05 '25 22:09 TimothyEDawson

Returning numpy arrays instead of cuPyNumeric arrays?

Yes, this is what I am referring to

ipdemes avatar Sep 05 '25 22:09 ipdemes

Integrating https://github.com/data-apis/array-api-tests into cuPyNumeric's CI would be the logical next step, I think.

lucascolley avatar Sep 05 '25 23:09 lucascolley