# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Fork startup guards that fail closed on a graph profile we never qualified. These run at model construction, on every rank, for the target model and for the MTP drafter, before profiling or capture. They exist because a *documented* launch flag is not a mechanism: the first reference-host run of a rebase will otherwise qualify a compile/capture identity nobody chose (see below). What this module does NOT do: choose a graph profile. That is the maintainer's call and it is open (the graph-profile choice). What it does is refuse the two states that are silently wrong on vLLM v0.30.0 -- * ``VLLM_USE_BREAKABLE_CUDAGRAPH`` left unset, which lets ``VllmConfig._maybe_enable_breakable_cudagraph`` turn it on for this fork's architectures and force ``CompilationMode.NONE``; and * a PIECEWISE-bearing cudagraph mode combined with ``VLLM_COMPILE`` and breakable-off, which upstream accepts at config time and then rejects at capture, because #55272 removed every ``@support_torch_compile`` wrapper from the NVIDIA Qwen4Exp model and MTP. -- and to make the resolved profile visible and assertable. Timing limit, stated honestly: this runs at model construction, i.e. AFTER ``VllmConfig.__post_init__`` has already called ``_maybe_enable_breakable_cudagraph`` (``config/vllm.py:1561``) and ``_normalize_piecewise_cudagraph_mode`` (``:1673``, ``:1844``). It is early enough to stop profiling and capture; it cannot un-mutate ``os.environ`` or recover the originally requested cudagraph mode. Requiring the pin to be *explicitly present* is the part of "validate before auto-enable" that is checkable from here: absence is exactly the state the auto-enable feeds on. """ import os from vllm.logger import init_logger logger = init_logger(__name__) BREAKABLE_ENV = "VLLM_USE_BREAKABLE_CUDAGRAPH" #: Opt-out for every guard in this module. Recorded in the launch identity and #: PROHIBITED in production profiles; a production launch must not set it. ALLOW_BREAKABLE_ENV = "VLLM_E2_ALLOW_BREAKABLE" #: Optional expected resolved cudagraph mode, by name (e.g. "FULL", #: "FULL_DECODE_ONLY"). When set, a mismatch is an error. This is how a launch #: profile pins the graph identity once the maintainer has chosen one. EXPECTED_MODE_ENV = "VLLM_E2_EXPECTED_CUDAGRAPH_MODE" #: The architectures vLLM v0.30.0 auto-enables breakable cudagraphs for, out of #: ``vllm/config/vllm.py:DEFAULT_BREAKABLE_CUDAGRAPH_ARCHITECTURES``, that are #: ours. Recorded so the guard's message can name the upstream mechanism. OUR_AUTO_BREAKABLE_ARCHITECTURES = frozenset( { "Qwen4ExpForCausalLM", "Qwen4ExpForConditionalGeneration", "Qwen4ExpMTP", } ) def _upstream_truth(raw: str | None) -> bool: """Upstream's exact semantics for the breakable switch. ``envs.py:937`` is ``os.environ.get(..., "0") == "1"``. Nothing else is on: ``"2"``, ``"true"``, ``"FALSE"`` and ``""`` are all off. Using one parser that matches it is the point -- a stricter local parser would reject boots upstream treats as off, which is a false rejection, not extra safety. """ return raw == "1" def breakable_resolved() -> bool: """Whether breakable cudagraphs are on, as the engine will see it. Two readings of ONE source, not three independent sources: ``vllm.compilation.breakable_cudagraph.is_breakable_cudagraph_enabled()`` is literally ``bool(envs.VLLM_USE_BREAKABLE_CUDAGRAPH)`` (``breakable_cudagraph.py:53``), so it adds nothing over the envs read. The envs read is kept beside the raw one because ``_maybe_enable_breakable_cudagraph`` mutates ``os.environ`` at config time while the envs accessor is cached, so the two can legitimately disagree; either saying "on" means on. No exception is swallowed. A guard that cannot read its own input must fail the boot, not report False. """ if _upstream_truth(os.environ.get(BREAKABLE_ENV)): return True import vllm.envs as envs return bool(envs.VLLM_USE_BREAKABLE_CUDAGRAPH) def _profile(vllm_config) -> tuple[list[str], object, object]: """Resolved (architectures, compilation mode, cudagraph mode). Fails closed.""" model_config = getattr(vllm_config, "model_config", None) if model_config is None: raise RuntimeError("e2-guard: vllm_config has no model_config to check") compilation_config = getattr(vllm_config, "compilation_config", None) if compilation_config is None: raise RuntimeError("e2-guard: vllm_config has no compilation_config to check") architectures = list(getattr(model_config, "architectures", None) or []) return ( architectures, compilation_config.mode, compilation_config.cudagraph_mode, ) def assert_graph_profile_is_executable(vllm_config, *, role: str = "target") -> None: """Refuse the two silently-wrong startup states on v0.30.0. ``role`` is ``"target"`` or ``"draft"``; both are checked, because ``Qwen4ExpMTP`` builds ``Qwen4ExpMultiTokenPredictor`` rather than ``Qwen4ExpForCausalLM`` (``mtp.py:397``, ``:411``) from a config rebuilt at ``mtp.py:159``, so the drafter's profile can differ from the target's. Intentional eager and intentional ``CompilationMode.NONE`` are PERMITTED. On v0.30.0's NVIDIA Qwen4Exp path ``NONE`` is a normal resolved state, not evidence of the auto-enable: #55272 removed the compile wrappers, so ``_normalize_piecewise_cudagraph_mode`` legitimately lands there. """ from vllm.config import CompilationMode architectures, mode, cudagraph_mode = _profile(vllm_config) if os.environ.get(ALLOW_BREAKABLE_ENV, "") == "1": logger.warning( "e2-guard[%s]: %s=1 -- guards disabled by request. architectures=%s " "breakable=%s compilation_mode=%s cudagraph_mode=%s. This boot is " "NOT a promotion-grade boot; a production launch must not set this flag.", role, ALLOW_BREAKABLE_ENV, architectures, os.environ.get(BREAKABLE_ENV), mode, cudagraph_mode, ) return # 1. The pin must be explicit. Absence is the state _maybe_enable_breakable_ # cudagraph feeds on (config/vllm.py:758: "not in os.environ"). if BREAKABLE_ENV not in os.environ: raise RuntimeError( f"e2-guard[{role}]: {BREAKABLE_ENV} is not set. vLLM v0.30.0 lists " f"{sorted(OUR_AUTO_BREAKABLE_ARCHITECTURES)} in " "DEFAULT_BREAKABLE_CUDAGRAPH_ARCHITECTURES and turns the switch on " "by itself when it is unset (vllm/config/vllm.py:756-774), forcing " "CompilationMode.NONE with it. Pin it in the launcher: " f"export {BREAKABLE_ENV}=0 (or =1 deliberately), or set " f"{ALLOW_BREAKABLE_ENV}=1 to take an unpinned boot on purpose." ) breakable = breakable_resolved() # 2. Breakable on is an identity this fork has never qualified. if breakable: raise RuntimeError( f"e2-guard[{role}]: {BREAKABLE_ENV} resolved ON for " f"{architectures or ''} " f"(compilation_mode={mode}, cudagraph_mode={cudagraph_mode}). That " "is not the identity the v2.0.1 results were measured on. Export " f"{BREAKABLE_ENV}=0, or set {ALLOW_BREAKABLE_ENV}=1 to take it " "deliberately." ) # 3. PIECEWISE with breakable off needs a torch-compiled submodule, and # #55272 removed every @support_torch_compile wrapper from # vllm/models/qwen4_exp/nvidia/*. Upstream only rewrites the mode when # compilation mode is NOT VLLM_COMPILE # (config/vllm.py:1010-1030), so VLLM_COMPILE + PIECEWISE + breakable-off # survives config time and then dies at # gpu/cudagraph_utils.py:605 in the middle of capture. Fail here instead, # with the candidates named. if ( cudagraph_mode.requires_piecewise_compilation() and mode is CompilationMode.VLLM_COMPILE ): raise RuntimeError( f"e2-guard[{role}]: cudagraph_mode={cudagraph_mode} needs piecewise " "capture, compilation mode is VLLM_COMPILE and breakable cudagraphs " "are off -- but vLLM PR #55272 removed every @support_torch_compile " "wrapper from vllm/models/qwen4_exp/nvidia/, so " "has_compiled_submodule() is False and capture will raise at " "vllm/v1/worker/gpu/cudagraph_utils.py:605. Executable candidates: " "(a) breakable off + -O0/CompilationMode.NONE, which normalises " "FULL_AND_PIECEWISE to FULL_DECODE_ONLY (changed prefill " "behaviour -- must be qualified); (b) breakable off + an explicit " "cudagraph_mode=FULL or NONE; (c) breakable PIECEWISE, i.e. " f"{BREAKABLE_ENV}=1, which is a different identity. See " "the graph-profile choice." ) # 4. Optional exact pin of the resolved mode, for a launch profile. expected = os.environ.get(EXPECTED_MODE_ENV, "").strip() if expected: actual = getattr(cudagraph_mode, "name", str(cudagraph_mode)) if actual != expected: raise RuntimeError( f"e2-guard[{role}]: resolved cudagraph_mode={actual} but " f"{EXPECTED_MODE_ENV}={expected}. The launch profile and the " "engine disagree about the graph identity." ) logger.info( "e2-guard[%s]: architectures=%s breakable=OFF compilation_mode=%s " "cudagraph_mode=%s expected=%s. Recorded, not certified -- this guard " "checks startup states, not that the profile was qualified.", role, architectures, mode, cudagraph_mode, expected or "", ) #: Back-compatible name for the earlier call sites. assert_breakable_cudagraph_pinned = assert_graph_profile_is_executable