Skip to content

core: a GGUF reader and an exact repack of GGUF's quantized blocks (#1920, steps 1-2) - #1926

Merged
ashhart merged 3 commits into
mainfrom
lead/s192-gguf-core
Nov 2, 2026
Merged

ashhart merged 3 commits into
mainfrom
lead/s192-gguf-core

Conversation

@ashhart

@ashhart ashhart commented Nov 2, 2026

Copy link
Copy Markdown
Owner

Steps 1 and 2 of #1920 (native GGUF on Apple Silicon): a GGUF reader in core, and an exact repack of GGUF's quantized blocks into per-group affine weights. No engine reads GGUF yet. Step 3, the Qwen3.8-27B load on Metal, comes next on top of this.

What changes

  • zig/src/core/gguf.zig reads a GGUF v3 file: the header, metadata of all 13 kinds (including nested arrays), tensor infos, alignment and the data offset. A GGUF file is untrusted input, so it checks every length word and count against the file before allocating anything, and caps strings, arrays, metadata and nesting depth. Tensor offsets must be aligned and inside the data region. Shape and byte products are overflow-checked, and overlapping, duplicate and truncated tensors are refused. Unknown kinds and tensor types are refused by name, and the second pass rechecks every range before slicing. It exposes the metadata a family config needs, plus the tokenizer arrays.
  • The repack turns one tensor's blocks into per-group affine codes with an fp32 scale and bias, for Q4_0, Q4_1, Q5_0, Q5_1, Q8_0, Q4_K, Q5_K, Q6_K, Q3_K and Q2_K. Each group's scale and bias is an fp16 times a small integer, which is exact in fp32. So the repacked weights equal the checkpoint's dequantized weights bit for bit.

Checks

  • Host: 53 focused tests. They build malformed files for every refusal, plus every prefix and byte mutation, and include a mutation test for the second-pass range check. The repack is compared with an independent float64 reference, exhaustively per byte for each format. The lean check finds no new problems.
  • Two independent reviews: the reader is LAND (its one change is folded in), and the repack is LAND, matching the block layouts line by line, with 30,000 random blocks compared against the layouts' own loops.
  • Still to do: a check against real tensors from a downloaded GGUF, which is step 4.

Preflight metadata, tensor geometry and byte ranges before allocating indexes. Expose borrowed family and tokenizer views with host corruption and allocation-failure tests.

Refs #1920.
Support ten layouts with FP32 coefficients and an independent FP64 host reference. Exhaust physical byte fields and half encodings, with seeded combinations and production fault controls.

Reject non-finite half fields. Affine cancellation preserves numerical zeros but can change their sign; strict IEEE weight-bit preservation is not claimed.

Refs #1920.
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