Repository navigation
Remove temporal coupling in applyRotaryPosition - #234
Closed
DePasqualeOrg wants to merge 1 commit into
Closed
DePasqualeOrg wants to merge 1 commit into
DePasqualeOrg wants to merge 1 commit into
Conversation
Contributor
|
Thanks @DePasqualeOrg |
2 of 4 tasks
davidkoski
approved these changes
May 7, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Very interesting -- thank you for the fix!
davidkoski
reviewed
May 7, 2026
Comment on lines
+46
to
+50
| if let batched = self as? BatchPositionedKVCache { | ||
| return .array(batched.batchOffset + 0) | ||
| } else { | ||
| return .scalar(offset) | ||
| } |
Member
There was a problem hiding this comment.
I don't know if it matters, but see also:
- fix #364 -- x[.ellipsis] returns self instead of a "copy" mlx-swift#367
- [BUG] x[.ellipsis] returns self instead of a "copy" mlx-swift#364
I don't know if batchOffset[.ellipsis] is more idiomatic or cheaper. The comment explains what it is doing well, enough.
4 tasks done
davidtai
added a commit
to davidtai/mlx-swift-lm
that referenced
this pull request
Oct 6, 2026
ChunkedKVCache.maybeTrimFront() compared the buffer length with the chunk size and kept the last chunkSize buffer rows. The buffer grows in steps, so its last rows can be unused. The cache then kept fewer valid tokens than mlx-lm, or moved startPosition past the offset. The trim now counts the valid tokens (offset - startPosition) and keeps the newest chunkSize of them, as mlx-lm cache.py does. Fixes ml-explore#234 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
applyRotaryPosition(_:to:cache:), which was introduced in #178 (cc @ronaldmannak), readscache.offsetimplicitly. Becausecache.update(...)advances the offset, the helper's result depends on call ordering relative to cache updates, and that dependency is invisible at the call site.44 of the 45 attention layers using this helper call it for queries and keys back to back before any
cache.update, so both reads return the same value and the coupling stays hidden. Gemma 3n is the exception: its KV-sharing branch callscache.updatebetween the key and query RoPE calls, so queries get rotated atcache.offset + Lwhile keys usecache.offset. That mismatch produces garbled output (see #233).Gemma 4 had the same interleaving and worked around it with file-local helpers that corresponded to this PR's API, which is now uniformly applied.
MLX LM in Python avoids this by making
offsetan explicit argument tonn.RoPE. Callers that interleavecache.update_and_fetchsnapshotoffset = cache.offsetfirst, making the timing visible.Solution
Make the offset explicit in Swift too. Preserve the batch polymorphism the helper was introduced for, but move the cache read out of the helper and into a one-line snapshot at the top of each
callAsFunction.New API (additive)
In
Libraries/MLXLMCommon/RoPEApplication.swift, add aRoPEOffsetenum (.scalar(Int)/.array(MLXArray)), aKVCache.ropeOffsetsnapshot property that defensively copies the batched variant, and a newapplyRotaryPosition(_:to:offset:)overload.Deprecated shim
Mark the existing
applyRotaryPosition(_:to:cache:)@available(*, deprecated)and forward it to the new overload viacache?.ropeOffset ?? .scalar(0), so downstream callers get a warning, not a compile error, during migration.Call site example
Before:
After:
The snapshot goes at the top of
callAsFunction, or at the narrowest scope containing the RoPE calls when branches or conditionals require it (Gemma 3n, GPTOSS, and AfMoE).