Skip to content

CUDA: native TFQ3 (3/4/5-bit 2D-FWHT) runtime + drafters for Qwen3.8-27B, GLM-5.3-Flash, and Flash Next - #4986

Merged
ashhart merged 11 commits into
mainfrom
feat/cuda-tfq3
Nov 26, 2026
Merged

ashhart merged 11 commits into
mainfrom
feat/cuda-tfq3

Conversation

@ashhart

@ashhart ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

Adds native CUDA runtime and speculative-drafter support for TFQ3 (3/4/5-bit 2D-AffineCoset/half4 + randomized 2D FWHT-128) checkpoints across all three shipped TFQ3 model families (sm_80+):

  • Shared CUDA TFQ3 core (zig/src/cuda/tfq3*.zig, zig/kernels/cuda/tfq3*.cu, tfq3_common.cuh):
    • Bounds-checked binary validators and loaders for PWV3 (pw_v3_tables.bin), TFPN (dense/non-MoE .tffpw), and TFPW (routed MoE .tffpw) v3 shards.
    • Direct PTX mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 dense kernels (tf_tfq3_wmma_sk8_{3,4,5}b with 3D grid.z for single-launch bit-exact verify across rows = 1..64 and tf_tfq3_wmma_fused_{3,4,5}b for wide-N lm_head and prefill), plus fused routed MoE kernels (tf_tfq3_moe_gu_swiglu, tf_tfq3_moe_down) supporting mixed 3/4/5-bit HeatAlloc experts.
  • Qwen3.8-27B (qwen3_5) CUDA TFQ3 + DFlash2 + DSpine:
    • Supports both pure BF16 residual checkpoints and MLX Q8/Q4 residual checkpoints, host-embed offloading, and full speculative decoding with DFlash2 and DSpine + copies.
  • GLM-5.3-Flash (glm) CUDA TFQ3 + MTP + DFlash2:
    • Loads nonmoe.tffpw + layer_03..44.tffpw + MLX/BF16 residuals, wires KDA/DSA/MLP/MoE TFQ3 projections, and shares the TFQ3 lm_head (DraftHead) with both built-in MTP (layers.45) and external DFlash2 drafters at zero extra VRAM.
  • Qwen3.8-Flash-Next (flashnext) CUDA TFQ3 + MTP + 3b/4b/8b PLE:
    • Loads routed layer_00..47.tffpw and MTP drafter mtp_00.tffpw layers, dequantizes MLX 8-bit/32 non-MoE weights, and adds MLX 3-bit/32 (plus 4b/8b) PLE n-gram row dequantization.

Verification (NVIDIA GB10, sm_121, CUDA 13.0)

  • Live Qwen3.8-27B-TFQ3 (tensorfold-native serve, greedy temp=0, max_tokens=256):
    • Coding (thinking=False, 100% bit-exact sha=738f62e12436): --no-drafts 15.1 tok/s → + DFlash2 52.5 tok/s (3.48×, 4.65 tok/round) → + DSpine 56.6 tok/s (3.75×, 5.12 tok/round).
    • Thinking (thinking=True, 100% bit-exact sha=1454fc526024): --no-drafts 15.1 tok/s → + DFlash2 43.8 tok/s (2.90×) → + DSpine 45.6 tok/s (3.02×).
    • Non-TFQ3 regression (Qwen3.8-27B-MLX-4bit): bit-exact nodraft == DFlash2 (15.0 → 62.0 tok/s).
  • Real GLM-5.3-Flash TFQ3 shards + live GLM-5.3-Flash-DFlash2:
    • nonmoe.tffpw dense 4b/5b sk8 + fused (lm_head, MLP, attention, shared expert): cos = 0.9999999–1.0000000 vs fp64, bit-exact r1 == r16 (0 diffs), 215–244 GB/s.
    • layer_03.tffpw MoE (288 experts, 3b/4b/5b, act=0, lim=7.0, top_k=8): cos = 0.9999967–0.9999989 vs fp64, bit-exact r1 == r16 (0/32,768 diffs).
    • Live GLM-5.3-Flash-DFlash2 (bits=4 0.93 GiB and BF16 2.11 GiB) wired to real TFQ3 DraftHead: passes full dflash_load + ingest + 5-layer pass + TFQ3 head + top-16 selection end-to-end.
  • Real Qwen3.8-Flash-Next TFQ3 shards + 3-bit PLE:
    • layer_00.tffpw target MoE & mtp_00.tffpw MTP drafter MoE (512 experts, 3b/4b/5b, act=1, lim=0.0, top_k=10): cos = 0.9999984–0.9999995 vs fp64, bit-exact r1 == r16 (0/25,600 diffs).
    • Verified MLX 3-bit/32 PLE shard ([2500012, 15] / [2500012, 5]) unpacking.
  • Host & static checks:
    • zig fmt --check clean; tools/zig/check_cuda_sources.py 12/12 passed across sm_80, sm_86, sm_89, sm_90, sm_120, sm_121.
    • All 15 host suites passed (test-cuda-host, test-kernel-symbols-host, test-cuda-release-flags-host, GLM/Qwen3.5/Flash-Next host suites).

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto origin/main (47a605aa, head 4ae547e5), adapted flashnext/cuda_moe_native.zig to dynamic s.slots (#3523), trimmed glm/cuda_engine.zig under 600 lines (pr_check.py --pr 4986: 0 error(s), 0 warning(s); lean 164 vs 164, 0 new), and added the BM=64, BN=128 Tensor Core prefill kernel (tf_tfq3_wmma_prefill64_{3,4,5}b, dispatched for rows >= 48 while keeping rows <= 32 on bit-exact sk8).

Live NVIDIA GB10 (sm_121) verification on Qwen3.8-27B-TFQ3:

  • Cold p3989 prefill: 341.8 -> 590.8 tok/s (1.73x faster, 11.67 s -> 6.75 s)
  • Greedy decode & speculative parity (128 tok): BM=16 (14.9 tok/s), BM=64 (15.0 tok/s), +DFlash2 (52.5 tok/s, 3.50x), and +DSpine (59.6 tok/s, 3.97x) all match 100% bit-exact (sha=d4942ce0f4b2).

@jschmied

Copy link
Copy Markdown
Contributor

@ashhart four error-path defects in the CUDA TFQ3 loader, read at b69a8bd415 (not run):

  1. Device double free when a shard fails to load (zig/src/cuda/tfq3.zig:369-387). loadDir and loadFile keep errdefer tables.free() after errdefer store.deinit(), and deinit frees store.tables too. In addFile (:403-408), s_buf and w_buf keep their own errdefer free() after they are appended to store.buffers, so any later error in the tensor loop frees them twice. DeviceBuffer.free asserts on the second free.
  2. Host double free on a duplicate tensor name (:412-416). The duplicate branch calls store.gpa.free(name_dup) and then returns an error, which runs errdefer store.gpa.free(name_dup) again. The same return also takes path 1.
  3. Kernels.reserve frees before it allocates (:119-132). If the new DeviceBuffer.alloc fails, xrot / yr / act_rot still hold the freed buffer and deinit (:137-139) frees it again. Allocating first, or setting the optional to null and its size to 0 before the allocation, closes it.
  4. Tensor offsets are not checked for 16-byte alignment (tfq3_format.zig). The sections are checked against section_align, but word_off (in 4-byte words) is not, and tfq3.cu:50-52 reads the weights as uint4. A shard whose word_off is not a multiple of 4 passes validation and then does a misaligned 16-byte load.

A possible fix for 1 and 2: commit to the store transactionally. Reserve buffers and tensors capacity first, drop the local errdefers once ownership moves, and remove the explicit free in the duplicate branch. A test that loads a shard with a duplicated canonical name would cover 1 and 2 together.

ashhart and others added 11 commits November 26, 2026 14:54
Reject misaligned tensor offsets and orphan table sidecars; preserve finite table refusals.

Co-authored-by: jschmied <[email protected]>
Keep explicit output-sign periods repeating across narrowed views. Add boundary, rounding and loader regression checks.
…release SMs

Measured on a GB10 with nvcc 13.0.88 by tf-cuda-test kernel-contract-limits over the release fatbins: 14 RECORD lines, five SM slots each. Later compilers keep their independent pending selection.
@ashhart
ashhart merged commit 630a8eb into main Nov 26, 2026
1 check failed
@ashhart
ashhart deleted the feat/cuda-tfq3 branch November 26, 2026 14:54
@ashhart

ashhart commented Nov 26, 2026

Copy link
Copy Markdown
Owner Author

Thank you @jschmied. This is merged into main with your commits under your own name, so you're credited as a TensorFold contributor.

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