Repository navigation
feat(glm): integrate Flash target and MTP recovery and export - #3604
Conversation
Extract the existing working fork feature for first maintainer feedback. Preserve carried code and tests; limit validation to lightweight structural checks. See the linked mechanism reference for actual API/call and failure paths.
f5b363f to
3325ada
Compare
Qubitium
left a comment
There was a problem hiding this comment.
Reviewed against current main.
The GLM integration is coherent:
GlmMoeDsaQModel.after_model_loadbinds learned-router recovery only to routedmlp.gatemodules and fails closed on a coverage mismatch againstnum_hidden_layers - first_k_dense_replace; it also rejects routers with notop_k.out_of_model_tensors = {"prefixes": ["model.layers.78"]}keeps the checkpoint-only MTP block byte-for-byte until the separate MTP phase rewrites its routed leaves.configure_base_replay_store/create_quantization_layer_output_writerpersist authoritative post-quantization frontiers with immutable provenance and skip the first three layers, which is the right boundary for the shared-embedding prefix.zero_route_recovery_contextdelegates to the shared learned-router implementation with GLM's rank window rather than duplicating it.Glm5NextQModelcarries the Flash target/MTP paths and the upstream boundary assertions.
Two small cleanups were needed and are in the branch: import ordering/unused imports across the touched modules, and test_glm5_next_module_tree_preserves_upstream_quantization_boundary computed a full module set without asserting anything — it now asserts flat <= full, which is the meaningful invariant.
One pre-existing failure is unrelated to this change: test_glm_moe_dsa_replay_propagates_topk_indices_to_shared_indexer_layer passes attention_mask=None and the installed Transformers modeling_glm_moe_dsa.py indexes it unconditionally. It fails identically on main, so it was deselected for validation rather than treated as a regression.
Validation: ruff check clean; pytest on the three touched test files reports 22 passed, 1 deselected (the pre-existing upstream-mask failure above).
Summary
Carry the existing GLM-5 Next and GLM MoE DSA model bindings, original token-ID capture, bounded replay frontiers, transformed mask/position handling, native expert conversion, and target/MTP route recovery. Keep upstream model-tree/role/shared-expert policies and native Defuser conversion; add MTP/recovery via the existing post-conversion hook.
This is layer 24/36, extracted from an already-used fork for initial maintainer feedback. It depends on #3603. Review only this feature; the cross-fork Files changed view is cumulative because the PR targets upstream
main.Incremental change: 7 files, +1122/−239 lines, including carried tests and the mechanism reference. No merge is automatic. Review value and API shape first; substantial rework can follow that feedback.
Flow and existing-code boundary
flowchart TD L["Glm5NextQuantizationLoader.from_config / from_pretrained"] --> A["_attach_glm5_next_mtp"] A --> M["Glm5NextMTPDecoderLayer: checkpoint-only layer 45"] M --> C["_convert_model_with_defuser: existing upstream flow"] C --> D["defuser.convert_model: native Glm5NextTextExperts"] D --> V["Glm5NextQModel.after_defuser_conversion"] V --> Q{"Target / MTP expert coverage valid?"} Q -->|"yes"| N["Native experts, activation and upstream module_tree retained"] Q -->|"no"| E["Raise: incomplete expert conversion"] N --> R["Glm5NextQModel.after_model_load"] R --> B["bind_sigmoid_grouped_router_recovery"] style E fill:#fee,stroke:#b22gptqmodel/models/definitions/glm5_next.py:13gptqmodel/models/definitions/glm5_next.py:254:Glm5NextQModelgptqmodel/models/definitions/glm_moe_dsa.py:11gptqmodel/models/definitions/glm_moe_dsa.py:19:GlmMoeDsaQModelCommitted reference: docs/glm-integrated-mtp.md, linked from README. Class entry points/calls and rejected-state paths describe code present in this branch, rather than planned integrations.
Validation
Carried test files:
tests/test_glm5_next_support.py,tests/test_glm5_next_upstream_support.py,tests/test_glm_moe_dsa_support.pyThis packaging pass checked Python parsing, whitespace, dependency order and balanced Mermaid blocks. The author reports successful prior use of these features. Per the requested resource limit and feedback-first scope, existing model quantization, GPU/ROCm tests, heavyweight CPU workloads and full model benchmarks were not rerun; no new performance or model-accuracy claim is made. Tests accompanying earlier stack layers retain their previously reported results.
Existing model support: upstream already supports this GLM/DeepSeek family. This proposal adds calibration/MTP/recovery capability; it does not add a competing MODEL_MAP registration. Native Transformers blocks and upstream model plans remain the integration basis.
Reconciled overlap explicitly: upstream module_tree, role metadata, dense shared-expert policy and processor-loading behavior are retained. Removed the fork custom expert forward/container and conversion hook; normal
defuser.convert_modelruns, followed by the existingafter_defuser_conversionhook to validate target/MTP coverage. Two tiny one-thread CPU integration tests passed (native object/forward preserved; lazy checkpoint materialization). The model-plan metadata group passed 10 tests. No model quantization ran.