From 4b06987d439181e26e906529a100e615f1542211 Mon Sep 17 00:00:00 2001 From: Marco Righini Date: Mon, 6 Jul 2026 16:45:25 +0000 Subject: [PATCH] Fix CPU backend abort on DeepSeek V4 PRO: use DS4_N_OUT_GROUP for attention-output grouping The CPU reference path hardcoded the attention-output group count to 8 (the Flash value) in layer_grouped_out_one(), layer_grouped_out_one_decode_scratch(), and layer_grouped_out_batch(). PRO uses n_out_group = 16, so the grouped Q8_0 matmul computed group_dim = n_head_dim * (n_head / 8) = 8192 while the actual attn_output_a tensor has dim[0] = 4096, tripping ds4_die("grouped Q8_0 tensor has an unexpected layout") at prefill layer 1. Replace the literal 8 with the existing DS4_N_OUT_GROUP macro (g_ds4_shape.n_out_group): 8 for Flash, 16 for PRO. Flash is unaffected (macro == 8); PRO Flash now runs on the CPU backend and produces coherent output. Tested (CPU backend, x86-64 Linux, Xeon w9-3595X): - Flash Q2 (DeepSeek-V4-Flash-IQ2XXS...imatrix): unchanged, coherent, ~3 t/s - PRO Q2 (DeepSeek-V4-Pro-IQ2XXS...Instruct-imatrix): previously aborted at prefill layer 1; now coherent, ~0.95 t/s Co-Authored-By: Claude Opus 4.8 (1M context) --- ds4.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ds4.c b/ds4.c index 640511eb0..0b9277546 100644 --- a/ds4.c +++ b/ds4.c @@ -7098,7 +7098,7 @@ static void layer_grouped_out_one( const ds4_model * model, const ds4_layer_weights * layer, const float * heads) { - const uint32_t n_groups = 8; + const uint32_t n_groups = DS4_N_OUT_GROUP; const uint32_t group_heads = DS4_N_HEAD / n_groups; const uint32_t group_dim = DS4_N_HEAD_DIM * group_heads; const uint32_t rank = 1024; @@ -7117,7 +7117,7 @@ static void layer_grouped_out_one_decode_scratch( const ds4_layer_weights * layer, const float * heads, ds4_cpu_decode_scratch * scratch) { - const uint32_t n_groups = 8; + const uint32_t n_groups = DS4_N_OUT_GROUP; const uint32_t group_heads = DS4_N_HEAD / n_groups; const uint32_t group_dim = DS4_N_HEAD_DIM * group_heads; const uint32_t rank = 1024; @@ -7134,7 +7134,7 @@ static void layer_grouped_out_batch( const ds4_layer_weights * layer, const float * heads, uint32_t n_tok) { - const uint32_t n_groups = 8; + const uint32_t n_groups = DS4_N_OUT_GROUP; const uint32_t group_heads = DS4_N_HEAD / n_groups; const uint32_t group_dim = DS4_N_HEAD_DIM * group_heads; const uint32_t rank = 1024;