From 64bca5315f59ae6b7e57cf4d8a13180f0e7e518f Mon Sep 17 00:00:00 2001 From: Cheng Wan <54331508+ch-wan@users.noreply.github.com> Date: Thu, 19 Feb 2026 19:15:05 -0800 Subject: [PATCH] Fix long prompt KV allocation by falling back to torch native APIs when exceeding Triton tensor limit (#18250) --- python/sglang/srt/mem_cache/allocator.py | 34 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/python/sglang/srt/mem_cache/allocator.py b/python/sglang/srt/mem_cache/allocator.py index 9b6be180e..fa08bb66a 100644 --- a/python/sglang/srt/mem_cache/allocator.py +++ b/python/sglang/srt/mem_cache/allocator.py @@ -411,7 +411,7 @@ class PagedTokenToKVPoolAllocator(BaseTokenToKVPoolAllocator): self.seen_max_num_extend_tokens_next_power_of_2 = max( self.seen_max_num_extend_tokens_next_power_of_2, - next_power_of_2(extend_num_tokens), + min(tl.core.TRITON_MAX_TENSOR_NUMEL, next_power_of_2(extend_num_tokens)), ) bs = len(prefix_lens) @@ -423,16 +423,28 @@ class PagedTokenToKVPoolAllocator(BaseTokenToKVPoolAllocator): out_indices = torch.empty( (extend_num_tokens,), dtype=torch.int64, device=self.device ) - alloc_extend_kernel[(bs,)]( - prefix_lens, - seq_lens, - last_loc, - self.free_pages, - out_indices, - next_power_of_2(bs), - self.page_size, - self.seen_max_num_extend_tokens_next_power_of_2, - ) + + if extend_num_tokens < tl.core.TRITON_MAX_TENSOR_NUMEL: + alloc_extend_kernel[(bs,)]( + prefix_lens, + seq_lens, + last_loc, + self.free_pages, + out_indices, + next_power_of_2(bs), + self.page_size, + self.seen_max_num_extend_tokens_next_power_of_2, + ) + else: + alloc_extend_naive( + prefix_lens, + seq_lens, + last_loc, + self.free_pages, + out_indices, + self.page_size, + self.device, + ) if self.debug_mode: assert len(torch.unique(out_indices)) == len(out_indices)