Commit 00f113ece for llama.cpp
commit 00f113eceb2758158582d56f98bf476b2fe3ed32
Author: hesiru <siruhe666@gmail.com>
Date: Tue Nov 3 14:06:03 2026 +0800
cuda : prevent premature eviction of graphs about to be used (#30662)
* cuda : prevent premature eviction of graphs about to be used
* add last_graph_use_time for entire cuda graph map and testcase for cuda graph
Assisted-by: Codex
* delete the test
diff --git a/ggml/src/ggml-cuda/common.cuh b/ggml/src/ggml-cuda/common.cuh
index a46c4d3f0..00476f0fd 100644
--- a/ggml/src/ggml-cuda/common.cuh
+++ b/ggml/src/ggml-cuda/common.cuh
@@ -1469,16 +1469,21 @@ struct ggml_backend_cuda_context {
// when the computation is split across CPU/GPU (e.g., with --n-cpu-moe)
std::unordered_map<const void *, std::unique_ptr<ggml_cuda_graph>> cuda_graphs;
- int64_t last_graph_eviction_sweep = 0;
+ int64_t next_graph_eviction_time = 0;
+ int64_t last_graph_use_time = 0;
- ggml_cuda_graph * cuda_graph(const void * first_node_ptr) {
- const int64_t time_now = ggml_time_us();
+ ggml_cuda_graph * cuda_graph(const void * first_node_ptr, int64_t time_now = ggml_time_us()) {
+ // Give all cached graphs 10s to be reused after an idle period, without updating each graph.
+ if (!cuda_graphs.empty() && time_now - last_graph_use_time >= 10'000'000) {
+ next_graph_eviction_time = time_now + 10'000'000;
+ }
+ last_graph_use_time = time_now;
- // sweep every 5s, evicting cuda graphs unused for >=10s
- if (time_now - last_graph_eviction_sweep >= 5'000'000) {
- last_graph_eviction_sweep = time_now;
+ // Sweep every 5s during active use, evicting graphs unused for at least 10s.
+ if (time_now >= next_graph_eviction_time) {
+ next_graph_eviction_time = time_now + 5'000'000;
for (auto it = cuda_graphs.begin(); it != cuda_graphs.end(); ) {
- if (time_now - it->second->last_used_time >= 10'000'000) {
+ if (it->first != first_node_ptr && time_now - it->second->last_used_time >= 10'000'000) {
it = cuda_graphs.erase(it);
} else {
++it;