mirror of
https://github.com/iafiscal1212/diffuse-cpp.git
synced 2026-12-04 00:28:08 +00:00
fix(gqa): use grouped repeat instead of interleaved for GQA heads
ggml_repeat with K[d,4,N]→[d,28,N] gives round-robin head assignment (kv0,kv1,kv2,kv3,kv0,...) but GQA expects grouped assignment (kv0×7, kv1×7, kv2×7, kv3×7). Fix: reshape to 4D, repeat the inner dimension, reshape back. K[d,kv,N] → K[d,1,kv,N] → repeat → K[d,rep,kv,N] → K[d,head,N] This was the root cause of garbage output on Dream-7B (GQA 28/4). LLaDA (MHA 32/32) is unaffected since the GQA path is never taken. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
7eb999b09a
commit
d261e604af
1 file changed
+20
-6
+20
-6
@@ -78,12 +78,20 @@ struct ggml_cgraph * diffuse_build_graph(
|
||||
K = ggml_rope_ext(ctx, K, inp_pos, nullptr, n_embd_head,
|
||||
GGML_ROPE_TYPE_NEOX, 0, hp.rope_theta, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);
|
||||
|
||||
// GQA: repeat K, V heads if n_head_kv < n_head
|
||||
// GQA: repeat each KV head n_rep times (grouped, not interleaved)
|
||||
// Reshape 3D→4D so ggml_repeat expands the right axis, then flatten back.
|
||||
// K[d, n_kv, N] → K[d, 1, n_kv, N] → repeat → K[d, n_rep, n_kv, N] → K[d, n_head, N]
|
||||
if (n_head_kv < n_head) {
|
||||
const int n_rep = n_head / n_head_kv;
|
||||
K = ggml_reshape_4d(ctx, K, n_embd_head, 1, n_head_kv, N);
|
||||
K = ggml_repeat(ctx, K,
|
||||
ggml_new_tensor_3d(ctx, K->type, n_embd_head, n_head, N));
|
||||
ggml_new_tensor_4d(ctx, K->type, n_embd_head, n_rep, n_head_kv, N));
|
||||
K = ggml_reshape_3d(ctx, K, n_embd_head, n_head, N);
|
||||
|
||||
V = ggml_reshape_4d(ctx, V, n_embd_head, 1, n_head_kv, N);
|
||||
V = ggml_repeat(ctx, V,
|
||||
ggml_new_tensor_3d(ctx, V->type, n_embd_head, n_head, N));
|
||||
ggml_new_tensor_4d(ctx, V->type, n_embd_head, n_rep, n_head_kv, N));
|
||||
V = ggml_reshape_3d(ctx, V, n_embd_head, n_head, N);
|
||||
}
|
||||
|
||||
// Permute Q, K: [n_embd_head, n_head, N] → [n_embd_head, N, n_head]
|
||||
@@ -453,12 +461,18 @@ struct ggml_cgraph * diffuse_build_graph_cached(
|
||||
K_active = ggml_rope_ext(ctx, K_active, inp_pos, nullptr, n_embd_head,
|
||||
GGML_ROPE_TYPE_NEOX, 0, hp.rope_theta, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);
|
||||
|
||||
// GQA expansion
|
||||
// GQA: repeat each KV head n_rep times (grouped, not interleaved)
|
||||
if (n_head_kv < n_head) {
|
||||
const int n_rep = n_head / n_head_kv;
|
||||
K_active = ggml_reshape_4d(ctx, K_active, n_embd_head, 1, n_head_kv, n_active);
|
||||
K_active = ggml_repeat(ctx, K_active,
|
||||
ggml_new_tensor_3d(ctx, K_active->type, n_embd_head, n_head, n_active));
|
||||
ggml_new_tensor_4d(ctx, K_active->type, n_embd_head, n_rep, n_head_kv, n_active));
|
||||
K_active = ggml_reshape_3d(ctx, K_active, n_embd_head, n_head, n_active);
|
||||
|
||||
V_active = ggml_reshape_4d(ctx, V_active, n_embd_head, 1, n_head_kv, n_active);
|
||||
V_active = ggml_repeat(ctx, V_active,
|
||||
ggml_new_tensor_3d(ctx, V_active->type, n_embd_head, n_head, n_active));
|
||||
ggml_new_tensor_4d(ctx, V_active->type, n_embd_head, n_rep, n_head_kv, n_active));
|
||||
V_active = ggml_reshape_3d(ctx, V_active, n_embd_head, n_head, n_active);
|
||||
}
|
||||
|
||||
// ── Name K_active, V_active for extraction ──────────────
|
||||
|
||||
Reference in new issue
Block a user