diff --git a/ggml/src/ggml-cuda/dsv4-hc.cu b/ggml/src/ggml-cuda/dsv4-hc.cu index 2d0b3cfeaed7..bdb11260121f 100644 --- a/ggml/src/ggml-cuda/dsv4-hc.cu +++ b/ggml/src/ggml-cuda/dsv4-hc.cu @@ -1,5 +1,6 @@ #include "common.cuh" #include "dsv4-hc.cuh" +#include "unary.cuh" static constexpr int DSV4_HC = 4; @@ -280,19 +281,14 @@ void ggml_cuda_op_dsv4_hc_pre(ggml_backend_cuda_context & ctx, ggml_tensor * dst scale); } -void ggml_cuda_op_dsv4_hc_post(ggml_backend_cuda_context & ctx, ggml_tensor * dst, - const ggml_tensor * gate_scale_in, const ggml_tensor * gate_scale_out) { +// post: the tensor the kernel reads the weights from (dst->src[2], or the head of a fused gate chain); +// when gated the kernel computes gate_out*sigmoid(gate_in*post) per element +static void ggml_cuda_dsv4_hc_post_impl(ggml_backend_cuda_context & ctx, ggml_tensor * dst, + const ggml_tensor * post, const bool gated, const float gate_in, const float gate_out) { const ggml_tensor * x = dst->src[0]; 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]; - - const float gate_in = gated ? 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); GGML_ASSERT(residual->type == GGML_TYPE_F32); GGML_ASSERT(post->type == GGML_TYPE_F32); @@ -331,3 +327,67 @@ void ggml_cuda_op_dsv4_hc_post(ggml_backend_cuda_context & ctx, ggml_tensor * ds nbd0 / sizeof(float), nbd1 / sizeof(float), nbd2 / sizeof(float), gate_in, gate_out); } + +void ggml_cuda_op_dsv4_hc_post(ggml_backend_cuda_context & ctx, ggml_tensor * dst, + const ggml_tensor * gate_scale_in, const ggml_tensor * gate_scale_out) { + // 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]; + + const float gate_in = gated ? 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_cuda_dsv4_hc_post_impl(ctx, dst, post, gated, gate_in, gate_out); +} + +void ggml_cuda_op_dsv4_hc_post_sigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst, + const ggml_tensor * sigmoid, const ggml_tensor * gate_scale_out) { + // the SIGMOID reads a (possibly strided) view of a merged matmul output; the kernel takes its strides + ggml_cuda_dsv4_hc_post_impl(ctx, dst, sigmoid->src[0], true, 1.0f, ggml_get_op_params_f32(gate_scale_out, 0)); +} + +// y = scale*x + bias over a contiguous [ncols, nrows] tensor (the SCALE node), and for the first n_lo columns of each row +// silu(y) into the contiguous [n_lo, nrows] silu_dst. One element per thread, the expressions of scale_f32 and op_silu. +static __global__ void scale_silu_lo_f32( + const float * x, float * y_dst, float * silu_dst, + const float scale, const float bias, const int64_t n_lo, const int64_t ncols, const int64_t k) { + ggml_cuda_pdl_lc(); + const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; + + ggml_cuda_pdl_sync(); + if (i >= k) { + return; + } + + const int64_t row = i / ncols; + const int64_t col = i - row*ncols; + + const float y = scale * x[i] + bias; + y_dst[i] = y; + if (col < n_lo) { + silu_dst[row*n_lo + col] = ggml_cuda_op_silu_single(y); + } +} + +void ggml_cuda_op_scale_silu_lo(ggml_backend_cuda_context & ctx, ggml_tensor * scale_node, ggml_tensor * silu_node) { + const ggml_tensor * x = scale_node->src[0]; + + GGML_ASSERT(x->type == GGML_TYPE_F32 && scale_node->type == GGML_TYPE_F32 && silu_node->type == GGML_TYPE_F32); + GGML_ASSERT(ggml_is_contiguous(x) && ggml_is_contiguous(scale_node) && ggml_is_contiguous(silu_node)); + + const float scale = ggml_get_op_params_f32(scale_node, 0); + const float bias = ggml_get_op_params_f32(scale_node, 1); + + const int64_t ncols = x->ne[0]; + const int64_t n_lo = silu_node->ne[0]; + const int64_t k = ggml_nelements(x); + + const int block_size = 256; + const dim3 block_dims(block_size, 1, 1); + const dim3 grid_dims((k + block_size - 1) / block_size, 1, 1); + const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); + + ggml_cuda_kernel_launch(scale_silu_lo_f32, launch_params, + (const float *) x->data, (float *) scale_node->data, (float *) silu_node->data, + scale, bias, n_lo, ncols, k); +} diff --git a/ggml/src/ggml-cuda/dsv4-hc.cuh b/ggml/src/ggml-cuda/dsv4-hc.cuh index 1885e76659d5..882c1652457a 100644 --- a/ggml/src/ggml-cuda/dsv4-hc.cuh +++ b/ggml/src/ggml-cuda/dsv4-hc.cuh @@ -6,3 +6,9 @@ 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] 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); +// the SIGMOID -> SCALE chain (no input scale) feeding src[2]; sigmoid->src[0] may be a non-contiguous 2-D view +void ggml_cuda_op_dsv4_hc_post_sigmoid(ggml_backend_cuda_context & ctx, ggml_tensor * dst, + const ggml_tensor * sigmoid, const ggml_tensor * gate_scale_out); +// SCALE -> VIEW(first n_lo columns) -> SILU in one launch: writes the SCALE result (it also feeds the inject view) +// and the silu of its first n_lo columns +void ggml_cuda_op_scale_silu_lo(ggml_backend_cuda_context & ctx, ggml_tensor * scale_node, ggml_tensor * silu_node); diff --git a/ggml/src/ggml-cuda/fusion.cu b/ggml/src/ggml-cuda/fusion.cu index d5eff36cb844..7455a7be0046 100644 --- a/ggml/src/ggml-cuda/fusion.cu +++ b/ggml/src/ggml-cuda/fusion.cu @@ -2447,6 +2447,89 @@ static int ggml_cuda_try_fuse_rms_norm_set_rows(ggml_backend_cuda_context * cuda } // try and fuse nodes and return the number of nodes to skip +// SCALE(1/hc) -> VIEW(first n_lo columns) -> SILU on the merged [n_lo + hc, T] down_inject matmul output (qwen4exp hc_mix). +// The SCALE result is also read by the inject view, so it stays an output of the fusion: one kernel writes both it and +// silu(lo). Same expressions as the separate SCALE and SILU kernels, so the results are bit-identical. +static int ggml_cuda_try_fuse_hc_scale_silu(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) { + static const bool disabled = getenv("GGML_CUDA_NO_HC_FUSION") != nullptr; + if (disabled || i + 2 >= cgraph->n_nodes) { + return 0; + } + + const std::initializer_list ops = { GGML_OP_SCALE, GGML_OP_VIEW, GGML_OP_UNARY }; + if (!ggml_can_fuse_subgraph(cgraph, i, ops, { i, i + 2 })) { + return 0; + } + + ggml_tensor * scale = cgraph->nodes[i]; + const ggml_tensor * view = cgraph->nodes[i + 1]; + ggml_tensor * silu = cgraph->nodes[i + 2]; + + const ggml_tensor * x = scale->src[0]; + if (ggml_get_unary_op(silu) != GGML_UNARY_OP_SILU || !ggml_check_edges(cgraph, i, {{1, 0, 0}, {2, 0, 1}}) || + x->type != GGML_TYPE_F32 || scale->type != GGML_TYPE_F32 || silu->type != GGML_TYPE_F32 || + !ggml_is_contiguous(x) || !ggml_is_contiguous(scale) || !ggml_is_contiguous(silu) || + x->ne[2] != 1 || x->ne[3] != 1 || !ggml_are_same_shape(x, scale) || + view->view_src != scale || view->view_offs != 0 || view->nb[1] != scale->nb[1] || + view->ne[0] > scale->ne[0] || view->ne[1] != scale->ne[1] || view->ne[2] != 1 || view->ne[3] != 1 || + !ggml_are_same_shape(view, silu)) { + return 0; + } + + // one thread per element: the scale may run in place on x, but the silu result must not touch x + if (ggml_cuda_tensors_overlap(silu, x) || ggml_cuda_tensors_overlap(silu, scale) || + (x->data != scale->data && ggml_cuda_tensors_overlap(x, scale))) { + return 0; + } + + ggml_cuda_op_scale_silu_lo(*cuda_ctx, scale, silu); + return 2; +} + +// SIGMOID -> SCALE -> DSV4_HC_POST (qwen4exp hc_combine with the merged down_inject matmul): the scatter weights +// scale*sigmoid(x) are computed inside the post kernel. x is a view of the merged [n_lo + hc, T] matmul output, +// so it is read with its own strides. The merged layout puts the 1/hc SCALE before the split, so unlike the +// SCALE -> SIGMOID -> SCALE chain in ggml_cuda_can_fuse there is no input scale. Bit-identical to the unfused chain: +// the kernel evaluates scale*(1.0f/(1.0f + expf(-(1.0f*x)))) and the SCALE kernel adds a zero bias to a positive +// number, which changes nothing. GGML_CUDA_NO_HC_FUSION disables this matcher. +static int ggml_cuda_try_fuse_hc_post_sigmoid(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) { + static const bool disabled = getenv("GGML_CUDA_NO_HC_FUSION") != nullptr; + if (disabled || i + 2 >= cgraph->n_nodes) { + return 0; + } + + const std::initializer_list ops = { GGML_OP_UNARY, GGML_OP_SCALE, GGML_OP_DSV4_HC_POST }; + if (!ggml_can_fuse_subgraph(cgraph, i, ops, { i + 2 })) { + return 0; + } + + const ggml_tensor * sigmoid = cgraph->nodes[i]; + const ggml_tensor * scale_out = cgraph->nodes[i + 1]; + ggml_tensor * dst = cgraph->nodes[i + 2]; + + if (ggml_get_unary_op(sigmoid) != GGML_UNARY_OP_SIGMOID || !ggml_check_edges(cgraph, i, {{1, 0, 0}, {2, 2, 1}})) { + return 0; + } + + const ggml_tensor * post = sigmoid->src[0]; + if (ggml_get_op_params_f32(scale_out, 1) != 0.0f || + post->type != GGML_TYPE_F32 || sigmoid->type != GGML_TYPE_F32 || scale_out->type != GGML_TYPE_F32 || + post->nb[0] != sizeof(float) || post->nb[1] % sizeof(float) != 0 || + post->ne[2] != 1 || post->ne[3] != 1 || + !ggml_are_same_shape(post, sigmoid) || !ggml_are_same_shape(post, scale_out) || + dst->src[1]->type != GGML_TYPE_F32 || post->ne[0] != dst->src[1]->ne[1] || post->ne[1] != dst->src[0]->ne[1]) { + return 0; + } + + int out_nodes[] = { i + 2 }; + if (!ggml_cuda_check_fusion_memory_ranges(cgraph, i, 3, out_nodes, 1)) { + return 0; + } + + ggml_cuda_op_dsv4_hc_post_sigmoid(*cuda_ctx, dst, sigmoid, scale_out); + return 2; +} + int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) { if (int n = ggml_cuda_try_fuse_inkling2(cuda_ctx, cgraph, i)) { return n; @@ -2466,6 +2549,18 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap } } + if (node->op == GGML_OP_SCALE) { + if (int n = ggml_cuda_try_fuse_hc_scale_silu(cuda_ctx, cgraph, i)) { + return n; + } + } + + if (node->op == GGML_OP_UNARY) { + if (int n = ggml_cuda_try_fuse_hc_post_sigmoid(cuda_ctx, cgraph, i)) { + return n; + } + } + // a scaled matmul only takes part in the gate/up/GLU fusion, which passes its weight scale on const bool scaled_mm = (node->op == GGML_OP_MUL_MAT || node->op == GGML_OP_MUL_MAT_ID) && ggml_get_src_scale(node, 0); diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp index c3aa490e347b..7176fa5ad2fd 100644 --- a/tests/test-backend-ops.cpp +++ b/tests/test-backend-ops.cpp @@ -5489,6 +5489,67 @@ struct test_dsv4_hc_post : public test_dsv4_hc { } }; +// qwen4exp hc_combine with the merged down_inject matmul: the scatter weights are 2*sigmoid of the last hc columns of a +// scaled [n_lo + hc, T] matmul output, i.e. a non-contiguous view, so backends can fuse SIGMOID -> SCALE -> DSV4_HC_POST +// (no input scale between the split and the sigmoid). +// with_lo: the merged output is a matmul result and its first n_lo columns also feed SILU -> MUL_MAT (the hc_mix lo path), +// the SCALE -> VIEW -> SILU fusion on the same node +struct test_dsv4_hc_post_gate3 : public test_dsv4_hc { + const int64_t n_embd; + const int64_t n_lo; + const int64_t n_tokens; + const bool with_lo; + + std::string op_desc(ggml_tensor * t) override { + GGML_UNUSED(t); + return "DSV4_HC_POST_GATE3"; + } + + std::string vars() override { + return VARS_TO_STR4(n_embd, n_lo, n_tokens, with_lo); + } + + bool run_whole_graph() override { return true; } + + test_dsv4_hc_post_gate3(int64_t n_embd = 2560, int64_t n_lo = 256, int64_t n_tokens = 1, bool with_lo = false) + : n_embd(n_embd), n_lo(n_lo), n_tokens(n_tokens), with_lo(with_lo) {} + + ggml_tensor * build_graph(ggml_context * ctx) override { + ggml_tensor * x = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_embd, n_tokens); + ggml_set_name(x, "x"); + + ggml_tensor * residual = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd, hc, n_tokens); + ggml_set_name(residual, "residual"); + + ggml_tensor * gate = nullptr; + if (with_lo) { + constexpr int64_t n_in = 64; + ggml_tensor * inp = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_in, n_tokens); + ggml_tensor * w_di = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_in, n_lo + hc); + gate = ggml_mul_mat(ctx, w_di, inp); + } else { + gate = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_lo + hc, n_tokens); + ggml_set_name(gate, "gate"); + } + + // the merged matmul output [n_lo + hc, T], scaled by 1/hc before the split + ggml_tensor * merged = ggml_scale(ctx, gate, 1.0f / (float) hc); + ggml_tensor * inject = ggml_view_2d(ctx, merged, hc, n_tokens, merged->nb[1], ggml_row_size(merged->type, n_lo)); + + if (with_lo) { + ggml_tensor * lo = ggml_view_2d(ctx, merged, n_lo, n_tokens, merged->nb[1], 0); + ggml_tensor * w_up = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n_lo, n_embd); + x = ggml_add(ctx, x, ggml_mul_mat(ctx, w_up, ggml_silu(ctx, lo))); + } + + ggml_tensor * w = ggml_scale(ctx, ggml_sigmoid(ctx, inject), 2.0f); + + out = ggml_dsv4_hc_post(ctx, x, residual, w, nullptr); + ggml_set_name(out, "out"); + return out; + } +}; + struct test_dsv4_hc_chain : public test_case { const ggml_type wtype; const int64_t n_embd; @@ -13368,6 +13429,15 @@ static std::vector> make_test_cases_eval() { test_cases.emplace_back(new test_dsv4_hc_post(31, 17, false, true)); test_cases.emplace_back(new test_dsv4_hc_post(4096, 1)); // DeepSeek-V4-Flash decode test_cases.emplace_back(new test_dsv4_hc_post(2560, 1, true, true)); // Qwen4-Exp decode + // SIGMOID -> SCALE -> DSV4_HC_POST on a view of the merged down_inject output (w2-qwen4-hc-fusions) + for (int64_t n_tokens : {1, 3, 512}) { + test_cases.emplace_back(new test_dsv4_hc_post_gate3(2560, 256, n_tokens)); + } + test_cases.emplace_back(new test_dsv4_hc_post_gate3(31, 5, 17)); + for (int64_t n_tokens : {1, 3, 512}) { + test_cases.emplace_back(new test_dsv4_hc_post_gate3(2560, 256, n_tokens, true)); + } + test_cases.emplace_back(new test_dsv4_hc_post_gate3(96, 40, 17, true)); for (int64_t n_embd : {96, 256}) { for (int64_t n_tokens : {1, 64}) { test_cases.emplace_back(new test_dsv4_hc_chain(GGML_TYPE_Q4_0, n_embd, n_tokens, true, true)); @@ -17196,6 +17266,8 @@ static std::vector> make_test_cases_perf() { test_cases.emplace_back(new test_dsv4_hc_post(7168, n_tokens)); // DeepSeek-V4-Pro test_cases.emplace_back(new test_dsv4_hc_post(2560, n_tokens, true, true)); // Qwen4-Exp, with the post gate chain test_cases.emplace_back(new test_dsv4_hc_post(2560, n_tokens, true)); // synthetic, Qwen4-Exp kernel without the gate chain + test_cases.emplace_back(new test_dsv4_hc_post_gate3(2560, 256, n_tokens)); // Qwen4-Exp merged down_inject layout + test_cases.emplace_back(new test_dsv4_hc_post_gate3(2560, 256, n_tokens, true)); // same, with the lo SILU path } // Qwen3.8-Flash-Next pooled indexer prefill.