@@ -425,6 +425,7 @@ struct vk_device_struct {
425
425
vk_pipeline pipeline_norm_f32;
426
426
vk_pipeline pipeline_group_norm_f32;
427
427
vk_pipeline pipeline_rms_norm_f32;
428
+ vk_pipeline pipeline_rms_norm_mul_f32;
428
429
vk_pipeline pipeline_rms_norm_back_f32;
429
430
vk_pipeline pipeline_l2_norm_f32;
430
431
@@ -987,6 +988,10 @@ struct ggml_backend_vk_context {
987
988
988
989
vk_command_pool compute_cmd_pool;
989
990
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 {};
990
995
};
991
996
992
997
static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
@@ -2664,7 +2669,8 @@ static void ggml_vk_load_shaders(vk_device& device) {
2664
2669
2665
2670
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);
2666
2671
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);
2668
2674
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);
2669
2675
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);
2670
2676
@@ -6438,7 +6444,7 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
6438
6444
return nullptr;
6439
6445
case GGML_OP_RMS_NORM:
6440
6446
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;
6442
6448
}
6443
6449
return nullptr;
6444
6450
case GGML_OP_RMS_NORM_BACK:
@@ -7538,18 +7544,19 @@ static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx
7538
7544
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);
7539
7545
}
7540
7546
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) {
7542
7548
float * op_params = (float *)dst->op_params;
7543
7549
const uint32_t src0_type_size = ggml_type_size(src0->type);
7550
+ const uint32_t src1_type_size = ggml_type_size(src1->type);
7544
7551
const uint32_t dst_type_size = ggml_type_size(dst->type);
7545
7552
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, {
7547
7554
(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,
7550
7558
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,
7553
7560
}, dryrun);
7554
7561
}
7555
7562
@@ -8753,7 +8760,8 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* t
8753
8760
8754
8761
// Returns true if node has enqueued work into the queue, false otherwise
8755
8762
// 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];
8757
8765
if (ggml_is_empty(node) || !node->buffer) {
8758
8766
return false;
8759
8767
}
@@ -8991,8 +8999,14 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * nod
8991
8999
8992
9000
break;
8993
9001
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
+ }
8996
9010
break;
8997
9011
case GGML_OP_RMS_NORM_BACK:
8998
9012
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
9727
9741
9728
9742
uint64_t total_mat_mul_bytes = 0;
9729
9743
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);
9731
9748
if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
9732
9749
total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
9733
9750
}
9751
+ i += ctx->num_additional_fused_ops;
9752
+ ctx->num_additional_fused_ops = 0;
9734
9753
}
9735
9754
if (ctx->device->need_compiles) {
9736
9755
ggml_vk_load_shaders(ctx->device);
@@ -9792,14 +9811,18 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
9792
9811
mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
9793
9812
}
9794
9813
9814
+ if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
9815
+ ctx->num_additional_fused_ops = 1;
9816
+ }
9817
+
9795
9818
// Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
9796
9819
bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
9797
9820
bool submit = (submitted_nodes >= nodes_per_submit) ||
9798
9821
(mul_mat_bytes >= mul_mat_bytes_per_submit) ||
9799
- (i == last_node) ||
9822
+ (i + ctx->num_additional_fused_ops == last_node) ||
9800
9823
(almost_ready && !ctx->almost_ready_fence_pending);
9801
9824
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);
9803
9826
9804
9827
if (vk_perf_logger_enabled) {
9805
9828
if (ctx->compute_ctx.expired()) {
@@ -9809,7 +9832,10 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
9809
9832
} else {
9810
9833
compute_ctx = ctx->compute_ctx.lock();
9811
9834
}
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
+ }
9813
9839
}
9814
9840
9815
9841
if (enqueued) {
@@ -9831,6 +9857,8 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
9831
9857
}
9832
9858
submit_count++;
9833
9859
}
9860
+ i += ctx->num_additional_fused_ops;
9861
+ ctx->num_additional_fused_ops = 0;
9834
9862
}
9835
9863
9836
9864
if (vk_perf_logger_enabled) {
0 commit comments