Files
vLLM/vllm/v1/e2_guards.py
T
MOVZXandHinata 23349e43b2 [PLE] Integrate the pull transport on v0.30.0 with upstream UVA refused
- VLLM_E2_PLE_PULL_TRANSPORT replaces the fork's old switch name, which
  v0.30.0 reuses for its UVA backend with the opposite default; strict parser;
  compile/capture factor.
- The pull transport is reachable on v0.30.0 through a pull embedding class
  that inherits upstream's without editing it; the upstream UVA backend is
  provably off while the pull switch is on.
- resolve_ple_mode()/assert_ple_mode() log and assert the effective PLE mode
  for target and drafter, and refuse a mixed configuration.
- Producer log lines for the PLE backend and weight dtype.
- CPU tests for mode resolution and the mixed-config refusal.

Co-Authored-By: Hinata <hinata@idihore.id>
Signed-off-by: MOVZX <movzx@yahoo.com>
2026-10-09 19:52:54 +07:00

365 lines
16 KiB
Python

# 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 '<unknown architecture>'} "
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 "<unset>",
)
# --------------------------------------------------------------------------
# PLE transport: carry the pull transport, UVA disabled.
# --------------------------------------------------------------------------
#: The fork's pull-transport switch (strict "0"/"1"; registered and hashed in
#: vllm/envs.py). Renamed from the E2 base's VLLM_PLE_CPU_OFFLOAD.
PULL_TRANSPORT_ENV = "VLLM_E2_PLE_PULL_TRANSPORT"
#: Upstream v0.30.0's switch for the pinned-host UVA PLE backend. Same name as
#: the E2 base's pull switch, opposite default (upstream: "1").
UPSTREAM_UVA_ENV = "VLLM_PLE_CPU_OFFLOAD"
def pull_transport_enabled() -> bool:
"""Whether the fork's pull transport is on. Strict; raises on bad values.
One source: the registered accessor ``envs.VLLM_E2_PLE_PULL_TRANSPORT``,
whose parser (``envs._e2_strict_switch``) accepts exactly unset/"0"/"1".
"""
import vllm.envs as envs
return bool(envs.VLLM_E2_PLE_PULL_TRANSPORT)
def check_upstream_uva_env_against_pull() -> None:
"""Fail loud if the old name would turn upstream's UVA path on.
With the pull transport on, ``VLLM_PLE_CPU_OFFLOAD`` may be unset (the
fork then supplies ``cpu_offload=False`` as the Engram default) or exactly
``"0"``. ``"1"`` -- the E2-era spelling of "enable our offload" -- would
enable upstream's pinned-host UVA backend beside ours: a double offload.
Any other value is rejected rather than interpreted. With the pull
transport off this function does nothing: the name is upstream's.
"""
if not pull_transport_enabled():
return
raw = os.environ.get(UPSTREAM_UVA_ENV)
if raw is None or raw == "0":
return
if raw == "1":
raise RuntimeError(
f"e2-guard: {PULL_TRANSPORT_ENV}=1 and {UPSTREAM_UVA_ENV}=1. On "
f"vLLM v0.30.0 {UPSTREAM_UVA_ENV} is UPSTREAM's switch for the "
"pinned-host UVA PLE backend (default on), not the E2 pull "
"transport, so this launch asks for both transports at once. Unset "
f"{UPSTREAM_UVA_ENV} or export {UPSTREAM_UVA_ENV}=0 "
"(the pull transport replaces that switch)."
)
raise ValueError(
f"e2-guard: {PULL_TRANSPORT_ENV}=1 and {UPSTREAM_UVA_ENV}={raw!r}. "
f'With the pull transport on, {UPSTREAM_UVA_ENV} must be unset or '
'exactly "0"; any other value is refused rather than guessed.'
)
def pull_transport_cpu_offload_default(upstream_default: bool) -> bool:
"""The Engram ``cpu_offload`` default, as this fork resolves it.
Pull off: ``upstream_default`` unchanged (the tree behaves as upstream).
Pull on: the old-name check above, then ``False`` -- upstream's UVA
backend is never the default beside the pull transport.
"""
if not pull_transport_enabled():
return upstream_default
check_upstream_uva_env_against_pull()
return False
def verify_engram_against_pull(engram_config) -> None:
"""Refuse an explicit ``cpu_offload=True`` while the pull transport is on.
``--engram-config '{"cpu_offload": true}'`` takes precedence over every
default, so it is checked on the resolved object, not the environment.
"""
if not pull_transport_enabled():
return
check_upstream_uva_env_against_pull()
if engram_config is not None and bool(engram_config.cpu_offload):
raise RuntimeError(
f"e2-guard: {PULL_TRANSPORT_ENV}=1 but the resolved EngramConfig "
"has cpu_offload=True (upstream's pinned-host UVA PLE backend). "
"Both transports would own the PLE table. Drop cpu_offload from "
"--engram-config (or set it false), or turn the pull transport off."
)
#: Effective PLE modes, as the guard names them.
PLE_MODE_PULL = "pull" # E2 pull transport (VLLM_E2_PLE_PULL_TRANSPORT=1)
PLE_MODE_UVA = "uva" # upstream Qwen4ExpPLEPinnedHostEmbedding
PLE_MODE_DEVICE = "device" # upstream Qwen4ExpPLEDeviceEmbedding
PLE_MODE_NONE = "none" # no Engram config resolved (no PLE layers / not CUDA)
def resolve_ple_mode(vllm_config) -> str:
"""The PLE transport this process will build, or an error if mixed.
Reads the two inputs that decide it -- the pull switch (strict) and the
RESOLVED ``engram_config.cpu_offload`` -- and nothing else. Mixed states
raise; there is no fallback from one transport to another.
"""
pull = pull_transport_enabled()
check_upstream_uva_env_against_pull()
engram_config = getattr(vllm_config, "engram_config", None)
cpu_offload = (
None if engram_config is None else bool(engram_config.cpu_offload)
)
if pull:
if cpu_offload:
raise RuntimeError(
f"e2-guard: {PULL_TRANSPORT_ENV}=1 with a resolved Engram "
"cpu_offload=True: the pull transport and upstream's UVA "
"backend would both own the PLE table."
)
return PLE_MODE_PULL
if cpu_offload is None:
return PLE_MODE_NONE
return PLE_MODE_UVA if cpu_offload else PLE_MODE_DEVICE
def assert_ple_mode(vllm_config, *, role: str = "target") -> str:
"""Startup guard: resolve, assert and log the effective PLE mode.
Runs beside ``assert_graph_profile_is_executable`` at model construction
for ``role="target"`` and ``role="draft"`` (the MTP drafter carries the
target's EngramConfig and the same process environment, so a mixed
configuration fails there too). NOT disabled by VLLM_E2_ALLOW_BREAKABLE:
that opt-out is about graph identity, not about owning the PLE table twice.
"""
mode = resolve_ple_mode(vllm_config)
model_config = getattr(vllm_config, "model_config", None)
text_config = getattr(model_config, "hf_text_config", None)
ple_layer_ids = list(getattr(text_config, "ple_layer_ids", None) or [])
engram_config = getattr(vllm_config, "engram_config", None)
logger.info(
"e2-guard[%s]: ple_mode=%s pull_transport=%d engram_cpu_offload=%s "
"%s=%s ple_layer_ids=%s.",
role,
mode,
1 if mode == PLE_MODE_PULL else 0,
None if engram_config is None else bool(engram_config.cpu_offload),
UPSTREAM_UVA_ENV,
os.environ.get(UPSTREAM_UVA_ENV, "<unset>"),
ple_layer_ids,
)
return mode
#: Back-compatible name for the earlier call sites.
assert_breakable_cudagraph_pinned = assert_graph_profile_is_executable