| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Unit tests for the ``ops.swap_blocks_batch`` C++ (cuMemcpyBatchAsync) path.""" |
| 4 | |
| 5 | import pytest |
| 6 | import torch |
| 7 | |
| 8 | from vllm import _custom_ops as ops |
| 9 | from vllm.platforms import current_platform |
| 10 | |
| 11 | |
| 12 | def _addrs(buffers: list[torch.Tensor]) -> torch.Tensor: |
| 13 | return torch.tensor([b.data_ptr() for b in buffers], dtype=torch.int64) |
| 14 | |
| 15 | |
| 16 | def _run_batch(sizes: list[int]) -> None: |
| 17 | src = [torch.randint(256, (s,), dtype=torch.uint8, device="cuda") for s in sizes] |
| 18 | dst = [torch.zeros_like(s) for s in src] |
| 19 | ops.swap_blocks_batch( |
| 20 | _addrs(src), _addrs(dst), torch.tensor(sizes, dtype=torch.int64) |
| 21 | ) |
| 22 | torch.accelerator.synchronize() |
| 23 | for s, d in zip(src, dst): |
| 24 | assert torch.equal(d, s) |
| 25 | |
| 26 | |
| 27 | @pytest.mark.skipif( |
| 28 | not current_platform.is_cuda(), reason="swap_blocks_batch requires CUDA" |
| 29 | ) |
| 30 | def test_swap_blocks_batch_default_stream(): |
| 31 | # cuMemcpyBatchAsync rejects the legacy default stream; the op must fall |
| 32 | # back to per-copy transfers instead of raising. |
| 33 | _run_batch([8, 4096, 8192]) |
| 34 | |
| 35 | |
| 36 | @pytest.mark.skipif( |
| 37 | not current_platform.is_cuda(), reason="swap_blocks_batch requires CUDA" |
| 38 | ) |
| 39 | def test_swap_blocks_batch_dedicated_stream(): |
| 40 | # A dedicated non-default stream exercises the cuMemcpyBatchAsync fast path. |
| 41 | with torch.cuda.stream(torch.cuda.Stream()): |
| 42 | _run_batch([8, 4096, 8192]) |