Skip to content

core: one copy of TFQ3's shared arithmetic (TFQ3 format move, piece 2) - #5341

Merged
ashhart merged 6 commits into
ashhart:mainfrom
jschmied:core-tfq3-arith
Nov 28, 2026
Merged

ashhart merged 6 commits into
ashhart:mainfrom
jschmied:core-tfq3-arith

Conversation

@jschmied

@jschmied jschmied commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Head: 2a82cd7c29 on main 91dbadce65 (two commits: the move, ef6ed7992c, and a test-only portability fix).

This is piece 2 of moving TFQ3's file format into zig/src/core/tfq3_format.zig (piece 1: #5281). It moves only the
arithmetic that the copies already compute the same way, on the same #2118/#4860 terms as piece 1: one piece per PR,
nothing a user sees changes.

What moves (each a pure function in core, std only):

  • Span: Flash Next's and GLM's byte range, the same two usize fields. Both now alias it.
  • overlaps(head, spans) bool: GLM's disjoint and the loop inline in Flash Next's header were the same test (two
    non-empty spans share a byte, or one starts inside the header). GLM's disjoint and Flash Next still raise
    Tfq3Overlap.
  • matWords(r, c, bits) ?usize: floor(R * C * bits / 32), checked in main's order (R * C, then * bits, then / 32).
    Flash Next and GLM keep matWords(...) Error!usize as wrappers that map null to Tfq3Overflow.
  • validBits(bits): 3, 4 or 5. CUDA's validBits is now an alias. Flash Next's b_bits < 3 or b_bits > 5 (a u32,
    so the same set) calls it.
  • uint4Misaligned(bits, word_off): bits == 4 and word_off % 4 != 0, the kernels' uint4 loads. Flash Next's expert
    check, CUDA's TFPN check and CUDA's TFPW check call it.

Each backend keeps every public name, signature, error name and check order. That includes CUDA's TFPW rule, which
checks all three alignments before any bounds, and Flash Next's rule, which checks each projection in turn (item 10 in
#5281). No loader, kernel or refusal changes.

Left per backend on purpose:

Evidence

  • test-tfq3-format gains zig/src/core/tfq3_arith_test.zig. It keeps main's bodies verbatim as oracles (Flash Next
    and GLM matWords, CUDA validBits, Flash Next's inline overlap loop, GLM's disjoint) and compares each moved
    function with them:
    • matWords over every combination of 28 dimensions (0, 1, 15/16/17, 127/128/129, the u32 and usize limits and
      their /32, /5, /4, /3, /2 points) and 12 widths, null exactly where main refused. It also pins literal values:
      640 x 2560 at 4 bits = 204,800 words; R * C overflowing with bits = 0 is refused; and the second product
      overflowing is refused even where the quotient would fit.
    • validBits over 0..69 and the u32 top values, against CUDA's set and Flash Next's range.
    • uint4Misaligned over widths 0..8 and offsets up to maxInt(u64).
    • overlaps against both main loops over every triple of spans with off 0..6 and len 0..4, for two header lengths
      (85,750 cases). It also covers named cases: Flash Next's valid shard, touching ends, reversed order, empty spans,
      the header's last byte, first-and-third and containment.
  • Refusal order, one fixture each way (new tests; the existing tests are unchanged): a 4-bit expert whose gate is on a
    uint4 boundary but past the words, with up one word off, is Tfq3Truncated in Flash Next
    (families/flashnext/tfq3_check_test.zig, last test). In CUDA it is MisalignedTfq3Tensor
    (cuda/tfq3_format.zig, "CUDA TFQ3: a TFPW expert's three 4-bit alignments are checked before any of its bounds").
  • Planted control, red before green: overlaps changed to count touching spans (a.off <= b.off + b.len).
    test-tfq3-format fails "overlaps refuses exactly what main's Flash Next loop and GLM disjoint refused" (expected false, found true; 7/8 passed, 1 failed). Restored, it passes again.
  • The backends' existing TFQ3 tests pass unchanged: test-glm-tfq3-host, test-flashnext-memory-host,
    test-flashnext-load-admission, test-cuda-tfq3-host and test-cuda-host, with test-tfq3-format.
    GB10 (aarch64, head ef6ed7992c): 533/533 steps, 10,110/10,158 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.
    test-tfq3-host test-glm-tfq3-host test-tfq3-format test-flashnext-memory-host test-cuda-tfq3-host: 22/22 steps, 106/106 tests (merge 31a7122ca5).
  • x86 (-Dfixed-cpu, full CI set, --final, head ef6ed7992c): test-host test test-golden test-kernel-contract test-server-security test-pull-security fuzz-cortheon check-release-paths: 2714/2714 steps, 19,878/20,012 tests passed (134 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, so the host fixtures and main's oracles are
    the comparison against main.
  • Speed rows: the moved functions compute the same values in the same order, and every caller keeps its checks, so no
    hot path changes. These checks run once per shard at load.
  • SASS: no .cu/.cuh or 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: no Metal source, pipeline or loader line changes. The Metal-side Zig compiles through the host
    roots above.

Next pieces

  1. One file-span helper (Flash Next span == GLM span(..., false)), with the pins above.
  2. One PWV3 table check that returns a fault, each backend keeping its precheck and error names (core: one copy of the TFQ3 records and wire constants (TFQ3 format move, piece 1) #5281 item 3).
  3. File discovery, then the RMSNorm +1 rule. After cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018 lands, GLM's tensor/layer move onto the same predicates.

Proposed CHANGELOG line (Unreleased): Internal: TFQ3's shared arithmetic (span overlap, matrix words, widths, the
4-bit alignment rule) lives once in core/tfq3_format.zig; nothing a user sees changes.

The cut was settled in review: which functions to move, ?usize over a core error set, keeping
matScales/tensorScales and span out, and the overflow and refusal-order pins.

32-bit targets: the new test's 2^32 and 2^33 edges are now only on a 64-bit usize, and the two overflow cases use 1 << (bits/2 + 1) and 1 << (bits - 2). zig test -target x86-linux -fno-emit-bin on the test: fails on ef6ed7992c (usize cannot represent 4294967296), compiles on 2a82cd7c29; on the GB10 test-tfq3-format 8/8. The change touches only the test.

@ashhart

ashhart commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. This looks safe to land.

The helpers preserve arithmetic, overflow checks, span behavior and each backend's refusal order.
The oracle tests use the old in-tree bodies, and it merges cleanly on main. It goes into a landing train next, which runs CI and the device checks.

  • Synthena

ashhart and others added 6 commits November 28, 2026 19:16
Span, the overlap test, matWords, validBits and the 4-bit uint4 predicate move
into core/tfq3_format.zig. Flash Next, GLM and CUDA keep every public name,
signature, error name and check order as thin wrappers; matScales, tensorWords,
tensorScales and span stay per backend. A core test pins each moved function
against main's bodies over edge values.
@ashhart
ashhart merged commit fa7c774 into ashhart:main Nov 28, 2026
2 of 12 checks passed
ashhart added a commit that referenced this pull request Nov 28, 2026
@ashhart

ashhart commented Nov 28, 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