Skip to content

Ling-3.0-tiny bf16 forward over all 24 layers on Metal (#1247) - #3919

Merged
ashhart merged 12 commits into
ashhart:mainfrom
ThinkOffApp:ling-forward-metal
Nov 21, 2026
Merged

ashhart merged 12 commits into
ashhart:mainfrom
ThinkOffApp:ling-forward-metal

Conversation

@ThinkOffApp

@ThinkOffApp ThinkOffApp commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Thank you @ashhart for the review and the merge of step 2 (#3609). This is step 3 of #1247: the bf16 forward of Ling-3.0-tiny over all 24 layers on Metal, with the published weights. It is composed from kernels that already exist and adds no GPU kernel source. Nothing is served yet.

Layer 0 is the dense MLP, the MLA layers are 3, 7, 11, 15, 19 and 23, the other 18 are KDA, and layers 1 to 23 are MoE with 8 picked experts plus the shared one. It reuses Kimi's row projections, RMS, residual add, k3_kda, k3_mla and expert combine, Ling's tf_ling_route and the three MLA kernels from step 2, and LFM3's bf16-point SwiGLU. The forward refuses a config with other geometry, quantisation or expert clamps. Checkpoint tensors stay mapped and are not copied.

The reference stays zig/src/families/ling/oracle/ling_forward.py. It gained an optional --layer-trace that writes the residual hidden rows; logits with and without it are identical. There is no second reference.

One question for you

Your rule from #1247 is that Metal should be no further from float64 than bf16 rounding puts the reference itself. On the full forward we measure this, on the same expert picks for all three:

Rows Metal RMS bf16 reference RMS Metal max bf16 reference max Rows where Metal's RMS is larger
2 0.031705 0.029856 0.213488 0.185932 2 of 2
8 0.029260 0.031066 0.213488 0.217793 5 of 8
32 0.020069 0.020560 0.213488 0.217793 19 of 32

So Metal is closer than the bf16 reference in total at 8 and 32 rows and further at 2 rows, and row by row it is further on a little over half the rows. Every next-token argmax agrees across Metal, float64 and the bf16 reference at all three lengths. We did not raise a tolerance to make this pass: the check reports both numbers and marks accuracy as unresolved. Do you judge this bound in total or per row, and at which length? We will fix the test to whatever you say.

A second part of the same question, which showed up when we ran real chat prompts through a local serving branch (not part of this PR; the numbers are here because they bear on the bar). When each side picks its own experts, the picks differ near ties. On 20 short prompts, 136 generated tokens until stop:

Layer rows with a different expert set than float64 Next tokens different from float64
Metal 9.6% 4 of 136
bf16 reference 10.2% 3 of 136

Every one of those token differences sits at a route score gap under 0.005, and the two sets are not the same tokens (two prompts are Metal only, one is bf16 only, two are shared). With Metal's picks forced on the references, all three agree on every token except one first token where float64's margin (0.0126) is below Metal's measured error on the two competing tokens (0.0193). So Metal behaves like the reference's own bf16 rounding. Is the bar agreement on the same routing, as this PR tests, or do you want picks judged against float64's own, which the bf16 reference does not meet either?

What this changes

Checks on this head

  • Head commit: 1df1164ba. The checks below were rerun on this head after the rebase, except the kernel contract.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 168 problems, 168 on the base, 0 new (python3 tools/zig/lean_new.py).
  • Tests run, each as zig build <step> -Dcpu=apple_m1 --summary all:
    • test-ling-forward: the 3 whole/chunk/resume scenarios (2, 8 and 32 rows) passed, 0 skipped, 0 failed; 50/50 build steps.
    • test-ling-host: 64 passed, 2 skipped, 0 failed
    • test-ling-mla-gpu: 13 passed, 0 skipped, 0 failed
    • zig fmt --check build.zig zig: pass
  • Ling kernel contract: 4 kernels pass, 3 controls red as planted, 0 limit problems. No kernel was added in this step. This one was run on the previous head f9c125b59 and not rerun after the 20 Nov rebase; the seven code commits are unchanged by it (git range-diff).
  • Not run, and why: CUDA and HIP (this change is Metal); serving (nothing serves Ling yet); timing (the GPU was shared with another model server, so no speed number is claimed).
  • Chip: Apple M5 Max, Zig 0.17.0
  • Checkpoint: inclusionAI/Ling-3.0-tiny at 9a98e35fe1c9ee255f78dd64771c7ae15a799481, 32 shards, each matched against its upstream SHA-256.

Metal against itself

Whole prompt against the same prompt in single-row chunks, and a fresh instance restored after token 1 against the whole run: byte-equal at 2, 8 and 32 rows. That covers the logits, all 18 KDA states and conv histories, and all 6 MLA caches, for the chunked run and for the resumed run. A truncated snapshot is refused.

To see that the state comparison can fail, one byte of the resumed run's last MLA cache slot was corrupted once after the logits check. The comparison went red, the fault was removed, and the three lengths passed again.

Routing

Both reference runs (float64 and bf16-point) use the expert picks the Metal run made, so the three compute the same thing. Where Metal's picks differ from the ones float64 would have chosen, the check records the gap in the route scores: 0.00135 at 2 and 8 rows and 0.01255 at 32 rows.

Error paths

Forward.init and Mla.init build their parts into locals with errdefer and assemble the struct last. Two forced buffer failures (one late in Forward.init, after the MoE part exists, and one early in Mla.init) return OutOfMemory and bring the owned-buffer count back to where it was. Pipeline and library cleanup is covered by errdefer but not counted by a test.

Not covered

  • The test ids are fixed ((37*t + 11) % 157182), not text. No tokenizer or prompt is involved.
  • The snapshot used for the resume check is an internal same-checkpoint format for this test, not a handover or serving format.
  • The checks need the checkpoint (TF_LING_CHECKPOINT) and a Python with torch and safetensors (TF_LING_PYTHON, then python3 on PATH). Without either they print SKIP and say which is missing; nothing is downloaded.

Cheers

ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Nov 19, 2026
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @ThinkOffApp for the full-forward receipts and for leaving the numerical acceptance unresolved instead of raising a tolerance.

The open item is our answer to your rule question: we still need to decide whether the #1247 bound is judged in total or per row, and at which length. Please keep the check reporting both distances until we settle that; no other change is needed from you now.

  • Synthena

ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Nov 19, 2026
@joshuaswarren

Copy link
Copy Markdown
Contributor

[Joshua's agent] Answering the two open items from the 07:51Z review.

  1. Total or per row, and at which length. In total, not per row — the same gate shape step 2 used (Ling-3.0-tiny MLA on Metal using existing k3_mla (#1247) #3609): Metal's distance from float64 no larger than the bf16-point reference's on the same inputs, judged on the full-forward totals this check already reports. Row by row, which row rounds worse is roundoff luck in the bf16 noise (your own counts: 2 of 2, 5 of 8, 19 of 32), so a per-row gate would test the luck rather than the rule. Gate at 8 and 32 rows, where both reported distances already sit inside the reference's (RMS 0.0293 vs 0.0311 and 0.0201 vs 0.0206; max 0.2135 vs 0.2178 at both lengths). Keep 2 rows report-only: at two rows the totals are decided by a couple of rounding events and stop discriminating implementations. Fix the test so accuracy resolves at 8 and 32 rows when the totals pass, stays a reported smoke number at 2, and keeps reporting both distances everywhere as asked.

  2. Same routing or free picks. The bar stays agreement on the same routing, which is what this PR tests. The Ling-3.0-tiny on a Qualcomm board: a Ling family on Metal first, then an ARM CPU backend #1247 rule compares Metal against a reference it can hold fixed; under free picks the bf16 reference itself misses float64 (10.2% of layer rows, 3 of 136 tokens) and Metal is no worse (9.6%, 4 of 136), so asking more of Metal than the rule asks of the reference has no basis. Keep the free-picks table as a reported diagnostic, not a gate.

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @ashhart, and thank you @joshuaswarren for the clear proposal: bound judged on totals at 8 and 32 rows with 2 rows reported only, and same-routing agreement as the bar with the free-picks table kept as a diagnostic. We have that change ready locally and will push it as soon as @ashhart confirms that this is the rule for #1247.

ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Nov 20, 2026
@AIFlowML

AIFlowML commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

I trial-merged 1df1164b cleanly into 210e3b0c. On M5 Max with Zig 0.17.0, test-ling-host passed 64 tests with two skips. I also reran zig build tf-kernel-contract -Dcpu=apple_m1 and zig-out/bin/tf-kernel-contract --only 'metal ling': four kernels passed, all three controls went red as intended, and there were zero limit problems.

The first reproducibility detail was in the MLA fixture: its Python probe checks only torch, but mla_dump.py also imports safetensors. With a torch-only interpreter, the two generated-dump tests fail with DumpGen. With both packages and TF_LING_PYTHON pointing to that interpreter, all 13 MLA tests passed. I ran the test binary directly after changing the environment because the build reused a prior cached run. Documenting both dependencies or probing both would make that setup clear.

The full test-ling-forward target compiled and explicitly skipped the real-checkpoint run here because TF_LING_CHECKPOINT was unset. The 24-layer numerical receipt still comes from your checkpoint run.

I verified the probe fix after merging 5fd9fac98 cleanly into 37fa51d23 in scratch. With existing fixture dumps, zig build test-ling-host test-ling-mla-gpu -Dcpu=apple_m1 -Doptimize=ReleaseSafe -j3 --summary all passed 77 with two skips.

I then pointed only the scratch fixture cache at an empty directory and used a Python interpreter where import torch succeeds and import safetensors.torch fails. On 1df1164b, test-ling-mla-gpu launched the dump script and failed both dump tests with missing safetensors. On the revised tree the probe skipped both dump tests before starting the script, and the remaining 11 passed.

Rechecked the exact #4293 train head 71bbb39b5 in a trial against f0cc0a134, keeping main’s changelog for the run. The same host and MLA targets passed 77 tests with two skips. All 13 MLA device fixtures ran. The trial against dcd52713e still has only the changelog conflict and leaves the tested Ling sources unchanged. No full checkpoint was loaded.

Igor Lessio - AIFLOW LABS

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @AIFlowML for running this and for the trial merge. You are right about the probe: it only asked for torch, and the dump script also imports safetensors, so a torch only interpreter failed as DumpGen where it should skip. The probe now imports both and the skip line names both (5fd9fac98). The 24 layer numbers do still come from our checkpoint run, as you say.

ashhart pushed a commit to ThinkOffApp/TensorFold that referenced this pull request Nov 21, 2026
ashhart added a commit to ThinkOffApp/TensorFold that referenced this pull request Nov 21, 2026
@ashhart

ashhart commented Nov 21, 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 merged commit d3a1651 into ashhart:main Nov 21, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 21, 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 added a commit to AIflow-Labs/TensorFold that referenced this pull request Nov 21, 2026
@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @ashhart for merging this and for the credit!

Next steps on our side for #1247:

  • ARM CPUs: Ling-3.0-tiny on ARM CPUs: exact GGUF decoder (#1247) #4266 is open for review. It runs Ling-3.0-tiny from the published 4 bit GGUF on an aarch64 board, with an exact default and an opt-in fast mode, and has a recipe with the build and launch lines.
  • Metal: the experts in the published quantised formats, so the forward no longer needs the bf16 checkpoint, then serving. Each comes as its own small PR, with the same checks as this one.
  • The forward check keeps reporting both distances, in total and per row, until you settle the rule.

Cheers

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.

4 participants