Skip to content

core: one copy of the TFQ3 records and wire constants (TFQ3 format move, piece 1) - #5281

Merged
ashhart merged 2 commits into
ashhart:mainfrom
jschmied:core-tfq3-format
Nov 27, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
jschmied:core-tfq3-format

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Head: 2ee35c56a5 on main 93dd46c33c.

TFQ3's file format lives in three copies today: families/flashnext/tfq3_check.zig (Metal Flash Next),
families/glm/tfq3_check.zig (Metal GLM) and cuda/tfq3_format.zig (#4986). This is the first piece of moving it
into one core module, on the #2118/#4860 terms: one piece per PR, nothing a user sees changes.

This piece moves only what the three copies already agree on byte for byte:

  • the three identical records LayerHeader (TFPW), ExpertMeta and NonMoeHeader (TFPN);
  • the wire constants: the three magics, the version, the 16 KiB section, the 10,816-byte table and the 16 KiB table file.

They now live in zig/src/core/tfq3_format.zig, a std-only file with one module owner. zig/build/tfq3_format.zig
wires it into every module the build compiles, the same way zig/build/bf16_round.zig does, from build.zig and from
product.zig (-Dgpu). Every backend keeps each public name as an alias, including the CUDA names (TfpwHeader,
TfpnHeader, TfpwExpertMeta, magic_tfpw, tables_min_bytes) and the type of each constant (CUDA's
section_align stays u64, GLM's stays usize). No caller changes, and no check, loader, kernel or refusal changes.

Left in place on purpose:

  • The TFPN tensor record keeps its two declarations. GLM's has R/C and nameSlice(*const). CUDA's has r/c and
    nameSlice(*align(1) const), which it needs because it borrows names out of unaligned views. One alias can't keep
    both field APIs.
  • GLM's file from NonMoeLayout down (line 166 on main) is untouched. cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018 rewrites that part. Its CLI imports GLM's
    record names through the glm_tfq3_check shim, and those names are still there.

Where the copies disagree (each backend keeps its own rule; unifying any of these is its own issue)

  1. TFPN tensor record: field names R/C (GLM) vs r/c (CUDA), and the nameSlice receiver alignment.
  2. Error names: Flash Next and GLM use UnsupportedQuantization/Tfq3*. CUDA uses InvalidTfq3Shard,
    InvalidTfq3Tables, MisalignedTfq3Tensor and Tfq3LayerMismatch, and its TFPN/TFPW header arithmetic can raise
    std's error.Overflow.
  3. PWV3 table check order. Flash Next refuses a header under 8 bytes, or a file under 10,816 bytes, as Tfq3Table
    before it reads the magic. GLM gives Tfq3Truncated under 8 bytes, then UnsupportedQuantization, then
    Tfq3Table. CUDA checks the length first and calls every fault InvalidTfq3Tables. Example from codex: an 8-byte
    head with a bad magic and file_len 8 gives Tfq3Table, UnsupportedQuantization and InvalidTfq3Tables.
  4. TFPN widths: GLM's loader takes 4/5-bit (cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018 adds an opt-in 3-bit policy for inspection); CUDA takes 3/4/5-bit.
  5. TFPN header: GLM refuses num_tensors == 0 and an empty tensor name (Tfq3Geometry); CUDA admits both.
  6. Section placement:
    • GLM: scales and weights start on 16 KiB, each rounded view fits inside the rounded mapping (mappedLen), and
      sections are pairwise disjoint and off the header.
    • CUDA: scales and weights start on 16 KiB, and sections are strictly ordered meta < scales < weights. It has no
      mapping rule.
    • Flash Next (Metal): no 16 KiB rule, only disjoint sections off the header.
  7. Record alignment: GLM refuses off_meta off the record's 8-byte alignment. Flash Next and CUDA don't; CUDA reads
    the records align(1).
  8. TFPW geometry:
    • Flash Next: fixed at 512 experts of 2560 x 640 with 1536 tensors; an optional layer index, Tfq3Layer.
    • GLM: layer, experts, hidden and inter must match the checkpoint (Tfq3Layer), then the fixed 4096/2048.
    • CUDA: any non-zero size that is a multiple of 128, with optional expectations; an optional layer index,
      Tfq3LayerMismatch. num_tensors is not checked.
  9. Word counts: Flash Next and GLM use matWords = floor(R*C*b/32) with overflow refusals. CUDA uses
    tensorWords = (R/16)*(C/128)*(2b)*32 in u64. They agree whenever R % 16 == 0 and C % 128 == 0 (every shape the
    validators admit) and differ elsewhere (R=17, C=129, b=4: 274 vs 256, from codex). Scales: GLM uses a checked
    Error!usize, Flash Next a plain usize, CUDA u32 to u64.
  10. The 4-bit uint4 (16 B) rule:
    • Flash Next and GLM check each projection in turn, alignment then bounds, gate, up, down.
    • CUDA checks all three alignments before any bounds, and its TFPN check names the offending tensor (bad_name).
  11. Discovery (piece 2):
    • Flash Next looks for the fixed names layer_NN/mtp_00, checks every shard present, and refuses shards that
      have no table.
    • CUDA lists *.tffpw sorted and probes a header count (hasRecords); a table alone never selects TFQ3.
  12. canonicalName exists only in CUDA (its tensor lookup), and only CUDA has validBits.

Evidence

  • test-tfq3-format (new, zig/src/core/tfq3_format_test.zig) pins every field's offset and size, each record's
    size and alignment, the magics' spelling and the sizes. It also checks one numbered record of each kind against
    main's bytes as hex.
  • Planted control, red before green: with gate_sc_off and up_sc_off swapped in ExpertMeta (same size, so every
    validator on main still passes), test-tfq3-format fails "every record's fields sit where main's three copies put them" (3/4 passed, 1 failed). Restored, it passes again.
  • The three backends' existing check tests pass unchanged, which shows the refusals are unchanged:
    test-glm-tfq3-host, test-flashnext-memory-host (Flash Next tfq3_check_test.zig, the sidecar scan),
    test-flashnext-load-admission, test-cuda-tfq3-host and test-cuda-host (tfq3_format and tfq3_lifecycle_test).
    One build on the GB10 (aarch64, head 2ee35c56a5): 527/527 steps, 10,038/10,086 tests passed (48 skipped).
  • cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018 at cbe8769e, merged onto this branch for review only (not pushed): the merge is clean, and
    test-tfq3-host test-glm-tfq3-host test-tfq3-format test-flashnext-memory-host pass. Merge caf4f5d2a3: 17/17 steps, 62/62 tests.
  • x86 (-Dfixed-cpu, full CI set, --final, head 2ee35c56a5): test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths: 2680/2680 steps, 28,647/28,828 tests
    passed (181 skipped); check-native-cuda-host compile-cuda-tests: 21/21 steps. X86-ALL-GREEN.
  • Local: zig fmt --check, tools/pr_check.py 0 errors (lean 123 vs 123, 0 new; import-cycle edges 24 vs 24), and
    git diff --check.

Not run, and why:

  • Tokens or bytes on a real checkpoint: no TFQ3 checkpoint is on this box (no TFPW/TFPN/PWV3 file under ~/models), so
    the host fixtures above are the comparison against main.
  • Speed rows: no compiled instruction can change. The moved records are the same extern structs at the same
    offsets, and every use site resolves to the same type.
  • SASS: no .cu/.cuh file and no kernel source is in the diff.
  • Seeded, resume and concurrency replies: no request path changes, and there is no checkpoint to serve.
  • Metal device run: none needed. No Metal source, pipeline or loader line changes, and the Metal-side Zig compiles
    through the same host roots above.

Next pieces (each its own PR, in this order)

  1. Arithmetic (matWords/matScales/tensorWords/validBits, the 16 B predicate), each signature and domain kept
    by name.
  2. One PWV3 table check that returns a fault, each backend keeping its precheck and its error names (item 3 above).
  3. File discovery (Flash Next tfq3_files, CUDA listShards/hasTables/hasRecords).
  4. The RMSNorm +1 rule (metadata first, the mean heuristic as a named fallback).
    After cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018 lands, the per-kind walkers move only once one rule has been picked for each disagreement above.

Proposed CHANGELOG line (Unreleased): Internal: the TFQ3 file records and wire constants live once in core/tfq3_format.zig for the
CUDA, Metal Flash Next and Metal GLM backends; nothing a user sees changes.

The split, the decision to keep the TFPN tensor record in two copies, the product-mode wiring and the #5018
integration check came from a review with codex.

jschmied and others added 2 commits November 27, 2026 03:50
The TFPW header, TFPW expert record and TFPN header, and the magics, version and
section and table sizes, were declared three times: Flash Next's and GLM's
tfq3_check.zig and CUDA's tfq3_format.zig. They now live in core/tfq3_format.zig,
wired into every module by zig/build/tfq3_format.zig like bf16_round. Each
backend keeps its public names as aliases, its constants' types, its TFPN tensor
record and every check; test-tfq3-format pins the records' offsets and bytes.
@ashhart
ashhart merged commit e51fd90 into ashhart:main Nov 27, 2026
1 check failed
@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants