Commit 72cda4a22 for llama.cpp
commit 72cda4a2228c254f95a556fa238488a1324b04d8
Author: Ruben Ortlam <rortlam@redhat.com>
Date: Tue Nov 24 15:21:34 2026 +0100
vulkan: add chunked GDN replay support (#32721)
* vulkan: add replay support to chunked GDN shaders
* use atomicStore instead of atomicExchange for readability
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan-types.h b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
index a121436e6..b71e73dd0 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan-types.h
+++ b/ggml/src/ggml-vulkan/ggml-vulkan-types.h
@@ -1015,9 +1015,9 @@ struct vk_device_struct {
vk_pipeline pipeline_lightning_indexer_f32[GGML_TYPE_COUNT];
// [size_idx][kda][replay] where size_idx: 0=d16, 1=d32, 2=d64, 3=d128
vk_pipeline pipeline_gated_delta_net[4][2][2];
- vk_pipeline pipeline_gated_delta_net_cm;
- vk_pipeline pipeline_gated_delta_net_cm_v32;
- vk_pipeline pipeline_gated_delta_net_cm_v128;
+ vk_pipeline pipeline_gated_delta_net_cm[2];
+ vk_pipeline pipeline_gated_delta_net_cm_v32[2];
+ vk_pipeline pipeline_gated_delta_net_cm_v128[2];
vk_pipeline pipeline_ssm_scan_f32_d96;
vk_pipeline pipeline_ssm_scan_f32_d128;
vk_pipeline pipeline_ssm_scan_f32_d256;
diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
index 74486b850..76550d60d 100644
--- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp
+++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp
@@ -1672,7 +1672,7 @@ static uint32_t ggml_vk_mul_mat_vec_max_cols(const vk_device & device) {
return device->coopmat2 && !device->disable_narrow_mul_mat ? 4u : mul_mat_vec_max_cols;
}
-static uint32_t ggml_vk_gated_delta_net_shmem_size(uint32_t v, bool coopmat2) {
+static uint32_t ggml_vk_gated_delta_net_shmem_size(uint32_t v, bool coopmat2, bool replay = false) {
const uint32_t C = 16;
const uint32_t LDP = GGML_VK_GDN_CM_LDP;
const uint32_t bf16_size = sizeof(ggml_bf16_t);
@@ -1682,7 +1682,9 @@ static uint32_t ggml_vk_gated_delta_net_shmem_size(uint32_t v, bool coopmat2) {
const uint32_t state = gmem_mirror ? 0 : 2 * v * LDP * bf16_size;
const uint32_t p_inv = 4 * C * C * bf16_size;
const uint32_t delta_solved = coopmat2 ? 2 * C * (v + GGML_VK_GDN_CM_PAD) * bf16_size : 4 * C * v * bf16_size;
- const uint32_t lower = (coopmat2 ? C * (C - 1) / 2 : C * C) * sizeof(float);
+ const uint32_t lower_elements = coopmat2 ? C * (C - 1) / 2 : C * C;
+ const uint32_t replay_partials = replay && !coopmat2 ? (128 / 16) * v : 0;
+ const uint32_t lower = std::max(lower_elements, replay_partials) * sizeof(float);
const uint32_t scan = 4 * C * sizeof(float);
return qk + state + p_inv + delta_solved + lower + scan;
@@ -4226,9 +4228,12 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
const uint32_t gdn_shmem = device->properties.limits.maxComputeSharedMemorySize;
#if defined(VK_NV_cooperative_matrix2) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
if (gdn_chunked && device->coopmat2 && device->coopmat2_gdn_support) {
- ggml_vk_create_pipeline(device, device->pipeline_gated_delta_net_cm,
+ ggml_vk_create_pipeline(device, device->pipeline_gated_delta_net_cm[0],
"gated_delta_net_f32_cm2", gated_delta_net_f32_cm2_len, gated_delta_net_f32_cm2_data,
"main", 7, sizeof(vk_op_gated_delta_net_push_constants), {1, 1, 1}, {}, 1, true, true, 32);
+ ggml_vk_create_pipeline(device, device->pipeline_gated_delta_net_cm[1],
+ "gated_delta_net_f32_cm2_rep", gated_delta_net_f32_cm2_rep_len, gated_delta_net_f32_cm2_rep_data,
+ "main", 11, sizeof(vk_op_gated_delta_net_push_constants), {1, 1, 1}, {}, 1, true, true, 32);
}
#endif
#if defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) && defined(GGML_VULKAN_COOPMAT_MAINTENANCE1_GLSLC_SUPPORT) && defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
@@ -4244,29 +4249,35 @@ void ggml_vk_load_shaders(vk_device& device, vk_pipeline requested) {
gdn_sg >= 16 && gdn_sg_ok(gdn_sg)) {
const bool bf16acc = device->coopmat_bf16_acc_support;
const std::vector<uint32_t> gdn_spec = { 16u * gdn_sg };
- // V=64: state mirror in shared, 7 bindings
+ const auto create_cm1 = [&](vk_pipeline & pipeline, uint32_t params, const char * name, size_t len, const void * data) {
+ ggml_vk_create_pipeline(device, pipeline, name, len, data,
+ "main", params, sizeof(vk_op_gated_delta_net_push_constants), {1, 1, 1}, gdn_spec, 1, true, true, gdn_sg);
+ };
+#define GDN_CM1_SHADER(suffix) \
+ bf16acc ? "gated_delta_net_f32_cm1_bf16acc" #suffix : "gated_delta_net_f32_cm1" #suffix, \
+ bf16acc ? gated_delta_net_f32_cm1_bf16acc##suffix##_len : gated_delta_net_f32_cm1##suffix##_len, \
+ bf16acc ? gated_delta_net_f32_cm1_bf16acc##suffix##_data : gated_delta_net_f32_cm1##suffix##_data
if (gdn_shmem >= ggml_vk_gated_delta_net_shmem_size(GGML_VK_GDN_CM_V, false)) {
- ggml_vk_create_pipeline(device, device->pipeline_gated_delta_net_cm,
- bf16acc ? "gated_delta_net_f32_cm1_bf16acc" : "gated_delta_net_f32_cm1",
- bf16acc ? gated_delta_net_f32_cm1_bf16acc_len : gated_delta_net_f32_cm1_len,
- bf16acc ? (const void *)gated_delta_net_f32_cm1_bf16acc_data : (const void *)gated_delta_net_f32_cm1_data,
- "main", 7, sizeof(vk_op_gated_delta_net_push_constants), {1, 1, 1}, gdn_spec, 1, true, true, gdn_sg);
+ create_cm1(device->pipeline_gated_delta_net_cm[0], 7, GDN_CM1_SHADER());
}
if (gdn_shmem >= ggml_vk_gated_delta_net_shmem_size(32u, false)) {
- ggml_vk_create_pipeline(device, device->pipeline_gated_delta_net_cm_v32,
- bf16acc ? "gated_delta_net_f32_cm1_bf16acc_v32" : "gated_delta_net_f32_cm1_v32",
- bf16acc ? gated_delta_net_f32_cm1_bf16acc_v32_len : gated_delta_net_f32_cm1_v32_len,
- bf16acc ? (const void *)gated_delta_net_f32_cm1_bf16acc_v32_data : (const void *)gated_delta_net_f32_cm1_v32_data,
- "main", 7, sizeof(vk_op_gated_delta_net_push_constants), {1, 1, 1}, gdn_spec, 1, true, true, gdn_sg);
+ create_cm1(device->pipeline_gated_delta_net_cm_v32[0], 7, GDN_CM1_SHADER(_v32));
}
- // V=128: state mirror in a gmem scratch buffer, 8 bindings
if (gdn_shmem >= ggml_vk_gated_delta_net_shmem_size(128u, false)) {
- ggml_vk_create_pipeline(device, device->pipeline_gated_delta_net_cm_v128,
- bf16acc ? "gated_delta_net_f32_cm1_bf16acc_v128" : "gated_delta_net_f32_cm1_v128",
- bf16acc ? gated_delta_net_f32_cm1_bf16acc_v128_len : gated_delta_net_f32_cm1_v128_len,
- bf16acc ? (const void *)gated_delta_net_f32_cm1_bf16acc_v128_data : (const void *)gated_delta_net_f32_cm1_v128_data,
- "main", 8, sizeof(vk_op_gated_delta_net_push_constants), {1, 1, 1}, gdn_spec, 1, true, true, gdn_sg);
+ create_cm1(device->pipeline_gated_delta_net_cm_v128[0], 8, GDN_CM1_SHADER(_v128));
+ }
+ if (device->coopmat_m1_reductions) {
+ if (gdn_shmem >= ggml_vk_gated_delta_net_shmem_size(GGML_VK_GDN_CM_V, false, true)) {
+ create_cm1(device->pipeline_gated_delta_net_cm[1], 11, GDN_CM1_SHADER(_rep));
+ }
+ if (gdn_shmem >= ggml_vk_gated_delta_net_shmem_size(32u, false, true)) {
+ create_cm1(device->pipeline_gated_delta_net_cm_v32[1], 11, GDN_CM1_SHADER(_v32_rep));
+ }
+ if (gdn_shmem >= ggml_vk_gated_delta_net_shmem_size(128u, false, true)) {
+ create_cm1(device->pipeline_gated_delta_net_cm_v128[1], 12, GDN_CM1_SHADER(_v128_rep));
+ }
}
+#undef GDN_CM1_SHADER
}
#endif
GGML_UNUSED(gdn_chunked);
@@ -11361,24 +11372,28 @@ void ggml_vk_gated_delta_net(ggml_backend_vk_context * ctx, vk_context& subctx,
const uint32_t s_off = S_v * H * n_tokens * n_seqs;
const ggml_tensor * src_g = dst->src[3];
+ const bool replay = dst->src[6] != nullptr;
+ const bool keep = replay && ggml_get_op_params_i32(dst, 2) != 0;
+ const vk_pipeline & cm = ctx->device->pipeline_gated_delta_net_cm[replay];
+ const vk_pipeline & cm_v32 = ctx->device->pipeline_gated_delta_net_cm_v32[replay];
+ const vk_pipeline & cm_v128 = ctx->device->pipeline_gated_delta_net_cm_v128[replay];
+ const bool rdna3 = ctx->device->architecture == AMD_RDNA3;
// RDNA3: the scan is faster below ~4k tokens
- const uint32_t cm_min_tokens = ctx->device->architecture == AMD_RDNA3 ? 4096u : 64u;
- const bool use_cm = (ctx->device->pipeline_gated_delta_net_cm != nullptr ||
- ctx->device->pipeline_gated_delta_net_cm_v32 != nullptr ||
- ctx->device->pipeline_gated_delta_net_cm_v128 != nullptr) &&
- src_g->ne[0] == 1 && S_v == 128 && dst->src[0]->ne[0] == 128 && K == 1 && n_tokens >= cm_min_tokens &&
- dst->src[6] == nullptr; // the chunked kernels do not replay
+ const uint32_t cm_min_tokens = rdna3 ? 4096u : 64u;
+ const bool use_cm = (cm || cm_v32 || cm_v128) &&
+ src_g->ne[0] == 1 && S_v == 128 && dst->src[0]->ne[0] == 128 && K == 1 && n_tokens >= cm_min_tokens && !keep;
const bool use_cm2 = use_cm && ctx->device->coopmat2;
- const bool have_v64 = ctx->device->pipeline_gated_delta_net_cm != nullptr;
- bool use_v32 = use_cm && !use_cm2 && !have_v64 && ctx->device->pipeline_gated_delta_net_cm_v32 != nullptr;
+ const bool have_v64 = cm != nullptr;
+ bool use_v32 = use_cm && !use_cm2 && !have_v64 && cm_v32 != nullptr;
bool use_v128 = use_cm && !use_cm2 && !have_v64 && !use_v32;
const uint32_t nsm = ctx->device->shader_core_count;
if (use_cm && !use_cm2 && have_v64 && nsm != 0) {
const uint32_t wg64 = 2u * H * n_seqs;
// V=32 only pays off while V=64 leaves at least half the cores idle
- use_v32 = ctx->device->pipeline_gated_delta_net_cm_v32 != nullptr && 2u * wg64 <= nsm;
- if (!use_v32 && ctx->device->pipeline_gated_delta_net_cm_v128 != nullptr) {
+ use_v32 = cm_v32 != nullptr && 2u * wg64 <= nsm;
+ // Strix Halo favors V=64 at equal CU fill.
+ if (!rdna3 && !use_v32 && cm_v128 != nullptr) {
const uint32_t wg128 = H * n_seqs;
const uint32_t waves64 = (wg64 + nsm - 1) / nsm;
const uint32_t waves128 = (wg128 + nsm - 1) / nsm;
@@ -11390,9 +11405,9 @@ void ggml_vk_gated_delta_net(ggml_backend_vk_context * ctx, vk_context& subctx,
const uint32_t cm_V = use_cm2 ? GGML_VK_GDN_CM2_V : (use_v128 ? 128u : (use_v32 ? 32u : GGML_VK_GDN_CM_V));
vk_pipeline pipeline = use_v128
- ? ctx->device->pipeline_gated_delta_net_cm_v128
- : use_v32 ? ctx->device->pipeline_gated_delta_net_cm_v32
- : (use_cm ? ctx->device->pipeline_gated_delta_net_cm
+ ? cm_v128
+ : use_v32 ? cm_v32
+ : (use_cm ? cm
: ggml_vk_op_get_pipeline(ctx, dst->src[0], dst->src[1], dst->src[2], dst, dst->op));
GGML_ASSERT(pipeline != nullptr);
@@ -11464,20 +11479,29 @@ void ggml_vk_gated_delta_net(ggml_backend_vk_context * ctx, vk_context& subctx,
src_pgb ? (uint32_t)(src_pgb->nb[3] / sizeof(float)) : 0u,
};
- if (use_v128) {
+ const std::array<uint32_t, 3> groups = use_cm ?
+ std::array<uint32_t, 3>{ S_v / cm_V, n_seqs, H } : std::array<uint32_t, 3>{ H, n_seqs, S_v };
+ if (use_v128 && replay) {
+ ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
+ {src_buf[0], src_buf[1], src_buf[2], src_buf[3], src_buf[4], src_buf[5], dst_buf,
+ src_buf[6], src_buf[7], src_buf[8], src_buf[9], mirror_buf},
+ pc, groups);
+ } else if (use_v128) {
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
{src_buf[0], src_buf[1], src_buf[2], src_buf[3], src_buf[4], src_buf[5], dst_buf, mirror_buf},
- pc, std::array<uint32_t, 3>{ S_v / cm_V, n_seqs, H });
- ctx->prealloc_x_need_sync = true;
- } else if (use_cm) {
+ pc, groups);
+ } else if (use_cm && !replay) {
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
{src_buf[0], src_buf[1], src_buf[2], src_buf[3], src_buf[4], src_buf[5], dst_buf},
- pc, std::array<uint32_t, 3>{ S_v / cm_V, n_seqs, H });
+ pc, groups);
} else {
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline,
{src_buf[0], src_buf[1], src_buf[2], src_buf[3], src_buf[4], src_buf[5], dst_buf,
src_buf[6], src_buf[7], src_buf[8], src_buf[9]},
- pc, std::array<uint32_t, 3>{ H, n_seqs, S_v });
+ pc, groups);
+ }
+ if (use_v128) {
+ ctx->prealloc_x_need_sync = true;
}
}
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm.comp b/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm.comp
index c5b4e4084..1539e0f17 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm.comp
@@ -42,29 +42,14 @@ const uint NQK = C * D / WGS;
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
-layout(push_constant) uniform Parameters {
- uint H;
- uint n_tokens;
- uint n_seqs;
- uint s_off;
- uint sq1, sq2, sq3;
- uint sk1, sk2, sk3;
- uint sv1, sv2, sv3;
- uint sb1, sb2, sb3;
- uint neq1, rq3;
- float scale;
- uint K;
-} p;
-
-layout(binding = 0) readonly buffer QBuf { float data_q[]; };
-layout(binding = 1) readonly buffer KBuf { float data_k[]; };
-layout(binding = 2) readonly buffer VBuf { float data_v[]; };
-layout(binding = 3) readonly buffer GBuf { float data_g[]; };
-layout(binding = 4) readonly buffer BetaBuf { float data_beta[]; };
-layout(binding = 5) readonly buffer StateBuf { float data_state[]; };
-layout(binding = 6) buffer DstBuf { float data_dst[]; };
+#include "gated_delta_net_cm.glsl"
+
#ifdef GMEM_MIRROR
-layout(binding = 7) coherent buffer MirrorBuf { OPD data_mirror[]; };
+#ifdef GDN_REPLAY
+layout(binding = 11) coherent buffer MirrorBuf { OPD data_mirror[]; };
+#else
+layout(binding = 7) coherent buffer MirrorBuf { OPD data_mirror[]; };
+#endif
#endif
shared OPD sh_qh[C * LDP], sh_ql[C * LDP]; // q*scale hi/lo (D padded)
@@ -76,7 +61,12 @@ shared OPD sh_dh[C * V], sh_dl[C * V]; // delta (pre-inv) / delta2 [C x V] row
shared OPD sh_soh[C * V], sh_sol[C * V]; // solved [C x V] row-major
shared OPD sh_ph[C * C], sh_pl[C * C]; // decayed P
shared OPD sh_ih[C * C], sh_il[C * C]; // (I + L)^-1
-shared float sh_lower[C * C]; // strictly-lower decayed L
+#ifdef GDN_REPLAY
+// Replay partial dots and the triangular solve use this scratch at different times.
+shared uint sh_lower[(D / TM) * V];
+#else
+shared uint sh_lower[C * C];
+#endif
shared float sh_prefix[C], sh_beta[C], sh_ep[C], sh_carry[C];
@@ -197,7 +187,7 @@ float StoreLower(const in uint32_t r, const in uint32_t c, const in float elem,
const in uint32_t c_o) {
const uint cc = c_o + c;
if (cc < r) {
- sh_lower[r * C + cc] = elem * exp(sh_prefix[r] - sh_prefix[cc]) * sh_beta[r];
+ sh_lower[r * C + cc] = floatBitsToUint(elem * exp(sh_prefix[r] - sh_prefix[cc]) * sh_beta[r]);
}
return elem;
}
@@ -268,11 +258,11 @@ void gram_pmat(const uint sg) {
void invert(const uint sg) {
const uint col = gl_SubgroupInvocationID;
[[unroll]] for (uint row = sg; row < C; row += NSUB) {
- float x = col < C ? (col == row ? 1.0 : (col < row ? -sh_lower[row * C + col] : 0.0)) : 0.0;
+ float x = col < C ? (col == row ? 1.0 : (col < row ? -uintBitsToFloat(sh_lower[row * C + col]) : 0.0)) : 0.0;
for (int pivot = int(row) - 1; pivot > 0; pivot--) {
const float xp = subgroupShuffle(x, uint(pivot));
if (col < uint(pivot)) {
- x -= xp * sh_lower[uint(pivot) * C + col];
+ x -= xp * uintBitsToFloat(sh_lower[uint(pivot) * C + col]);
}
}
if (col < C) {
@@ -289,6 +279,59 @@ float carry_decay(const uint valid) {
return exp(plast);
}
+#ifdef GDN_REPLAY
+float replay_product(const in uint32_t r, const in uint32_t c, const in float s, const in uint32_t k_off) {
+ return s * data_pk[k_off + r];
+}
+
+float replay_store_partial(const in uint32_t r, const in uint32_t c, const in float partial, const in uint32_t dot_off) {
+ if (r == 0) {
+ // A matrix element can be replicated across lanes.
+ atomicStore(sh_lower[dot_off + c], floatBitsToUint(partial), gl_ScopeWorkgroup, 0, 0);
+ }
+ return partial;
+}
+
+float replay_update(const in uint32_t r, const in uint32_t c, const in float s,
+ const in uint32_t k_off, const in uint32_t col) {
+ return s + data_pk[k_off + r] * uintBitsToFloat(sh_lower[col + c]);
+}
+
+void replay(inout coopmat<float, SG, TM, TN, MACC> state[PER_WARP],
+ uint seq_id, uint head_id, uint iq1, uint v0) {
+ const uint sg = gl_SubgroupID;
+ const uint tid = gl_LocalInvocationID.x;
+ const uint n_rep = replay_count(seq_id);
+ [[dont_unroll]] for (uint t = 0; t < n_rep; ++t) {
+ uint k_off, v_off;
+ float decay, beta;
+ replay_inputs(seq_id, head_id, iq1, t, k_off, v_off, decay, beta);
+ [[unroll]] for (uint j = 0; j < PER_WARP; ++j) {
+ const uint ti = sg + j * NSUB;
+ state[j] *= decay;
+ coopmat<float, SG, TM, TN, MACC> dot;
+ coopMatPerElementEXT(dot, state[j], replay_product, k_off + TILE_R(ti));
+ coopMatReduceEXT(dot, dot, gl_CooperativeMatrixReduceColumnEXT, replay_sum);
+ coopMatPerElementEXT(dot, dot, replay_store_partial, (ti / VT) * V + TILE_C(ti));
+ }
+ barrier();
+ if (tid < V) {
+ float dot = 0.0;
+ [[unroll]] for (uint i = 0; i < D / TM; ++i) {
+ dot += uintBitsToFloat(sh_lower[i * V + tid]);
+ }
+ sh_lower[tid] = floatBitsToUint(beta * (data_pv[v_off + v0 + tid] - dot));
+ }
+ barrier();
+ [[unroll]] for (uint j = 0; j < PER_WARP; ++j) {
+ const uint ti = sg + j * NSUB;
+ coopMatPerElementEXT(state[j], state[j], replay_update, k_off + TILE_R(ti), TILE_C(ti));
+ }
+ barrier();
+ }
+}
+#endif
+
void main() {
const uint head_id = gl_WorkGroupID.z;
const uint seq_id = gl_WorkGroupID.y;
@@ -314,6 +357,10 @@ void main() {
coopMatLoad(state[j], data_state, state_base + (v0 + TILE_C(ti)) * D + TILE_R(ti), D, COL);
}
+#ifdef GDN_REPLAY
+ replay(state, seq_id, head_id, iq1, v0);
+#endif
+
float qreg[NQK], kreg[NQK], greg, breg;
prefetch(0u, iq1, iq3, seq_id, head_id, qreg, kreg, greg, breg);
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm.glsl b/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm.glsl
new file mode 100644
index 000000000..2db576362
--- /dev/null
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm.glsl
@@ -0,0 +1,51 @@
+layout(push_constant) uniform Parameters {
+ uint H;
+ uint n_tokens;
+ uint n_seqs;
+ uint s_off;
+ uint sq1, sq2, sq3;
+ uint sk1, sk2, sk3;
+ uint sv1, sv2, sv3;
+ uint sb1, sb2, sb3;
+ uint neq1, rq3;
+ float scale;
+ uint K;
+#ifdef GDN_REPLAY
+ uint n_pend, keep;
+ uint spk1, spk2, spk3;
+ uint spv1, spv2, spv3;
+ uint spg1, spg2, spg3;
+#endif
+} p;
+
+layout(binding = 0) readonly buffer QBuf { float data_q[]; };
+layout(binding = 1) readonly buffer KBuf { float data_k[]; };
+layout(binding = 2) readonly buffer VBuf { float data_v[]; };
+layout(binding = 3) readonly buffer GBuf { float data_g[]; };
+layout(binding = 4) readonly buffer BetaBuf { float data_beta[]; };
+layout(binding = 5) readonly buffer StateBuf { float data_state[]; };
+layout(binding = 6) buffer DstBuf { float data_dst[]; };
+
+#ifdef GDN_REPLAY
+layout(binding = 7) readonly buffer PKBuf { float data_pk[]; };
+layout(binding = 8) readonly buffer PVBuf { float data_pv[]; };
+layout(binding = 9) readonly buffer PGBBuf { float data_pgb[]; };
+layout(binding = 10) readonly buffer NRepBuf { int data_n_rep[]; };
+
+uint replay_count(uint seq_id) {
+ return uint(clamp(data_n_rep[seq_id], 0, int(p.n_pend)));
+}
+
+void replay_inputs(uint seq_id, uint head_id, uint iq1, uint t,
+ out uint k_off, out uint v_off, out float decay, out float beta) {
+ k_off = seq_id * p.spk3 + t * p.spk2 + iq1 * p.spk1;
+ v_off = seq_id * p.spv3 + t * p.spv2 + head_id * p.spv1;
+ const uint g_off = seq_id * p.spg3 + t * p.spg2 + head_id * p.spg1;
+ decay = exp(data_pgb[g_off]);
+ beta = data_pgb[g_off + p.n_pend * p.spg2];
+}
+
+float replay_sum(const in float a, const in float b) {
+ return a + b;
+}
+#endif
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm2.comp b/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm2.comp
index 270ef35db..a9b7bc3f8 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm2.comp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/gated_delta_net_cm2.comp
@@ -29,27 +29,7 @@
layout(local_size_x = WGS, local_size_y = 1, local_size_z = 1) in;
-layout(push_constant) uniform Parameters {
- uint H;
- uint n_tokens;
- uint n_seqs;
- uint s_off;
- uint sq1, sq2, sq3;
- uint sk1, sk2, sk3;
- uint sv1, sv2, sv3;
- uint sb1, sb2, sb3;
- uint neq1, rq3;
- float scale;
- uint K;
-} p;
-
-layout(binding = 0) readonly buffer QBuf { float data_q[]; };
-layout(binding = 1) readonly buffer KBuf { float data_k[]; };
-layout(binding = 2) readonly buffer VBuf { float data_v[]; };
-layout(binding = 3) readonly buffer GBuf { float data_g[]; };
-layout(binding = 4) readonly buffer BetaBuf { float data_beta[]; };
-layout(binding = 5) readonly buffer StateBuf { float data_state[]; };
-layout(binding = 6) buffer DstBuf { float data_dst[]; };
+#include "gated_delta_net_cm.glsl"
// K and scaled Q occupy the two row blocks of one 32-row operand.
shared OPD sh_kqh[2 * C * LDP], sh_kql[2 * C * LDP];
@@ -205,6 +185,31 @@ void invert() {
}
}
+#ifdef GDN_REPLAY
+float replay_product(const in uint32_t r, const in uint32_t c, const in float s, const in uint32_t k_off) {
+ return s * data_pk[k_off + c];
+}
+
+float replay_update(const in uint32_t r, const in uint32_t c, const in float s, const in float dot,
+ const in uint32_t k_off, const in uint32_t v_off, const in float beta) {
+ return s + data_pk[k_off + c] * (beta * (data_pv[v_off + r] - dot));
+}
+
+void replay(inout coopmat<float, WG, V, D, MACC> state, uint seq_id, uint head_id, uint iq1, uint v0) {
+ const uint n_rep = replay_count(seq_id);
+ [[dont_unroll]] for (uint t = 0; t < n_rep; ++t) {
+ uint k_off, v_off;
+ float decay, beta;
+ replay_inputs(seq_id, head_id, iq1, t, k_off, v_off, decay, beta);
+ state *= decay;
+ coopmat<float, WG, V, D, MACC> dot;
+ coopMatPerElementNV(dot, state, replay_product, k_off);
+ coopMatReduceNV(dot, dot, gl_CooperativeMatrixReduceRowNV, replay_sum);
+ coopMatPerElementNV(state, state, replay_update, dot, k_off, v_off + v0, beta);
+ }
+}
+#endif
+
void main() {
const uint head_id = gl_WorkGroupID.z;
const uint seq_id = gl_WorkGroupID.y;
@@ -222,6 +227,10 @@ void main() {
coopmat<float, WG, V, D, MACC> state;
coopMatLoad(state, data_state, state_base, D, ROW);
+#ifdef GDN_REPLAY
+ replay(state, seq_id, head_id, iq1, v0);
+#endif
+
float qreg[NQK], kreg[NQK], greg, breg;
prefetch(0u, q_base, k_base, gb_base, qreg, kreg, greg, breg);
diff --git a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
index e20c94758..fb99f8f48 100644
--- a/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
+++ b/ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
@@ -1133,16 +1133,26 @@ void process_shaders() {
#if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT) && defined(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
string_to_spv("gated_delta_net_f32", "gated_delta_net_cm2.comp", base_dict, true, false, true);
+ string_to_spv("gated_delta_net_f32", "gated_delta_net_cm2.comp", merge_maps(base_dict, {{"GDN_REPLAY", "1"}}), true, false, true, false, "_rep");
#endif
#if defined(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT) && defined(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT) && defined(GGML_VULKAN_COOPMAT_MAINTENANCE1_GLSLC_SUPPORT)
- string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", merge_maps(base_dict, {{"BF16ACC", "1"}}), true, true, false, false, "_bf16acc");
- string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", base_dict, true, true);
- string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", merge_maps(base_dict, {{"BF16ACC", "1"}, {"V", "32"}}), true, true, false, false, "_bf16acc_v32");
- string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", merge_maps(base_dict, {{"V", "32"}}), true, true, false, false, "_v32");
- // whole-head V=128 variant: state mirror in a gmem scratch buffer (won't fit LDS).
- // For head counts that tail the CU grid at V=64 (e.g. 48-head 27B on 64 CUs).
- string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", merge_maps(base_dict, {{"BF16ACC", "1"}, {"GMEM_MIRROR", "1"}, {"V", "128"}}), true, true, false, false, "_bf16acc_v128");
- string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", merge_maps(base_dict, {{"GMEM_MIRROR", "1"}, {"V", "128"}}), true, true, false, false, "_v128");
+ for (bool replay : {false, true}) {
+ for (bool bf16acc : {false, true}) {
+ auto defines = base_dict;
+ if (replay) {
+ defines["GDN_REPLAY"] = "1";
+ }
+ if (bf16acc) {
+ defines["BF16ACC"] = "1";
+ }
+ const std::string acc_suffix = bf16acc ? "_bf16acc" : "";
+ const std::string rep_suffix = replay ? "_rep" : "";
+ string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", defines, true, true, false, false, acc_suffix + rep_suffix);
+ string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", merge_maps(defines, {{"V", "32"}}), true, true, false, false, acc_suffix + "_v32" + rep_suffix);
+ // The whole-head state mirror is too large for shared memory.
+ string_to_spv("gated_delta_net_f32", "gated_delta_net_cm.comp", merge_maps(defines, {{"GMEM_MIRROR", "1"}, {"V", "128"}}), true, true, false, false, acc_suffix + "_v128" + rep_suffix);
+ }
+ }
#endif
string_to_spv("opt_step_adamw_f32", "opt_step_adamw.comp", merge_maps(base_dict, {{"A_TYPE", "float"}}));
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 0b468fda2..772c9f02d 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -5537,17 +5537,20 @@ struct test_gated_delta_net_replay : public test_case {
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
if (ggml_is_view_op(t->op)) { continue; }
if (strcmp(t->name, "g") == 0) {
- init_tensor_uniform(t, -20.0f, -1e-4f);
+ // Keep the effect of replay visible in the first chunk.
+ init_tensor_uniform(t, n_seq_tokens >= 64 ? -0.125f : -20.0f, -1e-4f);
} else if (strcmp(t->name, "beta") == 0) {
init_tensor_uniform(t, 0.0f, 1.0f);
} else if (strcmp(t->name, "v") == 0) {
init_tensor_uniform(t, -0.3f, 5.0f);
} else if (strcmp(t->name, "n_rep") == 0) {
std::vector<int32_t> data(n_seqs);
- for (auto & x : data) {
- x = std::uniform_int_distribution<int32_t>(0, n_pend)(rng);
+ for (int64_t i = 0; i < n_seqs; ++i) {
+ data[i] = n_seq_tokens >= 64
+ ? (i % 2 == 0 ? n_pend : 0)
+ : std::uniform_int_distribution<int32_t>(0, n_pend)(rng);
}
- ggml_backend_tensor_set(t, data.data(), 0, data.size()*sizeof(int32_t));
+ ggml_backend_tensor_set(t, data.data(), 0, data.size() * sizeof(int32_t));
} else if (strcmp(t->name, "pend_k") == 0) {
// unit rows, as the l2 norm leaves them
std::vector<float> data(ggml_nelements(t));
@@ -14165,11 +14168,13 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_gated_delta_net_replay(4, 32, 1, 2, 2, false, 3, keep));
test_cases.emplace_back(new test_gated_delta_net_replay(4, 64, 3, 2, 1, true, 4, keep));
}
- // prefill sized: the replay runs before the chunked kernels (CUDA from 64 tokens, HIP from 2048)
+ // Prefill sized to exercise chunked kernels that support replay.
test_cases.emplace_back(new test_gated_delta_net_replay(16, 128, 65, 1, 1, false, 4, false));
test_cases.emplace_back(new test_gated_delta_net_replay(16, 128, 257, 1, 3, false, 4, false));
test_cases.emplace_back(new test_gated_delta_net_replay(16, 128, 513, 2, 2, false, 8, false));
test_cases.emplace_back(new test_gated_delta_net_replay(16, 128, 2048, 1, 3, false, 4, false));
+ // RDNA3 needs at least 4096 tokens for the chunked kernel.
+ test_cases.emplace_back(new test_gated_delta_net_replay( 4, 128, 4097, 2, 1, false, 4, false));
test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 16, 1, 1));
test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 16, 1, 1, 1, true, true));
test_cases.emplace_back(new test_gated_delta_net(GGML_TYPE_F32, 32, 16, 1, 1, 1, false, true));