Skip to content

fix: use one RoPE offset per Gemma3nText forward pass - #215

Merged
Gajesh2007 merged 2 commits into
mainfrom
fix/gemma3n-rope-offset
Oct 2, 2026
Merged

Gajesh2007 merged 2 commits into
mainfrom
fix/gemma3n-rope-offset

Conversation

@davidtai

@davidtai davidtai commented Sep 30, 2026 •

Copy link
Copy Markdown

Proposed changes

Part of #195: defects 1 and 2. One RoPE offset for each forward pass of Gemma3nText.

Defects

  1. Libraries/MLXLLM/Models/Gemma3nText.swift:304 with :292 and :299. The keys get RoPE (line 292) before cache.update (line 299). The queries get RoPE after cache.update (line 304). So the queries read the offset after the new tokens, L positions ahead of their keys. Every pass with a cache gives other logits than the pass without a cache.
  2. Gemma3nText.swift:269-273 with :304. A KV-shared layer reads its keys and values from the cache of a source layer (lines 269-273) and rotates its queries with the offset of that cache (line 304). The source layer has already moved that offset past the new tokens. This is the same mechanism as defect 1.

Origin: the fork did not change these lines. They are the upstream code at the fork base 7e2b7107. Upstream later fixed defect 1 (ml-explore/mlx-swift-lm b1b358f1, #280: one cache?.ropeOffset per attention call), but that fix still reads the offset of the shared cache in a KV-shared layer, so upstream still has defect 2.

Reference

mlx-lm mlx_lm/models/gemma3n.py (commit a9bd8af):

  • gemma3n.py:495: the language model reads offset = cache[0].offset once, before any layer runs.
  • gemma3n.py:511: it passes that offset to every layer.
  • gemma3n.py:139 and :149: the attention rotates the keys and the queries with that one offset.
  • gemma3n.py:131-133: a KV-shared layer reads the keys and values of the source cache and does not use that cache's offset.

Fix

  • New Gemma3nRoPEOffset (a scalar, or an array for the compile-safe and batched caches through graphOffsetArray(for:)).
  • Gemma3nLanguageModel reads it once from the first cache, before layer 0 updates its cache, and passes it through Gemma3nDecoderLayer to Gemma3nAttention.
  • Gemma3nAttention rotates the queries and the keys with it, in all three places.

Tests

New Tests/MLXLMTests/Kernel/LLM/Gemma3nTextRoPEOffsetTests.swift. It copies the two recording tests of PR #184 without their withKnownIssue blocks:

  • cachedDecodeMatchesTheFullForwardPass (defect 1): no KV sharing; the prompt in chunks [5, 3, 1, 1, 1] with a cache must give the logits of the full pass within 1e-4.
  • kvSharedLayersGiveTheSameLogitsInChunks (defect 2): with KV sharing; the chunks [5, 3, 1, 1, 1] must give the logits of one chunk of 11 within 1e-4.

The tiny random model helper of PR #183/#184 (SyntheticModel, ForwardPassChecks) is not on main. I copied the minimal parts into Tests/MLXLMTests/Kernel/LLM/Gemma3nTextTinyModel.swift with a new type name (Gemma3nTextTinyModel), so it does not collide with #183/#184. The four Gemma 3n PRs (#215, #216, #217, #218) add the same file with the same content, so they merge without a conflict.

fork.yaml: new section "Gemma 3n text fixes" for Gemma3nText.swift and Tests/MLXLMTests/Kernel/LLM/Gemma3nText*.swift. The four PRs add the same section.

Before and after (local run, macOS, MLX_ENABLE_TF32=0)

  • Before (code on main): cachedDecodeMatchesTheFullForwardPass fails at all 5 steps. The cached logits differ from the full pass by 1.464, 1.358, 3.687, 1.349 and 1.054. kvSharedLayersGiveTheSameLogitsInChunks fails: the difference is 3.248.
  • After: both pass within 1e-4.

Merge note

After this PR and #184 both merge, remove the withKnownIssue blocks in Gemma3nTextForwardPassTests.cachedDecodeMatchesTheFullForwardPass and Gemma3nTextForwardPassTests.kvSharedLayersGiveTheSameLogitsInChunks in #184. The pull request that merges second (this one or #184) must remove the named withKnownIssue blocks in the same change. A known issue that is not intermittent and no longer fails is reported as a failure.

The sliding-mask fix (defect 3, #216) is stacked on this branch, because it changes the same lines of Gemma3nDecoderLayer.

Merge order (fork.yaml). This PR has a fork.yaml conflict with #274 and #248. After the first of these PRs merges, each other one needs a merge of main and a new Fork Tests run before it merges.

Checklist

  • I have read the CONTRIBUTING document
  • I have run swift format format --in-place --configuration .swift-format on the changed Swift files
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed): fork.yaml

🤖 Generated with Claude Code

The queries got RoPE after cache.update, so they used the offset after the
new tokens, while the keys used the offset before them. The KV-shared
layers also rotated their queries with the offset of a cache that the
source layer had already moved. Read the offset once from the first cache
before any layer runs, and use it for the queries and keys of every layer,
as mlx-lm gemma3n.py does.

Part of #195 (defects 1 and 2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Gajesh2007
Gajesh2007 merged commit 8c6d6f2 into main Oct 2, 2026
2 checks passed
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