Repository navigation
Fix internlm2 dynamic-NTK RoPE: wrong seq axis, scale/factor conflation - #1554
Closed
robertlangdonn wants to merge 1 commit into
Closed
robertlangdonn wants to merge 1 commit into
robertlangdonn wants to merge 1 commit into
Conversation
Same defect class as ml-explore#1545 (internlm3) -- internlm2.py's DynamicNTKScalingRoPE is an identical copy-pasted class with the same four bugs: - Position scale defaulted to 2.0 whenever scaling wasn't "linear", doubling every position for "dynamic" and no-scaling configs alike. Only "linear" should scale positions (1/factor); default is 1.0. - The dynamic-NTK base formula reused that same (wrong) 2.0 instead of the config's `factor`, so a real config never reached the base recomputation. - seq_len was read from x.shape[1], the head-count axis post-transpose ([B, n_heads, L, head_dim]) -- not the sequence axis (shape[2]). The dynamic recompute could never fire at prefill regardless of prompt length. - The base recompute wasn't gated on rope_type, so "linear" scaling also rewrote the base past max_position_embeddings, when it should stay static and only the position scale should apply.
robertlangdonn
force-pushed
the
fix-internlm2-rope-dynamic-ntk
branch
from
July 11, 2026 21:28
0144299 to
2662e28
Compare
Member
|
Thanks for the PR but at the moment the number of PRs is way beyond our capacity to review so I'm closing the non-essential ones so we can actually work on this repo. |
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.
Fix internlm2 dynamic-NTK RoPE: wrong seq axis, scale/factor conflation
Summary
Twin of #1552's sibling PR for internlm3 (#1545 / this repo's #1553): while fixing internlm3's
DynamicNTKScalingRoPE, I foundinternlm2.pyhas an identical copy-pasted class with the same four bugs. No separate issue was filed for internlm2 specifically, so this PR describes and fixes it directly, scoped to that one file, rather than bundling it into #1553.Four related bugs in
DynamicNTKScalingRoPEand its construction inAttention.__init__(config key is"type"here, vs"rope_type"in internlm3 — otherwise identical):"linear"— including norope_scalingat all — doubling every position index fed intomx.fast.rope. Only"linear"should scale positions (1/factor); default should be1.0.factor, so a real config (e.g.factor=6.0) never reached the base recomputation.seq_lenwas read fromx.shape[1]— the head-count axis post-transpose ([B, n_heads, L, head_dim]) — not the sequence axis (shape[2]). The dynamic recompute could never fire at prefill regardless of prompt length.rope_type—"linear"scaling also rewrote the base once pastmax_position_embeddings, when it should stay static and only the position scale should apply.Fix
Same shape as #1553: split
scale(position scale) from a newntk_factor(configfactor, used only in the base-growth formula), threadrope_typethrough to gate the recompute, and readseq_lenfromx.shape[2].Total change: +22/−9 in
mlx_lm/models/internlm2.py.Verification
Falsified all four defects against the pre-fix code directly (pure position-math bugs, reproducible with tiny synthetic tensors, no checkpoint needed), then verified each is fixed — same method as #1553:
rope_scaling→scalewas2.0, now1.0.{"factor": 6.0, "type": "dynamic"}→scalewas2.0(factor discarded), nowscale=1.0/ntk_factor=6.0.(1, 4, 100, 8)(4 heads, real seq_len=100,max_position_embeddings=64) — pre-fix,seq_lenread as4and the recompute never fired; post-fix it correctly fires on100."linear"mode pastmax_position_embeddings— pre-fix, base was rewritten anyway; post-fix, output is bit-identical to a run wheremax_position_embeddingsis never exceeded.Test plan
test_internlm2_dynamic_ntk_rope, mirroringtest_internlm3_dynamic_ntk_ropefrom Fix internlm3 dynamic-NTK RoPE: wrong seq axis, scale/factor conflation #1553. Confirmed it fails on pre-fix code (2.0 != 1.0) and passes post-fix.python -m unittest discover tests/— pre-existing unrelated import errors only (datasets/lm_eval/requests).pre-commit run --files mlx_lm/models/internlm2.py tests/test_models.py— clean.