[srt] Batch scheduler cache frees (#33475)
This commit is contained in:
@@ -3232,6 +3232,7 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
|
||||
|
||||
eviction_interval = max(1, envs.SGLANG_SWA_EVICTION_INTERVAL.get())
|
||||
swa_maintenance_step = (self.forward_iter or 0) % eviction_interval == 0
|
||||
self.token_to_kv_pool_allocator.free_group_begin()
|
||||
for idx, req in enumerate(self.reqs):
|
||||
if self.forward_mode.is_decode():
|
||||
# We set evict_swa condition here with two reasons:
|
||||
@@ -3276,6 +3277,7 @@ class ScheduleBatch(ScheduleBatchDisaggregationDecodeMixin):
|
||||
self._evict_swa(req, pre_len)
|
||||
else:
|
||||
self._evict_swa(req, pre_len)
|
||||
self.token_to_kv_pool_allocator.free_group_end()
|
||||
|
||||
def _evict_swa(self, req: Req, pre_len: int):
|
||||
assert self.tree_cache.supports_swa(), "prefix cache must support swa"
|
||||
|
||||
@@ -195,6 +195,7 @@ class SchedulerBatchResultProcessor:
|
||||
result: Union[GenerationBatchResult, EmbeddingBatchResult],
|
||||
):
|
||||
skip_stream_req = None
|
||||
self.token_to_kv_pool_allocator.free_group_begin()
|
||||
|
||||
if self.is_generation:
|
||||
if result.copy_done is not None:
|
||||
@@ -360,6 +361,7 @@ class SchedulerBatchResultProcessor:
|
||||
req.inflight_middle_chunks -= 1
|
||||
req.time_stats.set_last_chunked_prefill_finish_time()
|
||||
|
||||
self.token_to_kv_pool_allocator.free_group_end()
|
||||
self.output_streamer.stream_output(
|
||||
batch.reqs, batch.return_logprob, skip_stream_req
|
||||
)
|
||||
|
||||
@@ -95,6 +95,7 @@ class SWATokenToKVPoolAllocator(BaseTokenToKVPoolAllocator):
|
||||
self.release_pages = None
|
||||
self.is_not_in_free_group = True
|
||||
self.free_group = []
|
||||
self.swa_free_group = []
|
||||
|
||||
self._kvcache = kvcache
|
||||
self.clear()
|
||||
@@ -348,6 +349,10 @@ class SWATokenToKVPoolAllocator(BaseTokenToKVPoolAllocator):
|
||||
if free_index.numel() == 0:
|
||||
return
|
||||
|
||||
if not self.is_not_in_free_group:
|
||||
self.swa_free_group.append(free_index)
|
||||
return
|
||||
|
||||
if self.page_size == 1:
|
||||
mapping_indices = free_index
|
||||
else:
|
||||
@@ -358,6 +363,17 @@ class SWATokenToKVPoolAllocator(BaseTokenToKVPoolAllocator):
|
||||
self.swa_attn_allocator.free(swa_indices)
|
||||
self.full_to_swa_index_mapping[mapping_indices] = 0
|
||||
|
||||
def free_group_begin(self):
|
||||
super().free_group_begin()
|
||||
self.swa_free_group = []
|
||||
|
||||
def free_group_end(self):
|
||||
super().free_group_end()
|
||||
if self.swa_free_group:
|
||||
swa_free_group = self.swa_free_group
|
||||
self.swa_free_group = []
|
||||
self.free_swa(torch.cat(swa_free_group))
|
||||
|
||||
def _expand_to_full_pages(self, indices: torch.Tensor) -> torch.Tensor:
|
||||
pages = torch.unique(indices // self.page_size)
|
||||
page_offsets = torch.arange(
|
||||
@@ -386,6 +402,7 @@ class SWATokenToKVPoolAllocator(BaseTokenToKVPoolAllocator):
|
||||
self.full_to_swa_index_mapping[:-1].fill_(0)
|
||||
self.is_not_in_free_group = True
|
||||
self.free_group = []
|
||||
self.swa_free_group = []
|
||||
|
||||
def get_cpu_copy(self, indices, mamba_indices=None):
|
||||
return self._kvcache.get_cpu_copy(indices, mamba_indices=mamba_indices)
|
||||
@@ -489,7 +506,10 @@ class PureSWATokenToKVPoolAllocator(SWATokenToKVPoolAllocator):
|
||||
def free_swa(self, free_index: torch.Tensor):
|
||||
if free_index.numel() == 0:
|
||||
return
|
||||
self.swa_attn_allocator.free(free_index[free_index > 0])
|
||||
if self.is_not_in_free_group:
|
||||
self.swa_attn_allocator.free(free_index[free_index > 0])
|
||||
else:
|
||||
self.free_group.append(free_index)
|
||||
|
||||
def free_group_begin(self):
|
||||
self.is_not_in_free_group = False
|
||||
|
||||
@@ -511,8 +511,10 @@ class RadixCache(KVCacheEventMixin, BasePrefixCache):
|
||||
)
|
||||
new_prefix_len = result.prefix_len
|
||||
|
||||
# Use the out-of-place values copy so the allocator can safely defer or group
|
||||
# this free after req_to_token is overwritten below.
|
||||
self.token_to_kv_pool_allocator.free_segment(
|
||||
kv_indices[req.cache_protected_len : new_prefix_len],
|
||||
values[req.cache_protected_len : new_prefix_len],
|
||||
start_pos=req.cache_protected_len,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user