@@ -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
@@ -987,6 +988,10 @@ struct ggml_backend_vk_context {
987988
988989 vk_command_pool compute_cmd_pool;
989990 vk_command_pool transfer_cmd_pool;
991+
992+ // number of additional consecutive nodes that are being fused with the
993+ // node currently being processed
994+ uint32_t num_additional_fused_ops {};
990995};
991996
992997static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
@@ -2664,7 +2669,8 @@ static void ggml_vk_load_shaders(vk_device& device) {
26642669
26652670 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);
26662671 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);
2667- 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);
2672+ 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);
2673+ 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);
26682674 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);
26692675 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);
26702676
@@ -6438,7 +6444,7 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
64386444 return nullptr;
64396445 case GGML_OP_RMS_NORM:
64406446 if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
6441- return ctx->device->pipeline_rms_norm_f32;
6447+ return ctx->num_additional_fused_ops > 0 ? ctx->device->pipeline_rms_norm_mul_f32 : ctx-> device->pipeline_rms_norm_f32;
64426448 }
64436449 return nullptr;
64446450 case GGML_OP_RMS_NORM_BACK:
@@ -7538,18 +7544,19 @@ static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx
75387544 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);
75397545}
75407546
7541- static void ggml_vk_rms_norm(ggml_backend_vk_context * ctx, vk_context& subctx, const ggml_tensor * src0, ggml_tensor * dst, bool dryrun = false) {
7547+ 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) {
75427548 float * op_params = (float *)dst->op_params;
75437549 const uint32_t src0_type_size = ggml_type_size(src0->type);
7550+ const uint32_t src1_type_size = ggml_type_size(src1->type);
75447551 const uint32_t dst_type_size = ggml_type_size(dst->type);
75457552
7546- ggml_vk_op_f32<vk_op_unary_push_constants >(ctx, subctx, src0, nullptr , nullptr, dst, GGML_OP_RMS_NORM, {
7553+ ggml_vk_op_f32<vk_op_binary_push_constants >(ctx, subctx, src0, src1 , nullptr, dst, GGML_OP_RMS_NORM, {
75477554 (uint32_t)ggml_nelements(src0),
7548- (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,
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,
7555+ (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,
7556+ (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,
7557+ (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,
75507558 0,
7551- op_params[0], 0.0f,
7552- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7559+ op_params[0], 0.0f, 0,
75537560 }, dryrun);
75547561}
75557562
@@ -8753,7 +8760,8 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* t
87538760
87548761// Returns true if node has enqueued work into the queue, false otherwise
87558762// If submit is true the current all operations queued so far are being submitted to Vulkan to overlap cmdlist creation and GPU execution.
8756- 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){
8763+ 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){
8764+ ggml_tensor * node = cgraph->nodes[node_idx];
87578765 if (ggml_is_empty(node) || !node->buffer) {
87588766 return false;
87598767 }
@@ -8991,8 +8999,14 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * nod
89918999
89929000 break;
89939001 case GGML_OP_RMS_NORM:
8994- ggml_vk_rms_norm(ctx, compute_ctx, src0, node, dryrun);
8995-
9002+ if (ctx->num_additional_fused_ops > 0) {
9003+ // fused rms_norm + mul
9004+ ggml_tensor *mul = cgraph->nodes[node_idx + 1];
9005+ ggml_tensor *other_src = mul->src[0] == node ? mul->src[1] : mul->src[0];
9006+ ggml_vk_rms_norm(ctx, compute_ctx, src0, other_src, mul, dryrun);
9007+ } else {
9008+ ggml_vk_rms_norm(ctx, compute_ctx, src0, src0, node, dryrun);
9009+ }
89969010 break;
89979011 case GGML_OP_RMS_NORM_BACK:
89989012 ggml_vk_rms_norm_back(ctx, compute_ctx, src0, src1, node, dryrun);
@@ -9727,10 +9741,15 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
97279741
97289742 uint64_t total_mat_mul_bytes = 0;
97299743 for (int i = 0; i < cgraph->n_nodes; i++) {
9730- ggml_vk_build_graph(ctx, cgraph->nodes[i], i, nullptr, 0, true, false, false, false);
9744+ if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
9745+ ctx->num_additional_fused_ops = 1;
9746+ }
9747+ ggml_vk_build_graph(ctx, cgraph, i, nullptr, 0, true, false, false, false);
97319748 if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
97329749 total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
97339750 }
9751+ i += ctx->num_additional_fused_ops;
9752+ ctx->num_additional_fused_ops = 0;
97349753 }
97359754 if (ctx->device->need_compiles) {
97369755 ggml_vk_load_shaders(ctx->device);
@@ -9792,14 +9811,18 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
97929811 mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
97939812 }
97949813
9814+ if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
9815+ ctx->num_additional_fused_ops = 1;
9816+ }
9817+
97959818 // Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
97969819 bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
97979820 bool submit = (submitted_nodes >= nodes_per_submit) ||
97989821 (mul_mat_bytes >= mul_mat_bytes_per_submit) ||
9799- (i == last_node) ||
9822+ (i + ctx->num_additional_fused_ops == last_node) ||
98009823 (almost_ready && !ctx->almost_ready_fence_pending);
98019824
9802- 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);
9825+ 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);
98039826
98049827 if (vk_perf_logger_enabled) {
98059828 if (ctx->compute_ctx.expired()) {
@@ -9809,7 +9832,10 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
98099832 } else {
98109833 compute_ctx = ctx->compute_ctx.lock();
98119834 }
9812- compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+1);
9835+ // If there are fused ops, just write out timestamps for all nodes to keep the accounting simple
9836+ for (int j = 0; j < ctx->num_additional_fused_ops + 1; ++j) {
9837+ compute_ctx->s->buffer.writeTimestamp(vk::PipelineStageFlagBits::eAllCommands, ctx->device->query_pool, i+j+1);
9838+ }
98139839 }
98149840
98159841 if (enqueued) {
@@ -9831,6 +9857,8 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
98319857 }
98329858 submit_count++;
98339859 }
9860+ i += ctx->num_additional_fused_ops;
9861+ ctx->num_additional_fused_ops = 0;
98349862 }
98359863
98369864 if (vk_perf_logger_enabled) {
0 commit comments