Skip to content

NEP-18 not working #15

@leofang

Description

@leofang

I am testing the compatibility with CuPy and I notice this error:

import cupy as cp
import numpy as np
import autocorr


N = 10240
np.random.seed(0)
t = np.arange(N)
A = np.exp(-0.05 * t)[:, np.newaxis] + np.random.rand(N, 24) * 0.1

A_cp = cp.asarray(A)
g1, tau = autocorr.multitau(A_cp.T, 16)
Traceback (most recent call last):
  File "test_autocorr_cupy.py", line 13, in <module>
    g1, tau = autocorr.multitau(A_cp.T, 16)
  File "/home/leofang/autocorr/autocorr/multitau.py", line 57, in multitau
    g2[:, i] = np.mean(a[:, :N - i] * a[:, i:], axis=1) / t1 / t2
ValueError: object __array__ method not producing an array

The reasons is that internally multitau allocates two arrays g2 and tau using NumPy API, which is not (yet) smart enough to dispatch to CuPy.

I tried to refresh my memory on NEP-18, but I don't think this issue was addressed there. Looks like to accommodate different NEP-18 compliant arrays, we need to detect the array source and call the corresponding array-creation API?

if isinstance(input, numpy.ndarray):
    g2 = numpy.empty(...)
elif isinstance(input, cupy.ndarray):
    g2 = cupy.empty(...)
elif  # dask? sparse?

I may have missed something obvious, though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions