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] Support independent PagedSelectiveSSM state precision (#37661)
Nesterov Alexanderd1008a77b344
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.clindex 82fbebfa2b0..dbb8346aca9 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.cl@@ -27,7 +27,28 @@ KERNEL(paged_causal_conv1d_ref)int bias_hidden_stride,int output_token_stride,int output_hidden_stride,- int num_blocks) {+ int num_blocks,+ int input_offset,+ int state_offset,+ int weight_offset,+ int bias_offset,+ int subsequence_offset,+ int block_indices_offset,+ int block_begins_offset,+ int past_lens_offset,+ int cache_interval_offset,+ int output_offset) {+ input_embeds += input_offset;+ conv_state_table += state_offset;+ conv_weight += weight_offset;+ conv_bias += bias_offset;+ subsequence_begins += subsequence_offset;+ block_indices += block_indices_offset;+ block_indices_begins += block_begins_offset;+ past_lens += past_lens_offset;+ cache_interval += cache_interval_offset;+ output_embeds += output_offset;+const int seq = (int)get_global_id(0);const int h = (int)get_global_id(1);diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.cppindex 2eaabf4715e..182d15b66f2 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_causal_conv1d_ref.cpp@@ -4,6 +4,8 @@#include "paged_causal_conv1d_ref.hpp"+#include <limits>+#include "intel_gpu/primitives/paged_causal_conv1d.hpp"#include "primitive_ocl_base.hpp"#include "utils/kernel_generator.hpp"@@ -11,6 +13,35 @@namespace ov::intel_gpu::ocl {namespace {+enum class ScalarArg : size_t {+ sequence_count,+ hidden_size,+ input_token_stride,+ input_hidden_stride,+ state_block_stride,+ state_hidden_stride,+ state_kernel_stride,+ weight_hidden_stride,+ weight_kernel_stride,+ bias_hidden_stride,+ output_token_stride,+ output_hidden_stride,+ state_block_count,+ input_offset,+ state_offset,+ weight_offset,+ bias_offset,+ subsequence_offset,+ block_indices_offset,+ block_begins_offset,+ past_lens_offset,+ cache_interval_offset,+ output_offset,+ count,+};++constexpr size_t scalar_arg_count = static_cast<size_t>(ScalarArg::count);+class PagedCausalConv1DRefGenerator : public KernelGenerator {public:PagedCausalConv1DRefGenerator() : KernelGenerator("paged_causal_conv1d_ref") {}@@ -40,8 +71,7 @@ protected:args.push_back({ArgumentDescriptor::Types::OUTPUT, 0});- constexpr size_t num_scalars = 13; // Must match scalars count in get_dispatch_data_func- for (size_t i = 0; i < num_scalars; i++) {+ for (size_t i = 0; i < scalar_arg_count; i++) {args.push_back({ArgumentDescriptor::Types::SCALAR, static_cast<uint32_t>(i)});}@@ -112,6 +142,16 @@ protected:static_cast<int32_t>(params.get_input_layout(paged_causal_conv1d::CONV_STATE_TABLE).get_partial_shape()[0].get_length()),};+ auto append_offset = [&scalars](const cldnn::layout& layout) {+ const auto offset = layout.get_linear_offset();+ OPENVINO_ASSERT(offset <= static_cast<size_t>(std::numeric_limits<int32_t>::max()), "PagedCausalConv1D layout offset exceeds int32 range");+ scalars.push_back(static_cast<int32_t>(offset));+ };+ for (const auto& input_layout : params.input_layouts)+ append_offset(input_layout);+ append_offset(out_layout);+ OPENVINO_ASSERT(scalars.size() == scalar_arg_count, "PagedCausalConv1D scalar arguments do not match the kernel signature");+for (auto v : scalars) {scalar_desc desc;desc.t = scalar_desc::Types::INT32;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 2d4a1835e8b..ce29615d19a 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@@ -30,18 +30,21 @@ struct PagedSelectiveSSMOpt : public cldnn::ImplementationManager {static constexpr std::array supported_real_types = {ov::element::f16, ov::element::f32, ov::element::bf16};static constexpr std::array supported_index_types = {ov::element::i32, ov::element::i64};- ov::element::Type real_type = ov::element::dynamic;+ ov::element::Type data_type = ov::element::dynamic;ov::element::Type index_type = ov::element::dynamic;for (size_t i = 0; i < node.get_dependencies().size(); i++) {const auto& in_layout = node.get_input_layout(i);if (!cldnn::one_of(in_layout.format, supported_fmts))return false;- if (i <= cldnn::paged_selective_ssm::RECURRENT_STATE_TABLE) {+ if (i < cldnn::paged_selective_ssm::RECURRENT_STATE_TABLE) {if (!cldnn::one_of(in_layout.data_type, supported_real_types))return false;- if (real_type.is_dynamic())- real_type = in_layout.data_type;- else if (real_type != in_layout.data_type)+ if (data_type.is_dynamic())+ data_type = in_layout.data_type;+ else if (data_type != in_layout.data_type)+ return false;+ } else if (i == cldnn::paged_selective_ssm::RECURRENT_STATE_TABLE) {+ if (!cldnn::one_of(in_layout.data_type, supported_real_types))return false;} else {if (!cldnn::one_of(in_layout.data_type, supported_index_types))@@ -54,7 +57,7 @@ struct PagedSelectiveSSMOpt : public cldnn::ImplementationManager {}const auto& out_layout = node.get_output_layout(0);- return cldnn::one_of(out_layout.format, supported_fmts) && out_layout.data_type == real_type;+ return cldnn::one_of(out_layout.format, supported_fmts) && out_layout.data_type == data_type;}};diff --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.clindex 0271390fe78..723f109aea7 100644--- 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.cl@@ -3,13 +3,7 @@//#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+#include "selective_ssm_type_utils.cl"KERNEL(paged_selective_ssm_jit_precompute)(OPTIONAL_SHAPE_INFO_ARGconst __global INPUT0_TYPE* A,@@ -33,7 +27,5 @@ KERNEL(paged_selective_ssm_jit_precompute)(OPTIONAL_SHAPE_INFO_ARGconst 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]));+ 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/paged_selective_ssm_large_state.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_large_state.clindex c38f9daffac..904948ecd16 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_large_state.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_large_state.cl@@ -4,14 +4,9 @@#include "include/batch_headers/common.cl"#include "include/batch_headers/fetch_data.cl"-#include "include/batch_headers/bf16_utils.cl"+#include "selective_ssm_type_utils.cl"#define SSM_MAX_HEAD_DIM_BLOCK 4-#if INPUT0_IS_FP-# define SSM_TO_FLOAT(v) convert_float(v)-#else-# define SSM_TO_FLOAT(v) _convert_as_bfloat16_float(v)-#endif#define SSM_STATE_ITERATION_TYPE size_t#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)@@ -120,7 +115,7 @@ KERNEL(paged_selective_ssm_large_state)(ulong write_slot = 1;const size_t heads_per_group = num_heads / num_groups;const size_t g = h / heads_per_group;- const float A_value = SSM_TO_FLOAT(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);+ const float A_value = ssm_to_float(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);const int valid_head_dim_block = p_base + (size_t)block <= head_dim? block: (int)(head_dim - p_base);@@ -133,7 +128,7 @@ KERNEL(paged_selective_ssm_large_state)(if (state_element >= state_size)break;state_scratch[scratch_base + state_element] =- SSM_TO_FLOAT(recurrent_state_table[GET_DATA_INDEX(INPUT5, (size_t)first_block, h, p, state_element)]);+ ssm_to_float(recurrent_state_table[GET_DATA_INDEX(INPUT5, (size_t)first_block, h, p, state_element)]);}}@@ -143,36 +138,33 @@ KERNEL(paged_selective_ssm_large_state)(#include "selective_ssm_recurrence.cl"#undef SSM_TOKEN_INDEX- if (cache_enabled) {- const bool at_boundary = --tokens_until_boundary == 0;- const bool at_sequence_end = token + 1 == token_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)block_indices_count) {- const long block_id = (long)block_indices[GET_DATA_INDEX(INPUT7, (size_t)block_position, 0, 0, 0)];- if (block_id >= 0 && (size_t)block_id < num_state_blocks) {- for (int p_offset = 0; p_offset < valid_head_dim_block; ++p_offset) {- const size_t p = p_base + (size_t)p_offset;- const size_t scratch_base = ((seq * num_heads + h) * head_dim + p) * state_size;- for (size_t step = 0; step < state_iterations; ++step) {- const size_t state_element = step * lws + lane;- if (state_element >= state_size)- break;- recurrent_state_table[GET_DATA_INDEX(INPUT5, (size_t)block_id, h, p, state_element)] =- TO_INPUT5_TYPE(state_scratch[scratch_base + state_element]);- }+ const bool at_boundary = cache_enabled && --tokens_until_boundary == 0;+ const bool at_sequence_end = token + 1 == token_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)block_indices_count) {+ const long block_id = (long)block_indices[GET_DATA_INDEX(INPUT7, (size_t)block_position, 0, 0, 0)];+ if (block_id >= 0 && (size_t)block_id < num_state_blocks) {+ for (int p_offset = 0; p_offset < valid_head_dim_block; ++p_offset) {+ const size_t p = p_base + (size_t)p_offset;+ const size_t scratch_base = ((seq * num_heads + h) * head_dim + p) * state_size;+ for (size_t step = 0; step < state_iterations; ++step) {+ const size_t state_element = step * lws + lane;+ if (state_element >= state_size)+ break;+ recurrent_state_table[GET_DATA_INDEX(INPUT5, (size_t)block_id, h, p, state_element)] =+ TO_INPUT5_TYPE(state_scratch[scratch_base + state_element]);}}}- if (at_boundary)- tokens_until_boundary = positive_interval;}+ if (at_boundary)+ tokens_until_boundary = positive_interval;}}}#undef SSM_MAX_HEAD_DIM_BLOCK-#undef SSM_TO_FLOAT#undef SSM_STATE_ITERATION_TYPE#undef SSM_DT_INDEX#undef SSM_B_INDEXdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_opt.clindex aecff8148c6..af661b70b3e 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_opt.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/paged_selective_ssm_opt.cl@@ -4,14 +4,9 @@#include "include/batch_headers/common.cl"#include "include/batch_headers/fetch_data.cl"-#include "include/batch_headers/bf16_utils.cl"+#include "selective_ssm_type_utils.cl"#define SSM_MAX_HEAD_DIM_BLOCK 4-#if INPUT0_IS_FP-# define SSM_TO_FLOAT(v) convert_float(v)-#else-# define SSM_TO_FLOAT(v) _convert_as_bfloat16_float(v)-#endif#define SSM_STATE_ITERATION_TYPE uint#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)@@ -121,7 +116,7 @@ KERNEL(paged_selective_ssm_opt)(ulong write_slot = 1;const size_t heads_per_group = num_heads / num_groups;const size_t g = h / heads_per_group;- const float A_value = SSM_TO_FLOAT(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);+ const float A_value = ssm_to_float(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);// Keep recurrent state in FP32 across tokens; cast only when writing output.__local float* local_state = work;__local float* reduction = work + (size_t)block * state_size;@@ -136,7 +131,7 @@ KERNEL(paged_selective_ssm_opt)(if (state_element >= state_size)break;const size_t state_idx = GET_DATA_INDEX(INPUT5, (size_t)first_block, h, p, state_element);- local_state[(size_t)p_offset * state_size + state_element] = SSM_TO_FLOAT(recurrent_state_table[state_idx]);+ local_state[(size_t)p_offset * state_size + state_element] = ssm_to_float(recurrent_state_table[state_idx]);}}@@ -146,36 +141,33 @@ KERNEL(paged_selective_ssm_opt)(#include "selective_ssm_recurrence.cl"#undef SSM_TOKEN_INDEX- if (cache_enabled) {- const bool at_boundary = --tokens_until_boundary == 0;- const bool at_sequence_end = token + 1 == token_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)block_indices_count) {- const long block_id = (long)block_indices[GET_DATA_INDEX(INPUT7, (size_t)block_position, 0, 0, 0)];- if (block_id >= 0 && (size_t)block_id < num_state_blocks) {- for (int p_offset = 0; p_offset < valid_head_dim_block; ++p_offset) {- const size_t p = p_base + (size_t)p_offset;- for (uint step = 0; step < state_iterations; ++step) {- const uint state_element = step * lws + lane;- if (state_element >= state_size)- break;- const size_t local_idx = (size_t)p_offset * state_size + state_element;- const size_t state_idx = GET_DATA_INDEX(INPUT5, (size_t)block_id, h, p, state_element);- recurrent_state_table[state_idx] = TO_INPUT5_TYPE(local_state[local_idx]);- }+ const bool at_boundary = cache_enabled && --tokens_until_boundary == 0;+ const bool at_sequence_end = token + 1 == token_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)block_indices_count) {+ const long block_id = (long)block_indices[GET_DATA_INDEX(INPUT7, (size_t)block_position, 0, 0, 0)];+ if (block_id >= 0 && (size_t)block_id < num_state_blocks) {+ for (int p_offset = 0; p_offset < valid_head_dim_block; ++p_offset) {+ const size_t p = p_base + (size_t)p_offset;+ for (uint step = 0; step < state_iterations; ++step) {+ const uint state_element = step * lws + lane;+ if (state_element >= state_size)+ break;+ const size_t local_idx = (size_t)p_offset * state_size + state_element;+ const size_t state_idx = GET_DATA_INDEX(INPUT5, (size_t)block_id, h, p, state_element);+ recurrent_state_table[state_idx] = TO_INPUT5_TYPE(local_state[local_idx]);}}}- if (at_boundary)- tokens_until_boundary = positive_interval;}+ if (at_boundary)+ tokens_until_boundary = positive_interval;}}}#undef SSM_MAX_HEAD_DIM_BLOCK-#undef SSM_TO_FLOAT#undef SSM_STATE_ITERATION_TYPE#undef SSM_DT_INDEX#undef SSM_B_INDEXdiff --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.clindex 43e27dccb93..f41aacfbed9 100644--- 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.cl@@ -4,18 +4,12 @@#include "include/batch_headers/common.cl"#include "include/batch_headers/fetch_data.cl"-#include "include/batch_headers/bf16_utils.cl"+#include "selective_ssm_type_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@@ -211,7 +205,7 @@ KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARG// 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_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_SLMfloat private_state[SSM_HEAD_DIM_BLOCK][SSM_STATE_ITERATIONS];@@ -225,7 +219,7 @@ KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARGconst 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)]);+ ssm_to_float(state[SSM_STATE_INDEX(initial_state_block, p, state_element)]);}}}@@ -237,7 +231,7 @@ KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARG#elseconst 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_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_DAconst float dA_lane = lane == 0 ? precomputed_dA[SSM_PRECOMPUTED_DA_INDEX(token_idx)] : 0.0f;@@ -252,7 +246,7 @@ KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARGfor (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)])+ ? 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;@@ -262,8 +256,8 @@ KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARGfor (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)]);+ 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 unrollfor (uint p_offset = 0; p_offset < SSM_HEAD_DIM_BLOCK; ++p_offset) {if (p_base + p_offset < SSM_HEAD_DIM) {@@ -286,35 +280,33 @@ KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARG// 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) {+ const bool at_boundary = cache_enabled && --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;+ 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));- }+ 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 (at_boundary) {# if IS_DYNAMIC- tokens_until_boundary = positive_interval;+ tokens_until_boundary = positive_interval;# else- tokens_until_boundary = (uint)min(positive_interval, (ulong)0xffffffffu);+ tokens_until_boundary = (uint)min(positive_interval, (ulong)0xffffffffu);# endif- }}}#endif@@ -337,7 +329,6 @@ KERNEL(selective_ssm_jit)(OPTIONAL_SHAPE_INFO_ARG#endif}-#undef SSM_TO_FLOAT#undef SSM_RUNTIME_INDEX#undef SSM_TOKEN_TYPE#undef SSM_COUNTDOWN_TYPEdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_large_state.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_large_state.clindex 9025574fe15..ef6aabc1d41 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_large_state.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_large_state.cl@@ -4,14 +4,9 @@#include "include/batch_headers/common.cl"#include "include/batch_headers/fetch_data.cl"-#include "include/batch_headers/bf16_utils.cl"+#include "selective_ssm_type_utils.cl"#define SSM_MAX_HEAD_DIM_BLOCK 4-#if INPUT0_IS_FP-# define SSM_TO_FLOAT(v) convert_float(v)-#else-# define SSM_TO_FLOAT(v) _convert_as_bfloat16_float(v)-#endif#define SSM_STATE_ITERATION_TYPE size_t#define SSM_DT_INDEX(token) GET_DATA_INDEX(INPUT1, b, token, h, 0)#define SSM_B_INDEX(token, state_element) GET_DATA_INDEX(INPUT2, b, token, g, state_element)@@ -69,7 +64,7 @@ KERNEL(selective_ssm_large_state)(const size_t heads_per_group = num_heads / num_groups;const size_t g = h / heads_per_group;- const float A_value = SSM_TO_FLOAT(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);+ const float A_value = ssm_to_float(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);const int valid_head_dim_block = p_base + (size_t)block <= head_dim? block: (int)(head_dim - p_base);@@ -82,7 +77,7 @@ KERNEL(selective_ssm_large_state)(if (state_element >= state_size)break;state_scratch[scratch_base + state_element] =- SSM_TO_FLOAT(initial_state[GET_DATA_INDEX(INPUT5, b, h, p, state_element)]);+ ssm_to_float(initial_state[GET_DATA_INDEX(INPUT5, b, h, p, state_element)]);}}@@ -106,7 +101,6 @@ KERNEL(selective_ssm_large_state)(}#undef SSM_MAX_HEAD_DIM_BLOCK-#undef SSM_TO_FLOAT#undef SSM_STATE_ITERATION_TYPE#undef SSM_DT_INDEX#undef SSM_B_INDEXdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_opt.clindex fc522c1bc21..61be6c17503 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_opt.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_opt.cl@@ -4,14 +4,9 @@#include "include/batch_headers/common.cl"#include "include/batch_headers/fetch_data.cl"-#include "include/batch_headers/bf16_utils.cl"+#include "selective_ssm_type_utils.cl"#define SSM_MAX_HEAD_DIM_BLOCK 4-#if INPUT0_IS_FP-# define SSM_TO_FLOAT(v) convert_float(v)-#else-# define SSM_TO_FLOAT(v) _convert_as_bfloat16_float(v)-#endif#define SSM_STATE_ITERATION_TYPE uint#define SSM_DT_INDEX(token) GET_DATA_INDEX(INPUT1, b, token, h, 0)#define SSM_B_INDEX(token, state_element) GET_DATA_INDEX(INPUT2, b, token, g, state_element)@@ -70,7 +65,7 @@ KERNEL(selective_ssm_opt)(const size_t heads_per_group = num_heads / num_groups;const size_t g = h / heads_per_group;- const float A_value = SSM_TO_FLOAT(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);+ const float A_value = ssm_to_float(A[GET_DATA_INDEX(INPUT0, h, 0, 0, 0)]);// Keep recurrent state in FP32 across tokens; cast only when writing output.__local float* local_state = work;__local float* reduction = work + (size_t)block * state_size;@@ -85,7 +80,7 @@ KERNEL(selective_ssm_opt)(if (state_element >= state_size)break;const size_t state_idx = GET_DATA_INDEX(INPUT5, b, h, p, state_element);- local_state[(size_t)p_offset * state_size + state_element] = SSM_TO_FLOAT(initial_state[state_idx]);+ local_state[(size_t)p_offset * state_size + state_element] = ssm_to_float(initial_state[state_idx]);}}@@ -108,7 +103,6 @@ KERNEL(selective_ssm_opt)(}#undef SSM_MAX_HEAD_DIM_BLOCK-#undef SSM_TO_FLOAT#undef SSM_STATE_ITERATION_TYPE#undef SSM_DT_INDEX#undef SSM_B_INDEXdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_recurrence.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_recurrence.clindex 8c4a49d0a04..5393f08a665 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_recurrence.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_recurrence.cl@@ -6,14 +6,14 @@// SSM_TOKEN_INDEX, and the state accessors. This file is included directly// inside the token loop to preserve the compiler-visible recurrence body.const size_t dt_idx = SSM_DT_INDEX(SSM_TOKEN_INDEX);-const float dt_value = SSM_TO_FLOAT(dt[dt_idx]);+const float dt_value = ssm_to_float(dt[dt_idx]);const float dA = exp(A_value * dt_value);float input_scales[SSM_MAX_HEAD_DIM_BLOCK];float partial[SSM_MAX_HEAD_DIM_BLOCK];for (int p_offset = 0; p_offset < valid_head_dim_block; ++p_offset) {const size_t p = p_base + (size_t)p_offset;- input_scales[p_offset] = SSM_TO_FLOAT(x[SSM_X_INDEX(SSM_TOKEN_INDEX, p)]) * dt_value;+ input_scales[p_offset] = ssm_to_float(x[SSM_X_INDEX(SSM_TOKEN_INDEX, p)]) * dt_value;partial[p_offset] = 0.0f;}@@ -23,8 +23,8 @@ for (SSM_STATE_ITERATION_TYPE step = 0; step < state_iterations; ++step) {break;const size_t b_idx = SSM_B_INDEX(SSM_TOKEN_INDEX, state_element);const size_t c_idx = SSM_C_INDEX(SSM_TOKEN_INDEX, state_element);- const float b_value = SSM_TO_FLOAT(B[b_idx]);- const float c_value = SSM_TO_FLOAT(C[c_idx]);+ const float b_value = ssm_to_float(B[b_idx]);+ const float c_value = ssm_to_float(C[c_idx]);for (int p_offset = 0; p_offset < valid_head_dim_block; ++p_offset) {const size_t recurrence_state_idx = SSM_STATE_INDEX(p_offset, state_element);const float new_state = fma(SSM_STATE_AT(recurrence_state_idx), dA, input_scales[p_offset] * b_value);diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_type_utils.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_type_utils.clnew file mode 100644index 00000000000..cb175b54004--- /dev/null+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/selective_ssm_type_utils.cl@@ -0,0 +1,22 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#ifndef SELECTIVE_SSM_TYPE_UTILS+#define SELECTIVE_SSM_TYPE_UTILS++#include "include/batch_headers/bf16_utils.cl"++inline float ssm_to_float(float value) __attribute__((overloadable)) {+ return value;+}++inline float ssm_to_float(half value) __attribute__((overloadable)) {+ return convert_float(value);+}++inline float ssm_to_float(ushort value) __attribute__((overloadable)) {+ return _convert_as_bfloat16_float(value);+}++#endifdiff --git a/src/plugins/intel_gpu/tests/functional/single_layer_tests/selective_ssm.cpp b/src/plugins/intel_gpu/tests/functional/single_layer_tests/selective_ssm.cppindex b60fd28d994..29d42119d03 100644--- a/src/plugins/intel_gpu/tests/functional/single_layer_tests/selective_ssm.cpp+++ b/src/plugins/intel_gpu/tests/functional/single_layer_tests/selective_ssm.cpp@@ -10,6 +10,7 @@#include <array>#include <cmath>#include <cstdint>+#include <limits>#include <sstream>#include <string>#include <tuple>@@ -20,6 +21,7 @@#include "openvino/op/parameter.hpp"#include "openvino/op/result.hpp"#include "openvino/runtime/core.hpp"+#include "openvino/runtime/properties.hpp"namespace {@@ -143,8 +145,9 @@ std::pair<std::vector<float>, std::vector<float>> paged_reference(const std::vecoutput[(token * num_heads + h) * head_dim + p] = sum;const int64_t cached_tokens = previous + token - token_begin + 1;- if (interval > 0 && (cached_tokens % interval == 0 || token + 1 == token_end)) {- const int64_t slot = 1 + (cached_tokens - 1) / interval;+ const bool interval_hit = interval > 0 && cached_tokens % interval == 0;+ if (interval_hit || token + 1 == token_end) {+ const int64_t slot = interval > 0 ? 1 + (cached_tokens - 1) / interval : 1;if (block_begin + slot < block_indices_begins[seq + 1]) {const int64_t block = block_indices[block_begin + slot];for (int32_t n = 0; n < state_size; n++)@@ -158,7 +161,7 @@ std::pair<std::vector<float>, std::vector<float>> paged_reference(const std::vecreturn {output, state};}-TEST(smoke_GPUSelectiveSSMIntegration, DISABLED_SelectiveSSMDynamicModel) {+TEST(smoke_GPUSelectiveSSMIntegration, SelectiveSSMDynamicModel) {struct SelectiveCase {size_t batch;size_t seq_len;@@ -189,7 +192,7 @@ TEST(smoke_GPUSelectiveSSMIntegration, DISABLED_SelectiveSSMDynamicModel) {ov::Core core;for (const auto& device : get_gpu_devices(core)) {SCOPED_TRACE(device);- auto request = core.compile_model(model, device).create_infer_request();+ auto request = core.compile_model(model, device, ov::hint::inference_precision(ov::element::f32)).create_infer_request();for (const auto& test_case : cases) {const auto [batch, seq_len, num_heads, num_groups, head_dim, state_size] = test_case;SCOPED_TRACE(testing::Message() << "batch=" << batch << ", seq_len=" << seq_len << ", heads=" << num_heads << ", groups=" << num_groups@@ -224,7 +227,7 @@ TEST(smoke_GPUSelectiveSSMIntegration, SelectiveSSMIndividualOutputs) {constexpr size_t head_dim = 3;constexpr size_t state_size = 5;- const auto make_model = [batch, num_heads, num_groups, head_dim, state_size](size_t seq_len, size_t output_index, bool dynamic) {+ const auto make_model = [=](size_t seq_len, size_t output_index, bool dynamic) {const auto shape = [dynamic](const ov::Shape& static_shape) {return dynamic ? ov::PartialShape::dynamic(static_shape.size()) : ov::PartialShape{static_shape};};@@ -259,7 +262,7 @@ TEST(smoke_GPUSelectiveSSMIntegration, SelectiveSSMIndividualOutputs) {};const auto run = [&](ov::Core& core, const std::string& device, size_t seq_len, size_t output_index, bool dynamic) {- auto compiled_model = core.compile_model(make_model(seq_len, output_index, dynamic), device);+ auto compiled_model = core.compile_model(make_model(seq_len, output_index, dynamic), device, ov::hint::inference_precision(ov::element::f32));auto request = compiled_model.create_infer_request();check(request, seq_len, output_index);std::stringstream blob;@@ -270,7 +273,8 @@ TEST(smoke_GPUSelectiveSSMIntegration, SelectiveSSMIndividualOutputs) {const auto run_dynamic_sequence = [&](ov::Core& core, const std::string& device, size_t output_index) {static constexpr std::array<size_t, 5> sequence_lengths{0, 1, 9, 0, 3};- auto compiled_model = core.compile_model(make_model(sequence_lengths.front(), output_index, true), device);+ auto compiled_model =+ core.compile_model(make_model(sequence_lengths.front(), output_index, true), device, ov::hint::inference_precision(ov::element::f32));auto request = compiled_model.create_infer_request();for (const auto seq_len : sequence_lengths) {check(request, seq_len, output_index);@@ -295,7 +299,9 @@ TEST(smoke_GPUSelectiveSSMIntegration, SelectiveSSMIndividualOutputs) {}}-TEST(smoke_GPUSelectiveSSMIntegration, DISABLED_PagedSelectiveSSMDynamicModel) {+TEST(smoke_GPUSelectiveSSMIntegration, PagedSelectiveSSMDynamicModel) {+ constexpr int64_t max_plugin_metadata = std::numeric_limits<int32_t>::max();+struct PagedCase {std::vector<int64_t> subsequences;std::vector<int64_t> blocks;@@ -350,8 +356,9 @@ TEST(smoke_GPUSelectiveSSMIntegration, DISABLED_PagedSelectiveSSMDynamicModel) {const std::vector<PagedCase> cases{{{0, 3, 5}, {0, 1, 2, 3, 4, 5}, {0, 3, 6}, {0, 1}, {2, 2}, 6, 4, 2, 3, 5},{{0, 1}, {1, 1}, {0, 2}, {4}, {2}, 2, 4, 2, 3, 5},+ {{0, 1}, {0, 0}, {0, 2}, {0}, {0}, 1, 4, 2, 3, 5},{{0, 2, 7}, {5, 2, 1, 4, 0, 3}, {0, 2, 6}, {1, 7}, {3, 2}, 6, 6, 3, 5, 33},- {{0, 2}, {0, 1}, {0, 2}, {4294967297}, {4294967296}, 2, 2, 1, 1, 513},+ {{0, 2}, {0, 1}, {0, 2}, {max_plugin_metadata - 1}, {max_plugin_metadata}, 2, 2, 1, 1, 513},{{0, 0}, {}, {0, 0}, {0}, {2}, 1, 2, 1, 4, 9},{{0, 1}, {0, 1}, {0, 2}, {0}, {1}, 2, 1, 1, 1, 8192},{{0, 3, 5}, {0, 1, 2, 3, 4, 5}, {0, 3, 6}, {0, 1}, {2, 2}, 6, 4, 2, 3, 5},@@ -360,7 +367,7 @@ TEST(smoke_GPUSelectiveSSMIntegration, DISABLED_PagedSelectiveSSMDynamicModel) {ov::Core core;for (const auto& device : get_gpu_devices(core)) {SCOPED_TRACE(device);- auto compiled_model = core.compile_model(model, device);+ auto compiled_model = core.compile_model(model, device, ov::hint::inference_precision(ov::element::f32));auto request = compiled_model.create_infer_request();const auto set_index_input = [&request](size_t index, const std::vector<int64_t>& values) {request.set_input_tensor(index, make_tensor<int64_t>(ov::element::i64, {values.size()}, values));@@ -449,7 +456,7 @@ TEST(smoke_GPUSelectiveSSMIntegration, SelectiveSSMChainedState) {ov::Core core;for (const auto& device : get_gpu_devices(core)) {SCOPED_TRACE(device);- auto request = core.compile_model(model, device).create_infer_request();+ auto request = core.compile_model(model, device, ov::hint::inference_precision(ov::element::f32)).create_infer_request();request.set_input_tensor(0, make_tensor<float>(ov::element::f32, {num_heads}, A));request.set_input_tensor(1, make_tensor<float>(ov::element::f32, {batch, seq_len, num_heads}, dt));request.set_input_tensor(2, make_tensor<float>(ov::element::f32, {batch, seq_len, num_groups, state_size}, B));@@ -463,7 +470,7 @@ TEST(smoke_GPUSelectiveSSMIntegration, SelectiveSSMChainedState) {}}-TEST(smoke_GPUSelectiveSSMIntegration, DISABLED_PagedSelectiveSSMChainedStateMutation) {+TEST(smoke_GPUSelectiveSSMIntegration, PagedSelectiveSSMChainedStateMutation) {constexpr int32_t tokens = 2;constexpr int32_t num_heads = 4;constexpr int32_t num_groups = 2;@@ -552,7 +559,7 @@ TEST(smoke_GPUSelectiveSSMIntegration, DISABLED_PagedSelectiveSSMChainedStateMutov::Core core;for (const auto& device : get_gpu_devices(core)) {SCOPED_TRACE(device);- auto compiled_model = core.compile_model(model, device);+ auto compiled_model = core.compile_model(model, device, ov::hint::inference_precision(ov::element::f32));auto state_tensor = compiled_model.get_context().create_tensor(ov::element::f32, {state_blocks, num_heads, head_dim, state_size});state_tensor.copy_from(make_tensor<float>(ov::element::f32, {state_blocks, num_heads, head_dim, state_size}, state));auto request = compiled_model.create_infer_request();diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/paged_causal_conv1d.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/paged_causal_conv1d.cppindex 7a54d18e2ba..2f8613fc1c6 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/paged_causal_conv1d.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/paged_causal_conv1d.cpp@@ -26,6 +26,7 @@ struct paged_causal_conv1d_test_params {ov::element::Type precision;bool with_bias;bool is_caching_test = false;+ bool padded_input = false;};struct paged_causal_conv1d_gpu_test : public ::testing::TestWithParam<paged_causal_conv1d_test_params> {@@ -53,6 +54,19 @@ struct paged_causal_conv1d_gpu_test : public ::testing::TestWithParam<paged_causset_values(mem, input_data);}+ template <typename T>+ void load_padded_input(cldnn::memory::ptr mem, const std::vector<T>& input_data, int32_t tokens, int32_t hidden_size) {+ cldnn::mem_lock<T, mem_lock_type::write> data(mem, get_test_stream());+ std::fill(data.begin(), data.end(), T{});+ const auto& layout = mem->get_layout();+ for (int32_t token = 0; token < tokens; token++) {+ for (int32_t hidden = 0; hidden < hidden_size; hidden++) {+ const auto offset = layout.get_linear_offset(tensor(batch(token), feature(hidden), spatial(0, 0, 0, 0)));+ data[offset] = input_data[static_cast<size_t>(token) * hidden_size + hidden];+ }+ }+ }+static paging_desc make_paging_desc(int32_t tokens, int32_t num_sequences) {OPENVINO_ASSERT(num_sequences > 0, "num_sequences must be positive");OPENVINO_ASSERT(tokens >= num_sequences, "tokens must be >= num_sequences");@@ -254,7 +268,8 @@ struct paged_causal_conv1d_gpu_test : public ::testing::TestWithParam<paged_causconst auto page = make_paging_desc(p.tokens, p.num_sequences);- const layout input_layout({p.tokens, p.hidden_size}, data_type, format::bfyx);+ const auto input_padding = p.padded_input ? padding({0, 3}, {0, 5}) : padding{};+ const layout input_layout({p.tokens, p.hidden_size}, data_type, format::bfyx, input_padding);const layout state_layout({page.num_blocks, p.hidden_size, p.kernel_size}, data_type, format::bfyx);const layout weight_layout({p.hidden_size, 1, p.kernel_size}, data_type, format::bfyx);const layout bias_layout({p.with_bias ? p.hidden_size : 0}, data_type, format::bfyx);@@ -298,7 +313,10 @@ struct paged_causal_conv1d_gpu_test : public ::testing::TestWithParam<paged_causp.kernel_size,ref_output);- load_input(input_mem, input_embeds);+ if (p.padded_input)+ load_padded_input(input_mem, input_embeds, p.tokens, p.hidden_size);+ else+ load_input(input_mem, input_embeds);load_input(state_mem, conv_state_table);load_input(weight_mem, conv_weight);if (!conv_bias.empty()) {@@ -321,18 +339,17 @@ struct paged_causal_conv1d_gpu_test : public ::testing::TestWithParam<paged_causcache_interval_layout,data_type);- auto [out_mem, net] =- run_network(topo,- input_mem,- state_mem,- weight_mem,- bias_mem,- subseq_mem,- block_idx_mem,- block_idx_begins_mem,- past_lens_mem,- cache_interval_mem,- p.is_caching_test);+ auto [out_mem, net] = run_network(topo,+ input_mem,+ state_mem,+ weight_mem,+ bias_mem,+ subseq_mem,+ block_idx_mem,+ block_idx_begins_mem,+ past_lens_mem,+ cache_interval_mem,+ p.is_caching_test);ASSERT_TRUE(out_mem != nullptr);ASSERT_EQ(out_mem->count(), ref_output.size());@@ -373,7 +390,7 @@ struct paged_causal_conv1d_gpu_test : public ::testing::TestWithParam<paged_causconst auto& p = info.param;return "paged_causal_conv1d_gpu_test_" + p.precision.to_string() + "_tokens_" + std::to_string(p.tokens) + "_seq_" + std::to_string(p.num_sequences) +"_hidden_" + std::to_string(p.hidden_size) + "_kernel_" + std::to_string(p.kernel_size) + (p.with_bias ? "_bias" : "_no_bias") +- (p.is_caching_test ? "_cached" : "");+ (p.is_caching_test ? "_cached" : "") + (p.padded_input ? "_padded_input" : "");}};@@ -390,7 +407,9 @@ INSTANTIATE_TEST_SUITE_P(smoke_paged_causal_conv1d_gpu_test,paged_causal_conv1d_test_params{12, 3, 32, 5, ov::element::f32, true},paged_causal_conv1d_test_params{8, 2, 16, 4, ov::element::f16, false},paged_causal_conv1d_test_params{8, 2, 16, 4, ov::element::f32, false},- paged_causal_conv1d_test_params{8, 2, 16, 4, ov::element::f16, true, true}),+ paged_causal_conv1d_test_params{8, 2, 16, 4, ov::element::f16, true, true},+ paged_causal_conv1d_test_params{8, 2, 16, 4, ov::element::f32, true, false, true},+ paged_causal_conv1d_test_params{8, 2, 16, 4, ov::element::f16, true, false, true}),paged_causal_conv1d_gpu_test::PrintToStringParamName);} // namespacediff --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 e9289be6fd0..c1b6e8b260a 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@@ -43,7 +43,7 @@ struct paged_selective_ssm_test_params {ov::element::Type precision;ov::element::Type index_precision;bool dynamic_shapes;- std::vector<bool> alias_first_write = {};+ std::vector<bool> alias_first_write;bool reverse_blocks = false;bool caching_test = false;bool padded_layouts = false;@@ -51,22 +51,28 @@ struct paged_selective_ssm_test_params {int32_t invalid_metadata = 0;bool accumulation_test = false;float relative_output_tolerance = 5e-5f;+ ov::element::Type state_precision = ov::element::dynamic;expected_ssm_impl expected_impl = expected_ssm_impl::any;};+paged_selective_ssm_test_params with_state_precision(paged_selective_ssm_test_params params, const ov::element::Type& state_precision) {+ params.state_precision = state_precision;+ return params;+}+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,- const std::vector<T>& dt,- const std::vector<T>& B,- const std::vector<T>& x,- const std::vector<T>& C,- std::vector<T>& state,+ template <typename DataT, typename StateT>+ static void run_reference(const std::vector<DataT>& A,+ const std::vector<DataT>& dt,+ const std::vector<DataT>& B,+ const std::vector<DataT>& x,+ const std::vector<DataT>& C,+ std::vector<StateT>& state,const std::vector<int32_t>& subsequence_begins,const std::vector<int32_t>& block_indices,const std::vector<int32_t>& block_indices_begins,@@ -76,7 +82,7 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_seleint32_t num_groups,int32_t head_dim,int32_t state_size,- std::vector<T>& output) {+ std::vector<DataT>& output) {const int32_t heads_per_group = num_heads / num_groups;const int32_t num_sequences = static_cast<int32_t>(subsequence_begins.size()) - 1;const int32_t tokens = static_cast<int32_t>(x.size()) / (num_heads * head_dim);@@ -114,18 +120,18 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_selelocal_state[n] = new_state;acc += local_state[n] * static_cast<float>(C[(token * num_groups + g) * state_size + n]);}- output[(token * num_heads + h) * head_dim + p] = static_cast<T>(acc);+ output[(token * num_heads + h) * head_dim + p] = static_cast<DataT>(acc);const int32_t processed_now = (token - token_begin) + 1;const int32_t cached_tokens = prev_nums + processed_now;const bool reached_interval_boundary = interval > 0 && ((cached_tokens % interval) == 0);const bool reached_sequence_end = token == token_end - 1;- if (interval > 0 && (reached_interval_boundary || reached_sequence_end)) {- const int32_t slot = 1 + (cached_tokens - 1) / interval;+ if (reached_interval_boundary || reached_sequence_end) {+ const int32_t slot = interval > 0 ? 1 + (cached_tokens - 1) / interval : 1;if (slot < seq_blocks) {const int32_t block_id = block_indices[block_begin + slot];for (int32_t n = 0; n < state_size; n++) {- state[state_off(block_id, h, p, n)] = static_cast<T>(local_state[n]);+ state[state_off(block_id, h, p, n)] = static_cast<StateT>(local_state[n]);}}}@@ -135,10 +141,12 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_sele}}- template <typename T>+ template <typename DataT, typename StateT>void execute_t(const paged_selective_ssm_test_params& p) {auto& engine = get_test_engine();const auto data_type = cldnn::element_type_to_data_type(p.precision);+ const auto state_precision = p.state_precision == ov::element::dynamic ? p.precision : p.state_precision;+ const auto state_data_type = cldnn::element_type_to_data_type(state_precision);const auto index_data_type = cldnn::element_type_to_data_type(p.index_precision);const int32_t num_sequences = static_cast<int32_t>(p.seq_tokens.size());const int32_t tokens = static_cast<int32_t>(std::accumulate(p.seq_tokens.begin(), p.seq_tokens.end(), 0));@@ -149,7 +157,7 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_seleint32_t total_blocks = 0;for (int32_t seq = 0; seq < num_sequences; seq++) {subsequence_begins.push_back(subsequence_begins.back() + p.seq_tokens[seq]);- int32_t required_slots = 1;+ int32_t required_slots = p.seq_tokens[seq] > 0 ? 2 : 1;if (p.cache_intervals[seq] > 0) {const int32_t processed = std::max(p.processed_tokens[seq], 0);const int32_t prev_nums = processed % p.cache_intervals[seq];@@ -182,7 +190,7 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_selelayout dt_layout({tokens, p.num_heads}, data_type, format::bfyx);layout BC_layout({tokens, p.num_groups, p.state_size}, data_type, format::bfyx);layout x_layout({tokens, p.num_heads, p.head_dim}, data_type, format::bfyx);- layout state_layout({total_blocks, p.num_heads, p.head_dim, p.state_size}, data_type, format::bfyx);+ layout state_layout({total_blocks, p.num_heads, p.head_dim, p.state_size}, state_data_type, format::bfyx);const auto state_memory_layout = p.padded_layouts ? state_layout.with_padding(padding({1, 2, 1, 3}, {2, 1, 2, 1})) : state_layout;const auto state_pitches = state_memory_layout.get_pitches();const auto state_offset = [&](int32_t block, int32_t h, int32_t dim, int32_t n) {@@ -213,21 +221,21 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_seleauto processed_mem = allocate(processed_layout);auto interval_mem = allocate(processed_layout);- auto A_data = make_test_values<T>(A_mem->count(), -0.03f, -0.2f);- auto dt_data = make_test_values<T>(dt_mem->count(), 0.006f, 0.07f);- auto B_data = make_test_values<T>(B_mem->count(), 0.009f);- auto x_data = make_test_values<T>(x_mem->count(), 0.013f);- auto C_data = make_test_values<T>(C_mem->count(), 0.011f);- auto state_data = make_test_values<T>(state_layout.count(), 0.007f);+ auto A_data = make_test_values<DataT>(A_mem->count(), -0.03f, -0.2f);+ auto dt_data = make_test_values<DataT>(dt_mem->count(), 0.006f, 0.07f);+ auto B_data = make_test_values<DataT>(B_mem->count(), 0.009f);+ auto x_data = make_test_values<DataT>(x_mem->count(), 0.013f);+ auto C_data = make_test_values<DataT>(C_mem->count(), 0.011f);+ auto state_data = make_test_values<StateT>(state_layout.count(), 0.007f);if (p.accumulation_test) {- A_data.assign(A_data.size(), static_cast<T>(0.f));- dt_data.assign(dt_data.size(), static_cast<T>(1.f));- B_data.assign(B_data.size(), static_cast<T>(1.f));+ A_data.assign(A_data.size(), static_cast<DataT>(0.f));+ dt_data.assign(dt_data.size(), static_cast<DataT>(1.f));+ B_data.assign(B_data.size(), static_cast<DataT>(1.f));const float increment = p.precision == ov::element::bf16 ? 0.0546875f : 0.195068359375f;- x_data.assign(x_data.size(), static_cast<T>(increment));- C_data.assign(C_data.size(), static_cast<T>(1.f));- state_data.assign(state_data.size(), static_cast<T>(0.f));+ x_data.assign(x_data.size(), static_cast<DataT>(increment));+ C_data.assign(C_data.size(), static_cast<DataT>(1.f));+ state_data.assign(state_data.size(), static_cast<StateT>(0.f));}const auto set_non_empty = [](const memory::ptr& mem, const auto& values) {@@ -240,9 +248,9 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_seleset_non_empty(x_mem, x_data);set_non_empty(C_mem, C_data);if (p.padded_layouts && !state_data.empty()) {- cldnn::mem_lock<T, mem_lock_type::write> state_ptr(state_mem, get_test_stream());+ cldnn::mem_lock<StateT, mem_lock_type::write> state_ptr(state_mem, get_test_stream());for (size_t i = 0; i < state_ptr.size(); i++)- state_ptr[i] = T{};+ state_ptr[i] = StateT{};for (int32_t block = 0; block < total_blocks; block++) {for (int32_t h = 0; h < p.num_heads; h++) {for (int32_t dim = 0; dim < p.head_dim; dim++) {@@ -280,7 +288,7 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_seleconst auto BC_input_layout = p.dynamic_shapes ? layout{ov::PartialShape{-1, p.num_groups, p.state_size}, data_type, format::bfyx} : BC_layout;const auto x_input_layout = p.dynamic_shapes ? layout{ov::PartialShape{-1, p.num_heads, p.head_dim}, data_type, format::bfyx} : x_layout;const auto state_input_layout =- p.dynamic_shapes ? layout{ov::PartialShape{-1, p.num_heads, p.head_dim, p.state_size}, data_type, format::bfyx} : state_memory_layout;+ p.dynamic_shapes ? layout{ov::PartialShape{-1, p.num_heads, p.head_dim, p.state_size}, state_data_type, format::bfyx} : state_memory_layout;const auto dynamic_index_layout = layout{ov::PartialShape{-1}, index_data_type, format::bfyx};const auto index_vec_input_layout = p.dynamic_shapes ? dynamic_index_layout : index_vec_layout;const auto block_indices_input_layout = p.dynamic_shapes ? dynamic_index_layout : block_indices_layout;@@ -364,13 +372,17 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_selenetwork->set_input_data("num_processed_tokens", processed_mem);network->set_input_data("cache_interval", interval_mem);- std::vector<T> ref_output;+ std::vector<DataT> ref_output;auto ref_state = state_data;- const float tol = p.precision == ov::element::f32 ? 2e-4f : (p.precision == ov::element::bf16 ? 0.08f : 0.03f);+ const auto tolerance_for = [](const ov::element::Type& precision) {+ return precision == ov::element::bf16 ? 0.08f : (precision == ov::element::f16 ? 0.03f : 2e-4f);+ };+ const float output_abs_tolerance = tolerance_for(p.precision);+ const float state_tolerance = tolerance_for(state_precision);for (int32_t iteration = 0; iteration < p.iterations; iteration++) {auto outputs = network->execute();if (p.invalid_metadata != 0) {- ref_output.assign(static_cast<size_t>(tokens) * p.num_heads * p.head_dim, T{});+ ref_output.assign(static_cast<size_t>(tokens) * p.num_heads * p.head_dim, DataT{});} else {run_reference(A_data,dt_data,@@ -393,23 +405,25 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_seleif (!ref_output.empty()) {auto out_mem = outputs.at("output").get_memory();ASSERT_NE(out_mem, nullptr);- cldnn::mem_lock<T, mem_lock_type::read> out_ptr(out_mem, get_test_stream());+ cldnn::mem_lock<DataT, mem_lock_type::read> out_ptr(out_mem, get_test_stream());for (size_t i = 0; i < ref_output.size(); i++) {const float reference = static_cast<float>(ref_output[i]);- const float output_tol = p.precision == ov::element::f32 ? std::max(tol, std::abs(reference) * p.relative_output_tolerance) : tol;- ASSERT_NEAR(static_cast<float>(out_ptr[i]), reference, output_tol) << "iteration=" << iteration << ", output idx=" << i;+ const float output_tolerance = p.precision == ov::element::f32+ ? std::max(output_abs_tolerance, std::abs(reference) * p.relative_output_tolerance)+ : output_abs_tolerance;+ ASSERT_NEAR(static_cast<float>(out_ptr[i]), reference, output_tolerance) << "iteration=" << iteration << ", output idx=" << i;}}if (!ref_state.empty()) {- cldnn::mem_lock<T, mem_lock_type::read> state_ptr(state_mem, get_test_stream());+ cldnn::mem_lock<StateT, mem_lock_type::read> state_ptr(state_mem, get_test_stream());for (int32_t block = 0; block < total_blocks; block++) {for (int32_t h = 0; h < p.num_heads; h++) {for (int32_t dim = 0; dim < p.head_dim; dim++) {for (int32_t n = 0; n < p.state_size; n++) {const auto logical_idx = ((block * p.num_heads + h) * p.head_dim + dim) * p.state_size + n;const auto physical_idx = state_offset(block, h, dim, n);- ASSERT_NEAR(static_cast<float>(state_ptr[physical_idx]), static_cast<float>(ref_state[logical_idx]), tol)+ ASSERT_NEAR(static_cast<float>(state_ptr[physical_idx]), static_cast<float>(ref_state[logical_idx]), state_tolerance)<< "iteration=" << iteration << ", state idx=" << logical_idx;}}@@ -419,13 +433,25 @@ struct paged_selective_ssm_gpu_test : public ::testing::TestWithParam<paged_sele}}+ template <typename DataT>+ void execute_with_data_type(const paged_selective_ssm_test_params& p) {+ const auto state_precision = p.state_precision == ov::element::dynamic ? p.precision : p.state_precision;+ if (state_precision == ov::element::f16) {+ execute_t<DataT, ov::float16>(p);+ } else if (state_precision == ov::element::bf16) {+ execute_t<DataT, ov::bfloat16>(p);+ } else {+ execute_t<DataT, float>(p);+ }+ }+void execute(const paged_selective_ssm_test_params& p) {if (p.precision == ov::element::f16) {- execute_t<ov::float16>(p);+ execute_with_data_type<ov::float16>(p);} else if (p.precision == ov::element::bf16) {- execute_t<ov::bfloat16>(p);+ execute_with_data_type<ov::bfloat16>(p);} else {- execute_t<float>(p);+ execute_with_data_type<float>(p);}}};@@ -443,7 +469,21 @@ 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},+ // The data path and recurrent state have independent floating-point types. A state size below the+ // subgroup width exercises the universal optimized path without depending on its internal kernel name.+ with_state_precision(paged_selective_ssm_test_params{{3, 2}, {1, 2}, {2, 0}, 4, 2, 8, 8, ov::element::f32, ov::element::i32, false}, ov::element::bf16),+ with_state_precision(paged_selective_ssm_test_params{{3, 2}, {1, 2}, {2, 0}, 4, 2, 8, 8, ov::element::bf16, ov::element::i32, false}, ov::element::f16),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),+ expect_impl(with_state_precision(paged_selective_ssm_test_params{{8}, {0}, {8}, 64, 1, 64, 128, ov::element::f32, ov::element::i32, false},+ ov::element::f16),+ expected_ssm_impl::jit),+ // Non-prefix-cached GenAI decode uses interval zero and aliases the read block with the final live-state write.+ // Multiple executions ensure that every decode step consumes the state produced by the preceding one.+ expect_impl(+ with_state_precision(+ paged_selective_ssm_test_params{{1}, {0}, {0}, 64, 1, 64, 128, ov::element::f32, ov::element::i32, true, {true}, false, false, false, 4},+ ov::element::f16),+ 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),@@ -460,8 +500,8 @@ INSTANTIATE_TEST_SUITE_P(paged_selective_ssm_test_params{{5}, {3}, {2}, 4, 2, 5, 17, ov::element::f16, ov::element::i32, false, {true}, true},paged_selective_ssm_test_params{{1}, {4}, {2}, 4, 2, 5, 17, ov::element::f32, ov::element::i64, false, {false}, true},paged_selective_ssm_test_params{{1}, {3}, {2}, 4, 2, 5, 17, ov::element::bf16, ov::element::i64, false, {true}, true},- // Disabled cache must leave every state block unchanged.- paged_selective_ssm_test_params{{4, 3}, {9, 2}, {0, -3}, 4, 2, 4, 31, ov::element::f32, ov::element::i64, false, {}, true},+ // Interval zero disables intermediate checkpoints but still persists the final live state in slot one.+ paged_selective_ssm_test_params{{4, 3}, {9, 2}, {0, -3}, 4, 2, 4, 31, ov::element::f32, ov::element::i64, false, {true, true}, true},// Empty sequences and completely empty token batches are legal no-ops.paged_selective_ssm_test_params{{0, 3, 0}, {0, 1, 7}, {2, 2, 0}, 4, 2, 4, 16, ov::element::f32, ov::element::i32, false, {true, false, false}, true},paged_selective_ssm_test_params{{0}, {0}, {2}, 2, 1, 2, 8, ov::element::f32, ov::element::i32, true, {true}, false},@@ -471,11 +511,15 @@ INSTANTIATE_TEST_SUITE_P(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},+ with_state_precision(+ 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},+ ov::element::f32),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},+ expect_impl(paged_selective_ssm_test_params{{3072}, {0}, {3072}, 2, 1, 64, 128, ov::element::f16, ov::element::i32, true, {}, false, true},+ expected_ssm_impl::jit),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},@@ -487,6 +531,10 @@ INSTANTIATE_TEST_SUITE_P(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),+ expect_impl(with_state_precision(+ paged_selective_ssm_test_params{{3}, {0}, {2}, 2, 1, 4, 64, ov::element::f16, ov::element::i64, false, {}, false, false, false, 2},+ ov::element::bf16),+ 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},@@ -510,6 +558,9 @@ INSTANTIATE_TEST_SUITE_P(0,false,2e-4f},- paged_selective_ssm_test_params{{2}, {0}, {2}, 1, 1, 1, large_state_size, ov::element::f16, ov::element::i32, true, {}, false, true}));+ paged_selective_ssm_test_params{{2}, {0}, {2}, 1, 1, 1, large_state_size, ov::element::f16, ov::element::i32, true, {}, false, true},+ with_state_precision(+ paged_selective_ssm_test_params{{2}, {0}, {2}, 1, 1, 1, large_state_size, ov::element::f16, ov::element::i32, true, {}, false, true},+ ov::element::bf16)));} // namespace