From 66d06535d9cc7f171306ec44bf60a6349a4b9505 Mon Sep 17 00:00:00 2001 From: Carmen Esteban Date: Sun, 5 Apr 2026 18:23:29 +0200 Subject: [PATCH] Phase 2b: persistent compute buffer + threadpool for AR decode - Pre-allocate compute buffer once, reuse across decode steps (no malloc/free) - Create persistent ggml_threadpool with strict CPU binding + high priority - Pre-allocate graph work buffer, grow only when needed - Use ggml_graph_plan + ggml_graph_compute instead of ggml_graph_compute_with_ctx to leverage the persistent threadpool Eliminates per-token overhead: - malloc/free of ~500MB compute buffer - pthread creation/destruction (12 threads) - work buffer allocation Co-Authored-By: Claude Opus 4.6 --- src/ar-cache.h | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/ar-graph.cpp | 19 +++++++++++++++---- src/ar-sampler.cpp | 18 ++++++++++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/ar-cache.h b/src/ar-cache.h index a5487a6..99f8407 100644 --- a/src/ar-cache.h +++ b/src/ar-cache.h @@ -14,6 +14,11 @@ // // K and V are stored BEFORE GQA expansion (n_head_kv, not n_head) // to save memory. GQA repeat happens in the graph at attention time. +// +// Also holds persistent compute resources (buffer, threadpool, work data) +// to avoid malloc/free and thread creation overhead each decode step. + +struct ggml_threadpool; // forward declaration struct ar_kv_cache { bool initialized = false; @@ -29,6 +34,13 @@ struct ar_kv_cache { std::vector> K; // K[layer] std::vector> V; // V[layer] + // ── Persistent compute resources (avoid malloc/free per step) ── + void * compute_buf = nullptr; // pre-allocated GGML context buffer + size_t compute_buf_size = 0; + uint8_t * work_buf = nullptr; // pre-allocated graph work buffer + size_t work_buf_size = 0; + struct ggml_threadpool * threadpool = nullptr; // persistent thread pool + // ── Lifecycle ──────────────────────────────────────────────── void init(int ctx_max, int n_lay, int n_eh, int n_hkv) { @@ -57,6 +69,14 @@ struct ar_kv_cache { V.clear(); n_past = 0; initialized = false; + free_compute_resources(); + } + + void free_compute_resources() { + if (compute_buf) { free(compute_buf); compute_buf = nullptr; compute_buf_size = 0; } + if (work_buf) { free(work_buf); work_buf = nullptr; work_buf_size = 0; } + // threadpool freed externally (owned by caller who created it) + threadpool = nullptr; } // Reset to empty (keep allocation) @@ -74,6 +94,30 @@ struct ar_kv_cache { return (size_t)2 * n_layer * n_ctx_max * pos_stride() * sizeof(float); } + // ── Ensure compute buffer is large enough ───────────────────── + void ensure_compute_buf(size_t needed) { + if (needed <= compute_buf_size) return; + free(compute_buf); + compute_buf_size = needed; + compute_buf = malloc(compute_buf_size); + if (!compute_buf) { + DIFFUSE_DIE("ar_kv_cache: failed to allocate compute buffer (%zu MB)", + compute_buf_size / (1024 * 1024)); + } + } + + // ── Ensure work buffer is large enough ──────────────────────── + void ensure_work_buf(size_t needed) { + if (needed <= work_buf_size) return; + free(work_buf); + work_buf_size = needed; + work_buf = (uint8_t *)malloc(work_buf_size); + if (!work_buf) { + DIFFUSE_DIE("ar_kv_cache: failed to allocate work buffer (%zu MB)", + work_buf_size / (1024 * 1024)); + } + } + // ── Append new K,V for n_new tokens at positions [n_past, n_past+n_new) ── // // K_new, V_new: [n_embd_head, n_head_kv, n_new] (GGML layout, pre-GQA) @@ -91,7 +135,6 @@ struct ar_kv_cache { memmove(V[layer].data(), V[layer].data() + n_new * stride, shift_bytes); memcpy(K[layer].data() + (n_ctx_max - n_new) * stride, K_new, new_bytes); memcpy(V[layer].data() + (n_ctx_max - n_new) * stride, V_new, new_bytes); - // n_past stays at n_ctx_max (updated by caller only once after all layers) return; } diff --git a/src/ar-graph.cpp b/src/ar-graph.cpp index 58247e2..16818a6 100644 --- a/src/ar-graph.cpp +++ b/src/ar-graph.cpp @@ -256,14 +256,17 @@ static bool ar_forward_impl( buf_size += 256ull * 1024 * 1024; buf_size = (size_t)(buf_size * 1.2); + // Use persistent compute buffer if available (avoids malloc/free each step) + cache->ensure_compute_buf(buf_size); + struct ggml_init_params cparams = { - /*.mem_size = */ buf_size, - /*.mem_buffer = */ nullptr, + /*.mem_size = */ cache->compute_buf_size, + /*.mem_buffer = */ cache->compute_buf, /*.no_alloc = */ false, }; struct ggml_context * ctx_compute = ggml_init(cparams); if (!ctx_compute) { - DIFFUSE_LOG("ar_forward: failed to allocate compute context (%zu MB)", + DIFFUSE_LOG("ar_forward: failed to init compute context (%zu MB)", buf_size / (1024 * 1024)); return false; } @@ -271,7 +274,15 @@ static bool ar_forward_impl( struct ggml_cgraph * gf = ar_build_graph(ctx, ctx_compute, tokens, n_new, n_past, cache); - enum ggml_status status = ggml_graph_compute_with_ctx(ctx_compute, gf, ctx->n_threads); + // Use persistent threadpool + work buffer for graph compute + struct ggml_cplan plan = ggml_graph_plan(gf, ctx->n_threads, cache->threadpool); + + if (plan.work_size > 0) { + cache->ensure_work_buf(plan.work_size); + plan.work_data = cache->work_buf; + } + + enum ggml_status status = ggml_graph_compute(gf, &plan); if (status != GGML_STATUS_SUCCESS) { DIFFUSE_LOG("ar_forward: graph compute failed with status %d", (int)status); ggml_free(ctx_compute); diff --git a/src/ar-sampler.cpp b/src/ar-sampler.cpp index d4ebe60..dbdf289 100644 --- a/src/ar-sampler.cpp +++ b/src/ar-sampler.cpp @@ -1,5 +1,6 @@ #include "ar-sampler.h" #include "ar-graph.h" +#include "ggml-cpu.h" #include #include @@ -159,6 +160,18 @@ std::vector ar_generate( cache.init(total_ctx, (int)hp.n_layer, (int)hp.n_embd_head(), (int)hp.n_head_kv); + // Create persistent threadpool (reused across all decode steps) + { + struct ggml_threadpool_params tparams = ggml_threadpool_params_default(ctx->n_threads); + tparams.strict_cpu = true; // bind threads to cores + tparams.prio = GGML_SCHED_PRIO_HIGH; + cache.threadpool = ggml_threadpool_new(&tparams); + if (cache.threadpool) { + DIFFUSE_LOG(" threadpool: %d threads (persistent, high priority)", + ggml_threadpool_get_n_threads(cache.threadpool)); + } + } + // RNG std::mt19937 rng(params.seed); @@ -281,6 +294,11 @@ done: cache.n_past, cache.n_ctx_max, (float)cache.total_bytes() / (1024 * 1024)); + // Free threadpool before clearing cache + if (cache.threadpool) { + ggml_threadpool_free(cache.threadpool); + cache.threadpool = nullptr; + } cache.clear(); return generated; }