Misc. bug: MUL_MAT: wrong results, exceeds tolerance; gfx1200 HIP/ROCm; f16 + deterministic.
Name and Version
0.7.0-dev, ggml commit a37cc46
Operating systems
Linux
Which llama.cpp modules do you know to be affected?
Test code, Other (Please specify in the next section)
Command line
./build-hip/bin/test-backend-ops test -j 1 -o MUL_MAT -p 'n=2105344'
Problem description & steps to reproduce
In MUL_MAT on HIP/ROCm gfx1200, I got wrong results, no crash. This happened on ROCm 7.1.1, Ubuntu 26.04, and RX 9060 XT. ERR = 0.004255280 > 0.000500000 , 8.5x tolerance. Case: MUL_MAT(type_a=f16,type_b=f32,m=16,n=2105344,k=32,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0,m_v=0,pad=0). Deterministic: 0.004253291 on October 21st, 0.004255280 on October 23rd, same to three significant figures across runs with different random inputs. So it's a systematic precision problem, not a flaky race. Not claimed: No root cause. Repro reports 0/1 tests passed. Build Command:
cmake -S . -B build-hip -DGGML_HIP=ON -DGPU_TARGETS=gfx1200
-DLLAMA_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build-hip --target test-backend-ops -j 12
First Bad Commit
Don’t know, present at a37cc46, haven’t bisected.
Relevant log output
[MUL_MAT] ERR = 0.004255280 > 0.000500000 MUL_MAT(type_a=f16,type_b=f32,m=16,n=2105344,k=32,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0,m_v=0,pad=0): FAIL
It fails every run, which is deterministic in the sense that matters for reproducing it. However, the result isn't deterministic because the error value varies from run to run, so the kernel isn't producing bit-identical output.
The exact failing case is MUL_MAT(type_a=f16,type_b=f32,m=16,n=2105344,k=32,bs=[1,1],nr=[1,1],per=[0,1,2,3]), with ERR 0.0043 against a 0.0005 tolerance, roughly 8x over.
Across four runs the error was 0.004258, 0.004265, 0.004283 and 0.004312.
It still fails on current master (ca89968a2), so it has not been fixed since October. It is unrelated to #31838: the failure is identical with and without that patch.
This call goes to rocblas and i cant repoduce this with rocm 7.2 on gfx1201 so that suggests that this is a bug in 7.1's rocblas rather than ggml
Thank you, and that matches what I found independently. I put an fprintf at the entry to ggml_cuda_mul_mat_cublas and confirmed this shape reaches rocBLAS through the fallback in ggml_cuda_mul_mat (src0=f16 ne00=32 ne01=16 ne11=2105344).
I'm less sure about the 7.1 rocBLAS part, because the error magnitude is about what f16 accumulation should produce on its own. The compute type selected on this path is F16, since src0 is F16 and fast_fp16_hardware_available(cc) is true on gfx1200. f16 epsilon is around 4.9e-4, and a 32-term sum random-walks to roughly sqrt(32) * 4.9e-4, about 2.8e-3, against the 4.3e-3 I measure. The test threshold of 5e-4 is about one f16 epsilon, which is the tolerance you'd pick for an f32 accumulator.
Setting GGML_CUDA_CUBLAS_COMPUTE_TYPE=f32 makes the case pass and the full MUL_MAT suite reports 2/2 backends passed. That's consistent with both explanations though, since f32 would mask a library bug just as well, so it doesn't settle anything by itself.
What would resolve it: On your 7.2 / gfx1201 setup, does this case still pass with GGML_CUDA_CUBLAS_COMPUTE_TYPE=f16 forced? If it fails there too, then 7.2 isn't fixing a defect, it's selecting a different compute path, and this is compute-type selection rather than rocBLAS. If it passes with f16 forced, you're right and it's a 7.1 defect.
One thing I can't rule out from here: your comparison moves 7.1 to 7.2 and gfx1200 to gfx1201 at the same time, so I can't tell which of the two accounts for the difference.
Separately, while looking at this I noticed BF16 already has a GGML_CUDA_CC_IS_AMD(cc) && src1->ne[1] > 32 guard forcing F32, with no equivalent for F16. If this does turn out to be selection, that's the shape a fix would take, though quantized src0 takes the first branch and wouldn't reach an F16 guard.
gfx1200, RX 9060 XT, Ubuntu-packaged ROCm, HIP 7.1.52801, librocblas5 7.1.0.
This still reproduces on master 0a7675746 (b12529): ERR = 0.004298 against the 5e-4 tolerance, unchanged in magnitude from my earlier report.
@IMbackK, could you force GGML_CUDA_CUBLAS_COMPUTE_TYPE=f16 on your ROCm 7.2 / gfx1201 setup? Your comparison changed both the ROCm version and the GPU, so it cannot separate which of the two accounts for the difference.
The two-line AMD f16 guard is written and ready if that is the direction you want.
Independent gfx1151 direct hipBLAS A/B controls for the FP16-compute hypothesis (not a reproduction of the reporter's full ggml test):
- Windows 11, Radeon 8060S gfx1151, ROCm 7.2 HIP runtime
71260602. A/B throughhipblasGemmEx: A/B/C areHIP_R_16F; onlyHIPBLAS_COMPUTE_16FvsHIPBLAS_COMPUTE_32Fchanges.m=16, k=32, withn=256andn=4096. - Three input controls at each size: all-zero, exact unit vectors, and deterministic mixed signed halves (
A[i+q*m]=half((((i*17+q*11)%47)-23)/16),B[q+j*k]=half((((j*29+q*13)%53)-26)/17)). Zero and unit controls are exact for both paths; six of six execution/invariant controls passed. - Error below is vs a CPU FP32 dot product rounded once to FP16 (this is not llama.cpp's
test-backend-ops ERRmetric):
| n | FP16 compute max abs / RMS | FP32 compute max abs / RMS |
|---|---|---|
| 256 | 0.015625 / 0.005302 | 0.000977 / 0.000034 |
| 4096 | 0.015625 / 0.005288 | 0.000977 / 0.000034 |
For n=4096, 36,014/65,536 FP16 outputs exceed 5e-4 absolute error against the rounded reference, versus 77/65,536 with FP32 compute. Thus a large accumulation-precision sensitivity persists on ROCm 7.2 / gfx1151, independently of the reporter's ROCm 7.1 / gfx1200 configuration. It does not distinguish a 7.1 rocBLAS regression from compute-type selection on gfx1200.
Source pinned: llama.cpp master c212d25ad34ff1fdf775793cda07e009560cf66b, ggml/src/ggml-cuda/ggml-cuda.cu, locally captured SHA-256 8b23f0a48b9f14bdf107fa607d5b1796a8100c9a5dcf7bddf17b421731211a96. In ggml_cuda_mul_mat_cublas, the F16 path can retain F16 compute on fast-FP16 devices; the GGML_CUDA_CUBLAS_COMPUTE_TYPE override can force F32/F16.
Limitations: This is a direct hipBLAS control, not the affected full ggml graph; I did not test n=2,105,344 or gfx1200/gfx1201. An older local test-backend-ops binary selected 0/0 matching tests and cannot support a ggml pass/fail claim. No upstream patch or fix is claimed.
Discriminating next check: On the same gfx1201 + ROCm 7.2 system, compare the exact -o MUL_MAT -p 'n=2105344' case under GGML_CUDA_CUBLAS_COMPUTE_TYPE=f16 and =f32, recording the selected rocBLAS compute path and ERR. That holds ROCm version and architecture constant.
I can reproduce this on rocm 10.1 and have traced it to a bug in rocblas this brench call repos it for me:
rocblas-bench -f gemm_ex --transposeA T --transposeB N -m 16 -n 2097153 -k 32 --lda 32 --ldb 32 --ldc 16 --ldd 16 --a_type f16_r --b_type f16_r --c_type f32_r --d_type f32_r --compute_type f32_r --alpha 1 --beta 0 --initialization rand_int -v 1 -t 1 -i 1 -j 0
looks like this is this issue: https://github.com/ROCm/rocm-libraries/issues/14983
Thanks for reproducing it. The rocblas-bench boundary at 2097153 is
consistent with my findings, as my case at n=262144 passed and n=2105344 failed,
both on the expected side of 2^21. I'll start following
ROCm/rocm-libraries#14983.