fix(dream): add QKV bias support for Qwen2.5 backbone

Dream/Qwen2.5 uses biases on Q, K, V projections. Without them,
attention is fundamentally incorrect producing garbage output.

Changes:
- diffuse_layer: add optional bq, bk, bv bias tensor pointers
- diffuse-model: load bias tensors from GGUF (nullptr if absent)
- diffuse-graph: apply ggml_add after QKV projection in all 3 graph builders
- convert-dream.py: include QKV bias tensors instead of skipping them

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
iafiscal1212andClaude Opus 4.6 committed 2026-03-22 13:28:50 +01:00
1 parent 3f6aa5597a
commit 531ef58222
4 files changed
+32 -1

No files matched your search

+5
View File
@@ -36,6 +36,11 @@ struct diffuse_layer {
struct ggml_tensor * wv; // V projection
struct ggml_tensor * wo; // output projection
// QKV biases (optional, nullptr for models without them e.g. LLaDA)
struct ggml_tensor * bq = nullptr; // Q bias (Dream/Qwen2.5)
struct ggml_tensor * bk = nullptr; // K bias
struct ggml_tensor * bv = nullptr; // V bias
// FFN (SwiGLU)
struct ggml_tensor * ffn_norm; // RMSNorm weight
struct ggml_tensor * ffn_gate; // gate projection (w1)
+15
View File
@@ -55,6 +55,11 @@ struct ggml_cgraph * diffuse_build_graph(
struct ggml_tensor * K = ggml_mul_mat(ctx, layer.wk, cur);
struct ggml_tensor * V = ggml_mul_mat(ctx, layer.wv, cur);
// Add QKV biases if present (Dream/Qwen2.5)
if (layer.bq) Q = ggml_add(ctx, Q, layer.bq);
if (layer.bk) K = ggml_add(ctx, K, layer.bk);
if (layer.bv) V = ggml_add(ctx, V, layer.bv);
// Reshape for multi-head: [n_embd_head, n_head, N]
Q = ggml_reshape_3d(ctx, Q, n_embd_head, n_head, N);
K = ggml_reshape_3d(ctx, K, n_embd_head, n_head_kv, N);
@@ -193,6 +198,11 @@ static struct ggml_cgraph * diffuse_build_graph_extractable(
struct ggml_tensor * K = ggml_mul_mat(ctx, layer.wk, cur);
struct ggml_tensor * V = ggml_mul_mat(ctx, layer.wv, cur);
// Add QKV biases if present (Dream/Qwen2.5)
if (layer.bq) Q = ggml_add(ctx, Q, layer.bq);
if (layer.bk) K = ggml_add(ctx, K, layer.bk);
if (layer.bv) V = ggml_add(ctx, V, layer.bv);
Q = ggml_reshape_3d(ctx, Q, n_embd_head, n_head, N);
K = ggml_reshape_3d(ctx, K, n_embd_head, n_head_kv, N);
V = ggml_reshape_3d(ctx, V, n_embd_head, n_head_kv, N);
@@ -420,6 +430,11 @@ struct ggml_cgraph * diffuse_build_graph_cached(
struct ggml_tensor * K_active = ggml_mul_mat(ctx, layer.wk, cur);
struct ggml_tensor * V_active = ggml_mul_mat(ctx, layer.wv, cur);
// Add QKV biases if present (Dream/Qwen2.5)
if (layer.bq) Q_active = ggml_add(ctx, Q_active, layer.bq);
if (layer.bk) K_active = ggml_add(ctx, K_active, layer.bk);
if (layer.bv) V_active = ggml_add(ctx, V_active, layer.bv);
// Reshape: [n_embd, n_active] → [n_embd_head, n_head(_kv), n_active]
Q_active = ggml_reshape_3d(ctx, Q_active, n_embd_head, n_head, n_active);
K_active = ggml_reshape_3d(ctx, K_active, n_embd_head, n_head_kv, n_active);
+5
View File
@@ -102,6 +102,11 @@ diffuse_model * diffuse_model_load_impl(const std::string & path, int n_threads)
l.ffn_gate = get_tensor(meta_ctx, fmt_layer("blk.%d.ffn_gate.weight", i).c_str());
l.ffn_up = get_tensor(meta_ctx, fmt_layer("blk.%d.ffn_up.weight", i).c_str());
l.ffn_down = get_tensor(meta_ctx, fmt_layer("blk.%d.ffn_down.weight", i).c_str());
// Optional QKV biases (Dream/Qwen2.5 has them, LLaDA does not)
l.bq = ggml_get_tensor(meta_ctx, fmt_layer("blk.%d.attn_q.bias", i).c_str());
l.bk = ggml_get_tensor(meta_ctx, fmt_layer("blk.%d.attn_k.bias", i).c_str());
l.bv = ggml_get_tensor(meta_ctx, fmt_layer("blk.%d.attn_v.bias", i).c_str());
}
DIFFUSE_LOG("model loaded: %lld tensors",
+7 -1
View File
@@ -51,6 +51,9 @@ LAYER_TENSOR_MAP = {
"self_attn.q_proj.weight": "attn_q.weight",
"self_attn.k_proj.weight": "attn_k.weight",
"self_attn.v_proj.weight": "attn_v.weight",
"self_attn.q_proj.bias": "attn_q.bias",
"self_attn.k_proj.bias": "attn_k.bias",
"self_attn.v_proj.bias": "attn_v.bias",
"self_attn.o_proj.weight": "attn_output.weight",
"mlp.gate_proj.weight": "ffn_gate.weight", # SwiGLU gate (w1)
"mlp.up_proj.weight": "ffn_up.weight", # SwiGLU up (w3)
@@ -66,8 +69,11 @@ EXPECTED_SHAPES_7B = {
"output.weight": (152064, 3584),
"blk.*.attn_norm.weight": (3584,),
"blk.*.attn_q.weight": (3584, 3584), # 28 heads * 128
"blk.*.attn_q.bias": (3584,),
"blk.*.attn_k.weight": (512, 3584), # 4 KV heads * 128
"blk.*.attn_k.bias": (512,),
"blk.*.attn_v.weight": (512, 3584), # 4 KV heads * 128
"blk.*.attn_v.bias": (512,),
"blk.*.attn_output.weight": (3584, 3584),
"blk.*.ffn_norm.weight": (3584,),
"blk.*.ffn_gate.weight": (18944, 3584),
@@ -249,7 +255,7 @@ def convert(args):
print(f"Tensors skipped: {n_skipped}")
print(f"Total parameters: {total_params:,} ({total_params/1e9:.2f}B)")
expected_tensors = 3 + n_layers * 9 # 3 global + 9 per layer
expected_tensors = 3 + n_layers * 12 # 3 global + 12 per layer (9 weights + 3 QKV biases)
if not has_lm_head:
expected_tensors -= 1 # No separate lm_head
if n_converted != expected_tensors: