@@ -425,6 +425,7 @@ struct vk_device_struct {
425425    vk_pipeline pipeline_norm_f32;
426426    vk_pipeline pipeline_group_norm_f32;
427427    vk_pipeline pipeline_rms_norm_f32;
428+     vk_pipeline pipeline_rms_norm_mul_f32;
428429    vk_pipeline pipeline_rms_norm_back_f32;
429430    vk_pipeline pipeline_l2_norm_f32;
430431
@@ -978,6 +979,10 @@ struct ggml_backend_vk_context {
978979
979980    vk_command_pool compute_cmd_pool;
980981    vk_command_pool transfer_cmd_pool;
982+ 
983+     // number of additional consecutive nodes that are being fused with the
984+     // node currently being processed
985+     uint32_t num_additional_fused_ops {};
981986};
982987
983988static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000;  // NOLINT
@@ -2655,7 +2660,8 @@ static void ggml_vk_load_shaders(vk_device& device) {
26552660
26562661    ggml_vk_create_pipeline(device, device->pipeline_norm_f32, "norm_f32", norm_f32_len, norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
26572662    ggml_vk_create_pipeline(device, device->pipeline_group_norm_f32, "group_norm_f32", group_norm_f32_len, group_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
2658-     ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 2, sizeof(vk_op_unary_push_constants), {1, 1, 1}, {}, 1);
2663+     ggml_vk_create_pipeline(device, device->pipeline_rms_norm_f32, "rms_norm_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 0}, 1);
2664+     ggml_vk_create_pipeline(device, device->pipeline_rms_norm_mul_f32, "rms_norm_mul_f32", rms_norm_f32_len, rms_norm_f32_data, "main", 3, sizeof(vk_op_binary_push_constants), {1, 1, 1}, {0, 1}, 1);
26592665    ggml_vk_create_pipeline(device, device->pipeline_rms_norm_back_f32, "rms_norm_back_f32", rms_norm_back_f32_len, rms_norm_back_f32_data, "main", 3, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
26602666    ggml_vk_create_pipeline(device, device->pipeline_l2_norm_f32, "l2_norm_f32", l2_norm_f32_len, l2_norm_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, {}, 1);
26612667
@@ -6430,7 +6436,7 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
64306436        return nullptr;
64316437    case GGML_OP_RMS_NORM:
64326438        if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
6433-             return ctx->device->pipeline_rms_norm_f32;
6439+             return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx-> device->pipeline_rms_norm_f32;
64346440        }
64356441        return nullptr;
64366442    case GGML_OP_RMS_NORM_BACK:
@@ -7530,18 +7536,19 @@ static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx
75307536    ggml_vk_op_f32<vk_op_push_constants>(ctx, subctx, src0, nullptr, nullptr, dst, GGML_OP_GROUP_NORM, { group_size, 0, eps, 0.0f }, dryrun);
75317537}
75327538
7533- static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
7539+ static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, const ggml_tensor * src1,  ggml_tensor * dst, bool dryrun = false) {
75347540    float * op_params = (float *)dst->op_params;
75357541    const uint32_t src0_type_size = ggml_type_size(src0->type);
7542+     const uint32_t src1_type_size = ggml_type_size(src1->type);
75367543    const uint32_t dst_type_size = ggml_type_size(dst->type);
75377544
7538-     ggml_vk_op_f32<vk_op_unary_push_constants >(ctx, subctx, src0, nullptr , nullptr, dst, GGML_OP_RMS_NORM, {
7545+     ggml_vk_op_f32<vk_op_binary_push_constants >(ctx, subctx, src0, src1 , nullptr, dst, GGML_OP_RMS_NORM, {
75397546        (uint32_t)ggml_nelements(src0),
7540-         (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2], (uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
7541-         (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2], (uint32_t) dst->ne[3], (uint32_t) dst->nb[0] /  dst_type_size, (uint32_t) dst->nb[1] /  dst_type_size, (uint32_t) dst->nb[2] /  dst_type_size, (uint32_t) dst->nb[3] /  dst_type_size,
7547+         (uint32_t)src0->ne[0], (uint32_t)src0->ne[1], (uint32_t)src0->ne[2],(uint32_t)src0->ne[3], (uint32_t)src0->nb[0] / src0_type_size, (uint32_t)src0->nb[1] / src0_type_size, (uint32_t)src0->nb[2] / src0_type_size, (uint32_t)src0->nb[3] / src0_type_size,
7548+         (uint32_t)src1->ne[0], (uint32_t)src1->ne[1], (uint32_t)src1->ne[2],(uint32_t)src1->ne[3], (uint32_t)src1->nb[0] / src1_type_size, (uint32_t)src1->nb[1] / src1_type_size, (uint32_t)src1->nb[2] / src1_type_size, (uint32_t)src1->nb[3] / src1_type_size,
7549+         (uint32_t) dst->ne[0], (uint32_t) dst->ne[1], (uint32_t) dst->ne[2],(uint32_t) dst->ne[3], (uint32_t) dst->nb[0] /  dst_type_size, (uint32_t) dst->nb[1] /  dst_type_size, (uint32_t) dst->nb[2] /  dst_type_size, (uint32_t) dst->nb[3] /  dst_type_size,
75427550        0,
7543-         op_params[0], 0.0f,
7544-         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7551+         op_params[0], 0.0f, 0,
75457552    }, dryrun);
75467553}
75477554
@@ -8736,7 +8743,8 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* t
87368743
87378744// Returns true if node has enqueued work into the queue, false otherwise
87388745// If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
8739- static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * node, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool almost_ready, bool submit){
8746+ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgraph, int node_idx, ggml_tensor *node_begin, int node_idx_begin, bool dryrun, bool last_node, bool almost_ready, bool submit){
8747+     ggml_tensor * node = cgraph->nodes[node_idx];
87408748    if (ggml_is_empty(node) || !node->buffer) {
87418749        return false;
87428750    }
@@ -8974,8 +8982,14 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * nod
89748982
89758983        break;
89768984    case GGML_OP_RMS_NORM:
8977-         ggml_vk_rms_norm(ctx, compute_ctx, src0, node, dryrun);
8978- 
8985+         if (ctx->num_additional_fused_ops > 0) {
8986+             // fused rms_norm + mul
8987+             ggml_tensor *mul = cgraph->nodes[node_idx + 1];
8988+             ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
8989+             ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, dryrun);
8990+         } else {
8991+             ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, dryrun);
8992+         }
89798993        break;
89808994    case GGML_OP_RMS_NORM_BACK:
89818995        ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
@@ -9710,10 +9724,15 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
97109724
97119725    uint64_t total_mat_mul_bytes = 0;
97129726    for (int i = 0; i < cgraph->n_nodes; i++) {
9713-         ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false, false);
9727+         if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
9728+             ctx->num_additional_fused_ops = 1;
9729+         }
9730+         ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
97149731        if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
97159732            total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
97169733        }
9734+         i += ctx->num_additional_fused_ops;
9735+         ctx->num_additional_fused_ops = 0;
97179736    }
97189737    if (ctx->device->need_compiles) {
97199738        ggml_vk_load_shaders(ctx->device);
@@ -9775,14 +9794,18 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
97759794            mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
97769795        }
97779796
9797+         if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
9798+             ctx->num_additional_fused_ops = 1;
9799+         }
9800+ 
97789801        // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
97799802        bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
97809803        bool submit = (submitted_nodes >= nodes_per_submit) ||
97819804                      (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
9782-                       (i == last_node) ||
9805+                       (i + ctx->num_additional_fused_ops  == last_node) ||
97839806                      (almost_ready && !ctx->almost_ready_fence_pending);
97849807
9785-         bool enqueued = ggml_vk_build_graph(ctx, cgraph->nodes[i] , i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i == last_node, almost_ready, submit);
9808+         bool enqueued = ggml_vk_build_graph(ctx, cgraph, i, cgraph->nodes[submit_node_idx], submit_node_idx, false, i + ctx->num_additional_fused_ops  == last_node, almost_ready, submit);
97869809
97879810        if (vk_perf_logger_enabled) {
97889811            if (ctx->compute_ctx.expired()) {
@@ -9792,7 +9815,10 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
97929815            } else {
97939816                compute_ctx = ctx->compute_ctx.lock();
97949817            }
9795-             compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+1);
9818+             // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
9819+             for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
9820+                 compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
9821+             }
97969822        }
97979823
97989824        if (enqueued) {
@@ -9814,6 +9840,8 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
98149840            }
98159841            submit_count++;
98169842        }
9843+         i += ctx->num_additional_fused_ops;
9844+         ctx->num_additional_fused_ops = 0;
98179845    }
98189846
98199847    if (vk_perf_logger_enabled) {
0 commit comments