Skip to content

Ling-3.0-tiny MLA on Metal using existing k3_mla (#1247) - #3609

Merged
ashhart merged 4 commits into
ashhart:mainfrom
ThinkOffApp:ling-mla-metal
Nov 19, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
ThinkOffApp:ling-mla-metal

Conversation

@ThinkOffApp

Copy link
Copy Markdown
Contributor

Wires Ling-3.0-tiny's MLA layers onto Kimi's existing k3_mla kernels, which are unchanged. Three small Ling kernels sit around them in zig/kernels/metal/ling/mla.metal: rope_cache rotates the 64 rope columns of the cache rows k3_mla_cache just wrote, rope_q rotates q's rope part, and gate_expand widens Ling's per-head gate (T, H) to (T, H*128) on the GPU so k3_mla_uv keeps Kimi's stride. Nothing is served yet.

RoPE follows ling_forward.py rot() and HF apply_rotary_pos_emb_interleave: GPT-J pairs, written back as halves, theta 6e6, all 64 rope dims (the checkpoint's partial_rotary_factor 0.5 is overridden to 1.0 in the HF modeling file). Softmax scale is 1/sqrt(192).

The host computes cos and sin for each row's position in float64 and rounds them to float32; the two rope kernels only apply them. With the angle computed in float32 on the GPU, rope_q at positions 29998 and 29999 came out further from float64 than the bf16 reference (rel L2 1.624e-3 against 1.616e-3), because at pos 30000 the frequency-0 angle is off by up to about 2e-3 rad. With host angles the two match.

ling_forward.py's mla() takes a pos0 used only for the RoPE check: mla(x, pos0) attends over the rows it is given, so a chunk at pos0 > 0 is that chunk as if it were the whole prompt. No test compares a Metal chunk with mla(chunk, pos0). Metal against the reference runs on whole prompts from position 0; whole against chunks and resume against fresh are Metal against Metal. one_mla is the one-layer entry and reuses Model.mla; --mla-layer refuses any layer without attention.kv_a_proj_with_mqa in the checkpoint, so a KDA layer or a missing index is refused.

What this changes

Checks on this head

  • Head commit: 90798a31e7f1295c75d60e5eb3c7b99da5fc1c7d
  • Lean checker, python3 tools/zig/lean_check.py, on the base (077998f7d) and on this head: 221 problems, 221 on the base, 0 new (python3 tools/zig/lean_new.py).
  • Tests run:
    • zig build test-ling-host --summary all: 64 passed, 2 skipped (TF_LING_INDEX unset), 0 failed
    • zig build test-ling-mla-gpu --summary all: 13 passed, 0 skipped, 0 failed
    • zig fmt --check build.zig zig: pass
  • Not run, and why: CUDA and HIP (this change is Metal); serving (nothing serves Ling yet).
  • Chip: Apple M5 Max, Zig 0.17.0

Reference: mixF64 against ling_forward.py

ling_forward.py --mla-layer 3 writes the float64 layer output (TFLINGA1); mixF64 plus host o_proj is compared with it.

case mixer rel L2 mixer max abs with o_proj rel L2 with o_proj max abs
T=4, pos0=0 2.41e-8 1.69e-8 2.42e-8 1.34e-8
T=2, pos0=30000 (host only) 2.86e-8 2.19e-8 2.93e-8 1.47e-8

Metal against float64

Gate: Metal's rel L2 and max abs against float64 are each no larger than the bf16 reference's.

case Metal rel L2 Metal max abs bf16 ref rel L2 bf16 ref max abs
T=4 against TFLINGA1 4.71e-3 2.75e-3 4.88e-3 2.75e-3
rope_q alone, pos 29998 and 29999 1.616e-3 3.895e-3 1.616e-3 3.895e-3
rope_cache alone, pos 29998 and 29999 1.757e-3 3.846e-3 1.757e-3 3.846e-3

The rope tests use a cache slot of 30000 keys, so the rows sit at their real positions and addresses.

Chunks and resume

Whole prompt against the same prompt in chunks, and resume against fresh: bit-equal at T=4 (2+2), T=100 (37+63) and T=257 (256+1; 256 is k3_mla's Metal prefill row cap).

rope_q, rope_cache and gate_expand alone are bit-equal against the host rotate and expand.

Not covered: the weights are seeded random values in Ling-3.0-tiny's tensor shapes (hidden 1536, 16 heads, q_lora 256, kv_lora 512), not the published weights. The test data is generated by oracle/mla_dump.py from ling_forward.py when torch is installed; without torch those tests skip and say so.

@ashhart

ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks @ThinkOffApp for putting Ling-3.0-tiny's MLA layer on the existing k3_mla, with the float64 host reference, the dump check and the chunk and resume bit-equality tests.

Two things only you can do. First, zig/tests/ling_mla_gpu_dump.zig sets dump_venv to a Python interpreter in a home folder on your machine; please take that path out of the commits themselves, because a later commit would leave it in history, and read the interpreter from a setting or PATH instead. Second, please register the three new kernels (tf_ling_mla_rope_cache, tf_ling_mla_rope_q, tf_ling_mla_gate_expand) with the kernel contract, with planted controls, and post the Metal SUMMARY line from zig build kernel-contract -Dcpu=apple_m1. The changelog line is ours.

  • Synthena

@ThinkOffApp

Copy link
Copy Markdown
Contributor Author

Thank you @ashhart for the quick review, and for catching the path!

Both are done on the new head 218358282. The branch is rewritten as three commits on main 077998f7d, so this was a force-push.

The interpreter path. It is gone from the commits themselves: git log -p 077998f7d..HEAD has no home-folder path. zig/tests/ling_mla_gpu_dump.zig now takes the interpreter from TF_LING_PYTHON, then python3 on PATH, and skips with a message naming both when neither has torch.

The kernel contract. tf_ling_mla_rope_cache, tf_ling_mla_rope_q and tf_ling_mla_gate_expand are registered with one planted control each (the two rope kernels rotate every row at cs[0]; the gate takes head 0 for every head). The three alone:

SUMMARY 3 kernels: 3 pass gates 1-4, 0 fail, 0 known findings; 3 controls: 3 red as planted, 0 missed; 0 limit problems; gates 10 ms, limits 224 ms

The full Metal run on this head (Apple M5 Max, built with -Dcpu=apple_m1, seed 0x6b63):

SUMMARY 815 kernels: 813 pass gates 1-4, 2 fail, 57 known findings; 697 controls: 697 red as planted, 0 missed; 0 limit problems; gates 4793230 ms, limits 20340 ms

Two things about that run that you should know:

  • The 2 failures are metal masked-V LFM2 prompt nax inf and metal masked-V LFM2 prompt nax nan (nonfinite, shapes, sums; repeat passes). They fail the same way on plain main 077998f7d on this machine, with the same first line (first=0 got=finite(0.94140625) want=finite(1)), so they are not from this PR. Tell me if you want an issue for them with the full lines.
  • zig build kernel-contract -Dcpu=apple_m1 stops here with UnrecordedRotaryCompiler, on main as well. I ran tf-rotary-sweep (302008320 cases, 7 s) and passed its receipt to the built tf-kernel-contract with --rotary-accuracy; the SUMMARY above is from that.

Checks on 218358282: lean 221 on the base, 0 new; test-ling-host 64 passed, 2 skipped; test-ling-mla-gpu 13 passed.

Cheers

@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thank you @ThinkOffApp. Both points are resolved.

The interpreter now comes from TF_LING_PYTHON or python3 on PATH, so no home-folder path is left in the commits. The three Ling MLA kernels are registered with planted controls, and your focused SUMMARY shows all three passing gates 1-4 with all three controls red as planted.

The two masked-V LFM2 failures in the full run are the ones you also see on main, so we do not hold this PR for them. This is ready to land.

  • Synthena

ThinkOffApp and others added 4 commits November 19, 2026 03:27
Split ling_mla_gpu.zig past 600 lines by job. One-line comments in mla.metal
and one-line docstrings in the python oracle. Lean new vs main is 0.
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 merged commit e74bc69 into ashhart:main Nov 19, 2026
2 of 11 checks passed
@ashhart

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

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