Skip to content

Fix prefill LMOutput.State being dropped on TokenIterator's .logits path - #419

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
NivDvir:fix/tokeniterator-state-carry
Jul 13, 2026
Merged

davidkoski merged 1 commit into
ml-explore:mainfrom
NivDvir:fix/tokeniterator-state-carry

Conversation

@NivDvir

@NivDvir NivDvir commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Proposed changes

Qwen2.5-VL seeds its MROPE positionIds/ropeDeltas into the per-call LMOutput.State during prefill (the #283 pattern), but TokenIterator.prepare(...)'s .logits branch never carries result.state into self.state, so the first decode step runs without them — positions drift by the image-token count and greedy decode emits an empty ``` fence instead of the JSON body.

Reproducible with mlx-community/Qwen2.5-VL-7B-Instruct-4bit on any dense full-page screenshot (e.g. a 1280×900 capture of a Wikipedia article) with a standard bbox_2d grounding prompt at temperature 0: main returns a lone fence; with the state carried, the same input grounds correctly.

The .tokens branch already gets the carry via step(previous:), and the speculative iterator's .logits branch does mainState = result.state; this makes the standard path consistent with both. +2 lines, one function.

Follows the Qwen2.5-VL fixes in #238 / #239 / #242 / #243 — #239 moved the MROPE state onto per-call LMOutput.State; this restores its prefill→decode handoff on the .logits path.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works — no new test; the repro above exercises the path end-to-end
  • I have updated the necessary documentation (if needed) — not needed

Qwen2.5-VL seeds its MROPE positionIds/ropeDeltas into the per-call
LMOutput.State during prefill, but prepare()'s .logits branch never carried
result.state into self.state, so the first decode step ran without them —
position drift after an image-token block, degenerate empty output on dense
frames. The .tokens branch already gets the carry via step(previous:) and the
speculative iterator sets mainState = result.state; this makes the standard
path consistent.

@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.

Change looks good. will be useful for #399 as well. Just waiting on CI

@davidkoski
davidkoski merged commit 42f08a8 into ml-explore:main Jul 13, 2026
2 checks passed
spokvulcan added a commit to spokvulcan/mlx-swift-lm that referenced this pull request Jul 14, 2026
Review rework for ml-explore#399: no new protocol — LanguageModel.prepare gains
state: LMOutput.State? instead, and prepareContinuation becomes a
private detail of Qwen35.

This also fixes a live cross-turn bug (the sibling of ml-explore#419's
prefill-to-decode drop): ChatSession keeps the KV cache warm across
turns, but the per-call M-RoPE state died with each turn's
TokenIterator, so Qwen3.5 recomputed positions from zero on every warm
continuation. On a tiny random-weight model, warm turn-2 logits diverge
from a cold full prefill by 0.43 max-abs against a 8.3e-07 decode-path
noise floor; with state threaded, all continuation shapes match the
one-shot reference.

- Qwen35: windowSize ?? 512 chunked prefill (Gemma3 parity); warm
  single-sequence continuations always route through the anchored
  windowed forward (anchor = cache offset + carried rope delta)
- TokenIterator: optional seed state, post-prefill state readable
- ChatSession: state persists across turns and tool-call restarts,
  stored alongside the KV cache
- All other conformers take the parameter mechanically and ignore it;
  speculative/MTP iterators unchanged
- Qwen35ContinuationTests: five tiny-model equivalence tests (warm
  text/image continuation, three-turn image resume state, windowed vs
  single-shot with an image straddling a window boundary)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
dkgv pushed a commit to dkgv/mlx-swift-lm that referenced this pull request Jul 30, 2026
…ath (ml-explore#419)

Qwen2.5-VL seeds its MROPE positionIds/ropeDeltas into the per-call
LMOutput.State during prefill, but prepare()'s .logits branch never carried
result.state into self.state, so the first decode step ran without them —
position drift after an image-token block, degenerate empty output on dense
frames. The .tokens branch already gets the carry via step(previous:) and the
speculative iterator sets mainState = result.state; this makes the standard
path consistent.
div0-space pushed a commit to LibraxisAI/mlx-swift-lm that referenced this pull request Nov 29, 2026
* ci: queue Swift LM workloads on native runners

* CI: stage disabled native CodeQL scanning and isolated uploads

Preserve the existing Actions, C/C++ and Python default suites and categories. Keep activation behind CODEQL_NATIVE until the operator verifies advanced setup and the upload path.

Prepared with OpenAI Codex assistance.

* docs: describe native queue and qualification limits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants