Repository navigation
llama on Metal: Qwen2 checkpoints, Bimo-7B-CyberAgent's bf16 release with its q, k and v biases and YaRN at HF's rounding points - #2890
Conversation
…ork tag that keeps the measured pin
|
Thanks @BobClawblaw, this is careful work. The receipts are thorough, and the 128-wide YaRN table pinned against transformers' words is exactly what we want. From reading, the existing models keep their bytes:
Main is frozen for the 1.2.7 release, so this lands after the cut.
|
|
Thanks for the read. Taking 2 through 5 now on this branch: separate biased row kernels so the unbiased path Nanbeige serves goes back under the contract exactly as it was, the YaRN table computed once at load, the For 1, I'll rebase onto #2792 and #2787 once they land and move the fp32 output onto the shared GEMM macro then. |
… table once at parse, copy drafts off for Bimo, the stricter config reads (review of ashhart#2890) ll_matvec_bias_* carry the bias as a fifth buffer; ll_matvec_* are main's again and the contract probes them at Nanbeige's shapes as before, the biased forms at Bimo's. A control rounds the sum before the bias joins it. Config computes YaRN's factor once at parse, refuses a root rope_theta that disagrees with rope_parameters' and any layer_types entry that is not full_attention, and reads a null rope_parameters as absent. Qualified's copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent (measured 6% slower). The --rope-yarn refusal names where YaRN comes from.
|
Pushed as 2, and the no-bias path. 3. 4. A control 5. The Checks on 1 stays for #2792 and #2787: once they land I rebase, drop the biased row kernels onto the shared matvec source and put the fp32 output on the shared GEMM macro. |
3c74a3c to
385ed08
Compare
|
1, rebased onto #2792 and #2787. Pushed as
Shared GEMM A/B. Every source built on A control on main that matches nothing. Main's Llama Checks (M5 Ultra; run on the same commits on main
|
|
Thanks @BobClawblaw. Everything we asked is in:
Thanks also for spotting that main's
|
385ed08 to
b1c9948
Compare
|
@BobClawblaw, rebased onto today's main and force-pushed (
|
|
Hi @BobClawblaw, a heads-up so our two changes don't step on each other: #3551 (a draft) teaches this family to read Llama 3's scaled rope, tied heads, What should make your rebase smaller:
This PR also conflicts with main on its own, in
|
|
Thanks for the heads-up. We'll do the rebase onto #3551 ourselves: bias and YaRN become fields on our qualified rows, and YaRN joins your rope reader as a third type. We'll push once the kernel contract and the logit A/Bs pass on the M5 Ultra. |
|
Thanks @BobClawblaw. We have noted your planned restack and M5 checks. We will coordinate the integration with #3551 and preserve current Qwen3 embedding support, including the public norm helper and Q/K norm fields. The changelog and routine landing fixes are ours; there is no extra request while you prepare that push.
|
|
A heads-up from the M5 checks on the restack: our On the restack (main + #3551 + this PR), seeds 1 to 20 of The cause is in the test, not the kernel. The bound assumes |cos| <= 1, but YaRN's mscale (about 1.14 for Bimo) lifts cos and sin past 1, where a bf16 step is coarsest. The fix is test-only ( The rest of the restack's M5 checks also pass. Nanbeige 3B and 33B logits are byte-identical to main. Qwen3-Embedding-0.6B passes 10/10 against the FP32 reference on both main and the restack, with byte-identical vectors. The contract reads: (The two known findings are #3551's MLX The fix is folded into the family commit of our restack, which we'll push once #3551 lands. If you would rather land this PR first, I can push the fix as a commit on the current head instead. Which do you prefer? |
|
Pushed the fix as 062f46b, one commit on top of b1c9948. It changes only the contract test and adds the bound's paragraph to the recipe doc. Measured on this PR's code: unfixed, 17 of 20 seeds fail The restack onto #3551 carries the same fix in its family commit. |
|
Thank you @BobClawblaw for finding the bound flaw and for pushing the test-only fix. Widening the ll_prep allowance for a cos or sin one bf16 step off is the right fix, and your 20-seed run keeps the planted control red with Bimo's outputs unchanged. There is nothing further we need from you here. We will carry the integration with #3551 on our side and then run the llama contract and the byte-for-byte checks on our M5.
|
062f46b to
17e7948
Compare
|
Thanks @BobClawblaw. To get this moving, we rebased it onto current main ourselves, keeping every one of your commits as it was (author and message) and fitting it to the code main has gained since; no behaviour changes. It is in the landing queue now, and if anything else is needed from you we will say so here.
|
17e7948 to
b69e82c
Compare
|
On head On that binary, Igor Lessio - AIFLOW LABS |
… storing fp32 sums tfp::gemm_nax takes D's element type as a last template parameter, T by default, so every existing kernel instantiates as before. TF_GEMM_NAX_KERNEL_TO(NAME, T, TD, ...) is TF_GEMM_NAX_KERNEL with D stored as TD, for a caller that keeps the fp32 sums for an epilogue of its own; TF_GEMM_NAX_KERNEL's text is unchanged.
…with its q, k and v biases and YaRN at HF's rounding points The family admits model_type qwen2 for the checkpoints checked against Hugging Face: the q, k and v biases join the projections' fp32 sums before their one rounding (ll_matvec_bias_* beside the shared ll_matvec_*, which stay core/bf16_matvec.metal's; a prompt chunk's GEMM stores its sums through ll_gemm_f32_*, gemm_nax with fp32 output, and ll_bias_round adds the bias and rounds once), and the rope carries transformers' static YaRN table for 128-wide heads (rope_yarn.yarnOf; the 64-wide table unchanged), computed once at parse, with its attention factor on the bf16 cosines and sines. Config refuses a root rope_theta that disagrees with rope_parameters', a layer_types entry that is not full_attention, and reads a null rope_parameters as absent. Every changed and new kernel is under the contract with five more planted controls; the controls' source edits now name the shared matvec's loop and the GEMM macro lines (ashhart#2787 left gemm_untransposed's edit matching nothing). --drafter is refused by name for a checkpoint no drafter was checked with, Qualified.copy_drafts turns the lane core's copies off for Bimo-7B-CyberAgent, and the template's lack of a think block is declared to the server. tools/zig/llama_f64_reference.py takes the Qwen2 layout.
…and serving checks on the M5 Ultra, its copy-draft measurement and the quality suite's thinking-off results)
Metal's precise::cos and sin are not correctly rounded, and YaRN's factor lifts them past 1, where a bf16 step is coarsest, so a rotation output can move two ulps. The bound now adds, per output, the widest move a neighbouring bf16 cosine or sine gives. Without it 17 of 20 seeds failed the shapes gate at Bimo's geometry by up to 1.25 of the bound; with it seeds 1 to 20 pass and the factor-skipped control stays red. The kernel is unchanged.
b69e82c to
8521943
Compare
|
Thanks @BobClawblaw, the Qwen2 and YaRN work is careful and the recipe is clear. One thing before this can land: the
|
Main's ricursive set calls dense.Matvec.init and dense.Gemm.init without the biased and wide arguments this branch adds, and retargets a Gemm with two served shapes where Bimo's gives it three, so tf-kernel-contract (and zig build dev-tools) did not compile. Ricursive runs the bf16 forms; a retargeted Gemm registers its two shapes.
|
Thanks. Here is the measurement you asked for, then the math the bound follows from, then what the seeds were. What is compared.
(ulp = fp32 ulps of the float64 value.) The exhaustive row covers any position times any inverse frequency up to 2^19, Nanbeige 4.3's 524,288 window, the longest among the family's geometries. Angles below 2^-126 are left out because the GPU flushes them to zero, and Why one step is the most. Metal's value is within 3.36 fp32 ulps of the true cosine or sine. Each path then rounds once more to fp32: the reference when it stores That is what the added term models. For each output, The seeds. Seeds 1 to 20 with the old bound, on the rebased head 8521943 (with the build fix below):
With the bound, seeds 1 to 20 all pass. A build break from the rebase. On 8521943,
The prep control that drops YaRN's attention factor stays red (5.68 of the bound) with the added term. The sweep (Swift;
|
|
Thanks @BobClawblaw. The sweep and explanation support the one-neighbor argument on the measured setup, and the new limit follows each output's possible rotation change. The Ricursive constructor and shape fix is also present. The remaining decision is ours: the allowance permits Q/K values that differ from the reference, including existing Llama cases, and does not guarantee equal generated tokens. That tolerance needs the owner's approval before landing. The reported serial/window equality remains useful evidence of a separate property.
|
|
A decision on the remaining question, @BobClawblaw: we cannot accept the cos/sin allowance. TensorFold keeps every output bit equal to the serial reference, and the bound lets rotated Q/K values differ from it, including on existing Llama paths that are bit-exact today. So any change to the existing rotation has to stay bit-identical there. The Qwen2 and YaRN support itself is welcome. If it can be built without changing the existing paths, for example by computing the new rotations with the same arithmetic the reference uses, or by keeping the new code path separate from today's exact one, we will happily look at it again. Thank you for the careful sweep; it made the trade-off clear.
|
Bimo-7B-CyberAgent's cosines and sines carry YaRN's attention factor, and Metal's precise::cos and sin are not correctly rounded, so the scaled ll_prep needed a wider contract allowance. Instead, ll_prep is main's kernel again (byte for byte, main's bound and shapes), and a YaRN checkpoint's rotation runs ll_prep_table: the same rotation reading bf16 cos | sin rows that llama/rope.zig makes once at load at HF's rounding points (8 MiB over 32,768 positions, counted in the load budget). The table equals transformers 5.22.2's Qwen2RotaryEmbedding (torch 2.15.0) in all but 19 of its 4,194,304 words. At those 19 the scaled value is on a bf16 tie and torch's fp32 cos or sin is an ulp off the correctly rounded one; a test pins them and, with them patched in, the table's hash. ll_prep_table's contract bound is zero: it reads the reference's own table. Two controls: the table read at the window's first position, and cos and sin swapped. The factor-skipped control and the bf16-step allowance go with the scaled ll_prep.
The forced-token counts, greedy and exact checks, needles and the harness's quick, speed and prefill-32k plans rerun on the table build; the quality suite's figures are labelled with the engine they were measured on. The tools auto case is described per template as each run showed it.
|
Thanks, that's clear. I've reworked it the second way you suggested: the existing rotation is untouched, and YaRN gets its own path. What changed (19382da, on top of 7d1922b)
One thing to flag about the table. Against transformers 5.22.2's Checks on an M5 Ultra
Served, on the same build. The recipe harness's
A 13,090-token second turn resumed from 13,062 kept tokens gives the same reply as a freshly started server. The passphrase needles come back as before at 4k–20k tokens and at 90% of a 27,633-token prompt. The recipe doc is updated to match (5ba960b): its forced-token, served and speed figures are from this build, and the quality-suite and copy-draft figures name the earlier engine they were measured on. |
|
Thanks @BobClawblaw. The existing rotation kernel is restored exactly, and the rejected allowance is gone. Separating YaRN and checking its rotation with a zero bound is a useful improvement. The remaining issue is the table itself: runtime keeps the 19 words that differ from the stated HF reference, while only the test buffer is patched. Please make the runtime table match that reference and check its full hash without patching. The reported near-tie token differences also cannot count as exact equality. The current-main integration remains ours.
|
model_type: qwen2on MetalBadLlamaConfig)UnsupportedLlamaGeometry)ll_matvec_bias_*(a llama template on the sharedtf_bf16_mv_args, the bias a fifth buffer) beside the unchangedll_matvec_*(TF_BF16_MATVECS(ll)fromcore/bf16_matvec.metal), and for prompt chunksll_gemm_f32_*(the shared GEMM storing fp32 sums throughTF_GEMM_NAX_KERNEL_TO) plusll_bias_roundUnsupportedLlamaRotary)rope_yarn.yarnOffor 128-wide heads (the 64-wide table unchanged, word for word); the attention factor on the bf16 cosines and sines (ll_prep'smscale, 1 for the default rope)3cacb821c's (tf-llama-run logits, 75-token prompt, 25,036,800 and 24,921,600 bytes equal)What this changes
The reference is
modeling_qwen2in bf16:modeling_llama's rounding points plusF.linear's bias in the fp32 sum and transformers' static YaRN (_compute_yarn_parameters, beta 32/1, truncated ends, 0.1 ln(factor) + 1 on cos and sin before their bf16 cast). The config readsrope_parameters(transformers 5,rope_thetainside it) andrope_scaling; a YaRN config naming any other key, or a context that is not factor × trained window, is refused.Qualifiedgainsbias,yarn,drafts(pays / slower / unchecked:--drafterrefused by name for a checkpoint no drafter was checked with),copy_drafts(off for this checkpoint: 1.05 tokens a round and 6% slower on the M5 Ultra) andreasoning(the template has no think block, declared to the server). The YaRN table is built once at parse (rope_scale); a rootrope_thetathat disagrees withrope_parameters' is refused, a nullrope_parametersis absent, andlayer_typesmust be allfull_attention.tools/zig/llama_f64_reference.pytakes the Qwen2 layout. Doc:docs/recipes/bimo-7b-cyberagent-metal.md.Core touched:
zig/src/core/rope_yarn.zig(yarnOf(comptime dims, ...),yarn=yarnOf(64, ...), the Qwen3.6 words pinned as before) andzig/src/tensorfold.zig(exportsrope_yarn);zig/src/native/metal.zigroutesqwen2to the llama host.Shared kernels (own commit):
prefill/gemm_nax.metal'sgemm_naxtakes a last template parameterTD = T, the typeDis stored as, andprefill/gemm_nax_kernel.metaladdsTF_GEMM_NAX_KERNEL_TO(NAME, T, TD, TA, TB, AM, AN); every existing caller keepsTDat its default.core/bf16_matvec.metalis unchanged. A/B: each source built ongemm_nax(prefillgemm_nax, Gemma4 vision, GLM vision, core Qwen vision, Step5 vision, Spark2.5 GEMM, Llama GEMM), concatenated as the runtime does, compiled from main and from this branch to optimized IR (xcrun metal -std=metal4.1 -O2 -S -emit-llvm): every existing kernel's IR is identical function by function, differing only ingemm_nax's mangled name and the source path; the branch's Llama source without its four_TOlines differs from main's only in the path line.Main's Llama control
gemm_untransposedhas matched no text since #2787 movedllama/gemm.metalonto the macro; here it names theTF_GEMM_NAX_KERNEL(ll_gemm_00, ...)line, and the controls on moved code name the shared matvec's loop and the macro lines (each must match exactly once).Checks on this head
385ed08f3(shared GEMM6d7138a27, familye07329b66, docs385ed08f3), on maineab305db0. The checks below ran on the same three commits on main3cacb821c(aa4533c03); the rebase ontoeab305db0changes only the changelog line's place (git range-diff), and none of the 11 commits between touch Metal kernels or the llama family.native/metal.zig:334, main's ownTF_FLASHNEXT_DUMPgetenv (line 333 on main).zig build test-golden,test-kernel-contract,check-release-paths,dev-tools: pass.zig build test: 12993 of 13102 pass, 108 skipped, 1 failed:dashboard_test, which looks for "Paged prefix cache" indashboard.html; main's page lacks that text too (3cacb821candeab305db0), and this branch touches neither file.zig build,zig fmt --check,git diff --checkon this head: pass.tf-kernel-contract --only llamaon an M5 Ultra:SUMMARY 40 kernels: 40 pass gates 1-4, 0 fail, 0 known findings; 19 controls: 19 red as planted, 0 missed; 0 limit problems; gates 694901 ms, limits 263 msReceipt (M5 Ultra, 256 GB)
llama_f64_reference.py) over 192 teacher-forced positions on three chats: engine 190 equal, 2 near-ties (policy pass); HF's own bf16 run (transformers 5.22.2, CPU) 189 and 3. Engine against HF's run: 191 and 1.tf-llama-run exact).tools; with them described in the system message the<tool_call>JSON is parsed; theautocase gets a fabricated<tool_response>, the model's habit). One stream 44-56 tok/s, two streams 89-113 together, 19 ms a round; cold prompts 5.7-6.2k tok/s to 17.5k tokens. Copy drafts land 1.05 tokens a round here (the recipe serves--no-drafts).evidence/).atc overlap --json(2026-11-11, before the rebase ontoeab305db0; coverage incomplete:otherPullRequestsLimit,pullFilesLimit): conflicts with native: Qwen3.5-2B on CUDA attests the execution witness's first path, every weight byte read once the server is ready #2956, train 1111234240: #2579 #2816 #2794 (CI only) #2959, dsv41 metal: a multi-stream round's per-slot stages run as one dispatch each (stacked on #1720) #2954, dsv41 metal: Engram rows are warmed ahead and joined at their layer's command buffer (stacked on #1720) #2953, DeepSeek-V4.1 on Metal: opt-in draft cost table timed at start, default pricing unchanged (stacked on #2983) #2952, dsv41 metal: TF_DSV41_ROUND_PROF times each decode round and the drafter's passes (stacked on #1720) #2951, dsv41 metal: prompts in pieces between the other streams' rounds (stacked on #1720) #2950, server: --execution-witness serves a read-scoped snapshot of what runs, or why it is unknown #2901, server: "execution_witness": true binds a reply to the witness at admission and at its end, with its prompt, resolved request and tokens #2958 and Vulkan: one submission per batch of dispatches, 30% to 56% less run time on an M1 with the same logits (stacked on #2015) #2016 inCHANGELOG.mdonly (neighbouring Unreleased lines). An earlier run on 2026-11-10 also met ricursive: Ripple-65B-Preview on native CUDA (NVFP4) #2302 (Ripple on CUDA) indocs/recipes/README.md,zig/src/native/metal.zigandzig/tests/contract/metal_llama_attention.zig, neighbouring lines in the recipe index, the model-type table and the served-shape lists.