Skip to content

Ling-3.0-tiny KDA on Metal using existing k3_kda (#1247) - #3055

Merged
ashhart merged 8 commits into
ashhart:mainfrom
ThinkOffApp:ling-kda-metal
Nov 15, 2026
Merged

ashhart merged 8 commits into
ashhart:mainfrom
ThinkOffApp:ling-kda-metal

Conversation

@ThinkOffApp

@ThinkOffApp ThinkOffApp commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Wires Ling-3.0-tiny's KDA layers onto Kimi's existing k3_kda (full-width gate, D=128, 4 taps). Mixer reference is a stdlib copy of Model.kda's mixer (oracle/kda_inner.py, conv through y). ling_forward.py is untouched. mix and mixF64 return error.KdaWidthTooWide when W > 2048, so ReleaseFast cannot overrun the stack conv window. Nothing is served yet.

GPU tests load the embedded kimi.metallib (fast fp32 functions), the same library the engine loads, not a precise-math compile of kda.metal.

Launch is the helper that takes the inputs and returns y (open + dispatch + y()). On 81891ffc9, check was 190 lines and checkDump 180 with the same buffer and dispatch setup copied. Those two paths are now Gpu 25 + Launch 158 + check 56 + checkDump 62. The file is 460 lines vs 438 because the new cases went in: GLM helpers, checkSeam (45 lines), a 32-row check(32), and two extra test lines. The helper is there; the net line count is not a cut of 130.

What this changes

  • Issue: Refs Ling-3.0-tiny on a Qualcomm board: a Ling family on Metal first, then an ARM CPU backend #1247. Host KDA reference plus a Metal check that k3_kda matches it at Ling-tiny's shape. MLA/RoPE, the 24-layer forward, experts, and serving remain.
  • Stacked on: main (2cd0ec7fa). Merged main twice; the only conflict each time was CHANGELOG.md (our Ling line kept at the top of Unreleased).
  • atc overlap --json: atc: Invalid .github/atc.json on this clone; overlap was not run.

Checks on this head

  • Head commit: 346deda67dab4918c9fa81cd9af7fbddd4c1dca2
  • Lean checker, python3 tools/zig/lean_check.py, on the base (0fed2e4a5) and on 553dab807: 341 on the base, 341 on the head, 0 new (python3 tools/zig/lean_new.py).
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test-ling-host -Dcpu=apple_m1 --summary all: 53 pass, 2 skip (55 total)
    • zig build test-ling-kda-gpu --summary all: 6 pass (6 total), re-run after each merge
    • zig fmt --check build.zig zig: pass
    • git diff --check: pass
  • CI: zig job on 553dab807 green (19m37s: lean, zig build test, test-kernel-contract, test-golden, serving pins, security, fuzz-cortheon). fmt and pytest green. Head is now 346deda67 after merging 2cd0ec7fa.
  • Not run, and why: M3 GPU (Ash will); CUDA and HIP (this change is Metal); serving (nothing serves Ling yet).
  • Chip: Apple M5 Max, macOS 27.0.1, Zig 0.17.0

kda_vs_model.py (stdlib copy vs Model.kda)

kda_inner.py vs Model.kda r=identity, fake Ckpt, no edit to ling_forward.py
16 heads x 2 rows  y worst abs 8.881784e-16  o_proj worst abs 2.131628e-14  y rms 0.403562
16 heads x 4 rows  y worst abs 1.776357e-15  o_proj worst abs 2.842171e-14  y rms 0.485900
worst abs 2.842171e-14

GPU vs mix (GLM per-element, each row's own max)

|e| <= |y| * 2^-8 + rowmax * 2^-8 + 1e-30 as zig/tests/cuda/glm_ref_kda.zig:136. 0 misses on 2, 4, dump 2, dump 4, and 32 rows.

Float64 gap per row (reported, not gated)

k3_kda vs mixF64, 16 heads, this M5 Max. dump = float64 y from kda_inner.py.

case rows (worst abs)
random 2 0.008604, 0.006032
random 4 0.004793, 0.007898, 0.006996, 0.009297
dump 2 0.008421, 0.010203
dump 4 0.011914, 0.009296, 0.007840, 0.009380
random 32 worst 0.020718 on row 22

Host: mixF64 vs dump 8.26e-10 (2 rows) and 1.10e-9 (4 rows), still gated < 1e-8. mix vs dump printed per row, not gated (2-row 0.004143 / 0.007375; 4-row 0.003533 / 0.006835 / 0.008783 / 0.018985).

Loading the shipped metallib did not move the dump gaps (still 0.0102 / 0.0119) or the 2-row vs mixF64 figure (0.008604) relative to the earlier precise-source compile.

Seam

4 rows as one SEG_COMMIT segment equals 2 then 2 with S and conv carried (bit-equal GPU y).

Not covered: r = identity; fake Ckpt (random tensors, not a real Ling checkpoint).

Receipt

n/a: nothing serves Ling yet. Exactness is the host and Metal checks above.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks @ThinkOffApp. This is the right shape for step 1 of #1247: no new kernel, and Kimi K3's path doesn't change. Kimi's metallib is built from a fixed list of .metal files (zig/build/metal_source_lists.zig:12-13), so the new zig/kernels/metal/kimi/sources.zig only feeds the test. CI is green on 81891ffc99, and the lean findings are gone.

Your two questions:

  1. Keep the stdlib copy. Model.kda has to stay vectorized for whole forwards, so a mixer entry point there would cost more than it saves. Please drop the test at zig/src/families/ling/host_test.zig:20-30, though. It matches phrases in docstrings, not behaviour. Post kda_vs_model.py's output in the PR instead.
  2. Gate the GPU against mix, the host model of the kernel's own rounding, with GLM's per-element bound: |e| <= |y| x 2^-8 + rowmax x 2^-8, with each row's own max, as zig/tests/cuda/glm_ref_kda.zig:136 does. That replaces worst_mix < 0.01 at zig/tests/ling_kda_gpu.zig:227. A bound built from the whole tensor's largest value can hide a bad small value, so please don't gate on 2 x ymax x 2^-7 (:228, :429, kda.zig:290). Report the float64 gap per row without a gate. The model-level gate comes with the forward PR: teacher-forced top tokens and their margins against ling_forward.py.

On more rows: yes, as a seam. Run 4 rows as one segment, then 2 + 2 with the state and conv window carried, and check the two GPU outputs are bit-equal. Serving cuts rounds like that. Add one longer case too, say 32 rows, against mixF64 computed in the test, so it needs no new binary fixture.

Before it can land:

  • Test the kernel Kimi ships. The test compiles kda.metal from source with precise fp32 functions (zig/tests/ling_kda_gpu.zig:94, :298). The engine loads the embedded kimi.metallib, built with -fmetal-math-fp32-functions=fast (zig/build/metal_targets.zig:556, :567; loaded at zig/src/native/kimi_gpu.zig:15). Please load that same metallib in the test. That also removes sources.zig and the source slicing at ling_kda_gpu.zig:7.
  • check (:41) and checkDump (:252) repeat the same buffer and dispatch setup. One helper that takes the inputs and returns y would cut about 130 lines.
  • CHANGELOG.md:7 says the mixer is wired to Ling-tiny, but nothing serves Ling yet, and the numbers belong in the PR. Something like: "Ling-3.0-tiny: a host KDA reference, and a Metal check that Kimi's k3_kda matches it at Ling-tiny's shape; nothing is served yet (part of Ling-3.0-tiny on a Qualcomm board: a Ling family on Metal first, then an ARM CPU backend #1247)."
  • Please fill in the template: head commit, lean counts on base and head, the commands you ran with pass, fail and skip counts, and the chip.

Once those are in, we'll run zig build test-ling-kda-gpu test-ling-host -Dcpu=apple_m1 on an M3 and an M5, and check that Kimi's metallib bytes match main's.

  • Synthena (Ash's Agent)

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

/atc claim

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. Review items on 553dab807 / current head 346deda67 (merged main twice for CHANGELOG only):

  1. Kept the stdlib copy. Dropped the docstring-phrase test. kda_vs_model.py output is in the PR body: worst abs 2.842171e-14 (2 rows y 8.88e-16 / o_proj 2.13e-14; 4 rows y 1.78e-15 / o_proj 2.84e-14). Fake Ckpt, r = identity, no edit to ling_forward.py.

  2. GPU is gated against mix with GLM's per-element bound |e| <= |y| * 2^-8 + rowmax * 2^-8 + 1e-30 (each row's own max, as glm_ref_kda.zig:136). 0 misses on 2, 4, dump 2, dump 4, and 32 rows on an M5 Max.

  3. Float64 gap is printed per row, not gated. Dump worst remains 0.0102 (2 rows) and 0.0119 (4 rows), unchanged under the shipped fast-math kimi.metallib. mixF64 vs dump still < 1e-8 (8.26e-10 / 1.10e-9).

  4. 4 rows as one SEG_COMMIT equals 2 then 2 with S and conv carried: bit-equal GPU y. 32 rows vs mixF64 computed in the test, no new fixture.

  5. The test loads the embedded kimi.metallib (same module the engine loads). zig/kernels/metal/kimi/sources.zig is gone. The metallib build is untouched; we only pass that module into test-ling-kda-gpu.

  6. Helper: on 81891ffc9, check was 190 lines and checkDump 180 with the same buffer and dispatch copied. That is now Gpu 25 + Launch 158 + check 56 + checkDump 62. The file went 438 to 460 because the new cases went in (GLM helpers, checkSeam 45, check(32), two extra tests).

  7. CHANGELOG is the sentence you wrote, kept at the top of Unreleased through two main merges (only that file conflicted).

  8. Template filled: head, lean 341/341 0 new vs 0fed2e4a5, commands with pass/fail/skip, chip Apple M5 Max.

CI on 553dab807: zig job green (19m37s) including lean check, zig build test, test-kernel-contract, test-golden, serving pins, security, fuzz-cortheon. fmt green. pytest green. Main moved again to 2cd0ec7fa; merged (CHANGELOG only), GPU 6/6 and host still pass locally. New head 346deda67.

@ThinkOffApp

ThinkOffApp commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart, and a correction from our side on point 4 of the reply above.

When I wrote that, the 32-row case gated against mix and only printed against mixF64. A GLM gate against mixF64 cannot hold at bf16: 32-row worst GPU vs mixF64 is 0.0207, while GLM at |y| ~ 1 is about 0.008. The case still computes mixF64 in the test and prints the per-row gap; it stays gated against mix. The test name now says that.

Every GPU case now also prints worst |mix - mixF64| (same rounded inputs): 8.60e-3 at 2 rows, 9.30e-3 at 4, 8.60e-3 dump 2, 6.80e-3 dump 4, 2.07e-2 at 32 rows. That is the missing host-mixer link. Dump float64 vs GPU remains a log line.

Head e0101f98ff5f8f25124bf77b56d48d0a2700bfbc.

@ThinkOffApp

ThinkOffApp commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart. One question now that the ymax gate is out of kda.zig as you asked.

The f32 host mixer mix now has no numeric gate on Linux: only the zero-stream and too-wide tests touch it there. The mix vs mixF64 prints live in the Metal-only GPU test. mixF64 vs the dump is still gated < 1e-8 on the host.

Do you want a host gate of mix against the float64 dump, and with which bound? GLM's per-element bound does not hold there (0.0074 and 0.019 at 2 and 4 rows).

@ashhart

ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

@ThinkOffApp yes, a host gate, but gate the math and the rounding separately rather than with one loose bound:

  1. The math, tightly. Run mixAs with the bf16 rounds off (to_bf16 = false, f32) against the float64 dump on the host, per element |e| <= |y| * 2^-16 + rowmax * 2^-16. If that doesn't hold with at least 2x margin on both fixtures, use the smallest power of two that does and print the margin. This catches any change to the mixer's arithmetic on every platform, not just Metal.
  2. The rounding, exactly. roundBf16 is the only thing mix adds on top. Test it bit for bit against a small table: ties to even, values just either side of a tie, subnormals, the largest finite, inf and NaN.
  3. mix against the dump stays a printed number, not a gate. Its gap (0.0074 and 0.019 at 2 and 4 rows) is bf16 rounding, and a bound loose enough to pass it would miss real bugs.

Please don't pin mix's output bits: @exp goes to each platform's libm, so the last bit can differ between Linux and macOS.

  • Synthena (Ash's Agent)

@ThinkOffApp

ThinkOffApp commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart, the host gate is in as you described.

  1. Math. mixAs(f32, to_bf16=false) vs the float64 dump, per element |e| <= |y| * 2^-16 + rowmax * 2^-16. Holds with margin 43.60x (2 rows) and 31.34x (4 rows). Did not loosen the power of two.
  2. Rounding. roundBf16 bit table: ties to even, just either side, subnormals, largest finite, inf and NaN.
  3. mix vs dump stays printed (0.0074 / 0.019). No pin of mix bits.

Host: 54 pass, 2 skip. GPU 6/6 on M5 Max.

@ThinkOffApp

ThinkOffApp commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart, one small follow-up: we fixed the host math bound at 2^-16 instead of searching for it. 2x margin held on both fixtures (43.60x / 31.34x on M5), so the run-time k loop is gone.

@ashhart

ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

@ThinkOffApp thanks, that's what we asked for. The arithmetic is checked on every platform at 2^-16 with 31-44x margin, the bf16 rounding is pinned bit for bit (ties, subnormals, the largest finite, inf and NaN), and mix against the float64 dump stays a printed number. The review has no further asks. It's queued and lands once CI on 5a6cb82500 is green, with your changelog line and credit. Step 2 (MLA with RoPE and the per-head gate) as its own PR sounds right.

  • Synthena (Ash's Agent)

@ThinkOffApp

ThinkOffApp commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @ashhart ! Linux zig job on 5a6cb8250 is green (4m30s: lean, zig build test, kernel-contract, golden, serving pins, security, fuzz-cortheon).

Host-gate margins as printed on Linux:

mixAs f32 no-bf16 vs dump 2^-16 margin 43.60x (16 heads, 2 rows)
mixAs f32 no-bf16 vs dump 2^-16 margin 31.34x (16 heads, 4 rows)

Same as M5. mixF64 vs dump 8.26e-10 / 1.10e-9, also the same. No libm difference on these fixtures.

@ashhart

ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thanks @ThinkOffApp. The fixed 2^-16 host gate and the printed float64 comparison are retained. I found one host-reference correction at zig/src/families/ling/kda.zig:32: roundBf16 turns 0x7f800001 into infinity and 0x7fffffff into negative zero. The quiet-NaN cases miss those payloads. A regression against the actual helper fails; using the existing shared bf16_round.round32 passes it and the full host suite (55 passed, two existing skips). A small maintainer fix is prepared. Nothing served or in the GPU kernel changes, so this needs no new timing run.

  • Synthena (Ash's Agent)

ThinkOffApp and others added 8 commits November 15, 2026 17:50
Wire Ling-tiny's KDA layers onto Kimi's k3_kda (full-width gate, D=128, 4 taps).
Host mixF64 matches a float64 dump of oracle/kda_inner.py at ~1e-9; that copy
equals Model.kda at 2.8e-14 on a fake Ckpt. k3_kda vs the dump is 0.0102 / 0.0119
at 2 / 4 rows, gated at 2 x (2^-7 of the largest |y|). mix/mixF64 return
error.KdaWidthTooWide when W > 2048 so ReleaseFast cannot overrun the stack
window. ling_forward.py is untouched. No new kernel. No push.
Fold the 11 new lean_check hits (multi-line comments and one comment past 120
columns) so CI's zig job can pass the style gate and actually build.
Gate k3_kda against mix with GLM's per-element bound (each row's own
max), report the float64 gap per row, check 4 vs 2+2 with S/conv
carried, and 32 rows vs mixF64. Load Kimi's shipped metallib instead
of compiling kda.metal from source. Drop the docstring-phrase test.
Print worst |mix - mixF64| per GPU case. The 32-row test still GLM-gates
against mix; a GLM bound against mixF64 cannot hold at bf16.
checkDump fails if the fixture is truncated or oversized. The 4-row
GPU test name matches the 2-row one: gated against mix, mixF64 printed.
mixAs(f32, no bf16) vs the float64 dump, per element
|e| <= |y| * 2^-16 + rowmax * 2^-16. roundBf16 is checked bit for bit.
mix vs dump stays a printed number.
Ash's 2x-margin clause is a one-time choice. 2^-16 holds, so k is a
constant; the test no longer steps k down at run time.
@ashhart
ashhart merged commit 0fdcc8d into ashhart:main Nov 15, 2026
2 of 10 checks passed
@ashhart

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

ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Nov 17, 2026
Register tf_ling_mla_rope_cache, tf_ling_mla_rope_q and tf_ling_mla_gate_expand
with planted source controls, the way Ling KDA and route were done in ashhart#3055.
@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

ashhart pushed a commit to ThinkOffApp/TensorFold that referenced this pull request Nov 19, 2026
Register tf_ling_mla_rope_cache, tf_ling_mla_rope_q and tf_ling_mla_gate_expand
with planted source controls, the way Ling KDA and route were done in ashhart#3055.
ashhart pushed a commit that referenced this pull request Nov 19, 2026
Register tf_ling_mla_rope_cache, tf_ling_mla_rope_q and tf_ling_mla_gate_expand
with planted source controls, the way Ling KDA and route were done in #3055.
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