Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Rework the paged Gated DeltaNet opt kernel and keep the existing one for short calls (#38793)

dhaidach4e3718ed5014
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.cpp
index 2e321693637..f5bcce8f824 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.cpp
@@ -7,6 +7,7 @@
#include <string_view>
#include "intel_gpu/primitives/paged_gated_delta_net.hpp"
+#include "paged_gated_delta_net_jit_utils.hpp"
#include "primitive_ocl_base.hpp"
#include "utils/kernel_generator.hpp"
@@ -18,21 +19,6 @@ size_t get_v_block_size(size_t v_head_dims) {
return 4;
}
-size_t get_subgroup_size(gpu_arch arch) {
- switch (arch) {
- case gpu_arch::gen9:
- case gpu_arch::gen11:
- case gpu_arch::xe_lp:
- case gpu_arch::xe_hp:
- case gpu_arch::xe_hpg:
- return 8;
- case gpu_arch::xe2:
- case gpu_arch::xe3:
- default:
- return 16;
- }
-}
-
size_t get_vec_size(const RuntimeParams& params) {
const auto& q_layout = params.get_input_layout(paged_gated_delta_net::QUERY);
const auto& q_shape = q_layout.get_partial_shape();
@@ -40,7 +26,7 @@ size_t get_vec_size(const RuntimeParams& params) {
const size_t k_head_dims = q_shape[2].get_length();
const size_t v_head_dims = v_shape[2].get_length();
- const size_t subgroup_size = get_subgroup_size(params.get_device_info().arch);
+ const size_t subgroup_size = paged_gated_delta_net_jit::get_subgroup_size(params.get_device_info().arch);
if ((k_head_dims % 16) != 0 || (v_head_dims % 16) != 0) {
return 1;
@@ -49,6 +35,7 @@ size_t get_vec_size(const RuntimeParams& params) {
size_t vec_size = 1;
switch (q_layout.data_type) {
case ov::element::f16:
+ case ov::element::bf16:
vec_size = 8;
break;
case ov::element::f32:
@@ -66,6 +53,51 @@ size_t get_vec_size(const RuntimeParams& params) {
return vec_size;
}
+// A call with few tokens per subsequence, decode included: up to 4 tokens for
+// one subsequence; for more, at most 8 tokens per subsequence on average for an
+// f32 query and at most 16 otherwise. The host sees only the totals, so the
+// average stands for the lengths.
+//
+// NOTE: The thresholds are deliberately powers of 2. They were chosen on Panther
+// Lake at (k_head_dims, v_head_dims) = (128, 128) with 16 key and 32 value
+// heads, in f16 and in f32 alike from the crossovers measured with one and
+// with two subsequences, and serve the other head sizes as well.
+// TODO: Consider retuning these thresholds for other devices, other (K, V) head
+// sizes and other numbers of subsequences.
+bool is_short_call(const RuntimeParams& params) {
+ const auto& q_layout = params.get_input_layout(paged_gated_delta_net::QUERY);
+ const auto& q_shape = q_layout.get_partial_shape();
+ const auto& seq_shape = params.get_input_layout(paged_gated_delta_net::SUBSEQUENCE_BEGINS).get_partial_shape();
+ if (q_shape[0].is_dynamic() || seq_shape[0].is_dynamic() || seq_shape[0].get_length() < 2) {
+ return false;
+ }
+ const auto tokens = q_shape[0].get_length();
+ const auto sequences = seq_shape[0].get_length() - 1;
+ const int64_t per_sequence = q_layout.data_type == ov::element::f32 ? 8 : 16;
+ return sequences == 1 ? tokens <= 4 : tokens <= per_sequence * sequences;
+}
+
+// Xe cores: the slices times the sub-slices per slice, or 0 where the driver
+// does not report them.
+size_t get_xe_core_count(const cldnn::device_info& info) {
+ return static_cast<size_t>(info.num_slices) * info.num_sub_slices_per_slice;
+}
+
+// A call runs in the short stage where is_short_call() holds, and every call does
+// on devices with more than 20 Xe cores.
+//
+// NOTE: On the B70 (BMG-G31), the general stage's kernel run on its own, at
+// (k_head_dims, v_head_dims) = (128, 128) with 16 key and 32 value heads, in f16,
+// for one subsequence, was on par with the short kernel at 256 tokens and faster
+// on longer subsequences up to 16Ki tokens, but the gain did not appear inside
+// Qwen3.5-9B-int4-ov. The cause is not known, nor whether it lies in the general
+// stage's kernel.
+// TODO: Find the cause on the B70 and run the general stage on the devices with
+// more than 20 Xe cores again.
+bool uses_short_stage(const RuntimeParams& params) {
+ return get_xe_core_count(params.get_device_info()) > 20 || is_short_call(params);
+}
+
class PagedGatedDeltaNetBaseGenerator : public KernelGenerator {
public:
PagedGatedDeltaNetBaseGenerator(std::string_view kernel_name, bool force_ref_path) : KernelGenerator(kernel_name), m_force_ref_path(force_ref_path) {}
@@ -91,7 +123,7 @@ protected:
jit.make("K_HEAD_DIM", k_head_dims);
jit.make("V_HEAD_DIM", v_head_dims);
jit.make("V_BLOCK_SIZE", get_v_block_size(v_head_dims));
- jit.make("SUBGROUP_SIZE", get_subgroup_size(params.get_device_info().arch));
+ jit.make("SUBGROUP_SIZE", paged_gated_delta_net_jit::get_subgroup_size(params.get_device_info().arch));
jit.make("K_VEC_SIZE", m_force_ref_path ? 1 : get_vec_size(params));
jit.make("FUSE_QK_L2NORM", desc->use_qk_l2norm ? 1 : 0);
jit.make("Q_L2_NORM_EPS", desc->q_l2_norm_eps);
@@ -129,7 +161,7 @@ protected:
const size_t v_head_dims = v_shape[2].get_length();
const size_t current_v_block_size = get_v_block_size(v_head_dims);
const size_t v_blocks = (v_head_dims + current_v_block_size - 1) / current_v_block_size;
- const size_t subgroup_size = get_subgroup_size(params.get_device_info().arch);
+ const size_t subgroup_size = paged_gated_delta_net_jit::get_subgroup_size(params.get_device_info().arch);
auto get_head_offset = [](const cldnn::layout& layout, size_t head_dim_idx) {
const auto& lower_pads = layout.data_padding._lower_size;
@@ -190,9 +222,118 @@ public:
PagedGatedDeltaNetRefGenerator() : PagedGatedDeltaNetBaseGenerator("paged_gated_delta_net_ref", true) {}
};
+class PagedGatedDeltaNetShortGenerator : public PagedGatedDeltaNetBaseGenerator {
+public:
+ PagedGatedDeltaNetShortGenerator() : PagedGatedDeltaNetBaseGenerator("paged_gated_delta_net_short", false) {}
+};
+
class PagedGatedDeltaNetOptGenerator : public PagedGatedDeltaNetBaseGenerator {
public:
PagedGatedDeltaNetOptGenerator() : PagedGatedDeltaNetBaseGenerator("paged_gated_delta_net_opt", false) {}
+
+protected:
+ [[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
+ auto jit = PagedGatedDeltaNetBaseGenerator::get_jit_constants(params);
+
+ const auto& q_shape = params.get_input_layout(paged_gated_delta_net::QUERY).get_partial_shape();
+ const size_t k_head_dims = q_shape[2].get_length();
+ const auto arch = params.get_device_info().arch;
+ const bool d_on_lanes = paged_gated_delta_net_jit::uses_d_on_lanes(k_head_dims, arch);
+ // NOTE: For k_head_dims > 128, where the tiling is always d-on-lanes, every
+ // call runs the plain token loop with one accumulator chain and no
+ // prefetch, instead of the staged loop with 4 chains and an 8-token
+ // prefetch. Tested on Panther Lake at (256, 256), f16 and f32, where the
+ // staged loop is faster on some very long subsequences and on some calls
+ // with many subsequences.
+ const bool plain_loop = k_head_dims > 128;
+
+ jit.make("D_LANES", d_on_lanes ? 1 : 0);
+ jit.remove("V_BLOCK_SIZE");
+ jit.make("V_BLOCK_SIZE", paged_gated_delta_net_jit::get_v_rows_per_thread(k_head_dims, arch));
+ jit.make("PLAIN_TOKEN_LOOP", plain_loop ? 1 : 0);
+
+ // Independent accumulator chains in the two dot products over the k axis.
+ jit.make("ACC_WAYS", plain_loop ? 1 : 4);
+
+ // Prefetch distance in tokens, and the request size in bytes.
+ jit.make("PF_AHEAD", plain_loop ? 0 : 8);
+ jit.make("PF_BYTES", 8);
+
+ jit.make("OCL_REQD_WG", d_on_lanes ? 1 : 0);
+
+ return jit;
+ }
+
+ // The state lives in registers, so the kernel is built for the large
+ // register file where the target has one.
+ [[nodiscard]] std::string get_build_options(const RuntimeParams& params) const override {
+ auto options = PagedGatedDeltaNetBaseGenerator::get_build_options(params);
+ if (paged_gated_delta_net_jit::supports_large_grf(params.get_device_info().arch)) {
+ options += " -cl-intel-256-GRF-per-thread";
+ }
+ return options;
+ }
+
+ [[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override {
+ return DispatchDataFunc{[](const RuntimeParams& params, KernelData& kd, ImplRuntimeParams* rt_params) {
+ assert(!params.is_dynamic());
+ auto& wgs = kd.params.workGroups;
+ const auto& q_shape = params.get_input_layout(paged_gated_delta_net::QUERY).get_partial_shape();
+ const auto& v_shape = params.get_input_layout(paged_gated_delta_net::VALUE).get_partial_shape();
+ const auto& seq_shape = params.get_input_layout(paged_gated_delta_net::SUBSEQUENCE_BEGINS).get_partial_shape();
+
+ const size_t sequences = seq_shape[0].get_length() > 0 ? seq_shape[0].get_length() - 1 : 0;
+ const size_t head_nums = v_shape[1].get_length();
+ const size_t k_head_dims = q_shape[2].get_length();
+ const size_t v_head_dims = v_shape[2].get_length();
+ const auto arch = params.get_device_info().arch;
+ const size_t subgroup_size = paged_gated_delta_net_jit::get_subgroup_size(arch);
+ const size_t v_rows = paged_gated_delta_net_jit::get_v_rows_per_thread(k_head_dims, arch);
+ const size_t v_tiles = v_head_dims / v_rows;
+
+ // The tile axis goes in dimension 0, so that the tiles of one head,
+ // which read the same q and k rows, have consecutive group ids.
+ wgs.global = {v_tiles * subgroup_size, head_nums, sequences};
+ wgs.local = {subgroup_size, 1, 1};
+
+ auto get_head_offset = [](const cldnn::layout& layout, size_t head_dim_idx) {
+ const auto& lower_pads = layout.data_padding._lower_size;
+ return lower_pads.size() > head_dim_idx ? lower_pads[head_dim_idx] : 0;
+ };
+
+ const auto& q_layout = params.input_layouts[paged_gated_delta_net::QUERY];
+ const auto& k_layout = params.input_layouts[paged_gated_delta_net::KEY];
+ const auto& v_layout = params.input_layouts[paged_gated_delta_net::VALUE];
+ auto read_pitch = [](const cldnn::layout& layout, size_t idx) -> int32_t {
+ const auto& pitches = layout.get_pitches();
+ if (idx < pitches.size()) {
+ return static_cast<int32_t>(pitches[idx]);
+ }
+ return 1;
+ };
+
+ kd.params.scalars.clear();
+ std::vector<int32_t> scalars{
+ static_cast<int32_t>(sequences),
+ static_cast<int32_t>(get_head_offset(q_layout, 1)),
+ static_cast<int32_t>(get_head_offset(k_layout, 1)),
+ static_cast<int32_t>(get_head_offset(v_layout, 1)),
+ read_pitch(q_layout, 0),
+ read_pitch(q_layout, 1),
+ read_pitch(k_layout, 0),
+ read_pitch(k_layout, 1),
+ read_pitch(v_layout, 0),
+ read_pitch(v_layout, 1),
+ };
+
+ for (auto v : scalars) {
+ scalar_desc desc;
+ desc.t = scalar_desc::Types::INT32;
+ desc.v.s32 = v;
+ kd.params.scalars.push_back(desc);
+ }
+ }};
+ }
};
class PagedGatedDeltaNetRefImpl : public PrimitiveImplOCL {
@@ -216,15 +357,26 @@ public:
DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::PagedGatedDeltaNetOptImpl)
Stage::Ptr paged_gated_delta_net = make_stage<PagedGatedDeltaNetOptGenerator>();
+ Stage::Ptr paged_gated_delta_net_short = make_stage<PagedGatedDeltaNetShortGenerator>();
PagedGatedDeltaNetOptImpl() : PrimitiveImplOCL(PagedGatedDeltaNetOpt::get_type_info_static()) {}
PagedGatedDeltaNetOptImpl(const program_node& node, const RuntimeParams& params) : PagedGatedDeltaNetOptImpl() {
+ // The general stage is always added, and added first: set_kernels() gives
+ // the i-th compiled kernel to the i-th stage declared with make_stage().
add_stage(paged_gated_delta_net, params);
+ if (params.is_dynamic() || uses_short_stage(params)) {
+ add_stage(paged_gated_delta_net_short, params);
+ }
}
[[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {
return make_deep_copy<PagedGatedDeltaNetOptImpl>(this);
}
+
+ // Indices in the order of make_stage().
+ [[nodiscard]] std::vector<size_t> get_stages_execution_order(const RuntimeParams& params) const override {
+ return {has_stage(paged_gated_delta_net_short) && uses_short_stage(params) ? size_t{1} : size_t{0}};
+ }
};
} // namespace
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.hpp
index 1d857b7da67..596ae9baf4f 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net.hpp
@@ -9,6 +9,7 @@
#include <utility>
#include "paged_gated_delta_net_inst.h"
+#include "paged_gated_delta_net_jit_utils.hpp"
#include "program_node.h"
#include "registry/implementation_manager.hpp"
@@ -82,7 +83,17 @@ protected:
const auto k_head_dims = q_shape[2].get_length();
const auto v_head_dims = v_shape[2].get_length();
- return (k_head_dims % 16) == 0 && (v_head_dims % 16) == 0;
+ if ((k_head_dims % 16) != 0 || (v_head_dims % 16) != 0) {
+ return false;
+ }
+
+ // The opt kernel puts the k axis (d-on-lanes) or the v axis
+ // (d_v-on-lanes) on the lanes, so the subgroup width has to divide
+ // k_head_dims and fit in v_head_dims. Shapes it cannot tile fall through
+ // to the ref path, which has no such requirement.
+ const auto& device_info = node.get_program().get_engine().get_device_info();
+ const auto subgroup_size = static_cast<int64_t>(paged_gated_delta_net_jit::get_subgroup_size(device_info.arch));
+ return (k_head_dims % subgroup_size) == 0 && v_head_dims >= subgroup_size;
}
};
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_jit_utils.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_jit_utils.hpp
new file mode 100644
index 00000000000..93cf5069587
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_jit_utils.hpp
@@ -0,0 +1,93 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cstddef>
+
+#include "intel_gpu/runtime/device_info.hpp"
+
+namespace ov::intel_gpu::ocl::paged_gated_delta_net_jit {
+
+inline size_t get_subgroup_size(cldnn::gpu_arch arch) {
+ switch (arch) {
+ case cldnn::gpu_arch::gen9:
+ case cldnn::gpu_arch::gen11:
+ case cldnn::gpu_arch::xe_lp:
+ case cldnn::gpu_arch::xe_hp:
+ case cldnn::gpu_arch::xe_hpg:
+ return 8;
+ case cldnn::gpu_arch::xe2:
+ case cldnn::gpu_arch::xe3:
+ default:
+ return 16;
+ }
+}
+
+// The 256-register mode exists from Xe-HP on. Earlier parts run with 128.
+inline bool supports_large_grf(cldnn::gpu_arch arch) {
+ switch (arch) {
+ case cldnn::gpu_arch::gen9:
+ case cldnn::gpu_arch::gen11:
+ case cldnn::gpu_arch::xe_lp:
+ return false;
+ default:
+ return true;
+ }
+}
+
+inline size_t get_grf_bytes(cldnn::gpu_arch arch) {
+ switch (arch) {
+ case cldnn::gpu_arch::gen9:
+ case cldnn::gpu_arch::gen11:
+ case cldnn::gpu_arch::xe_lp:
+ case cldnn::gpu_arch::xe_hp:
+ case cldnn::gpu_arch::xe_hpg:
+ return 32;
+ default:
+ return 64;
+ }
+}
+
+// Floats per lane in a thread's register file: its size divided by the subgroup
+// width.
+inline size_t get_lane_float_capacity(cldnn::gpu_arch arch) {
+ const size_t grf_regs = supports_large_grf(arch) ? 256 : 128;
+ return grf_regs * get_grf_bytes(arch) / (get_subgroup_size(arch) * sizeof(float));
+}
+
+// d_v-on-lanes keeps k_head_dims floats of state per lane. It needs the large
+// register file and applies while those are at most half of the lane's floats
+// in it; above that the k axis goes on the lanes instead.
+inline bool uses_d_on_lanes(size_t k_head_dims, cldnn::gpu_arch arch) {
+ return !supports_large_grf(arch) || k_head_dims > get_lane_float_capacity(arch) / 2;
+}
+
+// v rows a subgroup covers under d-on-lanes, where the count can be chosen
+// (under d_v-on-lanes it is the subgroup width).
+//
+// The floats a lane needs are estimated as the sum of
+// v_rows * lane_elems for the state,
+// 4 * lane_elems for q and k of this token and the next,
+// 32 a margin, set from spill audits, for everything else.
+// v_rows starts at 8, so that few subgroups read the same q and k rows, and is
+// halved, down to 1, while the estimate reaches the lane's floats in the
+// register file.
+inline size_t get_d_on_lanes_v_rows(size_t k_head_dims, cldnn::gpu_arch arch) {
+ const size_t lane_elems = k_head_dims / get_subgroup_size(arch);
+ const size_t capacity = get_lane_float_capacity(arch);
+ size_t v_rows = 8;
+ while (v_rows > 1 && (v_rows + 4) * lane_elems + 32 >= capacity) {
+ v_rows /= 2;
+ }
+ return v_rows;
+}
+
+// v rows a subgroup covers. Under d_v-on-lanes each lane holds one v row, so
+// this is the subgroup width.
+inline size_t get_v_rows_per_thread(size_t k_head_dims, cldnn::gpu_arch arch) {
+ return uses_d_on_lanes(k_head_dims, arch) ? get_d_on_lanes_v_rows(k_head_dims, arch) : get_subgroup_size(arch);
+}
+
+} // namespace ov::intel_gpu::ocl::paged_gated_delta_net_jit
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_opt.cl
index 1d48cff78b0..147facad08c 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_opt.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_opt.cl
@@ -6,79 +6,351 @@
#include "include/batch_headers/sub_group_block_read.cl"
#include "include/batch_headers/sub_group_block_write.cl"
#include "include/batch_headers/sub_group_shuffle.cl"
+#include "paged_gated_delta_net_type_utils.cl"
+
+// Gated delta rule, S_t = a_t S_{t-1} (I - b_t k_t k_t^T) + b_t v_t k_t^T, o_t = S_t q_t.
+//
+// Terms:
+// - S[d_v][d] is one head's V_HEAD_DIM x K_HEAD_DIM state; d_v indexes the v
+// axis, d the k axis.
+// - A q or k row is query[token][head][:] or key[token][head][:], the
+// K_HEAD_DIM elements of one token and head; a v row is S[d_v][:], the
+// K_HEAD_DIM elements of row d_v of the state.
+// - A tile is the V_BLOCK_SIZE v rows one subgroup holds.
+// - A lane is a work item of the subgroup, numbered by get_sub_group_local_id()
+// from 0 to SUBGROUP_SIZE - 1.
+// - A lane's interleaved share of a row is its elements d = lane,
+// lane + SUBGROUP_SIZE, lane + 2 * SUBGROUP_SIZE, ..., which is what a
+// subgroup block read of the row gives that lane.
+// - The input state is the state read from the subsequence's first block; a
+// snapshot is the state written to its next block after every
+// cache_interval tokens and after its last token.
+//
+// The state is held in registers and distributed over the subgroup in one of
+// two tilings, named after the index of S that runs across the lanes: under
+// d_v-on-lanes lane i holds row i of the tile, whole; under d-on-lanes every
+// lane holds its interleaved share of each row of the tile. The host picks the
+// tiling and V_BLOCK_SIZE; see get_v_rows_per_thread() and get_d_on_lanes_v_rows().
+//
+// Outside PLAIN_TOKEN_LOOP, operands for token t+1 are issued before the state
+// work for token t, so that the loads can overlap the arithmetic. The state
+// update and the output dot product run in one loop rather than two.
#ifndef FUSE_QK_L2NORM
# define FUSE_QK_L2NORM 0
#endif
-
#ifndef Q_L2_NORM_EPS
# define Q_L2_NORM_EPS 1e-6f
#endif
-
#ifndef K_L2_NORM_EPS
# define K_L2_NORM_EPS 1e-6f
#endif
-inline float FUNC(l2norm_scale)(float sum, float extra_scale, float eps) {
- return rsqrt(sum + eps) * extra_scale;
-}
+#if ((K_HEAD_DIM % 16) != 0) || ((V_HEAD_DIM % 16) != 0)
+# error "paged_gated_delta_net_opt requires K_HEAD_DIM and V_HEAD_DIM divisible by 16"
+#endif
+#if ((K_HEAD_DIM % SUBGROUP_SIZE) != 0)
+# error "K_HEAD_DIM must be a multiple of SUBGROUP_SIZE"
+#endif
-inline float FUNC(sum8)(float8 v) {
- return v.s0 + v.s1 + v.s2 + v.s3 + v.s4 + v.s5 + v.s6 + v.s7;
-}
+// Elements of the k axis per lane: the size of an interleaved share.
+#define K_LANE_ELEMS (K_HEAD_DIM / SUBGROUP_SIZE)
+#define K_VEC_COUNT (K_LANE_ELEMS / K_VEC_SIZE)
-inline void FUNC(normalize_kq_128)(float8* b_k, float8* b_q) {
-#if FUSE_QK_L2NORM
- float k_sum = FUNC(sum8)((*b_k) * (*b_k));
- k_sum = sub_group_reduce_add(k_sum);
- const float k_scale = FUNC(l2norm_scale)(k_sum, 1.0f, K_L2_NORM_EPS);
- *b_k *= k_scale;
-
- float q_sum = FUNC(sum8)((*b_q) * (*b_q));
- q_sum = sub_group_reduce_add(q_sum);
- const float q_scale = FUNC(l2norm_scale)(q_sum, SCALE_FACTOR, Q_L2_NORM_EPS);
- *b_q *= q_scale;
+#if ((K_LANE_ELEMS % K_VEC_SIZE) != 0)
+# error "K_VEC_SIZE must divide K_LANE_ELEMS"
+#endif
+#if ((V_HEAD_DIM % V_BLOCK_SIZE) != 0)
+# error "V_BLOCK_SIZE must tile V_HEAD_DIM"
+#endif
+#if ((K_HEAD_DIM % ACC_WAYS) != 0)
+# error "ACC_WAYS must divide K_HEAD_DIM"
+#endif
+
+// --------------------------------------------------------------------------
+// Tiling
+//
+// D_LANES selects the tiling (see the top of the file). d_v-on-lanes keeps
+// K_HEAD_DIM floats of state per lane, d-on-lanes V_BLOCK_SIZE * K_LANE_ELEMS.
+// --------------------------------------------------------------------------
+#if (!D_LANES && (V_BLOCK_SIZE != SUBGROUP_SIZE))
+# error "under d_v-on-lanes each lane holds one v row, so V_BLOCK_SIZE must equal SUBGROUP_SIZE"
+#endif
+
+// PLAIN_TOKEN_LOOP replaces the software pipeline and the prefetch with a plain
+// token loop.
+#if (PLAIN_TOKEN_LOOP && !D_LANES)
+# error "PLAIN_TOKEN_LOOP requires D_LANES"
+#endif
+
+// --------------------------------------------------------------------------
+// The distributed q/k/state read
+//
+// A subgroup block read of N elements per lane returns a vector of N in each
+// lane: component j of lane l's vector is the element at offset
+// j * SUBGROUP_SIZE + l. Chunk c reads K_VEC_SIZE elements per lane at offset
+// c * K_VEC_SIZE * SUBGROUP_SIZE, so array element e of lane l holds row element
+// e * SUBGROUP_SIZE + l: the interleaved share, in the order the unrolled loops
+// below index it, so no shuffle is needed.
+//
+// Results go into a float[] rather than a vector because a vector's components
+// can only be named .sN; an array indexed by a constant is register-promoted
+// after unrolling.
+// --------------------------------------------------------------------------
+#if (K_VEC_SIZE == 8)
+# define PGDN_SPREAD(dst, f, c) \
+ dst[(c) * 8 + 0] = f.s0; dst[(c) * 8 + 1] = f.s1; \
+ dst[(c) * 8 + 2] = f.s2; dst[(c) * 8 + 3] = f.s3; \
+ dst[(c) * 8 + 4] = f.s4; dst[(c) * 8 + 5] = f.s5; \
+ dst[(c) * 8 + 6] = f.s6; dst[(c) * 8 + 7] = f.s7
+# define PGDN_CHUNK_DECV(KT, v) PGDN_DECV(KT, v, 8)
+#elif (K_VEC_SIZE == 4)
+# define PGDN_SPREAD(dst, f, c) \
+ dst[(c) * 4 + 0] = f.s0; dst[(c) * 4 + 1] = f.s1; \
+ dst[(c) * 4 + 2] = f.s2; dst[(c) * 4 + 3] = f.s3
+# define PGDN_CHUNK_DECV(KT, v) PGDN_DECV(KT, v, 4)
+#elif (K_VEC_SIZE == 2)
+# define PGDN_SPREAD(dst, f, c) \
+ dst[(c) * 2 + 0] = f.s0; dst[(c) * 2 + 1] = f.s1
+# define PGDN_CHUNK_DECV(KT, v) PGDN_DECV(KT, v, 2)
#else
- *b_q *= SCALE_FACTOR;
+# define PGDN_SPREAD(dst, f, c) dst[(c)] = f
+# define PGDN_CHUNK_DECV(KT, v) PGDN_SDEC(KT, v)
#endif
-}
-#ifndef K_VEC_SIZE
-# define K_VEC_SIZE 1
+#define PGDN_READ_CHUNK(TYPE, KT, dst, p, base, c) \
+ do { \
+ const MAKE_VECTOR_TYPE(float, K_VEC_SIZE) _f = PGDN_CHUNK_DECV( \
+ KT, BLOCK_READN(TYPE, K_VEC_SIZE, (p), \
+ (base) + (c) * K_VEC_SIZE * SUBGROUP_SIZE)); \
+ PGDN_SPREAD(dst, _f, c); \
+ } while (0)
+
+#if (K_VEC_COUNT == 1)
+# define PGDN_READ_LANE(TYPE, KT, dst, p, base) \
+ do { \
+ PGDN_READ_CHUNK(TYPE, KT, dst, p, base, 0); \
+ } while (0)
+#elif (K_VEC_COUNT == 2)
+# define PGDN_READ_LANE(TYPE, KT, dst, p, base) \
+ do { \
+ PGDN_READ_CHUNK(TYPE, KT, dst, p, base, 0); \
+ PGDN_READ_CHUNK(TYPE, KT, dst, p, base, 1); \
+ } while (0)
+#else
+# define PGDN_READ_LANE(TYPE, KT, dst, p, base) \
+ do { \
+ unroll_for(int _c = 0; _c < K_VEC_COUNT; ++_c) \
+ PGDN_READ_CHUNK(TYPE, KT, dst, p, base, _c); \
+ } while (0)
#endif
-#if ((K_HEAD_DIM % 16) != 0) || ((V_HEAD_DIM % 16) != 0)
-# error "paged_gated_delta_net_opt requires K_HEAD_DIM and V_HEAD_DIM divisible by 16"
+// The inverse, for the d-on-lanes state: the same chunks, written back with
+// block writes.
+#if (K_VEC_SIZE == 8)
+# define PGDN_GATHER(src, c) \
+ (float8)(src[(c) * 8 + 0], src[(c) * 8 + 1], src[(c) * 8 + 2], \
+ src[(c) * 8 + 3], src[(c) * 8 + 4], src[(c) * 8 + 5], \
+ src[(c) * 8 + 6], src[(c) * 8 + 7])
+# define PGDN_CHUNK_ENCV(KT, f) PGDN_ENCV(KT, f, 8)
+#elif (K_VEC_SIZE == 4)
+# define PGDN_GATHER(src, c) \
+ (float4)(src[(c) * 4 + 0], src[(c) * 4 + 1], src[(c) * 4 + 2], src[(c) * 4 + 3])
+# define PGDN_CHUNK_ENCV(KT, f) PGDN_ENCV(KT, f, 4)
+#elif (K_VEC_SIZE == 2)
+# define PGDN_GATHER(src, c) (float2)(src[(c) * 2 + 0], src[(c) * 2 + 1])
+# define PGDN_CHUNK_ENCV(KT, f) PGDN_ENCV(KT, f, 2)
+#else
+# define PGDN_GATHER(src, c) src[(c)]
+# define PGDN_CHUNK_ENCV(KT, f) PGDN_SENC(KT, f)
#endif
-#define K_LANE_ELEMS (K_HEAD_DIM / SUBGROUP_SIZE)
+#define S_WRITE(p, base, src) \
+ do { \
+ unroll_for(int _c = 0; _c < K_VEC_COUNT; ++_c) \
+ BLOCK_WRITEN(INPUT3_TYPE, K_VEC_SIZE, (p), \
+ (base) + _c * K_VEC_SIZE * SUBGROUP_SIZE, \
+ PGDN_CHUNK_ENCV(S_KT, PGDN_GATHER(src, _c))); \
+ } while (0)
+
+#define Q_READ(dst, p, base) PGDN_READ_LANE(INPUT0_TYPE, Q_KT, dst, p, base)
+#define K_READ(dst, p, base) PGDN_READ_LANE(INPUT1_TYPE, K_KT, dst, p, base)
+#define S_READ(dst, p, base) PGDN_READ_LANE(INPUT3_TYPE, S_KT, dst, p, base)
+
+// One token's operands: q and k, normalized when FUSE_QK_L2NORM is set; v,
+// V_BLOCK_SIZE elements under d-on-lanes and one per lane under d_v-on-lanes;
+// exp(gate); beta.
+#if D_LANES
+# define PGDN_V_READ(dst, p) VLOAD_VB(dst, p)
+#else
+# define PGDN_V_READ(dst, p) dst = PGDN_SDEC(V_KT, BLOCK_READN(INPUT2_TYPE, 1, p, 0))
+#endif
+#define PGDN_READ_TOKEN(q, k, v, g, b) \
+ do { \
+ Q_READ(q, qp, 0); \
+ K_READ(k, kp, 0); \
+ FUNC_CALL(l2_normalize_qk)(q, k, scale_factor); \
+ PGDN_V_READ(v, vp); \
+ g = exp(PGDN_SDEC(G_KT, *gp)); \
+ b = PGDN_SDEC(B_KT, *bp); \
+ } while (0)
+
+// Under d_v-on-lanes the dot products over the k axis run inside each lane, which
+// needs every element of the q and k rows: element d is array element
+// d / SUBGROUP_SIZE of lane d % SUBGROUP_SIZE, so KD broadcasts it from there.
+#define KD(v, d) sub_group_broadcast((v)[(d) / SUBGROUP_SIZE], (d) % SUBGROUP_SIZE)
+
+// With V_BLOCK_SIZE 4 or 8, the V_BLOCK_SIZE contiguous elements are read in one
+// access rather than V_BLOCK_SIZE separate loads. start_iv and the output strides
+// are multiples of V_BLOCK_SIZE, and so are the value strides unless the v axis
+// of the value is padded, so the access is naturally aligned.
+#if (V_BLOCK_SIZE == 8)
+# define VLOAD_VB(dst, p) \
+ do { \
+ const float8 _v = PGDN_DECV( \
+ V_KT, \
+ *(const __global MAKE_VECTOR_TYPE(INPUT2_TYPE, 8)*)(p), 8); \
+ dst[0] = _v.s0; dst[1] = _v.s1; dst[2] = _v.s2; dst[3] = _v.s3; \
+ dst[4] = _v.s4; dst[5] = _v.s5; dst[6] = _v.s6; dst[7] = _v.s7; \
+ } while (0)
+# define VSTORE_VB(p, src) \
+ do { \
+ *(__global MAKE_VECTOR_TYPE(OUTPUT_TYPE, 8)*)(p) = \
+ PGDN_ENCV(O_KT, ((float8)(src[0], src[1], src[2], src[3], \
+ src[4], src[5], src[6], src[7])), \
+ 8); \
+ } while (0)
+#elif (V_BLOCK_SIZE == 4)
+# define VLOAD_VB(dst, p) \
+ do { \
+ const float4 _v = PGDN_DECV( \
+ V_KT, \
+ *(const __global MAKE_VECTOR_TYPE(INPUT2_TYPE, 4)*)(p), 4); \
+ dst[0] = _v.x; dst[1] = _v.y; dst[2] = _v.z; dst[3] = _v.w; \
+ } while (0)
+# define VSTORE_VB(p, src) \
+ do { \
+ *(__global MAKE_VECTOR_TYPE(OUTPUT_TYPE, 4)*)(p) = \
+ PGDN_ENCV(O_KT, ((float4)(src[0], src[1], src[2], src[3])), \
+ 4); \
+ } while (0)
+#else
+# define VLOAD_VB(dst, p) \
+ do { \
+ unroll_for(int _p = 0; _p < V_BLOCK_SIZE; ++_p) \
+ dst[_p] = PGDN_SDEC(V_KT, (p)[_p]); \
+ } while (0)
+# define VSTORE_VB(p, src) \
+ do { \
+ unroll_for(int _p = 0; _p < V_BLOCK_SIZE; ++_p) \
+ (p)[_p] = PGDN_SENC(O_KT, src[_p]); \
+ } while (0)
+#endif
-typedef MAKE_VECTOR_TYPE(float, K_VEC_SIZE) K_VEC_TYPE;
-#if (K_VEC_SIZE == 1)
-# define K_VEC_LOAD_Q(ptr, idx) convert_float(BLOCK_READN(INPUT0_TYPE, 1, (ptr), (idx)))
-# define K_VEC_LOAD_K(ptr, idx) convert_float(BLOCK_READN(INPUT1_TYPE, 1, (ptr), (idx)))
-# define K_VEC_LOAD_STATE(ptr, idx) convert_float(BLOCK_READN(INPUT3_TYPE, 1, (ptr), (idx)))
-# define K_VEC_TO_STATE(vec) ((INPUT3_TYPE)(vec))
-# define K_VEC_DOT(a, b) ((a) * (b))
-# define K_VEC_SUM_SQ(a) ((a) * (a))
-#elif (K_VEC_SIZE == 8)
-# define K_VEC_LOAD_Q(ptr, idx) convert_float8(BLOCK_READN(INPUT0_TYPE, 8, (ptr), (idx)))
-# define K_VEC_LOAD_K(ptr, idx) convert_float8(BLOCK_READN(INPUT1_TYPE, 8, (ptr), (idx)))
-# define K_VEC_LOAD_STATE(ptr, idx) convert_float8(BLOCK_READN(INPUT3_TYPE, 8, (ptr), (idx)))
-# define K_VEC_TO_STATE(vec) CAT(convert_, CAT(INPUT3_TYPE, 8))(vec)
-# define K_VEC_DOT(a, b) FUNC(sum8)((a) * (b))
-# define K_VEC_SUM_SQ(a) FUNC(sum8)((a) * (a))
+// Under d_v-on-lanes a lane's state is one contiguous v row of K_HEAD_DIM
+// elements, loaded in 16-element vectors: one access per lane per vector rather
+// than one per element. K_HEAD_DIM is a multiple of 16, so each vector is
+// naturally aligned.
+//
+// NOTE: The state is stored element by element rather than in such vectors.
+// Measured on Panther Lake at (128, 128), that is faster in f32 and on longer
+// f16 subsequences.
+#define S_VEC16 MAKE_VECTOR_TYPE(INPUT3_TYPE, 16)
+#define S_ROW_LOAD(dst, p) \
+ do { \
+ unroll_for(int _c = 0; _c < K_HEAD_DIM / 16; ++_c) { \
+ const float16 _f = \
+ PGDN_DECV(S_KT, ((const __global S_VEC16*)(p))[_c], 16); \
+ dst[_c * 16 + 0] = _f.s0; dst[_c * 16 + 1] = _f.s1; \
+ dst[_c * 16 + 2] = _f.s2; dst[_c * 16 + 3] = _f.s3; \
+ dst[_c * 16 + 4] = _f.s4; dst[_c * 16 + 5] = _f.s5; \
+ dst[_c * 16 + 6] = _f.s6; dst[_c * 16 + 7] = _f.s7; \
+ dst[_c * 16 + 8] = _f.s8; dst[_c * 16 + 9] = _f.s9; \
+ dst[_c * 16 + 10] = _f.sa; dst[_c * 16 + 11] = _f.sb; \
+ dst[_c * 16 + 12] = _f.sc; dst[_c * 16 + 13] = _f.sd; \
+ dst[_c * 16 + 14] = _f.se; dst[_c * 16 + 15] = _f.sf; \
+ } \
+ } while (0)
+
+// Accumulator chains cannot exceed what there is to chain: under d-on-lanes a
+// lane holds K_LANE_ELEMS of the k axis, not K_HEAD_DIM.
+#define WAYS_D (ACC_WAYS < K_LANE_ELEMS ? ACC_WAYS : K_LANE_ELEMS)
+
+inline void FUNC(l2_normalize_qk)(float* qq, float* kk, float scale_factor) {
+#if FUSE_QK_L2NORM
+ float qs = 0.0f, ks = 0.0f;
+ unroll_for(int c = 0; c < K_LANE_ELEMS; ++c) {
+ qs += qq[c] * qq[c];
+ ks += kk[c] * kk[c];
+ }
+ const float q_scale = rsqrt(sub_group_reduce_add(qs) + Q_L2_NORM_EPS) * scale_factor;
+ const float k_scale = rsqrt(sub_group_reduce_add(ks) + K_L2_NORM_EPS);
#else
-# define K_VEC_LOAD_Q(ptr, idx) CAT(convert_float, K_VEC_SIZE)(BLOCK_READN(INPUT0_TYPE, K_VEC_SIZE, (ptr), (idx)))
-# define K_VEC_LOAD_K(ptr, idx) CAT(convert_float, K_VEC_SIZE)(BLOCK_READN(INPUT1_TYPE, K_VEC_SIZE, (ptr), (idx)))
-# define K_VEC_LOAD_STATE(ptr, idx) CAT(convert_float, K_VEC_SIZE)(BLOCK_READN(INPUT3_TYPE, K_VEC_SIZE, (ptr), (idx)))
-# define K_VEC_TO_STATE(vec) CAT(convert_, CAT(INPUT3_TYPE, K_VEC_SIZE))(vec)
-# define K_VEC_DOT(a, b) dot((a), (b))
-# define K_VEC_SUM_SQ(a) dot((a), (a))
+ const float q_scale = scale_factor;
+ const float k_scale = 1.0f;
#endif
+ unroll_for(int c = 0; c < K_LANE_ELEMS; ++c) {
+ qq[c] *= q_scale;
+ kk[c] *= k_scale;
+ }
+}
+
+#if D_LANES
+// One token under d-on-lanes, in two passes over the lane's state elements.
+// Pass 1 takes h_k against the undecayed state and multiplies the result by the
+// decay a = exp(gate): a * (S . k) == (a * S) . k, so the decay reaches the
+// state only in pass 2 and the decayed state is computed once. Pass 2 fuses the
+// state update with the output dot product. Every lane returns the totals in
+// o_val. Under d_v-on-lanes the state is decayed in pass 1 instead.
+//
+// NOTE: Measured on Panther Lake at (256, 256), decaying the state in pass 1
+// here is slower in f16 and f32 and spills in f32.
+inline void FUNC(d_on_lanes_token)(float state[V_BLOCK_SIZE][K_LANE_ELEMS],
+ const float* qv,
+ const float* kv,
+ const float* v_val,
+ float b_g,
+ float b_beta,
+ float* o_val) {
+ float update[V_BLOCK_SIZE];
+ unroll_for(int p = 0; p < V_BLOCK_SIZE; ++p) {
+ float acc[WAYS_D];
+ unroll_for(int a = 0; a < WAYS_D; ++a) acc[a] = state[p][a] * kv[a];
+ unroll_for(int c = WAYS_D; c < K_LANE_ELEMS; ++c) acc[c % WAYS_D] += state[p][c] * kv[c];
+ float part = 0.0f;
+ unroll_for(int a = 0; a < WAYS_D; ++a) part += acc[a];
+ const float h_k = sub_group_reduce_add(part) * b_g;
+ update[p] = (v_val[p] - h_k) * b_beta;
+ }
+ unroll_for(int p = 0; p < V_BLOCK_SIZE; ++p) {
+ float acc[WAYS_D];
+ unroll_for(int a = 0; a < WAYS_D; ++a) {
+ state[p][a] = state[p][a] * b_g + update[p] * kv[a];
+ acc[a] = state[p][a] * qv[a];
+ }
+ unroll_for(int c = WAYS_D; c < K_LANE_ELEMS; ++c) {
+ state[p][c] = state[p][c] * b_g + update[p] * kv[c];
+ acc[c % WAYS_D] += state[p][c] * qv[c];
+ }
+ float part = 0.0f;
+ unroll_for(int a = 0; a < WAYS_D; ++a) part += acc[a];
+ o_val[p] = sub_group_reduce_add(part);
+ }
+}
-#define K_VEC_COUNT (K_LANE_ELEMS / K_VEC_SIZE)
+// Each lane holds its interleaved share of each v row, so a snapshot is
+// written row by row with block writes.
+inline void FUNC(d_on_lanes_snapshot)(__global INPUT3_TYPE* sp, float state[V_BLOCK_SIZE][K_LANE_ELEMS]) {
+ unroll_for(int p = 0; p < V_BLOCK_SIZE; ++p) S_WRITE(sp, p * K_HEAD_DIM, state[p]);
+}
+#endif
+// reqd_work_group_size lets the compiler prove the launch shape and therefore
+// prove gate, beta and the prefetch addresses uniform. The host sets it under
+// d-on-lanes.
+#if OCL_REQD_WG
+__attribute__((reqd_work_group_size(SUBGROUP_SIZE, 1, 1)))
+#endif
REQD_SUB_GROUP_SIZE(SUBGROUP_SIZE)
KERNEL(paged_gated_delta_net_opt)
(__global INPUT0_TYPE* query,
@@ -103,17 +375,25 @@ KERNEL(paged_gated_delta_net_opt)
int k_head_stride,
int v_token_stride,
int v_head_stride) {
- const int seq = get_global_id(0);
- const int h = get_global_id(1);
- const int v_block = get_group_id(2);
- const int lid = get_sub_group_local_id();
- const int start_iv = v_block * V_BLOCK_SIZE;
+ const int v_tile = (int)get_group_id(0);
+ const int h = (int)get_global_id(1);
+ const int seq = (int)get_global_id(2);
+ const int lane = (int)get_sub_group_local_id();
+ const int start_iv = v_tile * V_BLOCK_SIZE;
+
+ const float scale_factor = SCALE_FACTOR;
const int token_begin = subsequence_begins[seq];
const int token_end = subsequence_begins[seq + 1];
const int block_begin = block_indices_begins[seq];
const int past_len = past_lens[seq];
const int interval = cache_interval[seq];
+ // An empty subsequence has nothing to update, and the reads below would go
+ // past its end in query, key, value, gate and beta and take a snapshot block
+ // it need not have. Uniform across the work group.
+ if (token_begin >= token_end) {
+ return;
+ }
const int prev_nums = interval > 0 ? past_len % interval : 0;
const int group_size = V_HEAD_NUM / K_HEAD_NUM;
@@ -123,154 +403,220 @@ KERNEL(paged_gated_delta_net_opt)
const int v_head_base = (h + value_head_offset) * v_head_stride;
const int state_stride = K_HEAD_DIM * V_HEAD_DIM;
- K_VEC_TYPE state[V_BLOCK_SIZE][K_VEC_COUNT];
- K_VEC_TYPE q_norm[K_VEC_COUNT];
- K_VEC_TYPE k_norm[K_VEC_COUNT];
-
const int initial_block_id = block_indices[block_begin];
- const int initial_block_base = (initial_block_id * V_HEAD_NUM + h) * state_stride;
-
-#pragma unroll
- for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
- int curr_iv = start_iv + v_idx;
- int base = initial_block_base + curr_iv * K_HEAD_DIM;
-#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
- state[v_idx][0] = K_VEC_LOAD_STATE(recurrent_state_table, base);
-#else
-# pragma unroll
- for (int kc = 0; kc < K_VEC_COUNT; kc++) {
- const int k_base = kc * K_VEC_SIZE * SUBGROUP_SIZE;
- state[v_idx][kc] = K_VEC_LOAD_STATE(recurrent_state_table, base + k_base);
+ // The first snapshot's block, which a non-empty subsequence always writes,
+ // read ahead so that the snapshot does not wait for it behind the output
+ // stores.
+ int next_block_id = block_indices[block_begin + 1];
+ const long initial_base =
+ (long)(initial_block_id * V_HEAD_NUM + h) * state_stride + (long)start_iv * K_HEAD_DIM;
+
+ // The input state: under d-on-lanes each lane's interleaved share of each
+ // v row, read with block reads; under d_v-on-lanes each lane's whole v row,
+ // loaded in 16-element vectors.
+#if D_LANES
+ float state[V_BLOCK_SIZE][K_LANE_ELEMS];
+ {
+ const __global INPUT3_TYPE* sp = recurrent_state_table + initial_base;
+ unroll_for(int p = 0; p < V_BLOCK_SIZE; ++p) {
+ float row[K_LANE_ELEMS];
+ S_READ(row, sp, p * K_HEAD_DIM);
+ unroll_for(int c = 0; c < K_LANE_ELEMS; ++c) state[p][c] = row[c];
}
-#endif
}
+#else
+ float state[K_HEAD_DIM];
+ {
+ const __global INPUT3_TYPE* sp = recurrent_state_table + initial_base + (long)lane * K_HEAD_DIM;
+ S_ROW_LOAD(state, sp);
+ }
+#endif
int token = token_begin;
int slot = 1;
- int tokens_to_next_boundary = interval > 0 ? (prev_nums > 0 ? (interval - prev_nums) : interval) : (token_end - token_begin);
- while (token < token_end) {
- const int chunk_end = min(token + tokens_to_next_boundary, token_end);
-
- int q_base = token * q_token_stride + q_head_base;
- int k_base = token * k_token_stride + k_head_base;
- int v_base = token * v_token_stride + v_head_base;
- int g_idx = token * V_HEAD_NUM + h;
+ int tokens_to_next_boundary =
+ interval > 0 ? (prev_nums > 0 ? (interval - prev_nums) : interval) : (token_end - token_begin);
+
+ const __global INPUT0_TYPE* qp = query + (long)token * q_token_stride + q_head_base;
+ const __global INPUT1_TYPE* kp = key + (long)token * k_token_stride + k_head_base;
+ const __global INPUT2_TYPE* vp = value + (long)token * v_token_stride + v_head_base + start_iv;
+ const __global INPUT4_TYPE* gp = gate + (long)token * V_HEAD_NUM + h;
+ const __global INPUT5_TYPE* bp = beta + (long)token * V_HEAD_NUM + h;
+ __global OUTPUT_TYPE* op = output + (long)(token * V_HEAD_NUM + h) * V_HEAD_DIM + start_iv;
+
+#if !PLAIN_TOKEN_LOOP
+#if PF_AHEAD > 0
+ // Clamped: the prefetch runs PF_AHEAD tokens ahead, which is past the end
+ // for a subsequence shorter than that.
+ const int pf_first = min(PF_AHEAD, max(token_end - 1 - token, 0));
+ const __global INPUT0_TYPE* pfq = qp + (long)pf_first * q_token_stride;
+ const __global INPUT1_TYPE* pfk = kp + (long)pf_first * k_token_stride;
+ const __global INPUT2_TYPE* pfv = vp + (long)pf_first * v_token_stride;
+#endif
- for (; token < chunk_end; token++, q_base += q_token_stride, k_base += k_token_stride, v_base += v_token_stride, g_idx += V_HEAD_NUM) {
-#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
- q_norm[0] = K_VEC_LOAD_Q(query, q_base);
- k_norm[0] = K_VEC_LOAD_K(key, k_base);
- FUNC(normalize_kq_128)(&k_norm[0], &q_norm[0]);
+ // Prologue of the software pipeline: the first token's operands.
+ float q_pre[K_LANE_ELEMS], k_pre[K_LANE_ELEMS];
+#if D_LANES
+ float v_pre[V_BLOCK_SIZE];
#else
- float q_sum_local = 0.0f;
- float k_sum_local = 0.0f;
-# pragma unroll
- for (int kc = 0; kc < K_VEC_COUNT; kc++) {
- const int offset = kc * K_VEC_SIZE * SUBGROUP_SIZE;
- q_norm[kc] = K_VEC_LOAD_Q(query, q_base + offset);
- k_norm[kc] = K_VEC_LOAD_K(key, k_base + offset);
- q_sum_local += K_VEC_SUM_SQ(q_norm[kc]);
- k_sum_local += K_VEC_SUM_SQ(k_norm[kc]);
- }
-
- float q_scale = SCALE_FACTOR;
- float k_scale = 1.0f;
-# if FUSE_QK_L2NORM
- const float q_sum = sub_group_reduce_add(q_sum_local);
- const float k_sum = sub_group_reduce_add(k_sum_local);
-
- q_scale = FUNC(l2norm_scale)(q_sum, SCALE_FACTOR, Q_L2_NORM_EPS);
- k_scale = FUNC(l2norm_scale)(k_sum, 1.0f, K_L2_NORM_EPS);
-# endif
-# pragma unroll
- for (int kc = 0; kc < K_VEC_COUNT; kc++) {
- q_norm[kc] *= q_scale;
- k_norm[kc] *= k_scale;
- }
+ float v_pre;
+#endif
+ float g_pre, b_pre;
+ PGDN_READ_TOKEN(q_pre, k_pre, v_pre, g_pre, b_pre);
#endif
- const float b_g = exp(convert_float(gate[g_idx]));
- const float b_beta = convert_float(beta[g_idx]);
-
- float b_v_block[V_BLOCK_SIZE];
- float h_k_block[V_BLOCK_SIZE];
- float update_block[V_BLOCK_SIZE];
- float out_block[V_BLOCK_SIZE];
-#pragma unroll
- for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
- const int curr_iv = start_iv + v_idx;
- const int v_base_aligned = v_base + (curr_iv & ~(SUBGROUP_SIZE - 1));
- const int v_lane = curr_iv & (SUBGROUP_SIZE - 1);
- const float v_val = convert_float(BLOCK_READN(INPUT2_TYPE, 1, value, v_base_aligned));
- b_v_block[v_idx] = sub_group_broadcast(v_val, v_lane);
- }
+ while (token < token_end) {
+ const int chunk_end = min(token + tokens_to_next_boundary, token_end);
-#pragma unroll
- for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
- float h_k_local = 0.0f;
-#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
- state[v_idx][0] *= b_g;
- h_k_local = FUNC(sum8)(state[v_idx][0] * k_norm[0]);
+#if PLAIN_TOKEN_LOOP
+ for (; token < chunk_end; ++token) {
+ float qv[K_LANE_ELEMS], kv[K_LANE_ELEMS], v_val[V_BLOCK_SIZE], o_val[V_BLOCK_SIZE], b_g, b_beta;
+ PGDN_READ_TOKEN(qv, kv, v_val, b_g, b_beta);
+ FUNC_CALL(d_on_lanes_token)(state, qv, kv, v_val, b_g, b_beta, o_val);
+ if (lane == 0)
+ VSTORE_VB(op, o_val);
+ qp += q_token_stride;
+ kp += k_token_stride;
+ vp += v_token_stride;
+ gp += V_HEAD_NUM;
+ bp += V_HEAD_NUM;
+ op += V_HEAD_NUM * V_HEAD_DIM;
+ }
#else
-# pragma unroll
- for (int kc = 0; kc < K_VEC_COUNT; kc++) {
- state[v_idx][kc] *= b_g;
- h_k_local += K_VEC_DOT(state[v_idx][kc], k_norm[kc]);
+ // The pipeline stages token+1 and the prefetch reads PF_AHEAD further,
+ // so both would leave the buffer at the end of a subsequence. Rather
+ // than check the bounds in every iteration, the token range is split
+ // into segments and the advance strides are zeroed where advancing
+ // would overrun: segment 0 advances both, segment 1 freezes the
+ // prefetch, segment 2 freezes both. A frozen pointer re-reads the last
+ // valid token and the staged value is discarded by the loop exit, so no
+ // arithmetic changes.
+ //
+ // Unrolled under d_v-on-lanes only, so that each segment there sees its
+ // strides as constants.
+#if !D_LANES
+ __attribute__((opencl_unroll_hint))
+#endif
+ for (int seg = 0; seg < 3; ++seg) {
+ int seg_end;
+ if (seg == 0)
+ seg_end = min(chunk_end, max(token_end - PF_AHEAD - 1, token));
+ else if (seg == 1)
+ seg_end = min(chunk_end, max(token_end - 1, token));
+ else
+ seg_end = chunk_end;
+
+ const int qs = (seg < 2) ? q_token_stride : 0;
+ const int ks = (seg < 2) ? k_token_stride : 0;
+ const int vs = (seg < 2) ? v_token_stride : 0;
+ const int gs = (seg < 2) ? V_HEAD_NUM : 0;
+ const int bs = (seg < 2) ? V_HEAD_NUM : 0;
+ const int pq = (seg < 1) ? q_token_stride : 0;
+ const int pk = (seg < 1) ? k_token_stride : 0;
+ const int pv = (seg < 1) ? v_token_stride : 0;
+
+ for (; token < seg_end; ++token) {
+ float qv[K_LANE_ELEMS], kv[K_LANE_ELEMS];
+ unroll_for(int c = 0; c < K_LANE_ELEMS; ++c) {
+ qv[c] = q_pre[c];
+ kv[c] = k_pre[c];
}
+#if D_LANES
+ float v_val[V_BLOCK_SIZE];
+ unroll_for(int p = 0; p < V_BLOCK_SIZE; ++p) v_val[p] = v_pre[p];
+#else
+ const float v_val = v_pre;
#endif
- h_k_block[v_idx] = sub_group_reduce_add(h_k_local);
- }
-
-#pragma unroll
- for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
- update_block[v_idx] = (b_v_block[v_idx] - h_k_block[v_idx]) * b_beta;
- }
+ const float b_g = g_pre;
+ const float b_beta = b_pre;
+
+ {
+ qp += qs;
+ kp += ks;
+ vp += vs;
+ gp += gs;
+ bp += bs;
+ PGDN_READ_TOKEN(q_pre, k_pre, v_pre, g_pre, b_pre);
+ }
-#pragma unroll
- for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
- float out_val_local = 0.0f;
-#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
- state[v_idx][0] = fma(k_norm[0], update_block[v_idx], state[v_idx][0]);
- out_val_local = FUNC(sum8)(state[v_idx][0] * q_norm[0]);
-#else
-# pragma unroll
- for (int kc = 0; kc < K_VEC_COUNT; kc++) {
- state[v_idx][kc] = fma(k_norm[kc], update_block[v_idx], state[v_idx][kc]);
- out_val_local += K_VEC_DOT(state[v_idx][kc], q_norm[kc]);
+#if PF_AHEAD > 0
+ // PF_BYTES is the size of each prefetch request; it is not meant
+ // to cover the row. The prefetch issues one request each for q,
+ // k and v per token, at the start of each one's row.
+ {
+ pfq += pq;
+ pfk += pk;
+ pfv += pv;
+ prefetch(pfq, PF_BYTES / sizeof(INPUT0_TYPE));
+ prefetch(pfk, PF_BYTES / sizeof(INPUT1_TYPE));
+ prefetch(pfv, PF_BYTES / sizeof(INPUT2_TYPE));
}
#endif
- out_block[v_idx] = sub_group_reduce_add(out_val_local);
- }
-
-#pragma unroll
- for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
- int curr_iv = start_iv + v_idx;
- const float out_val = out_block[v_idx];
- if (lid == 0) {
- const int out_offset = (token * V_HEAD_NUM + h) * V_HEAD_DIM + curr_iv;
- output[out_offset] = TO_OUTPUT_TYPE(out_val);
+#if D_LANES
+ float o_val[V_BLOCK_SIZE];
+ FUNC_CALL(d_on_lanes_token)(state, qv, kv, v_val, b_g, b_beta, o_val);
+ // Every lane holds the totals, so lane 0 alone writes them, with
+ // an ordinary store: a block write has to be executed by the whole
+ // subgroup and cannot go under this if.
+ if (lane == 0)
+ VSTORE_VB(op, o_val);
+#else
+ // Pass 1 decays the state and takes h_k against the decayed state;
+ // pass 2 adds the update and takes the output dot product. Both
+ // dot products over the k axis run inside the lane. In
+ // d_on_lanes_token() the state is decayed in pass 2 instead.
+ //
+ // NOTE: Measured on Panther Lake at (128, 128), decaying the state
+ // in pass 1 here is faster in f32 on long subsequences.
+ float hk_acc[ACC_WAYS];
+ unroll_for(int a = 0; a < ACC_WAYS; ++a) {
+ state[a] *= b_g;
+ hk_acc[a] = state[a] * KD(kv, a);
+ }
+ unroll_for(int d = ACC_WAYS; d < K_HEAD_DIM; ++d) {
+ state[d] *= b_g;
+ hk_acc[d % ACC_WAYS] += state[d] * KD(kv, d);
+ }
+ float h_k = 0.0f;
+ unroll_for(int a = 0; a < ACC_WAYS; ++a) h_k += hk_acc[a];
+ const float update = (v_val - h_k) * b_beta;
+
+ float out_acc[ACC_WAYS];
+ unroll_for(int a = 0; a < ACC_WAYS; ++a) {
+ state[a] = fma(KD(kv, a), update, state[a]);
+ out_acc[a] = state[a] * KD(qv, a);
+ }
+ unroll_for(int d = ACC_WAYS; d < K_HEAD_DIM; ++d) {
+ state[d] = fma(KD(kv, d), update, state[d]);
+ out_acc[d % ACC_WAYS] += state[d] * KD(qv, d);
}
+ float o = 0.0f;
+ unroll_for(int a = 0; a < ACC_WAYS; ++a) o += out_acc[a];
+
+ BLOCK_WRITEN(OUTPUT_TYPE, 1, op, 0, PGDN_SENC(O_KT, o));
+#endif
+ // The output is a contiguous [token][v_head][v] tensor with no
+ // padding, so op advances by V_HEAD_NUM * V_HEAD_DIM per token,
+ // not by v_token_stride: the value input may be a padded view of
+ // a fused projection, whose token stride is larger.
+ op += V_HEAD_NUM * V_HEAD_DIM;
}
}
+#endif
- const int block_id = block_indices[block_begin + slot];
+ const int block_id = next_block_id;
slot++;
- const int block_base = (block_id * V_HEAD_NUM + h) * state_stride;
-#pragma unroll
- for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
- int curr_iv = start_iv + v_idx;
- int base = block_base + curr_iv * K_HEAD_DIM;
-#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
- BLOCK_WRITEN(INPUT3_TYPE, K_VEC_SIZE, recurrent_state_table, base, K_VEC_TO_STATE(state[v_idx][0]));
+ if (token < token_end)
+ next_block_id = block_indices[block_begin + slot];
+ const long block_base =
+ (long)(block_id * V_HEAD_NUM + h) * state_stride + (long)start_iv * K_HEAD_DIM;
+#if D_LANES
+ FUNC_CALL(d_on_lanes_snapshot)(recurrent_state_table + block_base, state);
#else
-# pragma unroll
- for (int kc = 0; kc < K_VEC_COUNT; kc++) {
- const int k_base = kc * K_VEC_SIZE * SUBGROUP_SIZE;
- BLOCK_WRITEN(INPUT3_TYPE, K_VEC_SIZE, recurrent_state_table, base + k_base, K_VEC_TO_STATE(state[v_idx][kc]));
- }
+ unroll_for(int d = 0; d < K_HEAD_DIM; ++d)
+ (recurrent_state_table + block_base + (long)lane * K_HEAD_DIM)[d] = PGDN_SENC(S_KT, state[d]);
#endif
- }
if (interval > 0)
tokens_to_next_boundary = interval;
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_short.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_short.cl
new file mode 100644
index 00000000000..9a700043755
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_short.cl
@@ -0,0 +1,281 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+// Gated delta rule, S_t = a_t S_{t-1} (I - b_t k_t k_t^T) + b_t v_t k_t^T, o_t = S_t q_t.
+
+#include "include/batch_headers/common.cl"
+#include "include/batch_headers/sub_group_block_read.cl"
+#include "include/batch_headers/sub_group_block_write.cl"
+#include "include/batch_headers/sub_group_shuffle.cl"
+#include "paged_gated_delta_net_type_utils.cl"
+
+#ifndef FUSE_QK_L2NORM
+# define FUSE_QK_L2NORM 0
+#endif
+
+#ifndef Q_L2_NORM_EPS
+# define Q_L2_NORM_EPS 1e-6f
+#endif
+
+#ifndef K_L2_NORM_EPS
+# define K_L2_NORM_EPS 1e-6f
+#endif
+
+inline float FUNC(l2norm_scale)(float sum, float extra_scale, float eps) {
+ return rsqrt(sum + eps) * extra_scale;
+}
+
+inline float FUNC(sum8)(float8 v) {
+ return v.s0 + v.s1 + v.s2 + v.s3 + v.s4 + v.s5 + v.s6 + v.s7;
+}
+
+inline void FUNC(normalize_kq_128)(float8* b_k, float8* b_q) {
+#if FUSE_QK_L2NORM
+ float k_sum = FUNC(sum8)((*b_k) * (*b_k));
+ k_sum = sub_group_reduce_add(k_sum);
+ const float k_scale = FUNC(l2norm_scale)(k_sum, 1.0f, K_L2_NORM_EPS);
+ *b_k *= k_scale;
+
+ float q_sum = FUNC(sum8)((*b_q) * (*b_q));
+ q_sum = sub_group_reduce_add(q_sum);
+ const float q_scale = FUNC(l2norm_scale)(q_sum, SCALE_FACTOR, Q_L2_NORM_EPS);
+ *b_q *= q_scale;
+#else
+ *b_q *= SCALE_FACTOR;
+#endif
+}
+
+#ifndef K_VEC_SIZE
+# define K_VEC_SIZE 1
+#endif
+
+#if ((K_HEAD_DIM % 16) != 0) || ((V_HEAD_DIM % 16) != 0)
+# error "paged_gated_delta_net_short requires K_HEAD_DIM and V_HEAD_DIM divisible by 16"
+#endif
+
+#define K_LANE_ELEMS (K_HEAD_DIM / SUBGROUP_SIZE)
+
+typedef MAKE_VECTOR_TYPE(float, K_VEC_SIZE) K_VEC_TYPE;
+#if (K_VEC_SIZE == 1)
+# define K_VEC_LOAD_Q(ptr, idx) PGDN_SDEC(Q_KT, BLOCK_READN(INPUT0_TYPE, 1, (ptr), (idx)))
+# define K_VEC_LOAD_K(ptr, idx) PGDN_SDEC(K_KT, BLOCK_READN(INPUT1_TYPE, 1, (ptr), (idx)))
+# define K_VEC_LOAD_STATE(ptr, idx) PGDN_SDEC(S_KT, BLOCK_READN(INPUT3_TYPE, 1, (ptr), (idx)))
+# define K_VEC_TO_STATE(vec) PGDN_SENC(S_KT, vec)
+# define K_VEC_DOT(a, b) ((a) * (b))
+# define K_VEC_SUM_SQ(a) ((a) * (a))
+#elif (K_VEC_SIZE == 8)
+# define K_VEC_LOAD_Q(ptr, idx) PGDN_DECV(Q_KT, BLOCK_READN(INPUT0_TYPE, 8, (ptr), (idx)), 8)
+# define K_VEC_LOAD_K(ptr, idx) PGDN_DECV(K_KT, BLOCK_READN(INPUT1_TYPE, 8, (ptr), (idx)), 8)
+# define K_VEC_LOAD_STATE(ptr, idx) PGDN_DECV(S_KT, BLOCK_READN(INPUT3_TYPE, 8, (ptr), (idx)), 8)
+# define K_VEC_TO_STATE(vec) PGDN_ENCV(S_KT, vec, 8)
+# define K_VEC_DOT(a, b) FUNC(sum8)((a) * (b))
+# define K_VEC_SUM_SQ(a) FUNC(sum8)((a) * (a))
+#else
+# define K_VEC_LOAD_Q(ptr, idx) PGDN_DECV(Q_KT, BLOCK_READN(INPUT0_TYPE, K_VEC_SIZE, (ptr), (idx)), K_VEC_SIZE)
+# define K_VEC_LOAD_K(ptr, idx) PGDN_DECV(K_KT, BLOCK_READN(INPUT1_TYPE, K_VEC_SIZE, (ptr), (idx)), K_VEC_SIZE)
+# define K_VEC_LOAD_STATE(ptr, idx) PGDN_DECV(S_KT, BLOCK_READN(INPUT3_TYPE, K_VEC_SIZE, (ptr), (idx)), K_VEC_SIZE)
+# define K_VEC_TO_STATE(vec) PGDN_ENCV(S_KT, vec, K_VEC_SIZE)
+# define K_VEC_DOT(a, b) dot((a), (b))
+# define K_VEC_SUM_SQ(a) dot((a), (a))
+#endif
+
+#define K_VEC_COUNT (K_LANE_ELEMS / K_VEC_SIZE)
+
+REQD_SUB_GROUP_SIZE(SUBGROUP_SIZE)
+KERNEL(paged_gated_delta_net_short)
+(__global INPUT0_TYPE* query,
+ __global INPUT1_TYPE* key,
+ __global INPUT2_TYPE* value,
+ __global INPUT3_TYPE* recurrent_state_table,
+ __global INPUT4_TYPE* gate,
+ __global INPUT5_TYPE* beta,
+ __global INPUT6_TYPE* subsequence_begins,
+ __global INPUT7_TYPE* block_indices,
+ __global INPUT8_TYPE* block_indices_begins,
+ __global INPUT9_TYPE* past_lens,
+ __global INPUT10_TYPE* cache_interval,
+ __global OUTPUT_TYPE* output,
+ int num_sequences,
+ int query_head_offset,
+ int key_head_offset,
+ int value_head_offset,
+ int q_token_stride,
+ int q_head_stride,
+ int k_token_stride,
+ int k_head_stride,
+ int v_token_stride,
+ int v_head_stride) {
+ const int seq = get_global_id(0);
+ const int h = get_global_id(1);
+ const int v_block = get_group_id(2);
+ const int lid = get_sub_group_local_id();
+ const int start_iv = v_block * V_BLOCK_SIZE;
+
+ const int token_begin = subsequence_begins[seq];
+ const int token_end = subsequence_begins[seq + 1];
+ const int block_begin = block_indices_begins[seq];
+ const int past_len = past_lens[seq];
+ const int interval = cache_interval[seq];
+ const int prev_nums = interval > 0 ? past_len % interval : 0;
+
+ const int group_size = V_HEAD_NUM / K_HEAD_NUM;
+ const int hk = h / group_size;
+ const int q_head_base = (hk + query_head_offset) * q_head_stride;
+ const int k_head_base = (hk + key_head_offset) * k_head_stride;
+ const int v_head_base = (h + value_head_offset) * v_head_stride;
+ const int state_stride = K_HEAD_DIM * V_HEAD_DIM;
+
+ K_VEC_TYPE state[V_BLOCK_SIZE][K_VEC_COUNT];
+ K_VEC_TYPE q_norm[K_VEC_COUNT];
+ K_VEC_TYPE k_norm[K_VEC_COUNT];
+
+ const int initial_block_id = block_indices[block_begin];
+ const int initial_block_base = (initial_block_id * V_HEAD_NUM + h) * state_stride;
+
+#pragma unroll
+ for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
+ int curr_iv = start_iv + v_idx;
+ int base = initial_block_base + curr_iv * K_HEAD_DIM;
+#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
+ state[v_idx][0] = K_VEC_LOAD_STATE(recurrent_state_table, base);
+#else
+# pragma unroll
+ for (int kc = 0; kc < K_VEC_COUNT; kc++) {
+ const int k_base = kc * K_VEC_SIZE * SUBGROUP_SIZE;
+ state[v_idx][kc] = K_VEC_LOAD_STATE(recurrent_state_table, base + k_base);
+ }
+#endif
+ }
+
+ int token = token_begin;
+ int slot = 1;
+ int tokens_to_next_boundary = interval > 0 ? (prev_nums > 0 ? (interval - prev_nums) : interval) : (token_end - token_begin);
+ while (token < token_end) {
+ const int chunk_end = min(token + tokens_to_next_boundary, token_end);
+
+ int q_base = token * q_token_stride + q_head_base;
+ int k_base = token * k_token_stride + k_head_base;
+ int v_base = token * v_token_stride + v_head_base;
+ int g_idx = token * V_HEAD_NUM + h;
+
+ for (; token < chunk_end; token++, q_base += q_token_stride, k_base += k_token_stride, v_base += v_token_stride, g_idx += V_HEAD_NUM) {
+#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
+ q_norm[0] = K_VEC_LOAD_Q(query, q_base);
+ k_norm[0] = K_VEC_LOAD_K(key, k_base);
+ FUNC(normalize_kq_128)(&k_norm[0], &q_norm[0]);
+#else
+ float q_sum_local = 0.0f;
+ float k_sum_local = 0.0f;
+# pragma unroll
+ for (int kc = 0; kc < K_VEC_COUNT; kc++) {
+ const int offset = kc * K_VEC_SIZE * SUBGROUP_SIZE;
+ q_norm[kc] = K_VEC_LOAD_Q(query, q_base + offset);
+ k_norm[kc] = K_VEC_LOAD_K(key, k_base + offset);
+ q_sum_local += K_VEC_SUM_SQ(q_norm[kc]);
+ k_sum_local += K_VEC_SUM_SQ(k_norm[kc]);
+ }
+
+ float q_scale = SCALE_FACTOR;
+ float k_scale = 1.0f;
+# if FUSE_QK_L2NORM
+ const float q_sum = sub_group_reduce_add(q_sum_local);
+ const float k_sum = sub_group_reduce_add(k_sum_local);
+
+ q_scale = FUNC(l2norm_scale)(q_sum, SCALE_FACTOR, Q_L2_NORM_EPS);
+ k_scale = FUNC(l2norm_scale)(k_sum, 1.0f, K_L2_NORM_EPS);
+# endif
+# pragma unroll
+ for (int kc = 0; kc < K_VEC_COUNT; kc++) {
+ q_norm[kc] *= q_scale;
+ k_norm[kc] *= k_scale;
+ }
+#endif
+
+ const float b_g = exp(PGDN_SDEC(G_KT, gate[g_idx]));
+ const float b_beta = PGDN_SDEC(B_KT, beta[g_idx]);
+
+ float b_v_block[V_BLOCK_SIZE];
+ float h_k_block[V_BLOCK_SIZE];
+ float update_block[V_BLOCK_SIZE];
+ float out_block[V_BLOCK_SIZE];
+#pragma unroll
+ for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
+ const int curr_iv = start_iv + v_idx;
+ const int v_base_aligned = v_base + (curr_iv & ~(SUBGROUP_SIZE - 1));
+ const int v_lane = curr_iv & (SUBGROUP_SIZE - 1);
+ const float v_val = PGDN_SDEC(V_KT, BLOCK_READN(INPUT2_TYPE, 1, value, v_base_aligned));
+ b_v_block[v_idx] = sub_group_broadcast(v_val, v_lane);
+ }
+
+#pragma unroll
+ for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
+ float h_k_local = 0.0f;
+#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
+ state[v_idx][0] *= b_g;
+ h_k_local = FUNC(sum8)(state[v_idx][0] * k_norm[0]);
+#else
+# pragma unroll
+ for (int kc = 0; kc < K_VEC_COUNT; kc++) {
+ state[v_idx][kc] *= b_g;
+ h_k_local += K_VEC_DOT(state[v_idx][kc], k_norm[kc]);
+ }
+#endif
+ h_k_block[v_idx] = sub_group_reduce_add(h_k_local);
+ }
+
+#pragma unroll
+ for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
+ update_block[v_idx] = (b_v_block[v_idx] - h_k_block[v_idx]) * b_beta;
+ }
+
+#pragma unroll
+ for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
+ float out_val_local = 0.0f;
+#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
+ state[v_idx][0] = fma(k_norm[0], update_block[v_idx], state[v_idx][0]);
+ out_val_local = FUNC(sum8)(state[v_idx][0] * q_norm[0]);
+#else
+# pragma unroll
+ for (int kc = 0; kc < K_VEC_COUNT; kc++) {
+ state[v_idx][kc] = fma(k_norm[kc], update_block[v_idx], state[v_idx][kc]);
+ out_val_local += K_VEC_DOT(state[v_idx][kc], q_norm[kc]);
+ }
+#endif
+ out_block[v_idx] = sub_group_reduce_add(out_val_local);
+ }
+
+#pragma unroll
+ for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
+ int curr_iv = start_iv + v_idx;
+ const float out_val = out_block[v_idx];
+
+ if (lid == 0) {
+ const int out_offset = (token * V_HEAD_NUM + h) * V_HEAD_DIM + curr_iv;
+ output[out_offset] = PGDN_SENC(O_KT, out_val);
+ }
+ }
+ }
+
+ const int block_id = block_indices[block_begin + slot];
+ slot++;
+ const int block_base = (block_id * V_HEAD_NUM + h) * state_stride;
+#pragma unroll
+ for (int v_idx = 0; v_idx < V_BLOCK_SIZE; v_idx++) {
+ int curr_iv = start_iv + v_idx;
+ int base = block_base + curr_iv * K_HEAD_DIM;
+#if (K_VEC_SIZE == 8) && (K_VEC_COUNT == 1)
+ BLOCK_WRITEN(INPUT3_TYPE, K_VEC_SIZE, recurrent_state_table, base, K_VEC_TO_STATE(state[v_idx][0]));
+#else
+# pragma unroll
+ for (int kc = 0; kc < K_VEC_COUNT; kc++) {
+ const int k_base = kc * K_VEC_SIZE * SUBGROUP_SIZE;
+ BLOCK_WRITEN(INPUT3_TYPE, K_VEC_SIZE, recurrent_state_table, base + k_base, K_VEC_TO_STATE(state[v_idx][kc]));
+ }
+#endif
+ }
+
+ if (interval > 0)
+ tokens_to_next_boundary = interval;
+ }
+}
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_type_utils.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_type_utils.cl
new file mode 100644
index 00000000000..1ba1d65bb6e
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_gated_delta_net_type_utils.cl
@@ -0,0 +1,37 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "include/batch_headers/bf16_utils.cl"
+
+// bf16 arrives as ushort, so the storage type is selected by (TYPE_SIZE, IS_FP).
+#define PGDN_KT_4_1 F32
+#define PGDN_KT_2_1 F16
+#define PGDN_KT_2_0 BF16
+#define PGDN_KT(size, is_fp) CAT(PGDN_KT_, CAT(size, CAT(_, is_fp)))
+#define Q_KT PGDN_KT(INPUT0_TYPE_SIZE, INPUT0_IS_FP)
+#define K_KT PGDN_KT(INPUT1_TYPE_SIZE, INPUT1_IS_FP)
+#define V_KT PGDN_KT(INPUT2_TYPE_SIZE, INPUT2_IS_FP)
+#define S_KT PGDN_KT(INPUT3_TYPE_SIZE, INPUT3_IS_FP)
+#define G_KT PGDN_KT(INPUT4_TYPE_SIZE, INPUT4_IS_FP)
+#define B_KT PGDN_KT(INPUT5_TYPE_SIZE, INPUT5_IS_FP)
+#define O_KT PGDN_KT(OUTPUT_TYPE_SIZE, OUTPUT_IS_FP)
+
+#define PGDN_DECV_F16(v, n) CAT(convert_float, n)(v)
+#define PGDN_DECV_F32(v, n) (v)
+#define PGDN_DECV_BF16(v, n) CONVERT_AS_BFLOAT16_FLOAT(v, n)
+#define PGDN_DECV(KT, v, n) CAT(PGDN_DECV_, KT)(v, n)
+
+#define PGDN_ENCV_F16(v, n) CAT(convert_half, n)(v)
+#define PGDN_ENCV_F32(v, n) (v)
+#define PGDN_ENCV_BF16(v, n) CONVERT_BFLOAT16_AS_USHORT(v, n)
+#define PGDN_ENCV(KT, v, n) CAT(PGDN_ENCV_, KT)(v, n)
+
+#define PGDN_SDEC_F16(x) convert_float(x)
+#define PGDN_SDEC_F32(x) (x)
+#define PGDN_SDEC_BF16(x) _convert_as_bfloat16_float(x)
+#define PGDN_SENC_F16(f) convert_half(f)
+#define PGDN_SENC_F32(f) (f)
+#define PGDN_SENC_BF16(f) _convert_bfloat16_as_ushort(f)
+#define PGDN_SDEC(KT, x) CAT(PGDN_SDEC_, KT)(x)
+#define PGDN_SENC(KT, f) CAT(PGDN_SENC_, KT)(f)
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sources.cmake b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sources.cmake
index f8a56adc5fd..61ae07f62ad 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sources.cmake
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sources.cmake
@@ -61,6 +61,7 @@ set(GPU_OCL_V2_IMPL_SOURCES
${CMAKE_CURRENT_LIST_DIR}/paged_causal_conv1d_ref.hpp
${CMAKE_CURRENT_LIST_DIR}/paged_gated_delta_net.cpp
${CMAKE_CURRENT_LIST_DIR}/paged_gated_delta_net.hpp
+ ${CMAKE_CURRENT_LIST_DIR}/paged_gated_delta_net_jit_utils.hpp
${CMAKE_CURRENT_LIST_DIR}/paged_selective_ssm.cpp
${CMAKE_CURRENT_LIST_DIR}/paged_selective_ssm.hpp
${CMAKE_CURRENT_LIST_DIR}/primitive_ocl_base.hpp
@@ -139,6 +140,8 @@ set(GPU_OCL_V2_KERNEL_SOURCES
${CMAKE_CURRENT_LIST_DIR}/paged_causal_conv1d_ref.cl
${CMAKE_CURRENT_LIST_DIR}/paged_gated_delta_net_opt.cl
${CMAKE_CURRENT_LIST_DIR}/paged_gated_delta_net_ref.cl
+ ${CMAKE_CURRENT_LIST_DIR}/paged_gated_delta_net_short.cl
+ ${CMAKE_CURRENT_LIST_DIR}/paged_gated_delta_net_type_utils.cl
${CMAKE_CURRENT_LIST_DIR}/paged_selective_ssm_jit_precompute.cl
${CMAKE_CURRENT_LIST_DIR}/paged_selective_ssm_large_state.cl
${CMAKE_CURRENT_LIST_DIR}/paged_selective_ssm_opt.cl