From fc3dc6cb33554c1d9946e62062753abae1829c56 Mon Sep 17 00:00:00 2001 From: realorko Date: Wed, 8 Apr 2026 00:01:15 +0100 Subject: [PATCH 1/2] fix: free ctx_copy in ggml_opt_free to plug per-training-session leak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ggml_opt_alloc populates opt_ctx->ctx_copy via a free+init pair every time the allocated graph shape changes. The last ctx_copy from the final ggml_opt_alloc call survives until ggml_opt_free is invoked, but ggml_opt_free was only freeing ctx_static and ctx_cpu, never ctx_copy. Each opt_ctx lifetime therefore leaks the final per-batch context — ~900 KB for a typical GNN training session in sindarin-pkg-tensor, surfaced via AddressSanitizer. ctx_copy is nullptr-initialized and ggml_free() handles NULL safely, so the new release is guard-free. --- ggml/src/ggml-opt.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ggml/src/ggml-opt.cpp b/ggml/src/ggml-opt.cpp index e078ad14a39c..a45f321c6a40 100644 --- a/ggml/src/ggml-opt.cpp +++ b/ggml/src/ggml-opt.cpp @@ -589,6 +589,13 @@ void ggml_opt_free(ggml_opt_context_t opt_ctx) { ggml_backend_buffer_free(opt_ctx->buf_cpu); ggml_free(opt_ctx->ctx_static); ggml_free(opt_ctx->ctx_cpu); + // ctx_copy is re-allocated by ggml_opt_alloc whenever the graph + // shape changes (see the ggml_free+ggml_init pair near the top of + // that function). The last allocation survives into ggml_opt_free, + // so we must release it here — otherwise the final per-batch + // context (~900 KB for typical GNN training) leaks per opt_ctx. + // Safe when ctx_copy == nullptr; ggml_free guards against NULL. + ggml_free(opt_ctx->ctx_copy); delete opt_ctx; } From 6940e4c56d0a563627d642f95e0e603fc7498db6 Mon Sep 17 00:00:00 2001 From: RealOrko <45273739+RealOrko@users.noreply.github.com> Date: Wed, 8 Apr 2026 10:03:28 +0100 Subject: [PATCH 2/2] Update ggml/src/ggml-opt.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Johannes Gäßler --- ggml/src/ggml-opt.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ggml/src/ggml-opt.cpp b/ggml/src/ggml-opt.cpp index a45f321c6a40..53903defa8f4 100644 --- a/ggml/src/ggml-opt.cpp +++ b/ggml/src/ggml-opt.cpp @@ -589,12 +589,6 @@ void ggml_opt_free(ggml_opt_context_t opt_ctx) { ggml_backend_buffer_free(opt_ctx->buf_cpu); ggml_free(opt_ctx->ctx_static); ggml_free(opt_ctx->ctx_cpu); - // ctx_copy is re-allocated by ggml_opt_alloc whenever the graph - // shape changes (see the ggml_free+ggml_init pair near the top of - // that function). The last allocation survives into ggml_opt_free, - // so we must release it here — otherwise the final per-batch - // context (~900 KB for typical GNN training) leaks per opt_ctx. - // Safe when ctx_copy == nullptr; ggml_free guards against NULL. ggml_free(opt_ctx->ctx_copy); delete opt_ctx; }