Files
1Cat-vLLM/tools/pre_commit/check_sm70_linear_policy.py
T
2026-10-10 10:17:08 +08:00

301 lines
10 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Keep migrated NVFP4 environment compatibility in its configuration adapter.
This complements #711's registration check: registered names can still be
incorrectly read at runtime rather than from a resolved per-engine config.
"""
import ast
import sys
from pathlib import Path
NAMES = {
"VLLM_SM70_NVFP4_QPN2",
"VLLM_SM70_NVFP4_QPN2_PREFILL",
"VLLM_SM70_NVFP4_QPN2_SHARED_WEIGHT",
"VLLM_SM70_NVFP4_QPN2_SHARED_SCALES",
"VLLM_SM70_NVFP4_QPN2_PREFILL_MIN_M",
"VLLM_SM70_AWQ_MLP_ENGINE",
"VLLM_SM70_AWQ_PREFILL_EXACT_DENSE",
}
FP8_NAMES = {
"VLLM_SM70_FP8_TURBOMIND",
"VLLM_SM70_FP8_DEQUANT_FALLBACK",
"VLLM_SM70_FP8_QPN8",
"VLLM_SM70_FP8_QPN8_PP2_TP4",
"VLLM_SM70_FP8_QPN8_PP2_TP4_SHARED_GATE",
"VLLM_SM70_FP8_PRESCALED_M1_DECODE",
"VLLM_SM70_FP8_PRESCALED_M1_SHARED_GATE",
"VLLM_SM70_FP8_PREFILL_PRESCALED",
"VLLM_SM70_FP8_PREFILL_EXACT_DENSE",
"VLLM_SM70_FP8_PREFILL_VISIBLE_DENSE_MM",
"VLLM_SM70_FP8_DENSE_GATED_SILU",
}
ALLOWED = {
"vllm/envs.py",
"vllm/config/kernel.py",
"vllm/config/sm70_dflash2.py",
"vllm/config/sm70_moe.py",
"vllm/config/sm70_native.py",
}
_POLICY = Path(__file__).resolve().parents[2] / "vllm/config/sm70_dflash2.py"
_POLICY_TREE = ast.parse(_POLICY.read_text())
DFLASH_NAMES = set(
next(
ast.literal_eval(node.value)
for node in _POLICY_TREE.body
if isinstance(node, ast.Assign)
and any(
isinstance(target, ast.Name) and target.id == "SM70_DFLASH2_LEGACY_FIELDS"
for target in node.targets
)
)
) - {"VLLM_SM70_FP8_QPN8"}
_MOE_POLICY = Path(__file__).resolve().parents[2] / "vllm/config/sm70_moe.py"
MOE_NAMES = {
node.value
for node in ast.walk(ast.parse(_MOE_POLICY.read_text()))
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith("VLLM_")
and node.value.isidentifier()
}
_CONFIG_ROOT = _POLICY.parent
_EXECUTION_TREE = ast.parse((_CONFIG_ROOT / "execution_policy.py").read_text())
RUNTIME_NAMES = {
node.value
for node in ast.walk(_EXECUTION_TREE)
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith("VLLM_")
and node.value.isidentifier()
}
RUNTIME_NAMES.update(
node.value
for cls in ast.parse((_CONFIG_ROOT / "sm70_native.py").read_text()).body
if isinstance(cls, ast.ClassDef) and cls.name == "CollectiveNativeConfig"
for node in ast.walk(cls)
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith("VLLM_")
and node.value.isidentifier()
)
for _class in ast.parse((_CONFIG_ROOT / "sm70_runtime.py").read_text()).body:
if isinstance(_class, ast.ClassDef) and _class.name in (
"RuntimeTraceConfig",
"Sm70RuntimeConfig",
):
for _field in _class.body:
if (
isinstance(_field, ast.AnnAssign)
and isinstance(_field.target, ast.Name)
and _field.target.id in ("layer_aliases", "warmup_aliases")
):
RUNTIME_NAMES.update(ast.literal_eval(_field.value).values())
for _node in ast.parse((_CONFIG_ROOT / "policy_defaults.py").read_text()).body:
if isinstance(_node, ast.Assign) and any(
isinstance(target, ast.Name) and target.id == "EXTRA_BINDINGS"
for target in _node.targets
):
RUNTIME_NAMES.update(ast.literal_eval(_node.value))
# Newly migrated provider declarations share the same runtime rule.
for _name in (
"sm70_sparse.py",
"gdn_projection.py",
"sm70_triton_attention.py",
"turboquant_runtime.py",
):
RUNTIME_NAMES.update(
node.value
for node in ast.walk(ast.parse((_CONFIG_ROOT / _name).read_text()))
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith("VLLM_")
and node.value.isidentifier()
)
# MoE route and unquantized policies now cover generic modular consumers too.
for _cls in ast.parse(_MOE_POLICY.read_text()).body:
if isinstance(_cls, ast.ClassDef) and _cls.name in (
"Sm70MoERoutingPolicy",
"Sm70UnquantizedMoEConfig",
):
RUNTIME_NAMES.update(
node.value
for node in ast.walk(_cls)
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith("VLLM_")
and node.value.isidentifier()
)
# Read the canonical diagnostic declarations statically, never invoke getters.
for _name in ("diagnostic_dump.py", "diagnostic_sampling.py"):
RUNTIME_NAMES.update(
node.value
for node in ast.walk(ast.parse((_CONFIG_ROOT / _name).read_text()))
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith("VLLM_")
and node.value.isidentifier()
)
for _class in ast.parse(_POLICY.read_text()).body:
if isinstance(_class, ast.ClassDef) and _class.name == "DFlashDiagnosticsConfig":
RUNTIME_NAMES.update(
node.value
for node in ast.walk(_class)
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith("VLLM_")
and node.value.isidentifier()
)
# Component-owned attention declarations include both public aliases and the
# initialized native binding. Importing the optional CUDA package is unnecessary.
for _class in ast.parse((_CONFIG_ROOT / "flash_v100.py").read_text()).body:
if not isinstance(_class, ast.ClassDef):
continue
for _field in _class.body:
if (
isinstance(_field, ast.AnnAssign)
and isinstance(_field.target, ast.Name)
and _field.target.id in ("aliases", "bindings", "legacy_aliases")
):
RUNTIME_NAMES.update(
node.value
for node in ast.walk(_field.value)
if isinstance(node, ast.Constant)
and isinstance(node.value, str)
and node.value.startswith(("VLLM_", "PREFIX_"))
)
def runtime_policy_reads(path: Path, tree: ast.AST) -> list[str]:
"""Ordered defaults can reference aliases; execution cannot read their envs."""
if path.as_posix().startswith("vllm/config/"):
return []
from tools.pre_commit.config_lifecycle import initialization_library_loaders
loading_nodes = {
child
for function in initialization_library_loaders(tree)
for child in ast.walk(function)
}
from tools.pre_commit.environment_readers import registered_and_wrapped_reads
errors = []
for key, node, _ in registered_and_wrapped_reads(tree):
if node in loading_nodes:
continue
name = key.value if isinstance(key, ast.Constant) else None
if name in RUNTIME_NAMES:
errors.append(
f"{path}:{node.lineno}: {name} belongs to initialized execution policy"
)
return errors
def moe_policy_reads(path: Path, tree: ast.AST) -> list[str]:
if not (
"/fused_moe/sm70/" in path.as_posix()
or path.name
in {
"awq_sm70_moe.py",
"fp8_sm70_moe.py",
"nvfp4_sm70_moe.py",
"mxfp4_sm70_moe.py",
"awq_qpn_sm70.py",
}
):
return []
errors = []
for node in ast.walk(tree):
name = None
if (
isinstance(node, ast.Attribute)
and isinstance(node.value, ast.Name)
and node.value.id == "envs"
):
name = node.attr
elif isinstance(node, ast.Call) and ast.unparse(node.func) in {
"os.getenv",
"os.environ.get",
"getattr",
}:
index = 1 if ast.unparse(node.func) == "getattr" else 0
if len(node.args) > index and isinstance(node.args[index], ast.Constant):
name = node.args[index].value
if name in MOE_NAMES:
errors.append(
f"{path}:{node.lineno}: {name} belongs to "
"KernelConfig.sm70_moe initialization"
)
return errors
def violations(path: Path) -> list[str]:
if path.as_posix() in ALLOWED or "vllm" not in path.parts:
return []
tree = ast.parse(path.read_text())
errors = moe_policy_reads(path, tree) + runtime_policy_reads(path, tree)
fp8_nodes = set()
if path.name == "sm70_fp8.py" or path.as_posix().endswith(
"kernels/linear/qpn/fp8.py"
):
fp8_nodes.update(ast.walk(tree))
elif path.name == "fp8.py":
for candidate in tree.body:
if (
isinstance(candidate, ast.ClassDef)
and candidate.name == "Fp8LinearMethod"
):
fp8_nodes.update(ast.walk(candidate))
legacy_glm_defaults = set()
if path.as_posix() == "vllm/config/vllm.py":
for assignment in tree.body:
if isinstance(assignment, ast.Assign) and any(
isinstance(target, ast.Name)
and target.id == "_SM70_GLM5_DFLASH_TP8_PP1_DEFAULTS"
for target in assignment.targets
):
legacy_glm_defaults.update(ast.walk(assignment))
for node in ast.walk(tree):
name = None
if isinstance(node, ast.Attribute):
name = node.attr
elif isinstance(node, ast.Constant) and isinstance(node.value, str):
name = node.value
if name in DFLASH_NAMES and node not in legacy_glm_defaults:
errors.append(
f"{path}:{node.lineno}: {name} belongs to the compatibility "
"adapter; consume speculative_config.sm70_dflash2 instead"
)
if name in NAMES or (node in fp8_nodes and name in FP8_NAMES):
errors.append(
f"{path}:{node.lineno}: {name} belongs to the deprecated compatibility "
"adapter; consume the resolved kernel_config policy instead"
)
return errors
def main():
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
paths = [Path(name) for name in sys.argv[1:]]
if not paths:
paths = list(Path("vllm").rglob("*.py"))
errors = [
error for path in paths if path.suffix == ".py" for error in violations(path)
]
print("\n".join(errors))
return bool(errors)
if __name__ == "__main__":
raise SystemExit(main())