Skip to content

Commit 16d66aa

Browse files
committed
ggml-cuda: fix ROCm multi-GPU illegal memory access in recurrent state restore
Cherry-pick of ggml-org#21170. Remove early-return optimization in ggml_cuda_set_device() that caused hipErrorIllegalAddress on ROCm multi-GPU setups when hipGetDevice() returns an unexpected value on threads with uninitialized device context. Also add missing ggml_cuda_set_device() in ggml_backend_cuda_set_tensor_async() for consistency with all other cudaMemcpyAsync call sites. Fixes GPU memory access fault during KV cache checkpoint restore on multi-turn agentic workflows with multi-GPU row split.
1 parent 991c220 commit 16d66aa

1 file changed

Lines changed: 3 additions & 9 deletions

File tree

ggml/src/ggml-cuda/ggml-cuda.cu

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,9 @@ void ggml_cuda_error(const char * stmt, const char * func, const char * file, in
9999
GGML_ABORT(GGML_CUDA_NAME " error");
100100
}
101101

102-
// this is faster on Windows
103-
// probably because the Windows CUDA libraries forget to make this check before invoking the drivers
102+
// always set device explicitly — early-return optimization is unsafe on ROCm multi-GPU
103+
// with uninitialized thread contexts (see https://github.com/ggml-org/llama.cpp/issues/21140)
104104
void ggml_cuda_set_device(int device) {
105-
int current_device;
106-
CUDA_CHECK(cudaGetDevice(&current_device));
107-
108-
if (device == current_device) {
109-
return;
110-
}
111-
112105
CUDA_CHECK(cudaSetDevice(device));
113106
}
114107

@@ -3022,6 +3015,7 @@ static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tens
30223015

30233016
GGML_ASSERT(buf->buft == ggml_backend_cuda_buffer_type(cuda_ctx->device) && "unsupported buffer type");
30243017

3018+
ggml_cuda_set_device(cuda_ctx->device);
30253019
CUDA_CHECK(cudaMemcpyAsync((char *) tensor->data + offset, data, size, cudaMemcpyHostToDevice, cuda_ctx->stream()));
30263020
}
30273021

0 commit comments

Comments
 (0)