Misc. bug: HIP: CU_CHECK(err) in the VMM pool lets cuMemAddressReserve spin forever
Name and Version
version: 0.9.0-dev (build 12338, commit 304e1ac22) built with GNU 15.2.0 for Linux x86_64
Operating systems
Linux
Which llama.cpp modules do you know to be affected?
llama-server
Command line
HIP build using the VMM pool (GGML_HIP_NO_VMM=OFF)
Problem description & steps to reproduce
On HIP CU_CHECK is defined as
#define CU_CHECK(fn) {hipError_t err = fn; if(err != hipSuccess) { GGML_ABORT("HipVMM Failure: %s\n", hipGetErrorString(err)); }}
This overshadows the error condition in #30542 causing an infinite loop on startup.
First Bad Commit
#30542 (ac931b5bc)
Relevant log output
Logs
Reproduced on gfx1200 (RX 9060 XT), ROCm 7.1.1, master 3549c274d, built with -DGGML_HIP_NO_VMM=OFF:
./build/bin/test-backend-ops -o ADD
The device reports VMM: yes. The 32 GiB reservation succeeds on the first try. Granularity is 4096.
Mechanism
CU_CHECK(fn) in vendors/hip.h declares a local hipError_t err. The call site at ggml-cuda.cu:638 passes a variable that is also named err, so it expands to hipError_t err = err;. That is self-initialization from an indeterminate value, which is undefined behavior.
The older call sites pass function calls (cuMemCreate, cuMemMap) and are unaffected. #30542 added the first call site that passes a variable.
The CUDA side is immune: CUDA_CHECK_GEN in common.cuh uses auto err_ = (err) inside do { } while (0).
Evidence
With fprintf probes bracketing the call, the real err is 0 before the macro, and control never reaches the statement after it.
Two failure modes from builds differing only by one added probe:
- Hang at 100% CPU on a single thread. The innermost backtrace frames are in
libamdhip64, called fromggml_cuda_pool_vmm::alloc, which is called fromggml_cuda_mul_mat_vec_q. - Segfault.
Two modes from one defect is what you would expect from UB whose outcome depends on code layout.
One correction to the report
The retry loop cannot itself spin. max_size strictly halves each iteration, so it is bounded: from 2^35 down to 2^13 at granularity 4096, at most 23 iterations. In my runs the loop never iterated at all, because the first reservation succeeded. The hang is in the check, not in the loop.
After making the macro hygienic
The first pool allocation completes and 9 ADD cases pass. Pool growth then hits a genuine error at ggml-cuda.cu:716: cuMemSetAccess returns invalid argument for a sub-range at a nonzero offset into the reservation. That failure was masked by this bug. I will file it separately.
I will open a PR making the HIP macro hygienic, matching the existing CUDA convention.
Can confirm that after reverting the bad commit my build using -DGGML_HIP_NO_VMM=OFF works again on gfx1101 without hanging. An alternative is to set -DGGML_HIP_NO_VMM=ON, which also avoids the hang.
Built commit: 2cbfc3cf34f94c8ada67eb8766efe7374a3d2db6 & revert #30542
Build command:
ROCM_PATH=$(rocm-sdk path --root) \
cmake -S . -B /app/build \
-DCMAKE_BUILD_TYPE=Release \
-DGGML_CCACHE=OFF \
-DGGML_CUDA_FORCE_CUBLAS=OFF \
-DGGML_HIP=ON \
-DGGML_HIP_GRAPHS=ON \
-DGGML_HIP_NO_VMM=OFF \
-DGGML_OPENMP=OFF \
-DGPU_TARGETS=gfx1101 \
-DLLAMA_OPENSSL=ON \
-DLLAMA_BUILD_TESTS=OFF && \
cmake --build /app/build --config Release -- -j 16 && \
cmake --install /app/build --prefix /app/llamacpp
Still present on master 19bc41dbb: hip.h:34 is unchanged and the call site is now ggml-cuda.cu:714.
laurimi's gfx1101 result matches what I saw on gfx1200, so this is not architecture-specific.
On the revert as the fix: reverting #30542 removes the only call site that passes a variable, but CU_CHECK itself stays unhygienic, so the next CU_CHECK(var) written anywhere hits the same thing. The CUDA side already handles this in CUDA_CHECK_GEN with do { } while (0) and a trailing-underscore err_.
I have the macro fix written and tested. It does not make the VMM pool work: it turns the hang into a GGML_ABORT carrying the real HIP error string, which is how I was able to file #32350. The GGML_HIP_NO_VMM=ON path is unaffected, test-backend-ops ADD still reports 2/2 backends passed.
Does a maintainer want a PR for the macro, or is reverting #30542 the preferred resolution?