Skip to content
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
3 changes: 3 additions & 0 deletions python/sglang/srt/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ class Envs:
# page tables (DP attn); paged backends like trtllm_mha consume it directly.
SGLANG_USE_HND_KVCACHE = EnvBool(False)

# size the KV pool after CUDA-graph capture
SGLANG_ENABLE_POST_CAPTURE_KV_SIZING = EnvBool(False)

# Scheduler: memory leak test
SGLANG_TEST_RETRACT = EnvBool(False)
SGLANG_TEST_RETRACT_INTERVAL = EnvInt(3)
Expand Down
7 changes: 5 additions & 2 deletions python/sglang/srt/managers/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,13 +866,16 @@ def init_model_worker(self):
self.init_tp_model_worker()
self.maybe_init_draft_worker()

# Allocate KV cache pools for all workers.
# Prepare KV cache pools for all workers
self.init_memory_pools()

# TODO: make memory profile consider cuda graph memory as well
self.init_all_attention_backends()
self.init_all_cuda_graphs()

model_runner = self.tp_worker.model_runner
if model_runner.token_to_kv_pool.post_capture_active:
model_runner.post_capture_resize_kv_pool()

# Dispatch the model worker
if self.spec_algorithm.is_none():
self.model_worker = self.tp_worker
Expand Down
6 changes: 6 additions & 0 deletions python/sglang/srt/mem_cache/allocator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def alloc_extend(self, *args, **kwargs):
def alloc_decode(self, *args, **kwargs):
raise NotImplementedError("alloc_decode is only for paged allocator")

def resize(self, config) -> None:
self.size = config.max_total_num_tokens
if self.page_size > 1:
self.num_pages = config.max_total_num_tokens // self.page_size
self.clear()

@abc.abstractmethod
def clear(self):
raise NotImplementedError()
Expand Down
14 changes: 14 additions & 0 deletions python/sglang/srt/mem_cache/allocator/swa.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,20 @@ def restore_state(self, state):
self.full_attn_allocator.restore_state(state[0])
self.swa_attn_allocator.restore_state(state[1])

def resize(self, config) -> None:
size_full = int(config.full_max_total_num_tokens)
size_swa = int(config.swa_max_total_num_tokens)
self._size_full = size_full
self._size_swa = size_swa
for alloc, sz in (
(self.full_attn_allocator, size_full),
(self.swa_attn_allocator, size_swa),
):
alloc.size = int(sz)
if self.page_size > 1:
alloc.num_pages = int(sz) // self.page_size
self.clear()

def clear(self):
self.swa_attn_allocator.clear()
self.full_attn_allocator.clear()
Expand Down
Loading
Loading