Skip to content

Commit 7958f1a

Browse files
justinchubyCopilot
andauthored
Introduce a GGUF support registry and fix five architecture drift bugs (#571)
Foundation for supporting every GGUF architecture and stored quantization type llama.cpp supports. This PR is structure and drift-proofing only — no new architecture cohorts, no sharded GGUF, no new dequantizers. ## Problem GGUF support was asserted implicitly by nine hand-maintained containers spread across five modules, keyed inconsistently on either the GGUF `general.architecture` string or the mobius `model_type`. Nothing cross-checked them, and they had drifted apart: 25 architectures had a config mapping, 28 had a tensor mapping, and the two sets disagreed in both directions. ## Design One immutable source of truth per question, with everything else derived. - **`GGUFArchitectureSpec`** answers four capability questions *independently* — `config`, `tensor_map`, `graph`, `runtime` — instead of one boolean, because they genuinely differ per architecture. Anything short of `SUPPORTED` must carry a reason, so listing an architecture is never itself a support claim. - **`GGUFQuantSpec`** does the same for stored quantization: parse-layer `readable`, `dequantize`, `native_preserve`, `affine_repack`. - Specs reference behavior **by name**, never by callable. The owning module resolves the name. That keeps `_arch_registry` an import leaf and makes both a typo and an *unreferenced implementation* a test failure rather than dead weight. Derived from the registry: `GGUF_ARCH_TO_MODEL_TYPE`, `_ARCH_KEY_MAPS`, `_CONFIG_POSTPROCESSORS`, the five `_*_FAMILY` frozensets and the `_build_mapping` if/elif chain, `LLAMA_QK_PERMUTE_MODEL_TYPES` and the processor table, `_OFFSET_NORM_GGUF_ARCHS`, `_V_HEAD_REORDER_GGUF_ARCHS`, the disabled-architecture guard, the mmproj VLM dispatch, and nine quantization tables across `_repacker` and `_builder`. ## Upstream pin llama.cpp `8d9af256337d1a501250f9bbf4c0859a654bddd6` — 147 real `llm_arch` entries and all 43 `ggml_type` slots, extracted mechanically (block geometry read from a compiled `libggml-base` via `ggml_get_type_traits()`). Vendored as **data**, trimmed to the fields the registry actually reads (198 lines). It exists to make coverage *measurable*, not to claim it: canonical names are validated against it so a mobius `model_type` can no longer masquerade as an architecture, and an unrecognized architecture gets a message naming its upstream cohort. ## Five proven bugs Each was reproduced and root-caused before being touched. 1. **Gemma 3 silent norm corruption.** The weight processor was keyed by `model_type` and registered under `gemma3`, but GGUF `gemma3` resolves to `gemma3_text`, so the Gemma un-offset never ran. `models/gemma3_text.py` uses `OffsetRMSNorm`, so llama.cpp's baked-in `+1` was applied twice on every norm. 2. **Nemotron norm sign inverted.** `_process_nemotron` *added* one while citing HF's `NemotronTensorProcessor`, which subtracts. llama.cpp `conversion/nemotron.py:191` writes `w_gguf = w_hf + 1` and `models/nemotron.py` uses `OffsetLayerNorm`, so the effective scale was `w_hf + 3` — a stored weight of 1.25 scaled by 3.25. Corrected, it is byte-identical to Gemma's, so the two consolidated into one processor. 3. **`qwen2moe`/`qwen3moe` unreachable.** mobius keyed on `qwen2_moe`/`qwen3_moe`, which are mobius `model_type` strings no GGUF carries. A real Qwen-MoE GGUF passed tensor mapping and then failed registry lookup: the reachable keys were dead and the dead keys were reachable. Now canonical, with the old spellings as declared aliases. 4. **`_gguf_arch` dropped by `dataclasses.replace`**, leaving the new registry dispatch dead on every non-float32 and every quantized import. 5. **`_build_mapping` exception base changed** for `clip`, which is reachable via `build_from_gguf` on an mmproj file. `gemma` and `internlm2` previously worked only by an unmapped `.get(arch, arch)` fall-through and are now declared explicitly. `bloom` and `t5` are declared configurable but unmappable, so they fail before config extraction with a reason instead of a contradiction — they never imported successfully. ## Behavior boundaries Behavior-preserving except for the five fixes above. - Tensor mappings are **byte-identical** for all 28 previously mappable architecture spellings, verified by reconstructing the old `if/elif` chain and diffing. - Every replaced quantization literal is pinned verbatim in `_quant_registry_test.py` and asserted equal to the derived table. - Public API unchanged: `__all__` untouched, `mobius.build_from_gguf` and `__main__` untouched, `GGUF_ARCH_TO_MODEL_TYPE` retained (now a derived read-only proxy). - New exceptions subclass the types each path already raised, so existing `except ValueError` / `except NotImplementedError` callers keep working. The tensor-mapping gate still reports every unmapped architecture as a `ValueError`; `_validate_gguf_model` still raises `NotImplementedError` for deliberately disabled ones. - Preserved unchanged: mmproj, MTP sidecar, Tencent Q1_0, native IQ/MXFP4 preservation, mixed-preset requantization, sharded-GGUF rejection. ## Validation - **1083** GGUF tests pass (was 377). - Broad suite: **5473 passed**. Two `glm_moe_dsa` failures are pre-existing, confirmed by stashing onto clean `main`. - `lintrunner` clean. - Every invariant was **mutation-tested** — reverting the Gemma 3 fix, re-introducing a `model_type`-as-architecture, adding an orphan processor, claiming tensor mapping without config, re-inverting the norm sign, and deleting the `_gguf_arch` re-attachment each failed exactly the intended test and nothing else. - `docs/api/build_from_gguf.md` replaces "Most decoder-only LLM architectures are supported" with a support matrix that a test asserts equals the registry. ## Follow-up The `OffsetRMSNorm` `Add(weight, 1.0)` is not constant-folded and ships in the graph. A separate PR will propose folding the offset at weight load on both the HF and GGUF paths; out of scope here. --------- Signed-off-by: Justin Chu <[email protected]> Co-authored-by: Copilot App <[email protected]>
1 parent 0f1fc0c commit 7958f1a

21 files changed

Lines changed: 2931 additions & 394 deletions

‎docs/api/build_from_gguf.md‎

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,83 @@ conversion explicitly.
8484

8585
## Supported GGUF Architectures
8686

87-
The GGUF builder maps GGUF architecture names (e.g. `llama`, `qwen2`,
88-
`gemma`) to the same model classes used by `build()`. Most decoder-only
89-
LLM architectures are supported.
87+
Support is declared once, in
88+
`mobius/integrations/gguf/_arch_registry.py`, as four independent verdicts per
89+
architecture — config extraction, tensor mapping, graph construction, and
90+
runtime packaging. Anything short of "supported" carries a reason, and the
91+
table below is checked against the registry by
92+
`_arch_registry_test.py::TestDocumentedSupportMatrix`, so it cannot drift.
93+
94+
Being listed is not a support claim. `build_from_gguf` refuses every
95+
architecture that is not fully supported, naming the missing capability and the
96+
reason.
97+
98+
<!-- BEGIN GGUF SUPPORT MATRIX (generated; see _arch_registry.py) -->
99+
100+
| GGUF architecture | Accepted aliases | mobius `model_type` | Status |
101+
|---|---|---|---|
102+
| `bloom` | — | `bloom` | tensor_map deferred |
103+
| `clip` | — | — | config rejected; tensor_map rejected; graph rejected; runtime rejected |
104+
| `deci` | — | `llama` | supported |
105+
| `deepseek4` | — | `deepseek_v4` | supported |
106+
| `falcon` | — | `falcon` | supported |
107+
| `gemma` | — | `gemma` | supported |
108+
| `gemma2` | — | `gemma2` | supported |
109+
| `gemma3` | — | `gemma3_text` | supported |
110+
| `gemma4` | — | `gemma4_text` | supported |
111+
| `glm-dsa` | `glm_dsa` | `glm_moe_dsa` | tensor_map deferred |
112+
| `gpt2` | — | `gpt2` | supported |
113+
| `hunyuan-dense` | `hunyuan_v1_dense` | `hunyuan_v1_dense` | supported |
114+
| `internlm2` | — | `internlm2` | supported |
115+
| `llama` | `mistral` | `llama` | supported |
116+
| `mamba` | — | `mamba` | supported |
117+
| `muse-glimmer` | `muse_glimmer` | `muse_glimmer_text` | supported |
118+
| `nemotron` | — | `nemotron` | supported |
119+
| `nemotron_h_moe` | — | — | config rejected; tensor_map rejected; graph rejected; runtime rejected |
120+
| `phi3` | — | `phi3` | supported |
121+
| `qwen2` | — | `qwen2` | supported |
122+
| `qwen2moe` | `qwen2_moe` | `qwen2_moe` | supported |
123+
| `qwen3` | — | `qwen3` | supported |
124+
| `qwen35` | — | `qwen3_5_text` | supported |
125+
| `qwen35moe` | — | `qwen3_5_moe` | supported |
126+
| `qwen3moe` | `qwen3_moe` | `qwen3_moe` | supported |
127+
| `stablelm` | — | `stablelm` | supported |
128+
| `starcoder2` | — | `starcoder2` | supported |
129+
| `t5` | — | `t5` | tensor_map deferred |
130+
131+
<!-- END GGUF SUPPORT MATRIX -->
132+
133+
Canonical names are the strings llama.cpp writes into `general.architecture`,
134+
validated against a vendored census of the 147 architectures llama.cpp defines
135+
at commit `8d9af256337d1a501250f9bbf4c0859a654bddd6`. Aliases are spellings
136+
llama.cpp does not emit but that mobius still accepts.
137+
138+
An architecture outside this table is refused with a message naming its
139+
upstream cohort, so an unsupported input is never mistaken for a broken one.
140+
`clip` files are multimodal projector sidecars: pass them as
141+
`build_from_gguf(text_gguf, mmproj=...)` rather than on their own.
90142

91143
Sharded GGUF files are rejected. A single shard has only part of the tensor
92144
table, and treating it as a complete checkpoint would create a corrupt model.
93145

146+
## Supported stored quantization types
147+
148+
`mobius/integrations/gguf/_quant_registry.py` covers all 43 `ggml_type` slots
149+
at the same pinned commit and answers four separate questions per slot:
150+
whether the GGUF parse layer can read it, whether it can be dequantized to
151+
float, whether its blocks are handed to the runtime unchanged, and whether it
152+
can be repacked into a `MatMulNBits` affine layout.
153+
154+
- **Repacked to `MatMulNBits`**: `Q4_0`, `Q4_1`, `Q8_0`, `Q4_K`, `Q6_K`, `Q1_0`.
155+
- **Preserved byte-for-byte**: `MXFP4`, `IQ4_NL`, `IQ4_XS`, `IQ3_S`, `IQ3_XXS`,
156+
`IQ2_XXS`, `IQ2_XS`, `IQ2_S`, `IQ1_S`, `IQ1_M`.
157+
- **Rejected**: the eight retired slots (`Q4_2`, `Q4_3`, `Q4_0_4_4`,
158+
`Q4_0_4_8`, `Q4_0_8_8`, `IQ4_NL_4_4`, `IQ4_NL_4_8`, `IQ4_NL_8_8`), which have
159+
a block size of 0 and are unreadable at the GGUF parse layer, and `Q8_1` /
160+
`Q8_K`, which are compute-only intermediates that are never valid weight
161+
storage.
162+
163+
94164
## NVIDIA Nemotron 3.5 Lightning waiver
95165

96166
Direct conversion of GGUF architecture `nemotron_h_moe` is intentionally

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ where = ["src"]
6161
[tool.setuptools.package-data]
6262
"mobius.upstream_patches" = ["data/*/*/*.patch", "data/*/*/*.json", "data/*/*/*.md"]
6363
"mobius.integrations.onnx_genai" = ["_schema/*.json"]
64+
"mobius.integrations.gguf._upstream_data" = ["*.json"]
6465

6566
[tool.setuptools.dynamic]
6667
version = { attr = "mobius.__version__" }

0 commit comments

Comments
 (0)