From af119619c4ca2090e0fac310867199f8fadec3a6 Mon Sep 17 00:00:00 2001 From: Minjang Kim Date: Mon, 24 Aug 2026 21:13:13 -0700 Subject: [PATCH] Exclude the cpu backend from vLLM's active-Triton-driver count (#53530) (#53530) Signed-off-by: Minjang Kim Co-authored-by: Autopilot Bot Co-authored-by: OpenAI Codex --- tests/test_triton_utils.py | 51 ++++++++++++++++++++++++++++++++++ vllm/triton_utils/importing.py | 9 +++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/tests/test_triton_utils.py b/tests/test_triton_utils.py index 7fe0a5d9c5..0ae4c0d6d6 100644 --- a/tests/test_triton_utils.py +++ b/tests/test_triton_utils.py @@ -1,13 +1,46 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project +import importlib import sys import types from unittest import mock +from vllm.triton_utils import importing as triton_importing from vllm.triton_utils.importing import TritonLanguagePlaceholder, TritonPlaceholder +def _has_triton_for_backends(**drivers: bool) -> bool: + """Re-evaluate ``HAS_TRITON`` against a synthetic ``triton.backends`` map. + + ``drivers`` maps backend name to whether its driver reports itself active. + """ + backends = {} + for name, is_active in drivers.items(): + driver = mock.Mock() + driver.is_active.return_value = is_active + backend = mock.Mock() + backend.driver = driver + backends[name] = backend + + triton_mod = types.ModuleType("triton") + triton_mod.__spec__ = importlib.machinery.ModuleSpec("triton", None) + backends_mod = types.ModuleType("triton.backends") + backends_mod.__dict__["backends"] = backends + triton_mod.__dict__["backends"] = backends_mod + + patched_modules = {"triton": triton_mod, "triton.backends": backends_mod} + try: + with ( + mock.patch.dict(sys.modules, patched_modules), + mock.patch.dict("os.environ", {}, clear=True), + ): + return importlib.reload(triton_importing).HAS_TRITON + finally: + # Restore the module state derived from the real environment. + importlib.reload(triton_importing) + + def test_triton_placeholder_is_module(): triton = TritonPlaceholder() assert isinstance(triton, types.ModuleType) @@ -77,6 +110,24 @@ def test_triton_placeholder_language_from_parent(): assert isinstance(lang, TritonLanguagePlaceholder) +def test_cpu_backend_does_not_disable_triton(): + # The cpu backend's driver is always active, so counting it alongside a GPU + # backend used to yield 2 active drivers and disable Triton entirely. + assert _has_triton_for_backends(amd=True, cpu=True) is True + + +def test_single_gpu_backend_keeps_triton(): + assert _has_triton_for_backends(amd=True) is True + + +def test_multiple_active_gpu_backends_disable_triton(): + assert _has_triton_for_backends(amd=True, nvidia=True) is False + + +def test_cpu_backend_alone_disables_triton(): + assert _has_triton_for_backends(cpu=True) is False + + def test_no_triton_fallback(): # clear existing triton modules sys.modules.pop("triton", None) diff --git a/vllm/triton_utils/importing.py b/vllm/triton_utils/importing.py index e17450f78d..2828ed467e 100644 --- a/vllm/triton_utils/importing.py +++ b/vllm/triton_utils/importing.py @@ -23,8 +23,15 @@ if HAS_TRITON: # It's generally expected that x.driver exists and has # an is_active method. # The `x.driver and` check adds a small layer of safety. + # The "cpu" backend's driver reports itself active unconditionally, so + # it must be excluded here or every GPU host looks like it has 2 active + # drivers. Triton's own driver selection skips it for the same reason + # (see triton.runtime.driver._create_driver): CPU is only ever used + # when explicitly selected. active_drivers = [ - x.driver for x in backends.values() if x.driver and x.driver.is_active() + x.driver + for name, x in backends.items() + if name != "cpu" and x.driver and x.driver.is_active() ] # Check if we're in a distributed environment where CUDA_VISIBLE_DEVICES