Skip to content

perf(qwen35/hip): the prompt conv writes q, k and v to their own rows - #2117

Merged
ashhart merged 2 commits into
ashhart:mainfrom
AbrahamCete:perf/hip-qwen-prompt-conv-split
Nov 4, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
AbrahamCete:perf/hip-qwen-prompt-conv-split

Conversation

@AbrahamCete

Copy link
Copy Markdown
Contributor

What this changes

In the prompt forward, Qwen 3.5's linear attention wrote the conv's output (tf_conv_prefill) to one fp32 buffer of rows by channels. Then splitQkv ran tf_copy_cols three times, for each layer and span of rows, to pull q, k and v apart before the q and k norms. In a 2048-token prompt pass that is 144 of the 27B's 160 tf_copy_cols launches, and 90 of the 35B-A3B's 100.

This PR gives tf_conv_prefill an optional split (ConvSplit):

  • With key_width set, each thread picks its channel's output once: q, k or v, each with its own contiguous rows. The sums and their order are unchanged, so every value has the same bits; only where it is stored changes.
  • Without it, the kernel writes rows by channels as before. Flash Next's chunked conv keeps that form.
  • splitQkv is now normQk, the two norms alone.
  • The tf_copy_cols launches left are the full-attention layers' split of q from its gate. This PR leaves them alone.
  • There is no new kernel symbol.

The new test kernel_tests/conv_split.zig runs 37 rows and 364 channels with a key width of 96, so a split falls inside the first 256-channel block and the values end inside the second. The split outputs hold the bytes of the single output's columns, and the new conv state is the same.

Receipt

  • Environment: Ryzen AI Max+ 395 (gfx1151), Ubuntu 26.04, Zig 0.17.0, hipcc with HIP 7.1.52801 and Ubuntu clang 21.1.8 (rocprofv3 from ROCm 7.13). Checkpoints: Qwen3.8-27B-MLX-4bit and Qwen3.6-35B-A3B-MLX-4bit-MTP. Base: main 45876f5. Both sides were built from git archive trees.

  • Kernel time, rocprofv3 over a 2048-token tf-qwen35-test prefill run, per prefill:

    • 27B: the conv and the column copies take 35.9 ms against 74.1 ms, with 16 tf_copy_cols launches instead of 160.
    • 35B-A3B: 18.3 ms against 37.6 ms, with 10 launches instead of 100.
    • The conv itself takes the same time: 30.0 against 29.7 ms on the 27B, 15.6 ms on both sides on the 35B-A3B.
  • tf-qwen35-test prefill, one prefill after a warm-up, alternated B C B C B C, seconds:

    model, tokens main this PR
    27B, 2048 5.307 / 5.276 / 5.317 5.238 / 5.260 / 5.262 (-0.9%)
    27B, 8192 22.076 / 22.117 / 22.222 21.997 / 22.040 / 22.148 (-0.3%)
    35B, 2048 1.007 / 1.020 / 1.017 0.991 / 0.996 / 1.005 (-1.7%)
    35B, 8192 4.650 / 4.648 / 4.650 4.578 / 4.571 / 4.575 (-1.6%)
  • Served, one fresh server a run, alternated B1 C1 B2 C2:

    • tools/bench_cache.py --require-cold --tokens 16, --context 34816 --parallel 1 --prompt-cache-gib 0, ttft_s:

      model, tokens main this PR
      27B, 2048 5.312 / 5.334 5.323 / 5.378 (+0.5%)
      27B, 8192 22.306 / 22.367 22.248 / 22.469 (+0.1%)
      27B, 32768 103.797 / 104.367 103.229 / 103.972 (-0.5%)
      35B, 2048 1.174 / 1.183 1.166 / 1.177 (-0.6%)
      35B, 8192 5.086 / 5.110 5.037 / 5.069 (-0.9%)
      35B, 32768 26.479 / 26.438 26.195 / 26.257 (-0.9%)

      Every cold request's token_sha is the same on both sides, with 0 cached tokens. On the 27B the copies were 0.7% of the pass, and these runs don't resolve it: their spread is about as large.

    • tools/bench_concurrent.py --levels 1,4 --tokens 256 --reps 3 --temperatures 0 --alone --serial, --parallel 4: token hashes are equal on every server (35B code 9f2923ac14d0, chat a8c28b8297b4; 27B code 2b28aa2108de, chat a06fab348fbe), 0 failed. Decode is unchanged: code at 4 streams 231.5 / 230.6 on main and 231.2 / 231.2 here on the 35B; 44.0 / 43.9 and 43.8 / 44.0 on the 27B.

  • Exactness:

    • Prompt logits (tf-qwen35-test logits, 2048 tokens) are byte-identical to main's on both checkpoints, bf16 and --f32 (27B dcc85692582d / 232dd4cf1fcd, 35B 18bee11563f1 / 84fbaeb871d7).
    • tf-qwen35-test check --only invariants: 27B 42 passed, 0 failed, 5 skipped; 35B-A3B 46 passed, 0 failed, 1 skipped, the same on both trees.
  • Tests run:

    • zig build test -Dhip=true --summary all: 570/570 steps, 7189/7251 tests passed, 62 skipped.
    • zig build hip-kernel-test --summary all: 132/136 passed, 4 skipped, 0 failed. The new test is not among the skips: before the change it didn't compile, and a planted offset in the split path fails it alone.
    • Kernel contract: SUMMARY gfx1151 93 kernels: 93 pass gates 1-4, 0 fail, 2 known findings; 11 controls: 11 red as planted, 0 missed; 147 limit problems, the same as main.
    • tools/zig/lean_check.py: 425 problems, the same as main. zig fmt --check is clean.
  • Not run, and why:

    • gfx1030, gfx1100, gfx1150 and gfx1201, because I have no card.
    • Tensor parallel, because I have one GPU. The split takes the same key width and channel count as the copies did.

@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thanks @AbrahamCete. Writing q, k and v to their own rows from the conv drops 144 of the 27B's 160 column copies with the same sums, and the byte test against the single output pins it. Logits byte-identical on both checkpoints and served tokens equal, with the prefill tool faster and the served cells inside their spread, is what this needed. Ready to land.

ashhart added a commit to AbrahamCete/TensorFold that referenced this pull request Nov 4, 2026
@ashhart
ashhart force-pushed the perf/hip-qwen-prompt-conv-split branch from 43905bd to af4af0a Compare November 4, 2026 22:20
AbrahamCete and others added 2 commits November 4, 2026 23:59
The linear attention's prompt forward wrote the conv's channels to one [rows, channels] buffer, then ran tf_copy_cols three times to pull q, k and v apart before the q and k norms. tf_conv_prefill now takes an optional split: with key_width, channel c goes to q, k or v at its own contiguous row, and the three copies are gone. Each output keeps the same sum, so the bytes are unchanged. Flash Next keeps the single output.
@ashhart
ashhart force-pushed the perf/hip-qwen-prompt-conv-split branch from af4af0a to e5104eb Compare November 4, 2026 23:59
@ashhart
ashhart merged commit 5bfa38d into ashhart:main Nov 4, 2026
0 of 7 checks passed
@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Thank you @AbrahamCete. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

@ashhart

ashhart commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants