From e27b6e0971c7fefe74f5d61d0e16e0636da5a2d6 Mon Sep 17 00:00:00 2001 From: Cheng Wan <54331508+ch-wan@users.noreply.github.com> Date: Wed, 30 Sep 2026 13:25:03 -0700 Subject: [PATCH] [Fix] Read TensorCast's WORLD placement under its current names (#41805) --- .../tensorcast_store/host_allocator.py | 4 +- .../test_tensorcast_host_allocator.py | 4 +- .../tensorcast_store/test_tensorcast_store.py | 2 +- test/registered/unit/test_runtime_context.py | 77 ++++++++++++++++++- 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/python/sglang/srt/mem_cache/storage/tensorcast_store/host_allocator.py b/python/sglang/srt/mem_cache/storage/tensorcast_store/host_allocator.py index 57a5e06331..24a0c60096 100644 --- a/python/sglang/srt/mem_cache/storage/tensorcast_store/host_allocator.py +++ b/python/sglang/srt/mem_cache/storage/tensorcast_store/host_allocator.py @@ -175,8 +175,8 @@ def build_tensorcast_session_options( from sglang.srt.runtime_context import get_parallel parallel = get_parallel() - world_rank = parallel.world_rank - world_size = parallel.world_size + world_rank = parallel.launch_world_rank + world_size = parallel.launch_world_size rank_label = format_tensorcast_rank_label(world_rank, cast(int, world_size)) if config.transfer_mode == TensorcastTransferMode.ALLOCATOR: diff --git a/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_host_allocator.py b/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_host_allocator.py index 29a508be8f..1a9fa52d96 100644 --- a/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_host_allocator.py +++ b/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_host_allocator.py @@ -214,8 +214,8 @@ def test_store_close_lifecycle_is_terminal_and_retains_allocator_roots_in_subpro ) runtime_context.get_parallel = lambda: SimpleNamespace( - world_rank=0, - world_size=1, + launch_world_rank=0, + launch_world_size=1, ) class FakeSession: diff --git a/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_store.py b/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_store.py index e6e8cc9b81..f16a82d5e7 100644 --- a/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_store.py +++ b/python/sglang/srt/mem_cache/storage/tensorcast_store/test_tensorcast_store.py @@ -410,7 +410,7 @@ def test_registration_constructor_claim_is_final_local_step( monkeypatch.setattr( runtime_context, "get_parallel", - lambda: SimpleNamespace(world_rank=0, world_size=1), + lambda: SimpleNamespace(launch_world_rank=0, launch_world_size=1), ) def claim(options: object, *, owner: TensorcastStore) -> _FakeSession: diff --git a/test/registered/unit/test_runtime_context.py b/test/registered/unit/test_runtime_context.py index 48d7c6bbc4..64058ba6aa 100644 --- a/test/registered/unit/test_runtime_context.py +++ b/test/registered/unit/test_runtime_context.py @@ -2,7 +2,7 @@ from sglang.test.ci.ci_register import register_cpu_ci -register_cpu_ci(est_time=33, suite="base-a-test-cpu") +register_cpu_ci(est_time=45, suite="base-a-test-cpu") import dataclasses import json @@ -3026,6 +3026,81 @@ class TestTheDpSlotComesFromTheSequenceItIndexes(CustomTestCase): self.assertEqual(_calls_named(legal, "dp_gather_slot"), 0) +class TestEveryParallelReadNamesAParallelField(CustomTestCase): + """Every ``get_parallel().`` read in the package names a parallel field. + + ``ParallelContext`` rejects an unknown name only when the read runs, so a + renamed or mistyped name on a path whose tests stub the context out ships + as an ``AttributeError``. + """ + + #: Reads of a name no parallel field declares yet, each with its reason. + UNDECLARED = { + # The KV-shard control plane's enable flag has no declared field; + # nothing calls that path yet. + ("srt/mem_cache/page_interleave.py", "enable_kv_cache_sharding"), + } + + def _reads(self): + """Yield ``(path, line, name)`` for each attribute read off the context. + + A read goes through ``get_parallel()`` directly or through a name the + file binds to it, as in ``parallel = get_parallel()``. + """ + import ast as _ast + + for path in _PACKAGE.rglob("*.py"): + tree = _ast.parse(path.read_text(encoding="utf-8-sig")) + relative = path.relative_to(_PACKAGE).as_posix() + bound = { + target.id + for node in _ast.walk(tree) + if isinstance(node, _ast.Assign) + and isinstance(node.value, _ast.Call) + and isinstance(node.value.func, _ast.Name) + and node.value.func.id == "get_parallel" + for target in node.targets + if isinstance(target, _ast.Name) + } + for node in _ast.walk(tree): + if not isinstance(node, _ast.Attribute): + continue + value = node.value + direct = ( + isinstance(value, _ast.Call) + and isinstance(value.func, _ast.Name) + and value.func.id == "get_parallel" + ) + if direct or (isinstance(value, _ast.Name) and value.id in bound): + yield relative, node.lineno, node.attr + + def test_every_read_names_a_declared_field(self): + from sglang.srt.runtime_context import ParallelContext, _parallel_fields + + known = set(_parallel_fields()) | set(dir(ParallelContext)) + reads = list(self._reads()) + self.assertGreater( + len(reads), 100, "found almost no context reads; the census is broken" + ) + offenders = sorted( + f"{path}:{line} get_parallel().{name}" + for path, line, name in reads + if name not in known + and not name.startswith("_") + and (path, name) not in self.UNDECLARED + ) + self.assertEqual( + offenders, + [], + "these read a name the parallel context does not declare; read the " + "field under its current name:\n " + "\n ".join(offenders), + ) + stale = self.UNDECLARED - {(path, name) for path, _, name in reads} + self.assertEqual( + stale, set(), f"these are listed as undeclared but no longer read: {stale}" + ) + + class TestTheRetiredNamesAreGoneEverywhere(CustomTestCase): """Classify parallel getters and reject retired package imports."""