Skip to content

[KV Offload] Move CPUOffloadingSpec onto SharedOffloadRegion - #50094

Merged
orozery merged 1 commit into
vllm-project:mainfrom
Change72:kv-offload-cpu-shared-region
Jul 29, 2026
Merged

orozery merged 1 commit into
vllm-project:mainfrom
Change72:kv-offload-cpu-shared-region

Conversation

@Change72

@Change72 Change72 commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Move the default KV-offload backend CPUOffloadingSpec's worker-side CPU buffer from a per-rank private pinned torch tensor onto the existing shared SharedOffloadRegion mmap, on CUDA/ROCm. This is the allocation-swap prerequisite for the TP-deduplication feature requested in #47929; it introduces no deduplication semantics on its own.

Maintainer authorization (upstream reviewers do not see the issue thread, so the requests are linked directly):

What changes

  • CPUOffloadingSpec.create_worker now builds a SharedOffloadRegion and passes it to CPUOffloadingWorker when running on CUDA/ROCm (current_platform.is_cuda_alike()), folding the physical device index into this replica's [0, world_size) slot range. This is the same allocation TieringOffloadingSpec.create_worker already performs. The mmap is host registered (pinned) only when PIN_MEMORY is set and cudaHostRegister succeeds; a failed registration falls back to unpinned DMA with a warning. A degenerate empty cache (num_blocks == 0) keeps the tensor path, since a zero-byte region cannot be mmap'd.
  • CPUOffloadingSpec.BLOCK_SIZE_ALIGNMENT moves from 1 to the region's page size (SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT), so block accounting rounds each offloaded row up to the mmap page size. This is applied unconditionally (scheduler and worker) because the scheduler computes num_blocks without knowing the worker platform; both sides must agree on the count.

Behavior changes

  • CUDA/ROCm default backend: the worker CPU buffer is the shared mmap region instead of a private pinned tensor. This takes PyTorch's CUDACachingHostAllocator off the allocation path; its power-of-2 rounding of large pinned allocations is the root cause of the host-memory blow-up diagnosed in [KV Offload] Bypass power-of-2 rounding in KV offload CPU pinned allocation #48436. Credit to @procr1337 for that diagnosis.
  • Block accounting picks up the region's page-size alignment, so num_blocks can be marginally lower when an offloaded row is not already a page multiple.
  • Non-CUDA-alike platforms (currently XPU) keep the existing per-rank pinned tensor path — unchanged.
  • TieringOffloadingSpec (a subclass) is unchanged: it already used the shared region and page-size alignment.
  • KV data is byte-identical to the previous path (the same bytes are copied, only the destination buffer differs), so model output is unaffected. The per-worker memory semantics are also unchanged: this PR keeps one private slot per worker (no dedup), and the region is node-local (/dev/shm).

Not in this PR (planned follow-up)

Duplicate-work check

Searched open PRs (47929 in:body, CPUOffloadingSpec, SharedOffloadRegion, pinned-host-memory keywords) and the history of the touched paths. No open PR performs this default-backend allocation swap. Nearest non-duplicates:

Test Plan

Run on a CUDA host:

# Unit tests (offloading spec + worker; the worker suite already exercises the
# mmap-backed path via use_shared_memory=True).
.venv/bin/python -m pytest tests/v1/kv_offload/ -q

# Lint / type checks as in CI.
pre-commit run --files vllm/v1/kv_offload/cpu/spec.py tests/v1/kv_offload/test_factory.py
pre-commit run mypy-3.12 --all-files --hook-stage manual

Host-memory reproduction of the #48436 symptom (TP=2, single node). Serve with the CPU offload tier sized to straddle a per-worker power-of-2 boundary and record per-worker RSS before/after this change:

# cpu_bytes_to_use is the TOTAL across workers. With TP=2:
#   64 GiB total -> 32 GiB/worker (already a power of two: control)
#   80 GiB total -> 40 GiB/worker (rounds up to 64 GiB under the old allocator)
vllm serve <model> --tensor-parallel-size 2 \
  --kv-transfer-config '{
    "kv_connector": "OffloadingConnector",
    "kv_role": "kv_both",
    "kv_connector_extra_config": {"cpu_bytes_to_use": 68719476736}
  }'
# repeat with "cpu_bytes_to_use": 85899345920, recording per-worker RSS
# (e.g. `ps -o rss= -p <worker_pid>`) for each run.

Test Result

Verified on an NVIDIA L4 host (torch 2.13.0+cu130, CUDA 13.0, Python 3.12.3).

Unit tests — pytest tests/v1/kv_offload/test_factory.py tests/v1/kv_offload/cpu/: 116 passed, 1 skipped. Covers the new create_worker platform-split and empty-cache/sizing tests, plus the existing worker suite whose test_transfer[use_shared_memory=True] cases perform byte-exact GPU↔mmap round-trips.

Lint / type — pre-commit run --files vllm/v1/kv_offload/cpu/spec.py tests/v1/kv_offload/test_factory.py: ruff, ruff-format, mypy, SPDX-header check, and the torch.cuda-API guard all pass.

Allocation behavior (the #48436 mechanism), measured directly — resident-set (VmRSS) delta of one host allocation of the given size, per path:

Requested torch.zeros(pin_memory=True) (old tensor path) mmap + cudaHostRegister (this PR's path)
6 GiB 8.00 GiB 6.00 GiB
10 GiB 16.00 GiB 10.00 GiB
12 GiB 16.00 GiB 12.00 GiB

The pinned-tensor path rounds each allocation up to the next power of two (PyTorch's CUDACachingHostAllocator); the shared-region path tracks the requested size. This is the same rounding #48436 reports at production sizes (e.g. an 80 GiB-per-worker request rounding up to 128 GiB).

The change has no GPU-architecture-specific path — the current_platform.is_cuda_alike() branch is identical across CUDA/ROCm GPUs — so this L4 micro-benchmark isolates the per-allocation rounding. The A100 TP=2 results below measure the end-to-end host-memory effect at scale (where the right metric is total physical memory, not per-worker RSS — see the note there).

A100 TP=2 E2E. 2× A100-SXM4-80GB, image vllm/vllm-openai:nightly-d223c900d8 (vLLM 0.26.1rc1.dev18+gd223c900d, whose v1/kv_offload/ is byte-identical to this PR's base — only cpu/spec.py is swapped between the baseline and branch runs), -tp 2 --load-format dummy --enforce-eager, Qwen3-0.6B.

Total host memory for the CPU offload tier (cgroup memory.current, cross-checked with summed PSS — both count the shared region once):

cpu_bytes_to_use Baseline (per-rank pinned tensors) Branch (shared mmap)
64 GiB 117.7 GiB 69.7 GiB
80 GiB 117.7 GiB 85.8 GiB

The default backend allocates one pinned torch.zeros per layer (28 for this model); CUDACachingHostAllocator rounds each to the next power of two, so a 64 GiB budget resides as ~118 GiB (and an 80 GiB budget lands at ~118 GiB too — both per-tensor sizes fall in the same 2 GiB bucket). The shared-region path packs all layers and workers into one page-aligned /dev/shm region and stays at roughly the configured budget plus fixed runtime overhead. (Per-worker RSS is not the right metric here: the region is shared across workers, so summed RSS double-counts it — cgroup/PSS count it once.)

Correctness (TP=2, greedy, 4 prompts sharing a long prefix; the connector offloaded blocks — ~45 MB stored, 72% prefix-cache hit): generated text is identical with offloading on (shared-mmap path) vs off.


This PR was developed with AI assistance (implementation and tests). The unit tests, lint/type checks, and the allocation micro-benchmark were run on the submitter's NVIDIA L4 workstation; the A100 TP=2 memory and correctness results were measured on 2× A100-80GB (nightly image noted above).

Back the default KV-offload backend's worker CPU buffer with the shared
SharedOffloadRegion mmap on CUDA/ROCm instead of a per-rank pinned tensor,
taking PyTorch's CUDACachingHostAllocator (and its power-of-2 rounding of
large pinned allocations, per vllm-project#48436) off the allocation path. Non-CUDA-alike
platforms keep the existing pinned-tensor path. Block accounting is page-size
aligned on both scheduler and worker so num_blocks stays consistent.

This is the allocation-swap prerequisite for the TP-dedup feature in vllm-project#47929;
it introduces no deduplication semantics.

Co-authored-by: Cursor Agent <cursor-agent@cursor.com>
Signed-off-by: Change72 <changg@nvidia.com>
@mergify mergify Bot added the v1 label Jul 28, 2026
@Change72
Change72 marked this pull request as ready for review July 28, 2026 13:28
@Change72
Change72 requested review from ApostaC and orozery as code owners July 28, 2026 13:28

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@Change72
Change72 marked this pull request as draft July 28, 2026 13:31
@Change72
Change72 marked this pull request as ready for review July 28, 2026 15:11
@orozery orozery added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 29, 2026
@orozery
orozery merged commit 542a8fa into vllm-project:main Jul 29, 2026
9 of 11 checks passed
@Change72
Change72 deleted the kv-offload-cpu-shared-region branch July 29, 2026 15:30
lukealonso pushed a commit to local-inference-lab/vllm that referenced this pull request Aug 11, 2026
* fix(kv-offload): backport shared CPU region allocation

Backport the allocation semantics of upstream vLLM vllm-project#50094 onto the Gilded Gnosis offload configuration boundary and adapt its coverage to the heterogeneous KV layout tests.

Co-authored-by: Chang Guo <changg@nvidia.com>

Assisted-by: OpenAI Codex <noreply@openai.com>

* fix(kv-offload): clear failed host registration error

* fix(kv-offload): segment failed host registrations

* docs(cuda): document cleared runtime error code

* fix(kv-offload): isolate host registration failures

* fix(kv-offload): unlink shared regions after mapping

* fix(kv-offload): align host registration segments to KV rows

* docs(kv-offload): document host registration helpers

* fix(kv-offload): preserve tiering shared backing

Keep the named mmap alive until the delayed scheduler has joined it, avoid redundant full-region prefaulting in the scheduler, and clean constructor resources plus rendezvous markers on failure. Add direct and multiprocess coverage for the delayed scheduler contract.
randomvariable pushed a commit to randomvariable/vllm that referenced this pull request Aug 19, 2026
…ct#217)

* fix(kv-offload): backport shared CPU region allocation

Backport the allocation semantics of upstream vLLM vllm-project#50094 onto the Gilded Gnosis offload configuration boundary and adapt its coverage to the heterogeneous KV layout tests.

Co-authored-by: Chang Guo <changg@nvidia.com>

Assisted-by: OpenAI Codex <noreply@openai.com>

* fix(kv-offload): clear failed host registration error

* fix(kv-offload): segment failed host registrations

* docs(cuda): document cleared runtime error code

* fix(kv-offload): isolate host registration failures

* fix(kv-offload): unlink shared regions after mapping

* fix(kv-offload): align host registration segments to KV rows

* docs(kv-offload): document host registration helpers

* fix(kv-offload): preserve tiering shared backing

Keep the named mmap alive until the delayed scheduler has joined it, avoid redundant full-region prefaulting in the scheduler, and clean constructor resources plus rendezvous markers on failure. Add direct and multiprocess coverage for the delayed scheduler contract.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready ONLY add when PR is ready to merge/full CI is needed v1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants