Repository navigation
fix: use one RoPE offset per Gemma3nText forward pass - #215
Merged
Merged
Conversation
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>
This was referenced Sep 30, 2026
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.
Proposed changes
Part of #195: defects 1 and 2. One RoPE offset for each forward pass of
Gemma3nText.Defects
Libraries/MLXLLM/Models/Gemma3nText.swift:304with:292and:299. The keys get RoPE (line 292) beforecache.update(line 299). The queries get RoPE aftercache.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.Gemma3nText.swift:269-273with: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-lmb1b358f1, #280: onecache?.ropeOffsetper 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(commita9bd8af):gemma3n.py:495: the language model readsoffset = cache[0].offsetonce, before any layer runs.gemma3n.py:511: it passes that offset to every layer.gemma3n.py:139and: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
Gemma3nRoPEOffset(a scalar, or an array for the compile-safe and batched caches throughgraphOffsetArray(for:)).Gemma3nLanguageModelreads it once from the first cache, before layer 0 updates its cache, and passes it throughGemma3nDecoderLayertoGemma3nAttention.Gemma3nAttentionrotates 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 theirwithKnownIssueblocks: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 onmain. I copied the minimal parts intoTests/MLXLMTests/Kernel/LLM/Gemma3nTextTinyModel.swiftwith 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" forGemma3nText.swiftandTests/MLXLMTests/Kernel/LLM/Gemma3nText*.swift. The four PRs add the same section.Before and after (local run, macOS, MLX_ENABLE_TF32=0)
main):cachedDecodeMatchesTheFullForwardPassfails at all 5 steps. The cached logits differ from the full pass by 1.464, 1.358, 3.687, 1.349 and 1.054.kvSharedLayersGiveTheSameLogitsInChunksfails: the difference is 3.248.Merge note
After this PR and #184 both merge, remove the
withKnownIssueblocks inGemma3nTextForwardPassTests.cachedDecodeMatchesTheFullForwardPassandGemma3nTextForwardPassTests.kvSharedLayersGiveTheSameLogitsInChunksin #184. The pull request that merges second (this one or #184) must remove the namedwithKnownIssueblocks 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 afork.yamlconflict with #274 and #248. After the first of these PRs merges, each other one needs a merge ofmainand a new Fork Tests run before it merges.Checklist
swift format format --in-place --configuration .swift-formaton the changed Swift filesfork.yaml🤖 Generated with Claude Code