Repository navigation
Ling-3.0-tiny MLA on Metal using existing k3_mla (#1247) - #3609
Conversation
|
Thanks @ThinkOffApp for putting Ling-3.0-tiny's MLA layer on the existing Two things only you can do. First,
|
90798a3 to
2183582
Compare
|
Thank you @ashhart for the quick review, and for catching the path! Both are done on the new head The interpreter path. It is gone from the commits themselves: The kernel contract. The full Metal run on this head (Apple M5 Max, built with Two things about that run that you should know:
Checks on Cheers |
|
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.
|
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.
2183582 to
d40fecd
Compare
|
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. |
Wires Ling-3.0-tiny's MLA layers onto Kimi's existing
k3_mlakernels, which are unchanged. Three small Ling kernels sit around them inzig/kernels/metal/ling/mla.metal:rope_cacherotates the 64 rope columns of the cache rowsk3_mla_cachejust wrote,rope_qrotates q's rope part, andgate_expandwidens Ling's per-head gate (T, H) to (T, H*128) on the GPU sok3_mla_uvkeeps Kimi's stride. Nothing is served yet.RoPE follows
ling_forward.pyrot()and HFapply_rotary_pos_emb_interleave: GPT-J pairs, written back as halves, theta 6e6, all 64 rope dims (the checkpoint'spartial_rotary_factor0.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'smla()takes apos0used only for the RoPE check:mla(x, pos0)attends over the rows it is given, so a chunk atpos0 > 0is that chunk as if it were the whole prompt. No test compares a Metal chunk withmla(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_mlais the one-layer entry and reusesModel.mla;--mla-layerrefuses any layer withoutattention.kv_a_proj_with_mqain the checkpoint, so a KDA layer or a missing index is refused.What this changes
077998f7d), merged once; the only overlap waszig/build/kernel_contract.zig(main'stest-llama-controls-hostplus ourtest-ling-mla-gpu).Checks on this head
90798a31e7f1295c75d60e5eb3c7b99da5fc1c7dpython3 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).zig build test-ling-host --summary all: 64 passed, 2 skipped (TF_LING_INDEX unset), 0 failedzig build test-ling-mla-gpu --summary all: 13 passed, 0 skipped, 0 failedzig fmt --check build.zig zig: passReference: mixF64 against
ling_forward.pyling_forward.py --mla-layer 3writes the float64 layer output (TFLINGA1);mixF64plus hosto_projis compared with it.Metal against float64
Gate: Metal's rel L2 and max abs against float64 are each no larger than the bf16 reference's.
TFLINGA1The 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_cacheandgate_expandalone 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.pyfromling_forward.pywhen torch is installed; without torch those tests skip and say so.