From bbc64ca1c57b6b34bd61eb7eacfd4109d32691c5 Mon Sep 17 00:00:00 2001 From: Lumpiasty Date: Mon, 23 Nov 2026 16:20:14 +0100 Subject: [PATCH] ggml-backend: copy the host weights of the next split while the current split computes (expert lookahead) With experts in host memory every host-weight MUL_MAT_ID starts its own split, and with one GPU the sched synchronizes before every expert copy, so upload and compute strictly alternate. A split whose inputs are all host weights, on the backend of the previous split, now gets its copies allocated before the previous split (so they share no memory with it) and issued right after the previous split is submitted. The copies wait on a backend event recorded before the previous split, i.e. for the splits whose memory they may reuse. On Vulkan with the transfer queue the upload then runs on the SDMA queue while the compute queue works. Knob: GGML_SCHED_EXPERT_LOOKAHEAD=0 restores upstream behaviour. Off with the MoE cache (the copy callback writes the cache banks). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PZz44SLQvTXMyWGio6t9DZ --- ggml/include/ggml-backend.h | 3 + ggml/src/ggml-backend.cpp | 141 ++++++++++++++++++++++++++++++------ src/llama-context.cpp | 4 + 3 files changed, 125 insertions(+), 23 deletions(-) diff --git a/ggml/include/ggml-backend.h b/ggml/include/ggml-backend.h index 99650fe06..e767e4785 100644 --- a/ggml/include/ggml-backend.h +++ b/ggml/include/ggml-backend.h @@ -395,6 +395,9 @@ extern "C" { // Set a callback to be called when the inputs weights of a split are being copied GGML_API void ggml_backend_sched_set_copy_callback(ggml_backend_sched_t sched, ggml_backend_sched_copy_callback callback, void * user_data); + // [fork] do not copy the host weights of a split while the previous split computes (on by default, GGML_SCHED_EXPERT_LOOKAHEAD=0 also turns it off) + GGML_API void ggml_backend_sched_disable_expert_lookahead(ggml_backend_sched_t sched); + // Set a callback to be called when the compute buffers of a graph are being reallocated GGML_API void ggml_backend_sched_set_realloc_callback(ggml_backend_sched_t sched, ggml_backend_sched_realloc_callback callback, void * user_data); diff --git a/ggml/src/ggml-backend.cpp b/ggml/src/ggml-backend.cpp index 7d69aedc4..94e291a29 100644 --- a/ggml/src/ggml-backend.cpp +++ b/ggml/src/ggml-backend.cpp @@ -1264,6 +1264,11 @@ struct ggml_backend_sched { int n_prefetched; int prefetch_split; // the split that reads the prefetched inputs int prefetch_backend_id; // the backend that copies them + + // [fork] the host weights of a split are copied while the previous split computes, see ggml_backend_sched_split_lookahead + bool expert_lookahead; // GGML_SCHED_EXPERT_LOOKAHEAD=0 disables + ggml_backend_event_t lookahead_events[GGML_SCHED_MAX_BACKENDS]; // created on first use, NULL if the backend has no events + bool lookahead_tried [GGML_SCHED_MAX_BACKENDS]; }; #define hash_id(tensor) ggml_hash_find_or_insert(&sched->hash_set, tensor) @@ -1488,6 +1493,30 @@ static void ggml_backend_sched_set_if_supported(ggml_backend_sched_t sched, stru } } +static bool ggml_backend_sched_is_host_weight(const struct ggml_tensor * t) { + return t->buffer != NULL && + ggml_backend_buffer_get_usage(t->buffer) == GGML_BACKEND_BUFFER_USAGE_WEIGHTS && + ggml_backend_buffer_is_host(t->buffer); +} + +// [fork] a split whose inputs are all host weights, on the backend of the previous split, copies them while the previous +// split computes: the copies are allocated before the previous split, so they do not share memory with it +static bool ggml_backend_sched_split_lookahead(ggml_backend_sched_t sched, int split_id) { + if (!sched->expert_lookahead || sched->n_copies > 1 || split_id <= 0 || split_id >= sched->n_splits) { + return false; + } + const struct ggml_backend_sched_split * split = &sched->splits[split_id]; + if (split->n_inputs == 0 || split->backend_id != sched->splits[split_id - 1].backend_id) { + return false; + } + for (int j = 0; j < split->n_inputs; j++) { + if (!ggml_backend_sched_is_host_weight(split->inputs[j])) { + return false; + } + } + return true; +} + // assigns backends to ops and splits the graph into subgraphs that can be computed on the same backend void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph) { // reset splits @@ -1942,26 +1971,38 @@ void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgra int n_dep_nodes_added = 0; + auto add_split_input = [&](const struct ggml_backend_sched_split * split, struct ggml_tensor * input) { + assert(graph_copy->size > (graph_copy->n_nodes + 1)); + + const size_t input_id = hash_id(input); + struct ggml_tensor * input_cpy = tensor_id_copy(input_id, split->backend_id, sched->cur_copy); + + // add a dependency to the input source so that it is not freed before the copy is done + struct ggml_tensor * input_dep = ggml_view_tensor(sched->ctx, input); + input_dep->src[0] = input; + sched->node_backend_ids[graph_copy->n_nodes] = sched->hv_tensor_backend_ids[input_id]; + graph_copy->nodes[graph_copy->n_nodes++] = input_dep; + + // add a dependency to the input copy so that it is allocated at the start of the split + sched->node_backend_ids[graph_copy->n_nodes] = split->backend_id; + graph_copy->nodes[graph_copy->n_nodes++] = input_cpy; + }; + for (int i = 0; i < sched->n_splits; i++) { struct ggml_backend_sched_split * split = &sched->splits[i]; // add inputs to the graph copy so that they are allocated by ggml-alloc at the start of the split - for (int j = 0; j < split->n_inputs; j++) { - assert(graph_copy->size > (graph_copy->n_nodes + 1)); - - struct ggml_tensor * input = split->inputs[j]; - const size_t input_id = hash_id(input); - struct ggml_tensor * input_cpy = tensor_id_copy(input_id, split->backend_id, sched->cur_copy); - - // add a dependency to the input source so that it is not freed before the copy is done - struct ggml_tensor * input_dep = ggml_view_tensor(sched->ctx, input); - input_dep->src[0] = input; - sched->node_backend_ids[graph_copy->n_nodes] = sched->hv_tensor_backend_ids[input_id]; - graph_copy->nodes[graph_copy->n_nodes++] = input_dep; - - // add a dependency to the input copy so that it is allocated at the start of the split - sched->node_backend_ids[graph_copy->n_nodes] = split->backend_id; - graph_copy->nodes[graph_copy->n_nodes++] = input_cpy; + // [fork] the inputs of a lookahead split were added before the previous split + if (!ggml_backend_sched_split_lookahead(sched, i)) { + for (int j = 0; j < split->n_inputs; j++) { + add_split_input(split, split->inputs[j]); + } + } + if (ggml_backend_sched_split_lookahead(sched, i + 1)) { + const struct ggml_backend_sched_split * next = &sched->splits[i + 1]; + for (int j = 0; j < next->n_inputs; j++) { + add_split_input(next, next->inputs[j]); + } } for (int j = split->i_start; j < split->i_end; j++) { @@ -2100,12 +2141,6 @@ static bool ggml_backend_sched_alloc_splits(ggml_backend_sched_t sched) { return true; } -static bool ggml_backend_sched_is_host_weight(const struct ggml_tensor * t) { - return t->buffer != NULL && - ggml_backend_buffer_get_usage(t->buffer) == GGML_BACKEND_BUFFER_USAGE_WEIGHTS && - 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) { const int split_backend_id = split->backend_id; ggml_backend_t split_backend = sched->backends[split_backend_id]; @@ -2219,11 +2254,37 @@ static void ggml_backend_sched_prefetch_inputs(ggml_backend_sched_t sched, int s } } +// [fork] copy a host weight of a lookahead split while the previous split computes, without a synchronization +static void ggml_backend_sched_copy_weight_ahead(ggml_backend_sched_t sched, struct ggml_backend_sched_split * split, struct ggml_tensor * input) { + ggml_backend_t split_backend = sched->backends[split->backend_id]; + ggml_backend_t input_backend = ggml_backend_sched_get_tensor_backend(sched, input); + struct ggml_tensor * input_cpy = tensor_copy(input, split->backend_id, sched->cur_copy); + + if (sched->callback_copy != NULL && + sched->callback_copy(split_backend, input, input_cpy, &split->graph, sched->callback_copy_user_data)) { + return; + } + if (!split_backend->iface.cpy_tensor_async || !split_backend->iface.cpy_tensor_async(input_backend, split_backend, input, input_cpy)) { + ggml_backend_synchronize(input_backend); + ggml_backend_synchronize(split_backend); + ggml_backend_tensor_copy(input, input_cpy); + } +} + +static ggml_backend_event_t ggml_backend_sched_lookahead_event(ggml_backend_sched_t sched, int backend_id) { + if (!sched->lookahead_tried[backend_id]) { + sched->lookahead_tried[backend_id] = true; + sched->lookahead_events[backend_id] = ggml_backend_event_new(sched->backends[backend_id]->device); + } + return sched->lookahead_events[backend_id]; +} + static enum ggml_status ggml_backend_sched_compute_splits(ggml_backend_sched_t sched) { GGML_ASSERT(sched); struct ggml_backend_sched_split * splits = sched->splits; int prev_backend_id = -1; + int lookahead_split = -1; // the split whose host weights were copied while the previous split computed for (int split_id = 0; split_id < sched->n_splits; split_id++) { struct ggml_backend_sched_split * split = &splits[split_id]; @@ -2251,13 +2312,22 @@ static enum ggml_status ggml_backend_sched_compute_splits(ggml_backend_sched_t s // 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); - for (int input_id = 0; input_id < split->n_inputs; input_id++) { + for (int input_id = 0; input_id < split->n_inputs && split_id != lookahead_split; 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); } } + // [fork] the copies of the next split may reuse memory of the earlier splits: they wait for this event + ggml_backend_event_t lookahead_event = NULL; + if (sched->callback_eval == NULL && ggml_backend_sched_split_lookahead(sched, split_id + 1)) { + lookahead_event = ggml_backend_sched_lookahead_event(sched, split_backend_id); + if (lookahead_event != NULL) { + ggml_backend_event_record(lookahead_event, split_backend); + } + } + if (!sched->callback_eval) { enum ggml_status ec = ggml_backend_graph_compute_async(split_backend, &split->graph); if (ec != GGML_STATUS_SUCCESS) { @@ -2302,6 +2372,18 @@ static enum ggml_status ggml_backend_sched_compute_splits(ggml_backend_sched_t s ggml_backend_event_record(sched->events[split_backend_id][sched->cur_copy], split_backend); } + if (lookahead_event != NULL) { + ggml_backend_event_wait(split_backend, lookahead_event); + struct ggml_backend_sched_split * next = &splits[split_id + 1]; + for (int input_id = 0; input_id < next->n_inputs; input_id++) { + struct ggml_tensor * input = next->inputs[input_id]; + if (sched->hv_tensor_requires_copy[hash_id(input)]) { + ggml_backend_sched_copy_weight_ahead(sched, next, input); + } + } + lookahead_split = split_id + 1; + } + prev_backend_id = split_backend_id; } @@ -2331,6 +2413,9 @@ ggml_backend_sched_t ggml_backend_sched_new( const char * GGML_SCHED_DEBUG_REALLOC = getenv("GGML_SCHED_DEBUG_REALLOC"); sched->debug_realloc = GGML_SCHED_DEBUG_REALLOC ? atoi(GGML_SCHED_DEBUG_REALLOC) : sched->debug_realloc; + const char * GGML_SCHED_EXPERT_LOOKAHEAD = getenv("GGML_SCHED_EXPERT_LOOKAHEAD"); + sched->expert_lookahead = GGML_SCHED_EXPERT_LOOKAHEAD == NULL || atoi(GGML_SCHED_EXPERT_LOOKAHEAD) != 0; + sched->n_backends = n_backends; sched->n_copies = parallel ? GGML_SCHED_MAX_COPIES : 1; @@ -2390,6 +2475,7 @@ void ggml_backend_sched_free(ggml_backend_sched_t sched) { ggml_backend_event_free(sched->events[b][c]); } ggml_backend_event_free(sched->prefetch_events[b]); + ggml_backend_event_free(sched->lookahead_events[b]); } ggml_gallocr_free(sched->galloc); ggml_free(sched->ctx); @@ -2500,6 +2586,10 @@ void ggml_backend_sched_synchronize(ggml_backend_sched_t sched) { GGML_ASSERT(sched); for (int i = 0; i < sched->n_backends; i++) { ggml_backend_synchronize(sched->backends[i]); + // lets the backend reuse the resources of the recorded events + if (sched->lookahead_events[i] != NULL) { + ggml_backend_event_synchronize(sched->lookahead_events[i]); + } } if (!sched->is_alloc) { // if the graph is not already allocated, always use copy 0 after a synchronization @@ -2521,6 +2611,11 @@ void ggml_backend_sched_set_copy_callback(ggml_backend_sched_t sched, ggml_backe sched->callback_copy_user_data = user_data; } +void ggml_backend_sched_disable_expert_lookahead(ggml_backend_sched_t sched) { + GGML_ASSERT(sched); + sched->expert_lookahead = false; +} + void ggml_backend_sched_set_realloc_callback(ggml_backend_sched_t sched, ggml_backend_sched_realloc_callback callback, void * user_data) { GGML_ASSERT(sched); sched->callback_realloc = callback; diff --git a/src/llama-context.cpp b/src/llama-context.cpp index ce009770f..9c3684ebc 100644 --- a/src/llama-context.cpp +++ b/src/llama-context.cpp @@ -776,6 +776,10 @@ void llama_context::sched_new(const sched_params & params) { ggml_backend_sched_set_copy_callback(sched.get(), sched_copy_experts, this); ggml_backend_sched_set_realloc_callback(sched.get(), sched_graph_realloc, this); + if (moe_cache) { + // [fork] the copy callback updates the cache banks, which the previous split can still read + ggml_backend_sched_disable_expert_lookahead(sched.get()); + } sched_params_cur = params; }