Files
nvdriver/tools/vm/cuda12_workload_matrix.py

568 lines
24 KiB
Python
Executable File

#!/usr/bin/env python3
"""CUDA 12 workload parity matrix for GSP-RM / Pre-GSP GPUs (Ampere/Ada/Volta).
Exercises real userspace CUDA 12 libraries (libcuda, libcudart, libcublas, libcublasLt,
and optionally libggml-cuda / libnvidia-ml) against nvidia_rs.ko:
1. cuBLAS FP32 SGEMM + FP16 & BF16 Tensor Core cublasGemmEx
2. cuBLASLt FP32 Matmul
3. Pinned host memory (cudaMallocHost) + multi-stream concurrency + events
4. CUDA Graphs capture, instantiate, and 5x replay
5. Stream-ordered memory allocator (cudaMallocAsync / cudaFreeAsync)
6. CUDA Virtual Memory Management (cuMemCreate / cuMemAddressReserve / cuMemMap / cuMemSetAccess)
7. Unified / Managed Memory (cudaMallocManaged + GPU kernel + cudaStreamAttachMemAsync
Single/Global/Host + cudaMemAdvise + cudaMemPrefetchAsync — exercises UVM_SET_RANGE_GROUP)
8. 2D pitched memory (cudaMallocPitch / cudaMemcpy2D)
9. Host-registered zero-copy mapped memory (cudaHostRegister / cudaHostGetDevicePointer)
10. 4-process concurrent cuBLAS execution
11. Cross-process CUDA IPC memory & event sharing (cuIpcGetMemHandle / cuIpcOpenMemHandle,
cuIpcGetEventHandle / cuIpcOpenEventHandle)
12. Optional GGML CUDA (llama.cpp / Ollama) device & memory discovery + NVML telemetry
"""
import ctypes
import multiprocessing
import os
import sys
SEARCH_LIB_DIRS = (
"/usr/local/cuda/lib64",
"/usr/local/lib/ollama/cuda_v12",
"/usr/local/lib/ollama",
"/usr/lib/x86_64-linux-gnu",
)
def _find_lib(candidates):
for name in candidates:
try:
return ctypes.CDLL(name)
except OSError:
pass
for d in SEARCH_LIB_DIRS:
path = os.path.join(d, name)
if os.path.exists(path):
try:
return ctypes.CDLL(path)
except OSError:
pass
raise RuntimeError(f"Could not load any of {candidates}")
class CUipcMemHandle(ctypes.Structure):
_fields_ = [("reserved", ctypes.c_uint8 * 64)]
class CUipcEventHandle(ctypes.Structure):
_fields_ = [("reserved", ctypes.c_uint8 * 64)]
class CUmemLocation(ctypes.Structure):
_fields_ = [("type", ctypes.c_int), ("id", ctypes.c_int)]
class CUmemAllocationProp(ctypes.Structure):
_fields_ = [
("type", ctypes.c_int),
("requestedHandleTypes", ctypes.c_int),
("location", CUmemLocation),
("win32HandleMetaData", ctypes.c_void_p),
("allocFlags_comp", ctypes.c_uint8),
("allocFlags_gpuDirect", ctypes.c_uint8),
("allocFlags_usage", ctypes.c_uint16),
("allocFlags_rsv", ctypes.c_uint8 * 4),
]
class CUmemAccessDesc(ctypes.Structure):
_fields_ = [("location", CUmemLocation), ("flags", ctypes.c_int)]
def _mp_worker(idx, q):
try:
rt = _find_lib(("libcudart.so.12", "libcudart.so"))
blas = _find_lib(("libcublas.so.12", "libcublas.so"))
assert rt.cudaSetDevice(0) == 0
h = ctypes.c_void_p()
assert blas.cublasCreate_v2(ctypes.byref(h)) == 0
n = 128
sz = n * n * 4
d_a, d_b, d_c = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
assert rt.cudaMalloc(ctypes.byref(d_a), sz) == 0
assert rt.cudaMalloc(ctypes.byref(d_b), sz) == 0
assert rt.cudaMalloc(ctypes.byref(d_c), sz) == 0
val = float(idx + 2)
h_a = (ctypes.c_float * (n * n))(*[val] * (n * n))
h_b = (ctypes.c_float * (n * n))(*[1.0] * (n * n))
h_c = (ctypes.c_float * (n * n))()
assert rt.cudaMemcpy(d_a, h_a, sz, 1) == 0
assert rt.cudaMemcpy(d_b, h_b, sz, 1) == 0
alpha, beta = ctypes.c_float(1.0), ctypes.c_float(0.0)
rc = blas.cublasSgemm_v2(
h, 0, 0, n, n, n, ctypes.byref(alpha), d_a, n, d_b, n, ctypes.byref(beta), d_c, n
)
assert rc == 0, f"cublasSgemm_v2={rc}"
assert rt.cudaDeviceSynchronize() == 0
assert rt.cudaMemcpy(h_c, d_c, sz, 2) == 0
assert abs(h_c[0] - val * n) < 1e-2, f"worker {idx}: C0={h_c[0]}"
rt.cudaFree(d_a)
rt.cudaFree(d_b)
rt.cudaFree(d_c)
blas.cublasDestroy_v2(h)
q.put((idx, "OK", h_c[0]))
except Exception as exc:
q.put((idx, f"ERR:{exc}", 0.0))
def _ipc_child(mbytes, ebytes, q):
try:
cu = _find_lib(("libcuda.so.1", "libcuda.so"))
cu.cuIpcOpenMemHandle_v2.argtypes = [
ctypes.POINTER(ctypes.c_uint64),
CUipcMemHandle,
ctypes.c_uint,
]
cu.cuIpcOpenEventHandle.argtypes = [ctypes.POINTER(ctypes.c_void_p), CUipcEventHandle]
assert cu.cuInit(0) == 0
dev = ctypes.c_int()
assert cu.cuDeviceGet(ctypes.byref(dev), 0) == 0
ctx = ctypes.c_void_p()
assert cu.cuCtxCreate_v2(ctypes.byref(ctx), 0, dev) == 0
mh = CUipcMemHandle()
ctypes.memmove(ctypes.byref(mh), mbytes, 64)
dptr = ctypes.c_uint64()
rc = cu.cuIpcOpenMemHandle_v2(ctypes.byref(dptr), mh, 1)
assert rc == 0, f"cuIpcOpenMemHandle_v2={rc}"
out = (ctypes.c_uint32 * 1024)()
assert cu.cuMemcpyDtoH_v2(out, dptr, 4096) == 0
assert out[0] == 0xBEEF0000 and out[1023] == 0xBEEF03FF, f"ipc read={hex(out[0])}"
src = (ctypes.c_uint32 * 1024)(*[0xFEED0000 + i for i in range(1024)])
assert cu.cuMemcpyHtoD_v2(dptr, src, 4096) == 0
assert cu.cuIpcCloseMemHandle(dptr) == 0
eh = CUipcEventHandle()
ctypes.memmove(ctypes.byref(eh), ebytes, 64)
ev = ctypes.c_void_p()
assert cu.cuIpcOpenEventHandle(ctypes.byref(ev), eh) == 0
assert cu.cuEventSynchronize(ev) == 0
assert cu.cuEventDestroy_v2(ev) == 0
assert cu.cuCtxDestroy_v2(ctx) == 0
q.put(("OK", "mem_and_event_ipc_ok"))
except Exception as exc:
q.put(("ERR", str(exc)))
def main():
cu = _find_lib(("libcuda.so.1", "libcuda.so"))
rt = _find_lib(("libcudart.so.12", "libcudart.so"))
blas = _find_lib(("libcublas.so.12", "libcublas.so"))
lt = _find_lib(("libcublasLt.so.12", "libcublasLt.so"))
assert rt.cudaSetDevice(0) == 0
failures = []
def run_case(name, fn):
try:
detail = fn()
print(f"WORKLOAD_PASS {name} {detail or ''}", flush=True)
except Exception as exc:
print(f"WORKLOAD_FAIL {name} {exc}", flush=True)
failures.append((name, str(exc)))
def test_cublas_gemms():
h = ctypes.c_void_p()
assert blas.cublasCreate_v2(ctypes.byref(h)) == 0
n = 128
sz32, sz16 = n * n * 4, n * n * 2
d_a, d_b, d_c = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
d_a16, d_b16 = ctypes.c_void_p(), ctypes.c_void_p()
for p, s in ((d_a, sz32), (d_b, sz32), (d_c, sz32), (d_a16, sz16), (d_b16, sz16)):
assert rt.cudaMalloc(ctypes.byref(p), s) == 0
h_a = (ctypes.c_float * (n * n))(*[1.0 if (i // n) == (i % n) else 0.0 for i in range(n * n)])
h_b = (ctypes.c_float * (n * n))(*[float((i % 17) - 8) for i in range(n * n)])
h_c = (ctypes.c_float * (n * n))()
assert rt.cudaMemcpy(d_a, h_a, sz32, 1) == 0
assert rt.cudaMemcpy(d_b, h_b, sz32, 1) == 0
a2, b0 = ctypes.c_float(2.0), ctypes.c_float(0.0)
assert blas.cublasSgemm_v2(h, 0, 0, n, n, n, ctypes.byref(a2), d_a, n, d_b, n, ctypes.byref(b0), d_c, n) == 0
assert rt.cudaDeviceSynchronize() == 0
assert rt.cudaMemcpy(h_c, d_c, sz32, 2) == 0
err_f32 = max(abs(h_c[i] - 2.0 * h_b[i]) for i in range(n * n))
assert err_f32 < 1e-4
# FP16 Tensor Core (1.0 * 0.5 * N = 64.0)
a1 = ctypes.c_float(1.0)
h_a16 = (ctypes.c_uint16 * (n * n))(*[0x3C00] * (n * n))
h_b16 = (ctypes.c_uint16 * (n * n))(*[0x3800] * (n * n))
assert rt.cudaMemcpy(d_a16, h_a16, sz16, 1) == 0
assert rt.cudaMemcpy(d_b16, h_b16, sz16, 1) == 0
assert blas.cublasGemmEx(h, 0, 0, n, n, n, ctypes.byref(a1), d_a16, 2, n, d_b16, 2, n, ctypes.byref(b0), d_c, 0, n, 68, -1) == 0
assert rt.cudaDeviceSynchronize() == 0
assert rt.cudaMemcpy(h_c, d_c, sz32, 2) == 0
assert abs(h_c[0] - 0.5 * n) < 1e-2
# BF16 Tensor Core (1.0 * 2.0 * N = 256.0)
h_abf = (ctypes.c_uint16 * (n * n))(*[0x3F80] * (n * n))
h_bbf = (ctypes.c_uint16 * (n * n))(*[0x4000] * (n * n))
assert rt.cudaMemcpy(d_a16, h_abf, sz16, 1) == 0
assert rt.cudaMemcpy(d_b16, h_bbf, sz16, 1) == 0
assert blas.cublasGemmEx(h, 0, 0, n, n, n, ctypes.byref(a1), d_a16, 14, n, d_b16, 14, n, ctypes.byref(b0), d_c, 0, n, 68, -1) == 0
assert rt.cudaDeviceSynchronize() == 0
assert rt.cudaMemcpy(h_c, d_c, sz32, 2) == 0
assert abs(h_c[0] - 2.0 * n) < 1e-2
for p in (d_a, d_b, d_c, d_a16, d_b16):
rt.cudaFree(p)
blas.cublasDestroy_v2(h)
return f"f32_err={err_f32:.1e} fp16_ok bf16_ok"
def test_cublaslt():
lt.cublasLtMatmul.argtypes = [
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p,
]
h_lt = ctypes.c_void_p()
assert lt.cublasLtCreate(ctypes.byref(h_lt)) == 0
n = 128
sz = n * n * 4
d_a, d_b, d_c = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
for p in (d_a, d_b, d_c):
assert rt.cudaMalloc(ctypes.byref(p), sz) == 0
h_a = (ctypes.c_float * (n * n))(*[2.0] * (n * n))
h_b = (ctypes.c_float * (n * n))(*[0.5] * (n * n))
h_c = (ctypes.c_float * (n * n))()
assert rt.cudaMemcpy(d_a, h_a, sz, 1) == 0
assert rt.cudaMemcpy(d_b, h_b, sz, 1) == 0
op_desc = ctypes.c_void_p()
assert lt.cublasLtMatmulDescCreate(ctypes.byref(op_desc), 68, 0) == 0
a_desc, b_desc, c_desc = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
for d in (a_desc, b_desc, c_desc):
assert lt.cublasLtMatrixLayoutCreate(
ctypes.byref(d), 0, ctypes.c_uint64(n), ctypes.c_uint64(n), ctypes.c_int64(n)
) == 0
alpha, beta = ctypes.c_float(1.0), ctypes.c_float(0.0)
assert lt.cublasLtMatmul(
h_lt, op_desc, ctypes.byref(alpha), d_a, a_desc, d_b, b_desc,
ctypes.byref(beta), d_c, c_desc, d_c, c_desc, None, None, 0, None
) == 0
assert rt.cudaDeviceSynchronize() == 0
assert rt.cudaMemcpy(h_c, d_c, sz, 2) == 0
assert abs(h_c[0] - float(n)) < 1e-2
for d in (a_desc, b_desc, c_desc):
lt.cublasLtMatrixLayoutDestroy(d)
lt.cublasLtMatmulDescDestroy(op_desc)
lt.cublasLtDestroy(h_lt)
for p in (d_a, d_b, d_c):
rt.cudaFree(p)
return f"C0={h_c[0]}"
def test_streams_events_pinned():
n = 4096
sz = n * 4
hp = ctypes.c_void_p()
assert rt.cudaMallocHost(ctypes.byref(hp), sz) == 0
arr = (ctypes.c_uint32 * n).from_address(hp.value)
for i in range(n):
arr[i] = 0x5A000000 + i
d1, d2 = ctypes.c_void_p(), ctypes.c_void_p()
assert rt.cudaMalloc(ctypes.byref(d1), sz) == 0
assert rt.cudaMalloc(ctypes.byref(d2), sz) == 0
s1, s2 = ctypes.c_void_p(), ctypes.c_void_p()
e1, e2 = ctypes.c_void_p(), ctypes.c_void_p()
assert rt.cudaStreamCreate(ctypes.byref(s1)) == 0
assert rt.cudaStreamCreate(ctypes.byref(s2)) == 0
assert rt.cudaEventCreate(ctypes.byref(e1)) == 0
assert rt.cudaEventCreate(ctypes.byref(e2)) == 0
assert rt.cudaMemcpyAsync(d1, hp, sz, 1, s1) == 0
assert rt.cudaEventRecord(e1, s1) == 0
assert rt.cudaStreamWaitEvent(s2, e1, 0) == 0
assert rt.cudaMemcpyAsync(d2, d1, sz, 3, s2) == 0
out = (ctypes.c_uint32 * n)()
assert rt.cudaMemcpyAsync(out, d2, sz, 2, s2) == 0
assert rt.cudaEventRecord(e2, s2) == 0
assert rt.cudaEventSynchronize(e2) == 0
ms = ctypes.c_float()
assert rt.cudaEventElapsedTime(ctypes.byref(ms), e1, e2) == 0
assert out[0] == 0x5A000000 and out[n - 1] == 0x5A000000 + n - 1
rt.cudaEventDestroy(e1)
rt.cudaEventDestroy(e2)
rt.cudaStreamDestroy(s1)
rt.cudaStreamDestroy(s2)
rt.cudaFree(d1)
rt.cudaFree(d2)
rt.cudaFreeHost(hp)
return f"elapsed_ms={ms.value:.3f}"
def test_cuda_graphs():
h = ctypes.c_void_p()
s = ctypes.c_void_p()
g = ctypes.c_void_p()
ge = ctypes.c_void_p()
d_a, d_b, d_c = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
capturing = False
try:
assert blas.cublasCreate_v2(ctypes.byref(h)) == 0
assert rt.cudaStreamCreate(ctypes.byref(s)) == 0
assert blas.cublasSetStream_v2(h, s) == 0
n = 64
sz = n * n * 4
for p in (d_a, d_b, d_c):
assert rt.cudaMalloc(ctypes.byref(p), sz) == 0
ones = (ctypes.c_float * (n * n))(*[1.0] * (n * n))
h_c = (ctypes.c_float * (n * n))()
assert rt.cudaMemcpy(d_a, ones, sz, 1) == 0
assert rt.cudaMemcpy(d_b, ones, sz, 1) == 0
assert rt.cudaMemset(d_c, 0, sz) == 0
alpha, beta = ctypes.c_float(1.0), ctypes.c_float(1.0)
assert rt.cudaStreamBeginCapture(s, 0) == 0
capturing = True
try:
assert blas.cublasSgemm_v2(h, 0, 0, n, n, n, ctypes.byref(alpha), d_a, n, d_b, n, ctypes.byref(beta), d_c, n) == 0
finally:
end_rc = rt.cudaStreamEndCapture(s, ctypes.byref(g))
capturing = False
assert end_rc == 0
assert rt.cudaGraphInstantiateWithFlags(ctypes.byref(ge), g, 0) == 0
for _ in range(5):
assert rt.cudaGraphLaunch(ge, s) == 0
assert rt.cudaStreamSynchronize(s) == 0
assert rt.cudaMemcpy(h_c, d_c, sz, 2) == 0
assert abs(h_c[0] - 5.0 * n) < 1e-2
return f"replays=5 C0={h_c[0]}"
finally:
if capturing and s.value:
rt.cudaStreamEndCapture(s, ctypes.byref(g))
if ge.value:
rt.cudaGraphExecDestroy(ge)
if g.value:
rt.cudaGraphDestroy(g)
if s.value:
rt.cudaStreamDestroy(s)
if h.value:
blas.cublasDestroy_v2(h)
for p in (d_a, d_b, d_c):
if p.value:
rt.cudaFree(p)
def test_malloc_async():
s = ctypes.c_void_p()
assert rt.cudaStreamCreate(ctypes.byref(s)) == 0
p = ctypes.c_void_p()
assert rt.cudaMallocAsync(ctypes.byref(p), 1024 * 1024, s) == 0
assert rt.cudaMemsetAsync(p, 0xAB, 4096, s) == 0
out = (ctypes.c_uint8 * 4096)()
assert rt.cudaMemcpyAsync(out, p, 4096, 2, s) == 0
assert rt.cudaFreeAsync(p, s) == 0
assert rt.cudaStreamSynchronize(s) == 0
assert out[0] == 0xAB and out[4095] == 0xAB
rt.cudaStreamDestroy(s)
return "bytes=1MiB"
def test_vmm():
prop = CUmemAllocationProp()
prop.type = 1
prop.location.type = 1
prop.location.id = 0
gran = ctypes.c_size_t()
assert cu.cuMemGetAllocationGranularity(ctypes.byref(gran), ctypes.byref(prop), 0) == 0
sz = gran.value
handle, ptr = ctypes.c_uint64(), ctypes.c_uint64()
assert cu.cuMemCreate(ctypes.byref(handle), sz, ctypes.byref(prop), 0) == 0
assert cu.cuMemAddressReserve(ctypes.byref(ptr), sz, 0, 0, 0) == 0
assert cu.cuMemMap(ptr, sz, 0, handle, 0) == 0
desc = CUmemAccessDesc()
desc.location.type = 1
desc.location.id = 0
desc.flags = 3
assert cu.cuMemSetAccess(ptr, sz, ctypes.byref(desc), 1) == 0
src = (ctypes.c_uint32 * 1024)(*[0xCAFE0000 + i for i in range(1024)])
dst = (ctypes.c_uint32 * 1024)()
assert cu.cuMemcpyHtoD_v2(ptr, src, 4096) == 0
assert cu.cuMemcpyDtoH_v2(dst, ptr, 4096) == 0
assert dst[0] == 0xCAFE0000 and dst[1023] == 0xCAFE03FF
assert cu.cuMemUnmap(ptr, sz) == 0
assert cu.cuMemRelease(handle) == 0
assert cu.cuMemAddressFree(ptr, sz) == 0
return f"gran={sz}"
def test_managed_memory_attach_and_kernel():
h = ctypes.c_void_p()
assert blas.cublasCreate_v2(ctypes.byref(h)) == 0
s = ctypes.c_void_p()
assert rt.cudaStreamCreate(ctypes.byref(s)) == 0
assert blas.cublasSetStream_v2(h, s) == 0
n = 64
sz = n * n * 4
m_a, m_b, m_c = ctypes.c_void_p(), ctypes.c_void_p(), ctypes.c_void_p()
for p in (m_a, m_b, m_c):
assert rt.cudaMallocManaged(ctypes.byref(p), sz, 1) == 0
# Exercise UVM_SET_RANGE_GROUP via cudaStreamAttachMemAsync (Single -> Global -> Host -> Global)
assert rt.cudaStreamAttachMemAsync(s, m_c, 0, 4) == 0
assert rt.cudaStreamAttachMemAsync(s, m_c, 0, 1) == 0
assert rt.cudaStreamAttachMemAsync(None, m_c, 0, 2) == 0
assert rt.cudaStreamAttachMemAsync(None, m_c, 0, 1) == 0
assert rt.cudaMemAdvise(m_a, sz, 3, 0) == 0
assert rt.cudaMemAdvise(m_b, sz, 5, 0) == 0
assert rt.cudaMemAdvise(m_a, sz, 1, 0) == 0
a_a = (ctypes.c_float * (n * n)).from_address(m_a.value)
a_b = (ctypes.c_float * (n * n)).from_address(m_b.value)
a_c = (ctypes.c_float * (n * n)).from_address(m_c.value)
for i in range(n * n):
a_a[i] = 1.5
a_b[i] = 2.0
a_c[i] = 0.0
assert rt.cudaMemPrefetchAsync(m_a, sz, 0, s) == 0
alpha, beta = ctypes.c_float(1.0), ctypes.c_float(0.0)
assert blas.cublasSgemm_v2(h, 0, 0, n, n, n, ctypes.byref(alpha), m_a, n, m_b, n, ctypes.byref(beta), m_c, n) == 0
assert rt.cudaStreamSynchronize(s) == 0
c0 = a_c[0]
assert abs(c0 - 3.0 * n) < 1e-2
for p in (m_a, m_b, m_c):
rt.cudaFree(p)
rt.cudaStreamDestroy(s)
blas.cublasDestroy_v2(h)
return f"attach_ok C0={c0}"
def test_memcpy2d_and_host_register():
pitch = ctypes.c_size_t()
dpitch = ctypes.c_void_p()
assert rt.cudaMallocPitch(ctypes.byref(dpitch), ctypes.byref(pitch), 256 * 4, 64) == 0
src = (ctypes.c_uint32 * (256 * 64))(*[0x22220000 + i for i in range(256 * 64)])
dst = (ctypes.c_uint32 * (256 * 64))()
assert rt.cudaMemcpy2D(dpitch, pitch, src, 256 * 4, 256 * 4, 64, 1) == 0
assert rt.cudaMemcpy2D(dst, 256 * 4, dpitch, pitch, 256 * 4, 64, 2) == 0
assert dst[0] == 0x22220000 and dst[256 * 64 - 1] == 0x22220000 + 256 * 64 - 1
rt.cudaFree(dpitch)
buf = (ctypes.c_uint32 * 1024)(*[0xA5A50000 + i for i in range(1024)])
assert rt.cudaHostRegister(buf, 4096, 2) == 0
dptr = ctypes.c_void_p()
assert rt.cudaHostGetDevicePointer(ctypes.byref(dptr), buf, 0) == 0
d = ctypes.c_void_p()
assert rt.cudaMalloc(ctypes.byref(d), 4096) == 0
assert rt.cudaMemcpy(d, dptr, 4096, 3) == 0
out = (ctypes.c_uint32 * 1024)()
assert rt.cudaMemcpy(out, d, 4096, 2) == 0
assert out[0] == 0xA5A50000 and out[1023] == 0xA5A503FF
rt.cudaFree(d)
rt.cudaHostUnregister(buf)
return f"pitch={pitch.value} host_register_ok"
def _reap_children(procs):
for p in procs:
if p.pid is None:
continue
if p.is_alive():
p.terminate()
p.join(5)
if p.is_alive():
p.kill()
p.join(5)
def test_multiprocess_cublas():
ctx = multiprocessing.get_context("spawn")
q = ctx.Queue()
procs = [ctx.Process(target=_mp_worker, args=(i, q)) for i in range(4)]
try:
for p in procs:
p.start()
for p in procs:
p.join(60)
if p.is_alive():
raise RuntimeError("multiprocess worker timed out")
assert p.exitcode == 0, f"worker exitcode={p.exitcode}"
results = [q.get(timeout=5) for _ in range(4)]
for idx, st, _ in results:
assert st == "OK", f"worker {idx}: {st}"
return "workers=4"
finally:
_reap_children(procs)
def test_cuda_ipc():
dptr = ctypes.c_uint64()
ev = ctypes.c_void_p()
procs = []
try:
assert cu.cuMemAlloc_v2(ctypes.byref(dptr), 65536) == 0
src = (ctypes.c_uint32 * 1024)(*[0xBEEF0000 + i for i in range(1024)])
assert cu.cuMemcpyHtoD_v2(dptr, src, 4096) == 0
mh = CUipcMemHandle()
assert cu.cuIpcGetMemHandle(ctypes.byref(mh), dptr) == 0
assert cu.cuEventCreate(ctypes.byref(ev), 0x6) == 0
assert cu.cuEventRecord(ev, None) == 0
eh = CUipcEventHandle()
assert cu.cuIpcGetEventHandle(ctypes.byref(eh), ev) == 0
ctx = multiprocessing.get_context("spawn")
q = ctx.Queue()
p = ctx.Process(target=_ipc_child, args=(bytes(mh.reserved), bytes(eh.reserved), q))
procs.append(p)
p.start()
p.join(30)
if p.is_alive():
raise RuntimeError("ipc child timed out")
assert p.exitcode == 0, f"ipc child exitcode={p.exitcode}"
st, info = q.get(timeout=5)
assert st == "OK", info
dst = (ctypes.c_uint32 * 1024)()
assert cu.cuMemcpyDtoH_v2(dst, dptr, 4096) == 0
assert dst[0] == 0xFEED0000 and dst[1023] == 0xFEED03FF
return info
finally:
_reap_children(procs)
if ev.value:
cu.cuEventDestroy_v2(ev)
if dptr.value:
cu.cuMemFree_v2(dptr)
def test_optional_ggml_and_nvml():
notes = []
try:
_find_lib(("libggml-base.so",))
ggml = _find_lib(("libggml-cuda.so",))
dc = ggml.ggml_backend_cuda_get_device_count()
assert dc >= 1
desc = ctypes.create_string_buffer(256)
ggml.ggml_backend_cuda_get_device_description(0, desc, 256)
free_b, total_b = ctypes.c_size_t(), ctypes.c_size_t()
ggml.ggml_backend_cuda_get_device_memory(0, ctypes.byref(free_b), ctypes.byref(total_b))
assert total_b.value > 0
notes.append(f"ggml_cuda={dc}({desc.value.decode()},{total_b.value>>20}MiB)")
except RuntimeError:
notes.append("ggml_cuda=skipped")
try:
nvml = _find_lib(("libnvidia-ml.so.1", "libnvidia-ml.so"))
assert nvml.nvmlInit_v2() == 0
dev = ctypes.c_void_p()
assert nvml.nvmlDeviceGetHandleByIndex_v2(0, ctypes.byref(dev)) == 0
temp = ctypes.c_uint()
assert nvml.nvmlDeviceGetTemperature(dev, 0, ctypes.byref(temp)) == 0
nvml.nvmlShutdown()
notes.append(f"nvml_temp={temp.value}C")
except RuntimeError:
notes.append("nvml=skipped")
return " ".join(notes)
run_case("cublas_f32_fp16_bf16_tc", test_cublas_gemms)
run_case("cublaslt_matmul", test_cublaslt)
run_case("streams_events_pinned", test_streams_events_pinned)
run_case("cuda_graphs_5x", test_cuda_graphs)
run_case("cuda_malloc_async", test_malloc_async)
run_case("cuda_vmm", test_vmm)
run_case("managed_attach_policy_kernel", test_managed_memory_attach_and_kernel)
run_case("memcpy2d_and_host_register", test_memcpy2d_and_host_register)
run_case("multiprocess_4x_cublas", test_multiprocess_cublas)
run_case("cuda_ipc_mem_and_event", test_cuda_ipc)
run_case("ggml_cuda_and_nvml", test_optional_ggml_and_nvml)
if failures:
print(f"CUDA12_MATRIX_FAIL count={len(failures)}", flush=True)
return 1
print("CUDA12_MATRIX_PASS all=11", flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())