Skip to content

Tiny add Engine.flush_cache API #5241

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

Merged
merged 12 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/sglang/srt/entrypoints/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ def __exit__(self, exc_type, exc_value, traceback):
self.shutdown()
return False

def flush_cache(self):
loop = asyncio.get_event_loop()
return loop.run_until_complete(self.tokenizer_manager.flush_cache())

def start_profile(self):
loop = asyncio.get_event_loop()
loop.run_until_complete(self.tokenizer_manager.start_profile())
Expand Down
4 changes: 2 additions & 2 deletions python/sglang/srt/entrypoints/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ async def classify_request(obj: EmbeddingReqInput, request: Request):
@app.api_route("/flush_cache", methods=["GET", "POST"])
async def flush_cache():
"""Flush the radix cache."""
_global_state.tokenizer_manager.flush_cache()
ret = await _global_state.tokenizer_manager.flush_cache()
return Response(
content="Cache flushed.\nPlease check backend logs for more details. "
"(When there are running or waiting requests, the operation will not be performed.)\n",
status_code=200,
status_code=200 if ret.success else HTTPStatus.BAD_REQUEST,
)


Expand Down
7 changes: 6 additions & 1 deletion python/sglang/srt/managers/io_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,15 @@ class BatchEmbeddingOut:


@dataclass
class FlushCacheReq:
class FlushCacheReqInput:
pass


@dataclass
class FlushCacheReqOutput:
success: bool


@dataclass
class UpdateWeightFromDiskReqInput:
# The model path with the new weights
Expand Down
10 changes: 6 additions & 4 deletions python/sglang/srt/managers/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
CloseSessionReqInput,
ExpertDistributionReq,
ExpertDistributionReqOutput,
FlushCacheReq,
FlushCacheReqInput,
FlushCacheReqOutput,
GetInternalStateReq,
GetInternalStateReqOutput,
GetWeightsByNameReqInput,
Expand Down Expand Up @@ -400,7 +401,7 @@ def __init__(
[
(TokenizedGenerateReqInput, self.handle_generate_request),
(TokenizedEmbeddingReqInput, self.handle_embedding_request),
(FlushCacheReq, self.flush_cache_wrapped),
(FlushCacheReqInput, self.flush_cache_wrapped),
(AbortReq, self.abort_request),
(OpenSessionReqInput, self.open_session),
(CloseSessionReqInput, self.close_session),
Expand Down Expand Up @@ -1652,8 +1653,9 @@ def watchdog_thread(self):
time.sleep(5)
self.parent_process.send_signal(signal.SIGQUIT)

def flush_cache_wrapped(self, recv_req: FlushCacheReq):
self.flush_cache()
def flush_cache_wrapped(self, recv_req: FlushCacheReqInput):
success = self.flush_cache()
return FlushCacheReqOutput(success=success)

def flush_cache(self):
"""Flush the memory pool and cache."""
Expand Down
15 changes: 11 additions & 4 deletions python/sglang/srt/managers/tokenizer_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
EmbeddingReqInput,
ExpertDistributionReq,
ExpertDistributionReqOutput,
FlushCacheReq,
FlushCacheReqInput,
FlushCacheReqOutput,
GenerateReqInput,
GetInternalStateReq,
GetInternalStateReqOutput,
Expand Down Expand Up @@ -258,6 +259,9 @@ def __init__(
self.resume_memory_occupation_communicator = _Communicator(
self.send_to_scheduler, server_args.dp_size
)
self.flush_cache_communicator = _Communicator(
self.send_to_scheduler, server_args.dp_size
)
self.start_profile_communicator = _Communicator(
self.send_to_scheduler, server_args.dp_size
)
Expand Down Expand Up @@ -308,6 +312,10 @@ def __init__(
ResumeMemoryOccupationReqOutput,
self.resume_memory_occupation_communicator.handle_recv,
),
(
FlushCacheReqOutput,
self.flush_cache_communicator.handle_recv,
),
(
ProfileReqOutput,
self.start_profile_communicator.handle_recv,
Expand Down Expand Up @@ -616,9 +624,8 @@ async def _handle_batch_request(
except StopAsyncIteration:
pass

def flush_cache(self):
req = FlushCacheReq()
self.send_to_scheduler.send_pyobj(req)
async def flush_cache(self) -> FlushCacheReqOutput:
return await self.flush_cache_communicator(FlushCacheReqInput())

def abort_request(self, rid: str):
if rid not in self.rid_to_state:
Expand Down
Loading