Fix buffer calculation: account for FFN intermediates

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 <noreply@anthropic.com>
This commit is contained in:
iafiscal1212andClaude Opus 4.6 committed 2026-03-19 05:07:52 +01:00
1 parent 2bb4358450
commit 621e635224
1 file changed
+10 -7
+10 -7
View File
@@ -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,