From 0925a8e8a7759907874fd9848cfbc665716b1b6c Mon Sep 17 00:00:00 2001 From: Change72 Date: Wed, 29 Jul 2026 09:49:26 -0700 Subject: [PATCH 1/5] [KV Offload] Enable single-copy MLA layout for CPUOffloadingSpec After #50094 moved the default CPUOffloadingSpec worker buffer onto the shared SharedOffloadRegion (CUDA-alike only), enable the same single-copy replicated MLA layout the tiering spec already uses -- but only on deployments that actually allocate on the shared region. The config replicated_layout gate has no platform clause, and non-CUDA-alike platforms keep a per-rank private pinned tensor. Enabling dedup there would let the connector's rank-0 writer gate ack rank>0 stores without writing, leaving those private buffers empty (data loss). So the flip is gated by _uses_shared_region() -- the single source of truth shared by both the replicated-layout sizing and the create_worker allocation path, so the two cannot drift. TieringOffloadingSpec overrides it to True (it always allocates the shared region), leaving its behavior unchanged. Co-authored-by: Cursor Agent Signed-off-by: Change72 --- tests/v1/kv_offload/test_factory.py | 102 +++++++++++++++++++++++++++- vllm/v1/kv_offload/cpu/spec.py | 33 ++++++--- vllm/v1/kv_offload/tiering/spec.py | 7 ++ 3 files changed, 131 insertions(+), 11 deletions(-) diff --git a/tests/v1/kv_offload/test_factory.py b/tests/v1/kv_offload/test_factory.py index 727b6a1eaa60..83d0c72b7255 100644 --- a/tests/v1/kv_offload/test_factory.py +++ b/tests/v1/kv_offload/test_factory.py @@ -270,9 +270,40 @@ def fake_region_ctor(**kwargs): @pytest.mark.parametrize("world_size", [2, 4, 8]) -def test_cpu_spec_replicated_config_preserves_per_rank_sizing(world_size: int): - # Use a page-multiple per-worker block so the row size is unaffected by the - # page-alignment rounding regardless of the host mmap page size. +def test_cpu_spec_replicated_sizing_on_shared_region(monkeypatch, world_size: int): + # On shared-region (CUDA-alike) platforms the default spec now honors + # replicated layout: a single MLA copy (num_copies=1), matching tiering. + import vllm.v1.kv_offload.cpu.spec as cpu_spec_module + + monkeypatch.setattr(cpu_spec_module.current_platform, "is_cuda_alike", lambda: True) + worker_kv_bytes_per_block = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT + spec = _create_spec( + cpu_bytes_to_use=worker_kv_bytes_per_block * 8, + worker_kv_bytes_per_block=worker_kv_bytes_per_block, + world_size=world_size, + replicated_layout=True, + ) + + assert isinstance(spec, CPUOffloadingSpec) + assert spec.replicated_layout is True + assert spec.cpu_page_size_per_worker == worker_kv_bytes_per_block + assert spec.kv_bytes_per_chunk == worker_kv_bytes_per_block + assert spec.num_blocks == 8 + + +@pytest.mark.parametrize("world_size", [2, 4, 8]) +def test_cpu_spec_replicated_config_ignored_off_shared_region( + monkeypatch, world_size: int +): + # Data-loss guard: non-CUDA-alike platforms keep a per-rank private pinned + # tensor (no shared medium), so replicated layout MUST stay off. Otherwise + # the rank-0 writer gate would ack rank>0 stores without writing, leaving + # those private buffers empty and corrupting subsequent loads. + import vllm.v1.kv_offload.cpu.spec as cpu_spec_module + + monkeypatch.setattr( + cpu_spec_module.current_platform, "is_cuda_alike", lambda: False + ) worker_kv_bytes_per_block = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT spec = _create_spec( cpu_bytes_to_use=worker_kv_bytes_per_block * world_size * 2, @@ -288,6 +319,30 @@ def test_cpu_spec_replicated_config_preserves_per_rank_sizing(world_size: int): assert spec.num_blocks == 2 +@pytest.mark.parametrize("config_replicated", [True, False]) +@pytest.mark.parametrize("cuda_alike", [True, False]) +def test_cpu_spec_replicated_layout_truth_matrix( + monkeypatch, cuda_alike: bool, config_replicated: bool +): + # replicated_layout is enabled iff the config gate passes AND the deployment + # actually allocates on the shared region (CUDA-alike). + import vllm.v1.kv_offload.cpu.spec as cpu_spec_module + + monkeypatch.setattr( + cpu_spec_module.current_platform, "is_cuda_alike", lambda: cuda_alike + ) + worker_kv_bytes_per_block = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT + spec = _create_spec( + cpu_bytes_to_use=worker_kv_bytes_per_block * 8, + worker_kv_bytes_per_block=worker_kv_bytes_per_block, + world_size=4, + replicated_layout=config_replicated, + ) + + assert isinstance(spec, CPUOffloadingSpec) + assert spec.replicated_layout is (cuda_alike and config_replicated) + + def test_cpu_spec_create_worker_uses_mmap_on_cuda_alike(monkeypatch): import vllm.v1.kv_offload.cpu.spec as cpu_spec_module @@ -389,6 +444,47 @@ def test_cpu_spec_create_worker_skips_mmap_for_empty_cache(monkeypatch): assert worker_calls[0]["mmap_region"] is None +def test_cpu_spec_create_worker_uses_single_slot_for_replicated_layout(monkeypatch): + import vllm.v1.kv_offload.cpu.spec as cpu_spec_module + + monkeypatch.setattr(cpu_spec_module.current_platform, "is_cuda_alike", lambda: True) + worker_kv_bytes_per_block = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT + spec = _create_spec( + cpu_bytes_to_use=worker_kv_bytes_per_block * 8, + worker_kv_bytes_per_block=worker_kv_bytes_per_block, + world_size=4, + replicated_layout=True, + ) + assert isinstance(spec, CPUOffloadingSpec) + assert spec.replicated_layout is True + + region = MagicMock() + region_calls: list[dict[str, Any]] = [] + worker_calls: list[dict[str, Any]] = [] + + def fake_region_ctor(**kwargs): + region_calls.append(kwargs) + return region + + def fake_worker_ctor(**kwargs): + worker_calls.append(kwargs) + return MagicMock() + + monkeypatch.setattr(cpu_spec_module, "SharedOffloadRegion", fake_region_ctor) + monkeypatch.setattr(cpu_spec_module, "CPUOffloadingWorker", fake_worker_ctor) + monkeypatch.setattr( + cpu_spec_module.torch.accelerator, "current_device_index", lambda: 5 + ) + + spec.create_worker(MagicMock()) + + # Replicated -> every rank maps slot 0 (not the device-fold 5 % 4 == 1), and + # the offloaded row holds a single copy. + assert region_calls[0]["rank"] == 0 + assert region_calls[0]["kv_bytes_per_block"] == worker_kv_bytes_per_block + assert worker_calls[0]["mmap_region"] is region + + def test_offloading_spec_has_replicated_layout_default(): spec = SingleArgExternalOffloadingSpec(_make_offloading_config()) assert spec.replicated_layout is False diff --git a/vllm/v1/kv_offload/cpu/spec.py b/vllm/v1/kv_offload/cpu/spec.py index 093a2d5e3fce..9be13095745b 100644 --- a/vllm/v1/kv_offload/cpu/spec.py +++ b/vllm/v1/kv_offload/cpu/spec.py @@ -26,7 +26,7 @@ class CPUOffloadingSpec(OffloadingSpec): BLOCK_SIZE_ALIGNMENT = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT - SUPPORTS_REPLICATED_LAYOUT = False + SUPPORTS_REPLICATED_LAYOUT = True @classmethod def build_metric_definitions( @@ -89,7 +89,9 @@ def __init__(self, config: OffloadingConfig): self.kv_bytes_per_chunk = 0 self.cpu_page_size_per_worker = 0 self.replicated_layout = ( - config.replicated_layout and self.SUPPORTS_REPLICATED_LAYOUT + config.replicated_layout + and self.SUPPORTS_REPLICATED_LAYOUT + and self._uses_shared_region() ) if config.worker_kv_bytes_per_block > 0 and world_size > 0: num_copies = 1 if self.replicated_layout else world_size @@ -144,17 +146,32 @@ def get_manager(self) -> OffloadingManager: ) return self._manager + def _uses_shared_region(self) -> bool: + """Whether this deployment backs the worker CPU buffer with the shared + mmap region rather than a per-rank private pinned tensor. + + Single source of truth for both the replicated-layout sizing gate and + the ``create_worker`` allocation path: deduplication (single MLA copy + + rank-0 writer gate) is only safe where a shared medium actually exists, + so non-CUDA-alike platforms (private-tensor path) must never enable it. + """ + return current_platform.is_cuda_alike() + def create_worker(self, kv_caches: CanonicalKVCaches) -> CPUOffloadingWorker: mmap_region: SharedOffloadRegion | None = None # num_blocks == 0 would size the region to zero bytes, which cannot be # mmap'd; fall back to the tensor path (empty tensors) as before. - if current_platform.is_cuda_alike() and self.num_blocks > 0: - # Back each worker's CPU buffer with a private slot in a single - # shared mmap region instead of a per-rank pinned tensor. Fold the - # global physical device index into this replica's + if self._uses_shared_region() and self.num_blocks > 0: + # Back each worker's CPU buffer with a slot in a single shared mmap + # region instead of a per-rank pinned tensor. Under replicated + # layout every rank maps slot 0 (a single MLA copy); otherwise fold + # the global physical device index into this replica's # [0, world_size) slot range. - world_size = self.config.parallel.world_size - rank = torch.accelerator.current_device_index() % world_size + if self.replicated_layout: + rank = 0 + else: + world_size = self.config.parallel.world_size + rank = torch.accelerator.current_device_index() % world_size mmap_region = SharedOffloadRegion( engine_id=self.config.engine_id, num_blocks=self.num_blocks, diff --git a/vllm/v1/kv_offload/tiering/spec.py b/vllm/v1/kv_offload/tiering/spec.py index e6ec192415e4..93363411365f 100644 --- a/vllm/v1/kv_offload/tiering/spec.py +++ b/vllm/v1/kv_offload/tiering/spec.py @@ -234,6 +234,13 @@ def get_manager(self) -> OffloadingManager: return self._manager + @override + def _uses_shared_region(self) -> bool: + # Tiering always allocates on the shared region (every platform), so the + # replicated-layout gate must not be narrowed by the CPU spec's + # CUDA-alike check. + return True + @override def create_worker(self, kv_caches: CanonicalKVCaches) -> CPUOffloadingWorker: world_size = self.config.parallel.world_size From b0a7a43a18374c34811bc29c4fd325844a2c4fea Mon Sep 17 00:00:00 2001 From: Change72 Date: Wed, 29 Jul 2026 10:27:20 -0700 Subject: [PATCH 2/5] [KV Offload] Drop redundant SUPPORTS_REPLICATED_LAYOUT flag With the default CPUOffloadingSpec now able to deduplicate on the shared region, the SUPPORTS_REPLICATED_LAYOUT class attribute is True for every in-tree spec, so ANDing it into the replicated_layout gate carried no information. Gate solely on _uses_shared_region() -- the per-spec predicate that already distinguishes "allocates on the shared region" (CPU: CUDA-alike; tiering: always) -- and remove the flag from both specs. Co-authored-by: Cursor Agent Signed-off-by: Change72 --- vllm/v1/kv_offload/cpu/spec.py | 7 +------ vllm/v1/kv_offload/tiering/spec.py | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/vllm/v1/kv_offload/cpu/spec.py b/vllm/v1/kv_offload/cpu/spec.py index 9be13095745b..d48285c1067b 100644 --- a/vllm/v1/kv_offload/cpu/spec.py +++ b/vllm/v1/kv_offload/cpu/spec.py @@ -26,7 +26,6 @@ class CPUOffloadingSpec(OffloadingSpec): BLOCK_SIZE_ALIGNMENT = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT - SUPPORTS_REPLICATED_LAYOUT = True @classmethod def build_metric_definitions( @@ -88,11 +87,7 @@ def __init__(self, config: OffloadingConfig): self.num_blocks = 0 self.kv_bytes_per_chunk = 0 self.cpu_page_size_per_worker = 0 - self.replicated_layout = ( - config.replicated_layout - and self.SUPPORTS_REPLICATED_LAYOUT - and self._uses_shared_region() - ) + self.replicated_layout = config.replicated_layout and self._uses_shared_region() if config.worker_kv_bytes_per_block > 0 and world_size > 0: num_copies = 1 if self.replicated_layout else world_size kv_bytes_per_block = config.worker_kv_bytes_per_block * num_copies diff --git a/vllm/v1/kv_offload/tiering/spec.py b/vllm/v1/kv_offload/tiering/spec.py index 93363411365f..7d3d84a622a4 100644 --- a/vllm/v1/kv_offload/tiering/spec.py +++ b/vllm/v1/kv_offload/tiering/spec.py @@ -76,7 +76,6 @@ class TieringOffloadingSpec(CPUOffloadingSpec): """ BLOCK_SIZE_ALIGNMENT = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT - SUPPORTS_REPLICATED_LAYOUT = True @classmethod @override From 3d396f3a0f013365d0e4a42611bffd23e9300af7 Mon Sep 17 00:00:00 2001 From: Change72 Date: Wed, 29 Jul 2026 23:40:33 -0700 Subject: [PATCH 3/5] [KV Offload] Address review: trim create_worker comment, rename test Per review on #50301: minimize the create_worker rank-selection comment, and rename test_cpu_spec_replicated_config_ignored_off_shared_region to test_cpu_spec_replicated_disabled_without_shared_region. Co-authored-by: Cursor Agent Signed-off-by: Change72 --- tests/v1/kv_offload/test_factory.py | 2 +- vllm/v1/kv_offload/cpu/spec.py | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/v1/kv_offload/test_factory.py b/tests/v1/kv_offload/test_factory.py index 83d0c72b7255..d73d9de53257 100644 --- a/tests/v1/kv_offload/test_factory.py +++ b/tests/v1/kv_offload/test_factory.py @@ -292,7 +292,7 @@ def test_cpu_spec_replicated_sizing_on_shared_region(monkeypatch, world_size: in @pytest.mark.parametrize("world_size", [2, 4, 8]) -def test_cpu_spec_replicated_config_ignored_off_shared_region( +def test_cpu_spec_replicated_disabled_without_shared_region( monkeypatch, world_size: int ): # Data-loss guard: non-CUDA-alike platforms keep a per-rank private pinned diff --git a/vllm/v1/kv_offload/cpu/spec.py b/vllm/v1/kv_offload/cpu/spec.py index d48285c1067b..9f3f1ffc1957 100644 --- a/vllm/v1/kv_offload/cpu/spec.py +++ b/vllm/v1/kv_offload/cpu/spec.py @@ -157,11 +157,8 @@ def create_worker(self, kv_caches: CanonicalKVCaches) -> CPUOffloadingWorker: # num_blocks == 0 would size the region to zero bytes, which cannot be # mmap'd; fall back to the tensor path (empty tensors) as before. if self._uses_shared_region() and self.num_blocks > 0: - # Back each worker's CPU buffer with a slot in a single shared mmap - # region instead of a per-rank pinned tensor. Under replicated - # layout every rank maps slot 0 (a single MLA copy); otherwise fold - # the global physical device index into this replica's - # [0, world_size) slot range. + # Replicated layout puts all ranks on slot 0 (single MLA copy); + # otherwise each rank takes its own slot by physical device index. if self.replicated_layout: rank = 0 else: From 31612014dc8bb6a70888270d0681c779c7835fe7 Mon Sep 17 00:00:00 2001 From: Change72 Date: Wed, 29 Jul 2026 23:47:43 -0700 Subject: [PATCH 4/5] [KV Offload] Minimize _uses_shared_region docstring per review Trim the verbose docstring orozery flagged on #50301 to the essentials; the full data-loss rationale for the platform gate lives in the PR description and the replicated-disabled-without-shared-region test. Co-authored-by: Cursor Agent Signed-off-by: Change72 --- vllm/v1/kv_offload/cpu/spec.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/vllm/v1/kv_offload/cpu/spec.py b/vllm/v1/kv_offload/cpu/spec.py index 9f3f1ffc1957..054bf34064e4 100644 --- a/vllm/v1/kv_offload/cpu/spec.py +++ b/vllm/v1/kv_offload/cpu/spec.py @@ -142,14 +142,8 @@ def get_manager(self) -> OffloadingManager: return self._manager def _uses_shared_region(self) -> bool: - """Whether this deployment backs the worker CPU buffer with the shared - mmap region rather than a per-rank private pinned tensor. - - Single source of truth for both the replicated-layout sizing gate and - the ``create_worker`` allocation path: deduplication (single MLA copy + - rank-0 writer gate) is only safe where a shared medium actually exists, - so non-CUDA-alike platforms (private-tensor path) must never enable it. - """ + """Whether the worker CPU buffer is the shared mmap region (vs a private + per-rank tensor); replicated-layout dedup is gated on this being True.""" return current_platform.is_cuda_alike() def create_worker(self, kv_caches: CanonicalKVCaches) -> CPUOffloadingWorker: From 8e251a062a2474b4d1d2445ae9a1d7acb05d1842 Mon Sep 17 00:00:00 2001 From: Change72 Date: Thu, 30 Jul 2026 08:45:08 -0700 Subject: [PATCH 5/5] [KV Offload] Parametrize create_worker rank test per review Replace the replicated-only rank test with a parametrized test_cpu_spec_create_worker_rank_assignment covering both the replicated (slot 0) and device-fold branches plus edge cases, asserting only the rank (the behavioral delta). Drop the now-redundant rank assertion from the mmap-wiring test. Co-authored-by: Cursor Agent Signed-off-by: Change72 --- tests/v1/kv_offload/test_factory.py | 37 +++++++++++++---------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/tests/v1/kv_offload/test_factory.py b/tests/v1/kv_offload/test_factory.py index d73d9de53257..ecd706828c40 100644 --- a/tests/v1/kv_offload/test_factory.py +++ b/tests/v1/kv_offload/test_factory.py @@ -376,8 +376,6 @@ def fake_worker_ctor(**kwargs): kv_caches = MagicMock() spec.create_worker(kv_caches) - # rank folds the physical device index into [0, world_size): 5 % 4 == 1. - assert region_calls[0]["rank"] == 1 assert region_calls[0]["engine_id"] == "test-engine" assert region_calls[0]["kv_bytes_per_block"] == worker_kv_bytes_per_block * 4 assert worker_calls[0]["kv_caches"] is kv_caches @@ -444,7 +442,18 @@ def test_cpu_spec_create_worker_skips_mmap_for_empty_cache(monkeypatch): assert worker_calls[0]["mmap_region"] is None -def test_cpu_spec_create_worker_uses_single_slot_for_replicated_layout(monkeypatch): +@pytest.mark.parametrize( + ("replicated_layout", "device_index", "world_size", "expected_rank"), + [ + (True, 5, 4, 0), # replicated: always slot 0 + (True, 0, 4, 0), # replicated: slot 0 regardless of device + (False, 5, 4, 1), # non-replicated: 5 % 4 == 1 + (False, 7, 4, 3), # non-replicated: 7 % 4 == 3 + ], +) +def test_cpu_spec_create_worker_rank_assignment( + monkeypatch, replicated_layout, device_index, world_size, expected_rank +): import vllm.v1.kv_offload.cpu.spec as cpu_spec_module monkeypatch.setattr(cpu_spec_module.current_platform, "is_cuda_alike", lambda: True) @@ -452,37 +461,25 @@ def test_cpu_spec_create_worker_uses_single_slot_for_replicated_layout(monkeypat spec = _create_spec( cpu_bytes_to_use=worker_kv_bytes_per_block * 8, worker_kv_bytes_per_block=worker_kv_bytes_per_block, - world_size=4, - replicated_layout=True, + world_size=world_size, + replicated_layout=replicated_layout, ) - assert isinstance(spec, CPUOffloadingSpec) - assert spec.replicated_layout is True - region = MagicMock() region_calls: list[dict[str, Any]] = [] - worker_calls: list[dict[str, Any]] = [] def fake_region_ctor(**kwargs): region_calls.append(kwargs) - return region - - def fake_worker_ctor(**kwargs): - worker_calls.append(kwargs) return MagicMock() monkeypatch.setattr(cpu_spec_module, "SharedOffloadRegion", fake_region_ctor) - monkeypatch.setattr(cpu_spec_module, "CPUOffloadingWorker", fake_worker_ctor) + monkeypatch.setattr(cpu_spec_module, "CPUOffloadingWorker", MagicMock()) monkeypatch.setattr( - cpu_spec_module.torch.accelerator, "current_device_index", lambda: 5 + cpu_spec_module.torch.accelerator, "current_device_index", lambda: device_index ) spec.create_worker(MagicMock()) - # Replicated -> every rank maps slot 0 (not the device-fold 5 % 4 == 1), and - # the offloaded row holds a single copy. - assert region_calls[0]["rank"] == 0 - assert region_calls[0]["kv_bytes_per_block"] == worker_kv_bytes_per_block - assert worker_calls[0]["mmap_region"] is region + assert region_calls[0]["rank"] == expected_rank def test_offloading_spec_has_replicated_layout_default():