Repository navigation
Commit 0665542
Fuse GLM native-block routed experts into sparse pkg.nxrt::BlockQuantizedMoE (+ build_from_gguf wiring) (#591)
## Native-block sparse-MoE fusion + `build_from_gguf` integration
Collapses GLM-style **routed** native-block expert storms (a per-expert
`pkg.nxrt::BlockQuantizedMatMul` for every gate/up/down of every expert,
dispatched by an `Equal` one-hot mask) into **one sparse top-k
`pkg.nxrt::BlockQuantizedMoE` per layer**, reusing the existing native
blocks
**byte-for-byte** (no dequantize, no requantize). Gated on graph/tensor
structure, never on model names.
### What's here
1. **Rewrite** (`rewrite_rules/_block_quantized_moe_fusion.py`) — the
transform:
finds the dense-fallback MoE subgraph, validates **all** layers, then
emits.
Two-pass so an unfusable layer fails **atomically** with the graph
untouched.
2. **Builder integration** (`integrations/gguf/_builder.py`) — wires the
fusion
into `build_from_gguf` at the **single** post-export authority point
(step
9b, after `pkg.apply_weights`, once every native block carries real
packed
bytes), then a final-graph-state honesty gate.
### Applicability verdict (Phase-1 audit)
- GLM-5.2 UD-IQ1_S/M routed experts are **per-projection mixed-format**
per
layer (fused `gate==up` in iq1_s/iq1_m; `down` in a higher-bit format
such as
iq4_xs). A v1 single-`format` `BlockQuantizedMoE` therefore fuses **0**
GLM
layers.
- Expressing a mixed-format layer needs the **`block_layout_version=2`
per-projection ABI** (`fc1_format`/`fc2_format`/`fc3_format`). That
runtime
ABI lives on an **unmerged onnx-genai branch**, so mobius's runnable
runtime
does **not** yet have it.
- **Honest consequence:** GLM-5.2 mixed-format layers **typed-reject**
(`SparseMoEExportError`, message names `block_layout_version=2`) until
the
runtime ships v2 — we never emit an unrunnable v2 node by default.
**Uniform**
native-block routed MoE **does** fuse today (v1).
### Honesty / fail-closed policy
- Default `allow_dense_moe=False`. Unsupported layers (mixed w/o v2,
per-expert
bias, ragged/incomplete groups) raise the shared
**`SparseMoEExportError`**
atomically; the graph is left as the original dense storm.
- `_perproj_bqmoe_v2_runtime_available()` gates v2 emission on
`MOBIUS_ENABLE_BQMOE_PERPROJ_V2` (default off). Flip it **only** once
the
onnx-genai v2 runtime ships.
- The **pre-export module-level gate call was removed** (it would reject
the
layers the fusion now collapses); enforcement moved to the **final graph
state** — the fusion's atomic reject + a `_assert_sparse_moe_graph`
backstop
that fails closed on any surviving routed `.experts.`
`BlockQuantizedMatMul`
storm. Single mechanism, no duplicate gate. Batty's
`_assert_sparse_moe_capability`
/ `_routed_dense_block_expert_paths` (#581) stay defined for their unit
tests.
- `allow_dense_moe=True` may warn + retain the dense fallback, but
**never**
counts as a performance path (no throughput claim).
### Emitted ABI (unchanged runtime contract)
`BlockQuantizedMoE` 9 inputs, FLOAT32: `[input, router_logits,
fc1_experts_weights(uint8 4D), fc1_bias?, fc2_experts_weights,
fc2_bias?,
fc3_experts_weights?, fc3_bias?, router_weights?]`. Uniform layer →
`format`
attr (v1). Mixed layer → `block_layout_version=2` +
`fc1_format`/`fc2_format`/
`fc3_format`. Expert banks are the source native blocks **stacked
verbatim**
(byte-preservation asserted in tests).
### Tests (same commits)
- **Rewrite unit** (`_block_quantized_moe_fusion_test.py`, 23):
GLM-style
Sigmoid+TopK+normalize routing, top-k {1,8}, byte-preserving stacking,
atomic fail-closed (dropped/duplicate/ragged/mixed-bias experts),
structural
proof the expert matmuls collapse, and the new **v2-runtime gate**
(uniform
fuses without v2; mixed typed-rejects without v2; mixed emits v2 when
opted in).
- **Builder integration** (`_block_quantized_moe_builder_test.py`, 20):
step-9b
helpers on synthetic exported packages (default fail-closed, v2 opt-in,
dense
retention, byte-preserving banks, deterministic external data,
final-state
backstop) **plus 5 real end-to-end `build_from_gguf` builds** on
synthetic
native-block MoE GGUFs (`qwen3_moe` / `qwen2_moe`, which lower through
the
identical routed dense-loop → `BlockQuantizedMatMul` storm and the same
step-9b
authority point):
- uniform → **one** `BlockQuantizedMoE`, only attention matmuls remain
(no storm);
- mixed → **`SparseMoEExportError`** (GLM-5.2's honest path);
- mixed + `MOBIUS_ENABLE_BQMOE_PERPROJ_V2=1` → one **v2** node;
- `allow_dense_moe=True` → dense storm retained (no fused node);
- **shared expert stays dense** while the routed storm collapses.
Full `rewrite_rules/` + `integrations/gguf/` suites: **576 passed, 1
skipped**.
`ruff check` + `ruff format --check`: clean.
### Remaining typed blockers (not overclaimed)
1. **v2 per-projection runtime ABI is unmerged in onnx-genai** → GLM-5.2
mixed-format typed-rejects end-to-end until it ships. Uniform
native-block
MoE fuses today.
2. **No A100 benchmark yet** — deferred until a runnable shape-faithful
importer path exists (GLM-5.2 can't build to a runnable graph until v2
lands).
Full-model speed is not claimed.
3. **`_builder.py` merge coordination** — Sapper's #578 (unmerged) also
rewrites
`_builder.py`; a future rebase will need coordinator-mediated conflict
resolution. No live collision on this base.
### Dependency on Batty's sharded import
Builds on the merged **#581** (`GgufShardSet`, `glm-dsa` → `glm_moe_dsa`
bridge,
shared `SparseMoEExportError`) and **#576**; rebased cleanly on
`059bf5f`. Only
uses those committed public interfaces — no edits to shard-reader /
architecture-
mapping files.
**Draft — do not merge.** Requesting independent review (excluding
author).
---------
Co-authored-by: Copilot <[email protected]>1 parent cec78d2 commit 0665542
5 files changed
Lines changed: 2156 additions & 7 deletions
File tree
- src/mobius
- integrations/gguf
- rewrite_rules
0 commit comments