[XPU] Support register KV offload mmap region as pinned host memory on XPU (#51956)
Signed-off-by: Chaojun Zhang <chaojun.zhang@intel.com>
This commit is contained in:
@@ -772,17 +772,14 @@ def test_cleanup_idempotent(iid):
|
||||
def test_cleanup_unregisters_every_pinned_chunk(iid, monkeypatch):
|
||||
"""cleanup() must release every chunk from a split host registration."""
|
||||
r = _make_region(iid)
|
||||
cudart = MagicMock()
|
||||
cudart.cudaHostUnregister.return_value = MagicMock(value=0)
|
||||
monkeypatch.setattr(region_module, "current_platform", MagicMock())
|
||||
region_module.current_platform.is_cuda_alike.return_value = True
|
||||
monkeypatch.setattr(region_module.torch.cuda, "cudart", lambda: cudart)
|
||||
unregister = MagicMock()
|
||||
monkeypatch.setattr(region_module, "host_unregister", unregister)
|
||||
r.pinned_addresses = [0x100000, 0x200000, 0x300000]
|
||||
r.is_pinned = True
|
||||
|
||||
r.cleanup()
|
||||
|
||||
assert cudart.cudaHostUnregister.call_args_list == [
|
||||
assert unregister.call_args_list == [
|
||||
call(0x300000),
|
||||
call(0x200000),
|
||||
call(0x100000),
|
||||
@@ -791,40 +788,35 @@ def test_cleanup_unregisters_every_pinned_chunk(iid, monkeypatch):
|
||||
|
||||
@pytest.fixture
|
||||
def host_register(monkeypatch):
|
||||
"""Pretend to run on CUDA with host registrations capped at seven pages.
|
||||
|
||||
Registration goes to a mock CudaRTLibrary and the region's cleanup to a
|
||||
separate mock torch runtime. Both stay installed through the cleanup that
|
||||
``_region`` runs on exit.
|
||||
"""Mock the cross-platform host_register/host_unregister helpers with
|
||||
registrations capped at seven pages. The same unregister mock backs both
|
||||
pin_mmap_region's rollback path and the region's cleanup() on exit from
|
||||
``_region``.
|
||||
"""
|
||||
cudart = MagicMock()
|
||||
cudart.cudaHostRegister.return_value = 0
|
||||
cudart.cudaHostUnregister.return_value = 0
|
||||
cudart.cudaGetLastError.return_value = 2
|
||||
torch_cudart = MagicMock()
|
||||
torch_cudart.cudaHostUnregister.return_value = MagicMock(value=0)
|
||||
monkeypatch.setattr(gpu_worker.current_platform, "is_cuda_alike", lambda: True)
|
||||
register = MagicMock(return_value=True)
|
||||
unregister = MagicMock()
|
||||
monkeypatch.setattr(gpu_worker, "MAX_HOST_REGISTER_CHUNK_BYTES", 7 * PAGE_SIZE)
|
||||
monkeypatch.setattr(gpu_worker, "CudaRTLibrary", lambda: cudart)
|
||||
monkeypatch.setattr(region_module.torch.cuda, "cudart", lambda: torch_cudart)
|
||||
return cudart, torch_cudart
|
||||
monkeypatch.setattr(gpu_worker, "host_register", register)
|
||||
monkeypatch.setattr(gpu_worker, "host_unregister", unregister)
|
||||
monkeypatch.setattr(region_module, "host_unregister", unregister)
|
||||
return register, unregister
|
||||
|
||||
|
||||
def test_pin_mmap_region_registers_row_aligned_chunks(iid, host_register):
|
||||
"""Chunks end on row boundaries: 3-page rows under a 7-page cap register
|
||||
as 6 + 6 + 3 pages, where a raw byte cap would give 7 + 7 + 1."""
|
||||
cudart, torch_cudart = host_register
|
||||
register, unregister = host_register
|
||||
with _region(iid, num_chunks=5, cpu_page_size=3 * PAGE_SIZE) as region:
|
||||
gpu_worker.pin_mmap_region(region)
|
||||
base = region._base.data_ptr()
|
||||
assert cudart.mock_calls == [
|
||||
call.cudaHostRegister(base, 6 * PAGE_SIZE),
|
||||
call.cudaHostRegister(base + 6 * PAGE_SIZE, 6 * PAGE_SIZE),
|
||||
call.cudaHostRegister(base + 12 * PAGE_SIZE, 3 * PAGE_SIZE),
|
||||
assert register.call_args_list == [
|
||||
call(base, 6 * PAGE_SIZE),
|
||||
call(base + 6 * PAGE_SIZE, 6 * PAGE_SIZE),
|
||||
call(base + 12 * PAGE_SIZE, 3 * PAGE_SIZE),
|
||||
]
|
||||
assert region.is_pinned
|
||||
|
||||
assert torch_cudart.cudaHostUnregister.call_args_list == [
|
||||
assert unregister.call_args_list == [
|
||||
call(base + 12 * PAGE_SIZE),
|
||||
call(base + 6 * PAGE_SIZE),
|
||||
call(base),
|
||||
@@ -833,10 +825,10 @@ def test_pin_mmap_region_registers_row_aligned_chunks(iid, host_register):
|
||||
|
||||
@pytest.mark.parametrize("fail_at", [0, 1, 2])
|
||||
def test_pin_mmap_region_failure_leaves_region_pageable(iid, host_register, fail_at):
|
||||
"""A failed chunk drains its pending error and unregisters the chunks
|
||||
before it on the same handle, so the region is pinned whole or not at all."""
|
||||
cudart, torch_cudart = host_register
|
||||
cudart.cudaHostRegister.side_effect = [0] * fail_at + [2]
|
||||
"""A failed chunk unregisters the chunks before it on the same range, so
|
||||
the region is pinned whole or not at all."""
|
||||
register, unregister = host_register
|
||||
register.side_effect = [True] * fail_at + [False]
|
||||
with _region(iid, num_chunks=5, cpu_page_size=3 * PAGE_SIZE) as region:
|
||||
gpu_worker.pin_mmap_region(region)
|
||||
base = region._base.data_ptr()
|
||||
@@ -845,25 +837,22 @@ def test_pin_mmap_region_failure_leaves_region_pageable(iid, host_register, fail
|
||||
|
||||
registered = [base + i * 6 * PAGE_SIZE for i in range(fail_at)]
|
||||
failed_size = min(6, 15 - 6 * fail_at) * PAGE_SIZE
|
||||
assert cudart.mock_calls == [
|
||||
*(call.cudaHostRegister(address, 6 * PAGE_SIZE) for address in registered),
|
||||
call.cudaHostRegister(base + fail_at * 6 * PAGE_SIZE, failed_size),
|
||||
call.cudaGetLastError(),
|
||||
*(call.cudaHostUnregister(address) for address in reversed(registered)),
|
||||
assert register.call_args_list == [
|
||||
*(call(address, 6 * PAGE_SIZE) for address in registered),
|
||||
call(base + fail_at * 6 * PAGE_SIZE, failed_size),
|
||||
]
|
||||
assert unregister.call_args_list == [
|
||||
call(address) for address in reversed(registered)
|
||||
]
|
||||
torch_cudart.cudaHostUnregister.assert_not_called()
|
||||
|
||||
|
||||
def test_pin_mmap_region_without_cuda_runtime_stays_pageable(
|
||||
iid, host_register, monkeypatch
|
||||
def test_pin_mmap_region_when_host_register_unsupported_stays_pageable(
|
||||
iid, monkeypatch
|
||||
):
|
||||
"""A CUDA runtime that cannot be loaded leaves the region pageable instead
|
||||
of failing worker startup."""
|
||||
|
||||
def missing_runtime():
|
||||
raise AssertionError("libcudart is not loaded in the current process")
|
||||
|
||||
monkeypatch.setattr(gpu_worker, "CudaRTLibrary", missing_runtime)
|
||||
"""A platform without host registration support (host_register()
|
||||
returning False) leaves the region pageable instead of failing worker
|
||||
startup."""
|
||||
monkeypatch.setattr(gpu_worker, "host_register", lambda ptr, num_bytes: False)
|
||||
with _region(iid) as region:
|
||||
gpu_worker.pin_mmap_region(region)
|
||||
assert not region.is_pinned
|
||||
|
||||
@@ -307,6 +307,7 @@ def test_cpu_spec_replicated_disabled_without_shared_region(
|
||||
# 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_xpu", lambda: False)
|
||||
monkeypatch.setattr(cpu_spec_module.current_platform, "is_cuda_alike", lambda: rocm)
|
||||
monkeypatch.setattr(cpu_spec_module.current_platform, "is_rocm", lambda: rocm)
|
||||
worker_kv_bytes_per_block = SharedOffloadRegion.BLOCK_SIZE_ALIGNMENT
|
||||
@@ -341,6 +342,7 @@ def test_cpu_spec_replicated_layout_truth_matrix(
|
||||
# actually allocates on the shared region.
|
||||
import vllm.v1.kv_offload.cpu.spec as cpu_spec_module
|
||||
|
||||
monkeypatch.setattr(cpu_spec_module.current_platform, "is_xpu", lambda: False)
|
||||
monkeypatch.setattr(
|
||||
cpu_spec_module.current_platform, "is_cuda_alike", lambda: cuda_alike
|
||||
)
|
||||
@@ -417,6 +419,7 @@ def test_cpu_spec_create_worker_uses_tensor_path_without_shared_region(
|
||||
worker_calls.append(kwargs)
|
||||
return MagicMock()
|
||||
|
||||
monkeypatch.setattr(cpu_spec_module.current_platform, "is_xpu", lambda: False)
|
||||
monkeypatch.setattr(cpu_spec_module.current_platform, "is_cuda_alike", lambda: rocm)
|
||||
monkeypatch.setattr(cpu_spec_module.current_platform, "is_rocm", lambda: rocm)
|
||||
monkeypatch.setattr(cpu_spec_module, "SharedOffloadRegion", fake_region_ctor)
|
||||
|
||||
Reference in New Issue
Block a user