models : replace ggml_tensor** out-params with output structs (#31142)
The hyper-connection and DSA graph builders wrote their extra outputs (post / comb gates, carried top-k selections, inject gates) through ggml_tensor** out-params. Return them in a small struct next to the primary output instead. Assisted-by: dsh:llama.cpp/ggml-org/Qwen3.8-Flash-Next-GGUF (MacBook, M5 Max, 128GB)
This commit is contained in:
1 parent
4d71b42fd8
commit
4b851fbee8
7 files changed
+216
-182
No files matched your search
+38
-40
@@ -342,24 +342,23 @@ ggml_tensor * llama_model_deepseek4::graph::build_hc_sinkhorn(
|
||||
return comb;
|
||||
}
|
||||
|
||||
ggml_tensor * llama_model_deepseek4::graph::build_hc_pre(
|
||||
llama_model_deepseek4::graph::hc_pre
|
||||
llama_model_deepseek4::graph::build_hc_pre(
|
||||
ggml_tensor * x,
|
||||
ggml_tensor * hc_fn,
|
||||
ggml_tensor * hc_scale,
|
||||
ggml_tensor * hc_base,
|
||||
ggml_tensor ** post,
|
||||
ggml_tensor ** comb,
|
||||
int il) const {
|
||||
return build_hc_pre(x, build_hc_mixes(x, hc_fn, hc_scale, hc_base, post, comb, il), il);
|
||||
hc_mixes mixes = build_hc_mixes(x, hc_fn, hc_scale, hc_base, il);
|
||||
return { build_hc_pre(x, mixes.pre, il), mixes.post, mixes.comb };
|
||||
}
|
||||
|
||||
ggml_tensor * llama_model_deepseek4::graph::build_hc_mixes(
|
||||
llama_model_deepseek4::graph::hc_mixes
|
||||
llama_model_deepseek4::graph::build_hc_mixes(
|
||||
ggml_tensor * x,
|
||||
ggml_tensor * hc_fn,
|
||||
ggml_tensor * hc_scale,
|
||||
ggml_tensor * hc_base,
|
||||
ggml_tensor ** post,
|
||||
ggml_tensor ** comb,
|
||||
int il) const {
|
||||
const int64_t hc = hparams.dsv4_hc_mult;
|
||||
const int64_t hc_dim = hc*n_embd;
|
||||
@@ -386,28 +385,29 @@ ggml_tensor * llama_model_deepseek4::graph::build_hc_mixes(
|
||||
pre = ggml_scale_bias(ctx0, pre, 1.0f, hparams.dsv4_hc_eps);
|
||||
cb(pre, "hc_pre", il);
|
||||
|
||||
*post = dsv4_view_2d(ctx0, mixes, hc, nt, hc);
|
||||
*post = dsv4_hc_affine(ctx0, *post, scale_post, base_post);
|
||||
*post = ggml_sigmoid(ctx0, *post);
|
||||
*post = ggml_scale(ctx0, *post, 2.0f);
|
||||
cb(*post, "hc_post", il);
|
||||
ggml_tensor * post = dsv4_view_2d(ctx0, mixes, hc, nt, hc);
|
||||
post = dsv4_hc_affine(ctx0, post, scale_post, base_post);
|
||||
post = ggml_sigmoid(ctx0, post);
|
||||
post = ggml_scale(ctx0, post, 2.0f);
|
||||
cb(post, "hc_post", il);
|
||||
|
||||
ggml_tensor * comb = nullptr;
|
||||
if (cparams.fused_dsv4_hc_comb) {
|
||||
*comb = ggml_dsv4_hc_comb(ctx0, mixes, hc_scale, hc_base, hparams.dsv4_hc_eps,
|
||||
comb = ggml_dsv4_hc_comb(ctx0, mixes, hc_scale, hc_base, hparams.dsv4_hc_eps,
|
||||
(int32_t) hparams.dsv4_hc_sinkhorn_iters);
|
||||
res->add_fused_node({LLM_FUSED_OP_DSV4_HC_COMB, *comb, il});
|
||||
res->add_fused_node({LLM_FUSED_OP_DSV4_HC_COMB, comb, il});
|
||||
} else {
|
||||
ggml_tensor * scale_comb = dsv4_view_1d(ctx0, hc_scale, 1, 2);
|
||||
ggml_tensor * base_comb = dsv4_view_1d(ctx0, hc_base, hc*hc, 2*hc);
|
||||
|
||||
*comb = dsv4_view_2d(ctx0, mixes, hc*hc, nt, 2*hc);
|
||||
*comb = dsv4_hc_affine(ctx0, *comb, scale_comb, base_comb);
|
||||
*comb = ggml_reshape_3d(ctx0, *comb, hc, hc, nt);
|
||||
*comb = build_hc_sinkhorn(*comb, il);
|
||||
comb = dsv4_view_2d(ctx0, mixes, hc*hc, nt, 2*hc);
|
||||
comb = dsv4_hc_affine(ctx0, comb, scale_comb, base_comb);
|
||||
comb = ggml_reshape_3d(ctx0, comb, hc, hc, nt);
|
||||
comb = build_hc_sinkhorn(comb, il);
|
||||
}
|
||||
cb(*comb, "hc_comb", il);
|
||||
cb(comb, "hc_comb", il);
|
||||
|
||||
return pre;
|
||||
return { pre, post, comb };
|
||||
}
|
||||
|
||||
ggml_tensor * llama_model_deepseek4::graph::build_hc_post(
|
||||
@@ -1252,14 +1252,12 @@ llama_model_deepseek4::graph::graph(const llama_model & model, const llm_graph_p
|
||||
}
|
||||
|
||||
ggml_tensor * residual = inpL;
|
||||
ggml_tensor * post = nullptr;
|
||||
ggml_tensor * comb = nullptr;
|
||||
|
||||
cur = build_hc_pre(inpL,
|
||||
const hc_pre attn_hc = build_hc_pre(inpL,
|
||||
model.layers[il].hc_attn_fn,
|
||||
model.layers[il].hc_attn_scale,
|
||||
model.layers[il].hc_attn_base,
|
||||
&post, &comb, il);
|
||||
il);
|
||||
cur = attn_hc.cur;
|
||||
cb(cur, "hc_attn_pre", il);
|
||||
|
||||
cur = build_norm(cur, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);
|
||||
@@ -1267,20 +1265,21 @@ llama_model_deepseek4::graph::graph(const llama_model & model, const llm_graph_p
|
||||
|
||||
cur = build_attention(model, inp_dsv4, cur, inp_pos, il);
|
||||
|
||||
inpL = build_hc_post(cur, residual, post, comb, il);
|
||||
inpL = build_hc_post(cur, residual, attn_hc.post, attn_hc.comb, il);
|
||||
cb(inpL, "hc_attn_post", il);
|
||||
|
||||
residual = inpL;
|
||||
cur = build_hc_pre(inpL,
|
||||
const hc_pre ffn_hc = build_hc_pre(inpL,
|
||||
model.layers[il].hc_ffn_fn,
|
||||
model.layers[il].hc_ffn_scale,
|
||||
model.layers[il].hc_ffn_base,
|
||||
&post, &comb, il);
|
||||
il);
|
||||
cur = ffn_hc.cur;
|
||||
cb(cur, "hc_ffn_pre", il);
|
||||
|
||||
ggml_build_forward_expand(gf, residual);
|
||||
ggml_build_forward_expand(gf, post);
|
||||
ggml_build_forward_expand(gf, comb);
|
||||
ggml_build_forward_expand(gf, ffn_hc.post);
|
||||
ggml_build_forward_expand(gf, ffn_hc.comb);
|
||||
|
||||
cur = build_norm(cur, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il);
|
||||
cb(cur, "ffn_norm", il);
|
||||
@@ -1330,7 +1329,7 @@ llama_model_deepseek4::graph::graph(const llama_model & model, const llm_graph_p
|
||||
cur = ggml_add(ctx0, moe_out, ffn_shexp);
|
||||
cb(cur, "ffn_out", il);
|
||||
|
||||
inpL = build_hc_post(cur, residual, post, comb, il);
|
||||
inpL = build_hc_post(cur, residual, ffn_hc.post, ffn_hc.comb, il);
|
||||
inpL = build_cvec(inpL, il);
|
||||
cb(inpL, "l_last", il);
|
||||
}
|
||||
@@ -1430,14 +1429,12 @@ llama_model_deepseek4::graph_mtp::graph_mtp(const llama_model & model, const llm
|
||||
cb(inpL, "mtp_eh_proj", il);
|
||||
|
||||
ggml_tensor * residual = inpL;
|
||||
ggml_tensor * post = nullptr;
|
||||
ggml_tensor * comb = nullptr;
|
||||
|
||||
ggml_tensor * cur = build_hc_pre(inpL,
|
||||
hc_pre attn_hc = build_hc_pre(inpL,
|
||||
layer.hc_attn_fn,
|
||||
layer.hc_attn_scale,
|
||||
layer.hc_attn_base,
|
||||
&post, &comb, il);
|
||||
il);
|
||||
ggml_tensor * cur = attn_hc.cur;
|
||||
cb(cur, "mtp_hc_attn_pre", il);
|
||||
|
||||
cur = build_norm(cur, layer.attn_norm, nullptr, LLM_NORM_RMS, il);
|
||||
@@ -1445,15 +1442,16 @@ llama_model_deepseek4::graph_mtp::graph_mtp(const llama_model & model, const llm
|
||||
|
||||
cur = build_attention(model, inp_attn, cur, inp_pos, il);
|
||||
|
||||
inpL = build_hc_post(cur, residual, post, comb, il);
|
||||
inpL = build_hc_post(cur, residual, attn_hc.post, attn_hc.comb, il);
|
||||
cb(inpL, "mtp_hc_attn_post", il);
|
||||
|
||||
residual = inpL;
|
||||
cur = build_hc_pre(inpL,
|
||||
hc_pre ffn_hc = build_hc_pre(inpL,
|
||||
layer.hc_ffn_fn,
|
||||
layer.hc_ffn_scale,
|
||||
layer.hc_ffn_base,
|
||||
&post, &comb, il);
|
||||
il);
|
||||
cur = ffn_hc.cur;
|
||||
cb(cur, "mtp_hc_ffn_pre", il);
|
||||
|
||||
cur = build_norm(cur, layer.ffn_norm, nullptr, LLM_NORM_RMS, il);
|
||||
@@ -1483,7 +1481,7 @@ llama_model_deepseek4::graph_mtp::graph_mtp(const llama_model & model, const llm
|
||||
cur = ggml_add(ctx0, moe_out, ffn_shexp);
|
||||
cb(cur, "mtp_ffn_out", il);
|
||||
|
||||
inpL = build_hc_post(cur, residual, post, comb, il);
|
||||
inpL = build_hc_post(cur, residual, ffn_hc.post, ffn_hc.comb, il);
|
||||
inpL = build_cvec(inpL, il);
|
||||
cb(inpL, "mtp_l_out", il);
|
||||
|
||||
|
||||
Reference in new issue
Block a user