Repository navigation
Conversation
trim() lowered the offset but kept the keys and values, so the next update_and_fetch concatenated onto the untrimmed arrays and reset the offset to their full length. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This branch has not been deployed
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.
I serve models using MLX-LM on my M5 Max Macbook Pro with 48GB of memory. I have them lean on cache rollback, and with speculative decoding I looked at using the trim function to drop rejected tokens. I also trim saved prompt cache back to the common prefix when a new request shares only part of an earlier prompt. I found that ConcatenateKVCache.trim(n) does only self.offset -= n , while leaving keys and values at full length. Other caches do not have this problem as they write at offset into a preallocated buffer and returns a slice up to offset.
ConcatenateKVCache works differently: update_and_fetch calls mx.concatenate([self.keys, keys], axis=-2) and sets self.offset = self.keys.shape[-2] . keys_and_values() returns the stored arrays unsliced. At the next update, trimmed entries are back in keys + values and the offset is reset to count them so the trim is undone.
This affects afm7 which puts this cache on the last layer.
This PR has trim(), after lowering the offset, slice keys and values to [..., :self.offset, :] when n > 0. It also adds a test.