gemma4_31b (MLX): don't allocate a second KV-cache copy for single-session runs#20560
Open
metascroy wants to merge 1 commit into
Open
gemma4_31b (MLX): don't allocate a second KV-cache copy for single-session runs#20560metascroy wants to merge 1 commit into
metascroy wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20560
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 Unclassified FailuresAs of commit 74e77f5 with merge base 51729bb ( UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
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.
Since #20473, the MLX engine always enables the multi-session mutable-state path, so create_session() allocates a per-session KV-cache copy on top of the program's default buffers — which rebind never uses again once a session exists — leaving single-session runs holding two full KV caches. This gates the MLX mutable_state creation on config.max_sessions > 1, so the single-session CLI runner (max_sessions = 1) executes against the default buffers (one KV cache, as before #20473) while the multi-session worker is unchanged. Result: lower peak memory and recovered prefill tok/s for single-session runs (significant at long context); decode is unaffected. Test: make gemma4_31b-mlx then run the runner — peak memory drops and prefill recovers, multi-session isolation still works.
Follow-up: This only fixes the single-session case. Under multi-session the program's default mutable buffers are still allocated but go unused once any session is created, so the worker pays one extra dead KV-cache copy (N+1 for N sessions). A follow-up could release or donate the default buffers to the first session under multi-session, with a check that the init chain only zero-initializes KV state (per-session buffers are freshly zeroed and don't replay it).