OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Add device-specific JIT kernels for SelectiveSSM (#37559)
Nesterov Alexanderbe29e31edb76
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.cppindex 9d892f5a9d5..02287d0e1ee 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.cpp@@ -22,6 +22,35 @@ using cldnn::program_node;namespace {+constexpr size_t max_jit_state_size = 512;+// Paged recurrence amortizes wider Xe2 private state; f32 and bf16 retain SLM beyond 256 elements.+constexpr size_t max_xe2_extended_private_state_size = 256;+constexpr size_t max_xe2_f16_private_state_size = 512;+// A separate dA kernel wins only when a long recurrence amortizes its launch and temporary-buffer traffic.+constexpr size_t precompute_da_min_tokens = 3072;+constexpr size_t precompute_da_min_head_dim_groups = 12;+constexpr size_t precompute_da_min_state_size = 128;+constexpr size_t precompute_da_reference_head_dim_groups = 16;+constexpr size_t precompute_da_min_group_tokens = precompute_da_min_tokens * precompute_da_reference_head_dim_groups;++enum PagedSelectiveSSMJitStages {+ PRECOMPUTE_DA = 0,+ PRECOMPUTED_DA_RECURRENCE,+ RECURRENCE,+};++bool has_supported_indexing(const cldnn::layout& layout) {+ return layout.get_partial_shape().is_dynamic() || (layout.data_padding == cldnn::padding() && layout.count() <= std::numeric_limits<uint32_t>::max());+}++bool has_static_rank(const ov::PartialShape& shape, const size_t rank) {+ return shape.rank().is_static() && static_cast<size_t>(shape.rank().get_length()) == rank;+}++bool has_static_value(const ov::Dimension& dimension, const size_t value) {+ return dimension.is_static() && static_cast<size_t>(dimension.get_length()) == value;+}+size_t get_scratch_elements(const std::array<size_t, 4>& dimensions) {size_t elements = 1;for (const size_t dimension : dimensions) {@@ -32,6 +61,193 @@ size_t get_scratch_elements(const std::array<size_t, 4>& dimensions) {return std::max<size_t>(elements, 1);}+bool use_discrete_slm(const RuntimeParams& params) {+ const auto& info = params.get_device_info();+ const size_t state_size = params.get_input_layout(2).get_partial_shape()[2].get_length();+ const auto data_type = params.get_input_layout(3).data_type;+ const bool supports_xe2_extended_private_state =+ info.arch == cldnn::gpu_arch::xe2 &&+ (state_size <= max_xe2_extended_private_state_size || (data_type == cldnn::data_types::f16 && state_size <= max_xe2_f16_private_state_size));+ const bool supports_private_state = selective_ssm_jit::supports_common_discrete_private_state(info, state_size) || supports_xe2_extended_private_state;+ return !supports_private_state;+}++size_t get_precomputed_da_head_dim_groups(const RuntimeParams& params) {+ const auto& info = params.get_device_info();+ if (info.dev_type != cldnn::device_type::discrete_gpu || info.arch < cldnn::gpu_arch::xe2)+ return 0;++ const auto& x_shape = params.get_input_layout(3).get_partial_shape();+ const auto& B_shape = params.get_input_layout(2).get_partial_shape();+ const size_t head_dim = x_shape[2].get_length();+ const size_t state_size = B_shape[2].get_length();+ if (state_size < precompute_da_min_state_size)+ return 0;++ const size_t subgroup_size = selective_ssm_jit::get_subgroup_size(info, selective_ssm_jit::device_kind::discrete);+ const size_t head_dim_block = selective_ssm_jit::get_head_dim_block(head_dim,+ state_size,+ subgroup_size,+ info,+ selective_ssm_jit::device_kind::discrete,+ selective_ssm_jit::paged_private_value_budget);+ return head_dim_block == 0 ? 0 : cldnn::ceil_div(head_dim, head_dim_block);+}++bool supports_precomputed_da(const RuntimeParams& params) {+ return get_precomputed_da_head_dim_groups(params) >= precompute_da_min_head_dim_groups;+}++bool use_precomputed_da(const RuntimeParams& params) {+ const size_t head_dim_groups = get_precomputed_da_head_dim_groups(params);+ if (head_dim_groups < precompute_da_min_head_dim_groups)+ return false;++ if (params.is_dynamic())+ return true;++ const size_t min_tokens = std::max(precompute_da_min_tokens, cldnn::ceil_div(precompute_da_min_group_tokens, head_dim_groups));+ return static_cast<size_t>(params.get_input_layout(1).get_partial_shape()[0].get_length()) >= min_tokens;+}++class PagedSelectiveSSMJitPrecomputeGenerator : public KernelGenerator {+public:+ PagedSelectiveSSMJitPrecomputeGenerator() : KernelGenerator("paged_selective_ssm_jit_precompute") {}++protected:+ [[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {+ auto jit = KernelGenerator::get_jit_constants(params);+ const auto& x_shape = params.get_input_layout(3).get_partial_shape();+ if (!params.is_dynamic())+ jit.make("SSM_TOKEN_COUNT", x_shape[0].get_length());+ jit.make("SSM_NUM_HEADS", x_shape[1].get_length());+ return jit;+ }++ [[nodiscard]] Arguments get_arguments_desc(const RuntimeParams& params) const override {+ Arguments args;+ if (params.is_dynamic())+ args.push_back({ArgumentDescriptor::Types::SHAPE_INFO, 0});+ args.push_back({ArgumentDescriptor::Types::INPUT, 0});+ args.push_back({ArgumentDescriptor::Types::INPUT, 1});+ args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, 0});+ return args;+ }++ [[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override {+ return DispatchDataFunc{[](const RuntimeParams& params, KernelData& kd, ImplRuntimeParams*) {+ const size_t lws = std::min<size_t>(256, params.get_device_info().max_work_group_size);+ if (params.is_dynamic()) {+ kd.params.workGroups.global = {lws, 1, 1};+ kd.params.workGroups.local = {lws, 1, 1};+ return;+ }++ const size_t work_items = params.get_input_layout(1).count();+ kd.params.workGroups.global = {cldnn::ceil_div(work_items, lws) * lws, 1, 1};+ kd.params.workGroups.local = {lws, 1, 1};+ }};+ }+};++template <selective_ssm_jit::device_kind Kind, bool PrecomputeDA>+constexpr const char* get_paged_jit_suffix() {+ if constexpr (Kind == selective_ssm_jit::device_kind::integrated) {+ static_assert(!PrecomputeDA, "Precomputed dA is supported only by the discrete paged JIT kernel");+ return "paged_integrated";+ } else if constexpr (PrecomputeDA) {+ return "paged_discrete_precomputed_da";+ } else {+ return "paged_discrete";+ }+}++template <selective_ssm_jit::device_kind Kind, bool PrecomputeDA = false>+class PagedSelectiveSSMJitGenerator : public KernelGenerator {+public:+ PagedSelectiveSSMJitGenerator() : KernelGenerator("selective_ssm_jit", get_paged_jit_suffix<Kind, PrecomputeDA>()) {}++protected:+ [[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {+ auto jit = KernelGenerator::get_jit_constants(params);+ const auto& x_shape = params.get_input_layout(3).get_partial_shape();+ const auto& B_shape = params.get_input_layout(2).get_partial_shape();+ const size_t state_size = B_shape[2].get_length();+ const size_t head_dim = x_shape[2].get_length();+ const size_t subgroup_size = selective_ssm_jit::get_subgroup_size(params.get_device_info(), Kind);+ const size_t head_dim_block = selective_ssm_jit::get_head_dim_block(head_dim,+ state_size,+ subgroup_size,+ params.get_device_info(),+ Kind,+ selective_ssm_jit::paged_private_value_budget);++ if (!params.is_dynamic())+ jit.make("SSM_TOKEN_COUNT", x_shape[0].get_length());+ jit.make("SSM_NUM_HEADS", x_shape[1].get_length());+ jit.make("SSM_HEAD_DIM", head_dim);+ jit.make("SSM_NUM_GROUPS", B_shape[1].get_length());+ jit.make("SSM_STATE_SIZE", state_size);+ jit.make("SSM_SUBGROUP_SIZE", subgroup_size);+ jit.make("SSM_HEAD_DIM_BLOCK", head_dim_block);+ jit.make("SSM_STATE_ITERATIONS", cldnn::ceil_div(state_size, subgroup_size));+ jit.make("SSM_PAGED", true);+ jit.make("SSM_JIT_PRECOMPUTE_DA", PrecomputeDA);+ jit.make("SSM_JIT_USE_SLM", Kind == selective_ssm_jit::device_kind::discrete && use_discrete_slm(params));+ return jit;+ }++ [[nodiscard]] Arguments get_arguments_desc(const RuntimeParams& params) const override {+ Arguments args;+ if (params.is_dynamic())+ args.push_back({ArgumentDescriptor::Types::SHAPE_INFO, 0});+ for (uint32_t i = 0; i < params.input_layouts.size(); i++)+ args.push_back({ArgumentDescriptor::Types::INPUT, i});+ args.push_back({ArgumentDescriptor::Types::OUTPUT, 0});+ if constexpr (PrecomputeDA)+ args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, 0});+ if constexpr (Kind == selective_ssm_jit::device_kind::discrete) {+ if (use_discrete_slm(params))+ args.push_back({ArgumentDescriptor::Types::LOCAL_MEMORY_SIZE, 0});+ }+ return args;+ }++ [[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override {+ return DispatchDataFunc{[](const RuntimeParams& params, KernelData& kd, ImplRuntimeParams*) {+ const auto& x_shape = params.get_input_layout(3).get_partial_shape();+ const auto& B_shape = params.get_input_layout(2).get_partial_shape();+ const size_t num_heads = x_shape[1].get_length();+ const size_t head_dim = x_shape[2].get_length();+ const size_t state_size = B_shape[2].get_length();+ const size_t subgroup_size = selective_ssm_jit::get_subgroup_size(params.get_device_info(), Kind);+ const size_t head_dim_block = selective_ssm_jit::get_head_dim_block(head_dim,+ state_size,+ subgroup_size,+ params.get_device_info(),+ Kind,+ selective_ssm_jit::paged_private_value_budget);++ kd.params.workGroups.local = {subgroup_size, 1, 1};+ kd.params.local_memory_args.clear();+ if constexpr (Kind == selective_ssm_jit::device_kind::discrete) {+ if (use_discrete_slm(params))+ kd.params.local_memory_args = {head_dim_block * state_size * sizeof(float)};+ }+ if (params.is_dynamic()) {+ kd.params.workGroups.global = {subgroup_size, 1, 1};+ return;+ }++ const auto& seq_shape = params.get_input_layout(6).get_partial_shape();+ const size_t sequences = seq_shape[0].get_length() > 0 ? seq_shape[0].get_length() - 1 : 0;+ kd.params.workGroups.global = {std::max<size_t>(cldnn::ceil_div(head_dim, head_dim_block), 1) * subgroup_size,+ std::max<size_t>(num_heads, 1),+ std::max<size_t>(sequences, 1)};+ }};+ }+};+class PagedSelectiveSSMOptGenerator : public KernelGenerator {public:PagedSelectiveSSMOptGenerator() : KernelGenerator("paged_selective_ssm_opt") {}@@ -184,14 +400,160 @@ public:}};+class PagedSelectiveSSMJitIntegratedImpl : public PrimitiveImplOCL {+public:+ DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::PagedSelectiveSSMJitIntegratedImpl)++ Stage::Ptr paged_selective_ssm = make_stage<PagedSelectiveSSMJitGenerator<selective_ssm_jit::device_kind::integrated>>();++ PagedSelectiveSSMJitIntegratedImpl() : PrimitiveImplOCL(PagedSelectiveSSMJitIntegrated::get_type_info_static()) {}+ PagedSelectiveSSMJitIntegratedImpl(const program_node&, const RuntimeParams& params) : PagedSelectiveSSMJitIntegratedImpl() {+ add_stage(paged_selective_ssm, params);+ }++ [[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {+ return make_deep_copy<PagedSelectiveSSMJitIntegratedImpl>(this);+ }+};++class PagedSelectiveSSMJitDiscreteImpl : public PrimitiveImplOCL {+public:+ DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::PagedSelectiveSSMJitDiscreteImpl)++ Stage::Ptr precompute_da = make_stage<PagedSelectiveSSMJitPrecomputeGenerator>();+ Stage::Ptr paged_selective_ssm_precomputed = make_stage<PagedSelectiveSSMJitGenerator<selective_ssm_jit::device_kind::discrete, true>>();+ Stage::Ptr paged_selective_ssm = make_stage<PagedSelectiveSSMJitGenerator<selective_ssm_jit::device_kind::discrete>>();++ PagedSelectiveSSMJitDiscreteImpl() : PrimitiveImplOCL(PagedSelectiveSSMJitDiscrete::get_type_info_static()) {}+ PagedSelectiveSSMJitDiscreteImpl(const program_node&, const RuntimeParams& params) : PagedSelectiveSSMJitDiscreteImpl() {+ const bool precompute = use_precomputed_da(params);+ if (precompute) {+ add_stage(precompute_da, params);+ add_stage(paged_selective_ssm_precomputed, params);+ }+ if ((params.is_dynamic() && supports_precomputed_da(params)) || !precompute)+ add_stage(paged_selective_ssm, params);+ }++ [[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {+ return make_deep_copy<PagedSelectiveSSMJitDiscreteImpl>(this);+ }++ [[nodiscard]] std::vector<BufferDescriptor> get_internal_buffer_descs(const RuntimeParams& params) const override {+ if (!has_stage(precompute_da))+ return {};++ const auto& dt_layout = params.get_input_layout(1);+ const size_t elements =+ use_precomputed_da(params) ? (dt_layout.is_dynamic() ? ov::shape_size(dt_layout.get_partial_shape().get_max_shape()) : dt_layout.count()) : 1;+ return {BufferDescriptor{elements, ov::element::f32}};+ }++ [[nodiscard]] std::vector<size_t> get_stages_execution_order(const RuntimeParams& params) const override {+ if (use_precomputed_da(params))+ return {PRECOMPUTE_DA, PRECOMPUTED_DA_RECURRENCE};++ return {RECURRENCE};+ }+};+} // namespace+bool validate_paged_selective_ssm_jit(const program_node& node, const selective_ssm_jit::device_kind kind) {+ const auto& info = node.get_program().get_engine().get_device_info();+ if (!selective_ssm_jit::matches_device_kind(info, kind))+ return false;++ const size_t subgroup_size = selective_ssm_jit::get_subgroup_size(info, kind);+ if (subgroup_size == 0)+ return false;++ for (size_t i = 0; i < node.get_dependencies().size(); i++) {+ if (!has_supported_indexing(node.get_input_layout(i)))+ return false;+ }+ if (!has_supported_indexing(node.get_output_layout(0)))+ return false;++ const auto& A_shape = node.get_input_layout(0).get_partial_shape();+ const auto& dt_shape = node.get_input_layout(1).get_partial_shape();+ const auto& B_shape = node.get_input_layout(2).get_partial_shape();+ const auto& x_shape = node.get_input_layout(3).get_partial_shape();+ const auto& C_shape = node.get_input_layout(4).get_partial_shape();+ const auto& state_shape = node.get_input_layout(5).get_partial_shape();+ const auto& subsequences_shape = node.get_input_layout(6).get_partial_shape();+ const auto& block_indices_shape = node.get_input_layout(7).get_partial_shape();+ const auto& block_begins_shape = node.get_input_layout(8).get_partial_shape();+ const auto& processed_shape = node.get_input_layout(9).get_partial_shape();+ const auto& interval_shape = node.get_input_layout(10).get_partial_shape();+ const auto& output_shape = node.get_output_layout(0).get_partial_shape();+ if (!has_static_rank(A_shape, 1) || !has_static_rank(dt_shape, 2) || !has_static_rank(B_shape, 3) || !has_static_rank(x_shape, 3) ||+ !has_static_rank(C_shape, 3) || !has_static_rank(state_shape, 4) || !has_static_rank(subsequences_shape, 1) ||+ !has_static_rank(block_indices_shape, 1) || !has_static_rank(block_begins_shape, 1) || !has_static_rank(processed_shape, 1) ||+ !has_static_rank(interval_shape, 1) || !has_static_rank(output_shape, 3)) {+ return false;+ }++ if (!x_shape[1].is_static() || !x_shape[2].is_static() || !B_shape[1].is_static() || !B_shape[2].is_static())+ return false;++ const size_t num_heads = x_shape[1].get_length();+ const size_t head_dim = x_shape[2].get_length();+ const size_t num_groups = B_shape[1].get_length();+ const size_t state_size = B_shape[2].get_length();+ if (num_heads == 0 || num_heads > std::numeric_limits<uint32_t>::max() || num_groups == 0 || head_dim == 0 ||+ head_dim > std::numeric_limits<uint32_t>::max() || state_size < subgroup_size || state_size > max_jit_state_size || num_heads % num_groups != 0) {+ return false;+ }++ const bool shapes_match =+ has_static_value(A_shape[0], num_heads) && has_static_value(dt_shape[1], num_heads) && has_static_value(C_shape[1], num_groups) &&+ has_static_value(C_shape[2], state_size) && has_static_value(state_shape[1], num_heads) && has_static_value(state_shape[2], head_dim) &&+ has_static_value(state_shape[3], state_size) && has_static_value(output_shape[1], num_heads) && has_static_value(output_shape[2], head_dim) &&+ dt_shape[0].compatible(x_shape[0]) && B_shape[0].compatible(x_shape[0]) && C_shape[0].compatible(x_shape[0]) && output_shape[0].compatible(x_shape[0]);+ if (!shapes_match)+ return false;++ if (x_shape[0].is_static() && x_shape[0].get_length() == 0)+ return false;+ if (state_shape[0].is_static() && state_shape[0].get_length() == 0)+ return false;+ if (block_indices_shape[0].is_static() && block_indices_shape[0].get_length() == 0)+ return false;+ if (subsequences_shape[0].is_static()) {+ const size_t subsequences_count = subsequences_shape[0].get_length();+ if (subsequences_count < 2)+ return false;+ const size_t sequences = subsequences_count - 1;+ if ((block_begins_shape[0].is_static() && static_cast<size_t>(block_begins_shape[0].get_length()) < subsequences_count) ||+ (processed_shape[0].is_static() && static_cast<size_t>(processed_shape[0].get_length()) < sequences) ||+ (interval_shape[0].is_static() && static_cast<size_t>(interval_shape[0].get_length()) < sequences)) {+ return false;+ }+ }++ return shapes_match &&+ selective_ssm_jit::get_head_dim_block(head_dim, state_size, subgroup_size, info, kind, selective_ssm_jit::paged_private_value_budget) != 0;+}+std::unique_ptr<primitive_impl> PagedSelectiveSSMOpt::create_impl(const program_node& node, const RuntimeParams& params) const {assert(node.is_type<paged_selective_ssm>());return std::make_unique<PagedSelectiveSSMOptImpl>(node, params);}+std::unique_ptr<primitive_impl> PagedSelectiveSSMJitIntegrated::create_impl(const program_node& node, const RuntimeParams& params) const {+ assert(node.is_type<paged_selective_ssm>());+ return std::make_unique<PagedSelectiveSSMJitIntegratedImpl>(node, params);+}++std::unique_ptr<primitive_impl> PagedSelectiveSSMJitDiscrete::create_impl(const program_node& node, const RuntimeParams& params) const {+ assert(node.is_type<paged_selective_ssm>());+ return std::make_unique<PagedSelectiveSSMJitDiscreteImpl>(node, params);+}+} // namespace ov::intel_gpu::oclBIND_BINARY_BUFFER_WITH_TYPE(cldnn::paged_selective_ssm)BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::PagedSelectiveSSMOptImpl)+BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::PagedSelectiveSSMJitIntegratedImpl)+BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::PagedSelectiveSSMJitDiscreteImpl)diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.hppindex 89ba4eef41b..2d4a1835e8b 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm.hpp@@ -11,9 +11,12 @@#include "paged_selective_ssm_inst.h"#include "program_node.h"#include "registry/implementation_manager.hpp"+#include "selective_ssm_jit_utils.hpp"namespace ov::intel_gpu::ocl {+bool validate_paged_selective_ssm_jit(const cldnn::program_node& node, selective_ssm_jit::device_kind kind);+struct PagedSelectiveSSMOpt : public cldnn::ImplementationManager {OV_GPU_PRIMITIVE_IMPL("ocl::paged_selective_ssm::opt")explicit PagedSelectiveSSMOpt(cldnn::shape_types shape_type, cldnn::ValidateFunc vf = nullptr)@@ -55,4 +58,27 @@ struct PagedSelectiveSSMOpt : public cldnn::ImplementationManager {}};+struct PagedSelectiveSSMJitIntegrated : public PagedSelectiveSSMOpt {+ OV_GPU_PRIMITIVE_IMPL("ocl::paged_selective_ssm::jit_integrated")+ explicit PagedSelectiveSSMJitIntegrated(cldnn::shape_types shape_type, cldnn::ValidateFunc vf = nullptr)+ : PagedSelectiveSSMOpt(shape_type, std::move(vf)) {}++ [[nodiscard]] std::unique_ptr<cldnn::primitive_impl> create_impl(const cldnn::program_node& node, const RuntimeParams& params) const override;++ [[nodiscard]] bool validate_impl(const cldnn::program_node& node) const override {+ return PagedSelectiveSSMOpt::validate_impl(node) && validate_paged_selective_ssm_jit(node, selective_ssm_jit::device_kind::integrated);+ }+};++struct PagedSelectiveSSMJitDiscrete : public PagedSelectiveSSMOpt {+ OV_GPU_PRIMITIVE_IMPL("ocl::paged_selective_ssm::jit_discrete")+ explicit PagedSelectiveSSMJitDiscrete(cldnn::shape_types shape_type, cldnn::ValidateFunc vf = nullptr) : PagedSelectiveSSMOpt(shape_type, std::move(vf)) {}++ [[nodiscard]] std::unique_ptr<cldnn::primitive_impl> create_impl(const cldnn::program_node& node, const RuntimeParams& params) const override;++ [[nodiscard]] bool validate_impl(const cldnn::program_node& node) const override {+ return PagedSelectiveSSMOpt::validate_impl(node) && validate_paged_selective_ssm_jit(node, selective_ssm_jit::device_kind::discrete);+ }+};+} // namespace ov::intel_gpu::ocldiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_jit_precompute.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_jit_precompute.clnew file mode 100644index 00000000000..0271390fe78--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_jit_precompute.cl@@ -0,0 +1,39 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "include/batch_headers/common.cl"+#include "include/batch_headers/bf16_utils.cl"++#if INPUT0_IS_FP+# define SSM_TO_FLOAT(value) convert_float(value)+#else+# define SSM_TO_FLOAT(value) _convert_as_bfloat16_float(value)+#endif++KERNEL(paged_selective_ssm_jit_precompute)(OPTIONAL_SHAPE_INFO_ARG+ const __global INPUT0_TYPE* A,+ const __global INPUT1_TYPE* dt,+ __global float* precomputed_dA) {+ const size_t index = get_global_id(0);+#if IS_DYNAMIC+ const size_t token_count = INPUT1_BATCH_NUM;+#else+ const size_t token_count = SSM_TOKEN_COUNT;+#endif+ if (index >= token_count * SSM_NUM_HEADS)+ return;++ const size_t token = index / SSM_NUM_HEADS;+ const size_t h = index % SSM_NUM_HEADS;+#if IS_DYNAMIC+ const size_t A_index = GET_DATA_INDEX(INPUT0, h, 0, 0, 0);+ const size_t dt_index = GET_DATA_INDEX(INPUT1, token, h, 0, 0);+#else+ const size_t A_index = h;+ const size_t dt_index = index;+#endif+ precomputed_dA[index] = exp(SSM_TO_FLOAT(A[A_index]) * SSM_TO_FLOAT(dt[dt_index]));+}++#undef SSM_TO_FLOATdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.cppindex d5803056119..65915d856cd 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.cpp@@ -5,6 +5,7 @@#include "selective_ssm.hpp"#include <algorithm>+#include <limits>#include "intel_gpu/primitives/selective_ssm.hpp"#include "primitive_ocl_base.hpp"@@ -20,6 +21,138 @@ using cldnn::selective_ssm;namespace {+constexpr size_t max_jit_state_size = 512;+// Larger Xe2 private state is profitable once recurrence work amortizes its scratch traffic.+constexpr size_t max_xe2_extended_private_state_size = 256;+constexpr size_t xe2_extended_private_min_sequence = 16;+constexpr size_t xe2_extended_bf16_private_min_sequence = 8;+constexpr size_t xe2_short_sequence_head_dim_block = 2;+constexpr size_t xe2_short_sequence_limit = 1;++bool is_plain_static_layout(const cldnn::layout& layout) {+ return layout.get_partial_shape().is_static() && layout.data_padding == cldnn::padding() && layout.count() <= std::numeric_limits<uint32_t>::max();+}++bool has_static_rank(const ov::PartialShape& shape, const size_t rank) {+ return shape.is_static() && shape.rank().is_static() && static_cast<size_t>(shape.rank().get_length()) == rank;+}++size_t get_private_values_budget(const size_t sequence_size, const cldnn::data_types data_type, const cldnn::device_info& info) {+ const bool supports_extended_f32_short_sequence = info.gfx_ver.major > 12 || (info.gfx_ver.major == 12 && info.gfx_ver.minor >= 70);+ const size_t short_sequence_limit = data_type == cldnn::data_types::f32 && supports_extended_f32_short_sequence ? 32 : 8;+ return sequence_size <= short_sequence_limit ? selective_ssm_jit::short_sequence_private_value_budget+ : selective_ssm_jit::long_sequence_private_value_budget;+}++bool use_discrete_slm(const RuntimeParams& params) {+ const auto& info = params.get_device_info();+ const size_t state_size = params.get_input_layout(2).get_partial_shape()[3].get_length();+ const size_t sequence_size = params.get_input_layout(3).get_partial_shape()[1].get_length();+ const auto data_type = params.get_input_layout(3).data_type;+ const size_t extended_private_min_sequence =+ data_type == cldnn::data_types::bf16 ? xe2_extended_bf16_private_min_sequence : xe2_extended_private_min_sequence;+ const bool supports_xe2_extended_private_state =+ info.arch == cldnn::gpu_arch::xe2 && state_size <= max_xe2_extended_private_state_size && sequence_size >= extended_private_min_sequence;+ // Xe HPG does not amortize the f32 private-state scratch cost for very short recurrences.+ const bool prefer_slm_for_short_f32 = info.arch == cldnn::gpu_arch::xe_hpg && data_type == cldnn::data_types::f32 && sequence_size <= 4;+ const bool supports_private_state = selective_ssm_jit::supports_common_discrete_private_state(info, state_size) || supports_xe2_extended_private_state;+ return !supports_private_state || prefer_slm_for_short_f32;+}++size_t get_discrete_head_dim_target(const RuntimeParams& params) {+ const auto& info = params.get_device_info();+ if (info.dev_type != cldnn::device_type::discrete_gpu)+ return selective_ssm_jit::default_discrete_head_dim_block;++ const size_t sequence_size = params.get_input_layout(3).get_partial_shape()[1].get_length();+ const bool use_short_sequence_block = info.arch == cldnn::gpu_arch::xe2 && !use_discrete_slm(params) && sequence_size <= xe2_short_sequence_limit;+ return use_short_sequence_block ? xe2_short_sequence_head_dim_block : selective_ssm_jit::default_discrete_head_dim_block;+}++template <selective_ssm_jit::device_kind Kind>+class SelectiveSSMJitGenerator : public KernelGenerator {+public:+ SelectiveSSMJitGenerator() : KernelGenerator("selective_ssm_jit", Kind == selective_ssm_jit::device_kind::integrated ? "integrated" : "discrete") {}++protected:+ [[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {+ auto jit = KernelGenerator::get_jit_constants(params);+ const auto& x_shape = params.get_input_layout(3).get_partial_shape();+ const auto& B_shape = params.get_input_layout(2).get_partial_shape();+ const size_t state_size = B_shape[3].get_length();+ const size_t head_dim = x_shape[3].get_length();+ const size_t subgroup_size = selective_ssm_jit::get_subgroup_size(params.get_device_info(), Kind);+ const size_t private_values_budget = get_private_values_budget(x_shape[1].get_length(), params.get_input_layout(3).data_type, params.get_device_info());+ const size_t discrete_target = get_discrete_head_dim_target(params);+ const size_t head_dim_block =+ selective_ssm_jit::get_head_dim_block(head_dim, state_size, subgroup_size, params.get_device_info(), Kind, private_values_budget, discrete_target);++ jit.make("SSM_SEQUENCE_SIZE", x_shape[1].get_length());+ jit.make("SSM_NUM_HEADS", x_shape[2].get_length());+ jit.make("SSM_HEAD_DIM", head_dim);+ jit.make("SSM_NUM_GROUPS", B_shape[2].get_length());+ jit.make("SSM_STATE_SIZE", state_size);+ jit.make("SSM_SUBGROUP_SIZE", subgroup_size);+ jit.make("SSM_HEAD_DIM_BLOCK", head_dim_block);+ jit.make("SSM_STATE_ITERATIONS", cldnn::ceil_div(state_size, subgroup_size));+ jit.make("SSM_PAGED", false);+ jit.make("SSM_JIT_PRECOMPUTE_DA", false);+ jit.make("SSM_JIT_USE_SLM", Kind == selective_ssm_jit::device_kind::discrete && use_discrete_slm(params));+ return jit;+ }++ [[nodiscard]] Arguments get_arguments_desc(const RuntimeParams& params) const override {+ OPENVINO_ASSERT(!params.is_dynamic(), "SelectiveSSM JIT kernel requires static shapes");+ Arguments args;+ for (uint32_t i = 0; i < params.input_layouts.size(); i++)+ args.push_back({ArgumentDescriptor::Types::INPUT, i});+ for (uint32_t i = 0; i < params.output_layouts.size(); i++)+ args.push_back({ArgumentDescriptor::Types::OUTPUT, i});+ args.push_back({ArgumentDescriptor::Types::SCALAR, 0});+ if constexpr (Kind == selective_ssm_jit::device_kind::discrete) {+ if (use_discrete_slm(params))+ args.push_back({ArgumentDescriptor::Types::LOCAL_MEMORY_SIZE, 0});+ }+ return args;+ }++ [[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override {+ return DispatchDataFunc{[](const RuntimeParams& params, KernelData& kd, ImplRuntimeParams*) {+ OPENVINO_ASSERT(!params.is_dynamic(), "SelectiveSSM JIT kernel requires static shapes");+ const auto& x_shape = params.get_input_layout(3).get_partial_shape();+ const auto& B_shape = params.get_input_layout(2).get_partial_shape();+ const size_t batch = x_shape[0].get_length();+ const size_t num_heads = x_shape[2].get_length();+ const size_t head_dim = x_shape[3].get_length();+ const size_t state_size = B_shape[3].get_length();+ const size_t subgroup_size = selective_ssm_jit::get_subgroup_size(params.get_device_info(), Kind);+ const size_t private_values_budget =+ get_private_values_budget(x_shape[1].get_length(), params.get_input_layout(3).data_type, params.get_device_info());+ const size_t discrete_target = get_discrete_head_dim_target(params);+ const size_t head_dim_block = selective_ssm_jit::get_head_dim_block(head_dim,+ state_size,+ subgroup_size,+ params.get_device_info(),+ Kind,+ private_values_budget,+ discrete_target);++ kd.params.workGroups.global = {cldnn::ceil_div(head_dim, head_dim_block) * subgroup_size, num_heads, batch};+ kd.params.workGroups.local = {subgroup_size, 1, 1};+ kd.params.scalars.clear();+ cldnn::scalar_desc sequence_desc;+ sequence_desc.t = cldnn::scalar_desc::Types::UINT32;+ sequence_desc.v.u32 = static_cast<uint32_t>(x_shape[1].get_length());+ kd.params.scalars.push_back(sequence_desc);+ kd.params.local_memory_args.clear();+ if constexpr (Kind == selective_ssm_jit::device_kind::discrete) {+ if (use_discrete_slm(params))+ kd.params.local_memory_args = {head_dim_block * state_size * sizeof(float)};+ }+ }};+ }+};+class SelectiveSSMOptGenerator : public KernelGenerator {public:SelectiveSSMOptGenerator() : KernelGenerator("selective_ssm_opt") {}@@ -170,14 +303,107 @@ public:}};+class SelectiveSSMJitIntegratedImpl : public PrimitiveImplOCL {+public:+ DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::SelectiveSSMJitIntegratedImpl)++ Stage::Ptr selective_ssm = make_stage<SelectiveSSMJitGenerator<selective_ssm_jit::device_kind::integrated>>();++ SelectiveSSMJitIntegratedImpl() : PrimitiveImplOCL(SelectiveSSMJitIntegrated::get_type_info_static()) {}+ SelectiveSSMJitIntegratedImpl(const program_node&, const RuntimeParams& params) : SelectiveSSMJitIntegratedImpl() {+ add_stage(selective_ssm, params);+ }++ [[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {+ return make_deep_copy<SelectiveSSMJitIntegratedImpl>(this);+ }+};++class SelectiveSSMJitDiscreteImpl : public PrimitiveImplOCL {+public:+ DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::SelectiveSSMJitDiscreteImpl)++ Stage::Ptr selective_ssm = make_stage<SelectiveSSMJitGenerator<selective_ssm_jit::device_kind::discrete>>();++ SelectiveSSMJitDiscreteImpl() : PrimitiveImplOCL(SelectiveSSMJitDiscrete::get_type_info_static()) {}+ SelectiveSSMJitDiscreteImpl(const program_node&, const RuntimeParams& params) : SelectiveSSMJitDiscreteImpl() {+ add_stage(selective_ssm, params);+ }++ [[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {+ return make_deep_copy<SelectiveSSMJitDiscreteImpl>(this);+ }+};+} // namespace+bool validate_selective_ssm_jit(const program_node& node, const selective_ssm_jit::device_kind kind) {+ const auto& info = node.get_program().get_engine().get_device_info();+ if (!selective_ssm_jit::matches_device_kind(info, kind))+ return false;++ const size_t subgroup_size = selective_ssm_jit::get_subgroup_size(info, kind);+ if (subgroup_size == 0)+ return false;++ for (size_t i = 0; i < node.get_dependencies().size(); i++) {+ if (!is_plain_static_layout(node.get_input_layout(i)))+ return false;+ }+ for (size_t i = 0; i < node.get_outputs_count(); i++) {+ if (!is_plain_static_layout(node.get_output_layout(i)))+ return false;+ }++ const auto& A_shape = node.get_input_layout(0).get_partial_shape();+ const auto& dt_shape = node.get_input_layout(1).get_partial_shape();+ const auto& B_shape = node.get_input_layout(2).get_partial_shape();+ const auto& x_shape = node.get_input_layout(3).get_partial_shape();+ const auto& C_shape = node.get_input_layout(4).get_partial_shape();+ const auto& state_shape = node.get_input_layout(5).get_partial_shape();+ const auto& output_shape = node.get_output_layout(0).get_partial_shape();+ const auto& output_state_shape = node.get_output_layout(1).get_partial_shape();+ if (!has_static_rank(A_shape, 1) || !has_static_rank(dt_shape, 3) || !has_static_rank(B_shape, 4) || !has_static_rank(x_shape, 4) ||+ !has_static_rank(C_shape, 4) || !has_static_rank(state_shape, 4) || !has_static_rank(output_shape, 4) || !has_static_rank(output_state_shape, 4)) {+ return false;+ }++ const size_t batch = x_shape[0].get_length();+ const size_t sequence = x_shape[1].get_length();+ const size_t num_heads = x_shape[2].get_length();+ const size_t head_dim = x_shape[3].get_length();+ const size_t num_groups = B_shape[2].get_length();+ const size_t state_size = B_shape[3].get_length();+ if (batch == 0 || sequence == 0 || num_heads == 0 || num_groups == 0 || head_dim == 0 || state_size < subgroup_size || state_size > max_jit_state_size ||+ num_heads % num_groups != 0) {+ return false;+ }++ const bool shapes_match = A_shape[0] == num_heads && dt_shape[0] == batch && dt_shape[1] == sequence && dt_shape[2] == num_heads && B_shape[0] == batch &&+ B_shape[1] == sequence && C_shape == B_shape && state_shape[0] == batch && state_shape[1] == num_heads &&+ state_shape[2] == head_dim && state_shape[3] == state_size && output_shape == x_shape && output_state_shape == state_shape;+ const size_t private_values_budget = get_private_values_budget(sequence, node.get_input_layout(3).data_type, info);+ return shapes_match && selective_ssm_jit::get_head_dim_block(head_dim, state_size, subgroup_size, info, kind, private_values_budget) != 0;+}+std::unique_ptr<primitive_impl> SelectiveSSMOpt::create_impl(const program_node& node, const RuntimeParams& params) const {assert(node.is_type<selective_ssm>());return std::make_unique<SelectiveSSMOptImpl>(node, params);}+std::unique_ptr<primitive_impl> SelectiveSSMJitIntegrated::create_impl(const program_node& node, const RuntimeParams& params) const {+ assert(node.is_type<selective_ssm>());+ return std::make_unique<SelectiveSSMJitIntegratedImpl>(node, params);+}++std::unique_ptr<primitive_impl> SelectiveSSMJitDiscrete::create_impl(const program_node& node, const RuntimeParams& params) const {+ assert(node.is_type<selective_ssm>());+ return std::make_unique<SelectiveSSMJitDiscreteImpl>(node, params);+}+} // namespace ov::intel_gpu::oclBIND_BINARY_BUFFER_WITH_TYPE(cldnn::selective_ssm)BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::SelectiveSSMOptImpl)+BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::SelectiveSSMJitIntegratedImpl)+BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::SelectiveSSMJitDiscreteImpl)diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.hppindex 6112c613a8b..2a541c62c9b 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm.hpp@@ -11,9 +11,12 @@#include "program_node.h"#include "registry/implementation_manager.hpp"#include "selective_ssm_inst.h"+#include "selective_ssm_jit_utils.hpp"namespace ov::intel_gpu::ocl {+bool validate_selective_ssm_jit(const cldnn::program_node& node, selective_ssm_jit::device_kind kind);+struct SelectiveSSMOpt : public cldnn::ImplementationManager {OV_GPU_PRIMITIVE_IMPL("ocl::selective_ssm::opt")explicit SelectiveSSMOpt(cldnn::shape_types shape_type, cldnn::ValidateFunc vf = nullptr)@@ -52,4 +55,26 @@ struct SelectiveSSMOpt : public cldnn::ImplementationManager {}};+struct SelectiveSSMJitIntegrated : public SelectiveSSMOpt {+ OV_GPU_PRIMITIVE_IMPL("ocl::selective_ssm::jit_integrated")+ explicit SelectiveSSMJitIntegrated(cldnn::shape_types shape_type, cldnn::ValidateFunc vf = nullptr) : SelectiveSSMOpt(shape_type, std::move(vf)) {}++ [[nodiscard]] std::unique_ptr<cldnn::primitive_impl> create_impl(const cldnn::program_node& node, const RuntimeParams& params) const override;++ [[nodiscard]] bool validate_impl(const cldnn::program_node& node) const override {+ return SelectiveSSMOpt::validate_impl(node) && validate_selective_ssm_jit(node, selective_ssm_jit::device_kind::integrated);+ }+};++struct SelectiveSSMJitDiscrete : public SelectiveSSMOpt {+ OV_GPU_PRIMITIVE_IMPL("ocl::selective_ssm::jit_discrete")+ explicit SelectiveSSMJitDiscrete(cldnn::shape_types shape_type, cldnn::ValidateFunc vf = nullptr) : SelectiveSSMOpt(shape_type, std::move(vf)) {}++ [[nodiscard]] std::unique_ptr<cldnn::primitive_impl> create_impl(const cldnn::program_node& node, const RuntimeParams& params) const override;++ [[nodiscard]] bool validate_impl(const cldnn::program_node& node) const override {+ return SelectiveSSMOpt::validate_impl(node) && validate_selective_ssm_jit(node, selective_ssm_jit::device_kind::discrete);+ }+};+} // namespace ov::intel_gpu::ocldiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_jit.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_jit.clnew file mode 100644index 00000000000..43e27dccb93--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_jit.cl@@ -0,0 +1,357 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "include/batch_headers/common.cl"+#include "include/batch_headers/fetch_data.cl"+#include "include/batch_headers/bf16_utils.cl"++#if SSM_JIT_PRECOMPUTE_DA && !SSM_PAGED+# error "Precomputed dA is supported only by the paged SelectiveSSM JIT kernel"+#endif++#if INPUT0_IS_FP+# define SSM_TO_FLOAT(value) convert_float(value)+#else+# define SSM_TO_FLOAT(value) _convert_as_bfloat16_float(value)+#endif++// Specialize tensor indexing for paged and dense layouts.+#if SSM_PAGED+# if IS_DYNAMIC+# define SSM_RUNTIME_INDEX(value) ((size_t)(value))+# define SSM_TOKEN_TYPE long+# define SSM_COUNTDOWN_TYPE ulong+# define SSM_A_INDEX GET_DATA_INDEX(INPUT0, h, 0, 0, 0)+# define SSM_DT_INDEX(token) GET_DATA_INDEX(INPUT1, token, h, 0, 0)+# define SSM_B_INDEX(token, state_element) GET_DATA_INDEX(INPUT2, token, g, state_element, 0)+# define SSM_C_INDEX(token, state_element) GET_DATA_INDEX(INPUT4, token, g, state_element, 0)+# define SSM_X_INDEX(token, p) GET_DATA_INDEX(INPUT3, token, h, p, 0)+# define SSM_OUTPUT_INDEX(token, p) GET_DATA_INDEX(OUTPUT, token, h, p, 0)+# define SSM_STATE_INDEX(block, p, state_element) GET_DATA_INDEX(INPUT5, block, h, p, state_element)+# define SSM_SUBSEQUENCE_INDEX(index) GET_DATA_INDEX(INPUT6, index, 0, 0, 0)+# define SSM_BLOCK_INDEX(index) GET_DATA_INDEX(INPUT7, index, 0, 0, 0)+# define SSM_BLOCK_BEGIN_INDEX(index) GET_DATA_INDEX(INPUT8, index, 0, 0, 0)+# define SSM_PROCESSED_INDEX(index) GET_DATA_INDEX(INPUT9, index, 0, 0, 0)+# define SSM_INTERVAL_INDEX(index) GET_DATA_INDEX(INPUT10, index, 0, 0, 0)+# else+# define SSM_RUNTIME_INDEX(value) ((uint)(value))+# define SSM_TOKEN_TYPE uint+# define SSM_COUNTDOWN_TYPE uint+# define SSM_A_INDEX h+# define SSM_DT_INDEX(token) (((token) * SSM_NUM_HEADS) + h)+# define SSM_B_INDEX(token, state_element) \+ ((((token) * SSM_NUM_GROUPS + g) * SSM_STATE_SIZE) + (state_element))+# define SSM_C_INDEX(token, state_element) SSM_B_INDEX(token, state_element)+# define SSM_X_INDEX(token, p) (((((token) * SSM_NUM_HEADS + h) * SSM_HEAD_DIM) + (p)))+# define SSM_OUTPUT_INDEX(token, p) SSM_X_INDEX(token, p)+# define SSM_STATE_INDEX(block, p, state_element) \+ ((((((block) * SSM_NUM_HEADS + h) * SSM_HEAD_DIM + (p)) * SSM_STATE_SIZE) + (state_element)))+# define SSM_SUBSEQUENCE_INDEX(index) (index)+# define SSM_BLOCK_INDEX(index) (index)+# define SSM_BLOCK_BEGIN_INDEX(index) (index)+# define SSM_PROCESSED_INDEX(index) (index)+# define SSM_INTERVAL_INDEX(index) (index)+# endif+#else+# define SSM_RUNTIME_INDEX(value) ((uint)(value))+# define SSM_TOKEN_TYPE uint+# define SSM_A_INDEX h+# define SSM_DT_INDEX(token) (((b * SSM_SEQUENCE_SIZE + (token)) * SSM_NUM_HEADS) + h)+# define SSM_B_INDEX(token, state_element) \+ ((((b * SSM_SEQUENCE_SIZE + (token)) * SSM_NUM_GROUPS + g) * SSM_STATE_SIZE) + (state_element))+# define SSM_C_INDEX(token, state_element) SSM_B_INDEX(token, state_element)+# define SSM_X_INDEX(token, p) \+ (((((b * SSM_SEQUENCE_SIZE + (token)) * SSM_NUM_HEADS + h) * SSM_HEAD_DIM) + (p)))+# define SSM_OUTPUT_INDEX(token, p) SSM_X_INDEX(token, p)+# define SSM_STATE_INDEX(batch, p, state_element) \+ ((((((batch) * SSM_NUM_HEADS + h) * SSM_HEAD_DIM + (p)) * SSM_STATE_SIZE) + (state_element)))+#endif++#if SSM_JIT_PRECOMPUTE_DA+# define SSM_PRECOMPUTED_DA_INDEX(token) (((token) * SSM_NUM_HEADS) + h)+#endif++// Select recurrence-state storage at compile time for each device specialization.+#if SSM_JIT_USE_SLM+# define SSM_STATE_AT(p_offset, step, state_element) slm_state[(p_offset) * SSM_STATE_SIZE + (state_element)]+#else+# define SSM_STATE_AT(p_offset, step, state_element) private_state[p_offset][step]+#endif++REQD_SUB_GROUP_SIZE(SSM_SUBGROUP_SIZE)+KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARG+ const __global INPUT0_TYPE* A,+ const __global INPUT1_TYPE* dt,+ const __global INPUT2_TYPE* B,+ const __global INPUT3_TYPE* x,+ const __global INPUT4_TYPE* C,+#if SSM_PAGED+ __global INPUT5_TYPE* state,+ const __global INPUT6_TYPE* subsequence_begins,+ const __global INPUT7_TYPE* block_indices,+ const __global INPUT8_TYPE* block_indices_begins,+ const __global INPUT9_TYPE* num_processed_tokens,+ const __global INPUT10_TYPE* cache_interval,+ __global OUTPUT_TYPE* output+#else+ const __global INPUT5_TYPE* state,+ __global OUTPUT_TYPE* output,+ __global OUTPUT1_TYPE* output_state,+ uint sequence_size+#endif+#if SSM_JIT_PRECOMPUTE_DA+ , const __global float* precomputed_dA+#endif+#if SSM_JIT_USE_SLM+ , __local float* slm_state+#endif+ ) {+ const uint lane = get_sub_group_local_id();++// Resolve paged recurrence and cache metadata before entering the common kernel body.+#if SSM_PAGED+# if IS_DYNAMIC+ const uint h = (uint)get_global_id(1);+ const size_t seq = get_global_id(2);+ const uint p_base = (uint)get_group_id(0) * SSM_HEAD_DIM_BLOCK;+ const size_t tokens = INPUT3_BATCH_NUM;+ const size_t sequences = INPUT6_BATCH_NUM > 0 ? INPUT6_BATCH_NUM - 1 : 0;++ if (INPUT0_BATCH_NUM != SSM_NUM_HEADS ||+ INPUT1_BATCH_NUM != tokens || INPUT1_FEATURE_NUM != SSM_NUM_HEADS ||+ INPUT2_BATCH_NUM != tokens || INPUT2_FEATURE_NUM != SSM_NUM_GROUPS || INPUT2_SIZE_Y != SSM_STATE_SIZE ||+ INPUT3_FEATURE_NUM != SSM_NUM_HEADS || INPUT3_SIZE_Y != SSM_HEAD_DIM ||+ INPUT4_BATCH_NUM != tokens || INPUT4_FEATURE_NUM != SSM_NUM_GROUPS || INPUT4_SIZE_Y != SSM_STATE_SIZE ||+ INPUT5_FEATURE_NUM != SSM_NUM_HEADS || INPUT5_SIZE_Y != SSM_HEAD_DIM || INPUT5_SIZE_X != SSM_STATE_SIZE ||+ OUTPUT_BATCH_NUM != tokens || OUTPUT_FEATURE_NUM != SSM_NUM_HEADS || OUTPUT_SIZE_Y != SSM_HEAD_DIM ||+ INPUT8_BATCH_NUM < sequences + 1 || INPUT9_BATCH_NUM < sequences || INPUT10_BATCH_NUM < sequences)+ return;++ if (seq >= sequences || h >= SSM_NUM_HEADS || p_base >= SSM_HEAD_DIM)+ return;++ const long token_begin = (long)subsequence_begins[SSM_SUBSEQUENCE_INDEX(seq)];+ const long token_end = (long)subsequence_begins[SSM_SUBSEQUENCE_INDEX(seq + 1)];+ const long block_begin = (long)block_indices_begins[SSM_BLOCK_BEGIN_INDEX(seq)];+ const long block_end = (long)block_indices_begins[SSM_BLOCK_BEGIN_INDEX(seq + 1)];+# else+ const uint h = get_global_id(1);+ const uint seq = get_global_id(2);+ const uint p_base = get_group_id(0) * SSM_HEAD_DIM_BLOCK;+ const long token_begin = (long)subsequence_begins[seq];+ const long token_end = (long)subsequence_begins[seq + 1];+ const long block_begin = (long)block_indices_begins[seq];+ const long block_end = (long)block_indices_begins[seq + 1];+# endif++# if IS_DYNAMIC+ if (token_begin < 0 || token_end < token_begin || (ulong)token_end > (ulong)tokens)+# else+ if (token_begin < 0 || token_end < token_begin || (ulong)token_end > (ulong)SSM_TOKEN_COUNT)+# endif+ return;+ if (token_begin == token_end)+ return;++ const SSM_TOKEN_TYPE recurrence_begin = (SSM_TOKEN_TYPE)token_begin;+ const SSM_TOKEN_TYPE recurrence_end = (SSM_TOKEN_TYPE)token_end;++ if (block_begin < 0 || block_end <= block_begin || (ulong)block_end > (ulong)INPUT7_BATCH_NUM) {+ if (lane == 0) {+ for (long token = token_begin; token < token_end; ++token) {+#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ const uint p = p_base + p_offset;+ if (p < SSM_HEAD_DIM)+ output[SSM_OUTPUT_INDEX(SSM_RUNTIME_INDEX(token), p)] = TO_OUTPUT_TYPE(0.0f);+ }+ }+ }+ return;+ }++ const long first_block = (long)block_indices[SSM_BLOCK_INDEX((size_t)block_begin)];+ if (first_block < 0 || (ulong)first_block >= (ulong)INPUT5_BATCH_NUM) {+ if (lane == 0) {+ for (long token = token_begin; token < token_end; ++token) {+#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ const uint p = p_base + p_offset;+ if (p < SSM_HEAD_DIM)+ output[SSM_OUTPUT_INDEX(SSM_RUNTIME_INDEX(token), p)] = TO_OUTPUT_TYPE(0.0f);+ }+ }+ }+ return;+ }++ const uint initial_state_block = (uint)first_block;+ const long processed_raw = (long)num_processed_tokens[SSM_PROCESSED_INDEX(seq)];+ const long interval = (long)cache_interval[SSM_INTERVAL_INDEX(seq)];+ const long processed = max(processed_raw, (long)0);+ const bool cache_enabled = interval > 0;+ const ulong positive_interval = cache_enabled ? (ulong)interval : 1;+ const ulong previous_in_interval = cache_enabled ? (ulong)processed % positive_interval : 0;+# if IS_DYNAMIC+ SSM_COUNTDOWN_TYPE tokens_until_boundary = cache_enabled ? positive_interval - previous_in_interval : 0;+# else+ SSM_COUNTDOWN_TYPE tokens_until_boundary =+ cache_enabled ? (uint)min(positive_interval - previous_in_interval, (ulong)0xffffffffu) : 0;+# endif+ ulong write_slot = 1;+#else+ const uint h = get_global_id(1);+ const uint b = get_global_id(2);+ const uint p_base = get_group_id(0) * SSM_HEAD_DIM_BLOCK;+ const SSM_TOKEN_TYPE recurrence_begin = 0;+ const SSM_TOKEN_TYPE recurrence_end = sequence_size;+ const uint initial_state_block = b;+#endif++ // Load the initial recurrence state into the selected private or SLM storage.+ const uint g = h / (SSM_NUM_HEADS / SSM_NUM_GROUPS);+ const float A_lane = lane == 0 ? SSM_TO_FLOAT(A[SSM_A_INDEX]) : 0.0f;+ const float A_value = sub_group_broadcast(A_lane, 0);+#if !SSM_JIT_USE_SLM+ float private_state[SSM_HEAD_DIM_BLOCK][SSM_STATE_ITERATIONS];+#endif++#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ const uint p = p_base + p_offset;+#pragma unroll+ for (uint step = 0; step < SSM_STATE_ITERATIONS; ++step) {+ const uint state_element = step * SSM_SUBGROUP_SIZE + lane;+ if (p < SSM_HEAD_DIM && state_element < SSM_STATE_SIZE) {+ SSM_STATE_AT(p_offset, step, state_element) =+ SSM_TO_FLOAT(state[SSM_STATE_INDEX(initial_state_block, p, state_element)]);+ }+ }+ }++ // Apply the common SelectiveSSM recurrence for the selected token range.+ for (SSM_TOKEN_TYPE token = recurrence_begin; token < recurrence_end; ++token) {+#if SSM_PAGED && IS_DYNAMIC+ const size_t token_idx = (size_t)token;+#else+ const uint token_idx = (uint)token;+#endif+ const float dt_lane = lane == 0 ? SSM_TO_FLOAT(dt[SSM_DT_INDEX(token_idx)]) : 0.0f;+ const float dt_value = sub_group_broadcast(dt_lane, 0);+#if SSM_JIT_PRECOMPUTE_DA+ const float dA_lane = lane == 0 ? precomputed_dA[SSM_PRECOMPUTED_DA_INDEX(token_idx)] : 0.0f;+#else+ const float dA_lane = lane == 0 ? exp(A_value * dt_value) : 0.0f;+#endif+ const float dA = sub_group_broadcast(dA_lane, 0);+ float input_scales[SSM_HEAD_DIM_BLOCK];+ float partial[SSM_HEAD_DIM_BLOCK];++#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ const uint p = p_base + p_offset;+ const float x_lane = lane == 0 && p < SSM_HEAD_DIM+ ? SSM_TO_FLOAT(x[SSM_X_INDEX(token_idx, p)])+ : 0.0f;+ input_scales[p_offset] = sub_group_broadcast(x_lane, 0) * dt_value;+ partial[p_offset] = 0.0f;+ }++#pragma unroll+ for (uint step = 0; step < SSM_STATE_ITERATIONS; ++step) {+ const uint state_element = step * SSM_SUBGROUP_SIZE + lane;+ if (state_element < SSM_STATE_SIZE) {+ const float b_value = SSM_TO_FLOAT(B[SSM_B_INDEX(token_idx, state_element)]);+ const float c_value = SSM_TO_FLOAT(C[SSM_C_INDEX(token_idx, state_element)]);+#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ if (p_base + p_offset < SSM_HEAD_DIM) {+ const float new_state =+ fma(SSM_STATE_AT(p_offset, step, state_element), dA, input_scales[p_offset] * b_value);+ SSM_STATE_AT(p_offset, step, state_element) = new_state;+ partial[p_offset] = fma(new_state, c_value, partial[p_offset]);+ }+ }+ }+ }++#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ const uint p = p_base + p_offset;+ const float total = sub_group_reduce_add(partial[p_offset]);+ if (lane == 0 && p < SSM_HEAD_DIM)+ output[SSM_OUTPUT_INDEX(token_idx, p)] = TO_OUTPUT_TYPE(total);+ }++ // Persist paged state snapshots at cache boundaries and at sequence completion.+#if SSM_PAGED+ if (cache_enabled) {+ const bool at_boundary = --tokens_until_boundary == 0;+ const bool at_sequence_end = token + 1 == recurrence_end;+ if (at_boundary || at_sequence_end) {+ const ulong block_position = (ulong)block_begin + write_slot++;+ if (block_position < (ulong)block_end && block_position < (ulong)INPUT7_BATCH_NUM) {+ const long block_id = (long)block_indices[SSM_BLOCK_INDEX((size_t)block_position)];+ if (block_id >= 0 && (ulong)block_id < (ulong)INPUT5_BATCH_NUM) {+#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ const uint p = p_base + p_offset;+#pragma unroll+ for (uint step = 0; step < SSM_STATE_ITERATIONS; ++step) {+ const uint state_element = step * SSM_SUBGROUP_SIZE + lane;+ if (p < SSM_HEAD_DIM && state_element < SSM_STATE_SIZE) {+ state[SSM_STATE_INDEX((uint)block_id, p, state_element)] =+ TO_INPUT5_TYPE(SSM_STATE_AT(p_offset, step, state_element));+ }+ }+ }+ }+ }+ if (at_boundary) {+# if IS_DYNAMIC+ tokens_until_boundary = positive_interval;+# else+ tokens_until_boundary = (uint)min(positive_interval, (ulong)0xffffffffu);+# endif+ }+ }+ }+#endif+ }++ // Dense SelectiveSSM returns the final recurrence state through its second output.+#if !SSM_PAGED+#pragma unroll+ for (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {+ const uint p = p_base + p_offset;+#pragma unroll+ for (uint step = 0; step < SSM_STATE_ITERATIONS; ++step) {+ const uint state_element = step * SSM_SUBGROUP_SIZE + lane;+ if (p < SSM_HEAD_DIM && state_element < SSM_STATE_SIZE) {+ output_state[SSM_STATE_INDEX(b, p, state_element)] =+ TO_OUTPUT1_TYPE(SSM_STATE_AT(p_offset, step, state_element));+ }+ }+ }+#endif+}++#undef SSM_TO_FLOAT+#undef SSM_RUNTIME_INDEX+#undef SSM_TOKEN_TYPE+#undef SSM_COUNTDOWN_TYPE+#undef SSM_A_INDEX+#undef SSM_DT_INDEX+#undef SSM_B_INDEX+#undef SSM_C_INDEX+#undef SSM_X_INDEX+#undef SSM_OUTPUT_INDEX+#undef SSM_STATE_INDEX+#undef SSM_SUBSEQUENCE_INDEX+#undef SSM_BLOCK_INDEX+#undef SSM_BLOCK_BEGIN_INDEX+#undef SSM_PROCESSED_INDEX+#undef SSM_INTERVAL_INDEX+#undef SSM_PRECOMPUTED_DA_INDEX+#undef SSM_STATE_ATdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_jit_utils.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_jit_utils.hppnew file mode 100644index 00000000000..284853b1940--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_jit_utils.hpp@@ -0,0 +1,77 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <algorithm>+#include <cstddef>++#include "intel_gpu/runtime/device_info.hpp"+#include "intel_gpu/runtime/utils.hpp"++namespace ov::intel_gpu::ocl::selective_ssm_jit {++enum class device_kind { integrated, discrete };++inline constexpr size_t short_sequence_private_value_budget = 12;+inline constexpr size_t long_sequence_private_value_budget = 24;+inline constexpr size_t paged_private_value_budget = 48;+inline constexpr size_t default_discrete_head_dim_block = 4;+// With block 4, cap the private recurrence state at 32 values on SIMD16 and 16 values on SIMD32.+inline constexpr size_t max_common_discrete_private_state_size = 128;++inline bool supports_simd_size(const cldnn::device_info& info, const size_t simd_size) {+ return std::find(info.supported_simd_sizes.begin(), info.supported_simd_sizes.end(), simd_size) != info.supported_simd_sizes.end();+}++inline size_t get_subgroup_size(const cldnn::device_info& info, const device_kind kind) {+ if ((!info.supports_khr_subgroups && !info.supports_intel_subgroups) || !info.supports_intel_required_subgroup_size)+ return 0;++ const size_t preferred = kind == device_kind::discrete && info.arch >= cldnn::gpu_arch::xe2 ? 32 : 16;+ if (supports_simd_size(info, preferred) && preferred <= info.max_work_group_size)+ return preferred;+ if (supports_simd_size(info, 16) && 16 <= info.max_work_group_size)+ return 16;+ if (supports_simd_size(info, 8) && 8 <= info.max_work_group_size)+ return 8;+ return 0;+}++inline bool supports_common_discrete_private_state(const cldnn::device_info& info, const size_t state_size) {+ const bool has_supported_arch = info.arch == cldnn::gpu_arch::xe_hpg || info.arch >= cldnn::gpu_arch::xe2;+ return has_supported_arch && state_size <= max_common_discrete_private_state_size;+}++inline size_t get_head_dim_block(const size_t head_dim,+ const size_t state_size,+ const size_t subgroup_size,+ const cldnn::device_info& info,+ const device_kind kind,+ const size_t max_private_values,+ const size_t discrete_target = default_discrete_head_dim_block) {+ if (head_dim == 0 || state_size == 0 || subgroup_size == 0)+ return 0;++ const size_t state_iterations = cldnn::ceil_div(state_size, subgroup_size);+ const size_t target = kind == device_kind::discrete ? discrete_target : (info.arch >= cldnn::gpu_arch::xe2 ? 8 : 4);+ size_t block = std::min(head_dim, target);+ if (kind == device_kind::integrated) {+ // Bound the scalarized recurrence state together with the two per-head-dimension temporaries.+ while (block > 1 && block * (state_iterations + 2) > max_private_values)+ --block;+ return block * (state_iterations + 2) <= max_private_values ? block : 0;+ }+ const size_t local_capacity = info.max_local_mem_size / sizeof(float);+ while (block > 1 && block * state_size > local_capacity)+ --block;+ return block * state_size <= local_capacity ? block : 0;+}++inline bool matches_device_kind(const cldnn::device_info& info, const device_kind kind) {+ const auto expected = kind == device_kind::integrated ? cldnn::device_type::integrated_gpu : cldnn::device_type::discrete_gpu;+ return info.dev_type == expected;+}++} // namespace ov::intel_gpu::ocl::selective_ssm_jitdiff --git a/src/plugins/intel_gpu/src/graph/registry/paged_selective_ssm_impls.cpp b/src/plugins/intel_gpu/src/graph/registry/paged_selective_ssm_impls.cppindex 25d0063d2df..6471fce4eb8 100644--- a/src/plugins/intel_gpu/src/graph/registry/paged_selective_ssm_impls.cpp+++ b/src/plugins/intel_gpu/src/graph/registry/paged_selective_ssm_impls.cpp@@ -17,7 +17,9 @@ using namespace cldnn;const std::vector<std::shared_ptr<cldnn::ImplementationManager>>& Registry<paged_selective_ssm>::get_implementations() {static const std::vector<std::shared_ptr<ImplementationManager>> impls = {#if OV_GPU_WITH_OCL- OV_GPU_CREATE_INSTANCE_OCL(ocl::PagedSelectiveSSMOpt, shape_types::any)+ OV_GPU_CREATE_INSTANCE_OCL(ocl::PagedSelectiveSSMJitIntegrated, shape_types::any)+ OV_GPU_CREATE_INSTANCE_OCL(ocl::PagedSelectiveSSMJitDiscrete, shape_types::any)+ OV_GPU_CREATE_INSTANCE_OCL(ocl::PagedSelectiveSSMOpt, shape_types::any)#endif};diff --git a/src/plugins/intel_gpu/src/graph/registry/selective_ssm_impls.cpp b/src/plugins/intel_gpu/src/graph/registry/selective_ssm_impls.cppindex ef1cd39444e..4900a44cb6e 100644--- a/src/plugins/intel_gpu/src/graph/registry/selective_ssm_impls.cpp+++ b/src/plugins/intel_gpu/src/graph/registry/selective_ssm_impls.cpp@@ -17,7 +17,9 @@ using namespace cldnn;const std::vector<std::shared_ptr<cldnn::ImplementationManager>>& Registry<selective_ssm>::get_implementations() {static const std::vector<std::shared_ptr<ImplementationManager>> impls = {#if OV_GPU_WITH_OCL- OV_GPU_CREATE_INSTANCE_OCL(ocl::SelectiveSSMOpt, shape_types::any)+ OV_GPU_CREATE_INSTANCE_OCL(ocl::SelectiveSSMJitIntegrated, shape_types::static_shape)+ OV_GPU_CREATE_INSTANCE_OCL(ocl::SelectiveSSMJitDiscrete, shape_types::static_shape)+ OV_GPU_CREATE_INSTANCE_OCL(ocl::SelectiveSSMOpt, shape_types::any)#endif};diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/paged_selective_ssm.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/paged_selective_ssm.cppindex 18e1ec7d010..e9289be6fd0 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/paged_selective_ssm.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/paged_selective_ssm.cpp@@ -9,6 +9,7 @@#include <intel_gpu/primitives/paged_selective_ssm.hpp>#include <intel_gpu/primitives/reorder.hpp>#include <numeric>+#include <string>#include <vector>#include "paged_selective_ssm_inst.h"@@ -21,6 +22,8 @@ namespace {constexpr int32_t large_state_size = 32 * 1024 + 1;+enum class expected_ssm_impl { any, jit, fallback };+template <typename T>std::vector<T> make_test_values(size_t count, float scale, float shift = 0.f) {std::vector<T> values(count);@@ -48,8 +51,14 @@ struct paged_selective_ssm_test_params {int32_t invalid_metadata = 0;bool accumulation_test = false;float relative_output_tolerance = 5e-5f;+ expected_ssm_impl expected_impl = expected_ssm_impl::any;};+paged_selective_ssm_test_params expect_impl(paged_selective_ssm_test_params params, expected_ssm_impl expected) {+ params.expected_impl = expected;+ return params;+}+struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_selective_ssm_test_params> {template <typename T>static void run_reference(const std::vector<T>& A,@@ -335,6 +344,14 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_seleExecutionConfig config = get_test_default_config(engine);config.set_property(ov::intel_gpu::allow_new_shape_infer(true));auto network = get_network(engine, topo, config, get_test_stream_ptr(), p.caching_test);+ if (p.expected_impl != expected_ssm_impl::any) {+ const auto primitive = network->get_primitive("paged_selective_ssm");+ ASSERT_NE(primitive, nullptr);+ auto* const impl = primitive->get_impl();+ ASSERT_NE(impl, nullptr);+ const bool is_jit = impl->get_kernel_name().find("jit_") != std::string::npos;+ EXPECT_EQ(is_jit, p.expected_impl == expected_ssm_impl::jit) << "selected implementation: " << impl->get_kernel_name();+ }network->set_input_data("A", A_mem);network->set_input_data("dt", dt_mem);network->set_input_data("B", B_mem);@@ -426,7 +443,16 @@ INSTANTIATE_TEST_SUITE_P(paged_selective_ssm_test_params{{3, 2}, {1, 2}, {2, 0}, 4, 2, 8, 8, ov::element::f16, ov::element::i32, false},paged_selective_ssm_test_params{{2, 1}, {0, 3}, {2, 1}, 2, 1, 4, 16, ov::element::f32, ov::element::i64, false},paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 16, ov::element::bf16, ov::element::i32, false},- paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 2, 513, ov::element::f32, ov::element::i32, false},+ expect_impl(paged_selective_ssm_test_params{{8}, {0}, {8}, 64, 1, 64, 128, ov::element::f32, ov::element::i32, false}, expected_ssm_impl::jit),+ paged_selective_ssm_test_params{{8}, {0}, {8}, 64, 1, 64, 128, ov::element::f16, ov::element::i32, false},+ expect_impl(paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 256, ov::element::f32, ov::element::i32, true, {}, false, true},+ expected_ssm_impl::jit),+ paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 256, ov::element::f16, ov::element::i32, false},+ paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 256, ov::element::bf16, ov::element::i32, false},+ paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 512, ov::element::f16, ov::element::i32, false},+ paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 512, ov::element::f32, ov::element::i32, false},+ paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 512, ov::element::bf16, ov::element::i32, false},+ expect_impl(paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 2, 513, ov::element::f32, ov::element::i32, false}, expected_ssm_impl::fallback),paged_selective_ssm_test_params{{2, 3}, {1, 0}, {2, 3}, 4, 2, 4, 16, ov::element::f32, ov::element::i64, true},// Five page-aliasing cases from the published specification.paged_selective_ssm_test_params{{5}, {0}, {2}, 4, 2, 5, 17, ov::element::f32, ov::element::i32, false, {true}, true},@@ -441,9 +467,15 @@ INSTANTIATE_TEST_SUITE_P(paged_selective_ssm_test_params{{0}, {0}, {2}, 2, 1, 2, 8, ov::element::f32, ov::element::i32, true, {true}, false},// Exercise binary serialization of the optimized implementation.paged_selective_ssm_test_params{{3, 2}, {1, 0}, {2, 2}, 4, 2, 4, 16, ov::element::f16, ov::element::i64, true, {true, true}, true, true},- paged_selective_ssm_test_params{{4, 2}, {3, 7}, {2, 3}, 4, 2, 3, 19, ov::element::f32, ov::element::i64, false, {true, false}, true, false, true},+ expect_impl(+ paged_selective_ssm_test_params{{4, 2}, {3, 7}, {2, 3}, 4, 2, 3, 19, ov::element::f32, ov::element::i64, false, {true, false}, true, false, true},+ expected_ssm_impl::fallback),paged_selective_ssm_test_params{{3, 2}, {1, 0}, {2, 2}, 4, 2, 4, 16, ov::element::f16, ov::element::i32, true, {true, true}, true, false, false, 3},paged_selective_ssm_test_params{{32, 17, 5}, {3, 7, 1}, {4, 3, 2}, 8, 4, 16, 64, ov::element::f16, ov::element::i64, true},+ // Exercise serialization of a dynamic-shape configuration eligible for the device-specific JIT kernels.+ paged_selective_ssm_test_params{{32, 17, 5}, {3, 7, 1}, {4, 3, 2}, 8, 4, 16, 64, ov::element::f16, ov::element::i64, true, {}, false, true},+ // Exercise serialization of the long dynamic recurrence that selects Xe2 dA precomputation.+ paged_selective_ssm_test_params{{3072}, {0}, {3072}, 2, 1, 64, 128, ov::element::f16, ov::element::i32, true, {}, false, true},paged_selective_ssm_test_params{{128}, {0}, {32}, 4, 2, 8, 32, ov::element::f16, ov::element::i32, false},paged_selective_ssm_test_params{{128}, {0}, {32}, 4, 2, 8, 32, ov::element::bf16, ov::element::i32, false},paged_selective_ssm_test_params{{128}, {0}, {128}, 1, 1, 1, 1, ov::element::f16, ov::element::i32, false, {}, false, false, false, 1, 0, true},@@ -451,6 +483,10 @@ INSTANTIATE_TEST_SUITE_P(paged_selective_ssm_test_params{{3}, {-7}, {2}, 2, 1, 4, 8, ov::element::f32, ov::element::i32, false},paged_selective_ssm_test_params{{3}, {0}, {2}, 2, 1, 4, 8, ov::element::f32, ov::element::i32, false, {}, false, false, false, 1, 1},paged_selective_ssm_test_params{{3}, {0}, {2}, 2, 1, 4, 8, ov::element::f16, ov::element::i64, false, {}, false, false, false, 1, 2},+ expect_impl(paged_selective_ssm_test_params{{3}, {0}, {2}, 2, 1, 4, 64, ov::element::f32, ov::element::i32, false, {}, false, false, false, 1, 1},+ expected_ssm_impl::jit),+ expect_impl(paged_selective_ssm_test_params{{3}, {0}, {2}, 2, 1, 4, 64, ov::element::f16, ov::element::i64, false, {}, false, false, false, 1, 2},+ expected_ssm_impl::jit),// Exercise local-memory-driven 4 -> 3 -> 2 -> 1 blocking and tails.paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 4, 5000, ov::element::f32, ov::element::i32, false},paged_selective_ssm_test_params{{2}, {0}, {2}, 2, 1, 3, 6000, ov::element::f32, ov::element::i32, false},diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/selective_ssm.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/selective_ssm.cppindex 70e7f04216d..54a515b429c 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/selective_ssm.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/selective_ssm.cpp@@ -7,6 +7,7 @@#include <intel_gpu/primitives/input_layout.hpp>#include <intel_gpu/primitives/reorder.hpp>#include <intel_gpu/primitives/selective_ssm.hpp>+#include <string>#include <vector>#include "selective_ssm_inst.h"@@ -19,6 +20,8 @@ namespace {constexpr int32_t large_state_size = 32 * 1024 + 1;+enum class expected_ssm_impl { any, jit, fallback };+template <typename T>std::vector<T> make_test_values(size_t count, float scale, float shift = 0.f) {std::vector<T> values(count);@@ -41,8 +44,14 @@ struct selective_ssm_test_params {int32_t iterations = 1;bool accumulation_test = false;float relative_output_tolerance = 5e-5f;+ expected_ssm_impl expected_impl = expected_ssm_impl::any;};+selective_ssm_test_params expect_impl(selective_ssm_test_params params, expected_ssm_impl expected) {+ params.expected_impl = expected;+ return params;+}+struct selective_ssm_gpu_test : public ::testing::TestWithParam<selective_ssm_test_params> {template <typename T>static void run_reference(const std::vector<T>& A,@@ -182,6 +191,14 @@ struct selective_ssm_gpu_test : public ::testing::TestWithParam<selective_ssm_teExecutionConfig config = get_test_default_config(engine);config.set_property(ov::intel_gpu::allow_new_shape_infer(true));auto network = get_network(engine, topo, config, get_test_stream_ptr(), p.caching_test);+ if (p.expected_impl != expected_ssm_impl::any) {+ const auto primitive = network->get_primitive("selective_ssm");+ ASSERT_NE(primitive, nullptr);+ auto* const impl = primitive->get_impl();+ ASSERT_NE(impl, nullptr);+ const bool is_jit = impl->get_kernel_name().find("jit_") != std::string::npos;+ EXPECT_EQ(is_jit, p.expected_impl == expected_ssm_impl::jit) << "selected implementation: " << impl->get_kernel_name();+ }network->set_input_data("A", A_mem);network->set_input_data("dt", dt_mem);network->set_input_data("B", B_mem);@@ -250,13 +267,29 @@ INSTANTIATE_TEST_SUITE_P(smoke_selective_ssm_gpu_test,selective_ssm_test_params{2, 3, 4, 1, 8, 16, ov::element::f32, false},selective_ssm_test_params{1, 4, 4, 2, 8, 8, ov::element::f16, false},selective_ssm_test_params{1, 3, 2, 1, 4, 16, ov::element::bf16, false},+ expect_impl(selective_ssm_test_params{1, 1, 64, 1, 64, 128, ov::element::f32, false}, expected_ssm_impl::jit),+ selective_ssm_test_params{1, 1, 64, 1, 64, 128, ov::element::f16, false},+ selective_ssm_test_params{1, 1, 64, 1, 64, 128, ov::element::bf16, false},+ selective_ssm_test_params{1, 2, 64, 1, 64, 128, ov::element::f16, false},+ selective_ssm_test_params{1, 2, 64, 1, 64, 128, ov::element::bf16, false},selective_ssm_test_params{1, 8, 64, 1, 64, 128, ov::element::f32, false},selective_ssm_test_params{1, 8, 64, 1, 64, 128, ov::element::f16, false},- selective_ssm_test_params{1, 2, 2, 1, 2, 513, ov::element::f32, false},- selective_ssm_test_params{2, 3, 4, 2, 4, 16, ov::element::f32, true},+ selective_ssm_test_params{1, 16, 64, 1, 64, 128, ov::element::f16, false},+ selective_ssm_test_params{1, 32, 64, 1, 64, 128, ov::element::f32, false},+ selective_ssm_test_params{1, 64, 64, 1, 64, 128, ov::element::f32, false},+ selective_ssm_test_params{1, 2, 2, 1, 4, 256, ov::element::f32, false},+ selective_ssm_test_params{1, 16, 2, 1, 4, 256, ov::element::f32, false},+ selective_ssm_test_params{1, 16, 2, 1, 4, 256, ov::element::f16, false},+ selective_ssm_test_params{1, 8, 2, 1, 4, 256, ov::element::bf16, false},+ selective_ssm_test_params{1, 2, 2, 1, 4, 512, ov::element::f32, false},+ selective_ssm_test_params{1, 2, 2, 1, 4, 512, ov::element::f16, false},+ selective_ssm_test_params{1, 2, 2, 1, 4, 512, ov::element::bf16, false},+ expect_impl(selective_ssm_test_params{1, 2, 2, 1, 2, 513, ov::element::f32, false}, expected_ssm_impl::fallback),+ expect_impl(selective_ssm_test_params{2, 3, 4, 2, 4, 16, ov::element::f32, true}, expected_ssm_impl::fallback),selective_ssm_test_params{1, 7, 4, 2, 5, 31, ov::element::f16, true},selective_ssm_test_params{1, 5, 4, 2, 8, 17, ov::element::f32, false, true},- selective_ssm_test_params{1, 5, 4, 2, 3, 19, ov::element::f32, false, false, true},+ expect_impl(selective_ssm_test_params{1, 5, 4, 2, 3, 19, ov::element::f32, false, false, true},+ expected_ssm_impl::fallback),selective_ssm_test_params{2, 0, 4, 2, 3, 17, ov::element::f16, true},selective_ssm_test_params{1, 0, 2, 1, 4, 9, ov::element::bf16, false},selective_ssm_test_params{2, 4, 4, 2, 3, 15, ov::element::f32, true, false, false, 3},