Skip to content

tfq3: GLM decode rows 8-16 match 1-row decode; loading checks and stray-file isolation - #4996

Open
ashhart wants to merge 6 commits into
mainfrom
lead/tfq3-release-fixes
Open

ashhart wants to merge 6 commits into
mainfrom
lead/tfq3-release-fixes

Conversation

@ashhart

@ashhart ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

TFQ3's contract: prompt and decode may differ in the last bits, but a repeat equals its prefill, 1-row decode equals multi-row decode at every window size, loading is safe, and every checkpoint without TFQ3 records takes exactly the ordinary paths. A release review of the TFQ3 changes (#4953, #4964) found one place where GLM broke the multi-row rule, and three loading gaps. This fixes all four.

  1. GLM decode windows of 8-16 rows now give each row its 1-row bytes. Dense TFQ3 runs used the prompt pass's MMA kernel for any chunk of 8 or more rows. The fused KDA-input and dense-GLU paths also stopped at 7 rows, and the unfused GLU rounds its gate/up result to bf16 in between. GLM decode windows run up to 16 rows, so a row in a large window could differ from the same row decoded alone.
    • Only the prompt pass (denseMm) now takes MMA.
    • Decode keeps the decode kernels at every row count, and the fused paths serve every decode window up to 16 rows.
    • Prompt speed is unchanged. Decode windows of 8-16 rows read the dense TFQ3 weights once per 4 rows instead of once per 32, so they are slower on those projections. That is the cost of the contract.
  2. GLM refuses a 4-bit record whose words are misaligned for the kernels' uint4 loads (Tfq3Misaligned), as Flash Next already does.
  3. A stray pw_v3_tables.bin turns nothing on. TFQ3 starts only when its records (.tffpw files) are present, in both GLM and Flash Next. A checkpoint without records takes the ordinary paths and allocations whatever files sit beside it.
  4. Flash Next's table check matches GLM's:
    • it requires the whole table;
    • every float and half must be finite;
    • it is sized to the table struct's real 10,816 bytes. Before, it required 16,384 bytes, so it refused a valid unpadded table.

Tests:

  • Dense, fused KDA input and dense GLU compare 1-, 8- and 16-row decode windows byte for byte, 4- and 5-bit, split-K and not. These run on a Metal GPU (zig build test-glm-tfq3).
  • Misaligned 4-bit records are refused by name. Main accepted them.
  • A stray tables file leaves a non-TFQ3 checkpoint unchanged, in both families.
  • Padded and unpadded Flash Next tables pass. NaN and Inf are refused.

Tokens: unchanged for every checkpoint without TFQ3 records, and for TFQ3 prefill. TFQ3 decode in 8-16-row windows now matches 1-row decode.

Checks: every GLM and Flash Next host step, fmt, lean (165, unchanged), gradient-serving pins and a macOS type-check of the Metal steps.

  • Synthena

…o 16 rows

The TFQ3 contract: 1-row decode equals multi-row decode. GLM broke it at 8 rows. tfq3_dispatch.dense sent a
chunk of 8 or more rows to dense_prefill_nax_* (MMA), and tryKdaIn and tryDenseGlu stopped at 7 rows, so in an
8-16-row decode window the dense projections, the KDA input and the dense MLP's gate/up took other kernels
than the same row alone.
- dense takes the MMA kernels only for a prompt pass (wide.Io.prompt, set by denseMm, which only prompt.zig
  runs); decode windows keep the decode kernels at every row count.
- tryKdaIn and tryDenseGlu fuse every decode window up to state.max_rows (16): the 16-row scratch fits
  (dense GLU: 16 x 2 x 12288 of max_yr_elems' 1,239,040).
Non-TFQ3 runs are untouched; the prompt pass keeps its MMA kernels.
… read them

GLM's checker accepted any word offset whose matrix fits the stream, but its 4-bit kernels read those words
four at a time as uint4 (dense_decode.metal:11, 15; moe_mv.metal:64, 114, 178, 227, 257; prefill.metal:133,
248, 376). As in Flash Next (fdd80c5), a 4-bit nonmoe.tffpw record (tensor) or expert projection (layer)
whose word offset is not a multiple of four words is refused by name (Tfq3Misaligned), in the one check
admission and loading share. 5-bit words are read a word at a time and keep any offset; the format is
unchanged.

Tests: misaligned 4-bit records inside the stream refused (main accepts them), 5-bit odd offset accepted;
the two earlier truncation cases move by a whole uint4 so they still test the end.
…h its records

A checkpoint without TFQ3 record files must take main's paths and allocations whatever sits beside it.
- GLM: initLoader mapped pw_v3_tables.bin whenever it existed and set has_tfq3 from it alone, so a stray
  tables file compiled TFQ3 pipelines and allocated their scratch (and a corrupt one failed the load).
  initLoader now returns first unless tfq3.hasRecords finds nonmoe.tffpw or a layer_NN.tffpw (up to the
  MTP layer's).
- Flash Next: engine_load mapped the tables file even when no shard was present, outside admission and
  budgeting. It maps it only when tfq3_files.hasRecords finds a layer or MTP shard, as scan admits them.
With records present nothing changes.
…, as GLM's

tfq3_check.tables took the header and a 16 KiB length only, and let NaN or Inf in pw_v3_tables.bin reach the
kernels. GLM's check (glm/tfq3_check.zig:158) also requires the six affine floats and every LUT and c64 half
finite; Flash Next's now does the same, and tfq3_files.checkTables reads the whole table for it, so the scan
before admission and the loader refuse the same files (Tfq3Table).

The size: both families read the same table struct, FnPwV3Tables (flashnext_tfq3/common.metal:40): 64 header
bytes, 256 + 1024 + 1024 LUT words and three 64-entry half4 tables, 10,816 bytes (GLM's tables_bytes). Flash
Next required 16,384, the 16 KiB section alignment, reused as a floor: it refused a valid unpadded table but
never admitted a short one, so it was over-strict, not unsafe. tables_bytes (10,816) is now the floor; a
table is read through a no-copy view only when its 16 KiB-rounded length fits the mapping, else copied.

Tests: a padded and an unpadded table pass (main refused the unpadded one), a NaN float and an Inf half are
refused, and the scan counts the table's real length.
…tes; stray tables and misaligned records

GLM, on a Metal GPU (test-glm-tfq3):
- dense: the prompt pass (MMA) repeats exactly, and 16-, 8- and 4-row decode windows each equal 1-row decode,
  both widths, split-K and not (runDense passes the prompt flag; a prompt's 16 rows still take MMA).
- glm_tfq3_rows_test.zig: the fused KDA input (tryKdaIn, its real 24576 + 320 x 4096 shape) and dense GLU
  (split-K and no-split) over 16 rows as one 16-row window, two 8-row and sixteen 1-row ones: every row's
  bytes equal, 4- and 5-bit; tryKdaIn now takes every window size.
- a 4-bit nonmoe.tffpw record one word off, inside its stream, refused by name (Tfq3Misaligned).
- a non-TFQ3 checkpoint with a stray pw_v3_tables.bin dry-loads with has_tfq3 false and main's bytes.
Flash Next, on the host: a stray table file is neither a record nor scanned (hasRecords false; an MTP shard
makes it true).

This branch has not been deployed

No deployments
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.

1 participant