Skip to content

fix(GlmOcr): carry M-RoPE delta into generation (repetition loop on large images) - #454

Closed
elaverdiere wants to merge 1 commit into
ml-explore:mainfrom
elaverdiere:glmocr-mrope-delta-fix
Closed

elaverdiere wants to merge 1 commit into
ml-explore:mainfrom
elaverdiere:glmocr-mrope-delta-fix

Conversation

@elaverdiere

Copy link
Copy Markdown

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 an AgAgAg…
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-res
images 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_index computes a
rope_delta = maxPosition + 1 - tokenCount — a large negative number for a
full 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.State
created in prepare(_:cache:windowSize:). The generation loop does not
carry that state into subsequent autoregressive steps, so
LanguageModel.callAsFunction finds no stored positions and
GlmOcrTextModel falls 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-vlm avoids 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 GlmOcrTextModel instance and apply it to the
autoregressive position computation. One file, Libraries/MLXVLM/Models/GlmOcr.swift.

Verification

Reproduced with a minimal Swift harness that mirrors the exact
VLMModelFactory load + UserInput(images:) path. On a full 1700×2200 page:

  • before: AgAgAgAg… (repetition loop)
  • after: faithful transcription, byte-for-byte plausible against mlx-vlm
    on the same image.

Layer-by-layer tensor-stat comparison against mlx-vlm confirmed the vision
encoder, prefill hidden states, position ids and rope_delta all matched; the
only divergence was the generation-phase positions, which this patch corrects.

…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>
// 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment -- we can't store state on the model.

@davidkoski

Copy link
Copy Markdown
Member

Closing: this is fixed in #419 -- the Output keys were being dropped in certain cases.

@davidkoski davidkoski closed this Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants