cuda.core#
2026-09-24
7 min read time
Advanced Micro Devices, Inc.
Minimal cuda.core compatibility shim, backed by HIP.
This is NOT a full port of NVIDIA’s cuda.core / cuda.core.experimental
package. It implements just enough of the high-level surface for HIP ports of
CUDA-Python consumers: a Device exposing uuid, the Stream
vocabulary type behind the CUDA stream protocol, and stream-ordered device
allocation through DeviceMemoryResource and Buffer.
Everything is implemented on top of the high-level rocm.bindings.hip HIP
runtime API. Additional high-level abstractions (Event, Program, Linker, …)
are intentionally out of scope and can be added on demand.
Classes#
HIP-backed stand-in for |
|
HIP-backed stand-in for |
|
HIP-backed stand-in for |
|
HIP-backed stand-in for |
Package Contents#
- class cuda.core.Device(device_id=None)#
HIP-backed stand-in for
cuda.core.Device.Only the members required by current consumers are implemented. Passing no
device_idselects the current device (viahipGetDevice).- property uuid: str#
Device UUID as a
GPU-<hex>string.Backed by
hipDeviceGetUuid. ROCm stores the UUID as ASCII text in the 16-byte field (e.g.b"50a437de77a657b8"), so the result matches theGPU-<hex>string reported byrocminfo/amd-smi. Falls back to a raw hex rendering if a runtime ever returns non-text bytes.
- set_current()#
- sync()#
- property default_stream#
The NULL stream, as a non-owning
Streamtoken.HIP has no per-thread default stream selected by an environment variable the way CUDA Python’s
CUDA_PYTHON_CUDA_PER_THREAD_DEFAULT_STREAMdoes, so this is always the legacy NULL stream. The token is created once perDeviceso that repeated reads compare equal.
- create_stream(obj=None)#
Create a HIP stream, or wrap a foreign one.
With no argument a new non-blocking stream is created — non-blocking being the
cuda.coredefault — and destroyed again when the returnedStreamis closed or collected. HIP associates a new stream with the current device rather than with the device this method was called on, so the two must agree.Passing
objinstead wraps an existing stream from any library implementing the CUDA stream protocol. The wrapper keepsobjalive and never destroys the borrowed stream.
- class cuda.core.Stream(*args, **kwargs)#
HIP-backed stand-in for
cuda.core.Stream.Instances come from
Device.create_stream()orDevice.default_stream. As incuda.core, constructing one directly is refused: whether the stream would be owned or borrowed is ambiguous, and the two differ in what closing them does.- property handle#
The underlying
hipStream_t.As in
cuda.core, this is a Python object; useint()on it to get the address of the C handle.
- sync()#
Block until all work queued on this stream has completed.
- close()#
Destroy an owned stream, or drop the reference to a borrowed one.
- class cuda.core.Buffer(*args, **kwargs)#
HIP-backed stand-in for
cuda.core.Buffer.Instances come from
DeviceMemoryResource.allocate(), and hold on to the stream that allocated them: closing without naming a stream frees the allocation in the order it was created, and a stream-ordered free onto an already destroyed stream would not be valid.- property handle#
The allocation, as the
DeviceArrayHIP returned for it.As in
cuda.core, this is a Python object; useint()on it to get the device address.
- property size#
Size of the allocation in bytes.
- close(stream=None)#
Free the allocation, ordered on
stream.streamdefaults to the one that allocated the buffer. The free is stream-ordered, so it takes effect once preceding work on that stream has run, not when this call returns.
- class cuda.core.DeviceMemoryResource(device_id)#
HIP-backed stand-in for
cuda.core.DeviceMemoryResource.Allocations are stream-ordered and come from the device’s current HIP memory pool, which is the device’s default pool unless one has been set with
hipDeviceSetMemPool. The pool therefore outlives this object and is never destroyed by it, matching howcuda.coretreats a memory resource built over a pool it does not own.- property device_id#
Ordinal of the device whose pool backs this resource.
- property handle#
The underlying
hipMemPool_t.
- allocate(size, stream=None)#
Allocate
sizebytes from the pool, ordered onstream.The allocation is only safe to access once preceding work on
streamhas run.streamdefaults to the NULL stream.
- deallocate(ptr, size, stream=None)#
Return
ptrto the pool, ordered onstream.