Skip to content

feat(glm): integrate Flash target and MTP recovery and export - #3604

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
tpurtell:upstream/24-glm-integrated-mtp-20261108
Nov 9, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
tpurtell:upstream/24-glm-integrated-mtp-20261108

Conversation

@tpurtell

@tpurtell tpurtell commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

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:#b22
Loading
Before this extraction layer Carried implementation
Previous stack layer: gptqmodel/models/definitions/glm5_next.py:13 gptqmodel/models/definitions/glm5_next.py:254: Glm5NextQModel
Previous stack layer: gptqmodel/models/definitions/glm_moe_dsa.py:11 gptqmodel/models/definitions/glm_moe_dsa.py:19: GlmMoeDsaQModel

Committed 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.py

This 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_model runs, followed by the existing after_defuser_conversion hook 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.

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.
@Qubitium
Qubitium force-pushed the upstream/24-glm-integrated-mtp-20261108 branch from f5b363f to 3325ada Compare November 9, 2026 09:40

@Qubitium Qubitium left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against current main.

The GLM integration is coherent:

  • GlmMoeDsaQModel.after_model_load binds learned-router recovery only to routed mlp.gate modules and fails closed on a coverage mismatch against num_hidden_layers - first_k_dense_replace; it also rejects routers with no top_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_writer persist 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_context delegates to the shared learned-router implementation with GLM's rank window rather than duplicating it.
  • Glm5NextQModel carries 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).

@Qubitium
Qubitium marked this pull request as ready for review November 9, 2026 09:40
@Qubitium
Qubitium merged commit a507672 into ModelCloud:main Nov 9, 2026
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.

2 participants