55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""How much device memory does the driver take for cuMemAlloc of a given size?
|
|
|
|
Offline experiment (Python is allowed for experiments): allocates N buffers of each size through the CUDA driver API
|
|
(ctypes, no toolkit needed) and prints the drop of cuMemGetInfo free memory per allocation. The leleka dense weights are
|
|
~700 separate cuMemAlloc calls of 0.5-10 MiB; if the driver rounds each one above 1 MiB up to a multiple of 2 MiB, about
|
|
0.7 GiB of the fixed footprint is rounding.
|
|
|
|
python3 -I scripts/dev/alloc-granularity-probe.py
|
|
|
|
Run it ONLY on an idle GPU (it allocates up to ~0.6 GiB and frees it again). On the 5090 server take /data/leleka-e2e/gpu.lock first.
|
|
Measured 2026-10-29 on the RTX 3080 laptop (WDDM): 4 KiB..1 MiB take exactly their size (packed), 3 MiB takes 4 MiB,
|
|
2.5 MiB takes 4 MiB.
|
|
"""
|
|
import ctypes
|
|
import sys
|
|
|
|
if sys.platform == "win32":
|
|
cu = ctypes.WinDLL("nvcuda.dll")
|
|
else:
|
|
cu = ctypes.CDLL("libcuda.so.1")
|
|
|
|
|
|
def check(code, what):
|
|
if code != 0:
|
|
raise SystemExit("%s failed with CUDA error %d" % (what, code))
|
|
|
|
|
|
check(cu.cuInit(0), "cuInit")
|
|
device = ctypes.c_int()
|
|
check(cu.cuDeviceGet(ctypes.byref(device), 0), "cuDeviceGet")
|
|
context = ctypes.c_void_p()
|
|
check(cu.cuCtxCreate_v2(ctypes.byref(context), 0, device), "cuCtxCreate")
|
|
|
|
|
|
def free_mib():
|
|
free, total = ctypes.c_size_t(), ctypes.c_size_t()
|
|
check(cu.cuMemGetInfo_v2(ctypes.byref(free), ctypes.byref(total)), "cuMemGetInfo")
|
|
return free.value / 1048576.0
|
|
|
|
|
|
warm = ctypes.c_uint64()
|
|
check(cu.cuMemAlloc_v2(ctypes.byref(warm), 1 << 20), "warm-up allocation")
|
|
for size, count in [(4096, 1000), (65536, 500), (262144, 400), (1 << 20, 200), (3 << 20, 100), ((5 << 20) // 2, 100), (4 << 20, 100)]:
|
|
pointers = []
|
|
before = free_mib()
|
|
for _ in range(count):
|
|
pointer = ctypes.c_uint64()
|
|
check(cu.cuMemAlloc_v2(ctypes.byref(pointer), size), "cuMemAlloc")
|
|
pointers.append(pointer)
|
|
taken = before - free_mib()
|
|
print("size %9d x %5d: asked %8.2f MiB, driver took %8.2f MiB, %.3f MiB per allocation" % (size, count, size * count / 1048576.0, taken, taken / count))
|
|
for pointer in pointers:
|
|
cu.cuMemFree_v2(pointer)
|