[Frontend][Core] Add release_kv_cache_memory() API (#44890)

Signed-off-by: Dakai An <dakaian108@gmail.com>
Signed-off-by: AlanFokCo <alanfok2868@gmail.com>
Signed-off-by: aoshen02 <aoshen@inferact.ai>
Signed-off-by: Dakai An <77474977+andakai@users.noreply.github.com>
Co-authored-by: AlanFokCo <alanfok2868@gmail.com>
Co-authored-by: aoshen02 <aoshen@inferact.ai>
Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
Dakai An
2026-09-18 07:12:12 +08:00
committed by GitHub
co-authored by AlanFokCo aoshen02 OpenAI Codex
parent a524f80b1f
commit 092bdd6d57
24 changed files with 574 additions and 44 deletions
+9 -2
View File
@@ -1079,10 +1079,17 @@ class AsyncLLM(EngineClient):
if self.logger_manager is not None:
self.logger_manager.record_sleep_state(1, level)
async def wake_up(self, tags: list[str] | None = None) -> None:
await self.engine_core.wake_up_async(tags)
async def release_kv_cache_memory(self) -> None:
await self.renderer.clear_mm_cache_async()
await self.engine_core.release_kv_cache_memory_async()
if self.logger_manager is not None:
self.logger_manager.record_sleep_state(1, 0)
async def wake_up(self, tags: list[str] | None = None) -> None:
fully_awake = await self.engine_core.wake_up_async(tags)
if self.logger_manager is not None and fully_awake:
self.logger_manager.record_sleep_state(0, 0)
async def checkpoint_prepare(self) -> None:
+27 -2
View File
@@ -957,12 +957,15 @@ class EngineCore:
pause_future.add_done_callback(pause_complete)
return future
def wake_up(self, tags: list[str] | None = None):
def wake_up(self, tags: list[str] | None = None) -> bool:
"""Wake up the engine from sleep.
Args:
tags: Tags to wake up. Use ["scheduling"] for level 0 wake up.
Returns:
Whether all executor memory is resident again (fully awake).
"""
if tags is not None and "scheduling" in tags:
# Remove "scheduling" from tags if there are other tags to process.
@@ -973,8 +976,30 @@ class EngineCore:
# Partial wakes intentionally keep the remaining allocations asleep.
# Resume scheduling only once all executor memory is resident again.
if not self.model_executor.is_sleeping:
fully_awake = not self.model_executor.is_sleeping
if fully_awake:
self.resume_scheduler()
return fully_awake
def release_kv_cache_memory(self) -> None:
"""Discard KV cache physical memory. Requires a completed pause
and all executor memory to be resident. Kept requests are recomputed
after wake-up.
"""
if not (
self.is_scheduler_paused()
and not self.scheduler.has_requests()
and not self.batch_queue
):
raise RuntimeError(
"release_kv_cache_memory() requires a completed pause first"
)
if self.model_executor.is_sleeping:
raise RuntimeError(
"release_kv_cache_memory() requires all executor memory to be resident"
)
self._reset_caches()
self.model_executor.discard(("kv_cache",))
def is_sleeping(self) -> bool:
"""Check if engine is sleeping at any level."""
+23 -8
View File
@@ -193,7 +193,10 @@ class EngineCoreClient(ABC):
def sleep(self, level: int = 1, mode: PauseMode = "abort") -> None:
raise NotImplementedError
def wake_up(self, tags: list[str] | None = None) -> None:
def release_kv_cache_memory(self) -> None:
raise NotImplementedError
def wake_up(self, tags: list[str] | None = None) -> bool:
raise NotImplementedError
def is_sleeping(self) -> bool:
@@ -285,7 +288,10 @@ class EngineCoreClient(ABC):
async def sleep_async(self, level: int = 1, mode: PauseMode = "abort") -> None:
raise NotImplementedError
async def wake_up_async(self, tags: list[str] | None = None) -> None:
async def release_kv_cache_memory_async(self) -> None:
raise NotImplementedError
async def wake_up_async(self, tags: list[str] | None = None) -> bool:
raise NotImplementedError
async def is_sleeping_async(self) -> bool:
@@ -397,8 +403,11 @@ class InprocClient(EngineCoreClient):
result = self.engine_core.sleep(level, mode)
assert result is None
def wake_up(self, tags: list[str] | None = None) -> None:
self.engine_core.wake_up(tags)
def release_kv_cache_memory(self) -> None:
self.engine_core.release_kv_cache_memory()
def wake_up(self, tags: list[str] | None = None) -> bool:
return self.engine_core.wake_up(tags)
def is_sleeping(self) -> bool:
return self.engine_core.is_sleeping()
@@ -1021,8 +1030,11 @@ class SyncMPClient(MPClient):
def sleep(self, level: int = 1, mode: PauseMode = "abort") -> None:
self.call_utility("sleep", level, mode)
def wake_up(self, tags: list[str] | None = None) -> None:
self.call_utility("wake_up", tags)
def release_kv_cache_memory(self) -> None:
self.call_utility("release_kv_cache_memory")
def wake_up(self, tags: list[str] | None = None) -> bool:
return self.call_utility("wake_up", tags)
def is_sleeping(self) -> bool:
return self.call_utility("is_sleeping")
@@ -1265,8 +1277,11 @@ class AsyncMPClient(MPClient):
async def sleep_async(self, level: int = 1, mode: PauseMode = "abort") -> None:
await self.call_utility_async("sleep", level, mode)
async def wake_up_async(self, tags: list[str] | None = None) -> None:
await self.call_utility_async("wake_up", tags)
async def release_kv_cache_memory_async(self) -> None:
await self.call_utility_async("release_kv_cache_memory")
async def wake_up_async(self, tags: list[str] | None = None) -> bool:
return await self.call_utility_async("wake_up", tags)
async def is_sleeping_async(self) -> bool:
return await self.call_utility_async("is_sleeping")
+9 -2
View File
@@ -375,10 +375,17 @@ class LLMEngine:
if self.logger_manager is not None:
self.logger_manager.record_sleep_state(1, level)
def wake_up(self, tags: list[str] | None = None):
self.engine_core.wake_up(tags)
def release_kv_cache_memory(self) -> None:
self.renderer.clear_mm_cache()
self.engine_core.release_kv_cache_memory()
if self.logger_manager is not None:
self.logger_manager.record_sleep_state(1, 0)
def wake_up(self, tags: list[str] | None = None):
fully_awake = self.engine_core.wake_up(tags)
if self.logger_manager is not None and fully_awake:
self.logger_manager.record_sleep_state(0, 0)
def is_sleeping(self) -> bool: