diff --git a/src/diffuse-common.h b/src/diffuse-common.h index ba9650d..fb978a1 100644 --- a/src/diffuse-common.h +++ b/src/diffuse-common.h @@ -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) diff --git a/src/diffuse-graph.cpp b/src/diffuse-graph.cpp index e0d3bd4..234a805 100644 --- a/src/diffuse-graph.cpp +++ b/src/diffuse-graph.cpp @@ -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); diff --git a/src/diffuse-model.cpp b/src/diffuse-model.cpp index 4c3339b..e2a88d9 100644 --- a/src/diffuse-model.cpp +++ b/src/diffuse-model.cpp @@ -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", diff --git a/tools/convert-dream.py b/tools/convert-dream.py index c0ef1a8..dd9d962 100644 --- a/tools/convert-dream.py +++ b/tools/convert-dream.py @@ -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: