Add release_kv_cache() API#44890
Conversation
Signed-off-by: Dakai An <dakaian108@gmail.com>
|
This pull request has merge conflicts that must be resolved before it can be |
|
Hi @andakai — this is closely related to #46438 ( The overlap:
The difference is scope: Would it make sense to build |
Sure. I did the allocator-level logic and the higher layers all in a single pr now. I checked we share the same logic betweeen |
|
Sounds good — I'll ping you once #46438 is merged so you can rebase on top of it. Happy to help review the integration on your side too. |
Motivation
Sleep level 2 can release KV cache memory, but it also discards more state than needed for cases where the model weights should stay resident. This PR provides a direct API for the common case where an upper-level framework wants to temporarily free KV cache memory, then later call
wake_up()and continue using the same model instance.Summary
This PR adds
release_kv_cache()API that releases KV cache memory while keeping model weights resident.The change is intentionally scoped to KV cache release only, rather than introducing generic tag-level memory management in the public API such as this pr did #42658.
Changes include:
LLM.release_kv_cache()and async/server plumbing for/release_kv_cache.release_tags()support for CUDA/XPU memory pools.release_kv_cache(),sleep(), andwake_up()are mixed.kv_cache_released.kv_cache_releasedonly when KV cache release actually happens.LLM.release_kv_cache()and dev server metrics.Similar work
#42658. This PR adds a
tagparameter tosleep, but its semantics are unclear—it is not obvious whether the tag represents discard or offload. This pr also proposed another approach, such as release(tag=..., offload_tag=...), this would introduce significant code changes and overlap with existing sleep levels. Based on my investigation, the current need is limited to releasing KV cache only, so we suggest adding a dedicated interface for this use case. More fine-grained tag management can be abstracted later if needed. The PR has already implemented arelease_taginterface incumem.py, which can be easily extended in the future.Tests
pytest tests/basic_correctness/test_mem.py -k release_kv_cache -v
Passed.