Skip to content

Commit e1d1ab2

Browse files
committed
fix: ggml_backend_cuda_get/set_tensor_async — fall back to buffer's own method when tensor is non-CUDA
When the scheduler calls get_tensor_async(cuda_backend, tensor) but the tensor's buffer is CPU/other, delegate to the buffer's own get_tensor/set_tensor instead of asserting. This fixes the 'unsupported buffer type' crash when the scheduler tries to read tensors through the wrong backend.
1 parent ae95a7c commit e1d1ab2

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,7 +3261,11 @@ static void ggml_backend_cuda_set_tensor_async(ggml_backend_t backend, ggml_tens
32613261
ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *) backend->context;
32623262
ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
32633263

3264-
GGML_ASSERT(ggml_backend_buft_is_cuda(buf->buft) && "unsupported buffer type");
3264+
if (!ggml_backend_buft_is_cuda(buf->buft)) {
3265+
buf->iface.set_tensor(buf, tensor, data, offset, size);
3266+
return;
3267+
}
3268+
32653269
ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buf->buft->context;
32663270
GGML_ASSERT(buft_ctx->device == cuda_ctx->device && "buffer device mismatch");
32673271

@@ -3272,12 +3276,13 @@ static void ggml_backend_cuda_get_tensor_async(ggml_backend_t backend, const ggm
32723276
ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *) backend->context;
32733277
ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer;
32743278

3275-
GGML_ASSERT(ggml_backend_buft_is_cuda(buf->buft) && "unsupported buffer type");
3276-
ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buf->buft->context;
3277-
if (buft_ctx->device != cuda_ctx->device) {
3278-
fprintf(stderr, "CUDA get_tensor_async: buf dev=%d ctx dev=%d tensor=%s size=%zu\n",
3279-
buft_ctx->device, cuda_ctx->device, tensor->name, size);
3279+
if (!ggml_backend_buft_is_cuda(buf->buft)) {
3280+
// Tensor not on CUDA — use buffer's own get_tensor
3281+
buf->iface.get_tensor(buf, tensor, data, offset, size);
3282+
return;
32803283
}
3284+
3285+
ggml_backend_cuda_buffer_type_context * buft_ctx = (ggml_backend_cuda_buffer_type_context *)buf->buft->context;
32813286
GGML_ASSERT(buft_ctx->device == cuda_ctx->device && "buffer device mismatch");
32823287

32833288
cudaStream_t rb_stream = cuda_ctx->readback_stream_get();

src/llama-context.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,8 +2094,6 @@ llm_graph_result * llama_context::process_ubatch(const llama_ubatch & ubatch, ll
20942094
ggml_backend_sched_reset(sched.get());
20952095
for (int i = 0; i < ggml_graph_n_nodes(phase2_gf); i++)
20962096
ggml_backend_sched_set_tensor_backend(sched.get(), ggml_graph_node(phase2_gf, i), gpu);
2097-
for (int i = 0; i < ggml_graph_n_leafs(phase2_gf); i++)
2098-
ggml_backend_sched_set_tensor_backend(sched.get(), ggml_graph_leaf(phase2_gf, i), gpu);
20992097
force_idxs_to_cpu();
21002098
if (!ggml_backend_sched_alloc_graph(sched.get(), phase2_gf)) {
21012099
ret = GGML_STATUS_ALLOC_FAILED; return nullptr;

0 commit comments

Comments
 (0)