llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Misc. bug: HIP/gfx1200 FLASH_ATTN_EXT produces varying results and NaNs

Open fox5611 opened this issue 3 weeks ago • 2 comments

Name and Version

Commit: 956591f4570aa6636895d90439892fef7c075b17 Compiler: Ubuntu clang 21.1.8 (6ubuntu1) HIP: 7.1.52801-9999 Build: Release GPU_TARGETS=gfx1200 GGML_HIP_GRAPHS=OFF GGML_HIP_NO_VMM=ON

Operating systems

Linux

Which llama.cpp modules do you know to be affected?

Other (Please specify in the next section), Test code

Command line

cd ~/ai/llama-fa192 && AMD_SERIALIZE_KERNEL=0 ./build-hip/bin/test-backend-ops test -b ROCm0 -j 1 -o FLASH_ATTN_EXT -p 'hsk=192,hsv=128,nh=4,nr23=\[16,1\],kv=512,nb=76,mask=1,sinks=1,max_bias=0'

Problem description & steps to reproduce

Hardware: AMD Radeon RX 9060 XT gfx1200 16304 MiB VRAM Wave size: 32

On RX 9060 XT, gfx1200, Ubuntu clang 21.1.8, wave size 32, and source 956591f45. Build: Release, HIP graphs OFF, and VMM OFF. Model-free FLASH_ATTN_EXT test and head sizes 192/128. With fixed-input diagnostics, input hashes matched across runs and CPU output hashes stayed identical, but GPU output hashes changed. Both 75-token and 76-token cases are affected. Also, the GPU produced NaNs while the corresponding CPU values were finite. The global kernel serialization passed six invocations, each testing two layouts at 75 tokens. Waiting immediately before or after the main kernel did not resolve it. I forced TILE, and it passed 3/3 invocations at 75 tokens. Additionally, the unmodified upstream tests failed first, and the attached patch only makes inputs repeatable, prints fingerprints, and changes 75 tokens to 76 tokens. The failures are intermittent, so one passing run doesn't rule them out. The root cause, first bad commit, and production inference impact are still unknown. Commit: 956591f4570aa6636895d90439892fef7c075b17.

Reproduction: Check out the recorded commit, apply the diagnostic patch, configure and build the HIP test executable. Then run the filtered command several times, compare the input hashes, CPU output hashes, GPU output hashes, and nonfinite counts between runs.

AI usage disclosure: Yes, I used GPT-6.1 Sol for comprehension and diagnostic test instrumentation.

First Bad Commit

Unknown, 956591f45 is the tested commit.

Relevant log output

NaN at index 36953 (ROCm0=-nan CPU=0.001484)

diagnostics.patch

environment.txt

fa192-output-1.txt

fa192-output-2.txt

fa192-output-3.txt

fox5611 avatar Nov 13 '26 05:11 fox5611

Environment: Commit 956591f4570aa6636895d90439892fef7c075b17; RX 9060 XT, gfx1200, wave size 32; Ubuntu clang 21.1.8; HIP 7.1.52801-9999; HIP graphs OFF; VMM OFF. Fixed-input test instrumentation, with matching input hashes and stable CPU hashes.

Broad two-line candidate: I added __forceinline__ to both ggml_cuda_unroll::operator() definitions in ggml/src/ggml-cuda/common.cuh. The fixed-input attention suite gave a baseline of 4,316/4,322 and a candidate result of 4,322/4,322. Removing the annotations brought back changing GPU hashes and a tolerance failure.

The clean candidate with the original test harness passed 20,188/20,190 cases. The remaining failures were the previously observed MUL_MAT and oversized PAD_REFLECT_1D cases.

Generated code, 192/128 kernel: The out-of-line unroll-helper symbols disappeared with broad inlining. VGPR spills went from 412 to 258, SGPR spills from 41 to 0, and private memory from 1,264 to 640 bytes. These changes do not establish the failure mechanism.

K/V isolation: The original reproducer had head sizes 192/128, kv=512, nb=75, GQA ratio 16, and mask and sinks enabled, with both layouts tested.

  • Fully inline K loading only: K helper removal verified; GPU hashes still varied.
  • Fully inline V loading only: V helper removal verified; both layouts passed with identical GPU hashes across three invocations.
  • Restrict inlining to V loading in 192/128: the same stable result across three invocations.

The narrow version’s full attention sweep passed 4,321/4,322 cases. The remaining failure was a head-256 sparse-mask case; a common cause is unproven.

Performance: Fixed inputs and three alternating baseline/candidate pairs. Head-128 prefill took approximately 1.9% more time with broad inlining. The candidate was slower in all six head-128 pairs across the original and reversed run orders.

Head-256 prefill went from 37.844 ms to 30.708 ms, approximately 18.9% less time.

The narrow version’s head-128 prefill timings were 12.099 ms, 12.185 ms, and 12.135 ms. These are near the earlier baseline, but this was not a paired confirmation. These are operator timings only; no whole-model performance claim.

Limits: The exact root cause and first bad commit remain unknown. Production inference impact is unverified. The narrow implementation is still diagnostic scaffolding, and no PR has been submitted.

fox5611 avatar Nov 17 '26 00:11 fox5611

Follow-up on 8b8ca4c98

All 10 fixed-input runs passed tolerance on my RX 9060 XT. The input hashes and CPU output hashes stayed identical across runs. The GPU produced three distinct output hashes for permute=[0,2,1,3] and one for permute=[0,1,2,3]. All outputs were finite in these ten runs. I observed NaNs on the earlier commit, 956591f45.

The full baseline FLASH_ATTN_EXT suite also passed all 4,370 cases on 8b8ca4c98.

Capture-by-value experiment

With the one-line change from [&] to [=], each layout produced a single GPU output hash across three fixed-input runs. The change increased median elapsed time for the head-128 prefill benchmark by 2.70% and was slower in all six timing pairs. I’ve held off on proposing it as a fix. The cause of the run-to-run variation is still open.

fox5611 avatar Nov 20 '26 01:11 fox5611