Skip to content

llama: the family reads Llama 3's rope, tied heads, Mistral and MLX affine checkpoints, each served once qualified has its row - #3551

Draft
ashhart wants to merge 2 commits into
mainfrom
lead/llama-mainstream
Draft

ashhart wants to merge 2 commits into
mainfrom
lead/llama-mainstream

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

The llama family on Metal admits only the Nanbeige checkpoints. Every mainstream Llama-architecture config is refused when it is read:

  • Llama 3.1 and 3.3 on rope_type: llama3;
  • Llama 3.2 on tie_word_embeddings;
  • Mistral on model_type;
  • MLX conversions on quantization.

This is the code layer that lets the family read them. It adds no served checkpoint: each one is admitted by its own qualified row after its device check, in a follow-up stacked here.

What this changes

  • Llama 3's scaled RoPE (rope_type: llama3): core/rope_llama3.zig builds transformers' inverse frequencies, following its fp32 steps. A host test pins them word for word for Llama 3.1's and 3.2's configs, using values from transformers 5.3.0 run locally. Any other rope type stays refused by name.

  • Tied heads: a tied config's head reads the embedding table. A tied config that also stores lm_head.weight is refused by name.

  • model_type: mistral: admitted when sliding_window is absent or null; Mistral 7B v0.1's window is refused by name. Explicit 128-wide heads under a wider hidden (Mistral NeMo and Small: hidden 5120, query width 4096) keep q, o and the caches a query row wide.

  • MLX affine checkpoints: 4 or 8 bits, in groups of 32 or 64, with bf16 tables, read in place.

    • Windows use the shared row kernels' strided form. Prompt chunks use affine_mm at the row's pitch, with q|k|v and gate|up read where they're packed.
    • The embedding is dequantized at MLX's two rounding points (bf16(bf16(s*q)+b)) by a new kernel, ll_embed_affine. A tied head is a row projection over the embedding's codes, as MLX's as_linear does it.
    • Float16 tables are refused by name. Every mlx-community 4- and 8-bit conversion of these six models stores float16 scales, biases and norms (per the Hub's tensor listing), and turning float16 into bf16 would lose bits. Exact support for those conversions needs float16-table forms of the shared affine kernels. That's a follow-up.
  • Admission: it now compares every field a qualified row shares with the config, so adding a checkpoint is one row. config_test.zig holds the expected rows for Llama 3.1 8B, 3.3 70B, 3.2 3B (bf16 and MLX 4-bit) and Mistral 7B v0.3, NeMo 12B and Small 24B (2501). A test checks each published config parses to its row.

  • Unchanged: Nanbeige4.3-33B and its INT4 release serve exactly as before.

    • The existing tests pass, the unchanged kernels pass the contract, and the bf16 and INT4 scratch sizes are pinned in a new test.
    • On two tiny random checkpoints on Nanbeige's path (bf16 and INT4 g32), base and head gave byte-identical window, prompt last-row and forced-step logits (cmp), and identical 48-token greedy replies.
  • The new paths, on four tiny checkpoints (MLX q4 g64 tied, MLX q8 g32, bf16 tied, and Mistral with Llama 3's band in use):

    • The family's judge against the float64 reference passes on all four: 100/100, 97/100 plus 3 near-ties, 100/100, and 98/100 plus 2 near-ties.
    • One-row steps equal 16- and 7-row windows bit for bit (0 of 40 rows differ).
  • Kernel contract:

  • tools/zig/llama_f64_reference.py reads Llama 3's rope and tied heads. The Nanbeige recipe and docs/kernel-contract.md are updated.

  • Issue: n/a for a new family. This admits mainstream checkpoints into the existing llama family under its existing gate: the float64 judge, the near-tie rule and window exactness from docs/recipes/nanbeige-4.3-33b-metal.md. Each checkpoint's row lands with its own receipt. It came from the 16 Nov check of competitor gap docs against main, where Llama 3.x and Mistral are the most-served models the family refuses.

  • Stacked on: main.

  • atc overlap --json: to run after review.

Checks on this head

  • Head commit: 3e4e6bf on main e2fa334.
  • Lean checker: 337 on base and head, 0 new (tools/pr_check.py: 0 errors, 1 advisory warning, the PR number).
  • Tests:
    • zig build test-llama (new step, macOS): 98/98. On base, every published config is refused: 3.1 and 3.3 UnsupportedLlamaRotary, 3.2 and its MLX copy UnsupportedLlamaGeometry, the three Mistral configs BadLlamaConfig.
    • zig build test-kernel-contract: 205/205. zig build dev-tools: 268/268. zig build native -Dcpu=apple_m1 builds.
    • zig build test-golden and test-cli pass, and fmt and git diff --check are clean.
    • The family's tool tests (Python): 9 OK.
    • zig build test-native: one failure, the qwen27_served reply-end test, which fails on base the same way (test-qwen27-reply-cache-host 3 pass, 1 fail).
  • Contract on an M5 Max (applegpu_g17):
    • tf-kernel-contract --only llama --rotary-accuracy RECEIPT: SUMMARY 38 kernels: 38 pass gates 1-4, 0 fail, 2 known findings; 17 controls: 17 red as planted, 0 missed; 0 limit problems (exit 0). The 2 known findings are main's existing offset_inf record for 4-bit affine_mm (metal_affine.zig), carried onto its new q|k|v registration.
    • The full Metal run's only 2 failures are metal masked-V LFM2 prompt nax inf/nan. The base binary fails them the same way, so they come from main.
    • This Mac's Metal compiler (382.5.3) isn't in the rotary records, so the run takes a rotary accuracy receipt from tf-rotary-sweep (COMPLETE, 302,008,320 cases).
  • CI: pending.
  • Not run: any real Llama 3 or Mistral checkpoint. That's the device qualification below.

Receipt

n/a for this layer: no new checkpoint is served, and Nanbeige's served bytes are unchanged (logits compared byte for byte above).

Each checkpoint's qualification, in the follow-up:

  1. Move its row from expected into qualified.
  2. Add its attention geometry to contracts.geometries and decode_vec's served shapes: (32, 8) for 8B, 7B, NeMo and Small, (64, 8) for 70B, (24, 8) for 3B. Then run the family's contract.
  3. Run the engine's logits and the float64 reference (llama_f64_reference.py) through llama_teacher_judge.py, with HF's bf16 run as the baseline.
  4. Check window exactness at 16 and 7 rows.
  5. Serve it and run bench_openai, bench_concurrent --alone --serial, cold prefill from 2k to 64k, structured output, tools and sampling, and, for Llama 3, contexts past 8,192 where the llama3 band applies.

The prompt path needs Metal 4 tensor units, so it runs on an M5 only. The 70B in bf16 needs a 192-256 GB Mac.

Checklist

  • The title and description match the head commit.

  • 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.

  • No precision traded: float16-table checkpoints are refused rather than converted, and Nanbeige's bytes are unchanged.

  • Synthena (Ash's Agent)

…ffine checkpoints, each served once qualified has its row

Llama 3's scaled rope (rope_type llama3) builds transformers' inverse frequencies, pinned word for word for Llama 3.1's
and 3.2's configs; any other type stays refused by name. A tied config's head reads the embedding table. model_type
mistral is admitted with sliding_window absent or null, and 128-wide heads under a wider hidden keep q, o and the caches
a query row wide. MLX affine checkpoints at 4 or 8 bits in groups of 32 or 64 with bf16 tables are read in place: the
shared row kernels' strided form a window, affine_mm at the row's pitch a prompt chunk, the embedding dequantized as
MLX rounds it, a tied head on the embedding's codes; float16 tables are refused by name.

Admission compares every field a qualified row shares with the config. No row is added: config_test.zig's expected
rows for Llama 3.1 8B, 3.3 70B, 3.2 3B and Mistral 7B v0.3, NeMo and Small 3 wait for their device check.
The GEMM control edits the macro form the GEMM moved to, and llama_f64_reference.py reads Llama 3's rope and tied heads.
…o their own file, keeping metal.zig at 600 lines
@ashhart
ashhart force-pushed the lead/llama-mainstream branch from 3b7ec92 to 3963325 Compare November 22, 2026 07:56

This branch has not been deployed

No deployments
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.

1 participant