[Perf] Batch KV cache swap copies via cuMemcpyBatchAsync (#38460)
Signed-off-by: Itay Etelis <itay.etelis@ibm.com> Co-authored-by: Itay Etelis <itay.etelis@ibm.com> Co-authored-by: Or Ozeri <oro@il.ibm.com>
This commit is contained in:
co-authored by
Itay Etelis
Or Ozeri
parent
3bc2734dd0
commit
4a06e1246e
@@ -2641,6 +2641,22 @@ def swap_blocks(
|
||||
torch.ops._C_cache_ops.swap_blocks(src, dst, block_size_in_bytes, block_mapping)
|
||||
|
||||
|
||||
def swap_blocks_batch(
|
||||
src_ptrs: torch.Tensor,
|
||||
dst_ptrs: torch.Tensor,
|
||||
sizes: torch.Tensor,
|
||||
) -> None:
|
||||
"""
|
||||
Batch version of swap_blocks: submit all copies in a single driver call.
|
||||
|
||||
Each entry specifies a raw pointer copy: src_ptrs[i] -> dst_ptrs[i]
|
||||
of sizes[i] bytes. All three tensors must be int64 CPU tensors.
|
||||
On CUDA 12.8+ this uses cuMemcpyBatchAsync for minimal submission
|
||||
overhead; on older CUDA it falls back to a loop of cudaMemcpyAsync.
|
||||
"""
|
||||
torch.ops._C_cache_ops.swap_blocks_batch(src_ptrs, dst_ptrs, sizes)
|
||||
|
||||
|
||||
def convert_fp8(
|
||||
output: torch.Tensor, input: torch.Tensor, scale: float = 1.0, kv_dtype: str = "fp8"
|
||||
) -> None:
|
||||
|
||||
@@ -149,6 +149,17 @@ class SingleDirectionOffloadingHandler(OffloadingHandler):
|
||||
# list of CUDA events available for re-use
|
||||
self._event_pool: list[torch.Event] = []
|
||||
|
||||
# Pre-compute base pointers and block sizes for batch copies.
|
||||
self._src_base_ptrs = np.array(
|
||||
[t.data_ptr() for t in self.src_tensors], dtype=np.int64
|
||||
)
|
||||
self._dst_base_ptrs = np.array(
|
||||
[t.data_ptr() for t in self.dst_tensors], dtype=np.int64
|
||||
)
|
||||
self._block_size_in_bytes_arr = np.array(
|
||||
self.tensor_block_size_in_bytes, dtype=np.int64
|
||||
)
|
||||
|
||||
def transfer_async(self, job_id: int, transfer_spec: TransferSpec) -> bool:
|
||||
src_spec, dst_spec = transfer_spec
|
||||
assert isinstance(src_spec, BlockIDsLoadStoreSpec)
|
||||
@@ -165,15 +176,35 @@ class SingleDirectionOffloadingHandler(OffloadingHandler):
|
||||
|
||||
assert dst_sub_block_count == src_sub_block_count - src_sub_blocks_to_skip
|
||||
|
||||
src_to_dst = np.empty((dst_sub_block_count, 2), dtype=np.int64)
|
||||
src_block_ids = np.empty(dst_sub_block_count, dtype=np.int64)
|
||||
dst_block_ids = np.empty(dst_sub_block_count, dtype=np.int64)
|
||||
expand_block_ids(
|
||||
src_blocks,
|
||||
self.src_block_size_factor,
|
||||
src_to_dst[:, 0],
|
||||
src_block_ids,
|
||||
skip_count=src_sub_blocks_to_skip,
|
||||
)
|
||||
expand_block_ids(dst_blocks, self.dst_block_size_factor, src_to_dst[:, 1])
|
||||
src_to_dst_tensor = torch.from_numpy(src_to_dst)
|
||||
expand_block_ids(dst_blocks, self.dst_block_size_factor, dst_block_ids)
|
||||
|
||||
# Build flat pointer arrays for all tensors × all block pairs.
|
||||
num_pairs = dst_sub_block_count
|
||||
num_tensors = len(self.src_tensors)
|
||||
total = num_pairs * num_tensors
|
||||
|
||||
all_src = np.empty(total, dtype=np.int64)
|
||||
all_dst = np.empty(total, dtype=np.int64)
|
||||
all_sizes = np.empty(total, dtype=np.int64)
|
||||
|
||||
for t_idx, bsz in enumerate(self._block_size_in_bytes_arr):
|
||||
start = t_idx * num_pairs
|
||||
end = start + num_pairs
|
||||
all_src[start:end] = self._src_base_ptrs[t_idx] + src_block_ids * bsz
|
||||
all_dst[start:end] = self._dst_base_ptrs[t_idx] + dst_block_ids * bsz
|
||||
all_sizes[start:end] = bsz
|
||||
|
||||
batch_src = torch.from_numpy(all_src)
|
||||
batch_dst = torch.from_numpy(all_dst)
|
||||
batch_sizes = torch.from_numpy(all_sizes)
|
||||
|
||||
stream = self._stream_pool.pop() if self._stream_pool else torch.cuda.Stream()
|
||||
start_event = (
|
||||
@@ -197,17 +228,8 @@ class SingleDirectionOffloadingHandler(OffloadingHandler):
|
||||
stream.wait_event(last_event)
|
||||
with torch.cuda.stream(stream):
|
||||
start_event.record(stream)
|
||||
for src_tensor, dst_tensor, block_size_in_bytes in zip(
|
||||
self.src_tensors,
|
||||
self.dst_tensors,
|
||||
self.tensor_block_size_in_bytes,
|
||||
):
|
||||
ops.swap_blocks(
|
||||
src_tensor,
|
||||
dst_tensor,
|
||||
block_size_in_bytes,
|
||||
src_to_dst_tensor,
|
||||
)
|
||||
if total > 0:
|
||||
ops.swap_blocks_batch(batch_src, batch_dst, batch_sizes)
|
||||
end_event.record(stream)
|
||||
|
||||
self._transfer_events[job_id] = end_event
|
||||
|
||||
Reference in New Issue
Block a user