Skip to content

Commit 0b5ea12

Browse files
justinchubyCopilot
andauthored
Add recurrent Mamba GGUF import (#577)
## Summary - add direct GGUF registry, config, tensor mapping, and transform support for pure `mamba` and `mamba2` - preserve recurrent conv/SSM state I/O, make Mamba1's one-token prompt threading explicit, retain Mamba2 multi-token prefill, and reject KV static-cache mode - validate GGUF conv/A/D/norm transforms, quantized-role dequantization, tied outputs, exact pinned tensor closure, malformed inputs, state evolution, and save/load - explicitly defer `arwkv7`, `rwkv6`, `rwkv6qwen2`, and `rwkv7` because Mobius has no matching RWKV graph ## Runtime status Runtime remains deferred pending independent full-logit parity and deterministic stateful generation on real GGUF payloads. Header-only range audits pinned: - Mamba Q2_K: `QuantFactory/mamba-130m-hf-GGUF@f781792fcca13eb3457a00dc41674715608b02da`, 69,302,464 bytes, SHA-256 `922a9c947f979024fe3675b11a9257637d2226f8953f1831a351890953a5209a` - Mamba2 Q8_0: `rpatel622/mamba2-130m-hf-Q8_0-GGUF@0daf70963405439f2102f6fefe92d7584e2c76eb`, 180,669,152 bytes, SHA-256 `c5d91a0653794a159f19d60159c362b179b506a206be120f8b63f43013155839` ## Validation - 1,268 affected GGUF/Mamba/task tests passed, 2 skipped - Mamba2 synthetic Hugging Face full-logit parity passed for single-token and multi-step state threading - full lintrunner passed - broad non-integration suite: 5,656 passed, 52 skipped; its only 2 failures reproduce independently in untouched `glm_moe_dsa` symbolic shape inference --------- Signed-off-by: Justin Chu <[email protected]> Co-authored-by: Copilot App <[email protected]>
1 parent 89fe423 commit 0b5ea12

18 files changed

Lines changed: 916 additions & 35 deletions

‎docs/api/build_from_gguf.md‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ reason.
146146
| GGUF architecture | Accepted aliases | mobius `model_type` | Float import | Quantized import |
147147
|---|---|---|---|---|
148148
| `arcee` | — | `arcee` | runtime deferred | supported |
149+
| `arwkv7` | — | — | config deferred; tensor_map deferred; graph deferred; runtime deferred | unreachable |
149150
| `bloom` | — | `bloom` | tensor_map deferred | unreachable |
150151
| `clip` | — | — | config rejected; tensor_map rejected; graph rejected; runtime rejected | unreachable |
151152
| `cohere2` | — | `cohere2` | runtime deferred | supported |
@@ -163,7 +164,8 @@ reason.
163164
| `hunyuan-dense` | `hunyuan_v1_dense` | `hunyuan_v1_dense` | supported | supported |
164165
| `internlm2` | — | `internlm2` | supported | rejected |
165166
| `llama` | `mistral` | `llama` | supported | supported |
166-
| `mamba` | — | `mamba` | supported | supported |
167+
| `mamba` | — | `mamba` | runtime deferred | rejected |
168+
| `mamba2` | — | `mamba2` | runtime deferred | rejected |
167169
| `muse-glimmer` | `muse_glimmer` | `muse_glimmer_text` | supported | supported |
168170
| `nemotron` | — | `nemotron` | supported | supported |
169171
| `nemotron_h_moe` | — | — | config rejected; tensor_map rejected; graph rejected; runtime rejected | unreachable |
@@ -178,6 +180,9 @@ reason.
178180
| `qwen35` | — | `qwen3_5_text` | supported | supported |
179181
| `qwen35moe` | — | `qwen3_5_moe` | supported | supported |
180182
| `qwen3moe` | `qwen3_moe` | `qwen3_moe` | runtime deferred | supported |
183+
| `rwkv6` | — | — | config deferred; tensor_map deferred; graph deferred; runtime deferred | unreachable |
184+
| `rwkv6qwen2` | — | — | config deferred; tensor_map deferred; graph deferred; runtime deferred | unreachable |
185+
| `rwkv7` | — | — | config deferred; tensor_map deferred; graph deferred; runtime deferred | unreachable |
181186
| `smollm3` | — | `smollm3` | supported | supported |
182187
| `stablelm` | — | `stablelm` | supported | supported |
183188
| `starcoder2` | — | `starcoder2` | supported | supported |
@@ -190,6 +195,40 @@ validated against a vendored census of the 147 architectures llama.cpp defines
190195
at commit `8d9af256337d1a501250f9bbf4c0859a654bddd6`. Aliases are spellings
191196
llama.cpp does not emit but that mobius still accepts.
192197

198+
### Pure recurrent Mamba evidence
199+
200+
`mamba` and `mamba2` have config, suffix-exact C++ tensor-name closure,
201+
graph-build, save/load,
202+
quantized-source dequantization, and recurrent prompt/decode state-threading
203+
coverage. Mamba1 ingests prompts as single-token recurrent steps; Mamba2 also
204+
supports multi-token prefill. Their runtime verdict remains **deferred**: no pinned real GGUF has
205+
yet passed an independent full-logit comparison plus deterministic multi-token
206+
generation against its source implementation.
207+
208+
The optional Mamba `ssm.dt_b_c_rms` metadata defaults to `false`, matching the
209+
pinned loader. Files that explicitly set it to `true` are rejected because they
210+
require FalconMamba's additional B/C/dt norms, which the pure Mamba graph does
211+
not implement.
212+
213+
The real-file audit was completed from HTTP range metadata before any payload
214+
download:
215+
216+
| Architecture | GGUF revision and file | Size | LFS SHA-256 | Header evidence |
217+
|---|---|---:|---|---|
218+
| Mamba | `QuantFactory/mamba-130m-hf-GGUF@f781792fcca13eb3457a00dc41674715608b02da`<br>`mamba-130m-hf-Q2_K.gguf` | 69,302,464 | `922a9c947f979024fe3675b11a9257637d2226f8953f1831a351890953a5209a` | 242 tensors: Q2_K projections, Q6_K tied embedding, F16 dt projection, F32 conv/state/norm |
219+
| Mamba2 | `rpatel622/mamba2-130m-hf-Q8_0-GGUF@0daf70963405439f2102f6fefe92d7584e2c76eb`<br>`mamba2-130m-hf-q8_0.gguf` | 180,669,152 | `c5d91a0653794a159f19d60159c362b179b506a206be120f8b63f43013155839` | 219 tensors: Q8_0 embedding/output/projections and F32 conv/state/norm; A/D use llama.cpp's `(heads, 1)` layout |
220+
221+
The corresponding source configuration/tokenizer revisions are
222+
`state-spaces/mamba-130m-hf@1e76775f628fbf1350fbe4dbb3d971ba64af25a1`
223+
and `AntonV/mamba2-130m-hf@05e8773fc4ac1cd067e8a18a5c45372ce5178405`.
224+
The header census covers every layer and confirms the pinned llama.cpp tensor
225+
families and qtypes without treating graph construction as runtime parity.
226+
227+
`arwkv7`, `rwkv6`, `rwkv6qwen2`, and `rwkv7` are explicitly deferred because
228+
their time-mixing/channel-mixing recurrence is a distinct architecture and
229+
Mobius has no corresponding graph. They must not be routed through either
230+
Mamba implementation.
231+
193232
GraniteMoE has real-weight import and deterministic ORT execution coverage from
194233
`bartowski/granite-3.0-1b-a400m-instruct-GGUF` revision
195234
`0e1c3cecaa6e49ac0721be91ef441ec72eae62d4`,

‎src/mobius/_configs/_base.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@ class BaseModelConfig:
394394

395395
# Model dtype (from HF config dtype)
396396
dtype: ir.DataType = ir.DataType.FLOAT
397+
quantization: QuantizationConfig | None = None
398+
399+
# HuggingFace identity and token metadata used by package persistence.
400+
model_type: str | None = None
401+
bos_token_id: int | None = None
402+
eos_token_id: int | list[int] | None = None
397403

398404

399405
@dataclasses.dataclass
@@ -2458,6 +2464,7 @@ class Mamba2Config(BaseModelConfig):
24582464
layer_norm_epsilon: float = 1e-5
24592465
use_conv_bias: bool = True
24602466
norm_before_gate: bool = True
2467+
chunk_size: int = 256
24612468

24622469
def __post_init__(self):
24632470
# Mamba2 requires d_inner = num_heads * head_dim.
@@ -2504,6 +2511,7 @@ def from_transformers(cls, config, parent_config=None) -> Mamba2Config:
25042511
layer_norm_epsilon=getattr(config, "layer_norm_epsilon", 1e-5),
25052512
use_conv_bias=getattr(config, "use_conv_bias", True),
25062513
norm_before_gate=getattr(config, "norm_before_gate", True),
2514+
chunk_size=getattr(config, "chunk_size", 256),
25072515
)
25082516

25092517
# Model dtype

‎src/mobius/integrations/gguf/_arch_registry.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@
104104
"weights. Use keep_quantized=False for explicit float import."
105105
)
106106

107+
_RECURRENT_RUNTIME_VALIDATION_PENDING = (
108+
"Config extraction, exact pinned tensor-name closure, GGUF value transforms, and "
109+
"synthetic recurrent-state execution are covered, but no representative real-weight "
110+
"GGUF has yet passed independent full-logit parity and deterministic multi-token "
111+
"stateful ORT generation. Runtime packaging remains deferred until that evidence exists."
112+
)
113+
114+
_NO_RWKV_GRAPH = (
115+
"llama.cpp implements this as a distinct RWKV recurrent architecture. Mobius has no "
116+
"matching RWKV model graph or state-cache contract, so mapping it to Mamba would build "
117+
"the wrong recurrence. Build from a supported source/runtime instead."
118+
)
119+
107120

108121
_SPECS: tuple[GGUFArchitectureSpec, ...] = (
109122
# ---------------------------------------------------------------- Llama
@@ -236,7 +249,38 @@
236249
gguf_arch="mamba",
237250
model_type="mamba",
238251
tensor_map_recipe=("mamba",),
252+
config_key_map="mamba",
253+
config_postprocessor="mamba",
239254
tensor_processor="mamba",
255+
required_metadata=(
256+
"attention.layer_norm_rms_epsilon",
257+
"ssm.conv_kernel",
258+
"ssm.inner_size",
259+
"ssm.state_size",
260+
"ssm.time_step_rank",
261+
),
262+
runtime=Support.DEFERRED,
263+
quantized_import=Support.REJECTED,
264+
reason=_RECURRENT_RUNTIME_VALIDATION_PENDING + " " + _NO_QUANTIZED_PROJECTION_REASON,
265+
),
266+
GGUFArchitectureSpec(
267+
gguf_arch="mamba2",
268+
model_type="mamba2",
269+
tensor_map_recipe=("mamba2",),
270+
config_key_map="mamba",
271+
config_postprocessor="mamba2",
272+
tensor_processor="mamba",
273+
required_metadata=(
274+
"attention.layer_norm_rms_epsilon",
275+
"ssm.conv_kernel",
276+
"ssm.group_count",
277+
"ssm.inner_size",
278+
"ssm.state_size",
279+
"ssm.time_step_rank",
280+
),
281+
runtime=Support.DEFERRED,
282+
quantized_import=Support.REJECTED,
283+
reason=_RECURRENT_RUNTIME_VALIDATION_PENDING + " " + _NO_QUANTIZED_PROJECTION_REASON,
240284
),
241285
GGUFArchitectureSpec(
242286
gguf_arch="starcoder2",
@@ -422,6 +466,38 @@
422466
"decoder-only GGUF tensor mapping models. " + _NO_TENSOR_MAP
423467
),
424468
),
469+
GGUFArchitectureSpec(
470+
gguf_arch="arwkv7",
471+
config=Support.DEFERRED,
472+
tensor_map=Support.DEFERRED,
473+
graph=Support.DEFERRED,
474+
runtime=Support.DEFERRED,
475+
reason=_NO_RWKV_GRAPH,
476+
),
477+
GGUFArchitectureSpec(
478+
gguf_arch="rwkv6",
479+
config=Support.DEFERRED,
480+
tensor_map=Support.DEFERRED,
481+
graph=Support.DEFERRED,
482+
runtime=Support.DEFERRED,
483+
reason=_NO_RWKV_GRAPH,
484+
),
485+
GGUFArchitectureSpec(
486+
gguf_arch="rwkv6qwen2",
487+
config=Support.DEFERRED,
488+
tensor_map=Support.DEFERRED,
489+
graph=Support.DEFERRED,
490+
runtime=Support.DEFERRED,
491+
reason=_NO_RWKV_GRAPH,
492+
),
493+
GGUFArchitectureSpec(
494+
gguf_arch="rwkv7",
495+
config=Support.DEFERRED,
496+
tensor_map=Support.DEFERRED,
497+
graph=Support.DEFERRED,
498+
runtime=Support.DEFERRED,
499+
reason=_NO_RWKV_GRAPH,
500+
),
425501
GGUFArchitectureSpec(
426502
gguf_arch="nemotron_h_moe",
427503
config=Support.REJECTED,

‎src/mobius/integrations/gguf/_arch_registry_test.py‎

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#: Number of importable architectures. Pinned so that adding support is a
5757
#: deliberate act that also updates the documented support matrix, and so that
5858
#: accidentally losing an architecture is a failure rather than a silence.
59-
_EXPECTED_SUPPORTED_COUNT = 32
59+
_EXPECTED_SUPPORTED_COUNT = 33
6060

6161
# Quantized reachability is separately pinned from float importability. A new
6262
# architecture must explicitly prove that its graph exposes packed projection
@@ -77,7 +77,6 @@
7777
"granitemoe",
7878
"hunyuan-dense",
7979
"llama",
80-
"mamba",
8180
"muse-glimmer",
8281
"nemotron",
8382
"olmo",
@@ -162,11 +161,12 @@ def test_every_float_importable_architecture_has_a_quantized_verdict(self) -> No
162161
if spec.is_importable
163162
}
164163
assert set(actual) == set(supported_architectures())
165-
assert actual["internlm2"] is Support.REJECTED
164+
rejected = {"internlm2", "mamba", "mamba2"}
165+
assert all(actual[arch] is Support.REJECTED for arch in rejected)
166166
assert all(
167167
verdict is Support.SUPPORTED
168168
for arch, verdict in actual.items()
169-
if arch != "internlm2"
169+
if arch not in rejected
170170
)
171171

172172

@@ -282,16 +282,23 @@ class TestPinnedTensorClosure:
282282
"qwen2moe",
283283
"qwen3moe",
284284
"granitemoe",
285+
"mamba",
286+
"mamba2",
285287
)
286288

287289
@staticmethod
288290
def _unmapped(architecture: str) -> list[str]:
289291
upstream = upstream_architectures()[architecture]
290292
unmapped = []
291-
for family in upstream.tensor_families:
292-
name = family.replace("{bid}", "0") + ".weight"
293+
names = (
294+
tuple((name, name) for name in upstream.tensor_names)
295+
if upstream.tensor_names
296+
else tuple((family + ".weight", family) for family in upstream.tensor_families)
297+
)
298+
for pinned_name, label in names:
299+
name = pinned_name.replace("{bid}", "0")
293300
if map_gguf_to_hf_names(name, architecture) is None and not is_known_skip(name):
294-
unmapped.append(family)
301+
unmapped.append(label)
295302
return unmapped
296303

297304
@pytest.mark.parametrize("architecture", _NEW_ARCHITECTURES)
@@ -334,6 +341,83 @@ def test_deleting_expert_mapping_breaks_moe_closure(self) -> None:
334341
moe_mapping["blk.{bid}.ffn_gate_exps"] = removed
335342
_build_mapping.cache_clear()
336343

344+
_RECURRENT_TENSOR_NAMES: ClassVar[dict[str, set[str]]] = {
345+
"mamba": {
346+
"token_embd.weight",
347+
"output_norm.weight",
348+
"output.weight",
349+
"blk.{bid}.attn_norm.weight",
350+
"blk.{bid}.ssm_in.weight",
351+
"blk.{bid}.ssm_conv1d.weight",
352+
"blk.{bid}.ssm_conv1d.bias",
353+
"blk.{bid}.ssm_x.weight",
354+
"blk.{bid}.ssm_dt.weight",
355+
"blk.{bid}.ssm_dt.bias",
356+
"blk.{bid}.ssm_a",
357+
"blk.{bid}.ssm_d",
358+
"blk.{bid}.ssm_out.weight",
359+
},
360+
"mamba2": {
361+
"token_embd.weight",
362+
"output_norm.weight",
363+
"output.weight",
364+
"blk.{bid}.attn_norm.weight",
365+
"blk.{bid}.ssm_in.weight",
366+
"blk.{bid}.ssm_conv1d.weight",
367+
"blk.{bid}.ssm_conv1d.bias",
368+
"blk.{bid}.ssm_dt.bias",
369+
"blk.{bid}.ssm_a",
370+
"blk.{bid}.ssm_d",
371+
"blk.{bid}.ssm_norm.weight",
372+
"blk.{bid}.ssm_out.weight",
373+
},
374+
}
375+
376+
@pytest.mark.parametrize("architecture", ["mamba", "mamba2"])
377+
def test_pure_recurrent_pinned_tensor_names_are_suffix_exact(
378+
self, architecture: str
379+
) -> None:
380+
upstream = upstream_architectures()[architecture]
381+
assert set(upstream.tensor_names) == self._RECURRENT_TENSOR_NAMES[architecture]
382+
for pinned_name in upstream.tensor_names:
383+
name = pinned_name.replace("{bid}", "7")
384+
assert map_gguf_to_hf_names(name, architecture) is not None, name
385+
386+
@pytest.mark.parametrize(
387+
("architecture", "malformed"),
388+
[
389+
("mamba", "blk.0.ssm_a.weight"),
390+
("mamba", "blk.0.ssm_d.bias"),
391+
("mamba", "blk.0.ssm_in.bias"),
392+
("mamba2", "blk.0.ssm_dt.weight"),
393+
("mamba2", "blk.0.ssm_a.bias"),
394+
("mamba2", "blk.0.ssm_norm.bias"),
395+
],
396+
)
397+
def test_pure_recurrent_malformed_suffixes_do_not_map(
398+
self, architecture: str, malformed: str
399+
) -> None:
400+
assert map_gguf_to_hf_names(malformed, architecture) is None
401+
402+
@pytest.mark.parametrize(
403+
("architecture", "mapping_key"),
404+
[
405+
("mamba", "blk.{bid}.ssm_x"),
406+
("mamba2", "blk.{bid}.ssm_norm"),
407+
],
408+
)
409+
def test_deleting_recurrent_mapping_breaks_closure(
410+
self, architecture: str, mapping_key: str
411+
) -> None:
412+
mapping = _MAPPING_TABLES[architecture]
413+
removed = mapping.pop(mapping_key)
414+
_build_mapping.cache_clear()
415+
try:
416+
assert any(name.startswith(mapping_key) for name in self._unmapped(architecture))
417+
finally:
418+
mapping[mapping_key] = removed
419+
_build_mapping.cache_clear()
420+
337421

338422
class TestRejectionsAreActionable:
339423
"""An unsupported input must say what it is and what to do instead."""
@@ -360,8 +444,8 @@ def test_a_dead_upstream_architecture_says_so(self) -> None:
360444
get_arch_spec("gptj")
361445

362446
def test_an_unimported_upstream_architecture_names_its_cohort(self) -> None:
363-
with pytest.raises(UnsupportedGGUFArchitectureError, match="C05-pure-recurrent"):
364-
get_arch_spec("mamba2")
447+
with pytest.raises(UnsupportedGGUFArchitectureError, match="distinct RWKV"):
448+
get_arch_spec("rwkv6")
365449

366450
def test_an_unknown_architecture_is_distinguished_from_an_upstream_one(self) -> None:
367451
with pytest.raises(UnsupportedGGUFArchitectureError, match="not among the 147"):

0 commit comments

Comments
 (0)