From 13a4a0406eecb8bcd85c1a3994fc89ec386b42b0 Mon Sep 17 00:00:00 2001 From: Cheng Wan <54331508+ch-wan@users.noreply.github.com> Date: Thu, 19 Feb 2026 20:02:21 -0800 Subject: [PATCH] Fix flashinfer autotune to only wrap run_once() (#19004) --- python/sglang/srt/model_executor/model_runner.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/python/sglang/srt/model_executor/model_runner.py b/python/sglang/srt/model_executor/model_runner.py index 094b1d317..d451c614a 100644 --- a/python/sglang/srt/model_executor/model_runner.py +++ b/python/sglang/srt/model_executor/model_runner.py @@ -152,6 +152,7 @@ from sglang.srt.utils import ( MultiprocessingSerializer, cpu_has_amx_support, dynamic_import, + empty_context, enable_show_time_cost, get_available_gpu_memory, get_cpu_ids_by_node, @@ -1861,12 +1862,14 @@ class ModelRunner(ModelRunnerKVCacheMixin): logger.info("Running FlashInfer autotune...") - with torch.inference_mode(), autotune(): - self._dummy_run(batch_size=self.req_to_token_pool.size) + self._dummy_run( + batch_size=self.req_to_token_pool.size, + run_ctx=autotune(), + ) logger.info("FlashInfer autotune completed.") - def _dummy_run(self, batch_size: int): + def _dummy_run(self, batch_size: int, run_ctx=None): """Run a dummy forward pass for warmup/profiling.""" if self.is_generation: capture_forward_mode = ForwardMode.DECODE @@ -2106,7 +2109,8 @@ class ModelRunner(ModelRunnerKVCacheMixin): torch.get_device_module(self.device).synchronize() self.tp_group.barrier() - run_once() + with torch.inference_mode(), run_ctx or empty_context(): + run_once() def init_device_graphs(self): """Capture device graphs."""