-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Reproducer:
The motivation for this reproducer is to mimic a user application code on aurora with cupy.ndarray constructor with memptr argument which I am not sure how to access it for dpnp. I've tried to mimic memptr from cupy to buffer arg in dpnp.ndarray. @antonwolfy Sorry about tagging in a wrong issue. Here is the reproducer for this [discussion](https://github.com/IntelPython/dpnp/issues/2486#issuecomment-3075387410)
The end goal is to use dpnp.ndarray.data.ptr to cast it to a void_p using cast. The error is this:
Traceback (most recent call last):
File "/lus/flare/projects/NWChemEx_aesp_CNDA/abagusetty/gpu4pyscf/test_dpnp_dataptr.py", line 58, in <module>
print(out.data) # This line will raise the TypeError
File "/lus/flare/projects/NWChemEx_aesp_CNDA/abagusetty/gpu4pyscf/dpnp/dpnp/dpnp_array.py", line 1030, in data
return dpm.create_data(self._array_obj)
File "/lus/flare/projects/NWChemEx_aesp_CNDA/abagusetty/gpu4pyscf/dpnp/dpnp/memory/_memory.py", line 105, in create_data
raise TypeError(f"Expected USM memory, but got {type(usm_data)}")
TypeError: Expected USM memory, but got <class 'dpnp.memory._memory.MemoryUSMDevice'>import dpnp
import numpy as np
import types
# --- Monkey patch cupy.ndarray to mimic memory handling via CuPy-like interface ---
class CuPyNdarrayWrapper:
def __call__(self, shape, dtype=np.float64, memptr=None):
if memptr is not None:
if hasattr(memptr, 'get_array'):
memptr = memptr.get_array()
# Try to reuse buffer for dpnp
return dpnp.ndarray(shape, dtype=dtype, buffer=memptr)
else:
return dpnp.ndarray(shape, dtype=dtype)
def __instancecheck__(self, instance):
return isinstance(instance, dpnp.dpnp_array.dpnp_array)
def __subclasscheck__(self, subclass):
return issubclass(subclass, dpnp.dpnp_array.dpnp_array)
# --- Fake "cupy" module ---
import sys
cupy_fake = types.ModuleType("cupy")
cupy_fake.ndarray = CuPyNdarrayWrapper()
sys.modules["cupy"] = cupy_fake
# --- Simulate user logic ---
comp = 1
nao_max = 59
MIN_BLK_SIZE = 4096
nao_sub = 58
ip0 = 0
ip1 = 4096
# Allocate initial buffer in dpnp (this uses USMDevice memory internally)
buf = dpnp.empty((comp, nao_max, MIN_BLK_SIZE), order='C')
# This is where the problem is triggered — we pass a USMDevice buffer directly
out = cupy_fake.ndarray((comp, nao_sub, ip1 - ip0), memptr=buf.data)
# Accessing .data will internally call dpnp → dpm.create_data()
print(out)
print(out.data) # This line will raise the TypeErrorMetadata
Metadata
Assignees
Labels
No labels