Commit 1acb0da
authored
gemma4_31b (MLX): don't allocate a second KV-cache copy for single-session runs (#20560)
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).1 parent 2af5638 commit 1acb0da
1 file changed
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
684 | 684 | | |
685 | 685 | | |
686 | 686 | | |
687 | | - | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
688 | 696 | | |
689 | 697 | | |
690 | 698 | | |
| |||
0 commit comments