Skip to content

cuda w4a8: smaller tiles of the int8 prompt GEMM where the 128x128 grid leaves SMs idle (TF_QWEN_INT8_TILES, default off) - #6098

Merged
ashhart merged 4 commits into
ashhart:mainfrom
jojomondag:qwen-cuda-int8-tiles
Dec 4, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
jojomondag:qwen-cuda-int8-tiles

Conversation

@jojomondag

@jojomondag jojomondag commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The int8 prompt GEMM (--prompt-precision int8, tf_w4a8_prefill) runs 128 x 128 tiles, one block an SM. A projection whose grid is at most one wave of blocks pays one block's whole K loop whatever its size, which is every turn that adds a short message or tool result to a kept conversation. With TF_QWEN_INT8_TILES=1 (default off) w4a8_prefill.cu also instantiates prefill8_body at 64x128, 32x128 and 16x64, and chooseTile(rows, n, k, sms) takes the tile with the least predicted time, on a card of 170 SMs (tuned_sms, the count its constants were measured on); a card of another SM count keeps the 128 x 128 tile. A tile only changes which outputs a block holds: each output's integer group dots and its ascending fma chain over the groups are the same, so the bytes are the same. Unset, every projection takes tf_w4a8_prefill as today.

Checks on this head

  • Head commit: 773ef4643 on main 0ba35fe6d.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 88 and 88, 0 new.
  • Tests run, with the command and pass, fail and skip counts:
    • zig build -Dfixed-cpu test-host: exit 0, no failed step.

    • zig build test-w4a8-contract-host (reached by test-host): the chooser's picks at 170 SMs, that the switch off and cards of 0, 48, 84, 128 and 188 SMs keep the 128 x 128 tile, that the chooser's row boundaries are served contract shapes, and the tiles' shared memory. A wrong expectation planted in it fails the step (expected 1, found 0, exit 1).

    • zig fmt --check build.zig zig and git diff --check: clean. zig build native and zig build compile-cuda-tests with -Dsm=120 compile.

    • tf-cuda-test kernel-contract w4a8 on one RTX 5090, on this head: each of the three tiles passes gates 1 to 4, and shapes reports 272 shapes bit-equal to the kernel it replaces for each; the planted control (a tile run one input group short) differs from byte 0.

      SUMMARY 5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 5 limit problems
      

      The run exits 1 on the 5 limit problems, which are the usage audit under my device compiler, nvcc 13.4, where this image has no record set: all five launches print recorded 0, 0, and main 0ba35fe6d built the same way prints the same for its two. tf-cuda-test w4a8 exits 0.

    • kernel-contract-limits on fatbins built by nvcc 13.0.88 for sm_80/86/89/120/121: record current for the three new launches on all five (147/147/147/125/125, 118/118/118/76/76 and 96/96/96/72/72 registers). The records are marked later_pending, since I have no 13.2 or 13.3 compiler for the sets between.

  • CI: not run yet on this pull request.
  • Not run, and why: sm_121 and every SM class but 170 (the constants are fitted on one RTX 5090, so a card of another SM count keeps the 128 x 128 tile); several streams; the issue's kernel duel and kept-state byte comparison on this head (they ran on the same kernel body in a v1.3.5-based build; on this head the contract's shapes gate compares the tiles on the device); Metal, HIP, XPU (CUDA-only change).

Receipt

  • Main against branch: cells and numbers, or why they can't be run: the switch unset against set on this head (unset is main's path: every projection takes tf_w4a8_prefill). One RTX 5090, one server an arm, order unset, set, set, unset in one run; --drafter --kv-cache fp8 --prompt-precision int8 --context 262144 --parallel 1 --prompt-cache-gib 24, TF_QWEN_REQUEST_STARTS=1 TF_QWEN_PROMPT_SPLITK=1 in every arm, tools/bench_tail_turns.py --user-words 400 --turns 4 --seed 1234. Median first token of the turns that each add one message (ms), the two servers of an arm:

    conversation turn adds unset set change
    75 exchanges, 33k tokens 43 rows 302 / 301 247 / 246 -55 ms
    126 rows 362 / 361 320 / 318 -42
    426 rows 420 / 424 382 / 378 -42
    926 rows 592 / 593 551 / 550 -42
    320 exchanges, 140k tokens 43 rows 922 / 916 862 / 862 -57
    126 rows 1,331 / 1,330 1,290 / 1,289 -41
    426 rows 1,396 / 1,402 1,351 / 1,346 -51
    926 rows 1,795 / 1,795 1,747 / 1,761 -41

    Cold reads (cut at message starts, unmerged): 11.72 / 11.73 -> 10.03 / 10.05 s at 33k, and 85.7 to 86.3 -> 78.7 to 79.2 s at 140k (four reads a server). Every reply's token and output hash equals the first server's, in all four servers of both runs. No panic line in the eight server logs.

    The tree that ran these differs from d123ff74d in one string, the issue number in the profile row. 773ef4643 changes no kernel and, on this 170-SM card, no pick.

  • One launch, today's tile against the chosen one (the issue's table, a v1.3.5-based build with the same kernel body): 7 to 32 rows 70-72 us -> 15.6-23 us (16x64); 110 rows 89 / 245 us -> 36 / 95 us (32x128); 410 rows 223 / 298 -> 150 / 215 us (64x128). Over the duel's 77 cells the chooser lands within 1.3 % of the best of the four tiles.

  • Hash run (tools/perf_receipt.py of ci: add tamper-evident performance receipt hash run and GitHub verifier #6078 at 51424e47). Base 0ba35fe6d against head 773ef4643, both arms served with --prompt-precision int8 and TF_QWEN_INT8_TILES=1 in the environment (the base build does not know the switch), two mirrored blocks, drafted: tokens equal on all four prompts, the planted control diverged, drafted equals "draft": false; first token (the receipt's ttft_s) 0.057 -> 0.032 s on each of the four prompts. Decode reads 329.73 -> 336.31 tok/s (+2.0 %, base against base 0.15 % apart), and I do not claim that for the change: the same two binaries with the switch unset read 328.59 -> 335.20 tok/s (+2.0 %, first token 0.057 -> 0.057 s), so the decode difference belongs to this build of the head and not to the switch, and the previous head's build read level (328.93 -> 328.65 tok/s) with the same first-token gain. The pack scores decode tok/s on four prompts of 65 to 89 tokens with the prompt cache off; this change acts in the prompt pass. The receipt is attached in a comment, not committed: a receipt committed on the branch cannot name the commit that holds it, so --expect-head would call it stale.

  • Boxes it reaches: the int8 prompt GEMM (--prompt-precision int8), whose callers on main are Qwen 3.5/3.8 on CUDA, only with the switch set and on a 170-SM card. bf16 prompts and decode keep their kernels. Large passes see the chooser too: at 3,945 to 8,192 rows it takes 64x128 for N of 1,024 and up and 16x64 for the 48-wide projections (the issue's numbers: worst cell in the duel 2.4 % slower, a merged 140k read 1.1 s sooner).

  • New switch? Default, accepted values, profile row: TF_QWEN_INT8_TILES, 0 or 1, unset 0, profile class identical, in docs/environment.md, docs/environment-names.md, platform/environment.zig, environment_valid_cases.zig, core/profile.zig and the CLI's environment list. No stored-data change; at most about 36 KB of shared memory a block (less than today's 55 KB).

  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: head 773ef4643 on main 0ba35fe6d, zig build native -Dsm=120, Zig 0.17.0, nvcc 13.4 (usage records from nvcc 13.0.88); one RTX 5090 32 GB (170 SMs), driver 615.78.08, Ryzen 7 9800X3D, Linux, nothing else on the GPU during a timed run; TensorFold/Qwen3.8-27B-MLX-4bit@22d8d538, drafter z-lab/Qwen3.8-27B-DFlash2@50307d4c.

  • Paths and platforms the change reaches, which I ran and which I could not: ran on sm_120: int8 prompts over whole fp8 caches, short turns and cold reads, with request starts and the split passes beside it. Could not run: sm_121 and other SM classes, several streams.

  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: the fixture's token and output hashes in every server, the contract's byte comparison of each tile with tf_w4a8_prefill (272 shapes a tile, a planted control), and the hash run's four prompts (base and head token-equal, drafted equal to "draft": false).

  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): the table above (unset is main's path) and the hash run (main's binary against the head's).

  • Decode speed before and after, from tools/bench_openai.py: the switch is read only by the int8 prompt GEMM; the hash run's decode tok/s is the same with the switch unset and set (335.20 and 336.31 tok/s on this head's build; 328.6 to 329.7 on main's).

  • Prompt speed before and after: the tables above.

  • Licence of any new weights or drafter, or "n/a": n/a

Checklist

  • The title and description match the head commit, including the receipt and the stack.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.

  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. Drafted against "draft": false is shown by the hash run on one prompt, and the issue's two disk stores resume each other (12 of 12 replies, the v1.3.5-based build). Concurrent against solo was not run on this change.

  • No precision traded for speed. If the bits change, the description says which sums change and why.

  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled. Measured: one stream with --drafter. Several streams and the drafter off were not measured.

@jojomondag

Copy link
Copy Markdown
Contributor Author

The hash run named in the description: tools/perf_receipt.py and its prompt pack from #6078 at 51424e47, base 0ba35fe6d against head d123ff74d, one RTX 5090, --pairs 2 --draft --check-draft, servers started with --drafter --kv-cache fp8 --prompt-precision int8 --context 262144 --parallel 1, TF_QWEN_INT8_TILES=1 in the environment of both arms (the base build does not know the switch).

  • Tokens: all four prompts equal between base and head, the planted control diverged, drafted equals "draft": false.
  • Decode, the scored number: 328.93 -> 328.65 tok/s (-0.08 %, base against base 0.12 % apart): level, as it should be for a change in the prompt pass.
  • First token, the receipt's ttft_s: 0.057 -> 0.032 s on each of the four prompts (65 to 89 prompt tokens, read cold).

verify --expect-base --expect-head accepts the hashes, the SHAs and the control and fails on the speed floor; with --allow-no-speedup it passes. What the pack can and cannot show for a prompt-pass change, and why the file is attached and not committed, is in #6056 (comment).

perf-receipt.json (receipt_sha256 69914d1085c6b05f...)
{
  "schema": 1,
  "pack_id": "tf-receipt-v1",
  "pack_sha256": "ab2a5d0ff262007e3ef4714fa46e62b2b2454b3f97b652ca18da49e4e0583c42",
  "generated_at_utc": "2026-12-03T00:50:48Z",
  "claim": "With --prompt-precision int8 and TF_QWEN_INT8_TILES=1 the head serves the same tokens as main; the change is in the prompt pass, so decode tok/s is level.",
  "git": {
    "base_sha": "0ba35fe6de5b290d660196add7ccc2be17998987",
    "head_sha": "d123ff74da1d682f6b2a6bd082abd60b37e959b5"
  },
  "environment": {
    "chip": "rtx-5090",
    "os": "linux",
    "arch": "x86_64",
    "checkpoint_id": "TensorFold/Qwen3.8-27B-MLX-4bit@22d8d538",
    "draft": true,
    "base_binary_sha256": "acb2594cb6cd59ec266e4c9085bdeb80b84fff01a23f7a1dc506b06b74cbebb9",
    "head_binary_sha256": "09e3aea6491aab2e59466892862a22ff1baedfc95efb29fedc06b975f45a35d5"
  },
  "control": {
    "prompt_id": "prose_seeded",
    "seed_a": 424242,
    "seed_b": 989898,
    "base_token_sha256": "3fba05a0ae462fc6e4ba0023037275856ae0c71e2e61ac42c764e43ccaf9541f",
    "alt_token_sha256": "a6c9d7d6e740c26e5aacf7eeae76c7ce1faa1927e2296b2782c199c9259ba49e",
    "diverged": true
  },
  "draft_check": {
    "prompt_id": "prose_greedy",
    "plain_token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
    "draft_token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
    "equal": true
  },
  "order": [
    "base",
    "head",
    "head",
    "base",
    "base",
    "head",
    "head",
    "base"
  ],
  "rounds": [
    {
      "index": 0,
      "arm": "base",
      "agg_tok_s": 328.783
    },
    {
      "index": 1,
      "arm": "head",
      "agg_tok_s": 328.205
    },
    {
      "index": 2,
      "arm": "head",
      "agg_tok_s": 328.398
    },
    {
      "index": 3,
      "arm": "base",
      "agg_tok_s": 328.735
    },
    {
      "index": 4,
      "arm": "base",
      "agg_tok_s": 329.121
    },
    {
      "index": 5,
      "arm": "head",
      "agg_tok_s": 328.976
    },
    {
      "index": 6,
      "arm": "head",
      "agg_tok_s": 329.025
    },
    {
      "index": 7,
      "arm": "base",
      "agg_tok_s": 329.073
    }
  ],
  "summary": {
    "mean_base_tok_s": 328.928,
    "mean_head_tok_s": 328.651,
    "gain_pct": -0.084,
    "base_noise_pct": 0.117
  },
  "prompts": {
    "prose_greedy": {
      "base": {
        "tokens": 128,
        "prompt_tokens": 71,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
        "output_sha256": "193650b0e6bfdd761c9cc7caf3154ec4d7fcd78072d3b3bb6ba094b516ab5384",
        "tok_s": 245.838,
        "ttft_s": 0.0569
      },
      "head": {
        "tokens": 128,
        "prompt_tokens": 71,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
        "output_sha256": "193650b0e6bfdd761c9cc7caf3154ec4d7fcd78072d3b3bb6ba094b516ab5384",
        "tok_s": 245.778,
        "ttft_s": 0.0322
      },
      "token_equal": true
    },
    "code_greedy": {
      "base": {
        "tokens": 128,
        "prompt_tokens": 78,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "0b47594d2975969cb4c72df23c9df9cfbe1ce63c166348a5843bf9e076f311c5",
        "output_sha256": "f419036bdae542ded35e3c7f39fcbf066d2dc41dbe918014a03aaac7e699fff7",
        "tok_s": 312.335,
        "ttft_s": 0.0572
      },
      "head": {
        "tokens": 128,
        "prompt_tokens": 78,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "0b47594d2975969cb4c72df23c9df9cfbe1ce63c166348a5843bf9e076f311c5",
        "output_sha256": "f419036bdae542ded35e3c7f39fcbf066d2dc41dbe918014a03aaac7e699fff7",
        "tok_s": 312.228,
        "ttft_s": 0.0327
      },
      "token_equal": true
    },
    "reasoning_greedy": {
      "base": {
        "tokens": 128,
        "prompt_tokens": 89,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "7da5c3a040233b558d6258914310660966d99ff3fc5a1f06c520a0344399cee2",
        "output_sha256": "8912fceb7f3c2c57054d8cb31f1626badb73c31745656efe32f517c9b600428a",
        "tok_s": 508.993,
        "ttft_s": 0.0577
      },
      "head": {
        "tokens": 128,
        "prompt_tokens": 89,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "7da5c3a040233b558d6258914310660966d99ff3fc5a1f06c520a0344399cee2",
        "output_sha256": "8912fceb7f3c2c57054d8cb31f1626badb73c31745656efe32f517c9b600428a",
        "tok_s": 509.036,
        "ttft_s": 0.0327
      },
      "token_equal": true
    },
    "prose_seeded": {
      "base": {
        "tokens": 64,
        "prompt_tokens": 65,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "3fba05a0ae462fc6e4ba0023037275856ae0c71e2e61ac42c764e43ccaf9541f",
        "output_sha256": "6666d17da18529064fb76b843e6eb12b6d2b8bbf1fbb7829a5547a8f3b9f99d0",
        "tok_s": 356.823,
        "ttft_s": 0.0567
      },
      "head": {
        "tokens": 64,
        "prompt_tokens": 65,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "3fba05a0ae462fc6e4ba0023037275856ae0c71e2e61ac42c764e43ccaf9541f",
        "output_sha256": "6666d17da18529064fb76b843e6eb12b6d2b8bbf1fbb7829a5547a8f3b9f99d0",
        "tok_s": 356.916,
        "ttft_s": 0.0319
      },
      "token_equal": true
    }
  },
  "exactness_sha256": "4714c808a025601edea9ef6b194c3ea2bf00c376ca2fe2d596af709b53eaca54",
  "receipt_sha256": "69914d1085c6b05f1de54320b5d5b21e32d8bb216d3077c3d62243b21ac8cd32"
}

@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Synthena Initial Review — Performance receipt required (kind:perf)

Adds an opt-in TF_QWEN_INT8_TILES switch that lets the int8 prompt GEMM pick smaller 64x128/32x128/16x64 tiles of tf_w4a8_prefill when the 128x128 grid would leave SMs idle, with a per-projection tile chooser and contract-test coverage.

Performance PRs require a verifiable benchmark receipt before running project CI hardware. See CONTRIBUTING.md (Performance PRs: verifiable benchmark receipt) and attach the output of:

python3 tools/perf_receipt.py run --base-bin /path/to/base-tf --head-bin /path/to/head-tf --model /path/to/model.tfold --out receipt.json

Findings:

  • [BLOCKER] zig/src/cuda/w4a8.zig: The chooser's predicted-time model is calibrated by hardcoded constants (floor_us, block_group_us) measured on one GPU (RTX 5090, 170 SMs); on other cards with different SM throughput or memory behavior the model can systematically pick a slower tile, and the 3% bias toward the 128x128 tile does not correct for that. Suggested fix: Validate the chooser's picks against measured wall-clock times on at least one other SM count/GPU, or gate the switch to cards where the constants were measured, and document the calibration source in the code.
  • [WARNING] zig/src/families/qwen3_5/cuda_prompt_int8.zig: The env read silently swallows errors with catch false, so a malformed value is indistinguishable from the off state and the user gets no signal that their setting was ignored. Suggested fix: Log or surface the parse failure (consistent with how other TF_QWEN_* flags report invalid values) instead of silently defaulting to off.
  • [WARNING] zig/tests/cuda/contract_w4a8_exception.zig: This changes the exception-case stride condition from == .prefill to != .quantize, which now also applies the prefill stride to the new prefill_m64/m32/m16 kinds; that is presumably intended, but it silently alters behavior for any future kind added to the enum without revisiting this line. Suggested fix: Switch on tileOf(kind) == 0 vs the tile kinds explicitly, or add a comment/test asserting the stride rule for each kind so future enum additions cannot silently inherit the wrong branch.
  • Synthena

@jojomondag

Copy link
Copy Markdown
Contributor Author

On the review at d123ff74d. The head is now 773ef4643 (one more commit).

The chooser's constants (the blocker). Taken the second way you suggest: tileFor gives the chooser's tile only on a card of 170 SMs (tuned_sms, the count the constants were measured on, one RTX 5090). A card of any other SM count keeps the 128 x 128 tile, switch set or not. The row in docs/environment.md says so, and the host test checks it at 0, 48, 84, 128 and 188 SMs (a wrong expectation planted there fails the step). I have one card, so I cannot validate the picks on a second SM count. For your GB10 arm this means set and unset are the same launches; if you want to try the chooser there it is that one constant, and I can refit the constants from numbers you measure.

catch false. A malformed value does not get that far: the server checks every registered name at start and exits, tensorfold: TF_QWEN_INT8_TILES="2": expected 0 or 1 (default 0) (run on d123ff74d). The catch only closes the error union of env.value, in the form main uses for TF_QWEN_PROMPT_SPLITK and TF_QWEN_LAZY_SAVE.

The stride rule in the exception case. It is now an exhaustive switch on the kind, so a new kind does not compile until its rule is written.

Checks on 773ef4643: zig build -Dfixed-cpu test-host exit 0, the lean checker 88 as on the base, zig build native and compile-cuda-tests with -Dsm=120 compile. On one RTX 5090: tf-cuda-test kernel-contract w4a8 gives the same line as before (5 kernels: 5 pass gates 1-4, 0 fail, 0 known findings; 13 controls: 13 red as planted, 0 missed; 5 limit problems, the five being the usage audit under nvcc 13.4), tf-cuda-test w4a8 exits 0, and the nvcc 13.0.88 fatbins give record current on all 15 lines of the three tiles.

The hash run on this head, same pack and settings as the one above: tokens equal on all four prompts, the planted control diverged, drafted equals "draft": false, first token 0.057 -> 0.032 s on each prompt. One thing in it I do not claim: decode reads 329.73 -> 336.31 tok/s (+2.0 %, base against base 0.15 % apart, and verify passes without --allow-no-speedup). The same two binaries with the switch unset read 328.59 -> 335.20 tok/s (+2.0 %, first token 0.057 -> 0.057 s), and a repeat with it set 328.41 -> 335.31. So that difference belongs to this build of the head, not to the switch: it sits on two of the four prompts (prose_greedy +2.7 %, reasoning_greedy +5.3 %; the other two level), and the previous head's build read level (328.93 -> 328.65). I have not found what in the build does it. It may be worth knowing for the gate in #6078: two builds of nearly the same source can differ by more than its 0.5 % floor on this pack. The description is updated to this head.

perf-receipt.json, switch set (receipt_sha256 48a8f543d1dce63a...)
{
  "schema": 1,
  "pack_id": "tf-receipt-v1",
  "pack_sha256": "ab2a5d0ff262007e3ef4714fa46e62b2b2454b3f97b652ca18da49e4e0583c42",
  "generated_at_utc": "2026-12-03T01:12:36Z",
  "claim": "With --prompt-precision int8 and TF_QWEN_INT8_TILES=1 the head serves the same tokens as main; the change is in the prompt pass, so decode tok/s is level.",
  "git": {
    "base_sha": "0ba35fe6de5b290d660196add7ccc2be17998987",
    "head_sha": "773ef46434351913ea07c92592f0081a436f5f3d"
  },
  "environment": {
    "chip": "rtx-5090",
    "os": "linux",
    "arch": "x86_64",
    "checkpoint_id": "TensorFold/Qwen3.8-27B-MLX-4bit@22d8d538",
    "draft": true,
    "base_binary_sha256": "acb2594cb6cd59ec266e4c9085bdeb80b84fff01a23f7a1dc506b06b74cbebb9",
    "head_binary_sha256": "f44ac8c66e97156479f395c5ab86b27b53846d1736689001bca4d9a45187b395"
  },
  "control": {
    "prompt_id": "prose_seeded",
    "seed_a": 424242,
    "seed_b": 989898,
    "base_token_sha256": "3fba05a0ae462fc6e4ba0023037275856ae0c71e2e61ac42c764e43ccaf9541f",
    "alt_token_sha256": "a6c9d7d6e740c26e5aacf7eeae76c7ce1faa1927e2296b2782c199c9259ba49e",
    "diverged": true
  },
  "draft_check": {
    "prompt_id": "prose_greedy",
    "plain_token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
    "draft_token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
    "equal": true
  },
  "order": [
    "base",
    "head",
    "head",
    "base",
    "base",
    "head",
    "head",
    "base"
  ],
  "rounds": [
    {
      "index": 0,
      "arm": "base",
      "agg_tok_s": 329.921
    },
    {
      "index": 1,
      "arm": "head",
      "agg_tok_s": 336.109
    },
    {
      "index": 2,
      "arm": "head",
      "agg_tok_s": 336.134
    },
    {
      "index": 3,
      "arm": "base",
      "agg_tok_s": 329.412
    },
    {
      "index": 4,
      "arm": "base",
      "agg_tok_s": 329.775
    },
    {
      "index": 5,
      "arm": "head",
      "agg_tok_s": 336.513
    },
    {
      "index": 6,
      "arm": "head",
      "agg_tok_s": 336.488
    },
    {
      "index": 7,
      "arm": "base",
      "agg_tok_s": 329.824
    }
  ],
  "summary": {
    "mean_base_tok_s": 329.733,
    "mean_head_tok_s": 336.311,
    "gain_pct": 1.995,
    "base_noise_pct": 0.155
  },
  "prompts": {
    "prose_greedy": {
      "base": {
        "tokens": 128,
        "prompt_tokens": 71,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
        "output_sha256": "193650b0e6bfdd761c9cc7caf3154ec4d7fcd78072d3b3bb6ba094b516ab5384",
        "tok_s": 246.188,
        "ttft_s": 0.0568
      },
      "head": {
        "tokens": 128,
        "prompt_tokens": 71,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "bae66e46655d5d9c423b8f9408ca82783bfc15d2117a9d5ea4aa7448846d28bb",
        "output_sha256": "193650b0e6bfdd761c9cc7caf3154ec4d7fcd78072d3b3bb6ba094b516ab5384",
        "tok_s": 252.797,
        "ttft_s": 0.0323
      },
      "token_equal": true
    },
    "code_greedy": {
      "base": {
        "tokens": 128,
        "prompt_tokens": 78,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "0b47594d2975969cb4c72df23c9df9cfbe1ce63c166348a5843bf9e076f311c5",
        "output_sha256": "f419036bdae542ded35e3c7f39fcbf066d2dc41dbe918014a03aaac7e699fff7",
        "tok_s": 313.229,
        "ttft_s": 0.0572
      },
      "head": {
        "tokens": 128,
        "prompt_tokens": 78,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "0b47594d2975969cb4c72df23c9df9cfbe1ce63c166348a5843bf9e076f311c5",
        "output_sha256": "f419036bdae542ded35e3c7f39fcbf066d2dc41dbe918014a03aaac7e699fff7",
        "tok_s": 314.005,
        "ttft_s": 0.0327
      },
      "token_equal": true
    },
    "reasoning_greedy": {
      "base": {
        "tokens": 128,
        "prompt_tokens": 89,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "7da5c3a040233b558d6258914310660966d99ff3fc5a1f06c520a0344399cee2",
        "output_sha256": "8912fceb7f3c2c57054d8cb31f1626badb73c31745656efe32f517c9b600428a",
        "tok_s": 510.218,
        "ttft_s": 0.0577
      },
      "head": {
        "tokens": 128,
        "prompt_tokens": 89,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "7da5c3a040233b558d6258914310660966d99ff3fc5a1f06c520a0344399cee2",
        "output_sha256": "8912fceb7f3c2c57054d8cb31f1626badb73c31745656efe32f517c9b600428a",
        "tok_s": 536.745,
        "ttft_s": 0.0331
      },
      "token_equal": true
    },
    "prose_seeded": {
      "base": {
        "tokens": 64,
        "prompt_tokens": 65,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "3fba05a0ae462fc6e4ba0023037275856ae0c71e2e61ac42c764e43ccaf9541f",
        "output_sha256": "6666d17da18529064fb76b843e6eb12b6d2b8bbf1fbb7829a5547a8f3b9f99d0",
        "tok_s": 357.724,
        "ttft_s": 0.0567
      },
      "head": {
        "tokens": 64,
        "prompt_tokens": 65,
        "cached_tokens": 0,
        "complete": true,
        "token_sha256": "3fba05a0ae462fc6e4ba0023037275856ae0c71e2e61ac42c764e43ccaf9541f",
        "output_sha256": "6666d17da18529064fb76b843e6eb12b6d2b8bbf1fbb7829a5547a8f3b9f99d0",
        "tok_s": 357.768,
        "ttft_s": 0.0318
      },
      "token_equal": true
    }
  },
  "exactness_sha256": "4714c808a025601edea9ef6b194c3ea2bf00c376ca2fe2d596af709b53eaca54",
  "receipt_sha256": "48a8f543d1dce63aa2ab411f56c4d9a548f79599794465a30f23b68fc13abd47"
}

@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Synthena Initial Review — Performance receipt required (kind:perf)

Adds an opt-in TF_QWEN_INT8_TILES switch that instantiates 64x128, 32x128 and 16x64 tiles of the int8 prompt GEMM kernel and picks per-projection tiles via a heuristic tuned for 170-SM cards, defaulting to the existing 128x128 tile.

  • Failing CI check(s): verify.

Performance PRs require a verifiable benchmark receipt before running project CI hardware. See CONTRIBUTING.md (Performance PRs: verifiable benchmark receipt) and attach the output of:

python3 tools/perf_receipt.py run --base-bin /path/to/base-tf --head-bin /path/to/head-tf --model /path/to/model.tfold --out receipt.json

Findings:

  • [BLOCKER] zig/src/cuda/w4a8.zig: The chooser divides by sms with no guard against zero; tileFor only gates on sms == tuned_sms, but chooseTile is exported and called directly in tests and could be reached with sms=0, producing an inf/NaN prediction and a wrong tile pick. Suggested fix: Clamp sms to at least 1 inside chooseTile (e.g. @max(sms, 1)) or return tile 0 when sms == 0.
  • [BLOCKER] zig/src/families/qwen3_5/cuda_prompt_int8.zig: The chooser is hard-gated to exactly 170 SMs (tuned_sms), so the perf benefit is claimed only for one specific GPU SKU while the PR description claims a general prefill speedup; on any other card the switch silently does nothing, which is a behavior/perf mismatch rather than a crash. Suggested fix: Either document the 170-SM-only scope in the perf claim and changelog, or generalize the chooser constants per SM count before claiming broad prefill gains.
  • [WARNING] zig/src/cuda/w4a8.zig: The 3% hysteresis is applied by inflating the smaller tile's predicted time, but the comparison is against best_t which is updated in the same loop, so a later tile is compared against an already-penalized predecessor rather than the raw 128x128 baseline; ordering of tile_shapes affects the pick. Suggested fix: Compare each candidate's raw prediction p against the unpunished 128x128 prediction, applying the 3% margin only against tile 0's time.
  • [WARNING] zig/src/core/contract/qwen_int8_tiles_records.zig: The record's register usage arrays are hand-transcribed constants (e.g. .{ 147, 147, 147, 125, 125 }) with a comment pinning them to nvcc 13.0.88; a toolchain bump silently invalidates them and there is no compile-time or CI check tying them to the actual compiled kernels. Suggested fix: Add a build step that extracts per-kernel register/shared usage from the compiled cubin and fails the build when the records drift.
  • Synthena

@jojomondag

Copy link
Copy Markdown
Contributor Author

On the red verify check (Verify performance receipt, live on main since #6078 landed): it fails here and on #6095 with PR is labeled 'perf' but includes no docs/receipts/**/perf-receipt*.json for head 773ef4643.... The receipts for both heads are in the comments above. I have not committed them yet, because as the check is written a committed receipt cannot pass, and I would rather not move the heads for a commit that stays red:

  • The head SHA. The step runs verify --expect-head "$HEAD_SHA" with the pull request's head. A receipt is measured on a commit and then committed, so the head becomes the commit that adds the file, and the receipt would have to contain the SHA of the commit that contains it. Committing this head's receipt would give stale head_sha 773ef4643435 != expected PR head <the receipt commit>. A rule that could work: expect the newest commit that changes anything outside docs/receipts/, and require that the commits after it touch only docs/receipts/.
  • The speed floor. The step runs verify without --allow-no-speedup, and the pack scores decode tok/s on prompts of 65 to 89 tokens with the prompt cache off. A change in the prompt pass (this one, qwen3_5 cuda: exact prompt attention for 49-1,536-row chunks that fills the card (TF_QWEN_PROMPT_COOP, default off) #6095, qwen3_5 cuda: run neighbouring request-start chunks as one forward (TF_QWEN_STARTS_MERGE, default off) #6056) shows equal tokens there and a level score, so it fails the floor however large its effect on the first token is. This head passes only because of the build effect I described above, which I do not claim.

Tell me which way you want it and I will commit the receipts as docs/receipts/<slug>/perf-receipt.json on #6098, #6095, #6056 and #5849 the same day: as they are (measured on the code head, one receipts-only commit after it), or after a change to the check.

@jojomondag

Copy link
Copy Markdown
Contributor Author

On the review at 773ef4643. I am answering in words and leaving the head where it is until the receipt question above is settled, so it moves once more at most.

chooseTile with zero SMs. The served path cannot get there (tileFor asks for 170 first), and a direct call gives the 128 x 128 tile: the grid term is +inf for every tile, and a smaller tile only replaces the 128 x 128 one when strictly less. I checked rows 1 to 4,096 over three widths in a scratch test. An explicit if (sms == 0) return 0; with a host test line reads better than relying on that, and it goes in with the next push.

The 170-SM scope. The description's first paragraph, its "Boxes it reaches" line and the switch's row in docs/environment.md all say the chooser acts on a 170-SM card and that a card of another SM count keeps the 128 x 128 tile; every number in the description is from one RTX 5090. No general claim is meant. If a changelog line is wanted from the contributor, tell me and I add one with that scope.

The 3 % margin and the order of the tiles. The three smaller tiles all carry the same factor, so comparing them with each other is a comparison of their raw predictions, and the factor only counts against the 128 x 128 tile, which is the form you describe. A scratch test confirms the pick equals "the best smaller tile by raw prediction, taken only if it beats the 128 x 128 prediction by 3 %" for every row count from 1 to 4,096, seven widths, two depths and 48, 84, 128, 170 and 188 SMs.

The usage records. They are checked against compiled kernels by the project's own tool: tf-cuda-test kernel-contract-limits reads the fatbins and prints record current or record stale per launch and architecture (15 of 15 current here on nvcc 13.0.88 fatbins), and the contract run's limits gate does the same for the device compiler. That is the mechanism main's records use; I have added nothing of my own to it.

@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. Opt-in (TF_QWEN_INT8_TILES, off by default). Device A/B on an RTX 5090: tokens equal in every cell and speed unchanged (within 0.6%, repetitions overlapping), so the default path is unaffected; the short-turn gain rests on the author's exact-token receipts for the switch.

  • Synthena

@ashhart ashhart added synthena:ready Reviewed by Synthena and ready for group train and removed synthena:needs-receipt Speed PR awaiting verified perf_receipt.py hash run labels Dec 3, 2026
ashhart added a commit to jojomondag/TensorFold that referenced this pull request Dec 3, 2026
@ashhart
ashhart force-pushed the qwen-cuda-int8-tiles branch from 773ef46 to f810945 Compare December 3, 2026 10:57
@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. Opt-in (TF_QWEN_INT8_TILES, off by default). Device A/B on an RTX 5090: tokens equal in every cell and speed unchanged (within 0.6%, repetitions overlapping), so the default path is unaffected; the short-turn gain rests on the author's exact-token receipts for the switch. This head is rebased onto current main and re-pins its serving sources for the new switch; the serving-pin tests pass.

  • Synthena

@ashhart ashhart added synthena:needs-ash Needs owner decision and removed synthena:ready Reviewed by Synthena and ready for group train labels Dec 3, 2026
@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Synthena Initial Review — Needs maintainer review (kind:perf)

Adds opt-in (TF_QWEN_INT8_TILES) smaller CUDA int8 prompt-GEMM tiles (64x128, 32x128, 16x64) with a shape-based tile chooser, defaulting to the existing 128x128 kernel and gated to cards with 170 SMs.

  • Failing CI check(s): verify.

Findings:

  • [WARNING] zig/src/cuda/w4a8.zig: The chooser only activates on exactly 170 SMs, so the optimization is a no-op on every other GPU; the PR description says a benchmark receipt exists but the diff contains no verifiable timing numbers for the smaller tiles versus 128x128. Suggested fix: Attach the measured benchmark receipt (per-tile latency at the flip-point shapes) to the PR, or generalize tuned_sms to a range validated per-SM-count.
  • [WARNING] zig/src/cuda/w4a8.zig: The cost model assumes all SMs are busy and divides by sms, but for grids smaller than one wave the predicted time is just the single-block floor, so the model can pick a small tile even when the 128x128 grid already fills the machine (or vice versa) at boundary shapes. Suggested fix: Clamp the wave term to ceil(blocks/sms) waves rather than blocks/sms, and validate the flip-point rows against the measured tile_rows list.
  • Synthena

@jojomondag

Copy link
Copy Markdown
Contributor Author

On the review at f810945e1 (your rebase and re-pin; the two commits of mine are unchanged). In words, no push.

Timing for the smaller tiles. The source carries none by the repository's rule. The numbers are in #6059 and in this description: one launch of each tile against 128 x 128 on the 27B's projections (7 to 32 rows 70-72 us -> 15.6-23 us, 110 rows 89 / 245 -> 36 / 95 us, 410 rows 223 / 298 -> 150 / 215 us), the served table of first-token times at 33k and 140k, and the hash run in the comment above. Other SM counts I cannot fit: I have one card, which is why tileFor asks for 170.

The cost model at one wave. The prediction is the larger of the block floor and the grid term, and the two meet near a full machine: 170 blocks of 128 x 128 at K = 5,120 give a grid term of 78.4 us against the 70.5 us floor. So a 128 x 128 grid that fills the card is priced by its grid term, not by the floor. I have not fitted a stepped ceil(blocks / sms) form, so I cannot say it would pick better. What is measured is the linear one: over the duel's 77 cells its picks sum to 8,988 us against 8,874 us for the best tile in each cell (13,007 us on 128 x 128 alone), worst cell 2.4 % slower than 128 x 128. A wrong pick costs time and no bytes, and the row counts where the pick flips are contract shapes, compared byte for byte on the device.

@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Synthena Initial Review — Needs maintainer review (kind:perf)

Adds an opt-in TF_QWEN_INT8_TILES switch that lets the int8 prompt GEMM pick smaller 64x128/32x128/16x64 tiles of tf_w4a8_prefill when the 128x128 grid would leave SMs idle, with a shape-based chooser gated to 170-SM cards and contract tests asserting byte-equality with the base kernel.

Findings:

  • [WARNING] zig/src/cuda/w4a8.zig: The chooser's predicted-time model is a heuristic with hardcoded constants (floor_us, block_group_us) and a 3% bias; the PR description cites contract byte-equality and chooser-pick tests but no measured latency/throughput comparison of the two arms on the target card. Suggested fix: Include a measured A/B timing receipt (e.g. per-projection kernel times or end-to-end tokens/sec with the switch on vs off at 170 SMs) so the perf claim is verifiable, or mark the PR as a candidate switch pending measurement.
  • [WARNING] zig/src/families/qwen3_5/cuda_prompt_int8.zig: The env read happens in resolve() at engine setup, but the docs say the variable is 'read once'; if resolve is ever re-run (e.g. device re-init) the value is re-read, and silently falling back to false on error hides misconfiguration. Suggested fix: Confirm resolve is called exactly once per process, and consider logging when the env parse fails so a typo'd value is not silently treated as off.
  • Synthena

@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. Opt-in (TF_QWEN_INT8_TILES, off by default). Device A/B on an RTX 5090: tokens equal in every cell and speed unchanged (within 0.6%, repetitions overlapping), so the default path is unaffected; the short-turn gain rests on the author's exact-token receipts for the switch. This head is rebased onto current main and re-pins its serving sources for the new switch; the serving-pin tests pass. Rebased onto main after #6095: both opt-in switches sit side by side, each off by default and independent, with the serving pins refreshed.

  • Synthena

@ashhart ashhart added the synthena:ready Reviewed by Synthena and ready for group train label Dec 3, 2026
@jojomondag

Copy link
Copy Markdown
Contributor Author

On the review at 61b52d8fc, in words; the head stays where it is.

  • Measured A/B: the hash run with the switch unset and set on one RTX 5090 is in cuda w4a8: smaller tiles of the int8 prompt GEMM where the 128x128 grid leaves SMs idle (TF_QWEN_INT8_TILES, default off) #6098 (comment) (tokens equal; first token 0.057 -> 0.032 s on the pack's prompts; decode level, and the unset control reads the same +2 %, so that is the build). Your own device A/B on this head is above.
  • Read once, and a typo: State.resolve runs once per engine, from initStage in cuda_engine.zig; a pipeline's stages read the same process environment, so they get the same value. TF_QWEN_INT8_TILES is registered in platform/environment.zig as a flag (0 or 1), and the server checks every registered setting in engines.environment before any engine starts: any other text stops the start with the name, the text and the accepted forms. The catch false in resolve is only reached by a value that check has already accepted.

@ashhart
ashhart force-pushed the qwen-cuda-int8-tiles branch from 61b52d8 to 889b74b Compare December 3, 2026 16:22
ashhart added a commit to jojomondag/TensorFold that referenced this pull request Dec 3, 2026
@ashhart

ashhart commented Dec 3, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. Opt-in (TF_QWEN_INT8_TILES, off by default). Device A/B on an RTX 5090: tokens equal in every cell and speed unchanged (within 0.6%, repetitions overlapping), so the default path is unaffected; the short-turn gain rests on the author's exact-token receipts for the switch. This head is rebased onto current main and re-pins its serving sources for the new switch; the serving-pin tests pass. Rebased onto main after #6095: both opt-in switches sit side by side, each off by default and independent, with the serving pins refreshed.

  • Synthena

jojomondag and others added 3 commits December 4, 2026 17:16
…id leaves SMs idle (TF_QWEN_INT8_TILES, default off)

tf_w4a8_prefill runs 128 x 128 tiles, so a projection whose grid is at most one wave of blocks pays one block's
whole K loop whatever its size. Three more instantiations of the same body (64x128, 32x128, 16x64) and a chooser
take the tile with the least predicted time from the shape and the card's SM count. A tile only changes which
outputs a block holds: each output's integer group dots and its ascending fma chain over the groups are the same,
so the bytes are the same. The switch is read through the environment registry in the Qwen family and handed to
the kernels as fields; unset, every projection takes tf_w4a8_prefill.

The three kernels are registered with the kernel contract: each is compared byte for byte with tf_w4a8_prefill on
the 27B's int8 projections, row counts from 1 to 4,096 and the chooser's boundaries, with a planted control that
runs the tile one input group short.

Refs ashhart#6059
@ashhart
ashhart force-pushed the qwen-cuda-int8-tiles branch from 889b74b to a861433 Compare December 4, 2026 17:20
@ashhart

ashhart commented Dec 4, 2026

Copy link
Copy Markdown
Owner

READY to land on this head. The prior RTX 5090 A/B matched every token cell with default-path speed within 0.6%. This rebase preserves all 16 non-pin PR changes and refreshes the serving pins on current main. TF_QWEN_INT8_TILES stays off by default. The author supplied the opt-in short-prompt exactness and timing evidence. The normal train will check the resulting tip.

  • Synthena

@ashhart ashhart removed the synthena:needs-ash Needs owner decision label Dec 4, 2026
@ashhart
ashhart merged commit bf1a2eb into ashhart:main Dec 4, 2026
30 of 39 checks passed
ashhart added a commit that referenced this pull request Dec 4, 2026
@ashhart

ashhart commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Thank you @jojomondag. 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.

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

Labels

kind:perf Classified by Synthena as performance improvement synthena:ready Reviewed by Synthena and ready for group train

Projects

None yet

2 participants