Skip to content

opencl: fix q6_K flat mul_mat for Adreno A6x/A7x GPUs with older E031 compilers - #26476

Draft
wanghqc wants to merge 2 commits into
ggml-org:masterfrom
qualcomm:hq/a7x-q6k-gated-r0730
Draft

opencl: fix q6_K flat mul_mat for Adreno A6x/A7x GPUs with older E031 compilers #26476
wanghqc wants to merge 2 commits into
ggml-org:masterfrom
qualcomm:hq/a7x-q6k-gated-r0730

Conversation

@wanghqc

@wanghqc wanghqc commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Overview

This PR is to workaround the issues seen on Adreno 740/642L GPUs with specific compiler version E031.

  • The flat q6_K mul_mat produces wrong answers on the older Adreno E031 compilers
    • 10 shapes (the m=16, k=256 family), which is exactly the MUL_MAT 909/919 seen on an Adreno 740 and an
      Adreno 642L today.
    • Wrong values, not an out-of-bounds write and not a sentinel trip.
  • The kernel gets four codegen workarounds, described in the commit message and emitted via -D ADRENO_OLD_COMPILER=1.
  • The defect tracks the compiler, not the GPU generation
    • It reproduces on E031.38 (Adreno 642L) and E031.41 (Adreno 740)
    • Fixed by E031.45 (Adreno 619).
    • Every other compiler builds the original source and keeps the original 17-argument kernel signature.

-test-backend-ops MUL_MAT, all 919/919 OK with this change:

device compiler workarounds before
Adreno 740 (A7X) E031.41 on 909/919
Adreno 642L (A6X) E031.38 on 909/919
Adreno 619 (A6X) E031.45 off 919/919
Adreno 840 (A8X) E031.50 off 919/919
Adreno 850 E17.51 off 919/919

Additional information

Over-applied where they are not needed the workarounds cost real throughput

  • 42.4 → 35.1 GFLOPS on an Adreno 840 q6_K GEMV, and −13.1% on an Adreno X2-90.

Why the gate is on the compiler and not the GPU generation.

  • An earlier revision gated on adreno_gen == A7X.
  • Measuring three E031 versions showed that is wrong in both directions:
    • It misses the Adreno 642L (A6X, E031.38), which is broken and shipping,
      -It would keep applying the workarounds to an A7X whose driver had been updated past the fix

Requirements

wanghqc added 2 commits August 2, 2026 12:59
…r SIGSEGV)

The Adreno 740 (A7X) compiler E031.41 crashes inside clBuildProgram when
building the flash_attn programs whose KV path is mixed-type or dequantized:
flash_attn_f32_f16, flash_attn_f32_q8_0, flash_attn_f32_q4_0. It is a driver
crash rather than a compile-error return, so build_program_from_source_ex()
cannot catch it. The uniform f32 and f16 programs build correctly.

Decline the three KV-convert variants on the A7X in supports_op so they never
lazy-compile; those attention layers run on the CPU backend instead. Same
idiom as the existing Intel DK=512 and X1E carve-outs.

test-backend-ops FLASH_ATTN_EXT on the 740: 226 OK / 0 FAIL, previously exit
139. Other parts are unaffected - the gate is dead code there.
kernel_mul_mv_q6_K_f32_flat produces ~10x-wrong output on the older Adreno
E031 compilers while q4_K and q5_K are correct. Four codegen defects, each
confirmed on-device against the CPU reference:

  1. 64-bit ulong arithmetic is miscompiled, so every weight and scale read
     hit the wrong address - the primary cause, and why q5_K (int offsets)
     was unaffected. The block index is computed in int and widened only
     inside the pointer expression.
  2. The vectorized dequant (int4/float4 bit-ops, convert_*4, dot()) is
     miscompiled; the 6-bit weights are reconstructed and the dot done
     scalar.
  3. vload4 of the f32 activations is miscompiled; replaced by a
     scalar-indexed load.
  4. The accumulation is miscompiled unless a side effect forces the partial
     sums to materialize. A printf under a guard the compiler cannot prove
     false acts as a zero-cost optimizer barrier; its placement is
     load-bearing.

The defect tracks the compiler, not the GPU generation: it reproduces on
E031.38 (Adreno 642L) and E031.41 (Adreno 740) and is fixed by E031.45
(Adreno 619), so the workarounds are gated on the compiler version. Where
they are not needed they cost real throughput - 42.4 -> 35.1 GFLOPS on an
Adreno 840 q6_K GEMV. The explicit compiler-type check is required, not
redundant: newer_than_or_same() is false for every non-E031 compiler, so
negating it alone would enable the workarounds on E17 and DX.

test-backend-ops MUL_MAT is 919/919 on the Adreno 740, 642L, 619, 840 and
850; the 740 and 642L were 909/919 before. The 642L additionally needs the
A6X per-kernel-program support to reach these tests at all.
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning OpenCL Issues specific to the OpenCL backend labels Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning OpenCL Issues specific to the OpenCL backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant