diff --git a/ggml/src/ggml-cuda/common.cuh b/ggml/src/ggml-cuda/common.cuh index a06bb678d0d9..ec310c910dc7 100644 --- a/ggml/src/ggml-cuda/common.cuh +++ b/ggml/src/ggml-cuda/common.cuh @@ -1416,15 +1416,14 @@ struct ggml_backend_cuda_context { int curr_stream_no = 0; #ifdef USE_CUDA_GRAPH - // Map from first_node_ptr to cuda_graph - allows multiple graphs per context - // when the computation is split across CPU/GPU (e.g., with --n-cpu-moe) - std::unordered_map> cuda_graphs; + // only the time-based sweep bounds this map: a count cap would evict in-use graphs (tensor split needs ~2 * n_layers + 1 per device and shape) + std::unordered_map> cuda_graphs; int64_t next_graph_eviction_time = 0; int64_t last_graph_use_time = 0; // defined in ggml-cuda.cu, which includes graph.cuh - ggml_cuda_graph * cuda_graph(const void * first_node_ptr, int64_t time_now = ggml_time_us()); + ggml_cuda_graph * cuda_graph(uint64_t graph_key, int64_t time_now = ggml_time_us()); // Check if any CUDA graph is enabled for this context (used by kernels that need to know // if graphs are in use without having access to the specific graph key) diff --git a/ggml/src/ggml-cuda/ggml-cuda.cu b/ggml/src/ggml-cuda/ggml-cuda.cu index f2b3740a520e..4169ef20f904 100644 --- a/ggml/src/ggml-cuda/ggml-cuda.cu +++ b/ggml/src/ggml-cuda/ggml-cuda.cu @@ -866,7 +866,7 @@ ggml_backend_cuda_context::ggml_backend_cuda_context(int device) : } #ifdef USE_CUDA_GRAPH -ggml_cuda_graph * ggml_backend_cuda_context::cuda_graph(const void * first_node_ptr, int64_t time_now) { +ggml_cuda_graph * ggml_backend_cuda_context::cuda_graph(uint64_t graph_key, int64_t time_now) { // 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; @@ -877,7 +877,7 @@ ggml_cuda_graph * ggml_backend_cuda_context::cuda_graph(const void * first_node_ 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 (it->first != first_node_ptr && time_now - it->second->last_used_time >= 10'000'000) { + if (it->first != graph_key && time_now - it->second->last_used_time >= 10'000'000) { it = cuda_graphs.erase(it); } else { ++it; @@ -885,9 +885,9 @@ ggml_cuda_graph * ggml_backend_cuda_context::cuda_graph(const void * first_node_ } } - auto it = cuda_graphs.find(first_node_ptr); + auto it = cuda_graphs.find(graph_key); if (it == cuda_graphs.end()) { - it = cuda_graphs.emplace(first_node_ptr, std::make_unique()).first; + it = cuda_graphs.emplace(graph_key, std::make_unique()).first; } it->second->last_used_time = time_now; return it->second.get(); @@ -3008,14 +3008,33 @@ static bool ggml_cuda_graph_check_compability(ggml_cgraph * cgraph) { return use_cuda_graph; } -static const void * ggml_cuda_graph_get_key(ggml_cgraph * cgraph) { - return cgraph->nodes[0]; +// split = first node's address; shapes = first and last node dims, since a captured graph hard-codes them. +// O(1) on purpose: walking every node would defeat the graph. a collision only re-captures. +static uint64_t ggml_cuda_graph_get_key(ggml_cgraph * cgraph) { + if (cgraph->n_nodes == 0) { + return 0; + } + + uint64_t key = (uint64_t) (uintptr_t) cgraph->nodes[0]; + + auto mix = [&key](uint64_t v) { + key = (key ^ v) * 0x100000001b3ull; + }; + + mix(cgraph->n_nodes); + + for (int d = 0; d < GGML_MAX_DIMS; d++) { + mix(cgraph->nodes[0]->ne[d]); + mix(cgraph->nodes[cgraph->n_nodes - 1]->ne[d]); + } + + return key; } static bool ggml_cuda_graph_update_required(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph) { bool res = false; - const void * graph_key = ggml_cuda_graph_get_key(cgraph); + const uint64_t graph_key = ggml_cuda_graph_get_key(cgraph); ggml_cuda_graph * graph = cuda_ctx->cuda_graph(graph_key); if (cgraph->uid != 0 && @@ -3159,7 +3178,7 @@ static void ggml_cuda_graph_launch_chunk(ggml_backend_cuda_context * cuda_ctx, g } #endif // USE_CUDA_GRAPH -static void ggml_cuda_graph_evaluate_and_capture(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, const bool use_cuda_graph, const bool cuda_graph_update_required, const void * graph_key) { +static void ggml_cuda_graph_evaluate_and_capture(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, const bool use_cuda_graph, const bool cuda_graph_update_required, uint64_t graph_key) { bool graph_evaluated_or_captured = false; // flag used to determine whether it is an integrated_gpu @@ -3389,7 +3408,7 @@ static void ggml_cuda_graph_evaluate_and_capture(ggml_backend_cuda_context * cud } #ifdef USE_CUDA_GRAPH -static bool ggml_cuda_graph_set_enabled(ggml_backend_cuda_context * cuda_ctx, const void * graph_key) { +static bool ggml_cuda_graph_set_enabled(ggml_backend_cuda_context * cuda_ctx, uint64_t graph_key) { ggml_cuda_graph * graph = cuda_ctx->cuda_graph(graph_key); if (graph->instances.empty()) { @@ -3412,7 +3431,7 @@ static enum ggml_status ggml_backend_cuda_graph_compute(ggml_backend_t backend, bool use_cuda_graph = false; bool cuda_graph_update_required = false; - const void * graph_key = nullptr; + uint64_t graph_key = 0; #ifdef USE_CUDA_GRAPH graph_key = ggml_cuda_graph_get_key(cgraph); @@ -3505,7 +3524,7 @@ static void ggml_backend_cuda_graph_optimize(ggml_backend_t backend, ggml_cgraph } #ifdef USE_CUDA_GRAPH - const void * graph_key = ggml_cuda_graph_get_key(cgraph); + const uint64_t graph_key = ggml_cuda_graph_get_key(cgraph); const bool use_cuda_graph = ggml_cuda_graph_set_enabled(cuda_ctx, graph_key); #else const bool use_cuda_graph = false; diff --git a/src/llama-memory-hybrid-idx.cpp b/src/llama-memory-hybrid-idx.cpp index 3866d5980f37..82f7e19538a5 100644 --- a/src/llama-memory-hybrid-idx.cpp +++ b/src/llama-memory-hybrid-idx.cpp @@ -63,10 +63,13 @@ llama_memory_hybrid_idx::llama_memory_hybrid_idx( hparams_idx.n_embd_head_k_mla_impl = model.hparams.indexer_head_size; hparams_idx.n_embd_head_v_mla_impl = model.hparams.indexer_head_size; - LLAMA_LOG_INFO("%s: creating indexer KV cache, size = %u cells\n", __func__, kv_size); + // glm5-next caches compressor gates beside the keys and they feed a softmax, so quantized -ctk stays F16 here + const ggml_type type_idx = model.arch == LLM_ARCH_GLM5_NEXT && ggml_is_quantized(type_k) ? GGML_TYPE_F16 : type_k; + + LLAMA_LOG_INFO("%s: creating indexer KV cache, size = %u cells, type = %s\n", __func__, kv_size, ggml_type_name(type_idx)); return new llama_kv_cache( - model, no_alloc, hparams_idx, type_k, type_v, v_trans, offload, unified, + model, no_alloc, hparams_idx, type_idx, type_v, v_trans, offload, unified, kv_size, n_seq_max, n_swa, swa_type, nullptr, filter_idx, nullptr, nullptr, "idx_"); }()) {}