From 15c801f726cc29d8a344725bf2d4e76d0b647e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bernl=C3=B6hr?= Date: Tue, 9 Jun 2026 04:43:54 +0200 Subject: [PATCH] fix(server): clamp piecewise_cuda_graph_max_tokens to context_length (#22516) Co-authored-by: XueLiang Yang <102161631+xueliangyang-oeuler@users.noreply.github.com> --- python/sglang/srt/server_args.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/sglang/srt/server_args.py b/python/sglang/srt/server_args.py index 5fdf50ab3..a93b26803 100644 --- a/python/sglang/srt/server_args.py +++ b/python/sglang/srt/server_args.py @@ -1576,6 +1576,14 @@ class ServerArgs: self.piecewise_cuda_graph_max_tokens, 4096 ) + # Clamp to context_length if explicitly set — prevents PCG warmup + # from compiling graphs with more tokens than the model buffers + # can hold, which causes illegal memory access (#21112) + if self.context_length is not None: + self.piecewise_cuda_graph_max_tokens = min( + self.piecewise_cuda_graph_max_tokens, self.context_length + ) + if self.piecewise_cuda_graph_tokens is None: self.piecewise_cuda_graph_tokens = ( self._generate_piecewise_cuda_graph_tokens()