From e5a1c5a4231694625b6a0c3a2cfd4032d32e5107 Mon Sep 17 00:00:00 2001 From: Cheng Wan <54331508+ch-wan@users.noreply.github.com> Date: Wed, 26 Aug 2026 16:17:02 -0700 Subject: [PATCH] Fix _is_compiling dynamo tracing: import torch instead of sys.modules lookup (#36573) --- python/sglang/srt/runtime_context.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/python/sglang/srt/runtime_context.py b/python/sglang/srt/runtime_context.py index 554ba1be1..4b59bb806 100644 --- a/python/sglang/srt/runtime_context.py +++ b/python/sglang/srt/runtime_context.py @@ -1265,9 +1265,12 @@ _RECORD_DUMP_REGISTERED = False def _is_compiling() -> bool: # Recording has Python side effects (set mutation, file I/O, atexit) that # must never run under tracing; torch.compiler.is_compiling() is dynamo's - # sanctioned probe. The lazy lookup keeps this module import-light. - torch = sys.modules.get("torch") - return torch is not None and torch.compiler.is_compiling() + # sanctioned probe. The function-level import keeps this module + # import-light; a sys.modules lookup here breaks fullgraph tracing (dynamo + # enumerates the dict, which other imports mutate mid-trace). + import torch + + return torch.compiler.is_compiling() def _ensure_record_dump_registered() -> None: