Skip to content

Metal: Qwen3.8-27B from a GGUF file, its blocks repacked exactly onto the affine kernels - #1955

Closed
ashhart wants to merge 15 commits into
mainfrom
lead/gguf-27b-metal
Closed

ashhart wants to merge 15 commits into
mainfrom
lead/gguf-27b-metal

Conversation

@ashhart

@ashhart ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Part of #1920. Metal serves Qwen3.8-27B straight from a GGUF file, with the weights the kernels multiply exactly the file's.

What it does

  • Exact repack. Each GGUF block (Q8_0, Q4_0/1, Q5_0/1, Q4_K, Q5_K, Q6_K, Q3_K, Q2_K) is repacked into the engine's affine form, scale × code + bias per group, with FP32 scales and biases. Every product is an fp16 times a small integer, so it's exact in FP32. The reader and repacker are core: a GGUF reader and an exact repack of GGUF's quantized blocks (#1920, steps 1-2) #1926, already on main.
  • Four new affine forms on the 27B's Metal kernels: 4-bit g32, 5-bit g32, 6-bit g16 and 8-bit g32, all with FP32 metadata. They're admitted only for GGUF images, and each is gated against float64 with planted controls.
  • Scalars as stored. The GGUF keeps the 161 norms as f32(1 + w), more precise than MLX's BF16 rounding, and ssm_a as f32(−exp(A_log)). Both are used exactly as stored, and only for an image whose config carries tensorfold_gguf.
  • Value-head order. GGUF stores the GDN value heads permuted (GGUF head j = MLX head (j % 16)·3 + j / 16). One inverse is applied to all eight value-indexed tensors.
  • The tokenizer comes from the file's own metadata, with the qwen35 split rules and the stop set {248046, 248044}.
  • The image is cached once. It's keyed by the file's bytes and every builder's identity.
  • MLX checkpoints keep their admission, kernels and arithmetic.

Checks

  • Host: the full gate passes, 889 of 889 steps, with lean 0 new.
  • Independent reviews: the series was reviewed independently at every step, and the last change came back LAND.
  • The real files, without a GPU (ggml-org/Qwen3.8-27B-GGUF @ a316ea527, Q8_0 and Q4_K_M):
    • all 851 tensors map;
    • all 498 quantized tensors repack exactly;
    • all 353 small tensors are admitted with their stored bits;
    • the tokenizer equals the MLX checkpoint's on 834 varied strings, every vocabulary id, the 33 added tokens and both stop ids.

Not yet run: the device gate

This doesn't land before the device gate: both real files served on Metal, with tokens and speed against the MLX checkpoints. A prepared image is about 26 GB, and the Macs don't have the room yet.

Give the row and lane modules one owner for their metadata type. Keep
native FP32 matrices out of the BF16-only Neural Engine MLP path and
exercise that refusal in the existing family host tests.

Fix the group16 packing test to check the layout packed-byte count.

Refs #1920.
Read actual metadata dtypes from checkpoint headers before admission.
Preserve the original BF16 tensor-unit group refusal, restrict FP32
metadata to the four gated forms, and check direct projection loads too.
Use the declared q4 lane threadgroup bytes in resource records.

Refs #1920.
Resolve local GGUF paths through a content-keyed native image before
the existing Metal startup. Reuse its fusions and projection loader,
keeping FP32 affine metadata and checking scalar transforms.

Adapt the reviewed core block repacker and expose an explicit tokenizer
callback. Keep its named refusal until the metadata tokenizer is linked.
Keep the qualified 27B production geometry.

Exercise synthetic complete images, independent projections, source and
builder cache invalidation, and failed or corrupt publication on the host.

Refs #1920.
Refuse inexact BF16 scalars before repack, retain named metadata errors,
and invert the measured key-fast value-head layout for all affected
Qwen35 tensors without changing packed codes or F32 affine metadata.
Use the shared core exact-narrowing helper for small tensors.
Keep stored norm multipliers unchanged. Test the inverse map, every
small tensor class, packed form, and complete prepared projection.
Keep norm multipliers and negative ssm_a exactly as stored, including value-head permutation. Dispatch F32 gain kernels and an explicit direct-decay mode while retaining the BF16 MLX specialization and legacy A_log expression. Cover exact image bytes, all norm roles, converter one-ulp neighbors, invalid coefficients and storage flags with host regressions.

References #1920.
Use Qwen35's letter-only split and ByteLevel flags. Keep the six FIM and repository added tokens ordinary for decoding while masking all added tokens in request text. Include the end-of-text control in model and generation stop sets, refuse an ordinary end-of-text spelling, and test effective eosIds against an independent generation config. Add a read-only CPU parity runner for supplied text cases and tokenizer assets.

References #1920.
Register input, residual and head norms plus prompt and decode decay paths against independent binary64 formulas, with coefficient controls and launch declarations. Simulate alternative RMS accumulators and state the BF16-output discrimination limit. Document scalar precision, tokenizer flags, stop sets and the CPU comparison runner; GPU qualification remains separate.

References #1920.
Carry the root config marker through Config and Shard into the weight
builder. Admit F32 gains and stored negative ssm_a only for a marked GGUF
image; retain the legacy BF16 gain and A_log path otherwise.

Exercise both refusals on a generated image with its marker removed,
plus valid and malformed markers, rank propagation and dtype admission.
No shader arithmetic changes.

References #1920.
Use the owning core module in standalone Qwen tools so scalar admission does not import safetensors twice. Keep the GGUF marker predicates unchanged.

Refs #1920.
@ashhart

ashhart commented Nov 4, 2026

Copy link
Copy Markdown
Owner Author

Closing as superseded by #2078, which serves the dense Qwen3.8-27B from a GGUF on Metal on top of the core GGUF adapter in #2064. This PR's two pieces that #2078 didn't have, the full stop set and the 834-string tokenizer comparison against the MLX checkpoint, moved over in #2078's second commit, and that comparison passes on both Q4_K_M and Q8_0.

@ashhart ashhart closed this Nov 4, 2026
ashhart added a commit that referenced this pull request Nov 5, 2026
Port the complete control-token stop set and tokenizer comparison from #1955. Check both providers against the same 834 varied strings, vocabulary slots, added-token flags and rendered chat IDs. Keep FIM controls ordinary for decoding and leave unused padding slots unmapped. Document the measured generic projection path under Unreleased.
@ashhart
ashhart deleted the lead/gguf-27b-metal branch November 10, 2026 16:34
ashhart added a commit that referenced this pull request Nov 12, 2026
Port the complete control-token stop set and tokenizer comparison from #1955. Check both providers against the same 834 varied strings, vocabulary slots, added-token flags and rendered chat IDs. Keep FIM controls ordinary for decoding and leave unused padding slots unmapped. Document the measured generic projection path under Unreleased.
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