Skip to content

[BUG]: cuda-core 1.2.0 breaks Buffer use for non-CUDA memory implementations #2769

Description

@carterbox

Is this a duplicate?

Type of Bug

Runtime Error

Component

cuda.core

Describe the bug

nvmath-python provides NVPL-backed host APIs; this means that we run in environments where the CUDA driver is not present or the context is not initialized.

I have implemented a MemoryResource sub-class that uses Numpy as the allocator. This is because I want to use the same interfaces for both CUDA and Non-CUDA when performing internal memory allocations.

Starting in cuda-core 1.2.0, it is now not possible to inherit from MemoryResource/Buffer without running cuInit? For example, I get the following error when running tests that do not use CUDA.

nvmath/_internal/workspace.py:519: in allocate
return Buffer.from_handle(ptr=ptr, size=size, mr=self)
cuda/core/_memory/_buffer.pyx:379: in cuda.core._memory._buffer.Buffer.from_handle
???
cuda/core/_memory/_buffer.pyx:310: in cuda.core._memory._buffer.Buffer._init
???
cuda/core/_utils/cuda_utils.pxd:23: in cuda.core._utils.cuda_utils.HANDLE_RETURN
???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
> ???
E cuda.core._utils.cuda_utils.CUDAError: CUDA_ERROR_NOT_INITIALIZED: This indicates that the CUDA driver has not been initialized with cuInit() or that initialization has failed.

How to Reproduce

importnumpyasnpfromcuda.coreimportMemoryResource, Buffer, StreamclassNumpyMemoryResource(MemoryResource):
"""Host :class:`cuda.core.MemoryResource` backed by :func:`numpy.empty`. Allocates plain (non-pinned) host memory via NumPy. Unlike :class:`PinnedMemoryResource` / :class:`LegacyPinnedMemoryResource`, this resource does not call into the CUDA driver, so it is safe to use in environments where no CUDA driver/device is available (CPU-only test runs, pure-host scratch space for parameter struct staging, etc.). Pair with ``Workspace(NumpyMemoryResource(), logger)`` and pass ``stream_holder=None`` to ``allocate_perhaps`` for a fully CUDA-free path (``device_id`` is read from the resource and is ``-1``). Not suitable as the host half of a :class:`MirroredWorkspace`: ``cudaMemcpyAsync`` requires page-locked memory for asynchronous correctness. """def__init__(self) ->None:
# Strong refs keyed by ptr — Buffer carries only the int pointer.self._held: dict[int, np.ndarray] = {}
defallocate(self, size: int, *, stream: Stream|None=None) ->Buffer:
arr=np.empty(size, dtype=np.uint8)
ptr=int(arr.ctypes.data)
self._held[ptr] =arrreturnBuffer.from_handle(ptr=ptr, size=size, mr=self)
defdeallocate(self, ptr: int, size: int, stream: Stream|None=None) ->None:
# `stream` must NOT be keyword-only: cuda.core < 1 calls deallocate with all# arguments positionally (Buffer.close -> mr.deallocate(ptr, size, stream)).self._held.pop(int(ptr), None)
@propertydefis_host_accessible(self) ->bool:
returnTrue@propertydefis_device_accessible(self) ->bool:
returnFalse@propertydefis_managed(self) ->bool:
returnFalse@propertydefdevice_id(self) ->int:
return-1if__name__=="__main__":
mr=NumpyMemoryResource()
b=mr.allocate(64, stream=None)

Expected behavior

No runtime error from Buffer constructor if the CUDA runtime hasn't been initialized.

Operating System

WSL2 Ubuntu 24.04

nvidia-smi output

+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 595.79 Driver Version: 596.59 CUDA Version: 13.2 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA RTX 1000 Ada Gene... On | 00000000:01:00.0 Off | N/A |
| N/A 52C P3 11W / 44W | 0MiB / 6141MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions