Skip to content

Commit 0c7ac6c

Browse files
justinchubyCopilot
andauthored
Promote first GGUF MoE architecture cohort (#680)
## Summary - promote float GGUF graph routes for `arctic`, `dbrx`, `ernie4_5-moe`, and `nomic-bert-moe` - preserve architecture-specific routing order, normalization, expert layouts, activations, norms, RoPE, shared-expert behavior, and layer schedules - add strict config/tensor/shape closure and CPU graph execution coverage - keep preserved quantized import rejected and runtime support deferred for all four routes - retain normal Hugging Face revision behavior: no production model-ID revision pins, defaults, or rejection gates ## Evidence - Arctic: pinned llama.cpp synthetic architecture fixture - DBRX: pinned 64 MiB bounded header from `dbrx-instruct.i1-IQ1_S.gguf` - ERNIE 4.5 MoE: pinned full `ERNIE-4.5-21B-A3B-Thinking.i1-IQ1_S.gguf` - NomicBERT-MoE: pinned full `nomic-embed-text-v2-moe.Q2_K.gguf` - total transferred payload: 4,816,875,008 bytes, below the 16 GiB cap; payloads were removed after evidence collection ## Validation - broad serial suite: `8662 passed, 64 skipped, 12 deselected` - focused cohort, attention, registry, census, Kimi K3 compatibility, and CPU execution tests pass - `lintrunner f --output oneline --all-files` - generated GGUF documentation check passes - changed-source mypy was run; the repository remains baseline-red in pre-existing diagnostics outside the cohort-added source lines - two independent reviews completed; ERNIE ignored output-bias handling and Arctic stacked-expert loading findings were fixed and re-reviewed --------- Signed-off-by: Justin Chu <[email protected]> Co-authored-by: Copilot App <[email protected]> Copilot-Session: 72a2c1ea-03e1-42a3-8439-c5a0f2e3fde7
1 parent 354b9ff commit 0c7ac6c

18 files changed

Lines changed: 2157 additions & 105 deletions

‎docs/api/build_from_gguf.md‎

Lines changed: 7 additions & 7 deletions
Large diffs are not rendered by default.

‎src/mobius/_configs/_base.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ class ArchitectureConfig(BaseModelConfig):
461461
attn_k_norm_biases: tuple[bool, ...] | None = None
462462
mlp_bias: bool = False
463463
attention_scale: float | None = None
464+
attention_clamp: float | None = None
464465
alibi_max_bias: float | None = None
465466
use_parallel_residual: bool = False
466467
layer_intermediate_sizes: tuple[int, ...] = ()
@@ -515,6 +516,7 @@ class ArchitectureConfig(BaseModelConfig):
515516
scoring_func: str = "softmax"
516517
topk_method: str = "greedy"
517518
first_k_dense_replace: int = 0
519+
moe_layer_frequency: int = 0
518520
n_shared_experts: int | None = None
519521
use_expert_bias: bool | None = None
520522
disable_qmoe: bool = False

‎src/mobius/_registry.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@
5151
from mobius.models import (
5252
ApertusCausalLMModel,
5353
ArceeCausalLMModel,
54+
ArcticGGUFCausalLMModel,
5455
BitNetCausalLMModel,
5556
CausalLMModel,
5657
ChatGLMCausalLMModel,
5758
CodeShellCausalLMModel,
5859
Cosmos3EdgeTextModel,
5960
Cosmos3EdgeVLModel,
6061
Cosmos3OmniReasonerModel,
62+
DbrxGGUFCausalLMModel,
6163
DeepSeekOCR2CausalLMModel,
6264
DeepSeekV3CausalLMModel,
6365
DeepSeekV4CausalLMModel,
@@ -68,6 +70,7 @@
6870
Eagle3DraftModel,
6971
EncDecRNNTModel,
7072
Ernie45MoECausalLMModel,
73+
Ernie45MoEGGUFCausalLMModel,
7174
ErnieCausalLMModel,
7275
ExaOne4CausalLMModel,
7376
Gemma2CausalLMModel,
@@ -168,6 +171,7 @@
168171
JinaBertV2GGUFModel,
169172
NeoBertGGUFModel,
170173
NomicBertGGUFModel,
174+
NomicBertMoEGGUFModel,
171175
)
172176
from mobius.models.gguf_legacy_decoders import ExactLegacyGGUFCausalLMModel
173177
from mobius.models.glm_asr import GlmAsrForConditionalGeneration
@@ -636,8 +640,11 @@ def _detect_fallback_registration(hf_config) -> ModelRegistration | None:
636640
"starcoder2": ModelRegistration(StarCoder2CausalLMModel),
637641
# --- Mixture of Experts ---
638642
"arctic": ModelRegistration(MoECausalLMModel),
643+
"arctic_gguf": ModelRegistration(ArcticGGUFCausalLMModel),
639644
"dbrx": ModelRegistration(MoECausalLMModel),
645+
"dbrx_gguf": ModelRegistration(DbrxGGUFCausalLMModel),
640646
"ernie4_5_moe": ModelRegistration(Ernie45MoECausalLMModel),
647+
"ernie4_5_moe_gguf": ModelRegistration(Ernie45MoEGGUFCausalLMModel),
641648
"bailing_moe": ModelRegistration(Ernie45MoECausalLMModel),
642649
"flex_olmo": ModelRegistration(MoECausalLMModel),
643650
"glm4_moe": ModelRegistration(Glm4MoECausalLMModel),
@@ -881,6 +888,9 @@ def _detect_fallback_registration(hf_config) -> ModelRegistration | None:
881888
"nomic_bert_gguf": ModelRegistration(
882889
NomicBertGGUFModel, task="gguf-encoder-feature-extraction"
883890
),
891+
"nomic_bert_moe_gguf": ModelRegistration(
892+
NomicBertMoEGGUFModel, task="gguf-encoder-feature-extraction"
893+
),
884894
"gemma_embedding_gguf": ModelRegistration(
885895
GemmaEmbeddingGGUFModel, task="gguf-embedding-feature-extraction"
886896
),

‎src/mobius/components/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"DiffusionFFN",
2626
"DiffusionSelfAttention",
2727
"Embedding",
28+
"FusedQKVAttention",
2829
"EncoderAttention",
2930
"EncoderDecoderAttention",
3031
"EncoderLayer",
@@ -107,6 +108,7 @@
107108
from mobius.components._activations import SiLU, get_activation
108109
from mobius.components._attention import (
109110
Attention,
111+
FusedQKVAttention,
110112
GQAContext,
111113
StaticCacheState,
112114
)

‎src/mobius/components/_attention.py‎

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -275,21 +275,7 @@ def __init__(
275275
# Gemma2-style logit soft-capping; 0.0 means disabled.
276276
self._softcap = getattr(config, "attn_logit_softcapping", 0.0) or 0.0
277277

278-
self.q_proj = linear_class(
279-
self.hidden_size,
280-
self.num_attention_heads * self.head_dim,
281-
bias=config.attn_qkv_bias,
282-
)
283-
self.k_proj = linear_class(
284-
self.hidden_size,
285-
self.num_key_value_heads * self.head_dim,
286-
bias=config.attn_qkv_bias,
287-
)
288-
self.v_proj = linear_class(
289-
self.hidden_size,
290-
self.num_key_value_heads * self.head_dim,
291-
bias=config.attn_qkv_bias,
292-
)
278+
self._init_qkv_projections(config, linear_class)
293279
self.o_proj = linear_class(
294280
self.num_attention_heads * self.head_dim,
295281
self.hidden_size,
@@ -314,6 +300,24 @@ def __init__(
314300
self.q_norm = None
315301
self.k_norm = None
316302

303+
def _init_qkv_projections(self, config: ArchitectureConfig, linear_class: type) -> None:
304+
"""Create the default independent query, key, and value projections."""
305+
self.q_proj = linear_class(
306+
self.hidden_size,
307+
self.num_attention_heads * self.head_dim,
308+
bias=config.attn_qkv_bias,
309+
)
310+
self.k_proj = linear_class(
311+
self.hidden_size,
312+
self.num_key_value_heads * self.head_dim,
313+
bias=config.attn_qkv_bias,
314+
)
315+
self.v_proj = linear_class(
316+
self.hidden_size,
317+
self.num_key_value_heads * self.head_dim,
318+
bias=config.attn_qkv_bias,
319+
)
320+
317321
def forward(
318322
self,
319323
op: OpBuilder,
@@ -323,9 +327,7 @@ def forward(
323327
past_key_value: tuple | None = None,
324328
static_cache: StaticCacheState | None = None,
325329
):
326-
query_states = self.q_proj(op, hidden_states)
327-
key_states = self.k_proj(op, hidden_states)
328-
value_states = self.v_proj(op, hidden_states)
330+
query_states, key_states, value_states = self._project_qkv(op, hidden_states)
329331
if not math.isclose(self._key_multiplier, 1.0):
330332
key_states = op.Mul(key_states, self._key_multiplier)
331333

@@ -402,6 +404,16 @@ def forward(
402404
attn_output = self._project_output(op, attn_output)
403405
return attn_output, (present_key, present_value)
404406

407+
def _project_qkv(
408+
self, op: OpBuilder, hidden_states: ir.Value
409+
) -> tuple[ir.Value, ir.Value, ir.Value]:
410+
"""Project hidden states into Q, K, and V tensors."""
411+
return (
412+
self.q_proj(op, hidden_states),
413+
self.k_proj(op, hidden_states),
414+
self.v_proj(op, hidden_states),
415+
)
416+
405417
def _project_output(self, op: OpBuilder, attn_output: ir.Value) -> ir.Value:
406418
"""Apply architecture-specific processing before the output projection."""
407419
return self.o_proj(op, attn_output)
@@ -466,6 +478,51 @@ def _forward_gqa(
466478
return attn_out, (present_key, present_value)
467479

468480

481+
class FusedQKVAttention(Attention):
482+
"""Attention whose checkpoint stores one contiguous Q/K/V projection.
483+
484+
The projection rows are ordered ``[Q | K | V]``. An optional symmetric
485+
activation clamp is applied to the fused output before it is split, so
486+
callers can represent architectures where clipping is part of the fused
487+
projection contract rather than an independent per-projection transform.
488+
"""
489+
490+
def __init__(
491+
self,
492+
config: ArchitectureConfig,
493+
*,
494+
clamp: float | None = None,
495+
linear_class: type | None = None,
496+
):
497+
super().__init__(config, linear_class=linear_class)
498+
self._clamp = clamp
499+
500+
def _init_qkv_projections(self, config: ArchitectureConfig, linear_class: type) -> None:
501+
self._q_width = self.num_attention_heads * self.head_dim
502+
self._kv_width = self.num_key_value_heads * self.head_dim
503+
self.qkv_proj = linear_class(
504+
self.hidden_size,
505+
self._q_width + 2 * self._kv_width,
506+
bias=config.attn_qkv_bias,
507+
)
508+
509+
def _project_qkv(
510+
self, op: OpBuilder, hidden_states: ir.Value
511+
) -> tuple[ir.Value, ir.Value, ir.Value]:
512+
# Keep clipping ahead of the split: this is observably different from
513+
# clipping only Q/K after RoPE or leaving V unclipped.
514+
qkv = self.qkv_proj(op, hidden_states) # (B, S, Q + K + V)
515+
if self._clamp is not None and self._clamp > 0:
516+
limit = op.CastLike(self._clamp, qkv)
517+
qkv = op.Clip(qkv, op.Neg(limit), limit)
518+
return op.Split(
519+
qkv,
520+
[self._q_width, self._kv_width, self._kv_width],
521+
axis=-1,
522+
_outputs=3,
523+
)
524+
525+
469526
class Qwen35Attention(nn.Module):
470527
"""Multi-head attention with output gating for Qwen3.5.
471528

‎src/mobius/components/_attention_test.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
create_test_input,
1515
make_config,
1616
)
17-
from mobius.components._attention import Attention, Qwen35Attention
17+
from mobius.components._attention import Attention, FusedQKVAttention, Qwen35Attention
1818

1919

2020
class TestAttention:
@@ -149,6 +149,33 @@ def test_parameter_count_with_bias(self):
149149
assert len(params) == 8
150150

151151

152+
class TestFusedQKVAttention:
153+
def test_clamps_fused_projection_before_split(self):
154+
config = make_config(
155+
hidden_size=8,
156+
num_attention_heads=2,
157+
num_key_value_heads=1,
158+
head_dim=4,
159+
)
160+
attention = FusedQKVAttention(config, clamp=8.0)
161+
builder, op, graph = create_test_builder()
162+
hidden = create_test_input(builder, "hidden", [1, 2, 8])
163+
164+
query, key, value = attention._project_qkv(op, hidden)
165+
builder._adapt_outputs([query, key, value], "")
166+
167+
split = query.producer()
168+
assert split is key.producer() is value.producer()
169+
assert split.op_type == "Split"
170+
assert split.inputs[0].producer().op_type == "Clip"
171+
assert split.inputs[0].producer().inputs[0].producer().op_type == "MatMul"
172+
assert {name for name, _ in attention.named_parameters()} == {
173+
"qkv_proj.weight",
174+
"o_proj.weight",
175+
}
176+
assert graph.num_nodes() > 0
177+
178+
152179
class TestQwen35Attention:
153180
"""Tests for Qwen3.5 gated attention."""
154181

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

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,6 @@
383383
"JinaBERT v3 uses RoPE and may alternate dense GELU and routed MoE layers. "
384384
"BertModel has absolute positions and no MoE path."
385385
),
386-
"nomic-bert-moe": (
387-
"NomicBERT-MoE alternates dense and routed-expert FFNs according to "
388-
"moe_every_n_layers. Mobius has no encoder MoE graph with that schedule."
389-
),
390386
}
391387

392388
_FINAL_CENSUS_DEFERRED_REASONS = {
@@ -485,11 +481,6 @@
485481
"optional attention-output bias and ERNIE-specific position metadata. Similarity "
486482
"to Qwen/Llama is not a suffix-exact tensor or graph contract."
487483
),
488-
"ernie4_5-moe": (
489-
"ERNIE 4.5 MoE selects periodic expert blocks after a dense prefix, permits an "
490-
"optional gate-expert matrix, and uses normalized routing with optional shared "
491-
"experts. Mobius has no matching per-layer schedule or converter transform."
492-
),
493484
"granite": (
494485
"The granite architecture is a conditional dense-or-MoE union with residual, "
495486
"embedding, attention, and inverse-logit scales, optional biases/RoPE factors, "
@@ -1493,6 +1484,24 @@
14931484
quantized_import=Support.REJECTED,
14941485
reason=_ENCODER_RUNTIME_VALIDATION_PENDING + " " + _NO_QUANTIZED_PROJECTION_REASON,
14951486
),
1487+
GGUFArchitectureSpec(
1488+
gguf_arch="nomic-bert-moe",
1489+
model_type="nomic_bert_moe",
1490+
module_type="nomic_bert_moe_gguf",
1491+
tensor_map_recipe=("nomic_bert", "nomic_bert_moe_extras"),
1492+
config_postprocessor="specialized_encoder",
1493+
required_metadata=(
1494+
"attention.causal",
1495+
"attention.layer_norm_epsilon",
1496+
"moe_every_n_layers",
1497+
"expert_count",
1498+
"expert_used_count",
1499+
"rope.freq_base",
1500+
),
1501+
runtime=Support.DEFERRED,
1502+
quantized_import=Support.REJECTED,
1503+
reason=_ENCODER_RUNTIME_VALIDATION_PENDING + " " + _NO_QUANTIZED_PROJECTION_REASON,
1504+
),
14961505
GGUFArchitectureSpec(
14971506
gguf_arch="jina-bert-v2",
14981507
model_type="bert",
@@ -1697,19 +1706,55 @@
16971706
# ------------------------- Remaining conventional-attention MoE (audited/deferred)
16981707
GGUFArchitectureSpec(
16991708
gguf_arch="arctic",
1700-
config=Support.DEFERRED,
1701-
tensor_map=Support.DEFERRED,
1702-
graph=Support.DEFERRED,
1709+
model_type="arctic",
1710+
module_type="arctic_gguf",
1711+
tensor_map_recipe=("llama", "arctic_extras"),
1712+
config_postprocessor="arctic",
1713+
required_metadata=(
1714+
"attention.layer_norm_rms_epsilon",
1715+
"expert_count",
1716+
"expert_used_count",
1717+
),
1718+
tensor_processor="llama",
1719+
llama_qk_permute=True,
1720+
quantized_import=Support.REJECTED,
17031721
runtime=Support.DEFERRED,
1704-
reason=_ARCTIC_GGUF_GRAPH_REASON,
1722+
reason=_RUNTIME_VALIDATION_PENDING + " " + _NO_QUANTIZED_PROJECTION_REASON,
17051723
),
17061724
GGUFArchitectureSpec(
17071725
gguf_arch="dbrx",
1708-
config=Support.DEFERRED,
1709-
tensor_map=Support.DEFERRED,
1710-
graph=Support.DEFERRED,
1726+
model_type="dbrx",
1727+
module_type="dbrx_gguf",
1728+
tensor_map_recipe=("llama", "dbrx_extras", "moe_extras"),
1729+
config_postprocessor="dbrx",
1730+
required_metadata=(
1731+
"attention.layer_norm_epsilon",
1732+
"attention.clamp_kqv",
1733+
"expert_count",
1734+
"expert_used_count",
1735+
),
1736+
quantized_import=Support.REJECTED,
1737+
runtime=Support.DEFERRED,
1738+
reason=_RUNTIME_VALIDATION_PENDING + " " + _NO_QUANTIZED_PROJECTION_REASON,
1739+
),
1740+
GGUFArchitectureSpec(
1741+
gguf_arch="ernie4_5-moe",
1742+
model_type="ernie4_5_moe",
1743+
module_type="ernie4_5_moe_gguf",
1744+
tensor_map_recipe=("llama", "moe_extras", "ernie45_moe_extras"),
1745+
config_key_map="ernie45_moe",
1746+
config_postprocessor="ernie45_moe",
1747+
required_metadata=(
1748+
"attention.layer_norm_rms_epsilon",
1749+
"expert_count",
1750+
"expert_used_count",
1751+
"expert_feed_forward_length",
1752+
"interleave_moe_layer_step",
1753+
),
1754+
rope_interleave=True,
1755+
quantized_import=Support.REJECTED,
17111756
runtime=Support.DEFERRED,
1712-
reason=_DBRX_GGUF_GRAPH_REASON,
1757+
reason=_RUNTIME_VALIDATION_PENDING + " " + _NO_QUANTIZED_PROJECTION_REASON,
17131758
),
17141759
GGUFArchitectureSpec(
17151760
gguf_arch="gpt-oss",

0 commit comments

Comments
 (0)