fix(glm): inline GLM-MoE-DSA routing to survive transformers without route_tokens_to_experts (#144) - #166
Merged
Merged
Conversation
…-5.15 (#144) SyncGlmMoeDsaMoEBlock borrowed transformers' internal GlmMoeDsaMoE.route_tokens_to_experts, which 5.15 removed after moving routing into GlmMoeDsaTopkRouter (now returns (router_logits, topk_weights, topk_indices)). _route is now adaptive: unpack the router tuple on 5.15+, else apply an inlined grouped-sigmoid-top-k (mirrors 5.12 route_tokens_to_experts). Removes the fragile dep+import. Verified: 5.12 numerical parity + method-absent path; 5.15 tuple-contract unpack + forward. Test: module-skip -> per-test skipif on parity only. Closes #144.
drunkcoding
force-pushed
the
fix/glm-route-tokens-compat
branch
from
August 16, 2026 14:58
cef76c9 to
2dd96f8
Compare
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (#144)
SyncGlmMoeDsaMoEBlockreused HuggingFace's internalGlmMoeDsaMoE.route_tokens_to_experts(bound to the offload block'sself). Investigation showed the GLM routing architecture changed across the pinnedtransformers>=5.3,<6range:GlmMoeDsaTopkRouter.forwardreturns rawrouter_logits;GlmMoeDsaMoE.route_tokens_to_experts(logits)does the grouped-sigmoid-top-k and returns(indices, weights).route_tokens_to_expertsis removed; the routing moved intoGlmMoeDsaTopkRouter.forward, which now returns a 3-tuple(router_logits, topk_weights, topk_indices).So GLM was genuinely broken on 5.15 (not just a test issue):
#147's guard turned the crash into aRuntimeErrorand the test module-skipped, hiding it. Theself.gate(...)output being a tuple on 5.15 is exactly why the block's routing failed ('tuple' object has no attribute 'sigmoid').Fix — version-robust routing
_routeis now adaptive:self.gate(...)returns(router_logits, topk_weights, topk_indices)→ unpack and use directly.self.gate(...)returns raw logits → apply an inlined_route_tokens_to_experts(a faithful copy of 5.12's grouped-sigmoid-top-k).The fragile
GlmMoeDsaMoEreference (and import) is removed, so routing no longer depends on an unstable HF internal.test_glm_routing.py: the module-level skip becomes a per-testskipifontest_routing_parityonly (that test compares against the HF method, absent on 5.15), so the other GLM tests now run and exercise the block on all builds.Verification
pytest test_glm_routing.py→ 5 passed. Direct numerical parity: inline routing == HFroute_tokens_to_experts(torch.equalindices,allcloseatol 1e-6 weights). Method-absent simulation: construct + forward OK._routeunpacks the real(router_logits, topk_weights, topk_indices)contract andforward()produces correct-shape output.ruff format/check,py_compile, unit-test collection: clean.Note: a pre-existing environment-only failure (
test_glm_moe_dsa.py::test_glm_cpp_dequant_matches_python, missing compiled_store.dequant_fp8_blockwise) is unrelated (fails identically on cleandev).Closes #144.