-
Notifications
You must be signed in to change notification settings - Fork 70
Cache reuse and cache fixes #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
christian-lms
wants to merge
40
commits into
lmstudio-ai:main
Choose a base branch
from
christian-lms:christian/cache_reuse_again
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Cache reuse and cache fixes #188
christian-lms
wants to merge
40
commits into
lmstudio-ai:main
from
christian-lms:christian/cache_reuse_again
Conversation
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
…y (had to pipe in is_key)
…/v at once, which bypasses the former stupid hack
All contributors have signed the CLA ✍️ ✅ |
acf168b
to
1c4cf24
Compare
Closed
Fixes are now going in #192 |
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.
This PR does two things (for text-only models): fix a bunch of bugs with the KV cache implementation and bring cache reuse to MLX. The two are so intertwined that it ended up being easiest to put them both in one PR.
To the former, this PR closes #177, #179, and another undocumented bug:
To the latter, we spin our own variant of MLX-LM's
RotatingKVCache
to allow us to trim it properly and Unlike the llama.cpp implementation, this does not RoPE shift reused chunks. (Actually, when I tried RoPE shifting, it gave gibberish responses.) This aligns more closely with what MLX-LM does under the hood (they're fine with discontinuous positional embeddings), so I'm inclined to believe this is fine, though this will need testing.We also don't change the
offset
of the KV cache at all, which tells the LLM's positional embeddings where we are in the overall sequence. Again, this aligns with what MLX-LM does, and will need testing to validate.Known issues that remain as of posting include:
<think></think>
block being counted inn_keep
. This PR fixes the previous behavior (Qwen3 cache wastage #176) whereby the last generation would get nuked, but the whole cache still gets nuked when the context overflows due to a miscountedn_keep
retaining tokens from the assistant message it wasn't supposed to and consequently preventing later sections from being reused. This may not be a blocker on account of the fact that it's not technically regressing and we know where the behavior comes from.makeCache
function despite having no good reason to do so. These aren't very popular ones, so we can either 1) monkeypatch this, 2) upstream a change into MLX-LM, or 3) ignore the issue altogether.Technically the code is mergeable, but I want to do more testing before we finalize this.
cc @mattjcly @neilmehta24 @yagil