Skip to content

CUDA: fuse bit-exact Qwen3.8 BF16 prefill launches - #1439

Merged
ashhart merged 1 commit into
ashhart:mainfrom
IF-YFXJ:feat/qwen38-bitexact-prefill-gb10
Oct 28, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
IF-YFXJ:feat/qwen38-bitexact-prefill-gb10

Conversation

@IF-YFXJ

@IF-YFXJ IF-YFXJ commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1398

Scope

This is a bit-exact BF16 prefill launch optimization for the Qwen3.8 CUDA affine-4 path. It does not use E4M3 activation quantization or prompt-only E4M3 gate/up weights, and it does not change decode, KV-cache, GDN recurrent-state arithmetic, projection operands, accumulation order, or rounding.

The branch is one commit (b47b537d) on current upstream main (1b9a987e).

Implementation

  • tf_qmm_prefill_many runs up to four prompt projections in one grid. Each projection still executes the shipped BF16 qmm body with its own original block order; only host setup, grid launch, address selection, and BF16 input loading are shared.
  • The existing 2x2-warp/two-stage tile is admitted only for the measured large-prompt Qwen3.8 widths; all other geometries retain the main path.
  • The shipped tf_qmm_prefill kernel body remains literal. Refactoring it through the shared helper was rejected because it changed Blackwell SASS under nvcc 13.0/13.3.
  • The Qwen3.8 projection path selects the fused launch only for the exact supported geometry and otherwise falls back to separate launches.
  • Contract coverage is split into focused files: contract_qmm_prefill_many.zig (385 lines) and contract_qmm_prefill_tile22.zig (125 lines). The aggregate contract_tests.zig is exactly 598 lines and contract_limits.zig is 170 lines.
  • Both candidates have independent planted controls that swap equal-width output bindings. Both controls are discriminating and red at the shapes gate.
  • The tile22 limit registration has no min_sm; the many launch retains its GB10 admission.

Final-head host validation (b47b537d)

  • zig fmt --check build.zig zig: pass.
  • zig build test-kernel-contract: pass.
  • git diff --check: pass.
  • Latest GB10/sm_121 focused contract, nvcc 13.3 fatbins; the final CI fix changes only a comment and does not change executable test logic:
    • qmm_prefill_many: 1 pass, 0 fail; 1 controls red, 0 missed; four served shapes, five runs byte-identical, 487,424 output bytes.
    • qmm_prefill_tile22: 1 pass, 0 fail; 1 controls red, 0 missed; six served shapes, five runs byte-identical, 1,835,008 output bytes.
  • The candidate CUDA runner compiled successfully after upstream moved the CUDA family builders into cuda_engines.zig; the later CI fix is comment-only.
  • test-cuda-host test-kernel-contract: 272/274 steps, 1,670/1,699 tests passed, 28 skipped. The only actual failure is the existing Windows video.zig test exhausting host memory; the focused kernel-contract step passed.

Full GPU contract and live limits

A complete nvcc 13.3 GB10 run immediately before the final upstream-only rebases produced:

SUMMARY 422 kernels: 422 pass gates 1-4, 0 fail, 6 known findings; 314 controls: 314 red as planted, 0 missed; 214 limit problems

The final rebases and lean-check fix did not change the qmm runtime or focused-contract executable logic; the focused contracts were rebuilt and rerun before the final comment-only fix. In the full run, both new candidates passed nonfinite, repeat, shapes, and live-limit gates:

  • many: 256 threads, 128 registers on sm_121, 62,976 dynamic shared bytes.
  • tile22: 128 threads, 220 registers on sm_121, 41,984 dynamic shared bytes.

The full command exits nonzero because the repository currently has 214 unrelated live-limit records with missing/outdated receipts (principally existing qwen4 entries); neither new qmm registration is among them.

Compiler receipts and SASS

Independent source/cache/output directories built 138 five-architecture fatbins (sm_80,86,89,120,121) with nvcc 13.0, 13.2, and 13.3. The new records are current in all three compiler audits.

Candidate nvcc 13.0 regs (80/86/89/120/121) nvcc 13.2/13.3 regs bound
many 171/171/171/128/128 172/172/172/128/128 256
tile22 255/255/255/214/214 255/255/255/220/220 128

nvcc 13.2 and 13.3 produced identical new receipts, so one qmm_prefill_132.zig override covers both. The existing shipped prefill instantiations are SASS-identical to current main across all five SMs for both nvcc 13.0 and 13.3. The compared SASS dump SHA-256 values are 90b91b0c5e0651f21ff7434344b953501dc4956c9743415d29ce72247cd26aba (13.0) and dcae3cd480b5486d55aa74559ebb0224122648bf788f877cadfd92fa0e511fda (13.3).

Prior same-path correctness evidence

Before the review rework, the same runtime optimization path had token-hash parity for C1/C8, concurrent/solo, drafted/serial, and fresh/resume; 29/29 planned-grid fresh/cache-on replies matched full token SHA-256, and KV-cache/GDN state receipts were byte-identical. The final review changes preserve the runtime arithmetic path and add stricter contracts, but those end-to-end model parity scenarios were not rerun at b47b537d.

Frozen same-stack A1-B-A2 performance evidence

This is prior evidence for the same runtime path, not a new benchmark on final head. Frozen contract: NVIDIA GB10 sm_121; Qwen3.8-27B MLX affine-4 plus DFlash2 drafter; BF16 prompt; FP8 KV; 2,048 input tokens; 512 output tokens; greedy seed 11230; prefix cache off; three repetitions; C1 and C8. A1/A2 used upstream 8c36359d; B used cd3df397.

Metric A1 B A2 B vs A1/A2 median
C1 prefill tok/s 1278.059 1405.535 1288.529 +9.526%
C8 aggregate prefill tok/s 1170.803 1267.958 1172.558 +8.217%
C1 decode tok/s 46.086 46.477 45.915 +1.036%
C8 aggregate decode tok/s 115.3 117.5 114.5 +2.263%

Maximum A1/A2 drift was 0.816%. The frozen gate required <=5% drift, >=3% C1 prefill gain, and no metric below 97% of the A1/A2 median; it returned qualified: true.

Overlap audit

atc overlap --base upstream/main --json reports 20 changed files and no open-PR overlap. It reached its time limit (complete: false, timeLimit); the only reported conflicts are with local historical experiment worktrees, not remote PRs.

@ashhart

ashhart commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thanks @yguanrong. As far as we can read, the bit-exactness argument holds. tf_qmm_prefill_many runs the same prefill_body<64, 128, 128, 2, 4, 3> as the shipped launch, on projection-local block ids with the same group. The 2x2 tile keeps each element's single fp32 chain over K, in the same group and k16 order. Both are gated to the MLX 27B on a GB10. On the PR head, fmt, lean, test-cuda-host, test-kernel-contract and the CUDA cross-compile pass, and the new qmm_prefill.cu pin matches.

  1. Please rebase onto current main as one commit, without the merge commit. A merge now conflicts in zig/tests/cuda/contract_limits.zig, where main has since added an image to the same list; keep both names. After the merge contract_tests.zig comes to 601 lines, over the 600-line limit, so please trim it, for instance by letting the focused files append their own gates in place of contract_tests.zig:209-214.
  2. Neither new registration has a planted control. Other registrations plant one, for example contract_glm_index.zig's swapped table. Could you add one for tf_qmm_prefill_many (say, two parts' first swapped) and one for the 2x2 gate, each of which must go red? The 2x2 record (registry.zig:43) could also drop min_sm = 121, since main already serves that tile on every other GPU.

Then, on a GB10, we'll run qmm-prefill-many-contract, qmm-prefill-tile22-contract and kernel-contract with nvcc 13.0 and 13.3, and diff the SASS of the two existing prefill instantiations. We'll also run the MLX 27B with DFlash2 against main: cold prefill from 2k to 64k, token hashes at levels 1 and 8, drafted == plain, and resumed == fresh with tails of 17 and about 200 rows past a kept state, so that the fused path runs.

@IF-YFXJ

IF-YFXJ commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review requests in 7c5135c1:

  • Rebased onto current main (db3fd5ab) as exactly one commit, with no merge commit. The latest upstream CUDA builder split is retained.
  • Resolved the limits conflict without dropping upstream registrations (nvfp4_experts_tile and glm_index remain); the two qmm registrations are added alongside them.
  • Split candidate coverage into contract_qmm_prefill_many.zig (386 lines) and contract_qmm_prefill_tile22.zig (125 lines). contract_tests.zig is exactly 600 lines; contract_limits.zig is 170 lines.
  • Added a discriminating planted output-binding control for each candidate. Final-head GB10 results:
    • SUMMARY qmm_prefill_many: 1 pass, 0 fail; 1 controls red, 0 missed
    • SUMMARY qmm_prefill_tile22: 1 pass, 0 fail; 1 controls red, 0 missed
  • Removed min_sm from the tile22 limit registration.
  • Added five-SM compiler receipts from independent nvcc 13.0, 13.2, and 13.3 builds. nvcc 13.2/13.3 produce the same new records; nvcc 13.0 differs only in the recorded register counts.
  • Restored the shipped prefill kernel body literally after detecting that helper reuse changed Blackwell codegen. Existing prefill SASS is now byte-identical to main across sm_80/86/89/120/121 under both nvcc 13.0 and 13.3.
  • Complete nvcc 13.3 GB10 gates before the final upstream-only rebases: 422/422 pass gates 1-4, 314/314 controls red, 0 gate failures. Both new live limits pass. The command remains nonzero only because of 214 unrelated existing live-limit receipt problems; this boundary is called out in the PR body.
  • Recompiled the CUDA runner after the final upstream cuda_engines.zig refactor and reran both focused contracts at exact final head.

The PR body now separates final-head proof, pre-final full-contract evidence, existing repository limit debt, and the prior frozen A-B-A performance/model-parity evidence. Please re-review when convenient.

@IF-YFXJ
IF-YFXJ force-pushed the feat/qwen38-bitexact-prefill-gb10 branch from 7c5135c to ebd934c Compare October 28, 2026 09:34
@IF-YFXJ

IF-YFXJ commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the failing lean check from run 40628998581/job 122707245988.

The only new finding was:

zig/tests/cuda/contract_qmm_prefill_many.zig: multi-line comment (1 new)

I collapsed the two-line explanatory comment into one line; no code, contract shape, control, or numerical behavior changed. I also rebased the single commit onto current main (80b30ed8). New head: ebd934c1.

Validation after the rebase:

  • lean comparison: 432 findings on base, 432 on head, 0 new
  • zig fmt --check build.zig zig: pass
  • zig build test-kernel-contract: pass
  • git diff --check: pass
  • branch remains exactly one commit ahead

The first parallel local contract attempt hit host OOM while two full-repository lean scans were running; its 116 executed tests passed. A standalone rerun after those scans exited passed completely.

@IF-YFXJ

IF-YFXJ commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Maintainers: the PR branch has been rewritten to remove the merge commit and is now exactly one optimization commit on the latest main.

  • Base: 1b9a987e971ee9c1e08e1cf3ff8433ccc5f4859d
  • Head: b47b537d1b7c26fb76e434137667452263719bf1
  • PR-only commits: 1
  • PR-only merge commits: 0
  • zig fmt --check build.zig zig: pass
  • zig build -j1 test-kernel-contract: pass
  • Lean scan: 0 new findings
  • git diff --check: pass

The new fork workflows are waiting for approval:

Please click Approve and run workflows for this new head. Thank you.

@ashhart
ashhart force-pushed the feat/qwen38-bitexact-prefill-gb10 branch from b47b537 to 8b303f3 Compare October 28, 2026 14:18
@ashhart
ashhart merged commit a05d620 into ashhart:main Oct 28, 2026
@ashhart

ashhart commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thanks @yguanrong, this is merged. On a GB10 with Qwen3.8-27B MLX 4-bit, alternating main, PR, PR, main:

  • Prefill: +10.8% at 2k, +9.7% at 8k and +7.8% at 32k.
  • Tokens: every prompt row equals main's. Decode and the 1 to 8 stream sweep are unchanged.
  • Kernel contract on sm_121: your two new kernels pass gates 1-4, and both planted controls go red at the shapes gate as planted, with none missed.

Thanks for keeping the shipped kernel body literal, and for splitting the contract into its own files.

ashhart added a commit that referenced this pull request Oct 28, 2026
…'s wide decode select (#1424), its indexer split across ranks (#1404) and its share-billed prompt step ending on the GPU (#1352)
@IF-YFXJ
IF-YFXJ deleted the feat/qwen38-bitexact-prefill-gb10 branch October 28, 2026 15:39
@ashhart

ashhart commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Thanks @yguanrong: the fused BF16 prefill launches (Qwen3.8-27B prefill +8.8% to +11.1% on a GB10 against 1.1.8, same tokens) shipped in TensorFold 1.1.9.

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.

CUDA/Qwen3.8: bit-exact BF16 prompt launch fusion on GB10 (proposal and gate)

2 participants