Skip to content

Ling: grouped expert routing on Metal, checked against a float64 reference - #1836

Merged
ashhart merged 10 commits into
ashhart:mainfrom
ThinkOffApp:ling-metal
Nov 3, 2026
Merged

ashhart merged 10 commits into
ashhart:mainfrom
ThinkOffApp:ling-metal

Conversation

@ThinkOffApp

Copy link
Copy Markdown
Contributor

Grouped expert routing for the Ling family on Metal, for #1247.

The kernel is tf_ling_route. A float64 reference checks the same picks. Host tests pin the sigmoid, the group score, keep-4, and the float64 weights. Padded picks are rejected, and the Metal weight bound is 1e-6. GPU cases cover tied groups and a planted NaN. After a NaN the picks stay unique, and the float64 reference uses distinct ids.

Rebased onto current main. zig build test with Zig 0.17 exited 0.

This is the routing step only. Tokenizer, gates, MLA, the full forward pass, and the ARM board backend stay later.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @ThinkOffApp. We checked the kernel against the reference, and it matches the f32 formula (and f32 matches float64 in the host tests) on ties (lower group, then lower id), NaN scores (never in a group score or a pick), plus and minus infinity, and groups with fewer finite experts than top-k.

One change before it lands:

  • Rows with fewer usable choices than top-k (n_group of 1, keep equal to n_group, or an all-NaN row) produce out-of-range ids: experts, experts + 1, and so on, or 1 << 30 at 512 experts. The reference gives experts. Please define an empty pick, id 0 with weight 0, in the kernel and both references, and add a test for it.

Two smaller asks:

  • Nothing validates a Shape today. ling config.parse already refuses the limits, so a Shape.of(config) in route.zig would make that the only way to build one. A Shape.check() with the same bounds would cover the contract's and tests' hand-built shapes and any future dispatch.
  • A +inf bias next to a -inf one gives a NaN group score, so that group never matches. Biases are checkpoint constants, so a refusal at load is enough.

It needs a mechanical rebase for two registration lines. After that we'll run the Metal kernel contract on an M5 and land it.

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @ashhart for the detailed check!

This is now rebased on main at 1ce4526 (the conflict was the all_metal list in registry.zig; main's list is kept, Ling's records last). It still merges cleanly with today's main.

  • Empty pick. Once every expert left in a row is NaN (an all-NaN row with n_group 1 or every group kept), the kernel and both references end the row with empty picks, id 0 with weight 0, after the real ones. selectF32 and selectF64 return how many picks are real, and the contract and GPU checks hold only those to distinct ids. New tests: host rows for both references (all-NaN with one group and with keep equal to n_group, and three usable experts under top-k 8), and the same rows on the GPU at 128 and 512 experts (512 has no padded slots, the old 1 << 30 case). The two GPU tests fail on the previous kernel and pass now.
  • Shape. Shape.of(config) builds it from a parsed config, and Shape.check() holds the bounds. config.parse now refuses through Shape.check(), so the bounds live in one place, with the same messages. The contract and GPU harnesses check their hand-built shapes.
  • Bias. route.checkBias refuses a group holding both a +inf and a -inf bias. There is no Ling weight loader in the tree yet, so the harnesses call it now and the loader will when it lands.

On an M5 Max: test-ling-host 48 pass (2 skip without TF_LING_INDEX), test-ling-route-gpu 14 pass, tf-kernel-contract --only "ling route" gates 1 to 4 pass, and the full zig build test test-kernel-contract 8733 pass, 52 skip, 0 fail.

After this making the Ventuno HW proposal as we discussed.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @ThinkOffApp: all three earlier asks are answered (empty picks, Shape.check keeping config.parse's bounds, and checkBias refusing a group with both infinities), and the routing we cleared is unchanged. One mechanical change: families/ling/route.zig adds 9 lean problems (433 against main's 424). Those are multi-line doc comments at :21, :22, :39, :43, :44, :147 and :185, and comments past 120 columns at :22 and :23. Please make each doc comment one line within 120 columns. Then it lands. A follow-up note: there's no Ling loader yet, so when one lands it must call checkBias.

@ashhart

ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Thanks @ThinkOffApp for the quick fix. It's gated and queued to land, and we've written the changelog line. One thing we changed while preparing it: your commits were authored with a personal email address, and we only publish GitHub noreply addresses in the repository's history. So the commits that land carry your noreply address ([email protected]), under your name. Nothing else changed. If you'd rather use a different public address, tell us before it merges.

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart, the noreply address is now our default for commits.

ThinkOffApp and others added 10 commits November 3, 2026 02:34
keep_groups then top-k, as glue.select and ling_forward.py take it.
Tiny (16 experts a group) and flash (64) both run. The tiny-index
TF_LING_INDEX check against tiny_config.json ships here. Refs ashhart#1247.
tf-kernel-contract now compiles: route.zig is a named module, not a
path outside it. Gate 5 knows the launch. NaN/Inf, ties and the 32-thread
record all pass. Refs ashhart#1247.
Mutation left six common-mode bugs green: top-1 group score, keep+1,
bias in the weights, sigmoid(2x), mask-to-zero, and f64 without
renorm. These cases use hand-computed weights and rankings the
shared host/Metal oracle cannot see. Refs ashhart#1247.
A padded choice of 0 beats every negative expert. GPU smoke now uses
bias-2 so those slots stay out of the picks. The 1e-5 bound hid a
1e-4 epsilon. Refs ashhart#1247.
The contract never ties at the keep boundary, and its nonfinite gate
only runs the tiny shape, so the whole-column branch never saw NaN.
Both shapes now keep the lower groups on a full tie; flash plants a
NaN at expert 64. Refs ashhart#1247.
A picked slot is marked NaN, not -inf, so a later -inf tie cannot
repeat the id. Host and Metal both do this. Tests cover a trailing
NaN in the winning group, an odd-column NaN on flash, all-NaN and
one-finite-per-group rows, all -10 / -100 weights, and a NaN-aware
weight check. Refs ashhart#1247.
selectF64 must not repeat an id after finite choices run out. The
contract states that filler picks with NaN scores make the row's
weights NaN. Refs ashhart#1247.
…he bias refusal

A row with fewer usable experts than top-k (every expert left NaN: an all-NaN row
with one group or every group kept) wrote out-of-range ids: experts, experts + 1
on the kernel's padded slots, 1 << 30 at 512 experts, and experts on the host.
The kernel and both references now end such a row with empty picks, id 0 with
weight 0, after the real picks; selectF32 and selectF64 return how many are real.

Shape.of(config) builds the shape from a parsed config, and Shape.check holds
glue.select's bounds; config.parse now refuses through Shape.check, so the bounds
live in one place. checkBias refuses a group holding both a +inf and a -inf bias,
whose top-2 sum would be NaN. The contract and GPU harnesses call both.
tools/zig/lean_check.py reported 9 problems in route.zig: multi-line doc comments at 21, 22, 39, 43, 44, 147
and 185, and comments past 120 columns at 22 and 23. Each doc comment is now one line within 120 columns, and
the file reports none. Comments only: zig fmt --check is clean and test-ling-host gives 48/50 (2 skipped).
@ashhart
ashhart merged commit bfc374f into ashhart:main Nov 3, 2026
4 of 7 checks passed
@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

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

@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Landed as bfc374f. Thanks @ThinkOffApp!

@ashhart

ashhart commented Nov 3, 2026

Copy link
Copy Markdown
Owner

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