[Bugfix] Runtime driver check for cuMemcpyBatchAsync in swap_blocks_batch (#38919)
Signed-off-by: Itay Etelis <itay.etelis@ibm.com> Co-authored-by: Itay Etelis <itay.etelis@ibm.com> Co-authored-by: Michael Goin <mgoin64@gmail.com>
This commit is contained in:
co-authored by
Itay Etelis
Michael Goin
parent
59b2f7b640
commit
bd8bd52308
+42
-30
@@ -104,37 +104,49 @@ void swap_blocks_batch(const torch::Tensor& src_ptrs,
|
|||||||
static_assert(sizeof(CUdeviceptr) == sizeof(int64_t));
|
static_assert(sizeof(CUdeviceptr) == sizeof(int64_t));
|
||||||
static_assert(sizeof(size_t) == sizeof(int64_t));
|
static_assert(sizeof(size_t) == sizeof(int64_t));
|
||||||
#if !defined(USE_ROCM) && defined(CUDA_VERSION) && CUDA_VERSION >= 12080
|
#if !defined(USE_ROCM) && defined(CUDA_VERSION) && CUDA_VERSION >= 12080
|
||||||
CUmemcpyAttributes attr = {};
|
// Resolve cuMemcpyBatchAsync at runtime via cuGetProcAddress so that
|
||||||
attr.srcAccessOrder = CU_MEMCPY_SRC_ACCESS_ORDER_STREAM;
|
// binaries compiled with CUDA 12.8+ still work on older drivers, and
|
||||||
size_t attrs_idx = 0;
|
// we avoid the CUDA 13.0 header remapping (#define to _v2 signature).
|
||||||
#if defined(CUDA_VERSION) && CUDA_VERSION >= 13000
|
// The function pointer is cached after the first call.
|
||||||
CUresult result = cuMemcpyBatchAsync(
|
using BatchFn =
|
||||||
reinterpret_cast<CUdeviceptr*>(dst_data),
|
CUresult (*)(CUdeviceptr*, CUdeviceptr*, size_t*, size_t,
|
||||||
reinterpret_cast<CUdeviceptr*>(src_data),
|
CUmemcpyAttributes*, size_t*, size_t, size_t*, CUstream);
|
||||||
reinterpret_cast<size_t*>(size_data), static_cast<size_t>(n), &attr,
|
static BatchFn batch_fn = []() -> BatchFn {
|
||||||
&attrs_idx, 1, static_cast<CUstream>(stream));
|
CUdriverProcAddressQueryResult sym_status;
|
||||||
TORCH_CHECK(result == CUDA_SUCCESS, "cuMemcpyBatchAsync failed with error ",
|
void* fn_ptr = nullptr;
|
||||||
result);
|
CUresult res = cuGetProcAddress("cuMemcpyBatchAsync", &fn_ptr, 12080,
|
||||||
#else
|
CU_GET_PROC_ADDRESS_DEFAULT, &sym_status);
|
||||||
size_t fail_idx = 0;
|
if (res != CUDA_SUCCESS || fn_ptr == nullptr) {
|
||||||
CUresult result = cuMemcpyBatchAsync(
|
return nullptr;
|
||||||
reinterpret_cast<CUdeviceptr*>(dst_data),
|
}
|
||||||
reinterpret_cast<CUdeviceptr*>(src_data),
|
return reinterpret_cast<BatchFn>(fn_ptr);
|
||||||
reinterpret_cast<size_t*>(size_data), static_cast<size_t>(n), &attr,
|
}();
|
||||||
&attrs_idx, 1, &fail_idx, static_cast<CUstream>(stream));
|
|
||||||
TORCH_CHECK(result == CUDA_SUCCESS, "cuMemcpyBatchAsync failed at index ",
|
if (batch_fn != nullptr) {
|
||||||
fail_idx, " with error ", result);
|
CUmemcpyAttributes attr = {};
|
||||||
#endif
|
attr.srcAccessOrder = CU_MEMCPY_SRC_ACCESS_ORDER_STREAM;
|
||||||
#else
|
size_t attrs_idx = 0;
|
||||||
// Fallback for CUDA < 12.8 and ROCm: individual async copies.
|
size_t fail_idx = 0;
|
||||||
// cudaMemcpyDefault lets the driver infer direction from pointer types.
|
CUresult result = batch_fn(reinterpret_cast<CUdeviceptr*>(dst_data),
|
||||||
for (int64_t i = 0; i < n; i++) {
|
reinterpret_cast<CUdeviceptr*>(src_data),
|
||||||
cudaMemcpyAsync(reinterpret_cast<void*>(dst_data[i]),
|
reinterpret_cast<size_t*>(size_data),
|
||||||
reinterpret_cast<void*>(src_data[i]),
|
static_cast<size_t>(n), &attr, &attrs_idx, 1,
|
||||||
static_cast<size_t>(size_data[i]), cudaMemcpyDefault,
|
&fail_idx, static_cast<CUstream>(stream));
|
||||||
stream);
|
TORCH_CHECK(result == CUDA_SUCCESS, "cuMemcpyBatchAsync failed at index ",
|
||||||
}
|
fail_idx, " with error ", result);
|
||||||
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
// Fallback for CUDA < 12.8, older drivers, and ROCm:
|
||||||
|
// individual async copies.
|
||||||
|
// cudaMemcpyDefault lets the driver infer direction from pointer types.
|
||||||
|
for (int64_t i = 0; i < n; i++) {
|
||||||
|
cudaMemcpyAsync(reinterpret_cast<void*>(dst_data[i]),
|
||||||
|
reinterpret_cast<void*>(src_data[i]),
|
||||||
|
static_cast<size_t>(size_data[i]), cudaMemcpyDefault,
|
||||||
|
stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace vllm {
|
namespace vllm {
|
||||||
|
|||||||
Reference in New Issue
Block a user