Repository navigation
core: one copy of TFQ3's shared arithmetic (TFQ3 format move, piece 2) - #5341
Merged
Merged
Conversation
Owner
|
Thank you, @jschmied. This looks safe to land. The helpers preserve arithmetic, overflow checks, span behavior and each backend's refusal order.
|
This was referenced Nov 27, 2026
ashhart
force-pushed
the
core-tfq3-arith
branch
2 times, most recently
from
November 28, 2026 10:20
f12be77 to
5d1ef19
Compare
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.
… so the test compiles for x86-linux
ashhart
force-pushed
the
core-tfq3-arith
branch
from
November 28, 2026 19:16
5d1ef19 to
dd70da8
Compare
ashhart
added a commit
that referenced
this pull request
Nov 28, 2026
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Head:
2a82cd7c29on main91dbadce65(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 thearithmetic 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 twousizefields. Both now alias it.overlaps(head, spans) bool: GLM'sdisjointand the loop inline in Flash Next'sheaderwere the same test (twonon-empty spans share a byte, or one starts inside the header). GLM's
disjointand Flash Next still raiseTfq3Overlap.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!usizeas wrappers that map null toTfq3Overflow.validBits(bits): 3, 4 or 5. CUDA'svalidBitsis now an alias. Flash Next'sb_bits < 3 or b_bits > 5(au32,so the same set) calls it.
uint4Misaligned(bits, word_off):bits == 4 and word_off % 4 != 0, the kernels' uint4 loads. Flash Next's expertcheck, 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:
matScales(Flash Next plainusize, GLM checked) and CUDA'stensorScales(u32in,u64out) give the samevalue, but their overflow behaviour differs. Routing CUDA's through
usizewould add a 32-bit trap for inputs itsAPI can hold today.
tensorWordsuses a different formula (16-row strips of 128-column blocks). It agrees withmatWordsonlyon the 16 x 128 grid (R=17, C=129, b=4: 256 vs 274; core: one copy of the TFQ3 records and wire constants (TFQ3 format move, piece 1) #5281 item 9).
span: Flash Next's is the same as GLM's withgpu = false, but it has two refusals (Tfq3OverflowbeforeTfq3Truncated). It is the next range piece, with its own pins: end == file_len, a zero length past the end, andoverflow before truncation.
tensordown (line 135 on main): cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018 rewritestensor,nonMoeandlayer. GLM's own 3..5 and uint4expressions there stay inline until cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018 lands. GLM's
mappedLenand its 4/5-bit dense rule stay as they are.halfs * 2,words * 4): one multiply each, and GLM's sites are in cli: add tensorfold tfq3 MODEL subcommand for TFQ3 conversion and inspection #5018's part.Evidence
test-tfq3-formatgainszig/src/core/tfq3_arith_test.zig. It keeps main's bodies verbatim as oracles (Flash Nextand GLM
matWords, CUDAvalidBits, Flash Next's inline overlap loop, GLM'sdisjoint) and compares each movedfunction with them:
matWordsover every combination of 28 dimensions (0, 1, 15/16/17, 127/128/129, the u32 and usize limits andtheir /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.
validBitsover 0..69 and the u32 top values, against CUDA's set and Flash Next's range.uint4Misalignedover widths 0..8 and offsets up tomaxInt(u64).overlapsagainst 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.
uint4 boundary but past the words, with up one word off, is
Tfq3Truncatedin Flash Next(
families/flashnext/tfq3_check_test.zig, last test). In CUDA it isMisalignedTfq3Tensor(
cuda/tfq3_format.zig, "CUDA TFQ3: a TFPW expert's three 4-bit alignments are checked before any of its bounds").overlapschanged to count touching spans (a.off <= b.off + b.len).test-tfq3-formatfails "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.test-glm-tfq3-host,test-flashnext-memory-host,test-flashnext-load-admission,test-cuda-tfq3-hostandtest-cuda-host, withtest-tfq3-format.GB10 (aarch64, head
ef6ed7992c): 533/533 steps, 10,110/10,158 tests passed (48 skipped).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 (merge31a7122ca5).-Dfixed-cpu, full CI set,--final, headef6ed7992c):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.zig fmt --check,tools/pr_check.py0 errors (lean 123 vs 123, 0 new; import-cycle edges 24 vs 24), andgit diff --check.Not run, and why:
the comparison against main.
hot path changes. These checks run once per shard at load.
.cu/.cuhor kernel source is in the diff.roots above.
Next pieces
span== GLMspan(..., false)), with the pins above.tensor/layermove 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,
?usizeover a core error set, keepingmatScales/tensorScalesandspanout, 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 use1 << (bits/2 + 1)and1 << (bits - 2).zig test -target x86-linux -fno-emit-binon the test: fails onef6ed7992c(usizecannot represent 4294967296), compiles on2a82cd7c29; on the GB10test-tfq3-format8/8. The change touches only the test.