From cec70a4708cfcc5df0a66f95ed7a65fe9ef5803d Mon Sep 17 00:00:00 2001 From: Mohammad Miadh Angkad <176301910+mmangkad@users.noreply.github.com> Date: Fri, 25 Sep 2026 04:48:39 +0000 Subject: [PATCH] [Fix] Keep the target's DP sync slot in draft scopes (#41062) Co-authored-by: Mohammad Angkad Co-authored-by: Cheng Wan <54331508+ch-wan@users.noreply.github.com> --- .../sglang/srt/distributed/parallel_state.py | 13 ++ .../sglang/srt/layers/attention/dsa/utils.py | 6 +- python/sglang/srt/layers/dp_attention.py | 40 +++- .../srt/model_executor/forward_batch_info.py | 19 +- .../runner/prefill_cuda_graph_runner.py | 4 +- python/sglang/srt/runtime_context.py | 3 + .../test_mlp_sync_hybrid_target_verify.py | 2 +- .../model_executor/test_mlp_sync_pad_unpad.py | 82 +++++++- .../spec/test_dp_spec_prefill_coordination.py | 4 +- test/registered/unit/test_runtime_context.py | 181 ++++++++++++++++++ 10 files changed, 330 insertions(+), 24 deletions(-) diff --git a/python/sglang/srt/distributed/parallel_state.py b/python/sglang/srt/distributed/parallel_state.py index fcf4379c87..7b078f2be7 100644 --- a/python/sglang/srt/distributed/parallel_state.py +++ b/python/sglang/srt/distributed/parallel_state.py @@ -54,6 +54,7 @@ from sglang.srt.model_executor.runner_backend_utils.tc_piecewise_cuda_graph impo from sglang.srt.platforms.device_mixin import _DEVICE_TO_DISTRIBUTED_BACKEND from sglang.srt.runtime_context import ( derive_parallel_widths, + get_flags, get_global_dwdp_manager, get_parallel, set_global_dwdp_manager, @@ -3067,6 +3068,16 @@ def patch_tensor_parallel_group(tp_group: GroupCoordinator, *, owns_attention: b global _TP_STATE_PATCHED assert not _TP_STATE_PATCHED, "Should not call when it's already patched" + # A draft forwards the target's DP sync, and the narrowed ranks cannot recover + # this process's slot in it; read it before narrowing (config only, not _TP). + dp_flags = get_flags().dp + saved_gather_slot = dp_flags.scoped_gather_slot + scoped_gather_slot = saved_gather_slot + if owns_attention and dp_flags.enabled: + from sglang.srt.layers.dp_attention import dp_gather_slot + + scoped_gather_slot = dp_gather_slot() + _TP_STATE_PATCHED = True global _TP old_tp_group = _TP @@ -3093,10 +3104,12 @@ def patch_tensor_parallel_group(tp_group: GroupCoordinator, *, owns_attention: b moe_tp_size=tp_group.world_size, moe_tp_rank=tp_group.rank_in_group, ) + dp_flags.scoped_gather_slot = scoped_gather_slot try: with get_parallel().override(**narrowed): yield finally: + dp_flags.scoped_gather_slot = saved_gather_slot _TP_STATE_PATCHED = False _TP = old_tp_group diff --git a/python/sglang/srt/layers/attention/dsa/utils.py b/python/sglang/srt/layers/attention/dsa/utils.py index 8a5e9f4319..2ae70ec78e 100644 --- a/python/sglang/srt/layers/attention/dsa/utils.py +++ b/python/sglang/srt/layers/attention/dsa/utils.py @@ -5,7 +5,7 @@ import torch import triton from sglang.srt.environ import envs -from sglang.srt.layers.dp_attention import DpPaddingMode +from sglang.srt.layers.dp_attention import DpPaddingMode, dp_slot_in from sglang.srt.model_executor.runner_backend_utils.breakable_cuda_graph import ( is_in_breakable_cuda_graph, ) @@ -184,10 +184,8 @@ def cal_padded_tokens(forward_batch: "ForwardBatch"): ) if dp_padding_mode.is_max_len(): tokens = max(global_num_tokens) - elif len(global_num_tokens) > 1: - tokens = global_num_tokens[get_parallel().attn_dp_rank] else: - tokens = global_num_tokens[0] + tokens = global_num_tokens[dp_slot_in(global_num_tokens)] if can_dsa_prefill_cp_interleave(forward_batch): tokens = ceil_div(tokens, attn_cp_size) return tokens diff --git a/python/sglang/srt/layers/dp_attention.py b/python/sglang/srt/layers/dp_attention.py index 38a0c3409c..44760c3d0b 100644 --- a/python/sglang/srt/layers/dp_attention.py +++ b/python/sglang/srt/layers/dp_attention.py @@ -64,18 +64,45 @@ def dp_gather_width() -> int: """Return the DP gather width. After elastic scale-up, the gather spans the expanded WORLD; otherwise - it spans the attention-DP replicas. + it spans the attention-DP replicas. Inside a draft scope that owns its + attention, it is still the target's gather (see patch_tensor_parallel_group). """ parallel = get_parallel() - return parallel.dp_size if world_dp_gather_enabled() else parallel.attn_dp_size + if world_dp_gather_enabled(): + return parallel.dp_size + if get_flags().dp.scoped_gather_slot is not None: + return deployment_attn_dp_size() + return parallel.attn_dp_size + + +def dp_slot_in(per_rank) -> int: + """Return this process's slot in a per-DP-replica sequence. + + The sequence carries one entry per replica of the gather this process + takes part in, so its length is the gather width. Length one is the + all-gather-skipped batch, which carries this process's entry alone. + """ + if len(per_rank) == 1: + return 0 + width = dp_gather_width() + if len(per_rank) != width: + raise ValueError( + f"a per-replica sequence of {len(per_rank)} entries does not " + f"belong to a DP gather of width {width}" + ) + return dp_gather_slot() def dp_gather_slot() -> int: """Return this process's index in the DP gather. After elastic scale-up, use the TP rank plus the join offset; otherwise - use the attention-DP rank. + use the attention-DP rank. Inside a draft scope that owns its attention, + it is still the slot in the target's gather (see patch_tensor_parallel_group). """ + scoped = get_flags().dp.scoped_gather_slot + if scoped is not None: + return scoped parallel = get_parallel() if world_dp_gather_enabled(): return parallel.tp_rank + parallel.ep_join_rank_offset @@ -458,9 +485,8 @@ def is_allocation_symmetric() -> bool: def get_dp_local_info(forward_batch: ForwardBatch) -> Tuple[torch.Tensor, torch.Tensor]: # `get_dp_local_info` is only called in global DP gather and scatter. We use global DP rank here. - dp_rank = dp_gather_slot() - if forward_batch.dp_local_start_pos is None: + dp_rank = dp_slot_in(forward_batch.global_num_tokens_gpu) cumtokens = torch.cumsum(forward_batch.global_num_tokens_gpu, dim=0) if dp_rank == 0: local_start_pos = torch.zeros_like(cumtokens[0]) @@ -482,7 +508,7 @@ def get_dp_local_slice_cpu( # CPU (start, length) slice for DP-local data in a rank-padded buffer. # Returns Python ints (no D2H sync) and handles the cuda-graph-padded layout. global_num_tokens = forward_batch.global_num_tokens_cpu - dp_rank = dp_gather_slot() + dp_rank = dp_slot_in(global_num_tokens) local_num_tokens = global_num_tokens[dp_rank] if can_run_graph: local_start_pos = dp_rank * cuda_graph_batch @@ -800,7 +826,7 @@ def _dp_gather_via_all_gatherv( # each rank's local tensor up to sizes[rank] with zeros (matching the # buffer's reserved per-rank slot) so sum(sizes) == buffer rows and there # is no uninitialized tail for the MoE to read. - rank = get_parallel().attn_dp_rank + rank = dp_slot_in(sizes) local_rows = sizes[rank] if local_tokens.shape[0] == local_rows: local_real = local_tokens diff --git a/python/sglang/srt/model_executor/forward_batch_info.py b/python/sglang/srt/model_executor/forward_batch_info.py index 1859893321..b47c4af0ca 100644 --- a/python/sglang/srt/model_executor/forward_batch_info.py +++ b/python/sglang/srt/model_executor/forward_batch_info.py @@ -45,7 +45,7 @@ from sglang.srt.kv_canary.req_to_expected_token_ids_manager import ( ) from sglang.srt.layers.dp_attention import ( DpPaddingMode, - dp_gather_slot, + dp_slot_in, set_dp_buffer_len_from_batch, set_is_extend_in_batch, world_dp_gather_enabled, @@ -1193,7 +1193,9 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin): if self.global_num_tokens_cpu is not None: # DP / MLP-sync path: per-DP padded width. if require_mlp_tp_gather(): - num_tokens_per_dp = self.global_num_tokens_cpu[dp_gather_slot()] + num_tokens_per_dp = self.global_num_tokens_cpu[ + dp_slot_in(self.global_num_tokens_cpu) + ] else: num_tokens_per_dp = self.global_num_tokens_cpu[0] else: @@ -1592,10 +1594,7 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin): else: buffer_len = sum(global_num_tokens) - if len(global_num_tokens) > 1: - num_tokens = global_num_tokens[dp_gather_slot()] - else: - num_tokens = global_num_tokens[0] + num_tokens = global_num_tokens[dp_slot_in(global_num_tokens)] self.attn_tp_sequence_sharded = model_runner.attn_tp_sequence_sharded( num_tokens @@ -1736,6 +1735,14 @@ class ForwardBatch(ForwardBatchDeepSeekMHAMixin): if self.input_embeds is not None: # Keep token-aligned inputs consistent after padding. self.input_embeds = self._pad_tensor_to_size(self.input_embeds, num_tokens) + if ( + self.mm_input_embeds is not None + and self.mm_input_embeds.shape[0] < num_tokens + ): + # A draft reads the target's mm embeds in place of its input_ids. + self.mm_input_embeds = self._pad_tensor_to_size( + self.mm_input_embeds, num_tokens + ) self.req_pool_indices = self._pad_tensor_to_size(self.req_pool_indices, bs) if self.req_pool_indices_cpu is not None: self.req_pool_indices_cpu = self._pad_tensor_to_size( diff --git a/python/sglang/srt/model_executor/runner/prefill_cuda_graph_runner.py b/python/sglang/srt/model_executor/runner/prefill_cuda_graph_runner.py index 08f69c058b..1b506b826c 100644 --- a/python/sglang/srt/model_executor/runner/prefill_cuda_graph_runner.py +++ b/python/sglang/srt/model_executor/runner/prefill_cuda_graph_runner.py @@ -70,6 +70,7 @@ from sglang.srt.layers.cp.utils import ( ) from sglang.srt.layers.dp_attention import ( DpPaddingMode, + dp_slot_in, set_dp_buffer_len, set_is_extend_in_batch, ) @@ -672,8 +673,7 @@ class PrefillCudaGraphRunner(BaseCudaGraphRunner): global_num_tokens = forward_batch.global_num_tokens_for_logprob_cpu if global_num_tokens is not None: - dp_rank = get_parallel().attn_dp_rank - return int(global_num_tokens[dp_rank if len(global_num_tokens) > 1 else 0]) + return int(global_num_tokens[dp_slot_in(global_num_tokens)]) return sum( max(int(seq_len) - int(start_len), 1) diff --git a/python/sglang/srt/runtime_context.py b/python/sglang/srt/runtime_context.py index 5a6e82b217..07cd7d9e10 100644 --- a/python/sglang/srt/runtime_context.py +++ b/python/sglang/srt/runtime_context.py @@ -577,6 +577,9 @@ class DpFlags(_FlagGroupBase): # gather/scatter helpers, whose captured geometry needs one shared bucket. capturing_prefill_graph: bool = False prefill_graph_has_dp_gather: bool = False + # This process's slot in the target's DP sync while a draft scope narrows + # attention; dp_gather_width() also reads it as the in-scope marker. + scoped_gather_slot: Optional[int] = None # DP gathered-buffer allocation metadata (model hidden size / dtype / # device), set by initialize_dp_attention alongside the flags above. buffer_hidden_size: Any = None diff --git a/test/registered/unit/model_executor/test_mlp_sync_hybrid_target_verify.py b/test/registered/unit/model_executor/test_mlp_sync_hybrid_target_verify.py index a85292d316..2804a0864f 100644 --- a/test/registered/unit/model_executor/test_mlp_sync_hybrid_target_verify.py +++ b/test/registered/unit/model_executor/test_mlp_sync_hybrid_target_verify.py @@ -52,7 +52,7 @@ def _sync(forward_mode, num_reqs, global_num_tokens): get_parallel=lambda: SimpleNamespace(attn_tp_size=1), get_exec=lambda: no_prefill_graph, _elastic_should_preserve_local_token_counts=lambda **kwargs: False, - dp_gather_slot=lambda: 0, + dp_slot_in=lambda per_rank: 0, set_dp_buffer_len_from_batch=lambda *args: None, set_is_extend_in_batch=lambda *args: None, _is_cpu=True, diff --git a/test/registered/unit/model_executor/test_mlp_sync_pad_unpad.py b/test/registered/unit/model_executor/test_mlp_sync_pad_unpad.py index 2a74025639..4f80f9c08e 100644 --- a/test/registered/unit/model_executor/test_mlp_sync_pad_unpad.py +++ b/test/registered/unit/model_executor/test_mlp_sync_pad_unpad.py @@ -11,15 +11,19 @@ Pure dataclass logic — CPU only. import unittest from types import SimpleNamespace -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch import torch +from sglang.srt.distributed import parallel_state +from sglang.srt.distributed.parallel_state import GroupCoordinator from sglang.srt.layers.logits_processor import LogitsMetadata, LogitsProcessor +from sglang.srt.model_executor.cuda_graph_config import CudaGraphConfig, PhaseConfig from sglang.srt.model_executor.forward_batch_info import ForwardBatch, ForwardMode from sglang.srt.model_executor.runner.decode_cuda_graph_runner import ( DecodeCudaGraphRunner, ) +from sglang.srt.runtime_context import get_context, get_flags, get_parallel from sglang.test.ci.ci_register import register_cpu_ci from sglang.test.test_utils import CustomTestCase @@ -230,5 +234,81 @@ class TestMlpSyncPadUnpad(CustomTestCase): self.assertEqual(fb.extend_logprob_start_lens_cpu, [0]) +class TestDraftScopeMlpSync(CustomTestCase): + SLOT = 2 + HIDDEN = 8 + + def setUp(self): + override = get_context().override_server_args( + tp_size=4, + dp_size=4, + enable_dp_attention=True, + cuda_graph_config=CudaGraphConfig(prefill=PhaseConfig(bs=[])), + ) + override.install() + self.addCleanup(override.restore) + + def _sync_in_draft_scope(self, global_num_tokens): + num_tokens = global_num_tokens[self.SLOT] + fb = ForwardBatch( + forward_mode=ForwardMode.EXTEND, + batch_size=1, + input_ids=torch.arange(num_tokens), + req_pool_indices=torch.tensor([0]), + seq_lens=torch.tensor([num_tokens]), + out_cache_loc=torch.arange(num_tokens), + seq_lens_sum=num_tokens, + positions=torch.arange(num_tokens), + seq_lens_cpu=torch.tensor([num_tokens]), + mm_input_embeds=torch.ones(num_tokens, self.HIDDEN), + is_extend_in_batch=True, + global_num_tokens_cpu=list(global_num_tokens), + global_num_tokens_for_logprob_cpu=list(global_num_tokens), + global_num_tokens_gpu=torch.zeros( + len(global_num_tokens), dtype=torch.int32 + ), + ) + runner = _mock_model_runner() + runner.attn_backend.get_cpu_graph_seq_len_fill_value.return_value = 1 + runner.is_draft_worker = True + runner.attn_tp_sequence_sharded.return_value = False + draft_group = GroupCoordinator.__new__(GroupCoordinator) + draft_group.world_size = 1 + draft_group.rank_in_group = 0 + with ( + get_flags().dp.override(enabled=True), + get_parallel().override( + tp_rank=self.SLOT, attn_tp_rank=0, attn_dp_rank=self.SLOT + ), + patch.object(parallel_state, "_TP", draft_group), + # CPU runners have no driver to pin the synced token counts with. + patch("sglang.srt.model_executor.forward_batch_info._is_cpu", True), + parallel_state.patch_tensor_parallel_group( + draft_group, owns_attention=True + ), + ): + fb.prepare_mlp_sync_batch(runner) + return fb + + def _assert_token_rows(self, fb, rows): + self.assertEqual(fb.input_ids.shape[0], rows) + self.assertEqual(fb.positions.shape[0], rows) + self.assertEqual(tuple(fb.mm_input_embeds.shape), (rows, self.HIDDEN)) + + def test_extend_keeps_its_own_rank_count(self): + """A draft extend pads to its own DP rank's token count, not rank 0's.""" + fb = self._sync_in_draft_scope([9, 5, 6, 4]) + self.assertEqual(fb.global_num_tokens_cpu, [9, 5, 6, 4]) + self._assert_token_rows(fb, rows=6) + + def test_max_len_extend_pads_mm_input_embeds(self): + """A MAX_LEN-padded extend pads mm_input_embeds along with input_ids.""" + with get_flags().dp.override(max_len_with_idle=True): + fb = self._sync_in_draft_scope([9, 0, 6, 4]) + self.assertEqual(fb.global_num_tokens_cpu, [9, 9, 9, 9]) + self._assert_token_rows(fb, rows=9) + torch.testing.assert_close(fb.mm_input_embeds[6:], torch.zeros(3, self.HIDDEN)) + + if __name__ == "__main__": unittest.main() diff --git a/test/registered/unit/spec/test_dp_spec_prefill_coordination.py b/test/registered/unit/spec/test_dp_spec_prefill_coordination.py index ac403fc2f6..f3156d83b0 100644 --- a/test/registered/unit/spec/test_dp_spec_prefill_coordination.py +++ b/test/registered/unit/spec/test_dp_spec_prefill_coordination.py @@ -224,9 +224,7 @@ class TestDPSpecPrefillCoordinationPlan(CustomTestCase): "get_dp_padding_mode", return_value=DpPaddingMode.MAX_LEN, ), - patch( - f"{module}.dp_gather_slot", return_value=0 if idle else 1 - ), + patch(f"{module}.dp_slot_in", return_value=0 if idle else 1), patch(f"{module}.set_dp_buffer_len_from_batch"), patch(f"{module}.set_is_extend_in_batch"), patch( diff --git a/test/registered/unit/test_runtime_context.py b/test/registered/unit/test_runtime_context.py index 83d5f207a8..7b429457a0 100644 --- a/test/registered/unit/test_runtime_context.py +++ b/test/registered/unit/test_runtime_context.py @@ -2830,6 +2830,87 @@ class TestWhoAnswersDuringADraftScope(CustomTestCase): self.assertEqual(parallel.attn_tp_size, 2) self.assertEqual(parallel.dp_size, 2) + def test_a_narrowed_draft_still_indexes_the_targets_dp_sync(self): + """A draft scope that owns its attention answers the DP gather width and + slot of the target's sync, and stops answering once the scope exits.""" + from sglang.srt.distributed import parallel_state + from sglang.srt.layers.dp_attention import dp_gather_slot, dp_gather_width + + reset_context() + self.addCleanup(reset_context) + publish( + ServerArgs( + model_path="dummy", tp_size=4, dp_size=4, enable_dp_attention=True + ), + role="scheduler", + ranks=SpawnRanks(world_rank=0, dp_rank=0), + ) + group = self._group(world_size=1, rank=0) + with ( + get_flags().dp.override(enabled=True), + get_parallel().override(tp_rank=2, attn_tp_rank=0, attn_dp_rank=2), + patch.object(parallel_state, "_TP", group), + ): + with parallel_state.patch_tensor_parallel_group(group, owns_attention=True): + self.assertEqual(get_parallel().attn_dp_size, 1) + self.assertEqual(get_parallel().attn_dp_rank, 0) + self.assertEqual(dp_gather_width(), 4) + self.assertEqual(dp_gather_slot(), 2) + self.assertIsNone(get_flags().dp.scoped_gather_slot) + self.assertEqual(dp_gather_slot(), 2) + + def _draft_scope_at_slot_two(self): + """A rank in slot 2 of a four-replica gather, inside a draft scope.""" + from sglang.srt.distributed import parallel_state + + reset_context() + self.addCleanup(reset_context) + publish( + ServerArgs( + model_path="dummy", tp_size=4, dp_size=4, enable_dp_attention=True + ), + role="scheduler", + ranks=SpawnRanks(world_rank=0, dp_rank=0), + ) + group = self._group(world_size=1, rank=0) + outer = [ + get_flags().dp.override(enabled=True), + get_parallel().override(tp_rank=2, attn_tp_rank=0, attn_dp_rank=2), + patch.object(parallel_state, "_TP", group), + ] + for entered in outer: + entered.__enter__() + self.addCleanup(entered.__exit__, None, None, None) + scope = parallel_state.patch_tensor_parallel_group(group, owns_attention=True) + scope.__enter__() + self.addCleanup(scope.__exit__, None, None, None) + + def test_a_sequence_is_read_at_this_process_s_slot(self): + from sglang.srt.layers.dp_attention import dp_slot_in + + self._draft_scope_at_slot_two() + self.assertEqual(dp_slot_in([9, 5, 6, 4]), 2) + + def test_a_sequence_of_one_is_this_process_s_own_entry(self): + """The scheduler skips the all-gather for a single entry, and does so + even where the gather is wider than one.""" + from sglang.srt.layers.dp_attention import dp_gather_width, dp_slot_in + + self._draft_scope_at_slot_two() + self.assertEqual(dp_gather_width(), 4) + self.assertEqual(dp_slot_in([7]), 0) + + def test_a_sequence_the_gather_did_not_produce_is_refused(self): + """The width and the slot have to come from one topology: a sequence + sized by another is what a stale slot reads at the wrong index.""" + from sglang.srt.layers.dp_attention import dp_slot_in + + self._draft_scope_at_slot_two() + with self.assertRaises(ValueError) as caught: + dp_slot_in([9, 5, 6, 4, 3]) + self.assertIn("5 entries", str(caught.exception)) + self.assertIn("width 4", str(caught.exception)) + def test_a_report_built_for_a_runner_follows_that_runner(self): from sglang.srt.distributed import parallel_state from sglang.srt.utils.weight_checker import WeightChecker @@ -2845,6 +2926,106 @@ class TestWhoAnswersDuringADraftScope(CustomTestCase): self.assertEqual((info.pp_rank, info.pp_size), (0, 1)) +def _calls_named(text: str, name: str) -> int: + """How many times ``name`` is called in ``text``.""" + import ast as _ast + + return sum( + 1 + for node in _ast.walk(_ast.parse(text)) + if isinstance(node, _ast.Call) + and getattr(node.func, "id", getattr(node.func, "attr", None)) == name + ) + + +def _sequences_indexed_by_the_attention_rank(text: str) -> list: + """Per-replica sequences subscripted by the attention-DP rank. + + That rank is the process's place in the attention topology, which a draft + scope narrows; the sequence is sized by the gather. Indexing one with the + other reads whatever sits at the wrong slot. + """ + import ast as _ast + + offenders = [] + for node in _ast.walk(_ast.parse(text)): + if not isinstance(node, _ast.Subscript): + continue + base = getattr(node.value, "attr", getattr(node.value, "id", "")) or "" + if not base.startswith("global_num_tokens"): + continue + if "attn_dp_rank" in _ast.unparse(node.slice): + offenders.append( + f"{base}[{_ast.unparse(node.slice)}] at line {node.lineno}" + ) + return offenders + + +class TestTheDpSlotComesFromTheSequenceItIndexes(CustomTestCase): + """A per-replica sequence is read at the slot of the gather that produced + it. ``dp_slot_in`` is where the two are checked against each other, so a + bare slot read has to be one of the places that legitimately has no + sequence to check against. + """ + + #: Defines the helpers; captures the slot on draft-scope entry; and asserts + #: what the bare slot answers, which is this file. + MAY_READ_THE_BARE_SLOT = ( + "srt/layers/dp_attention.py", + "srt/distributed/parallel_state.py", + "unit/test_runtime_context.py", + ) + + def test_nothing_else_reads_the_bare_slot(self): + offenders, exercised = [], set() + for path in _sources(): + text = path.read_text(encoding="utf-8-sig") + if not _calls_named(text, "dp_gather_slot"): + continue + rel = str(path).replace("\\", "/") + allowed = [a for a in self.MAY_READ_THE_BARE_SLOT if rel.endswith(a)] + if allowed: + exercised.update(allowed) + else: + offenders.append(rel) + self.assertEqual( + offenders, + [], + "read the slot through dp_slot_in() instead, so the " + "width it came from is checked:\n " + "\n ".join(offenders), + ) + # An allowlist entry that stopped calling it would hide a new offender. + self.assertEqual(exercised, set(self.MAY_READ_THE_BARE_SLOT)) + + def test_no_sequence_is_indexed_by_the_attention_rank(self): + offenders = [] + for path in _sources(): + found = _sequences_indexed_by_the_attention_rank( + path.read_text(encoding="utf-8-sig") + ) + offenders.extend(f"{path}: {one}" for one in found) + self.assertEqual( + offenders, + [], + "a draft scope narrows attn_dp_rank to 0; index with " + "dp_slot_in():\n " + "\n ".join(offenders), + ) + + def test_the_censuses_would_notice_one(self): + planted = ( + "def f(fb):\n" + " x = global_num_tokens[get_parallel().attn_dp_rank]\n" + " y = fb.global_num_tokens_cpu[parallel.attn_dp_rank]\n" + " return dp_gather_slot(), x, y\n" + ) + self.assertEqual(len(_sequences_indexed_by_the_attention_rank(planted)), 2) + self.assertEqual(_calls_named(planted, "dp_gather_slot"), 1) + # A legal shape stays quiet. + legal = "def f(s):\n return s[dp_slot_in(s)]\n" + self.assertEqual(_sequences_indexed_by_the_attention_rank(legal), []) + self.assertEqual(_calls_named(legal, "dp_gather_slot"), 0) + + class TestTheRetiredNamesAreGoneEverywhere(CustomTestCase): """Classify parallel getters and reject retired package imports."""