[Test] Remove obsolete configuration migration guards (#40976)

This commit is contained in:
Cheng Wan
2026-09-23 15:33:54 -07:00
committed by GitHub
parent 6fe4b66f5c
commit 0010f56a0d
4 changed files with 22 additions and 260 deletions
@@ -282,7 +282,9 @@ class TestResolutionDeclarations(CustomTestCase):
from sglang.srt.runtime_context import publish, reset_context
mapping = namespace_of(ServerArgs)
self.assertGreater(len(mapping), 400, "the namespace mapping collapsed")
self.assertEqual(
set(mapping), {field.name for field in msgspec.structs.fields(ServerArgs)}
)
self.assertEqual(
set(),
@@ -1,28 +1,8 @@
"""Ratchet guard: process-global config reads may only decrease.
"""Runtime config reads use namespace accessors instead of the raw startup record.
``get_server_args()`` returns the published ``ServerArgs`` — one process's
startup record. Config decisions read the namespace accessors instead
(``get_exec()`` / ``get_memory()`` / …), which carry the resolved value
including post-publish overrides, and per-runner values come from the runner
that owns them.
Business code no longer reads the published record for a config value at all:
both baselines are zero, over the whole package minus the modules that own the
slot.
The reads that remain live in ``runtime_context.py`` (exempt by module): the
``@property`` / method members computed from several fields plus the HF config,
which are not namespace leaves and have no home but ``ServerArgs``.
What the scan sees: ``get_server_args().field``, an alias (``sa =
get_server_args()`` then ``sa.field`` -- function-local, module-level, or parked
on an instance attribute), a local copy of an alias (``cfg = sa``), and the
``getattr(<either>, "field")`` spelling of each. It matches the accessors by
their literal names, which is why import-renaming them is banned below. A name
computed at runtime, or indirection deeper than a local name copy, is invisible
here -- the census tool in the context repo audits that shape. A whole-object
pass (``def f(server_args)``) is not a global read and is not counted: there the
caller decided which instance to hand over.
Scan direct field reads and local, module, or instance aliases of
``get_server_args()`` outside the modules that own configuration resolution.
Dynamic indirection is outside this check; whole-object handoffs are allowed.
"""
from sglang.test.ci.ci_register import register_cpu_ci
@@ -45,9 +25,6 @@ _PACKAGE_ROOT = Path(next(iter(sglang.__path__)))
# resolution pipeline.
_SLOT_OWNERS = ("srt/runtime_context.py", "srt/server_args.py", "srt/arg_groups/")
_DIRECT_BASELINE = 0
_ALIAS_BASELINE = 0
def _is_global_call(node) -> bool:
"""``get_server_args()`` however it is spelled: bare, or module-qualified
@@ -112,7 +89,7 @@ def _collect(rel: str, tree: ast.AST):
# A copy of an alias reaches the same record (``cfg = sa`` after
# ``sa = get_server_args()``), so follow Name-to-Name assignments to a
# fixpoint. Deeper indirection (through containers, attributes of
# other objects, cross-scope copies) stays census-tool territory.
# other objects, cross-scope copies) is outside this check.
changed = True
while changed:
changed = False
@@ -325,35 +302,18 @@ def _field_reads():
return direct, alias
class TestGlobalConfigReadRatchet(CustomTestCase):
def _check(self, kind, reads, baseline):
if len(reads) > baseline:
self.fail(
f"{kind} process-global config field reads grew: {len(reads)} > "
f"baseline {baseline}. Read the namespace accessor for the "
"field's namespace, or the owning runner for a per-runner "
"field:\n" + "\n".join(reads)
)
if len(reads) < baseline:
self.fail(
f"{kind} process-global config field reads shrank: {len(reads)} < "
f"baseline {baseline}. Lower the baseline in this file to lock "
"in the progress."
)
def test_global_field_reads_match_the_baseline(self):
class TestGlobalConfigReads(CustomTestCase):
def test_no_global_config_field_reads(self):
direct, alias = _field_reads()
self._check("direct", direct, _DIRECT_BASELINE)
self._check("alias-form", alias, _ALIAS_BASELINE)
self.assertFalse(
direct + alias,
"read resolved config through namespace accessors, or use the owning "
"runner for per-runner values:\n" + "\n".join(direct + alias),
)
class TestNoRenamedAccessorImports(CustomTestCase):
"""The baseline scanner matches ``get_server_args`` by its literal name, so
an ``import ... as`` rename would walk a read straight past the zero
baseline. Renaming the accessor buys nothing (the name is already short and
unambiguous), so it is banned outright — which is exactly what makes
literal-name matching sound. (The configured-size registry resolves
``get_parallel`` aliases itself, so it needs no such ban.)"""
"""Import aliases must not bypass the raw-config read scanner."""
def test_the_scanned_accessors_are_never_import_renamed(self):
offenders = []
@@ -380,8 +340,8 @@ class TestNoRenamedAccessorImports(CustomTestCase):
self.assertFalse(
offenders,
"get_server_args imported under another name; the read ratchet "
"matches it by its literal name, so a rename silently escapes the "
"baseline:\n" + "\n".join(offenders),
"matches it by its literal name, so a rename escapes detection:\n"
+ "\n".join(offenders),
)
+1 -191
View File
@@ -82,28 +82,6 @@ def _sources():
yield path
def _scope_entries_that_say_nothing(paths):
"""Return ``path:line`` for draft scopes missing ``owns_attention``."""
import ast
missing = []
for path in paths:
try:
tree = ast.parse(path.read_text(encoding="utf-8"))
except (SyntaxError, UnicodeDecodeError):
continue
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
func = node.func
name = getattr(func, "attr", None) or getattr(func, "id", None)
if name not in ("draft_tp_context", "patch_tensor_parallel_group"):
continue
if not any(kw.arg == "owns_attention" for kw in node.keywords):
missing.append(f"{path}:{node.lineno}")
return missing
_PS = "sglang.srt.distributed.parallel_state"
@@ -2344,17 +2322,6 @@ class TestTheAccessorsHaveNoCallersOutsideTheirPackage(CustomTestCase):
"get_mooncake_transfer_engine",
}
# Require zero callers, but do not deprecate: these getters have no
# equivalent context field. Group widths may differ from configured widths.
NOT_ANSWERED_BY_THE_CONTEXT = {
"get_moe_data_parallel_world_size",
"get_moe_tensor_parallel_world_size",
"get_dcp_world_size",
# Answers `None` where the context asserts.
"get_dcp_group_no_assert",
"get_torch_distributed_pg_options",
}
def _accessors(self):
"""Find public getters defined in the parallel-state source."""
from sglang.srt.distributed import parallel_state as parallel_state_module
@@ -2407,41 +2374,6 @@ class TestTheAccessorsHaveNoCallersOutsideTheirPackage(CustomTestCase):
"context cannot answer them",
)
# Maximum caller counts; reduce these as callers migrate to the context.
ALLOWED_CALLERS = {
"get_self_pp_group": 1,
"get_default_distributed_backend": 1,
"get_mooncake_transfer_engine": 6,
}
def test_the_exempt_accessors_do_not_grow_new_callers(self):
for name, allowed in sorted(self.ALLOWED_CALLERS.items()):
callers = self._callers(name)
self.assertLessEqual(
len(callers),
allowed,
f"{name} grew a caller: {callers}. Read it through "
f"get_parallel() if the context can answer it; if it truly "
f"cannot, lower this number only when one goes away.",
)
def test_every_getter_the_context_answers_is_deprecated(self):
from sglang.srt.distributed import parallel_state
marked = set(parallel_state._CONTEXT_NAME_OF)
unclassified = (
self._accessors() - self.ALLOWED - self.NOT_ANSWERED_BY_THE_CONTEXT
)
for name in sorted(unclassified):
if name in marked:
continue
self.assertIn(
name,
marked,
f"{name} is neither deprecated nor listed as exempt -- give it "
"a context name or say here why it has none",
)
def test_calling_one_from_outside_the_package_is_deprecated(self):
import warnings
@@ -2463,21 +2395,6 @@ class TestTheAccessorsHaveNoCallersOutsideTheirPackage(CustomTestCase):
f"expected the replacement to be named, got {messages}",
)
def test_nothing_the_context_answers_with_calls_back_into_the_package(self):
import inspect
from sglang.srt.distributed import parallel_state
from sglang.srt.runtime_context import ParallelContext, _derived_widths
written = {n for n, d in _derived_widths().items() if not d.fn}
self.assertTrue(written, "no written-at-runtime names; this proves nothing")
self.assertIn("tp_group", written)
body = inspect.getsource(ParallelContext._read)
self.assertNotIn("_ps()", body)
self.assertNotIn("parallel_state", body)
self.assertNotIn("sglang.srt.runtime_context", parallel_state._EXEMPT_CALLERS)
def test_a_scope_reaches_callers_that_went_straight_to_the_getter(self):
from sglang.srt.distributed import parallel_state
@@ -2890,34 +2807,6 @@ class TestWhoAnswersDuringADraftScope(CustomTestCase):
self.assertEqual(get_parallel().attn_dp_size, 2)
self.assertEqual(get_parallel().dp_size, 2)
def test_every_caller_says_whether_the_draft_owns_its_attention(self):
self.assertEqual(
_scope_entries_that_say_nothing(_sources()),
[],
"these enter the draft scope without saying",
)
def test_the_census_would_notice_one(self):
trees = {
part
for path in _sources()
for part in ("benchmark", "examples", "scripts", "test")
if f"/{part}/" in path.as_posix()
}
self.assertEqual(
trees,
{"benchmark", "examples", "scripts", "test"},
"the walk misses a tree that can enter the scope",
)
with tempfile.TemporaryDirectory() as tmp:
probe = _pathlib.Path(tmp) / "probe.py"
probe.write_text(
"with self.draft_tp_context(runner.tp_group):\n pass\n",
encoding="utf-8",
)
self.assertEqual(_scope_entries_that_say_nothing([probe]), [f"{probe}:1"])
def test_a_full_width_swap_leaves_the_attention_layout_alone(self):
from sglang.srt.distributed import parallel_state
@@ -2956,56 +2845,8 @@ class TestWhoAnswersDuringADraftScope(CustomTestCase):
self.assertEqual((info.pp_rank, info.pp_size), (0, 1))
class TestTheRecordIsNeverWrittenTo(CustomTestCase):
"""Runtime configuration changes use ``RuntimeContext.override``.
Only the resolution pipeline in ``arg_groups`` may write ``ServerArgs``.
"""
#: Assignments here are the record being built, not mutated behind a reader.
EXEMPT = ("srt/arg_groups/",)
def test_nothing_assigns_a_field_of_the_record(self):
import ast as _ast
from sglang.srt.arg_groups.arg_utils import namespace_of
from sglang.srt.server_args import ServerArgs
fields = set(namespace_of(ServerArgs))
offenders = []
for path in _sources():
rel = path.as_posix()
if "sglang/srt/" not in rel and "sglang/benchmark/" not in rel:
continue
if any(part in rel for part in self.EXEMPT):
continue
for node in _ast.walk(_ast.parse(path.read_text(encoding="utf-8-sig"))):
targets = (
node.targets
if isinstance(node, _ast.Assign)
else [node.target]
if isinstance(node, (_ast.AugAssign, _ast.AnnAssign))
else []
)
for target in targets:
if not isinstance(target, _ast.Attribute):
continue
base = target.value
name = getattr(base, "id", getattr(base, "attr", None))
if target.attr.startswith("_"):
continue
if name == "server_args" and target.attr in fields:
offenders.append(f"{rel}:{target.lineno} .{target.attr}")
self.assertEqual(
offenders,
[],
"write the bag through get_context().override(source, ...) instead "
"-- the record is not a channel:\n " + "\n ".join(offenders),
)
class TestTheRetiredNamesAreGoneEverywhere(CustomTestCase):
"""Reject retired getter imports and group-initialization width arguments."""
"""Classify parallel getters and reject retired package imports."""
#: Getters that answer something other than a place in the topology.
NOT_A_PLACEMENT = {
@@ -3087,37 +2928,6 @@ class TestTheRetiredNamesAreGoneEverywhere(CustomTestCase):
"from parallel_state, or read get_parallel():\n " + "\n ".join(offenders),
)
def test_nothing_passes_a_width_to_the_build(self):
import ast as _ast
import inspect
from sglang.srt.distributed.parallel_state import initialize_model_parallel
takes = set(inspect.signature(initialize_model_parallel).parameters)
offenders = []
for path in _sources():
if "multimodal_gen" in path.parts:
continue
for node in _ast.walk(_ast.parse(path.read_text(encoding="utf-8-sig"))):
if (
isinstance(node, _ast.Call)
and getattr(node.func, "id", getattr(node.func, "attr", None))
== "initialize_model_parallel"
):
stale = [
kw.arg for kw in node.keywords if kw.arg and kw.arg not in takes
]
if stale or node.args:
offenders.append(
f"{path}:{node.lineno} {stale or 'positional'}"
)
self.assertEqual(
offenders,
[],
"the build reads every width from the context; publish the "
"topology instead of passing it:\n " + "\n ".join(offenders),
)
class TestNothingReadsThePlacementBeforeItIsFrozen(CustomTestCase):
"""Runner initialization must set placement attributes before reading them."""
@@ -23,7 +23,7 @@ from sglang.test.test_utils import CustomTestCase
register_cpu_ci(est_time=31, suite="base-a-test-cpu")
# Locked taxonomy (global_context/11-server-args-namespace-split.md).
# Supported runtime configuration namespaces.
VALID_NAMESPACES = {
"parallel",
"device",
@@ -81,7 +81,7 @@ class TestServerArgsNamespaces(CustomTestCase):
for node in context_module.body
if isinstance(node, ast.FunctionDef) and node.name.startswith("get_")
}
self.assertGreater(len(accessors), 15, "the accessor derivation broke")
self.assertTrue(accessors, "no runtime context accessors found")
shadowed = []
for path in sorted(srt.rglob("*.py")):
@@ -208,20 +208,11 @@ class TestServerArgsNamespaces(CustomTestCase):
)
self.assertGreater(
sites,
1500,
0,
f"only {sites} bag reads were matched; the scan broke and this "
"check stopped covering anything",
)
def test_every_field_has_a_namespace(self):
nsmap = namespace_of(ServerArgs)
missing = sorted(_field_names() - set(nsmap))
self.assertFalse(
missing,
"ServerArgs fields missing an NS(...) marker "
f"(assign a namespace in server_args.py): {missing}",
)
def test_all_namespaces_are_known(self):
nsmap = namespace_of(ServerArgs)
bad = {f: p for f, p in nsmap.items() if p not in VALID_NAMESPACES}
@@ -230,7 +221,6 @@ class TestServerArgsNamespaces(CustomTestCase):
def test_namespace_map_covers_all_fields(self):
nsmap = namespace_of(ServerArgs)
self.assertEqual(set(nsmap), _field_names())
self.assertGreaterEqual(len(nsmap), 440)
if __name__ == "__main__":