Commit a8fd0643e for llama.cpp
commit a8fd0643e0cb8055f97a0136e67e259b024606d8
Author: 风起 <62558119+3219378872@users.noreply.github.com>
Date: Mon Dec 7 03:32:15 2026 +0800
cuda : fuse 3-op SIGMOID -> SCALE -> DSV4_HC_POST for qwen4exp (#33707)
Assisted-by: Antigravity
diff --git a/ggml/src/ggml-cuda/dsv4-hc.cu b/ggml/src/ggml-cuda/dsv4-hc.cu
index 2d0b3cfea..217c6a78f 100644
--- a/ggml/src/ggml-cuda/dsv4-hc.cu
+++ b/ggml/src/ggml-cuda/dsv4-hc.cu
@@ -286,11 +286,13 @@ void ggml_cuda_op_dsv4_hc_post(ggml_backend_cuda_context & ctx, ggml_tensor * ds
const ggml_tensor * residual = dst->src[1];
const ggml_tensor * comb = dst->src[3];
- // when fused, read the raw post weights from before the SCALE -> SIGMOID -> SCALE chain
- const bool gated = gate_scale_in != nullptr;
- const ggml_tensor * post = gated ? gate_scale_in->src[0] : dst->src[2];
+ // when fused, read the raw post weights from before the [SCALE ->] SIGMOID -> SCALE chain
+ const bool gated = gate_scale_out != nullptr;
+ const ggml_tensor * post = gated
+ ? (gate_scale_in != nullptr ? gate_scale_in->src[0] : gate_scale_out->src[0]->src[0])
+ : dst->src[2];
- const float gate_in = gated ? ggml_get_op_params_f32(gate_scale_in, 0) : 1.0f;
+ const float gate_in = (gated && gate_scale_in != nullptr) ? ggml_get_op_params_f32(gate_scale_in, 0) : 1.0f;
const float gate_out = gated ? ggml_get_op_params_f32(gate_scale_out, 0) : 1.0f;
GGML_ASSERT(x->type == GGML_TYPE_F32);
diff --git a/ggml/src/ggml-cuda/dsv4-hc.cuh b/ggml/src/ggml-cuda/dsv4-hc.cuh
index 1885e7665..798f8bbe3 100644
--- a/ggml/src/ggml-cuda/dsv4-hc.cuh
+++ b/ggml/src/ggml-cuda/dsv4-hc.cuh
@@ -3,6 +3,6 @@
void ggml_cuda_op_dsv4_hc_comb(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
void ggml_cuda_op_dsv4_hc_pre(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
-// gate_scale_in/gate_scale_out: the SCALE nodes of a fused SCALE -> SIGMOID -> SCALE chain feeding src[2]
+// gate_scale_in/gate_scale_out: the SCALE nodes of a fused [SCALE ->] SIGMOID -> SCALE chain feeding src[2]
void ggml_cuda_op_dsv4_hc_post(ggml_backend_cuda_context & ctx, ggml_tensor * dst,
const ggml_tensor * gate_scale_in = nullptr, const ggml_tensor * gate_scale_out = nullptr);
diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu
index 0a31c2187..920c00c36 100644
--- a/ggml/src/ggml-cuda/fusion.cu
+++ b/ggml/src/ggml-cuda/fusion.cu
@@ -1133,6 +1133,24 @@ static bool ggml_cuda_can_fuse(const struct ggml_cgraph * cgraph,
return false;
}
+ // scale_out*sigmoid(post) as the hc_post weights (qwen4exp hc_combine when inject was already scaled in hc_mix)
+ std::initializer_list<enum ggml_op> hc_post_gate_ops_3 = { GGML_OP_UNARY, GGML_OP_SCALE, GGML_OP_DSV4_HC_POST };
+
+ if (is_equal(hc_post_gate_ops_3, ops) && ggml_can_fuse_subgraph(cgraph, node_idx, ops, { node_idx + 2 })) {
+ const ggml_tensor * sigmoid = cgraph->nodes[node_idx];
+ const ggml_tensor * scale_out = cgraph->nodes[node_idx + 1];
+
+ if (ggml_check_edges(cgraph, node_idx, {{1, 0, 0}, {2, 2, 1}}) &&
+ ggml_get_unary_op(sigmoid) == GGML_UNARY_OP_SIGMOID &&
+ ggml_get_op_params_f32(scale_out, 1) == 0.0f &&
+ sigmoid->src[0]->type == GGML_TYPE_F32 && scale_out->type == GGML_TYPE_F32 &&
+ ggml_are_same_shape(sigmoid->src[0], scale_out)) {
+ int out_nodes[] = { node_idx + 2 };
+ return ggml_cuda_check_fusion_memory_ranges(cgraph, node_idx, (int)ops.size(), out_nodes, 1);
+ }
+ return false;
+ }
+
if (!ggml_can_fuse(cgraph, node_idx, ops)) {
return false;
}
@@ -2474,6 +2492,11 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap
return 3;
}
+ if (ggml_cuda_can_fuse(cgraph, i, { GGML_OP_UNARY, GGML_OP_SCALE, GGML_OP_DSV4_HC_POST }, { GGML_UNARY_OP_SIGMOID })) {
+ ggml_cuda_op_dsv4_hc_post(*cuda_ctx, cgraph->nodes[i + 2], nullptr, cgraph->nodes[i + 1]);
+ return 2;
+ }
+
if (ggml_cuda_can_fuse(cgraph, i, { GGML_OP_SCALE, GGML_OP_UNARY, GGML_OP_SCALE }, { GGML_UNARY_OP_TANH })) {
ggml_cuda_op_softcap(*cuda_ctx, cgraph->nodes[i + 2], node);
return 2;