[XPU] Support EC connector KV Offloading on XPU (#49532)
Signed-off-by: Chaojun Zhang <chaojun.zhang@intel.com>
This commit is contained in:
@@ -102,16 +102,15 @@ class ECCPUWorker:
|
||||
|
||||
assert self._save_count + len(block_ids) <= self._save_bufs.src_ptrs.numel()
|
||||
|
||||
src_ptrs, dst_ptrs, sizes = self._save_bufs
|
||||
bufs = self._save_bufs
|
||||
src_base = src.view(-1).view(torch.uint8).data_ptr()
|
||||
dst_base = self._region.blocks.data_ptr()
|
||||
idx = self._save_count
|
||||
|
||||
for i, block_idx in enumerate(block_ids):
|
||||
start = i * block_size
|
||||
src_ptrs[idx] = src_base + start
|
||||
dst_ptrs[idx] = dst_base + block_idx * block_size
|
||||
sizes[idx] = min(block_size, total_bytes - start)
|
||||
bufs.set_ptrs(idx, src_base + start, dst_base + block_idx * block_size)
|
||||
bufs.sizes[idx] = min(block_size, total_bytes - start)
|
||||
idx += 1
|
||||
|
||||
self._save_count = idx
|
||||
@@ -123,7 +122,7 @@ class ECCPUWorker:
|
||||
|
||||
bufs = self._save_bufs
|
||||
assert bufs is not None
|
||||
src_ptrs, dst_ptrs, sizes = bufs
|
||||
src_ptrs, dst_ptrs, sizes = bufs.src_ptrs, bufs.dst_ptrs, bufs.sizes
|
||||
n = self._save_count
|
||||
swap_blocks_batch(src_ptrs[:n], dst_ptrs[:n], sizes[:n])
|
||||
|
||||
@@ -173,8 +172,11 @@ class ECCPUWorker:
|
||||
op_idx = 0
|
||||
for block_ids in load_items.values():
|
||||
for block_idx in block_ids:
|
||||
src_ptrs[op_idx] = src_base + block_idx * block_size
|
||||
dst_ptrs[op_idx] = dst_buf_base + op_idx * block_size
|
||||
bufs.set_ptrs(
|
||||
op_idx,
|
||||
src_base + block_idx * block_size,
|
||||
dst_buf_base + op_idx * block_size,
|
||||
)
|
||||
op_idx += 1
|
||||
|
||||
swap_blocks_batch(src_ptrs, dst_ptrs, sizes, is_src_access_order_any=True)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Reusable pool of (src_ptrs, dst_ptrs, sizes) int64 tensor triples.
|
||||
"""Reusable pool of (src_ptrs, dst_ptrs, sizes) tensor triples.
|
||||
|
||||
Used by ECCPUWorker to batch swap_blocks_batch descriptors without
|
||||
per-step allocation overhead.
|
||||
@@ -8,20 +8,46 @@ per-step allocation overhead.
|
||||
|
||||
from typing import NamedTuple
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
# CUDA/ROCm cache_kernels.cu requires int64 pointers; the XPU DMA engine
|
||||
# requires uint64 (see vllm._custom_ops.swap_blocks_batch).
|
||||
_PTR_DTYPE = torch.uint64 if current_platform.is_xpu() else torch.int64
|
||||
|
||||
|
||||
class DescriptorBuffers(NamedTuple):
|
||||
src_ptrs: torch.Tensor
|
||||
dst_ptrs: torch.Tensor
|
||||
sizes: torch.Tensor
|
||||
# Numpy aliases of src_ptrs/dst_ptrs, written via set_ptrs().
|
||||
src_np: np.ndarray
|
||||
dst_np: np.ndarray
|
||||
|
||||
def set_ptrs(self, idx: int, src: int, dst: int) -> None:
|
||||
"""Record the source and destination address of descriptor *idx*.
|
||||
|
||||
TODO(torch>=2.14): drop this indirection and assign the tensors
|
||||
directly once the minimum supported torch is 2.14. The numpy detour
|
||||
exists only because torch's setitem unpacks the value as a signed
|
||||
long long before 2.14 (pytorch#191458), rejecting XPU USM pointers
|
||||
>= 2**63, while the two's-complement rewrite those versions accept is
|
||||
in turn rejected by uint64 from 2.14 on. Numpy casts against the
|
||||
array dtype and so works on either. `sizes` holds byte counts and
|
||||
needs no such care.
|
||||
"""
|
||||
self.src_np[idx] = src
|
||||
self.dst_np[idx] = dst
|
||||
|
||||
|
||||
class DescriptorBufferPool:
|
||||
"""Pool of descriptor buffer triples for swap_blocks_batch.
|
||||
|
||||
Each buffer is a `DescriptorBuffers` namedtuple of three 1-D int64
|
||||
tensors of equal length. Buffers are recycled across steps; if a
|
||||
Each buffer is a `DescriptorBuffers` namedtuple of three 1-D tensors
|
||||
(dtype `_PTR_DTYPE`, platform-dependent) of equal length, paired with
|
||||
numpy aliases used to fill them. Buffers are recycled across steps; if a
|
||||
returned buffer is too small it is discarded and a fresh one allocated.
|
||||
"""
|
||||
|
||||
@@ -35,11 +61,8 @@ class DescriptorBufferPool:
|
||||
bufs = self._pool.pop()
|
||||
if bufs.src_ptrs.numel() >= n:
|
||||
return bufs
|
||||
return DescriptorBuffers(
|
||||
torch.empty(n, dtype=torch.int64),
|
||||
torch.empty(n, dtype=torch.int64),
|
||||
torch.empty(n, dtype=torch.int64),
|
||||
)
|
||||
src, dst, sizes = (torch.empty(n, dtype=_PTR_DTYPE) for _ in range(3))
|
||||
return DescriptorBuffers(src, dst, sizes, src.numpy(), dst.numpy())
|
||||
|
||||
def release(self, bufs: DescriptorBuffers) -> None:
|
||||
"""Return a buffer triple to the pool for reuse."""
|
||||
|
||||
Reference in New Issue
Block a user