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()