Commit 0e8be6d0 for whisper.cpp
commit 0e8be6d0ef50faa58a6b2591f4d723bfa0f2ba7c
Author: Aman Gupta <amangupta052@gmail.com>
Date: Wed Nov 25 16:56:15 2026 +0800
ggml: allow input async copies (llama/30685)
* ggml: allow input async copies
Copy the host inputs of a split with set_tensor_async followed by a
single synchronize, instead of one blocking copy per input. This is
opt-in per device through the new async_host_input cap, which only
CUDA sets. Other backends keep the current path.
The async path is used without pipeline parallelism only. With
pipeline parallelism, the input backend also waits on the slot event
before an async copy, since CUDA peer copies run on the source stream.
This replaces the synchronize on graph reuse in process_ubatch.
* remove sched change
* tests : add get_props to test-alloc dummy device
* tests : check that sched inputs can be overwritten after compute_async
* ggml : rename async_host_input to fast_async_host_input, address review comments
diff --git a/ggml/include/ggml-backend.h b/ggml/include/ggml-backend.h
index afb86338..a077e221 100644
--- a/ggml/include/ggml-backend.h
+++ b/ggml/include/ggml-backend.h
@@ -161,6 +161,8 @@ extern "C" {
bool events;
// mmap is supported for loading
bool mmap_support;
+ // set_tensor_async from host memory can be ordered with the graph computation and can therefore be faster than a blocking write
+ bool fast_async_host_input;
};
// all the device properties
diff --git a/ggml/src/ggml-backend-meta.cpp b/ggml/src/ggml-backend-meta.cpp
index 42a4b447..c442f603 100644
--- a/ggml/src/ggml-backend-meta.cpp
+++ b/ggml/src/ggml-backend-meta.cpp
@@ -133,6 +133,7 @@ static void ggml_backend_meta_device_get_props(ggml_backend_dev_t dev, ggml_back
/* .buffer_from_host_ptr = */ false, // Not implemented.
/* .events = */ false, // Not implemented.
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
for (ggml_backend_dev_t simple_dev : meta_dev_ctx->simple_devs) {
ggml_backend_dev_props tmp_props;
diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp
index 97f267d4..9a12391b 100644
--- a/ggml/src/ggml-backend.cpp
+++ b/ggml/src/ggml-backend.cpp
@@ -1262,6 +1262,9 @@ struct ggml_backend_sched {
// inputs of the next split read from the backend of the current split before it computes, see ggml_backend_sched_prefetch_inputs
ggml_backend_event_t prefetch_events[GGML_SCHED_MAX_BACKENDS]; // created on first use, NULL if the backend has no events
bool prefetch_tried [GGML_SCHED_MAX_BACKENDS];
+
+ // backends that take host inputs with set_tensor_async, see ggml_backend_dev_caps
+ bool fast_async_host_input[GGML_SCHED_MAX_BACKENDS];
struct ggml_tensor * prefetched[GGML_SCHED_MAX_SPLIT_INPUTS];
int n_prefetched;
int prefetch_split; // the split that reads the prefetched inputs
@@ -2153,7 +2156,8 @@ static bool ggml_backend_sched_is_host_weight(const struct ggml_tensor * t) {
ggml_backend_buffer_is_host(t->buffer);
}
-static void ggml_backend_sched_copy_input(ggml_backend_sched_t sched, struct ggml_backend_sched_split * split, struct ggml_tensor * input) {
+// returns true if the copy is reading asynchronously from host memory, to safely write to the memory a synchronization on the split backend is then needed
+static bool ggml_backend_sched_copy_input(ggml_backend_sched_t sched, struct ggml_backend_sched_split * split, struct ggml_tensor * input) {
const int split_backend_id = split->backend_id;
ggml_backend_t split_backend = sched->backends[split_backend_id];
ggml_backend_t input_backend = ggml_backend_sched_get_tensor_backend(sched, input);
@@ -2164,11 +2168,20 @@ static void ggml_backend_sched_copy_input(ggml_backend_sched_t sched, struct ggm
for (int i = 0; i < sched->n_prefetched; i++) {
if (sched->prefetched[i] == input) {
ggml_backend_event_synchronize(sched->prefetch_events[sched->prefetch_backend_id]);
- return;
+ return false;
}
}
}
+ // without pipeline parallelism the input copy has a single slot, so it can be written in order on the split backend
+ ggml_backend_buffer_t input_buf = input->view_src ? input->view_src->buffer : input->buffer;
+ if (sched->fast_async_host_input[split_backend_id] && sched->events[split_backend_id][sched->cur_copy] == NULL &&
+ ggml_backend_dev_type(ggml_backend_get_device(input_backend)) == GGML_BACKEND_DEVICE_TYPE_CPU &&
+ ggml_backend_buffer_is_host(input_buf) && !ggml_backend_sched_is_host_weight(input) && ggml_nbytes(input) > 0) {
+ ggml_backend_tensor_set_async(split_backend, input_cpy, input->data, 0, ggml_nbytes(input));
+ return true;
+ }
+
if (input->flags & GGML_TENSOR_FLAG_INPUT) {
// inputs from the user must be copied immediately to prevent the user overwriting the data before the copy is done
if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
@@ -2177,7 +2190,7 @@ static void ggml_backend_sched_copy_input(ggml_backend_sched_t sched, struct ggm
ggml_backend_synchronize(split_backend);
}
ggml_backend_tensor_copy(input, input_cpy);
- return;
+ return false;
}
// wait for the split backend to finish using the input before overwriting it
@@ -2189,7 +2202,7 @@ static void ggml_backend_sched_copy_input(ggml_backend_sched_t sched, struct ggm
if (sched->callback_copy != NULL && ggml_backend_sched_is_host_weight(input) &&
sched->callback_copy(split_backend, input, input_cpy, &split->graph, sched->callback_copy_user_data)) {
- return;
+ return false;
}
// try async copy, but if not possible, we can still use a sync copy without synchronizing the dst backend, since we handle the synchronization here with multiple copies and events
@@ -2203,6 +2216,7 @@ static void ggml_backend_sched_copy_input(ggml_backend_sched_t sched, struct ggm
}
ggml_backend_tensor_copy(input, input_cpy);
}
+ return false;
}
// the inputs of the next split that are computed on the backend of this split before it, and copied to host memory,
@@ -2289,12 +2303,17 @@ static enum ggml_status ggml_backend_sched_compute_splits(ggml_backend_sched_t s
// copy the input tensors to the split backend
// the weights in host memory are copied last, so that the copy callback can read the other inputs of the split
+ bool host_read_pending = false;
for (int input_id = 0; input_id < split->n_inputs; input_id++) {
struct ggml_tensor * input = split->inputs[input_id];
if (!ggml_backend_sched_is_host_weight(input) && sched->hv_tensor_requires_copy[hash_id(input)]) {
- ggml_backend_sched_copy_input(sched, split, input);
+ host_read_pending |= ggml_backend_sched_copy_input(sched, split, input);
}
}
+ // the host data of async copies can be overwritten by later splits or by the user after this function returns
+ if (host_read_pending) {
+ ggml_backend_synchronize(split_backend);
+ }
// after the copies above, which may read host memory reused by the copies of the next split
// before the weights, whose copy callback may queue large uploads
ggml_backend_sched_prefetch_inputs(sched, split_id);
@@ -2413,6 +2432,10 @@ ggml_backend_sched_t ggml_backend_sched_new(
sched->bufts[b] = bufts ? bufts[b] : ggml_backend_get_default_buffer_type(backends[b]);
GGML_ASSERT(ggml_backend_supports_buft(backends[b], sched->bufts[b]));
+ struct ggml_backend_dev_props props;
+ ggml_backend_dev_get_props(ggml_backend_get_device(backends[b]), &props);
+ sched->fast_async_host_input[b] = props.caps.fast_async_host_input && backends[b]->iface.set_tensor_async != NULL && !ggml_backend_is_meta(backends[b]);
+
if (sched->n_copies > 1) {
for (int c = 0; c < sched->n_copies; c++) {
sched->events[b][c] = ggml_backend_event_new(backends[b]->device);
diff --git a/ggml/src/ggml-blas/ggml-blas.cpp b/ggml/src/ggml-blas/ggml-blas.cpp
index 6feb370b..285c9684 100644
--- a/ggml/src/ggml-blas/ggml-blas.cpp
+++ b/ggml/src/ggml-blas/ggml-blas.cpp
@@ -370,6 +370,7 @@ static void ggml_backend_blas_device_get_props(ggml_backend_dev_t dev, struct gg
/* .buffer_from_host_ptr = */ true,
/* .events = */ false,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-cann/ggml-cann.cpp b/ggml/src/ggml-cann/ggml-cann.cpp
index 4ea2f216..abed4806 100644
--- a/ggml/src/ggml-cann/ggml-cann.cpp
+++ b/ggml/src/ggml-cann/ggml-cann.cpp
@@ -2847,6 +2847,7 @@ static void ggml_backend_cann_device_get_props(ggml_backend_dev_t dev, ggml_back
/* .buffer_from_host_ptr = */ false,
/* .events = */ true,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-cpu/ggml-cpu.cpp b/ggml/src/ggml-cpu/ggml-cpu.cpp
index 38589810..eadf943b 100644
--- a/ggml/src/ggml-cpu/ggml-cpu.cpp
+++ b/ggml/src/ggml-cpu/ggml-cpu.cpp
@@ -398,6 +398,7 @@ static void ggml_backend_cpu_device_get_props(ggml_backend_dev_t dev, struct ggm
/* .buffer_from_host_ptr = */ true,
/* .events = */ false,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu
index 9116830e..c014bbe5 100644
--- a/ggml/src/ggml-cuda/ggml-cuda.cu
+++ b/ggml/src/ggml-cuda/ggml-cuda.cu
@@ -3259,6 +3259,14 @@ static bool ggml_backend_cuda_cpy_tensor_async(ggml_backend_t backend_src, ggml_
}
if (backend_src != backend_dst) {
+ // the copy runs on the src stream, so it must wait for the work queued on the dst stream that still reads dst
+ if (!cuda_ctx_dst->copy_event) {
+ ggml_cuda_set_device(cuda_ctx_dst->device);
+ CUDA_CHECK(cudaEventCreateWithFlags(&cuda_ctx_dst->copy_event, cudaEventDisableTiming));
+ }
+ CUDA_CHECK(cudaEventRecord(cuda_ctx_dst->copy_event, cuda_ctx_dst->stream()));
+ CUDA_CHECK(cudaStreamWaitEvent(cuda_ctx_src->stream(), cuda_ctx_dst->copy_event, 0));
+
// copy on src stream
// compare the backing physical devices: distinct virtual devices may share one physical GPU,
// in which case a same-device copy (not a peer copy) is required
@@ -6363,6 +6371,7 @@ static void ggml_backend_cuda_device_get_props(ggml_backend_dev_t dev, ggml_back
/* .buffer_from_host_ptr = */ false,
/* .events = */ events,
/* .mmap_support = */ props->type != GGML_BACKEND_DEVICE_TYPE_IGPU,
+ /* .fast_async_host_input = */ true,
};
}
diff --git a/ggml/src/ggml-et/ggml-et.cpp b/ggml/src/ggml-et/ggml-et.cpp
index 42aa7227..57bcb618 100644
--- a/ggml/src/ggml-et/ggml-et.cpp
+++ b/ggml/src/ggml-et/ggml-et.cpp
@@ -1659,6 +1659,7 @@ static void ggml_backend_et_device_get_props(ggml_backend_dev_t dev, struct ggml
/* .buffer_from_host_ptr = */ false,
/* .events = */ false,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-hexagon/ggml-hexagon.cpp b/ggml/src/ggml-hexagon/ggml-hexagon.cpp
index f9daf10f..aa6e6d9a 100644
--- a/ggml/src/ggml-hexagon/ggml-hexagon.cpp
+++ b/ggml/src/ggml-hexagon/ggml-hexagon.cpp
@@ -3100,6 +3100,7 @@ static void ggml_backend_hexagon_device_get_props(ggml_backend_dev_t dev, struct
/* .buffer_from_host_ptr = */ false,
/* .events = */ true,
/* .mmap_support = */ false,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-metal/ggml-metal.cpp b/ggml/src/ggml-metal/ggml-metal.cpp
index 22860653..7aee237c 100644
--- a/ggml/src/ggml-metal/ggml-metal.cpp
+++ b/ggml/src/ggml-metal/ggml-metal.cpp
@@ -782,11 +782,12 @@ static void ggml_backend_metal_device_get_props(ggml_backend_dev_t dev, ggml_bac
ggml_backend_metal_device_get_memory(dev, &props->memory_free, &props->memory_total);
props->caps = {
- /* .async = */ true,
- /* .host_buffer = */ false,
- /* .buffer_from_host_ptr = */ true,
- /* .events = */ true,
- /* .mmap_support = */ true,
+ /* .async = */ true,
+ /* .host_buffer = */ false,
+ /* .buffer_from_host_ptr = */ true,
+ /* .events = */ true,
+ /* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-opencl/ggml-opencl.cpp b/ggml/src/ggml-opencl/ggml-opencl.cpp
index cdcc05f8..18d8b9ce 100644
--- a/ggml/src/ggml-opencl/ggml-opencl.cpp
+++ b/ggml/src/ggml-opencl/ggml-opencl.cpp
@@ -18491,6 +18491,7 @@ static void ggml_backend_opencl_device_get_props(ggml_backend_dev_t dev, struct
/* .buffer_from_host_ptr = */ false,
/* .events = */ false,
/* .mmap_support = */ false,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-openvino/ggml-openvino.cpp b/ggml/src/ggml-openvino/ggml-openvino.cpp
index e1ba0bae..5791ea30 100644
--- a/ggml/src/ggml-openvino/ggml-openvino.cpp
+++ b/ggml/src/ggml-openvino/ggml-openvino.cpp
@@ -1458,6 +1458,7 @@ static void ggml_backend_openvino_device_get_props(ggml_backend_dev_t dev, ggml_
/* .buffer_from_host_ptr = */ is_self_contained_mmap_enabled(),
/* .events = */ false,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-rpc/ggml-rpc.cpp b/ggml/src/ggml-rpc/ggml-rpc.cpp
index 1814002c..b0242545 100644
--- a/ggml/src/ggml-rpc/ggml-rpc.cpp
+++ b/ggml/src/ggml-rpc/ggml-rpc.cpp
@@ -2796,6 +2796,7 @@ static void ggml_backend_rpc_device_get_props(ggml_backend_dev_t dev, struct ggm
/* .buffer_from_host_ptr = */ false,
/* .events = */ true,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-sycl/ggml-sycl.cpp b/ggml/src/ggml-sycl/ggml-sycl.cpp
index 12d1bdfb..2630b8e7 100644
--- a/ggml/src/ggml-sycl/ggml-sycl.cpp
+++ b/ggml/src/ggml-sycl/ggml-sycl.cpp
@@ -7279,6 +7279,7 @@ static void ggml_backend_sycl_device_get_props(ggml_backend_dev_t dev, ggml_back
/* .buffer_from_host_ptr = */ false,
/* .events = */ events,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index 76550d60..f28690f6 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -16674,6 +16674,7 @@ static void ggml_backend_vk_device_get_props(ggml_backend_dev_t dev, struct ggml
/* .buffer_from_host_ptr = */ false,
/* .events = */ true,
/* .mmap_support = */ !ctx->is_integrated_gpu,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-webgpu/ggml-webgpu.cpp b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
index 8bf62e1c..ed7088ae 100644
--- a/ggml/src/ggml-webgpu/ggml-webgpu.cpp
+++ b/ggml/src/ggml-webgpu/ggml-webgpu.cpp
@@ -4694,6 +4694,7 @@ static void ggml_backend_webgpu_device_get_props(ggml_backend_dev_t dev, struct
/* .buffer_from_host_ptr = */ false,
/* .events = */ false,
/* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-zdnn/ggml-zdnn.cpp b/ggml/src/ggml-zdnn/ggml-zdnn.cpp
index ccdd4f38..c4dc23cd 100644
--- a/ggml/src/ggml-zdnn/ggml-zdnn.cpp
+++ b/ggml/src/ggml-zdnn/ggml-zdnn.cpp
@@ -615,11 +615,12 @@ static void ggml_backend_zdnn_device_get_props(ggml_backend_dev_t dev, ggml_back
props->type = ggml_backend_zdnn_device_get_type(dev);
ggml_backend_zdnn_device_get_memory(dev, &props->memory_free, &props->memory_total);
props->caps = {
- /* .async = */ false,
- /* .host_buffer = */ false,
- /* .buffer_from_host_ptr = */ false,
- /* .events = */ false,
- /* .mmap_support = */ true,
+ /* .async = */ false,
+ /* .host_buffer = */ false,
+ /* .buffer_from_host_ptr = */ false,
+ /* .events = */ false,
+ /* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}
diff --git a/ggml/src/ggml-zendnn/ggml-zendnn.cpp b/ggml/src/ggml-zendnn/ggml-zendnn.cpp
index 85f1263f..68f4a124 100644
--- a/ggml/src/ggml-zendnn/ggml-zendnn.cpp
+++ b/ggml/src/ggml-zendnn/ggml-zendnn.cpp
@@ -656,11 +656,12 @@ static void ggml_backend_zendnn_device_get_props(ggml_backend_dev_t dev, struct
props->type = ggml_backend_zendnn_device_get_type(dev);
ggml_backend_zendnn_device_get_memory(dev, &props->memory_free, &props->memory_total);
props->caps = {
- /* .async = */ false,
- /* .host_buffer = */ false,
- /* .buffer_from_host_ptr = */ true,
- /* .events = */ false,
- /* .mmap_support = */ true,
+ /* .async = */ false,
+ /* .host_buffer = */ false,
+ /* .buffer_from_host_ptr = */ true,
+ /* .events = */ false,
+ /* .mmap_support = */ true,
+ /* .fast_async_host_input = */ false,
};
}