Repository navigation
fix(GlmOcr): carry M-RoPE delta into generation (repetition loop on large images) - #454
Closed
elaverdiere wants to merge 1 commit into
Closed
elaverdiere wants to merge 1 commit into
elaverdiere wants to merge 1 commit into
Conversation
…arge images) GLM-OCR degenerated into an 'AgAgAg…' repetition loop on large document images while transcribing small images correctly. Root cause: the M-RoPE rope_delta computed at prefill was stored only on the transient LMOutput.State, which the generate loop does not carry into autoregressive steps. Generation therefore fell back to sequential KV-cache-offset positions (e.g. 4830, 4831, …) instead of continuing from the LM's last M-RoPE position after prefill (~90). Because GLM-OCR packs many image patches onto few M-RoPE positions, the delta is a large negative number for a full page, so the mismatch is small for small images (tolerable) and catastrophic for large ones — matching the observed size threshold. Fix: persist the delta on the model instance (as mlx-vlm does with self._rope_deltas) and apply it to the autoregressive position computation. Verified against mlx-vlm on a full 1700x2200 page: identical faithful transcription instead of a repetition loop. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
davidkoski
reviewed
Jul 27, 2026
| // carry — so generation fell back to sequential cache-offset positions (0,1,2… → 4830,4831… | ||
| // for a large image), a catastrophic M-RoPE mismatch that degenerates into a repetition | ||
| // loop. Persisting it here restores parity with mlx-vlm. Set in `prepare(_:cache:windowSize:)`. | ||
| fileprivate var glmRopeDelta: Int32 = 0 |
Member
There was a problem hiding this comment.
We can't store state on the model:
- it isn't thread safe
- it can leak between concurrent evaluation contexts
Is the problem that the state[ropeDeltasKey] persists between tokens but not between responses?
davidkoski
requested changes
Jul 27, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
See comment -- we can't store state on the model.
Member
|
Closing: this is fixed in #419 -- the Output keys were being dropped in certain cases. |
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(GlmOcr): carry M-RoPE delta into generation (repetition loop on large images)
Problem
GLM-OCR(e.g.mlx-community/GLM-OCR-4bit) degenerates into anAgAgAg…repetition loop when transcribing large document images, while
transcribing small images correctly with the same model, prompt and
temperature = 0. The Python reference (mlx-vlm) handles the same full-resimages fine, so this is a Swift-port bug, not a model/quantization issue.
Root cause
GLM-OCR uses M-RoPE (3D positions). Its image tokens pack many patches onto a
small span of M-RoPE positions, so after prefill the language model's last
position is far below the token count.
get_rope_indexcomputes arope_delta = maxPosition + 1 - tokenCount— a large negative number for afull page — that generated tokens must use to continue from the last real
position rather than from the raw KV-cache offset.
In the Swift port, that delta is stored only on the transient
LMOutput.Statecreated in
prepare(_:cache:windowSize:). The generation loop does notcarry that state into subsequent autoregressive steps, so
LanguageModel.callAsFunctionfinds no stored positions andGlmOcrTextModelfalls back to sequential KV-cache-offset positions(e.g. 4830, 4831, 4832 …) instead of continuing from ~90. Because the delta is
tiny for small images and huge for large ones, the mismatch is tolerable for
small images and catastrophic for large ones — exactly the observed size
threshold.
mlx-vlmavoids this by persisting the delta on the model instance(
self._rope_deltas); the Swift port only put it on the per-call state.Fix
Persist the M-RoPE delta on the
GlmOcrTextModelinstance and apply it to theautoregressive position computation. One file,
Libraries/MLXVLM/Models/GlmOcr.swift.Verification
Reproduced with a minimal Swift harness that mirrors the exact
VLMModelFactoryload +UserInput(images:)path. On a full 1700×2200 page:AgAgAgAg…(repetition loop)mlx-vlmon the same image.
Layer-by-layer tensor-stat comparison against
mlx-vlmconfirmed the visionencoder, prefill hidden states, position ids and rope_delta all matched; the
only divergence was the generation-phase positions, which this patch corrects.