Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions ggml/src/ggml-cuda/fusion.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "concat.cuh"
#include "dsv4-hc.cuh"
#include "fp8.cuh"
#include "fwht.cuh"
#include "gated_delta_net.cuh"
#include "inkling2.cuh"
#include "mmsm.cuh"
Expand Down Expand Up @@ -2354,6 +2355,97 @@ static int ggml_cuda_try_fuse_rms_norm2(ggml_backend_cuda_context * cuda_ctx, gg
return n1 + 1;
}

// the K/V cache stores fused with the op that makes the rows, GGML_CUDA_NO_KV_STORE_FUSION=1 turns them off
static bool ggml_cuda_kv_store_fusion_disabled() {
static const bool disabled = getenv("GGML_CUDA_NO_KV_STORE_FUSION") != nullptr;
return disabled;
}

// Hadamard rotation of K or V (MUL_MAT with the Hadamard hint) stored in a quantized cache: MUL_MAT, RESHAPE, VIEW,
// [RESHAPE of the cache,] SET_ROWS -> one kernel that rotates, quantizes and stores
static int ggml_cuda_try_fuse_fwht_set_rows(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
if (ggml_cuda_kv_store_fusion_disabled() || cuda_ctx->curr_stream_no != 0) {
return 0;
}
ggml_tensor ** n = cgraph->nodes + i;
if (i + 3 >= cgraph->n_nodes || !ggml_cuda_op_mul_mat_use_fwht(n[0]) || n[1]->op != GGML_OP_RESHAPE || n[1]->src[0] != n[0] ||
n[2]->op != GGML_OP_VIEW || n[2]->src[0] != n[1] || n[2]->view_offs != 0) {
return 0;
}
int last = i + 3;
if (n[3]->op == GGML_OP_RESHAPE && i + 4 < cgraph->n_nodes && n[4]->op == GGML_OP_SET_ROWS && n[4]->src[2] == n[3]) {
last = i + 4;
}
ggml_tensor * set_rows = cgraph->nodes[last];
if (set_rows->op != GGML_OP_SET_ROWS || set_rows->src[0] != n[2] || (set_rows->type != GGML_TYPE_Q8_0 && set_rows->type != GGML_TYPE_Q4_0) ||
!ggml_is_contiguous(n[2])) {
return 0;
}
const ggml_op ops4[] = { GGML_OP_MUL_MAT, GGML_OP_RESHAPE, GGML_OP_VIEW, GGML_OP_SET_ROWS };
const ggml_op ops5[] = { GGML_OP_MUL_MAT, GGML_OP_RESHAPE, GGML_OP_VIEW, GGML_OP_RESHAPE, GGML_OP_SET_ROWS };
const int outputs[] = { last };
if (!ggml_can_fuse_subgraph(cgraph, i, last - i + 1, last == i + 3 ? ops4 : ops5, outputs, 1)) {
return 0;
}
if (!ggml_cuda_op_fwht_set_rows(*cuda_ctx, n[0]->src[1], set_rows)) {
return 0;
}
return last - i;
}

// RMS norm without weight of V stored in the cache (Gemma 4, Inkling 2) -> one kernel: RMS_NORM, VIEW, [RESHAPE of the cache,]
// SET_ROWS for a F16 / F32 / BF16 cache, RMS_NORM, RESHAPE, MUL_MAT (Hadamard), RESHAPE, VIEW, [RESHAPE,] SET_ROWS for a rotated q8_0 / q4_0 cache
static int ggml_cuda_try_fuse_rms_norm_set_rows(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
if (ggml_cuda_kv_store_fusion_disabled() || cuda_ctx->curr_stream_no != 0) {
return 0;
}
ggml_tensor ** n = cgraph->nodes + i;
if (n[0]->op != GGML_OP_RMS_NORM) {
return 0;
}
// rotated quantized cache: RMS_NORM, RESHAPE, MUL_MAT (Hadamard), RESHAPE, VIEW, [RESHAPE of the cache,] SET_ROWS
if (i + 5 < cgraph->n_nodes && n[1]->op == GGML_OP_RESHAPE && n[1]->src[0] == n[0] && ggml_cuda_op_mul_mat_use_fwht(n[2]) &&
n[2]->src[1] == n[1] && n[3]->op == GGML_OP_RESHAPE && n[3]->src[0] == n[2] && n[4]->op == GGML_OP_VIEW &&
n[4]->src[0] == n[3] && n[4]->view_offs == 0 && ggml_is_contiguous(n[4])) {
int last = i + 5;
if (n[5]->op == GGML_OP_RESHAPE && i + 6 < cgraph->n_nodes && n[6]->op == GGML_OP_SET_ROWS && n[6]->src[2] == n[5]) {
last = i + 6;
}
ggml_tensor * set_rows = cgraph->nodes[last];
if (set_rows->op != GGML_OP_SET_ROWS || set_rows->src[0] != n[4]) {
return 0;
}
const ggml_op ops6[] = { GGML_OP_RMS_NORM, GGML_OP_RESHAPE, GGML_OP_MUL_MAT, GGML_OP_RESHAPE, GGML_OP_VIEW, GGML_OP_SET_ROWS };
const ggml_op ops7[] = { GGML_OP_RMS_NORM, GGML_OP_RESHAPE, GGML_OP_MUL_MAT, GGML_OP_RESHAPE, GGML_OP_VIEW, GGML_OP_RESHAPE, GGML_OP_SET_ROWS };
const int outputs[] = { last };
if (!ggml_can_fuse_subgraph(cgraph, i, last - i + 1, last == i + 5 ? ops6 : ops7, outputs, 1)) {
return 0;
}
return ggml_cuda_op_rms_norm_fwht_set_rows(*cuda_ctx, n[0], n[2], set_rows) ? last - i : 0;
}
if (i + 2 >= cgraph->n_nodes || n[1]->op != GGML_OP_VIEW || n[1]->src[0] != n[0] || n[1]->view_offs != 0 || !ggml_is_contiguous(n[1])) {
return 0;
}
int last = i + 2;
if (n[2]->op == GGML_OP_RESHAPE && i + 3 < cgraph->n_nodes && n[3]->op == GGML_OP_SET_ROWS && n[3]->src[2] == n[2]) {
last = i + 3;
}
ggml_tensor * set_rows = cgraph->nodes[last];
if (set_rows->op != GGML_OP_SET_ROWS || set_rows->src[0] != n[1]) {
return 0;
}
const ggml_op ops3[] = { GGML_OP_RMS_NORM, GGML_OP_VIEW, GGML_OP_SET_ROWS };
const ggml_op ops4[] = { GGML_OP_RMS_NORM, GGML_OP_VIEW, GGML_OP_RESHAPE, GGML_OP_SET_ROWS };
const int outputs[] = { last };
if (!ggml_can_fuse_subgraph(cgraph, i, last - i + 1, last == i + 2 ? ops3 : ops4, outputs, 1)) {
return 0;
}
if (!ggml_cuda_op_rms_norm_set_rows(*cuda_ctx, n[0], set_rows)) {
return 0;
}
return last - i;
}

// try and fuse nodes and return the number of nodes to skip
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)) {
Expand All @@ -2362,6 +2454,18 @@ int ggml_cuda_try_fuse(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgrap

ggml_tensor * node = cgraph->nodes[i];

if (node->op == GGML_OP_MUL_MAT) {
if (int n = ggml_cuda_try_fuse_fwht_set_rows(cuda_ctx, cgraph, i)) {
return n;
}
}

if (node->op == GGML_OP_RMS_NORM) {
if (int n = ggml_cuda_try_fuse_rms_norm_set_rows(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);

Expand Down
Loading
Loading