@@ -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
@@ -978,6 +979,10 @@ struct ggml_backend_vk_context {
978
979
979
980
vk_command_pool compute_cmd_pool;
980
981
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 {};
981
986
};
982
987
983
988
static void * const vk_ptr_base = (void *)(uintptr_t) 0x1000; // NOLINT
@@ -2655,7 +2660,8 @@ static void ggml_vk_load_shaders(vk_device& device) {
2655
2660
2656
2661
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);
2657
2662
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);
2659
2665
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);
2660
2666
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);
2661
2667
@@ -6430,7 +6436,7 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
6430
6436
return nullptr;
6431
6437
case GGML_OP_RMS_NORM:
6432
6438
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;
6434
6440
}
6435
6441
return nullptr;
6436
6442
case GGML_OP_RMS_NORM_BACK:
@@ -7530,18 +7536,19 @@ static void ggml_vk_group_norm(ggml_backend_vk_context * ctx, vk_context& subctx
7530
7536
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);
7531
7537
}
7532
7538
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) {
7534
7540
float * op_params = (float *)dst->op_params;
7535
7541
const uint32_t src0_type_size = ggml_type_size(src0->type);
7542
+ const uint32_t src1_type_size = ggml_type_size(src1->type);
7536
7543
const uint32_t dst_type_size = ggml_type_size(dst->type);
7537
7544
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, {
7539
7546
(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,
7542
7550
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,
7545
7552
}, dryrun);
7546
7553
}
7547
7554
@@ -8736,7 +8743,8 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context* ctx, ggml_tensor* t
8736
8743
8737
8744
// Returns true if node has enqueued work into the queue, false otherwise
8738
8745
// 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];
8740
8748
if (ggml_is_empty(node) || !node->buffer) {
8741
8749
return false;
8742
8750
}
@@ -8974,8 +8982,14 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_tensor * nod
8974
8982
8975
8983
break;
8976
8984
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
+ }
8979
8993
break;
8980
8994
case GGML_OP_RMS_NORM_BACK:
8981
8995
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
9710
9724
9711
9725
uint64_t total_mat_mul_bytes = 0;
9712
9726
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);
9714
9731
if (cgraph->nodes[i]->op == GGML_OP_MUL_MAT || cgraph->nodes[i]->op == GGML_OP_MUL_MAT_ID) {
9715
9732
total_mat_mul_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
9716
9733
}
9734
+ i += ctx->num_additional_fused_ops;
9735
+ ctx->num_additional_fused_ops = 0;
9717
9736
}
9718
9737
if (ctx->device->need_compiles) {
9719
9738
ggml_vk_load_shaders(ctx->device);
@@ -9775,14 +9794,18 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
9775
9794
mul_mat_bytes += ggml_nbytes(cgraph->nodes[i]->src[0]);
9776
9795
}
9777
9796
9797
+ if (ggml_can_fuse(cgraph, i, { GGML_OP_RMS_NORM, GGML_OP_MUL })) {
9798
+ ctx->num_additional_fused_ops = 1;
9799
+ }
9800
+
9778
9801
// Signal the almost_ready fence when the graph is mostly complete (< 20% remaining)
9779
9802
bool almost_ready = (cgraph->n_nodes - i) < cgraph->n_nodes / 5;
9780
9803
bool submit = (submitted_nodes >= nodes_per_submit) ||
9781
9804
(mul_mat_bytes >= mul_mat_bytes_per_submit) ||
9782
- (i == last_node) ||
9805
+ (i + ctx->num_additional_fused_ops == last_node) ||
9783
9806
(almost_ready && !ctx->almost_ready_fence_pending);
9784
9807
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);
9786
9809
9787
9810
if (vk_perf_logger_enabled) {
9788
9811
if (ctx->compute_ctx.expired()) {
@@ -9792,7 +9815,10 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
9792
9815
} else {
9793
9816
compute_ctx = ctx->compute_ctx.lock();
9794
9817
}
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
+ }
9796
9822
}
9797
9823
9798
9824
if (enqueued) {
@@ -9814,6 +9840,8 @@ static ggml_status ggml_backend_vk_graph_compute(ggml_backend_t backend, ggml_cg
9814
9840
}
9815
9841
submit_count++;
9816
9842
}
9843
+ i += ctx->num_additional_fused_ops;
9844
+ ctx->num_additional_fused_ops = 0;
9817
9845
}
9818
9846
9819
9847
if (vk_perf_logger_enabled) {
0 commit comments