Add support for GLM-4.7-Flash (glm4_moe_lite) - #1300
Open
Knightkolla wants to merge 2 commits into
Open
Conversation
glm4_moe_lite reuses glm4_moe's (GLM-4.5) MoE block, decoder loop, and model wrappers unchanged. The one architectural difference upstream is attention: Glm4MoeLiteAttention is Multi-head Latent Attention (MLA), the same mechanism DeepSeek-V3 uses, with GLM's interleaved RoPE convention on top. Adds QEffGlm4MoeLiteAttention combining DeepSeek's MLA structure with GLM's rotary embedding handling; everything else is a thin, self-contained port of the existing glm4_moe QEff wrappers. Covers the baseline full-KV decode path only. Blocked/chunked decode and the compressed-cache "absorption" optimization are deferred. Generalizes a DeepSeek-only architecture-name check in modeling_auto.py (MLA retained-state export shape override) to the existing is_mla_model utility, since glm4_moe_lite hits the same gap DeepSeek-V3 already needed a workaround for: get_padding_shape_from_config can't derive MLA's asymmetric key/value head_dim from a single head_dim field. Closes quic#850. Validated: HF PyTorch vs QEff PyTorch parity (exact match, single-shot and 4-step greedy decode), ONNX export, and the existing quickcheck MoE-prefill-subfunction export-smoke test extended to glm4_moe_lite. Full test_model_quickcheck.py suite and transform-regression tests run clean (184 passed, 17 skipped, 1 pre-existing unrelated failure verified against main). See PR description for exact commands. Signed-off-by: Knightkolla <kartikeyadhavala2468@gmail.com>
Signed-off-by: Knightkolla <166029176+Knightkolla@users.noreply.github.com>
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.
Closes #850.
Summary
Adds QEfficient support for GLM-4.7-Flash (HF architecture
glm4_moe_lite). This ships natively intransformers==5.5.4, already pinned in this repo, so no upstream dependency bump is needed.glm4_moe_liteis, per HF's own modular source (transformers/models/glm4_moe_lite/modular_glm4_moe_lite.py), a bare pass-through subclass ofglm4_moe(GLM-4.5) for rotary embedding, MLP, TopkRouter, RMSNorm, Experts, MoE, decoder layer, Model, and ForCausalLM. The one real architectural difference is attention:Glm4MoeLiteAttentionsubclassesDeepseekV3Attentionupstream, meaning GLM-4.7-Flash uses Multi-head Latent Attention (MLA), the same mechanism DeepSeek-V3 uses, with GLM's interleaved RoPE convention layered on top.Changes
QEfficient/transformers/models/glm4_moe_lite/modeling_glm4_moe_lite.py, following this repo's existing per-family convention (self-contained, mirrorsglm4_moe/deepseek_v3's wrapper files, no cross-file class inheritance). Reuses glm4_moe's decoder-loop/MoE/model logic where behavior is identical; writesQEffGlm4MoeLiteAttentionfresh, combining DeepSeek's MLA structure with GLM's interleaved RoPE and this repo's precomputed-sin/cos + cache-update conventions.pytorch_transforms.pynow includes the new classes inCustomOpsTransform,KVCacheTransform, andOptimizedMoEMapperTransform, plus an opt-out inBlockingAttentionTransform._skip_classessince blocked decode isn't implemented for this attention yet.modeling_auto.py: generalizes a previously DeepSeek-V3-only, exact-architecture-string check (governing the retained-statepast_key_valuesexport shape override) to the repo's existingis_mla_modelutility.get_padding_shape_from_configcan't derive MLA's asymmetric key/value head_dim from a singlehead_dimconfig field.get_dummy_pkv_cacheto work around this, andglm4_moe_liteneeds the same, but the consuming check was hardcoded to DeepSeek's architecture name only.tests/unit_test/models/test_model_quickcheck.py'sTINY_MOE_PREFILL_SUBFUNCTION_CONFIGSwith aglm4_moe_liteentry (config-derived tiny model, no Hub checkpoint exists for this architecture yet), picked up automatically by the existingtest_moe_prefill_subfunction_export_uses_einsum_reductionstest.Scope / What's Deferred
This covers the baseline full-KV decode path only, per this repo's MoE onboarding guidance (start with the plain path, add optimizations only after baseline parity and export stability are proven). Deliberately not included:
BlockingAttentionTransform._skip_classesso it warns instead of silently ignoringattn_blocking_config, rather than being silently wrong).QEffDeepseekV3Attention.fused_forward*implements for DeepSeek.SPECIALIZED_DISAGG_SERVING_MODEL_ARCHis not extended).ApiRunner-based runtime-parity testing. No stable CPU runtime-parity lane exists yet for MLA/MoE families in this repo's quickcheck (neither DeepSeek-V3 nor plain glm4_moe has one either), so this PR's coverage tier (export-smoke) matches existing precedent.ApiRunnercurrently can't build correct MLA cache shapes (same root cause as themodeling_auto.pyfix above, but inQEfficient/utils/generate_inputs.py'sInputHandler, which wasn't touched here to keep this change's blast radius scoped to the new model).Test Plan
Notes on the full quickcheck run:
test_causal_subfunction_export_uses_semantic_weight_and_node_names): pre-existing, unrelated to this change. Reproduced identically onmainwith this PR's changes fully removed (git stash -u), using a plainLlamaConfig, no glm4_moe_lite/MLA involvement. Not fixed here; flagging for a maintainer.test_kimi_k25_quickcheck_hf_qeff_vision_logits_parity): consistently stalls on network I/O formoonshotai/Kimi-K2.5(trust_remote_code=Trueremote-code fetch) in this environment, 9+ minutes wall-clock with ~15s of actual CPU time (blocked, not computing). Unrelated to glm4_moe_lite; excluded from this run rather than left to hang. Worth a maintainer re-checking in an environment with better connectivity to that repo.Known Issues Found (Out of Scope for This PR)
QEffDeepseekV3Attention'sforward/forward_full_kv*methods (QEfficient/transformers/models/deepseek_v3/modeling_deepseek.py) referenceself.q_head_dimandself.softmax_scale, which don't exist ontransformers==5.5.4'sDeepseekV3Attention(renamed toqk_head_dimandscalingupstream). Looks pre-existing, predating thetransformerspin moving to 5.5.4. Not touched here since it's orthogonal to this change, but worth a maintainer's attention.test_causal_subfunction_export_uses_semantic_weight_and_node_namesfails onmaintoday, unrelated to this PR (see Test Plan).test_kimi_k25_quickcheck_hf_qeff_vision_logits_paritystalls on network I/O fetchingmoonshotai/Kimi-K2.5remote code in this environment, unrelated to this PR (see Test Plan).torchvisioncan't install locally on macOS, it's pinned inpyproject.tomlto Linux-x86_64-only prebuilt wheel URLs (manylinux_2_28_x86_64), so it silently doesn't install on Darwin. CI (ubuntu-latest) is unaffected. Unrelated to this PR; flagging since it affects anyone developing on a Mac.AI Assistance Disclosure
This change was developed with Claude Code assistance (exploration, code authoring, and the validation described above). I have reviewed every changed line and can defend this change end-to-end.