mirror of
https://github.com/iafiscal1212/diffuse-cpp.git
synced 2026-12-05 09:08:08 +00:00
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:
1 parent
3f6aa5597a
commit
531ef58222
4 files changed
+32
-1
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user