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
36 changes: 36 additions & 0 deletions ggml/src/ggml-cuda/fusion.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "fwht.cuh"
#include "gated_delta_net.cuh"
#include "inkling2.cuh"
#include "lightning-indexer.cuh"
#include "mmsm.cuh"
#include "mmsk.cuh"
#include "mmq.cuh"
Expand Down Expand Up @@ -2446,6 +2447,35 @@ static int ggml_cuda_try_fuse_rms_norm_set_rows(ggml_backend_cuda_context * cuda
return last - i;
}

// Qwen4 QSA: GET_ROWS of the pooled keys in the key cache, RESHAPE, LIGHTNING_INDEXER -> the indexer reads the keys from the
// cache rows, the f32 copy of all pooled keys is not made. GGML_CUDA_NO_LI_GATHER_FUSION=1 turns it off.
static int ggml_cuda_try_fuse_li_gather(ggml_backend_cuda_context * cuda_ctx, ggml_cgraph * cgraph, int i) {
static const bool disabled = [] {
const char * env = getenv("GGML_CUDA_NO_LI_GATHER_FUSION");
return env != nullptr && strcmp(env, "0") != 0;
}();
if (disabled || cuda_ctx->curr_stream_no != 0 || i + 2 >= cgraph->n_nodes) {
return 0;
}
ggml_tensor ** n = cgraph->nodes + i;
if (n[0]->op != GGML_OP_GET_ROWS || n[1]->op != GGML_OP_RESHAPE || n[1]->src[0] != n[0] ||
n[2]->op != GGML_OP_LIGHTNING_INDEXER || n[2]->src[1] != n[1] || n[2]->src[0] == n[1] || n[2]->src[2] == n[1] || n[2]->src[3] == n[1]) {
return 0;
}
// the GET_ROWS result and its reshape have no other consumer and are no graph outputs
const ggml_op ops[] = { GGML_OP_GET_ROWS, GGML_OP_RESHAPE, GGML_OP_LIGHTNING_INDEXER };
const int outputs[] = { i + 2 };
if (!ggml_can_fuse_subgraph(cgraph, i, 3, ops, outputs, 1)) {
return 0;
}
if (!ggml_cuda_check_fusion_memory_ranges(cgraph, i, 3, outputs, 1) ||
!ggml_cuda_lightning_indexer_rows_supported(cuda_ctx->device, n[2], n[0])) {
return 0;
}
ggml_cuda_op_lightning_indexer_rows(*cuda_ctx, n[2], n[0]);
return 2;
}

// 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 @@ -2454,6 +2484,12 @@ 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_GET_ROWS) {
if (int n = ggml_cuda_try_fuse_li_gather(cuda_ctx, cgraph, i)) {
return n;
}
}

if (node->op == GGML_OP_MUL_MAT) {
if (int n = ggml_cuda_try_fuse_fwht_set_rows(cuda_ctx, cgraph, i)) {
return n;
Expand Down
87 changes: 76 additions & 11 deletions ggml/src/ggml-cuda/lightning-indexer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ static __global__ void lightning_indexer_kernel_mma(
size_t nbk1, size_t nbk2, size_t nbk3,
size_t nbw1, size_t nbw2, size_t nbw3,
size_t nbm1, size_t nbm2, size_t nbm3,
int64_t nem3
int64_t nem3, const int32_t * K_rows
) {
GGML_UNUSED(K_rows); // the gathered keys are scored by the vec and tile kernels only

constexpr int MMA_DIM = 16;
constexpr int WAVE_SIZE = ggml_cuda_get_physical_warp_size();
constexpr int THREADS_PER_BLOCK = WARPS_PER_BLOCK * WAVE_SIZE;
Expand Down Expand Up @@ -195,7 +197,7 @@ static __global__ void lightning_indexer_kernel_vec(
size_t nbk1, size_t nbk2, size_t nbk3,
size_t nbw1, size_t nbw2, size_t nbw3,
size_t nbm1, size_t nbm2, size_t nbm3,
int64_t nem3
int64_t nem3, const int32_t * K_rows
) {

constexpr int K_VECS_PER_WARP = K_VECS_PER_BLOCK / WARPS_PER_BLOCK;
Expand Down Expand Up @@ -224,7 +226,8 @@ static __global__ void lightning_indexer_kernel_vec(
for (int k = 0; k < K_VECS_PER_WARP; ++k) {
int i_kv = start_kv + k;
if (i_kv < n_kv) {
const float4 * k_base = (const float4 *) ((const char *) K + i_kv*nbk2 + i_stream*nbk3);
const int64_t i_row = K_rows ? K_rows[i_kv] : i_kv;
const float4 * k_base = (const float4 *) ((const char *) K + i_row*nbk2 + i_stream*nbk3);
k_reg_f[k] = k_base[i_lane];
} else {
k_reg_f[k] = make_float4(0, 0, 0, 0);
Expand All @@ -237,7 +240,8 @@ static __global__ void lightning_indexer_kernel_vec(
for (int k = 0; k < K_VECS_PER_WARP; ++k) {
int i_kv = start_kv + k;
if (i_kv < n_kv) {
const void * k_base = (const void *) ((const char *) K + i_kv*nbk2 + i_stream*nbk3);
const int64_t i_row = K_rows ? K_rows[i_kv] : i_kv;
const void * k_base = (const void *) ((const char *) K + i_row*nbk2 + i_stream*nbk3);
dequantize_k(k_base, &k_reg_f[k], i_lane * 4);
} else {
k_reg_f[k] = make_float4(0, 0, 0, 0);
Expand Down Expand Up @@ -341,7 +345,7 @@ static __global__ void lightning_indexer_kernel_tile(
size_t nbk1, size_t nbk2, size_t nbk3,
size_t nbw1, size_t nbw2, size_t nbw3,
size_t nbm1, size_t nbm2, size_t nbm3,
int64_t nem3
int64_t nem3, const int32_t * K_rows
) {

constexpr int THREADS_PER_BLOCK = WARPS_PER_BLOCK * WARP_SIZE;
Expand Down Expand Up @@ -375,7 +379,8 @@ static __global__ void lightning_indexer_kernel_tile(
half2 lo = make_half2(0.0f, 0.0f);
half2 hi = lo;
if (start_kv + r < n_kv) {
const char * k_row = K + (start_kv + r)*nbk2 + i_stream*nbk3;
const int64_t i_row = K_rows ? K_rows[start_kv + r] : start_kv + r;
const char * k_row = K + i_row*nbk2 + i_stream*nbk3;
if constexpr (TYPE_K == GGML_TYPE_F16) {
lo = ((const half2 *) k_row)[2*c4 + 0];
hi = ((const half2 *) k_row)[2*c4 + 1];
Expand Down Expand Up @@ -494,16 +499,18 @@ static __global__ void lightning_indexer_kernel_tile(
n_stream, n_batch, n_kv, \
nb1, nb2, nb3, \
nbq1, nbq2, nbq3, \
nbk1, nbk2, nbk3, \
nbk1, k_nb2, k_nb3, \
nbw1, nbw2, nbw3, \
nbm1, nbm2, nbm3, \
nem3 \
nem3, rows_d \
); \
} else

void ggml_cuda_lightning_indexer(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
// k is the tensor the keys are read from: dst->src[1], or with rows the 2-D source of the GET_ROWS that
// made it, and then rows[i] is the row of key i and n_kv_rows their number
static void lightning_indexer_launch(
ggml_backend_cuda_context & ctx, ggml_tensor * dst, const ggml_tensor * k, const int32_t * rows_d, int64_t n_kv_rows) {
const ggml_tensor * q = dst->src[0];
const ggml_tensor * k = dst->src[1];
const ggml_tensor * w = dst->src[2]; // weights
const ggml_tensor * m = dst->src[3]; // mask

Expand Down Expand Up @@ -539,7 +546,12 @@ void ggml_cuda_lightning_indexer(ggml_backend_cuda_context & ctx, ggml_tensor *
const int n_head = q->ne[1];
const int n_batch = q->ne[2];
const int n_stream = q->ne[3];
const int n_kv = k->ne[2];
const int n_kv = rows_d ? n_kv_rows : k->ne[2];

// key row strides: a gathered key is a row of the 2-D source, one stream
const size_t k_nb2 = rows_d ? nbk1 : nbk2;
const size_t k_nb3 = rows_d ? 0 : nbk3;
GGML_ASSERT(!rows_d || (n_stream == 1 && n_embd == 128 && n_head == 4));

const float * q_d = (const float *) q->data;
const char * k_d = (const char *) k->data;
Expand Down Expand Up @@ -701,6 +713,59 @@ void ggml_cuda_lightning_indexer(ggml_backend_cuda_context & ctx, ggml_tensor *
}
}

void ggml_cuda_lightning_indexer(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
lightning_indexer_launch(ctx, dst, dst->src[1], nullptr, 0);
}

// GET_ROWS of a key cache + RESHAPE + LIGHTNING_INDEXER: the indexer reads the keys straight from the cache rows,
// the gathered f32 copy is never made. The keys are widened exactly as GET_ROWS widens them (f16 is exact, q8_0 is
// d*q in f32 with the same single multiply), so the scores are bit identical.
void ggml_cuda_op_lightning_indexer_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst, const ggml_tensor * get_rows) {
const ggml_tensor * src0 = get_rows->src[0];
const ggml_tensor * rows = get_rows->src[1];

lightning_indexer_launch(ctx, dst, src0, (const int32_t *) rows->data, get_rows->ne[1]);
}

bool ggml_cuda_lightning_indexer_rows_supported(int device, const ggml_tensor * dst, const ggml_tensor * get_rows) {
const ggml_tensor * src0 = get_rows->src[0];
const ggml_tensor * rows = get_rows->src[1];
const ggml_tensor * q = dst->src[0];
const ggml_tensor * k = dst->src[1];

if (!ggml_cuda_lightning_indexer_supported(device, dst)) {
return false;
}

// the 4 head vec and tile kernels only, one stream
if (q->ne[0] != 128 || q->ne[1] != 4 || q->ne[3] != 1 || k->ne[3] != 1) {
return false;
}

// the keys the indexer would have read: the rows of a 2-D cache view
if (get_rows->type != GGML_TYPE_F32 || k->type != GGML_TYPE_F32 || k->ne[0] != 128 || k->ne[1] != 1 ||
k->ne[2] != get_rows->ne[1] || get_rows->ne[0] != 128 || get_rows->ne[2] != 1 || get_rows->ne[3] != 1) {
return false;
}
if (src0->ne[0] != 128 || src0->ne[2] != 1 || src0->ne[3] != 1 || src0->nb[0] != ggml_type_size(src0->type)) {
return false;
}
if (rows->type != GGML_TYPE_I32 || rows->ne[0] != get_rows->ne[1] || rows->ne[1] != 1 || rows->ne[2] != 1 ||
rows->ne[3] != 1 || rows->nb[0] != sizeof(int32_t)) {
return false;
}

// the key loads of the kernels: f16 in 8 byte pieces, q8_0 in 2 byte aligned quants
switch (src0->type) {
case GGML_TYPE_F16:
return src0->nb[1] % 8 == 0 && (uintptr_t) src0->data % 8 == 0;
case GGML_TYPE_Q8_0:
return src0->nb[1] % 2 == 0 && (uintptr_t) src0->data % 2 == 0;
default:
return false;
}
}

bool ggml_cuda_lightning_indexer_supported(int device, const ggml_tensor * dst) {
GGML_UNUSED(device);

Expand Down
5 changes: 5 additions & 0 deletions ggml/src/ggml-cuda/lightning-indexer.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

void ggml_cuda_lightning_indexer(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
bool ggml_cuda_lightning_indexer_supported(int device, const ggml_tensor * dst);

// the keys gathered by get_rows (src[0] a 2-D f16 or q8_0 cache view, src[1] the I32 rows) read in place: the indexer must score
// the reshape of get_rows and use the 4 head kernels
bool ggml_cuda_lightning_indexer_rows_supported(int device, const ggml_tensor * dst, const ggml_tensor * get_rows);
void ggml_cuda_op_lightning_indexer_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst, const ggml_tensor * get_rows);
Loading
Loading