From 19f4859b30843f2df3a9c0db1859f10591c2a8ec Mon Sep 17 00:00:00 2001 From: Liangsheng Yin Date: Fri, 17 Jul 2026 02:11:41 -0700 Subject: [PATCH] [CI] Exclude current process memory from GPU idle check (#31571) --- python/sglang/test/test_utils.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/python/sglang/test/test_utils.py b/python/sglang/test/test_utils.py index cf95676d2..773b1c888 100644 --- a/python/sglang/test/test_utils.py +++ b/python/sglang/test/test_utils.py @@ -768,6 +768,11 @@ def _subprocess_popen_with_outputs( env: Optional[dict], return_stdout_stderr: Optional[tuple], ) -> subprocess.Popen: + # Release allocator-cached GPU memory to the driver before spawning a + # server: cached blocks stay cudaMalloc'd and shrink the child's memory. + if torch.cuda.is_initialized(): + torch.cuda.empty_cache() + if not return_stdout_stderr: return subprocess.Popen(command, stdout=None, stderr=None, env=env) @@ -2201,6 +2206,7 @@ def _visible_gpu_indices(pynvml) -> List[int]: def _collect_busy_gpu_reports(pynvml, gpu_indices: List[int]) -> List[str]: + self_pid = os.getpid() reports = [] for index in gpu_indices: handle = pynvml.nvmlDeviceGetHandleByIndex(index) @@ -2209,10 +2215,25 @@ def _collect_busy_gpu_reports(pynvml, gpu_indices: List[int]) -> List[str]: continue try: procs = pynvml.nvmlDeviceGetComputeRunningProcesses(handle) - proc_info = ", ".join( - f"pid={proc.pid} {_format_gib(proc.usedGpuMemory)}" for proc in procs - ) except pynvml.NVMLError: + procs = None + if procs is not None: + # Discount our own usage: the caching allocator retains memory + # across test classes, and waiting on ourselves never succeeds. + self_used = sum( + proc.usedGpuMemory or 0 for proc in procs if proc.pid == self_pid + ) + if used_bytes - self_used < _GPU_IDLE_USED_MEMORY_THRESHOLD: + continue + proc_info = ", ".join( + f"pid={proc.pid} {_format_gib(proc.usedGpuMemory)}" + for proc in procs + if proc.pid != self_pid + ) or ( + f"no other compute processes;" + f" self pid={self_pid} holds {_format_gib(self_used)}" + ) + else: proc_info = "" reports.append( f"GPU {index} uses {_format_gib(used_bytes)}"