From 621e635224980b71be181b9a807befc09abdd3b4 Mon Sep 17 00:00:00 2001 From: iafiscal1212 Date: Thu, 19 Mar 2026 05:07:52 +0100 Subject: [PATCH] Fix buffer calculation: account for FFN intermediates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per-layer buffer now includes: 10×embd + 3×ff + 2×attn_score + norm casts Plus 20% safety margin. Fixes OOM for 48+ token sequences. Co-Authored-By: Claude Opus 4.6 --- src/diffuse-graph.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/diffuse-graph.cpp b/src/diffuse-graph.cpp index a43bc2c..7ac2ffb 100644 --- a/src/diffuse-graph.cpp +++ b/src/diffuse-graph.cpp @@ -145,13 +145,16 @@ bool diffuse_forward(diffuse_context * ctx, float * logits_out) { const auto & hp = ctx->model->hparams; - // Compute buffer: logits + per-layer intermediates + attention matrices + overhead - // Each layer needs: Q,K,V,attn_out (4×n_embd×N) + attn scores (N×N×n_head) - // Plus norm casts, SwiGLU intermediates, graph nodes overhead - size_t buf_size = 512ull * 1024 * 1024; // base overhead for graph nodes + plan - buf_size += (size_t)n_tokens * hp.n_vocab * sizeof(float) * 2; // logits + output matmul - buf_size += (size_t)n_tokens * hp.n_embd * 16 * sizeof(float); // per-layer intermediates - buf_size += (size_t)n_tokens * (size_t)n_tokens * hp.n_head * sizeof(float) * 2; // attn scores + softmax + // Compute buffer: all intermediate tensors for forward pass + // Per layer: ~10 embd-sized + 3 ff-sized + 2 attn-score matrices + size_t per_layer = (size_t)n_tokens * hp.n_embd * sizeof(float) * 10 + + (size_t)n_tokens * hp.n_ff * sizeof(float) * 3 + + (size_t)n_tokens * n_tokens * hp.n_head * sizeof(float) * 2 + + hp.n_embd * sizeof(float) * 2; // norm weight casts + size_t buf_size = per_layer * hp.n_layer; + buf_size += (size_t)n_tokens * hp.n_vocab * sizeof(float) * 2; // logits + buf_size += 256ull * 1024 * 1024; // graph nodes + execution plan overhead + buf_size = (size_t)(buf_size * 1.2); // 20% safety margin struct ggml_init_params cparams = { /*.mem_size = */ buf_size,