Skip to content

fix: import GLM5 fused experts under transformers>=5.13 - #385

Merged
yghstill merged 1 commit into
Tencent:mainfrom
Linxiushen:fix-glm5-transformers-experts-rename
Sep 25, 2026
Merged

yghstill merged 1 commit into
Tencent:mainfrom
Linxiushen:fix-glm5-transformers-experts-rename

Conversation

@Linxiushen

Copy link
Copy Markdown
Contributor

Fixes #382

Problem

angelslim/models/llm/glm5_1.py does

from transformers.models.glm_moe_dsa.modeling_glm_moe_dsa import GlmMoeDsaNaiveMoe

and angelslim/models/llm/__init__.py imports GLM5_1 eagerly. transformers renamed that class to GlmMoeDsaExperts in 5.13.0 (5.12.x still has GlmMoeDsaNaiveMoe; 5.13.0 onwards only GlmMoeDsaExperts), while requirements/requirements.txt allows transformers>=5.6.0,<6.0. So with transformers 5.13–5.17 (current release 5.17.0) every entry point fails at import time, for any model:

$ python tools/run.py --help
ImportError: cannot import name 'GlmMoeDsaNaiveMoe' from 'transformers.models.glm_moe_dsa.modeling_glm_moe_dsa'
$ python -c "from angelslim import Engine"
ImportError: cannot import name 'GlmMoeDsaNaiveMoe' ...

Fix

Try the old name first and fall back to the new one:

try:
    from transformers.models.glm_moe_dsa.modeling_glm_moe_dsa import GlmMoeDsaNaiveMoe
except ImportError:
    from transformers.models.glm_moe_dsa.modeling_glm_moe_dsa import GlmMoeDsaExperts as GlmMoeDsaNaiveMoe

The alias is exact, not just name-compatible: diffing GlmMoeDsaNaiveMoe (v5.12.0) against GlmMoeDsaExperts (v5.17.0) in transformers shows the only change is the class name — same num_experts / hidden_dim / intermediate_dim / act_fn attributes, same 3-D gate_up_proj / down_proj parameters, same forward(hidden_states, top_k_index, top_k_weights). Those are exactly what _is_glm_naive_moe() and GlmExpertsWithLinear rely on, so the GLM5 quantization path keeps working rather than silently skipping the expert replacement.

Test

tests/test_glm5_experts_compat.py:

  • angelslim.models.llm imports, and glm5_1.GlmMoeDsaNaiveMoe is whichever fused-experts class the installed transformers provides;
  • a tiny GlmMoeDsaConfig (hidden 8, intermediate 6, 4 experts) fused experts module wrapped in GlmExpertsWithLinear gives the same forward output as the original module (torch.testing.assert_close), i.e. the linearization still matches the renamed class.

Checked with transformers 5.17.0 / torch 2.14: on main both import angelslim.models and tools/run.py --help raise the ImportError above and the new test errors at collection; with this change they succeed, the two new tests pass, and tests/test_config_parser.py + tests/test_install_extras.py still pass (8 passed). black (99) / isort (black profile) / flake8 (99) clean on the changed lines (the E231 flake8 reports in glm5_1.py are pre-existing, on lines this PR does not touch).

Related: #330 is a different, runtime-level transformers-5.x incompatibility (rope / tied-weights); this PR only restores importability.

Written with Claude Code (AI-assisted) and submitted under the account owner's authorization.

transformers 5.13 renamed GlmMoeDsaNaiveMoe to GlmMoeDsaExperts (same class
body). angelslim/models/llm/glm5_1.py imported the old name unconditionally
and angelslim/models/llm/__init__.py imports GLM5_1 eagerly, so with any
transformers release from 5.13 up to the declared <6.0 ceiling every CLI and
`from angelslim import Engine` failed at import time, even for non-GLM
models.

Fall back to the new name when the old one is missing, and add a test that
the adapter imports and that GlmExpertsWithLinear reproduces the fused
experts module's forward on the installed transformers.

Fixes Tencent#382
@yghstill
yghstill merged commit a7274af into Tencent:main Sep 25, 2026
5 checks passed
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.

transformers>=5.6,<6 下 GLM5 导入不兼容,导致 AngelSlim 全部 CLI 无法启动

2 participants