Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: StridedMemoryView fails to wrap NumPy array #473

Open
carterbox opened this issue Feb 26, 2025 · 1 comment
Open

BUG: StridedMemoryView fails to wrap NumPy array #473

carterbox opened this issue Feb 26, 2025 · 1 comment
Labels
triage Needs the team's attention

Comments

@carterbox
Copy link

carterbox commented Feb 26, 2025

I'm using numpy 2.0.2 and cuda-core 0.1.1. I cannot successfully convert a numpy array into a StridedMemoryView. My understanding is that this should be possible because numpy arrays implement the dlpack interface.

import nvmath  # used to patch cupy imports of nvidia-wheels
import numpy as np
import cuda.core.experimental as ccx
import cupy as cp
import torch

host_array = np.ones(50)
device_array = cp.ones(50)

device = ccx.Device()
device.set_current()

torch_array = torch.ones(50)

stream = device.create_stream()

# StridedMemoryView cannot wrap NumPy arrays because of impossible constraints on the
# stream_ptr parameter.

# Try 1: Provide a valid stream pointer
print(host_array)
wrapped_host_array = ccx.utils.StridedMemoryView(host_array, stream_ptr=stream.handle)
# RuntimeError: NumPy only supports stream=None.
print(wrapped_host_array)

# Try 2: Provide no stream pointer
print(host_array)
wrapped_host_array = ccx.utils.StridedMemoryView(host_array, stream_ptr=None)
# BufferError: stream=None is ambiguous with view()
print(wrapped_host_array)

print(torch_array)
wrapped_torch_array = ccx.utils.StridedMemoryView(torch_array, stream_ptr=None)
# BufferError: stream=None is ambiguous with view()
print(wrapped_torch_array)

# The following work

print(torch_array)
wrapped_torch_array = ccx.utils.StridedMemoryView(torch_array, stream_ptr=stream.handle)
print(wrapped_torch_array)

torch_array = torch_array.to('cuda')

print(torch_array)
wrapped_torch_array = ccx.utils.StridedMemoryView(torch_array, stream_ptr=stream.handle)
print(wrapped_torch_array)

print(device_array)
wrapped_device_array = ccx.utils.StridedMemoryView(device_array, stream_ptr=stream.handle)
print(wrapped_device_array)
@github-actions github-actions bot added the triage Needs the team's attention label Feb 26, 2025
@leofang
Copy link
Member

leofang commented Feb 27, 2025

Could you try passing stream_ptr=-1?

def test_strided_memory_view_cpu(self, in_arr):
# stream_ptr=-1 means "the consumer does not care"
view = StridedMemoryView(in_arr, stream_ptr=-1)
self._check_view(view, in_arr)

It is a documented deviation from the __dlpack__ interface:
To opt-out of the stream ordering operation in either DLPack or CAI,
please pass ``stream_ptr=-1``. Note that this deviates (on purpose)
from the semantics of ``obj.__dlpack__(stream=None, ...)`` since ``cuda.core``
does not encourage using the (legacy) default/null stream, but is
consistent with the CAI's semantics. For DLPack, ``stream=-1`` will be
internally passed to ``obj.__dlpack__()`` instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Needs the team's attention
Projects
None yet
Development

No branches or pull requests

2 participants