Skip to content

fix(models): split dots3_note routed experts through the loader's Defuser pass - #6448

Merged
Qubitium merged 1 commit into
mainfrom
fix/dots3-note-split-experts
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/dots3-note-split-experts

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Dots3NoteQModel.before_model_load swapped modeling_dots3_note.Dots3NoteTextExperts for a per-expert ModuleList adapter. The vendored port's Dots3NoteTextMoE builds Dots3NoteExperts, a different class, so the swap never took effect. The routed experts stayed fused as 3-D gate_up_proj / down_proj parameters, and the declared mlp.experts.#.{gate,up,down}_proj specs matched nothing. Because of layer_modules_strict = False this was silent. Quantizing a dots3_note checkpoint then failed with:

RuntimeError: Lazy forward direct-state materialization left META tensors under `mlp.experts`.
RuntimeError: Offload: modules remain on the meta device after quantization (model.layers.1.mlp.experts).

The hook also assigned onto the port module and onto Dots3NotePreTrainedModel._init_weights at load time. AGENTS.md forbids that ("never assigns onto a model class at load time").

The fix uses the existing declarative mechanism instead. The other native fused-expert definitions that have no Defuser registry entry (kimi_linear, mimo_v2_flash, gemma4) set defuser_generic_experts = True, and the loader's _convert_model_with_defuser then splits the experts.

What Changed

  • gptqmodel/models/definitions/dots3_note.py: declares defuser_generic_experts = True. Removes _SeparateDots3Experts and before_model_load, so no port-class assignment happens at load time. module_tree, after_model_load (router recovery binding) and zero_route_recovery_context are unchanged.
  • tests/module_tree/test_dots3_note_fork_plan.py: two new regression tests (see below).
  • docs/dots3-separate-experts.md: the reference page now describes the shipped mechanism; the removed adapter and hook are dropped from the diagram.
  • Behavior change: only for dots3_note. Its routed experts are now real per-expert nn.Linear modules under the checkpoint names (model.layers.N.mlp.experts.K.{gate,up,down}_proj). They run through Defuser's generic linear_loop forward, the same one kimi_linear and mimo_v2_flash use.
  • Out of scope:
    • The dots3_note AWQ scale-group crash on sliding-attention layers is being fixed separately.
    • GPTQModel.load of a saved dots3_note checkpoint fails in load_hf_tokenizer on main (see Notes).

Tests

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

The new tests build the tiny vendored-config model on CPU and follow the loader's order: _call_before_model_load, then construction, then _convert_model_with_defuser.

  • test_dots3_note_routed_experts_split_into_declared_linears checks four things:
    • no 3-D expert parameters remain;
    • every mlp.experts.<i>.{gate,up,down}_proj on each sparse layer is an nn.Linear;
    • each of those modules is found by find_modules, declared by simple_layer_modules(), and quantized according to ModulePlan.find();
    • the split forward matches the fused forward.
  • test_dots3_note_load_hook_leaves_port_classes_untouched checks that the load hook leaves vars() of the port module and of Dots3NotePreTrainedModel unchanged.
export HF_HOME=/root/.cache/huggingface CUDA_VISIBLE_DEVICES=
# origin/main (ef7af83db) with the new test file copied in
python -P -m pytest -q tests/module_tree/test_dots3_note_fork_plan.py
E  AssertionError: assert [{'__name__':...}] == [...]          # hook mutated modeling_dots3_note / Dots3NotePreTrainedModel
E  AssertionError: routed experts were not split: ['model.layers.1.mlp.experts.gate_up_proj', 'model.layers.1.mlp.experts.down_proj']
2 failed, 1 passed

# this branch
python -P -m pytest -q tests/module_tree/test_dots3_note_fork_plan.py
3 passed

python -P -m pytest -q tests/test_native_definition_loading_0.py -k dots3
1 passed, 31 deselected
python -P -m pytest -q -rs tests/test_native_auto_registry.py tests/module_tree/test_registered_module_plans.py \
  tests/test_model_port_policy.py tests/test_transformers_unpatched.py tests/module_tree/test_dots3_note_fork_plan.py \
  tests/module_tree/test_fusable_models.py
553 passed, 460 skipped   # all 460 skips: test_fusable_models.py:262 "Native Marlin qualification requires NVIDIA CUDA hardware" (run CPU-only; dots3_note has no fusion profile)

# opt-in meta sweep
python -P -m pytest -q -rs tests/module_tree/test_moe_expert_coverage.py -m meta_coverage -k dots3
main:   SKIPPED "dots3_note: installed defuser 0.0.33 leaves 90 fused 3-D expert projection(s) unsplit"   # the sweep hid this bug as a version skip
branch: 1 passed

bash format/check.sh   # ruff 0.14.2: All checks passed!

Measured Impact

Measured on CPU with a tiny random-init dots3_note built from tests/fixtures/ports/native_parity/dots3_note.json (layer 0 dense, layer 1 MoE with 4 routed experts, SmolLM2 tokenizer vocab).

Check main branch
GPTQModel.load(..., QuantizeConfig(bits=4, group_size=16), device="cpu").quantize(...) RuntimeError: META tensors left under mlp.experts completes; save() writes 12 per-expert mlp.experts.{0..3}.{gate,up,down}_proj.qweight, no fused expert keys; in-session quantized logits are finite
Fused vs split logits (fp32, 4x33 tokens) not split (diff 0, nothing converted) max abs diff 1.19e-07, relative Frobenius diff 5.0e-08, argmax identical

The split forward differs from the fused forward only by fp32 rounding. Defuser's generic linear_loop adds up the top-k slots, while the fused loop accumulates per expert with index_add_. The fused path runs grouped_mm, which rejects fp64, so an fp64 comparison is not possible.

No shared code is touched; only the dots3_note definition changes. Its module_tree, so its registry plan fixture, is unchanged.

Model Support / Module Plan

  • Every new MODEL_MAP entry has an explicit registry plan case. (No new entry; the existing Dots3NoteQModel/gptq case passes unchanged.)
  • A new model-specific module-plan unit test builds the real model class and verifies declared paths, checkpoint weight ownership/tags, roles, and applicable consumers, including layout variants and method overrides. (The test covers the dense and sparse layers of the tiny model, and the routed projections against find_modules, simple_layer_modules and ModulePlan.find.)
  • Runtime-created tensors are distinguished from checkpoint-owned weights. (The per-expert linears carry the checkpoint key names; the e2e save writes them as experts.<i>.<proj> keys.)
  • Fusion eligibility is recorded in fusable_models.json: unchanged, an existing non-applicability reason.
  • Fusion modules/parents resolve through model.module_plan(): not applicable, because no fusion changes.
  • Composed fusion configs: not applicable, because no fusion changes.
  • New fusion qualifications: not applicable, because no fusion changes.
  • Registry and model-specific tests passed; skips are listed above.

Model test node IDs and results:

  • tests/module_tree/test_dots3_note_fork_plan.py::test_dots3_note_owns_declared_routed_projection PASSED
  • tests/module_tree/test_dots3_note_fork_plan.py::test_dots3_note_load_hook_leaves_port_classes_untouched PASSED (FAILED on main)
  • tests/module_tree/test_dots3_note_fork_plan.py::test_dots3_note_routed_experts_split_into_declared_linears PASSED (FAILED on main)
  • tests/test_native_definition_loading_0.py::test_definition_loader_builds_vendored_classes[dots3_note] PASSED
  • tests/module_tree/test_moe_expert_coverage.py::test_every_defined_model_type_resolves_routed_experts[dots3_note] PASSED (SKIPPED on main)

AWQ onboarding safety

  • Native forward evidence: not provided.
  • Neutral-fold and candidate-search tests: not provided.
  • Context/chunk parity and fallback tests: not provided.
  • Per-variant AWQ policy: dots3_note remains AWQ-unqualified.

AWQ evidence and per-variant policy: not applicable, with one caveat. This PR makes no AWQ grouping, search, replay or fold changes, and module_tree is unchanged. It does make the routed experts reachable, so AWQ now sees them. AWQ on dots3_note already crashes on main with _AWQScaleGroupError ... kv_a_proj_with_mqa has multiple scaling groups on sliding-attention layers, and that is being fixed in a separate PR. Only GPTQ was validated end-to-end here.

Logic Flow Documentation

  • This change replaces a decision mechanism: a load-time class swap becomes the declared defuser_generic_experts metadata. The existing reference page docs/dots3-separate-experts.md (linked from docs/proposed-feature-reference-pages.md) is updated.
  • The diagram uses real symbol names.
  • Removed mechanism: gptqmodel/models/definitions/dots3_note.py:14 _SeparateDots3Experts and :83 before_model_load on main, both deleted.
  • Failure paths: after_model_load still raises RuntimeError on a router-count mismatch.
  • README link: the page is linked from docs/proposed-feature-reference-pages.md, as before; no new page was added.

Diagram summary: the existing page now shows Dots3NoteQModel without the removed adapter and hook.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible. (This removes a load-time port-class patch.)
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Not applicable: no kernel, dispatch or packing change.
  • Not applicable: no fusion change.
  • Not applicable: no kernel inventory change.

Notes

  • Not verified: a real Dots3 Note checkpoint (only the tiny vendored-config model was tested), EXL3 quantization (the zero-route recovery reads expert names, which the split model now exposes), and GPU runs.
  • Pre-existing bug, not fixed here: GPTQModel.load() of a saved dots3_note checkpoint fails before the model loads. The error is AttributeError: 'dict' object has no attribute 'bos_token', raised from load_hf_tokenizer -> Tokenicer.auto_fix_model_config. It reproduces on main with an all-dense tiny dots3_note, so it is independent of this change. Because of it, quantized reload could not be exercised.

🤖 Generated with Claude Code

…user pass

Dots3NoteQModel.before_model_load replaced
`modeling_dots3_note.Dots3NoteTextExperts` with a per-expert ModuleList
adapter, but the vendored port's Dots3NoteTextMoE builds
`Dots3NoteExperts`. The swap therefore never took effect: routed experts
stayed fused as 3-D `gate_up_proj`/`down_proj` parameters, the declared
`mlp.experts.#.{gate,up,down}_proj` specs resolved nothing (silently, since
`layer_modules_strict = False`), and quantizing a dots3_note checkpoint
failed with "Lazy forward direct-state materialization left META tensors
under `mlp.experts`". The hook also assigned onto the port module and
`Dots3NotePreTrainedModel._init_weights` at load time, which AGENTS.md
forbids.

Declare `defuser_generic_experts = True`, the mechanism the other native
fused-expert definitions without a Defuser registry entry use (kimi_linear,
mimo_v2_flash, gemma4). The loader's `_convert_model_with_defuser` now
splits the experts into per-expert `nn.Linear` modules under the checkpoint
names. Remove the adapter class and the load-time class patching, and
update the dots3 reference page.

The regression test builds the tiny vendored-config model, runs the
loader's hook + conversion, and checks every routed projection is an
nn.Linear resolved and declared by the module plan, that the split forward
matches the fused forward, and that the load hook leaves the port classes
untouched.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 21:27
@Qubitium
Qubitium merged commit 55f39d3 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/dots3-note-split-experts branch November 29, 2026 21:28
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