Skip to content

fix(models): support Transformers 5.22 trees and calibration (#4040) - #4055

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4040-trees
Nov 14, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4040-trees

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Part of #4040

This PR implements sections E-I and the Also list of issue #4040. Sections A-D landed separately in #4049. The issue should close when both halves are complete.

Root causes and fixes

Area Root cause on Transformers 5.22 Fix
LFM2-MoE gptqmodel/models/definitions/lfm2_moe.py:30 declared self_attn.o_proj, while 5.22 uses out_proj; line 36 declared experts.#.w1/w3/w2, while Defuser exposes gate_proj/up_proj/down_proj. 13 of 51 decoder linears were silently skipped. Declares out_proj and the defused expert names, and uses GateUpDownMoELifecycleHooks. tests/module_tree/test_moe_expert_coverage.py now requires every LFM2-MoE quantizable spec to resolve.
GLM-4.5/4.6/4.7 and GLM-4.7-Flash Defuser 0.0.30's replacement MoE block passes the 5.22 router tuple (logits, weights, indices) into route_tokens_to_experts, which calls .sigmoid(). gptqmodel/utils/defuser_compat.py:74 source-gates a shim that unwraps the tuple and delegates to Defuser's original routing method. Quantize and reload paths install it. The authoritative upstream fix still belongs in Defuser.
mllama_text_model MllamaTextConfig is not registered with AutoModelForCausalLM in 5.22, and the text-only decoder lives at model.model. Registers the config/class pair in mllama.py:27 and resolves the text-only layout in mllama.py:92.
Native kimi_linear The registry key always selected the AttnRes/K3 definition, whose quantize() requires KimiK3Source; native KimiLinearForCausalLM has no AttnRes and uses self_attn plus mlp.experts/shared_experts. auto.py:645 dispatches on AttnRes/K3 config fields. Native configs use KimiLinearNativeQModel and Defuser's generic experts pass; K3 configs keep the bounded K3 runner.
HRM-Text The flattened L0/L1/H0/H1 chain captured only the first recurrent invocation. The real forward runs L H_cycles * L_cycles times and H H_cycles times, with H input z_H + final_norm(z_L). hrm_text.py:167 captures every invocation with full-model forward hooks and pre_quantize() serves each layer its own invocation batches. Captures are released in after_quantize().
GLM-ASR AutoModel built GlmAsrModel, whose layers are at language_model.layers and which has no lm_head. Loads GlmAsrForConditionalGeneration, uses model.language_model.layers, top-level lm_head, and the matching rotary/norm paths.
Llama 4 The default lm_head path missed language_model.lm_head. Declares the nested path.
Qwen2.5-Omni thinker.model.norm no longer exists in 5.22. Declares ordered candidates for the 5.22 and legacy layouts.
MiMo-V2-Flash Hub configs report mimo_v2_flash, which had no definition. Adds and registers MimoV2FlashQModel, including generic Defuser expert expansion.
DeepSeek-V4 GPTQ/AWQ/RTN target layers stayed on META because the packed EXL3 lazy boundary was applied to every method. Keeps the lazy boundary for EXL3/floatx only; ordinary methods materialize the full layer before its first subset forward.

Resolution flow

flowchart TD
    A["Installed Transformers config"] --> B["check_and_get_model_definition"]
    B --> C{"model_type is kimi_linear"}
    C -->|"AttnRes or K3 fields"| D["KimiLinearQModel"]
    C -->|"native config"| E["KimiLinearNativeQModel"]
    B --> F["mllama_text_model"]
    F --> G["MllamaForCausalLM"]
    G --> H["resolve model.model"]
    B --> I["mimo_v2_flash"]
    I --> J["MimoV2FlashQModel"]
    E --> K["generic Defuser experts pass"]
    J --> K
    K --> L["module tree resolves every quantizable leaf"]
    M["glm4_moe / glm4_moe_lite"] --> N["source-gated router tuple shim"]
    N --> O["original Defuser routing math"]
    P["HRM-Text"] --> Q["capture every recurrent invocation"]
    Q --> R["per-layer recurrent Hessians"]
Loading

Before / after

Tiny random checkpoints, GPTQ 4-bit, group_size=32, desc_act=False, save, reload with BACKEND.TORCH, logits against fp16 and an RTN baseline. Coverage excludes lm_head, which remains dense under the default configuration.

Family GPTQ vs fp16 RTN vs fp16 Quantized decoder linears Before
LFM2-MoE 0.0837 0.1213 51/51 38/51 (13 skipped)
GLM-4.5 MoE 0.0158 0.0603 26/26 tuple crash
Mllama text 0.0242 0.0555 14/14 config/load crash
Native Kimi-Linear 0.0209 0.0541 32/32 requires KimiK3Source
HRM-Text 0.0008 0.0016 32/32 one-cycle calibration; GPTQ worse than RTN
GLM-ASR 0.0162 0.0507 14/14 no layer / no head
MiMo-V2-Flash 0.0355 0.1134 23/23 no definition
DeepSeek-V4 0.0242 0.0286 48/48 first-subset META crash

ROCm reproduced the same behavior: LFM2-MoE 0.0837 vs 0.1213, GLM-4 0.0158 vs 0.0603, Mllama 0.0242 vs 0.0555, Kimi-Linear 0.0209 vs 0.0541, HRM 0.0008 vs 0.0016, GLM-ASR 0.0163 vs 0.0507, MiMo 0.0353 vs 0.1133, and DeepSeek-V4 0.0242 vs 0.0286. All outputs were finite and GPTQ was better than RTN.

Validation

  • ruff check gptqmodel docs/eora tests setup.py -> All checks passed
  • CUDA module-plan/fusion/MoE suite: 1024 passed, 135 skipped
  • CUDA targeted support suite: 108 passed, 12 skipped
  • CUDA + ROCm tests/test_mllama_support.py tests/test_hrm_text_support.py -> 19 passed on each
  • Eight tiny end-to-end GPTQ/RTN save/reload runs -> all passed on both CUDA and ROCm
  • Hardware: NVIDIA RTX 4090 GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae and AMD R9700 via HIP_VISIBLE_DEVICES=0. The RTX 5090 was not used.

Documentation

docs/transformers-5-compat.md now combines the landed A-D flow with these E-I/Also flows and measured evidence, and is linked from README.md. Mermaid fences are validated by GitHub-compatible syntax.

Reviewer notes

  • Defuser remains the owner of the GLM-4.x router fix. This PR's shim is source-gated so a future Defuser release that unpacks the tuple is not masked.
  • HRM-Text materializes the complete recurrent model for one calibration pass. This is bounded for the 1B family; a larger recurrent model would need a streaming replay.

…urrent calibration (#4040)

Sections E-I and the "Also" list of issue #4040. Part of #4040; the issue
closes when both halves land.

- lfm2_moe: declare the 5.22 `out_proj` and defused per-expert
  gate/up/down projections; switch to GateUpDownMoELifecycleHooks and pin
  expert coverage. 13/51 -> 51/51 decoder linears quantized.
- glm4_moe / glm4_moe_lite: add a source-gated Defuser shim that unwraps the
  5.22 `(logits, weights, indices)` router tuple before routing. The upstream
  fix belongs in Defuser.
- mllama_text_model: register MllamaTextConfig with AutoModelForCausalLM and
  resolve the text-only `model.model` layout.
- kimi_linear: dispatch native KimiLinearForCausalLM to a native tree and run
  Defuser's generic experts pass; AttnRes/K3 configs keep the K3 runner.
- hrm_text: capture every L/H stack invocation with full-model forward hooks
  and accumulate the Hessian over the true recurrent inputs.
- glmasr: load the complete GlmAsrForConditionalGeneration wrapper and use the
  model.language_model tree and top-level lm_head.
- llama4: resolve lm_head under language_model.
- qwen2_5_omni: declare both pre-lm-head norm layouts as candidates.
- mimo_v2_flash: register the native MiMo-V2-Flash tree with generic experts.
- deepseek_v4: keep the packed lazy boundary only for EXL3/floatx; GPTQ/AWQ/RTN
  materialize target layers eagerly instead of failing on META projections.

Adds docs/transformers-5-compat.md with the resolution and calibration flows.
# Conflicts:
#	docs/transformers-5-compat.md
#	tests/test_qwen2_5_omni_lm_head.py
Comment thread gptqmodel/utils/defuser_compat.py Dismissed
@Qubitium
Qubitium merged commit 451d304 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4040-trees branch November 14, 2026 19:14
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