ggml-cuda: load large weights via unified memory on integrated GPUs
Merged
DA
6mo ago+127+127+012345678901234567890123456789-3-3-0123456789
What
On integrated GPUs (APUs where the CPU and GPU share one pool of RAM, e.g. AMD Strix Halo gfx1151) the loader allocated a separate device buffer and copied every weight tensor host-to-device. For a model whose weights approach total RAM this doubles residency, so the device buffer plus the mmap'd source no longer fit and the load thrashes. This routes large weight allocations on integrated devices to managed (unified) memory and fills it with a direct parallel host copy.
Changes (ggml/src/ggml-cuda/ggml-cuda.cu, ~127 lines)
ggml_cuda_device_malloc: on an integrated GPU, auto-selectcudaMallocManaged+ coarse-grain advise for allocations aboveDFLASH_HIP_UMA_MIN_FRACof system RAM (default0.45), i.e. where a separate device buffer plus the source would not fit. Probes a freshcudaGetDeviceProperties().integratedvia thread-safe init (the cachedggml_cuda_info()flag is hard-forcedfalse). Opt out:DFLASH_HIP_NO_AUTO_UMA=1. Force on:GGML_CUDA_ENABLE_UNIFIED_MEMORY.set_tensor: for managed buffers, fill with a parallel hostmemcpy, skipping the pageable host-to-device staged copy and the per-tensor synchronize.ggml_backend_cuda_buffer_is_managed: small accessor so loaders can stream weights straight into a unified buffer.
Results (Strix Halo gfx1151, ROCm 7.2.2)
| Model | Before | After |
|---|---|---|
| ~86 GB (near total RAM) | un-loadable (>1500 s, thrash) | ~70-90 s |
| 16 GB (fits) | unchanged | unchanged; decode output byte-identical |
Safety / scope
- Linux + integrated only. Discrete GPUs, non-integrated devices, and models that fit keep the legacy
hipMalloc+ H2D path untouched; the unified path degrades to legacy where/proc/meminfois unavailable. - No decode regression: greedy output is byte-identical between the managed and legacy paths.
- Page-cache management for near-RAM models (dropping copied source pages) is intentionally left to the model loader, where the source is known to be a read-only file mapping, rather than the generic backend.