Repository navigation
[model] fix: use the transformers 5 rotary API in the GLM-4V attention patch - #7620
Conversation
โฆn patch glm4v_attn_forward imported apply_multimodal_rotary_pos_emb and passed it an mrope_section. transformers 5 folds mrope_section into cos/sin inside Glm4vTextRotaryEmbedding and exposes only apply_rotary_pos_emb, so that import raised ImportError at the first attention layer on every version this repo supports (transformers>=5.5.3,!=5.6.0,<5.11). The patch is installed whenever remove_padding or ulysses_sp_size > 1, which is the ordinary GLM-4V path. The mrope_section lookup went with it: it read self.rope_scaling or self.rope_parameter off the attention module, and neither exists there in transformers 5.
| """ | ||
|
|
||
| import torch | ||
| from transformers.models.glm4v.configuration_glm4v import Glm4vTextConfig |
There was a problem hiding this comment.
Removed. The import and rotary-call fix in glm4v.py stands on its own โ apply_multimodal_rotary_pos_emb does not exist in any transformers this repo supports, so the GLM-4V path raises ImportError at the first attention layer with remove_padding or ulysses_sp_size > 1.
Removed at review request; the API fix stands on its own.
|
The That test builds the server with Happy to send that as a separate one-liner if it is not already being handled. |
What does this PR do?
GLM-4V training dies at the first attention layer on every transformers version this repo
supports.
glm4v_attn_forwardreplacesGlm4vTextAttention.forwardwheneveruse_remove_paddingorulysses_sp_size > 1(monkey_patch.py:473), so this is the ordinary GLM-4V path, not a corner:transformers 5.0 replaced
apply_multimodal_rotary_pos_emb(q, k, cos, sin, mrope_section)withapply_rotary_pos_emb(q, k, cos, sin)and moved the sectioning intoGlm4vTextRotaryEmbedding, which readsmrope_sectionfromconfig.rope_parametersand foldsit into cos/sin before the attention ever sees them.
Glm4vTextAttention.forwardupstream isnow just:
Absent in every supported version โ
requirements.txtasks fortransformers>=5.5.3,!=5.6.0,<5.11, and the extras pin 5.3.0 / 5.5.3:apply_multimodal_rotary_pos_embinmodeling_glm4vIt came in with #5724 (
transformers upgrade to 5.3.0), which updated the config-siderope_scalingโrope_parametersmigration a few lines above but not the function that movedwith it.
qwen2_vl.pyuses the same call and is not affected:modeling_qwen2_vlstill exportsapply_multimodal_rotary_pos_emb. Only GLM-4V was migrated.The change
Call the function upstream calls, and drop the
mrope_sectionlookup that fed the old one.That lookup was dead twice over โ it reads
self.rope_scalingorself.rope_parameteroff theattention module, and on transformers 5 the attention module has neither (the config
attribute is
rope_parameters, plural, and lives on the config):so the
assertunder it would have fired had the import ever got that far.Restoring the old function instead would be wrong, not just unavailable: the 4.x call expects
cos/sin with a leading 3-section dim, and applying it to what transformers 5 produces raises
IndexError: index 1 is out of bounds for dimension 0 with size 1. The sectioning genuinelymoved.
Checklist Before Starting
apply_multimodal_rotary_pos_embon thisrepo; the one hit, RuntimeError: Tensor size mismatch on apply_multimodal_rotary_pos_emb with ulysses_sequence_parallel_size=2ย #2603, predates the 5.x upgrade and is closed.
[{modules}] {type}: {description}.Test
tests/models/test_glm4v_rotary_on_cpu.py, following theon_cpu.pyconvention so it runs incpu_unit_tests.yml. It builds a realGlm4vTextAttentionandGlm4vTextRotaryEmbedding, runsglm4v_attn_forwardwith the CUDA-only flash-attention call patched out, and asserts the q/kreaching attention equal
apply_rotary_pos_emb(q, k, cos, sin)โ the same rotary upstreamapplies โ with KV repeated to the query head count. A second case records why the argument had
to go rather than be restored.
Load-bearing, run against a clean
origin/mainworktree in the same environment:The flash-attention call itself is CUDA-only and unchanged by this PR, so it stays covered by
the existing GPU tests.
API and Usage Example
No API change.
Design & Code Changes
One import and one call site in
verl/models/transformers/glm4v.py; themrope_sectionderivation is removed with them.
Checklist Before Submitting