Repository navigation
cuda: int8 dp4a decode for TQ3_1S, two-row dp4a kernels, dp4a on RDNA2/RDNA3 - #415
Conversation
TheTom
left a comment
There was a problem hiding this comment.
Built with #413 on a GB10 (sm_121, CUDA 13.0.88): no compiler warnings, no new spills in the TQ kernels. The two-row TQ4_1S dp4a kernel runs there and -o MUL_MAT / -o MUL_MAT_ID for tq4_1s pass under GGML_TQ_NATIVE=1. Speed on NVIDIA was not measured.
| // nvcc / MUSA prmt takes one selector NIBBLE per output byte, so the identical indices pack | ||
| // at 4-bit spacing instead. __byte_perm supplies bytes 0-3 from its first operand and 4-7 | ||
| // from its second, which is the same {HI,LO} window; prmt sign-replicate (nibble bit 3) is | ||
| // never reached because the indices are 0..7. NOTE: this branch has never been built or run |
There was a problem hiding this comment.
This NOTE is wrong on two counts. The fork builds and ships on NVIDIA, so 'has only ever targeted HIP' is false. And tq_use_dp4a returns false for TQ3_1S on non-HIP builds, so this branch is not 'reachable where tq_use_dp4a says it should be'; it is compiled on nvcc and MUSA and never called. Cut the comment to the operand-order fact.
| return false; | ||
| } | ||
| #endif | ||
| static const int force = getenv("GGML_TQ_DP4A") ? atoi(getenv("GGML_TQ_DP4A")) : -1; |
There was a problem hiding this comment.
GGML_TQ_DP4A is a new env knob and it is not in the AGENTS.md environment table. Add a row (unset = auto per architecture, 1/0 force dp4a or scalar, measurement override only, TQ3_1S ignores it on non-HIP builds).
| return false; | ||
| } | ||
| return !GGML_CUDA_CC_IS_AMD(cc) || GGML_CUDA_CC_IS_CDNA(cc) || | ||
| GGML_CUDA_CC_IS_RDNA2(cc) || GGML_CUDA_CC_IS_RDNA3(cc); |
There was a problem hiding this comment.
GGML_CUDA_CC_IS_RDNA3 also matches RDNA3.5 (gfx1150/gfx1151). The numbers in this PR are gfx1100 only, and RDNA4 was measured slower on dp4a, so RDNA3.5 could go either way. Use GGML_CUDA_CC_IS_RDNA3_0 here until someone posts an RDNA3.5 tg number, or post one.
|
|
||
| #pragma unroll | ||
| for (int r = 0; r < nrows_per_warp; r++) { | ||
| const int row = row0 + r; |
There was a problem hiding this comment.
This tail branch has no test. Every TQ MUL_MAT case in test-backend-ops has an even m, so row0 + 1 >= nrows_x never happens. Please add tq3_1s and tq4_1s cases with odd m at n = 3..8, for example m in {1, 5, 33} with k=256, so the last warp owns one real row and one skipped row.
| int stride_col_y, int stride_col_dst, cudaStream_t stream) { | ||
| // Same crossover as the TQ3_1S path (launch_tq3_1s_dp4a_multi): two rows per warp only | ||
| // pays from ncols_dst >= 3. | ||
| if constexpr (ncols_dst >= 3) { |
There was a problem hiding this comment.
This also switches NVIDIA and MUSA to the two-row kernel for native TQ4_1S at n >= 3, and the crossover was measured on gfx90a only. It compiles on sm_121 with no new spills and passes -o MUL_MAT -p type_a=tq4_1s under GGML_TQ_NATIVE=1, so the open question is speed. Post a test-backend-ops perf A/B on an NVIDIA card at n = 3..8, or keep the two-row path under GGML_USE_HIP until then.
| @@ -635,11 +997,30 @@ static __global__ void mul_mat_tq4_1s_scalar_multi( | |||
| // AMD: uses scalar half path for TQ4_1S (dp4a regresses on RDNA4) | |||
There was a problem hiding this comment.
Stale after this PR: TQ4_1S on AMD now takes dp4a on CDNA, RDNA2 and RDNA3. The same goes for the 'Scalar float path: TQ3_1S (all vendors) + TQ4_1S on AMD' comment in the else branch of ggml_cuda_mul_mat_tq. Point both at tq_use_dp4a.
| } | ||
| } | ||
|
|
||
| // Multi-row variant of the TQ3_1S dp4a kernel. |
There was a problem hiding this comment.
Comment blocks like this one (10 lines, us/column figures) and the ones on tq_use_dp4a and launch_tq3_1s_dp4a_multi are past the fork's 1-2 line convention. Keep one line on what the kernel does and why n >= 3, and leave the measurements in the commit message, where they already are.
| // TQ3_1S dp4a MoE variant: mul_mat_tq4_1s_dp4a_moe with the 3-bit unpack of | ||
| // mul_mat_tq3_1s_dp4a_multi. | ||
| template <int LPR, bool USE_TABLE> // LPR: lanes cooperating on one output row (must divide WARP_SIZE) | ||
| static __global__ void mul_mat_tq3_1s_dp4a_moe( |
There was a problem hiding this comment.
The MoE decode PPL row moves 5.7437 -> 5.7738 on 4 chunks while the three dense rows move by 0.006 or less. Please rerun the Qwen3.6-35B-A3B-TQ3_1S row with at least 16 chunks, and with GGML_TQ_DP4A=0 on this branch as the control, to show the +0.030 is noise and not this kernel.
|
Thanks @jasstrong. Numbers on gfx90a, gfx1100 and gfx1030 are what I want on every kernel PR. Comments are inline. The open one is a speed A/B on NVIDIA for the two-row TQ4_1S kernel. |
|
Thanks. Done in fdf3762:
MoE PPL rerun on the V620: Qwen3.6-35B-A3B-TQ3_1S,
The scalar control matches base exactly, and dp4a is +0.005 against +/-0.31 error bars. The +0.030 at 4 chunks was noise. I've updated the table in the description. @mgeldi or @seanrasch, could one of you (or anyone else with suitable hardware) run an A/B on an NVIDIA card? This is for the two-row TQ4_1S kernel: Run it at this head, then again with the |
This branch carried TheTom#415 as cherry-picks, so TheTom#415 would conflict with it once merged. Conflicts in mmvq-tq.cu keep this branch's TQ3_4S-templated kernels and dispatch; every other TheTom#415 line is present. The merge also brings TheTom#415's corrected graph-capture comments (eddb429), which the cherry-picks had missed: mmvq.cu merged cleanly, and the same comment is applied by hand in mmvq-tq.cu, where it sat inside a conflict. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
This branch sits on the old base ( The rebased branch is on my fork, so it can be pushed as-is: head: It picks up #414 (src1 column count in the TQ fused and cuBLAS paths), #412 (null |
|
Thanks @jasstrong. All eight review threads are done at 317f03f. GB10 (sm_121, CUDA 13), run at the #416 head, which contains this branch, merged onto the current base: no warnings from these files. For tq4_1s, I did not run the NVIDIA A/B for the two-row kernel, so the HIP guard stays as it is. Left before I merge, after #413:
|
|
#403 has merged, so this branch's base moved again (the fork tip is now I rebased this branch onto the new base; the result is code-identical to the current head and merges cleanly into the base: head: The only conflict was The same rebase exists for the rest of the stack: |
TQ3_1S decode ran only the scalar float kernels: one lane per weight, a float centroid lookup and an fmaf per element. TQ4_1S has had dp4a kernels for a while (q8_1 activations, int8 centroids, ggml_cuda_dp4a); this gives TQ3_1S the same, for the dense matvec (ncols_dst <= 8) and for MUL_MAT_ID. The only new piece is the unpack. A 32-value TQ3_1S block holds four 8-element groups of 3-bit indices in 12 bytes; tq3_groups reads them as three aligned words, and tq3_cents8_reg expands a group into two int8x4 words with one byte permute each: the 8-entry int8 centroid table (kvalues_tq3) is exactly the 8-byte source window of v_perm_b32 / prmt. The kernels are otherwise the TQ4_1S ones with that unpack and the TQ3 rescale. The path is chosen by tq_use_dp4a, which keeps the existing arch rule (NVIDIA and CDNA) and takes TQ3_1S on HIP only: the nvcc branch of tq3_cents8_reg has not been built or run, and the fused TQ3_1S path is not taken on NVIDIA anyway. MI210 (gfx90a), Qwen3.8-27B-TQ3_1S, tg128: 11.3 -> 50.0 t/s. Kernel time for m=4096 k=14336, n=1: 204 -> 33 us. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The dp4a decode kernels give each warp one row, so every warp re-reads the same activation blocks; with ncols_dst columns that is a full activation pass per column over the whole matrix. At speculative-verify widths this dominates: on gfx90a (m=4096, k=14336) a width-3 call cost 1.87x a width-1 call. Give each warp two rows from ncols_dst >= 3, loading each activation block once for both. Two rows add a little fixed cost (two weight streams, more registers), so below 3 columns the one-row kernels stay. Four or more rows regressed at every width (occupancy). Applies to TQ4_1S and TQ3_1S. MI210, TQ4_1S kernel time, m=4096 k=14336 (us): n=3 61.4 -> 51.3, n=4 80.9 -> 54.8, n=8 138.9 -> 104.3; n=1 and n=2 unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
RDNA2 (sdot4) and RDNA3 (sudot4) have dot4 throughput, but tq_use_dp4a kept
them on the scalar float kernels, which are instruction-bound there. Take dp4a
on RDNA2 and RDNA3 too. RDNA1 has no dot4 instruction and RDNA4 was measured
slower on dp4a, so both keep the scalar path. GGML_TQ_DP4A=1 / =0 forces either
way, e.g. to measure RDNA4 again.
tg128, GGML_TQ_NATIVE=1, base -> this series:
RX 7900 XTX Qwen3.8-27B-TQ3_1S 21.7 -> 42.9
Qwen3.8-27B-TQ4_1S 26.2 -> 37.3
PRO V620 Qwen3.8-27B-TQ3_1S 16.2 -> 25.1
Qwen3.6-35B-A3B-TQ3_1S 59.3 -> 78.7
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The comments said capture runs in relaxed mode, which allows cudaMalloc during capture. On HIP for RDNA2 the graph is captured in ThreadLocal mode, where hipMalloc returns hipErrorStreamCaptureUnsupported and invalidates the capture (measured on a PRO V620, ROCm 7.2.3; forcing the rotation cache to regrow during capture aborts in CUDA_CHECK). Real workloads never grow the caches during capture, since each graph runs uncaptured first. Comment-only change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d-m tests - The two-row TQ4_1S dp4a kernel at 3+ columns is HIP only until it has been measured on NVIDIA. NVIDIA and MUSA keep the one-row kernel. - dp4a by default on RDNA3.0 only (GGML_CUDA_CC_IS_RDNA3_0): the numbers are from gfx1100, and RDNA3.5 has not been measured. - test-backend-ops: tq3_1s and tq4_1s MUL_MAT with odd m (1, 5, 33) at n = 3..8, k = 256, so the last warp of a two-row kernel owns one real row and one past the end. - AGENTS.md: GGML_TQ_DP4A row. - Comments cut to what the code does; the measurements stay in the earlier commit messages. Stale "scalar on AMD" comments now point at tq_use_dp4a, and the wrong NOTE on the nvcc TQ3_1S branch is gone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With m = 1 a case has only 3-8 output values, and on the 7900 XTX the int8 activation rounding of the dp4a path alone pushed the NMSE over the 5e-4 threshold in about one run in four (0.00053-0.0024 at m = 1, n = 3..5, k = 256, TQ4_1S). m = 5 and m = 33 passed 25 of 25 runs there. Every m is still odd, so the last warp of the two-row kernels still owns one row past the end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
317f03f to
36eaa5d
Compare
TQ3_4S is TQ3_1S with four 32-value sub-blocks sharing one fp16 scale and 6-bit sub-scales, so the CUDA/HIP side reuses the TQ3_1S kernels with a per-block scale lookup: - dequantize, convert and get_rows for the f16/f32 fallback paths; - dense and MoE decode on the fused TQ3 kernels in mmvq-tq.cu, with the dp4a and two-row variants of TheTom#415; - the native int8 MMQ path (mmq-load-tiles.cuh, mmq-instance-tq3_4s.cu) with config entries for CDNA, RDNA2 and RDNA3. It stays opt-in (GGML_TQ_MMQ=1); RDNA4 has no rows until someone measures them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Rebased onto |
No content change. The branch already holds TheTom#413, TheTom#415 and TheTom#418; this records their merge commits as parents so the pull request merges cleanly.
Rebased onto
50b4aa28b. TheGGML_TQ_DP4Arow is now in the new AGENTS.md knob table and indocs/KV-cache-quantization.md.What
On AMD, TQ3_1S decode ran only the scalar float kernels: one lane per weight, a float centroid lookup and an fmaf per element. TQ4_1S had dp4a kernels, but on AMD only CDNA used them. Three commits:
kvalues_tq3table is exactly the 8-byte window ofv_perm_b32/prmt. TQ3_1S takes this on HIP only; the fused TQ3_1S path is off on NVIDIA anyway.ncols_dst >= 3each warp takes two rows and loads each activation block once for both. Below 3 columns the one-row kernels stay; four or more rows regressed everywhere. This covers TQ4_1S and TQ3_1S. The TQ4_1S two-row kernel is HIP only until it has been measured on NVIDIA (review follow-up).tq_use_dp4anow also takes RDNA2 (sdot4) and RDNA3.0 (sudot4). RDNA1 has no dot4, RDNA4 was measured slower on dp4a, and RDNA3.5 has not been measured, so they stay scalar.GGML_TQ_DP4A=1/0forces either way. If anyone has RDNA3.5 hardware to test on, please do.Results
llama-bench -fa 1 -n 128,GGML_TQ_NATIVE=1, ROCm 7.2.3. "scalar" is this branch withGGML_TQ_DP4A=0.TQ4_1S on the MI210 doesn't move: CDNA already used the TQ4 dp4a kernels.
Kernel time,
test-backend-ops perf -o MUL_MAT, m=4096 k=14336, us/run (base -> this PR):The MI210 TQ4_1S row isolates commit 2: n = 1 and 2 are unchanged, n >= 3 drops by 16-32%.
Perplexity on the decode path,
-c 512 --chunks 4 -b 512 -ub 1, so every token goes through these kernels. The int8 activations stay within noise:Review follow-up: the MoE row rerun with 16 chunks on the V620. The scalar control matches base exactly:
-ub 1GGML_TQ_DP4A=0(Error bars are about +/-0.45 at 4 chunks and +/-0.31 at 16.
-b 512keeps one sequence per batch. With several sequences, hybrid models hit an unrelated bug in the fused TQ path, fixed in #414.)Testing
On all three cards, with
GGML_TQ_NATIVE=1so TQ4_1S stays TQ4_1S on the device:test-backend-ops -o MUL_MAT/-o MUL_MAT_IDgive tq3_1s 176/176 and 44/44, tq4_1s 167/167 and 44/44, by default and withGGML_TQ_DP4A=0. The cases include n = 3..8, so the two-row kernels run. The review added 18 cases per type with odd m (5, 33, 65) at n = 3..8, so the last warp of a two-row kernel owns one real row and one past the end. On the rebased head: this branch alone on the MI210 passes tq3_1s 183/183 and tq4_1s 174/174, by default and withGGML_TQ_DP4A=0. #416's head, which contains it, gives the same counts plusMUL_MAT_ID44/44 on all three cards.Limits of this testing
GGML_TQ_DP4A=1lets someone with an RDNA4 card try it.🤖 Generated with Claude Code