Skip to content

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

[GPU] Add BF16 support for SDPA with vectorized optimizations (#37179)

Prithviraj-R9c45a2c4fa1e
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl
index 53f256a505a..38641fde629 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl
+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/options.inl
@@ -22,7 +22,7 @@ OV_CONFIG_RELEASE_OPTION(ov::device, id, "0", "ID of the current device")
OV_CONFIG_RELEASE_OPTION(ov, cache_dir, "", "Directory where model cache can be stored. Caching is disabled if empty")
OV_CONFIG_RELEASE_OPTION(ov, num_streams, 1, "Number of streams to be used for inference")
OV_CONFIG_RELEASE_OPTION(ov, compilation_num_threads, std::max(1, static_cast<int>(std::thread::hardware_concurrency())), "Max number of CPU threads used for model compilation for the stages that supports parallelism")
-OV_CONFIG_RELEASE_OPTION(ov::hint, inference_precision, ov::element::f16, "Model floating-point inference precision. Supported values: { f16, f32, dynamic }", [](ov::element::Type t) { return t == ov::element::f16 || t == ov::element::f32 || t == ov::element::dynamic; })
+OV_CONFIG_RELEASE_OPTION(ov::hint, inference_precision, ov::element::f16, "Model floating-point inference precision. Supported values: { f16, f32, bf16, dynamic }", [](ov::element::Type t) { return t == ov::element::f16 || t == ov::element::f32 || t == ov::element::bf16 || t == ov::element::dynamic; })
OV_CONFIG_RELEASE_OPTION(ov::hint, model_priority, ov::hint::Priority::MEDIUM, "High-level hint that defines the priority of the model. It may impact number of threads used for model compilation and inference as well as device queue settings")
OV_CONFIG_RELEASE_OPTION(ov::hint, performance_mode, ov::hint::PerformanceMode::LATENCY, "High-level hint that defines target model inference mode. It may impact number of streams, auto batching, etc")
OV_CONFIG_RELEASE_OPTION(ov::hint, execution_mode, ov::hint::ExecutionMode::PERFORMANCE, "High-level hint that defines the most important metric for the model. Performance mode allows unsafe optimizations that may reduce the model accuracy")
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/kv_cache.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/kv_cache.cpp
index 454bfbffcf5..7d416e01cb4 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/kv_cache.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/kv_cache.cpp
@@ -697,7 +697,7 @@ struct kv_cache_impl : multi_stage_primitive<kv_cache> {
namespace detail {
attach_kv_cache_impl::attach_kv_cache_impl() {
- auto types = { data_types::i8, data_types::f16, data_types::f32 };
+ auto types = { data_types::i8, data_types::f16, data_types::f32, data_types::bf16 };
auto formats = { format::bfyx };
implementation_map<kv_cache>::add(impl_types::ocl,
shape_types::dynamic_shape,
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp
index b866d0de69f..ad6d34daa06 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_micro.cpp
@@ -49,6 +49,8 @@ micro::Type convert_type(ov::element::Type t) {
return micro::Type::f32;
case ov::element::f16:
return micro::Type::f16;
+ case ov::element::bf16:
+ return micro::Type::bf16;
case ov::element::i8:
return micro::Type::s8;
case ov::element::u8:
@@ -1600,7 +1602,7 @@ void SDPAMicroGenerator::init_microkernels(const kernel_impl_params& params,
problem.Ta_ext = convert_type(kv_cache_precision);
}
- problem.Ta = problem.Tb = micro::Type::f16;
+ problem.Ta = problem.Tb = (Q.data_type == ov::element::bf16) ? micro::Type::bf16 : micro::Type::f16;
problem.Tc = problem.Tc_ext = micro::Type::f32;
problem.Ts = problem.Tc;
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_opt.cpp
index 151a77c8e50..0527de614f7 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_opt.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_gen_opt.cpp
@@ -31,6 +31,7 @@ JitConstants SDPAOptGeneratorBase::get_jit_constants_base(const kernel_impl_para
constexpr ov::element::Type softmax_accumulator_type = ov::element::f32;
jit.add(make_type_jit_constants("SOFTMAX_ACCUMULATOR", softmax_accumulator_type));
+
constexpr size_t subgroup_size = 16;
jit.make("SUBGROUP_SIZE", subgroup_size);
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.hpp
index fdf58697446..c95e708559a 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.hpp
@@ -33,10 +33,12 @@ struct SDPAOpt : public ImplementationManager {
static constexpr std::array supported_q_types = {
ov::element::f32,
ov::element::f16,
+ ov::element::bf16,
};
static constexpr std::array supported_kv_types = {
ov::element::f32,
ov::element::f16,
+ ov::element::bf16,
ov::element::i8,
};
const auto& q_layout = node.get_input_layout(ScaledDotProductAttentionInputIdx::QUERY);
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.cpp
index 408f37a7a34..2b75c857d05 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.cpp
@@ -17,7 +17,11 @@ namespace ov::intel_gpu::ocl {
namespace {
ov::element::Type get_accumulator_type(const kernel_impl_params& params) {
- return params.get_input_layout(0).data_type;
+ auto input_dt = params.get_input_layout(0).data_type;
+ // Use f32 accumulator for bf16 inputs to maintain precision
+ if (input_dt == ov::element::bf16)
+ return ov::element::f32;
+ return input_dt;
}
class SDPARefGenerator : public SDPABase {
@@ -149,15 +153,16 @@ public:
auto desc = params.typed_desc<scaled_dot_product_attention>();
const auto& q_l = params.input_layouts[0];
+ const auto acc_type = get_accumulator_type(params);
if (!params.is_dynamic()) {
const auto& k_l = params.input_layouts[1];
const auto& q_shape = q_l.get_shape();
const auto& k_shape = k_l.get_shape();
const size_t buf_size = q_l.count() / q_shape[desc->input_q_transpose_order[3]] * k_shape[desc->input_k_transpose_order[2]];
- internal_buffers.emplace_back(buf_size, q_l.data_type);
+ internal_buffers.emplace_back(buf_size, acc_type);
} else {
- internal_buffers.emplace_back(1, q_l.data_type);
+ internal_buffers.emplace_back(1, acc_type);
}
return internal_buffers;
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.hpp
index 59f325f7d84..98e2315f569 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_ref.hpp
@@ -21,10 +21,12 @@ struct SDPARef : public ImplementationManager {
static constexpr std::array supported_q_types = {
ov::element::f32,
ov::element::f16,
+ ov::element::bf16,
};
static constexpr std::array supported_kv_types = {
ov::element::f32,
ov::element::f16,
+ ov::element::bf16,
ov::element::i8,
};
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.cl
index bb5b491bb4c..f2c05489148 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_micro.cl
@@ -16,8 +16,17 @@
#include "include/batch_headers/generic_vector_ops.cl"
#include "include/batch_headers/sdpa_utils.cl"
+#include "include/batch_headers/bf16_utils.cl"
#include "include/batch_headers/tile_ops.cl"
+#if INPUT0_IS_BF16
+#define KV_SLM_T ushort
+#define tile_copy_S_to_kv(t, t_new) tile_copy_to_bf16x2(t, t_new)
+#else
+#define KV_SLM_T half
+#define tile_copy_S_to_kv(t, t_new) tile_copy_to_half2(t, t_new)
+#endif
+
/* The quantization parameter may be unique for each token/element */
#define QUANTIZE_2D 2
@@ -80,11 +89,23 @@ DECLARE_2D_TILE_COPY_REBLOCK(a_tile_type, SUBGROUP_SIZE, ugemm_vs_c_type_block0,
ugemm_vs_c_type_block1, ugemm_vs_c_type_nblock0,
ugemm_vs_c_type_nblock1, a_tile_type_half, SUBGROUP_SIZE,
ugemm_vs_sg_tile_m, 1, 1, ugemm_vs_sg_tile_n)
+#if INPUT0_IS_BF16
+DECLARE_2D_TILE_COPY_REBLOCK_TO_BF16BITS(a_tile_type, SUBGROUP_SIZE, ugemm_vs_c_type_block0,
+ ugemm_vs_c_type_block1, ugemm_vs_c_type_nblock0,
+ ugemm_vs_c_type_nblock1, a_tile_type_half, SUBGROUP_SIZE,
+ ugemm_vs_sg_tile_m, 1, 1, ugemm_vs_sg_tile_n)
+#endif
#else
DECLARE_2D_TILE_COPY_REBLOCK(a_tile_type, SUBGROUP_SIZE, ugemm_vs_c_type_block0,
ugemm_vs_c_type_block1, ugemm_vs_c_type_nblock0,
ugemm_vs_c_type_nblock1, a_tile_type_half, SUBGROUP_SIZE,
ugemm_vs_sg_tile_m, 8, 1, ugemm_vs_sg_tile_n / 8)
+#if INPUT0_IS_BF16
+DECLARE_2D_TILE_COPY_REBLOCK_TO_BF16BITS(a_tile_type, SUBGROUP_SIZE, ugemm_vs_c_type_block0,
+ ugemm_vs_c_type_block1, ugemm_vs_c_type_nblock0,
+ ugemm_vs_c_type_nblock1, a_tile_type_half, SUBGROUP_SIZE,
+ ugemm_vs_sg_tile_m, 8, 1, ugemm_vs_sg_tile_n / 8)
+#endif
#endif
DECLARE_2D_TILE_VREDUCE(s_tile_type, SUBGROUP_SIZE, ugemm_kq_c_type_block0,
@@ -310,7 +331,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
uint ldvc = HEAD_SIZE * KV_HEADS_NUM + INPUT2_PAD_BEFORE_FEATURE_NUM + INPUT2_PAD_AFTER_FEATURE_NUM;
#if IS_KV_COMPRESSED_PA
#if IS_INT4_KV_CACHE
- // INT4 K BY_CHANNEL: scale stride = ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE bytes / 2 in f16 elements
+ // INT4 BY_CHANNEL: scale stride = ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE bytes / 2 in f16 elements
uint ldkq = ADJUSTED_PAGED_ATTENTION_BLOCK_SIZE / 2;
// INT4 V per-token: scale stride = ADJUSTED_V_HEAD_SIZE bytes / 2 in f16 elements
uint ldvq = ADJUSTED_V_HEAD_SIZE / 2;
@@ -357,8 +378,8 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
local char slm[Q_slm_size + S_slm_size + S_sum_slm_size + S_max_slm_size
+ ugemm_slm_size];
- local half *Q_slm = (local half *)&slm[0];
- local half *S_slm = (local half *)&slm[Q_slm_size];
+ local KV_SLM_T *Q_slm = (local KV_SLM_T *)&slm[0];
+ local KV_SLM_T *S_slm = (local KV_SLM_T *)&slm[Q_slm_size];
local float *S_sum_slm = (local float *)&slm[Q_slm_size + S_slm_size];
local float *S_max_slm
= (local float *)&slm[Q_slm_size + S_slm_size + S_sum_slm_size];
@@ -946,7 +967,13 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
tile_elementwise(S_tile, mask_scale_op);
#elif WITH_ATTN_MASK
mask_tile_type_float mask_tile_float;
+#if INPUT0_IS_BF16
+ // Mask buffer holds bf16 values but the kernel reads it as half*;
+ // reinterpret the 16-bit values as bf16 bits when converting to float.
+ tile_copy_bf16bits_to_float(mask_tile, mask_tile_float);
+#else
tile_copy(mask_tile, mask_tile_float);
+#endif
#ifdef LOG_2_E_MUL_SCALE
#define unscale(x) ((x)*iscale)
tile_elementwise(mask_tile_float, unscale);
@@ -1192,7 +1219,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
}
#endif
- /* Convert to half, VNNI format */
+ /* Convert to KV compute type (half or bf16), VNNI format */
s_tile_type_half2 S_tile_half2;
#ifdef PA_INTEGRITY_CHECK // CODE FOR DEBUGGING
/* Snapshot softmax'd S to S_check_slm before packing to VNNI.
@@ -1219,7 +1246,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
}
}
#endif
- tile_copy_to_half2(S_tile, S_tile_half2);
+ tile_copy_S_to_kv(S_tile, S_tile_half2);
/* Store to SLM, in packed format */
tile_store_t_sys_src2(S_tile_half2, (local uint *)S_slm,
@@ -1347,7 +1374,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
}
#endif
uint s_block_num = kb0 / PAGED_ATTENTION_BLOCK_SIZE;
- local half *Sb0 = S_slm + s_block_num * ugemm_kq_sg_tile_m * ugemm_kq_sg_tile_n;
+ local KV_SLM_T *Sb0 = S_slm + s_block_num * ugemm_kq_sg_tile_m * ugemm_kq_sg_tile_n;
uint v_block_num = (k0 + kb0) / PAGED_ATTENTION_BLOCK_SIZE;
global VAL_DATA_T *Vb0 = V + KV_HEADS_NUM * ADJUSTED_V_HEAD_SIZE * PAGED_ATTENTION_BLOCK_SIZE * block_indices[base_block_index + v_block_num];
int kb_chunk = min(k_chunk - kb0, PAGED_ATTENTION_BLOCK_SIZE);
@@ -1438,7 +1465,7 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
for (; kb0 < k_chunk; kb0 += k_chunk) {
global QRY_DATA_T *Vb0 = Vc + ldvc * (k0 + kb0 - past_lens[gws_mapping]);
uint s_block_num = kb0 / PAGED_ATTENTION_BLOCK_SIZE;
- local half *Sb0 = S_slm + s_block_num * ugemm_kq_sg_tile_m * ugemm_kq_sg_tile_n;
+ local KV_SLM_T *Sb0 = S_slm + s_block_num * ugemm_kq_sg_tile_m * ugemm_kq_sg_tile_n;
int kb_chunk = k_chunk - kb0;
a_tile_type A_tile1 = ugemm_vcs(
@@ -1508,9 +1535,13 @@ KERNEL(micro_sdpa)(OPTIONAL_SHAPE_INFO_ARG
tile_elementwise(A_scale_tile, native_vrecip);
tile_hbroadcast_mul(&A_tile, A_scale_tile);
- /* Convert to half precision and store */
+ /* Convert and store */
a_tile_type_half A_tile_half;
+#if INPUT0_IS_BF16
+ tile_copy_reblock_to_bf16bits(A_tile, &A_tile_half);
+#else
tile_copy_reblock(A_tile, &A_tile_half);
+#endif
uint sg_i0_vs = sg_i_vs * ugemm_vs_sg_tile_m;
uint sg_j0_vs = sg_j_vs * ugemm_vs_sg_tile_n + wg_j0;
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.cl
index d2dbe58ec77..f8a201fa4d3 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_opt.cl
@@ -4,6 +4,7 @@
#include "include/batch_headers/fetch_data.cl"
#include "include/batch_headers/common.cl"
+#include "include/batch_headers/bf16_utils.cl"
#include "include/batch_headers/int4_utils.cl"
#include "include/batch_headers/sub_group_block_read.cl"
#include "include/batch_headers/sub_group_block_write.cl"
@@ -125,6 +126,7 @@ inline uint FUNC(get_bt_index_value)(OPTIONAL_SHAPE_INFO_ARG uint b, uint f, uin
#define OUTPUT_BLOCK_READ(ptr, offset) BLOCK_READN(OUTPUT_TYPE, 1, ptr, offset)
#define OUTPUT_BLOCK_WRITE(ptr, offset, val) BLOCK_WRITEN(OUTPUT_TYPE, 1, ptr, offset, val)
+
#if IS_INT4_COMPRESSED
#define VALUE_BLOCK_READ(ptr, offset) ((ptr)[(offset) + sglid])
#else
@@ -220,7 +222,7 @@ KERNEL(sdpa_opt)(
: (SOURCE_SEQ_LEN - partition_idx * SEQ_LEN_PARTITION_SIZE);
// SLM for query inputs
- __local INPUT0_TYPE query_local[K_HEAD_SIZE * TARGET_SEQ_LEN_BLOCK_SIZE];
+ __local INPUT0_COMPUTE_TYPE query_local[K_HEAD_SIZE * TARGET_SEQ_LEN_BLOCK_SIZE];
// SLM for intermediate QK results
__local SOFTMAX_ACCUMULATOR_TYPE qk_local[SEQ_LEN_PARTITION_SIZE * TARGET_SEQ_LEN_BLOCK_SIZE];
// SLM buffers for SoftMax calculation and qk_max/qk_sums results aggregation across all WG
@@ -237,11 +239,11 @@ KERNEL(sdpa_opt)(
{
// Gemm1 calculation
#if HAS_SCALE_INPUT
- const OUTPUT_TYPE scale_val = *scale;
+ const OUTPUT_COMPUTE_TYPE scale_val = TO_OUTPUT_COMPUTE_TYPE(*scale);
#elif defined(STATIC_SCALE_VALUE)
- const OUTPUT_TYPE scale_val = TO_OUTPUT_TYPE(STATIC_SCALE_VALUE);
+ const OUTPUT_COMPUTE_TYPE scale_val = TO_OUTPUT_COMPUTE_TYPE(STATIC_SCALE_VALUE);
#else
- const OUTPUT_TYPE scale_val = OUTPUT_VAL_ONE / sqrt(TO_OUTPUT_TYPE(V_HEAD_SIZE));
+ const OUTPUT_COMPUTE_TYPE scale_val = OUTPUT_VAL_ONE / sqrt(TO_OUTPUT_COMPUTE_TYPE(V_HEAD_SIZE));
#endif
#if K_HEAD_SIZE > V_HEAD_SIZE
for (uint block_idx = sgid; block_idx < K_HEAD_SIZE / SUBGROUP_SIZE; block_idx++)
@@ -270,7 +272,7 @@ KERNEL(sdpa_opt)(
#define QUERY_BLOCK_SIZE 1
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, QUERY_BLOCK_SIZE, query_input, query_offset);
- query_local[query_local_offset] = val * scale_val;
+ query_local[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_local_offset += QUERY_STEP_LOCAL;
query_offset += query_pitch;
}
@@ -331,8 +333,8 @@ KERNEL(sdpa_opt)(
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
uint query_offset = seq_idx * K_HEAD_SIZE + head_idx_index;
- INPUT0_TYPE q_val0 = query_local[query_offset + 2 * sglid];
- INPUT0_TYPE q_val1 = query_local[query_offset + 2 * sglid + 1];
+ INPUT0_COMPUTE_TYPE q_val0 = query_local[query_offset + 2 * sglid];
+ INPUT0_COMPUTE_TYPE q_val1 = query_local[query_offset + 2 * sglid + 1];
acc[seq_idx] = mad(TO_SOFTMAX_ACCUMULATOR_TYPE(q_val0), TO_SOFTMAX_ACCUMULATOR_TYPE(key_val0), acc[seq_idx]);
acc[seq_idx] = mad(TO_SOFTMAX_ACCUMULATOR_TYPE(q_val1), TO_SOFTMAX_ACCUMULATOR_TYPE(key_val1), acc[seq_idx]);
}
@@ -355,8 +357,8 @@ KERNEL(sdpa_opt)(
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
uint query_offset = seq_idx * K_HEAD_SIZE + head_idx_index;
- INPUT0_TYPE q_val0 = query_local[query_offset + 2 * sglid];
- INPUT0_TYPE q_val1 = query_local[query_offset + 2 * sglid + 1];
+ INPUT0_COMPUTE_TYPE q_val0 = query_local[query_offset + 2 * sglid];
+ INPUT0_COMPUTE_TYPE q_val1 = query_local[query_offset + 2 * sglid + 1];
acc[seq_idx] = mad(TO_SOFTMAX_ACCUMULATOR_TYPE(q_val0), TO_SOFTMAX_ACCUMULATOR_TYPE(key_val0), acc[seq_idx]);
acc[seq_idx] = mad(TO_SOFTMAX_ACCUMULATOR_TYPE(q_val1), TO_SOFTMAX_ACCUMULATOR_TYPE(key_val1), acc[seq_idx]);
}
@@ -377,7 +379,7 @@ KERNEL(sdpa_opt)(
#define KEY_BLOCK MAKE_VECTOR_TYPE(INPUT1_TYPE, KEY_BLOCK_SIZE)
#define KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KEY_COMPRESSION_SCALE_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
KEY_BLOCK key_vec_packed = KEY_BLOCK_READ(key_input, key_offset + head_idx_index);
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
@@ -385,7 +387,10 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
KEY_BLOCK_UNCOMPRESSED key_vals = (TO_KEY_BLOCK_UNCOMPRESSED_TYPE(key_vec_packed)) * comp_scale;
#else
- KEY_BLOCK key_vals = key_vec_packed;
+ MAKE_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, KEY_BLOCK_SIZE) key_vals;
+ unroll_for(uint _kb = 0; _kb < KEY_BLOCK_SIZE; _kb++) {
+ key_vals[_kb] = DECODE_INPUT1_COMPUTE_TYPE(key_vec_packed[_kb]);
+ }
#endif
uint query_offset = head_idx_index + sglid;
@@ -409,7 +414,7 @@ KERNEL(sdpa_opt)(
#define KEY_BLOCK MAKE_VECTOR_TYPE(INPUT1_TYPE, KEY_BLOCK_SIZE)
#define KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KEY_COMPRESSION_SCALE_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
KEY_BLOCK key_vec_packed = KEY_BLOCK_READ(key_input, key_offset + head_idx_index);
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
@@ -417,7 +422,10 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
KEY_BLOCK_UNCOMPRESSED key_vals = (TO_KEY_BLOCK_UNCOMPRESSED_TYPE(key_vec_packed)) * comp_scale;
#else
- KEY_BLOCK key_vals = key_vec_packed;
+ MAKE_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, KEY_BLOCK_SIZE) key_vals;
+ unroll_for(uint _kb = 0; _kb < KEY_BLOCK_SIZE; _kb++) {
+ key_vals[_kb] = DECODE_INPUT1_COMPUTE_TYPE(key_vec_packed[_kb]);
+ }
#endif
uint query_offset = head_idx_index + sglid;
@@ -441,7 +449,7 @@ KERNEL(sdpa_opt)(
#define KEY_BLOCK MAKE_VECTOR_TYPE(INPUT1_TYPE, KEY_BLOCK_SIZE)
#define KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KEY_COMPRESSION_SCALE_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
KEY_BLOCK key_vec_packed = KEY_BLOCK_READ(key_input, key_offset + head_idx_index);
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
@@ -449,7 +457,10 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
KEY_BLOCK_UNCOMPRESSED key_vals = (TO_KEY_BLOCK_UNCOMPRESSED_TYPE(key_vec_packed)) * comp_scale;
#else
- KEY_BLOCK key_vals = key_vec_packed;
+ MAKE_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, KEY_BLOCK_SIZE) key_vals;
+ unroll_for(uint _kb = 0; _kb < KEY_BLOCK_SIZE; _kb++) {
+ key_vals[_kb] = DECODE_INPUT1_COMPUTE_TYPE(key_vec_packed[_kb]);
+ }
#endif
uint query_offset = head_idx_index + sglid;
@@ -473,7 +484,7 @@ KERNEL(sdpa_opt)(
#define KEY_BLOCK MAKE_VECTOR_TYPE(INPUT1_TYPE, KEY_BLOCK_SIZE)
#define KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KEY_COMPRESSION_SCALE_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
KEY_BLOCK key_vec_packed = KEY_BLOCK_READ(key_input, key_offset + head_idx_index);
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
@@ -481,7 +492,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
KEY_BLOCK_UNCOMPRESSED key_vals = (TO_KEY_BLOCK_UNCOMPRESSED_TYPE(key_vec_packed)) * comp_scale;
#else
- KEY_BLOCK key_vals = key_vec_packed;
+ INPUT1_COMPUTE_TYPE key_vals = DECODE_INPUT1_COMPUTE_TYPE(key_vec_packed);
#endif
uint query_offset = head_idx_index + sglid;
@@ -522,7 +533,7 @@ KERNEL(sdpa_opt)(
qk_val[seq_idx] += INPUT0_VAL_MIN;
#elif !IS_CAUSAL && HAS_ATTN_MASK_INPUT
const uint attn_mask_offset = INPUT3_GET_INDEX_SAFE(b0_idx, b1_idx, target_seq_idx + seq_idx, start_partition_idx + seq_len);
- INPUT3_TYPE mask_val = attn_mask[attn_mask_offset];
+ INPUT3_COMPUTE_TYPE mask_val = DECODE_INPUT3_COMPUTE_TYPE(attn_mask[attn_mask_offset]);
#ifdef CLAMP_ATTN_MASK_INPUT
// Conditionally clamp attention mask when attention mask differs from SOFTMAX_ACCUMULATOR_TYPE(f32)
mask_val = INPUT3_MAX_FUNC(mask_val, INPUT3_VAL_MIN);
@@ -615,7 +626,7 @@ KERNEL(sdpa_opt)(
if (local_data_idx < partition_seq_len) {
for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
SOFTMAX_ACCUMULATOR_TYPE qk_new = native_exp(TO_SOFTMAX_ACCUMULATOR_TYPE(qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx]) - qk_max[seq_idx]);
- qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx] = TO_OUTPUT_TYPE(qk_new);
+ qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx] = TO_OUTPUT_COMPUTE_TYPE(qk_new);
exp_sum[seq_idx] += qk_new;
}
@@ -686,7 +697,7 @@ KERNEL(sdpa_opt)(
if (local_data_idx < partition_seq_len) {
for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
SOFTMAX_ACCUMULATOR_TYPE qk_new = TO_SOFTMAX_ACCUMULATOR_TYPE(qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx]) / exp_sum[seq_idx];
- qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx] = TO_OUTPUT_TYPE(qk_new);
+ qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + local_data_idx] = TO_OUTPUT_COMPUTE_TYPE(qk_new);
}
}
}
@@ -714,7 +725,7 @@ KERNEL(sdpa_opt)(
{
// Gemm2 calculation
- OUTPUT_TYPE acc[TARGET_SEQ_LEN_BLOCK_SIZE] = {OUTPUT_VAL_ZERO};
+ OUTPUT_COMPUTE_TYPE acc[TARGET_SEQ_LEN_BLOCK_SIZE] = {OUTPUT_VAL_ZERO};
#ifndef BEAM_TABLE_TYPE
#ifdef INPUT2_DIMS_ORDER
uint value_offset = FUNC_CALL(get_input2_index)(OPTIONAL_SHAPE_INFO_TENSOR b0_idx, b1_idx, 0, 0, 0, 0);
@@ -785,9 +796,9 @@ KERNEL(sdpa_opt)(
#endif
#endif
- OUTPUT_TYPE qk_val[TARGET_SEQ_LEN_BLOCK_SIZE];
+ OUTPUT_COMPUTE_TYPE qk_val[TARGET_SEQ_LEN_BLOCK_SIZE];
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
- qk_val[seq_idx] = qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + seq_len * SUBGROUP_SIZE + sglid];
+ qk_val[seq_idx] = TO_OUTPUT_COMPUTE_TYPE(qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + seq_len * SUBGROUP_SIZE + sglid]);
}
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
@@ -815,10 +826,10 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
- INPUT2_TYPE value_val = value_packed;
+ INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
- acc[seq_idx] = mad(sub_group_broadcast(qk_val[seq_idx], i), value_val, acc[seq_idx]);
+ acc[seq_idx] = mad(sub_group_broadcast(qk_val[seq_idx], i), TO_OUTPUT_COMPUTE_TYPE(value_val), acc[seq_idx]);
}
#ifndef BEAM_TABLE_TYPE
@@ -863,9 +874,9 @@ KERNEL(sdpa_opt)(
#endif
#endif
- OUTPUT_TYPE qk_val[TARGET_SEQ_LEN_BLOCK_SIZE];
+ OUTPUT_COMPUTE_TYPE qk_val[TARGET_SEQ_LEN_BLOCK_SIZE];
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
- qk_val[seq_idx] = qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + seq_len];
+ qk_val[seq_idx] = TO_OUTPUT_COMPUTE_TYPE(qk_local[seq_idx * SEQ_LEN_PARTITION_SIZE + seq_len]);
}
#if IS_INT4_COMPRESSED
@@ -887,11 +898,11 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
const VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * comp_scale);
#else
- const INPUT2_TYPE value_val = value_packed;
+ const INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
- acc[seq_idx] = mad(qk_val[seq_idx], value_val, acc[seq_idx]);
+ acc[seq_idx] = mad(qk_val[seq_idx], TO_OUTPUT_COMPUTE_TYPE(value_val), acc[seq_idx]);
}
}
@@ -926,13 +937,13 @@ KERNEL(sdpa_opt)(
(target_seq_idx + seq_idx) * (num_of_partitions * V_HEAD_SIZE) +
partition_idx * (V_HEAD_SIZE) +
head_size_idx;
- tmp_out[tmp_out_offset] = acc[seq_idx];
+ tmp_out[tmp_out_offset] = TO_OUTPUT_TYPE(acc[seq_idx]);
}
} else {
const uint seq_idx_end = 1;
for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
const uint output_offset = SDPA_OUTPUT_GET_INDEX(b0_idx, b1_idx, target_seq_idx + seq_idx, head_size_idx);
- output[output_offset] = acc[seq_idx];
+ output[output_offset] = TO_OUTPUT_TYPE(acc[seq_idx]);
}
}
#if SG_SCALE_FACTOR > 1
@@ -984,7 +995,7 @@ KERNEL(sdpa_opt)(
#define APPLY_SCALES_TO_QUERY 1
#endif
-#define MASK_VECTOR_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
+#define MASK_VECTOR_TYPE MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
inline MASK_VECTOR_TYPE FUNC(load_attn_mask)(OPTIONAL_SHAPE_INFO_ARG
uint b0_idx,
@@ -1010,13 +1021,13 @@ inline MASK_VECTOR_TYPE FUNC(load_attn_mask)(OPTIONAL_SHAPE_INFO_ARG
if (source_seq_idx + SUBGROUP_SIZE <= (uint)SOURCE_SEQ_LEN) {
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
const INPUT3_TYPE mask_val = attn_mask[attn_mask_offset + i];
- mask_vec[i] = mask_val;
+ mask_vec[i] = DECODE_INPUT3_COMPUTE_TYPE(mask_val);
}
} else {
const uint max_mask_offset = min(source_seq_idx + SUBGROUP_SIZE, (uint)SOURCE_SEQ_LEN);
for (uint i = 0; i < SUBGROUP_SIZE; i++) {
const INPUT3_TYPE mask_val = source_seq_idx + i < max_mask_offset ? attn_mask[attn_mask_offset + i] : NAN;
- mask_vec[i] = mask_val;
+ mask_vec[i] = DECODE_INPUT3_COMPUTE_TYPE(mask_val);
}
}
}
@@ -1054,9 +1065,9 @@ inline MASK_VECTOR_TYPE FUNC(load_attn_mask)(OPTIONAL_SHAPE_INFO_ARG
#endif
#if HAS_SCALE_INPUT
- const OUTPUT_TYPE scale_val = OUTPUT_VAL_ONE / *scale;
+ const OUTPUT_COMPUTE_TYPE scale_val = OUTPUT_VAL_ONE / TO_OUTPUT_COMPUTE_TYPE(*scale);
#else
- const INPUT0_TYPE scale_val = TO_INPUT0_TYPE(STATIC_SCALE_VALUE_INV);
+ const INPUT0_COMPUTE_TYPE scale_val = TO_INPUT0_COMPUTE_TYPE(STATIC_SCALE_VALUE_INV);
#endif
// Apply scale to attn_mask
@@ -1311,10 +1322,10 @@ KERNEL(sdpa_opt)(
#define sgid (uint)get_sub_group_id()
// SLM buffer for query inputs
- __local INPUT0_TYPE slm_query[K_HEAD_SIZE * TARGET_SEQ_LEN_BLOCK_SIZE];
+ __local INPUT0_COMPUTE_TYPE slm_query[K_HEAD_SIZE * TARGET_SEQ_LEN_BLOCK_SIZE];
// SLM buffer for intermediate QK results
- __local OUTPUT_TYPE slm_qk_vals[TARGET_SEQ_LEN_BLOCK_SIZE][SEQ_LEN_PARTITION_SIZE];
+ __local OUTPUT_COMPUTE_TYPE slm_qk_vals[TARGET_SEQ_LEN_BLOCK_SIZE][SEQ_LEN_PARTITION_SIZE];
// SLM buffers for SoftMax calculation and qk_max/qk_sums results aggregation across all WGs
#if IS_FLASHATTEN_V2
@@ -1414,12 +1425,12 @@ KERNEL(sdpa_opt)(
#if APPLY_SCALES_TO_QUERY
#if HAS_SCALE_INPUT
- const INPUT0_TYPE scale_val = *scale;
+ const INPUT0_COMPUTE_TYPE scale_val = TO_INPUT0_COMPUTE_TYPE(*scale);
#else
- const INPUT0_TYPE scale_val = TO_INPUT0_TYPE(STATIC_SCALE_VALUE);
+ const INPUT0_COMPUTE_TYPE scale_val = TO_INPUT0_COMPUTE_TYPE(STATIC_SCALE_VALUE);
#endif
#else
- const INPUT0_TYPE scale_val = INPUT0_VAL_ONE;
+ const INPUT0_COMPUTE_TYPE scale_val = INPUT0_VAL_ONE;
#endif
if (seq_idx_end != TARGET_SEQ_LEN_BLOCK_SIZE) {
@@ -1428,7 +1439,7 @@ KERNEL(sdpa_opt)(
for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1438,7 +1449,7 @@ KERNEL(sdpa_opt)(
if (sglid < valid_workers) {
unroll_for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
INPUT0_TYPE val = query_input[query_offset];
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1448,7 +1459,7 @@ KERNEL(sdpa_opt)(
if (k_sgid * SUBGROUP_SIZE < K_HEAD_SIZE) {
for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1459,7 +1470,7 @@ KERNEL(sdpa_opt)(
if (k_sgid < (K_HEAD_SIZE / SUBGROUP_SIZE)) {
unroll_for (uint seq_idx = 0; seq_idx < (TARGET_SEQ_LEN_BLOCK_SIZE / SG_SCALE_FACTOR); seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1469,7 +1480,7 @@ KERNEL(sdpa_opt)(
unroll_for (uint seq_idx = 0; seq_idx < (TARGET_SEQ_LEN_BLOCK_SIZE / SG_SCALE_FACTOR); seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1480,7 +1491,7 @@ KERNEL(sdpa_opt)(
unroll_for (uint seq_idx = 0; seq_idx < (TARGET_SEQ_LEN_BLOCK_SIZE / SG_SCALE_FACTOR); seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1503,7 +1514,7 @@ KERNEL(sdpa_opt)(
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
INPUT0_TYPE val = BLOCK_READN(INPUT0_TYPE, 1, query_input, query_offset);
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1513,7 +1524,7 @@ KERNEL(sdpa_opt)(
if (sglid < valid_workers) {
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
INPUT0_TYPE val = query_input[query_offset];
- slm_query[query_local_offset] = val * scale_val;
+ slm_query[query_local_offset] = DECODE_INPUT0_COMPUTE_TYPE(val) * scale_val;
query_offset += query_pitch;
query_local_offset++;
}
@@ -1536,7 +1547,7 @@ KERNEL(sdpa_opt)(
}
// Q*K calculation loop
- MAKE_VECTOR_TYPE(OUTPUT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) output_acc = OUTPUT_VAL_ZERO;
+ MAKE_VECTOR_TYPE(OUTPUT_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) output_acc = OUTPUT_VAL_ZERO;
#if IS_INT4_COMPRESSED && !defined(BEAM_TABLE_TYPE)
#ifdef INPUT1_DIMS_ORDER
@@ -1557,7 +1568,7 @@ KERNEL(sdpa_opt)(
const uint partition_seq_len = min((uint)SOURCE_SEQ_LEN - start_partition_idx, (uint)SEQ_LEN_PARTITION_SIZE);
#endif
- MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERO;
+ MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_acc = INPUT0_VAL_ZERO;
#if IS_CAUSAL
if (seq_len <= this_work_item_seq_range_temp.subgroup_max) { // keep tril i.e. m >= n
#endif
@@ -1625,7 +1636,7 @@ KERNEL(sdpa_opt)(
// Lane i holds key at head dims (hi + 2*i) and (hi + 2*i + 1).
// Load query values matching adjacent-packed head dims.
#define KEY_BLOCK_READ(ptr, offset) ((ptr)[(offset) + sglid])
- #define QUERY_VEC MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
+ #define QUERY_VEC MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
for (uint hi = 0; hi + 2 * SUBGROUP_SIZE <= K_HEAD_SIZE; hi += 2 * SUBGROUP_SIZE) {
QUERY_VEC qvec_lo, qvec_hi;
// qvec_lo[i] = query at head dim (hi + 2*i) for target token sglid
@@ -1688,7 +1699,7 @@ KERNEL(sdpa_opt)(
__attribute__((opencl_unroll_hint(1)))
for (; head_idx_index + SUBGROUP_SIZE <= K_HEAD_SIZE; head_idx_index += SUBGROUP_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, 1, ptr, offset);
- #define QUERY_VEC MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
+ #define QUERY_VEC MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
QUERY_VEC queries_vec;
uint query_local_offset = (head_idx_index * TARGET_SEQ_LEN_BLOCK_SIZE) + sglid;
@@ -1709,7 +1720,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
KEY_COMPRESSION_SCALE_TYPE key_vals = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
#else
- INPUT1_TYPE key_vals = key_packed;
+ INPUT1_COMPUTE_TYPE key_vals = DECODE_INPUT1_COMPUTE_TYPE(key_packed);
#endif
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
@@ -1735,7 +1746,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
KEY_COMPRESSION_SCALE_TYPE key_vals = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
#else
- INPUT1_TYPE key_vals = key_packed;
+ INPUT1_COMPUTE_TYPE key_vals = DECODE_INPUT1_COMPUTE_TYPE(key_packed);
#endif
unroll_for (uint i = 0; i < K_HEAD_SIZE_LEFTOVER; i++) {
qk_acc[key_row_idx] = mad(sub_group_broadcast(key_vals, i), queries_vec[i], qk_acc[key_row_idx]);
@@ -1755,7 +1766,7 @@ KERNEL(sdpa_opt)(
{
// INT4 partial block: process 2*SUBGROUP_SIZE logical head dims per iteration
#define KEY_BLOCK_READ(ptr, offset) ((ptr)[(offset) + sglid])
- #define QUERY_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
+ #define QUERY_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
const uint key_pitch_int4 = INPUT1_SIZE_X;
for (uint hi = 0; hi + 2 * SUBGROUP_SIZE <= K_HEAD_SIZE; hi += 2 * SUBGROUP_SIZE) {
QUERY_VEC_TYPE qvec_lo, qvec_hi;
@@ -1825,15 +1836,15 @@ KERNEL(sdpa_opt)(
__attribute__((opencl_unroll_hint(1)))
for (; head_idx_index + SUBGROUP_SIZE <= K_HEAD_SIZE; head_idx_index += SUBGROUP_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, 1, ptr, offset)
- #define QUERY_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
+ #define QUERY_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
#if IS_KV_COMPRESSED
#define KEY_UNPACKED_TYPE KEY_COMPRESSION_SCALE_TYPE
#define KEY_UNPACKED_VEC_TYPE MAKE_VECTOR_TYPE(KEY_COMPRESSION_SCALE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
#define TO_KEY_UNPACKED_TYPE(val) TO_KEY_COMPRESSION_SCALE_TYPE(val)
#else
- #define KEY_UNPACKED_TYPE INPUT1_TYPE
- #define KEY_UNPACKED_VEC_TYPE MAKE_VECTOR_TYPE(INPUT1_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
- #define TO_KEY_UNPACKED_TYPE(val) TO_INPUT1_TYPE(val)
+ #define KEY_UNPACKED_TYPE INPUT1_COMPUTE_TYPE
+ #define KEY_UNPACKED_VEC_TYPE MAKE_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
+ #define TO_KEY_UNPACKED_TYPE(val) DECODE_INPUT1_COMPUTE_TYPE(val)
#endif
QUERY_VEC_TYPE queries_vec;
@@ -1901,7 +1912,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
KEY_COMPRESSION_SCALE_TYPE key_val = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
#else
- INPUT1_TYPE key_val = key_packed;
+ INPUT1_COMPUTE_TYPE key_val = DECODE_INPUT1_COMPUTE_TYPE(key_packed);
#endif
unroll_for (uint i = 0; i < K_HEAD_SIZE_LEFTOVER; i++) {
qk_acc[key_row_idx] = mad(sub_group_broadcast(key_val, i), queries_vec[i], qk_acc[key_row_idx]);
@@ -1924,9 +1935,9 @@ KERNEL(sdpa_opt)(
#if !APPLY_SCALES_TO_QUERY
#if HAS_SCALE_INPUT
- const OUTPUT_TYPE scale_val = *scale;
+ const OUTPUT_COMPUTE_TYPE scale_val = TO_OUTPUT_COMPUTE_TYPE(*scale);
#else
- const OUTPUT_TYPE scale_val = TO_OUTPUT_TYPE(STATIC_SCALE_VALUE);
+ const OUTPUT_COMPUTE_TYPE scale_val = TO_OUTPUT_COMPUTE_TYPE(STATIC_SCALE_VALUE);
#endif
qk_acc[i] *= scale_val;
#endif // !APPLY_SCALES_TO_QUERY
@@ -1992,7 +2003,7 @@ KERNEL(sdpa_opt)(
SOFTMAX_ACCUMULATOR_TYPE exp_sum_new = SOFTMAX_ACCUMULATOR_VAL_ZERO;
for (uint k = sglid; k < partition_seq_len; k += SUBGROUP_SIZE) {
SOFTMAX_ACCUMULATOR_TYPE a = native_exp(TO_SOFTMAX_ACCUMULATOR_TYPE(slm_qk_vals[m][k]) - qk_max_new);
- slm_qk_vals[m][k] = TO_OUTPUT_TYPE(a);
+ slm_qk_vals[m][k] = TO_OUTPUT_COMPUTE_TYPE(a);
exp_sum_new += a;
}
exp_sum_new = sub_group_reduce_add(exp_sum_new);
@@ -2171,7 +2182,7 @@ KERNEL(sdpa_opt)(
// QK*V calculation
{
- MAKE_VECTOR_TYPE(OUTPUT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) acc_output_res = OUTPUT_VAL_ZERO;
+ MAKE_VECTOR_TYPE(OUTPUT_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) acc_output_res = OUTPUT_VAL_ZERO;
#if IS_PAGED_ATTENTION
const uint value_pitch = (V_HEAD_SIZE * NUM_KV_HEADS + INPUT2_PAD_BEFORE_FEATURE_NUM + INPUT2_PAD_AFTER_FEATURE_NUM);
#else
@@ -2240,7 +2251,7 @@ KERNEL(sdpa_opt)(
#endif
#endif
#endif
- MAKE_VECTOR_TYPE(OUTPUT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_val;
+ MAKE_VECTOR_TYPE(OUTPUT_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_val;
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
qk_val[seq_idx] = slm_qk_vals[seq_idx][seq_len + sglid];
}
@@ -2275,7 +2286,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
- INPUT2_TYPE value_val = value_packed;
+ INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
acc_output_res[seq_idx] = mad(sub_group_broadcast(qk_val[seq_idx], i), value_val, acc_output_res[seq_idx]);
@@ -2305,7 +2316,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
- INPUT2_TYPE value_val = value_packed;
+ INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
acc_output_res[seq_idx] = mad(sub_group_broadcast(qk_val[seq_idx], i), value_val, acc_output_res[seq_idx]);
@@ -2363,7 +2374,7 @@ KERNEL(sdpa_opt)(
#endif
#endif // IS_KV_COMPRESSED
- MAKE_VECTOR_TYPE(OUTPUT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_val;
+ MAKE_VECTOR_TYPE(OUTPUT_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_val;
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
qk_val[seq_idx] = slm_qk_vals[seq_idx][seq_len * SUBGROUP_SIZE + sglid];
}
@@ -2391,7 +2402,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
- INPUT2_TYPE value_val = value_packed;
+ INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
acc_output_res[seq_idx] = mad(sub_group_broadcast(qk_val[seq_idx], i), value_val, acc_output_res[seq_idx]);
@@ -2421,7 +2432,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
- INPUT2_TYPE value_val = value_packed;
+ INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
acc_output_res[seq_idx] = mad(sub_group_broadcast(qk_val[seq_idx], i), value_val, acc_output_res[seq_idx]);
@@ -2437,7 +2448,7 @@ KERNEL(sdpa_opt)(
// QK*V leftovers processing
const uint seq_len_leftovers_start = ((seq_len_end / SUBGROUP_SIZE) * SUBGROUP_SIZE);
if (seq_len_leftovers_start != seq_len_end) {
- MAKE_VECTOR_TYPE(OUTPUT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_val;
+ MAKE_VECTOR_TYPE(OUTPUT_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE) qk_val;
unroll_for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
qk_val[seq_idx] = slm_qk_vals[seq_idx][seq_len_leftovers_start+sglid];
}
@@ -2515,7 +2526,7 @@ KERNEL(sdpa_opt)(
#elif IS_KV_COMPRESSED
VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, seq_len_idx));
#else
- INPUT2_TYPE value_val = value_packed;
+ INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
for (uint seq_idx = 0; seq_idx < TARGET_SEQ_LEN_BLOCK_SIZE; seq_idx++) {
@@ -2537,7 +2548,7 @@ KERNEL(sdpa_opt)(
for (uint seq_idx = 0; seq_idx < seq_idx_end; seq_idx++) {
if (start_partition_idx > 0) {
- OUTPUT_TYPE updated_prev_res = TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * slm_update_factor[seq_idx];
+ OUTPUT_COMPUTE_TYPE updated_prev_res = TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * slm_update_factor[seq_idx];
acc_output_res[seq_idx] += updated_prev_res;
}
output_acc[seq_idx] = acc_output_res[seq_idx];
@@ -2559,7 +2570,7 @@ KERNEL(sdpa_opt)(
SOFTMAX_ACCUMULATOR_TYPE updated_total_exp_sum = updated_exp_sum_prev + updated_exp_sum_cur;
if (start_partition_idx > 0 && updated_total_exp_sum > SOFTMAX_ACCUMULATOR_VAL_ZERO) {
- OUTPUT_TYPE updated_prev_res = TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * updated_exp_sum_prev / updated_total_exp_sum;
+ OUTPUT_COMPUTE_TYPE updated_prev_res = TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * updated_exp_sum_prev / updated_total_exp_sum;
acc_output_res[seq_idx] *= updated_exp_sum_cur / updated_total_exp_sum;
acc_output_res[seq_idx] += updated_prev_res;
}
@@ -2586,7 +2597,7 @@ KERNEL(sdpa_opt)(
SOFTMAX_ACCUMULATOR_TYPE max_new = SOFTMAX_ACCUMULATOR_MAX_FUNC(max_prev, sink_val);
SOFTMAX_ACCUMULATOR_TYPE correction = native_exp(max_prev - max_new);
// Rescale output_acc (all work items do this for their own register)
- output_acc[seq_idx] = TO_OUTPUT_TYPE(TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * correction);
+ output_acc[seq_idx] = TO_OUTPUT_COMPUTE_TYPE(TO_SOFTMAX_ACCUMULATOR_TYPE(output_acc[seq_idx]) * correction);
// Update exp_sum (only one thread per seq_idx)
if (sgid == 0 && sglid == 0) {
slm_exp_sum_prev[seq_idx] = slm_exp_sum_prev[seq_idx] * correction + native_exp(sink_val - max_new);
@@ -2632,7 +2643,7 @@ KERNEL(sdpa_opt)(
output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
// Use scalar writes since block writes require alignment, but we process leftovers here
- output[output_offset + sglid] = output_acc[seq_idx];
+ output[output_offset + sglid] = TO_OUTPUT_TYPE(output_acc[seq_idx]);
output_offset += output_pitch;
}
} else if (sglid < V_HEAD_SIZE_LEFTOVER) {
@@ -2640,7 +2651,7 @@ KERNEL(sdpa_opt)(
#if IS_FLASHATTEN_V2
output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
- output[output_offset + sglid] = output_acc[seq_idx];
+ output[output_offset + sglid] = TO_OUTPUT_TYPE(output_acc[seq_idx]);
output_offset += output_pitch;
}
}
@@ -2651,7 +2662,7 @@ KERNEL(sdpa_opt)(
output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
// Use scalar writes since block writes require alignment, but we process leftovers here
- output[output_offset + sglid] = output_acc[seq_idx];
+ output[output_offset + sglid] = TO_OUTPUT_TYPE(output_acc[seq_idx]);
output_offset += output_pitch;
}
} else if (sglid < V_HEAD_SIZE_LEFTOVER) {
@@ -2659,7 +2670,7 @@ KERNEL(sdpa_opt)(
#if IS_FLASHATTEN_V2
output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
- output[output_offset + sglid] = output_acc[seq_idx];
+ output[output_offset + sglid] = TO_OUTPUT_TYPE(output_acc[seq_idx]);
output_offset += output_pitch;
}
}
@@ -2670,7 +2681,7 @@ KERNEL(sdpa_opt)(
#if IS_FLASHATTEN_V2
output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
- OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);
+ OUTPUT_BLOCK_WRITE(output, output_offset, TO_OUTPUT_TYPE(output_acc[seq_idx]));
output_offset += output_pitch;
}
} else {
@@ -2678,7 +2689,7 @@ KERNEL(sdpa_opt)(
#if IS_FLASHATTEN_V2
output_acc[seq_idx] /= slm_exp_sum_prev[seq_idx];
#endif
- OUTPUT_BLOCK_WRITE(output, output_offset, output_acc[seq_idx]);
+ OUTPUT_BLOCK_WRITE(output, output_offset, TO_OUTPUT_TYPE(output_acc[seq_idx]));
output_offset += output_pitch;
}
}
@@ -2796,7 +2807,13 @@ KERNEL(sdpa_opt_finalization_stage)(
target_seq_idx * (num_of_partitions * V_HEAD_SIZE) +
partition_idx * (V_HEAD_SIZE) + local_id;
OUTPUT_TYPE out_val = tmp_out[tmp_out_offset];
- acc += TO_SOFTMAX_ACCUMULATOR_TYPE(out_val) * TO_SOFTMAX_ACCUMULATOR_TYPE(max_logits_u_exp_sum[partition_idx]);
+#if defined(INPUT0_IS_FP) && INPUT0_IS_FP
+ const SOFTMAX_ACCUMULATOR_TYPE out_val_f = TO_SOFTMAX_ACCUMULATOR_TYPE(out_val);
+#else
+ // bf16 is stored as ushort; reinterpret bits rather than performing an integer cast.
+ const SOFTMAX_ACCUMULATOR_TYPE out_val_f = _convert_as_bfloat16_float(out_val);
+#endif
+ acc += out_val_f * TO_SOFTMAX_ACCUMULATOR_TYPE(max_logits_u_exp_sum[partition_idx]);
}
const uint out_offset = b0_idx * (NUM_HEADS * TARGET_SEQ_LEN * V_HEAD_SIZE) +
#ifdef OUTPUT_TRANSPOSE_ORDER_PRESENT
@@ -2808,7 +2825,7 @@ KERNEL(sdpa_opt_finalization_stage)(
#endif
local_id;
- output[out_offset] = TO_OUTPUT_TYPE(acc) / TO_OUTPUT_TYPE(global_exp_sum);
+ output[out_offset] = TO_OUTPUT_TYPE(acc / global_exp_sum);
}
#endif
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_ref.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_ref.cl
index 8738184ce04..e0d2e565716 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_ref.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa_ref.cl
@@ -3,6 +3,72 @@
//
#include "include/batch_headers/fetch_data.cl"
+#include "include/batch_headers/bf16_utils.cl"
+
+// For bf16 the raw storage is ushort and requires bit-reinterpretation (not a numeric convert)
+// to obtain a usable float value. For all other types the input type is already numerically
+// usable, so we keep the original variable type and use an identity load. This preserves the
+// pre-bf16 codegen for fp16/fp32 paths (no unintended promotion to the accumulator type).
+//
+// INPUTi_IS_FP is false for bf16 in the ocl_v2 jitter (see make_type_jit_constants), so the
+// pair (TYPE_SIZE == 2 && !IS_FP) uniquely identifies bf16 without matching fp16.
+
+#if INPUT0_TYPE_SIZE == 2 && !INPUT0_IS_FP
+ #define INPUT0_COMPUTE_T ACCUMULATOR_TYPE
+ #define LOAD_INPUT0(v) _convert_as_bfloat16_float(v)
+#else
+ #define INPUT0_COMPUTE_T INPUT0_TYPE
+ #define LOAD_INPUT0(v) (v)
+#endif
+
+#if INPUT1_TYPE_SIZE == 2 && !INPUT1_IS_FP
+ #define INPUT1_COMPUTE_T ACCUMULATOR_TYPE
+ #define LOAD_INPUT1(v) _convert_as_bfloat16_float(v)
+#else
+ #define INPUT1_COMPUTE_T INPUT1_TYPE
+ #define LOAD_INPUT1(v) (v)
+#endif
+
+#if INPUT2_TYPE_SIZE == 2 && !INPUT2_IS_FP
+ #define INPUT2_COMPUTE_T ACCUMULATOR_TYPE
+ #define LOAD_INPUT2(v) _convert_as_bfloat16_float(v)
+#else
+ #define INPUT2_COMPUTE_T INPUT2_TYPE
+ #define LOAD_INPUT2(v) (v)
+#endif
+
+// attn_mask input (INPUT3) - same treatment as Q/K/V: bit-reinterpret bf16 storage,
+// identity load for the remaining supported (numerically usable) mask types.
+#ifdef INPUT3_TYPE
+#if INPUT3_TYPE_SIZE == 2 && !INPUT3_IS_FP
+ #define INPUT3_COMPUTE_T ACCUMULATOR_TYPE
+ #define LOAD_INPUT3(v) _convert_as_bfloat16_float(v)
+#else
+ #define INPUT3_COMPUTE_T INPUT3_TYPE
+ #define LOAD_INPUT3(v) (v)
+#endif
+#endif
+
+// Output store: for bf16 we bit-reinterpret the float accumulator into a ushort payload; for
+// other output types the standard numeric conversion is correct. OUTPUT_COMPUTE_T is the type
+// used to hold in-flight values that will eventually be stored to `output` (mirrors the
+// INPUTi_COMPUTE_T pattern above). TO_OUTPUT_COMPUTE_T() is the corresponding numeric convert
+// for scalar/literal values populating an OUTPUT_COMPUTE_T variable.
+#if OUTPUT_TYPE_SIZE == 2 && !OUTPUT_IS_FP
+ #define OUTPUT_COMPUTE_T ACCUMULATOR_TYPE
+ #define TO_OUTPUT_COMPUTE_T(v) convert_float(v)
+ #define STORE_OUTPUT(v) _convert_bfloat16_as_ushort(v)
+ #define OUTPUT_COMPUTE_VAL_ZERO ACCUMULATOR_VAL_ZERO
+ #define OUTPUT_COMPUTE_VAL_ONE ACCUMULATOR_VAL_ONE
+ #define OUTPUT_COMPUTE_VAL_MIN ACCUMULATOR_VAL_MIN
+#else
+ #define OUTPUT_COMPUTE_T OUTPUT_TYPE
+ #define TO_OUTPUT_COMPUTE_T(v) TO_OUTPUT_TYPE(v)
+ #define STORE_OUTPUT(v) TO_OUTPUT_TYPE(v)
+ #define OUTPUT_COMPUTE_VAL_ZERO OUTPUT_VAL_ZERO
+ #define OUTPUT_COMPUTE_VAL_ONE OUTPUT_VAL_ONE
+ #define OUTPUT_COMPUTE_VAL_MIN OUTPUT_VAL_MIN
+#endif
// query_input [batch, heads_num, q_len, head_size]
// key_input [batch, kv_heads_num, kv_len, head_size]
@@ -155,7 +221,7 @@ KERNEL(sdpa_ref)(
#ifdef BEAM_TABLE_TYPE
const __global BEAM_TABLE_TYPE* beam_table,
#endif
- __global OUTPUT_TYPE* tmp_buf
+ __global OUTPUT_COMPUTE_T* tmp_buf
)
{
const uint batch_idx = get_global_id(0);
@@ -165,18 +231,18 @@ KERNEL(sdpa_ref)(
const uint head_size_idx = get_global_id(2);
#if HAS_SCALE_INPUT
- const OUTPUT_TYPE scale_val = *scale;
+ const OUTPUT_COMPUTE_T scale_val = *scale;
#elif defined(STATIC_SCALE_VALUE)
- const OUTPUT_TYPE scale_val = TO_OUTPUT_TYPE(STATIC_SCALE_VALUE);
+ const OUTPUT_COMPUTE_T scale_val = TO_OUTPUT_COMPUTE_T(STATIC_SCALE_VALUE);
#else
- const OUTPUT_TYPE scale_val = OUTPUT_VAL_ONE / sqrt(TO_OUTPUT_TYPE(INPUT1_SIZE_X));
+ const OUTPUT_COMPUTE_T scale_val = OUTPUT_COMPUTE_VAL_ONE / sqrt(TO_OUTPUT_COMPUTE_T(INPUT1_SIZE_X));
#endif
// Process 1*seq_len elements (Gemm1 + SoftMax) using a single work item, saving results to tmp_buf and
// reusing them between all work items within a single workgroup for Gemm2 calculations.
if (get_local_id(2) == 0) {
for (uint s = 0; s < SOURCE_SEQ_LEN /* seq_len */; s++) {
- OUTPUT_TYPE acc = 0;
+ OUTPUT_COMPUTE_T acc = OUTPUT_COMPUTE_VAL_ZERO;
for (uint h = 0; h < HEAD_SIZE /* head_size */; h++) {
uint query_offset = FUNC_CALL(get_input0_index)(OPTIONAL_SHAPE_INFO_TENSOR b0, b1, 0, 0, target_seq_idx, h);
#ifdef BEAM_TABLE_TYPE
@@ -187,9 +253,9 @@ KERNEL(sdpa_ref)(
uint key_offset = FUNC_CALL(get_input1_index)(OPTIONAL_SHAPE_INFO_TENSOR b_idx, b1, 0, 0, s, h);
#if APPLY_SCALE_TO_QUERY
- INPUT0_TYPE q_val = query_input[query_offset] * scale_val;
+ INPUT0_COMPUTE_T q_val = LOAD_INPUT0(query_input[query_offset]) * scale_val;
#else
- INPUT0_TYPE q_val = query_input[query_offset];
+ INPUT0_COMPUTE_T q_val = LOAD_INPUT0(query_input[query_offset]);
#endif
INPUT1_TYPE k_val_packed = key_input[key_offset];
@@ -207,7 +273,7 @@ KERNEL(sdpa_ref)(
KEY_COMPRESSION_SCALE_TYPE k_val = ((k_val_packed - comp_zp) * comp_scale);
#else
- INPUT1_TYPE k_val = k_val_packed;
+ INPUT1_COMPUTE_T k_val = LOAD_INPUT1(k_val_packed);
#endif
acc += q_val * k_val;
}
@@ -228,17 +294,17 @@ KERNEL(sdpa_ref)(
b1 * (TARGET_SEQ_LEN * SOURCE_SEQ_LEN) +
target_seq_idx * (SOURCE_SEQ_LEN) + s;
#if IS_CAUSAL
- OUTPUT_TYPE attn_mask_val = s > target_seq_idx ? OUTPUT_VAL_MIN : 0;
+ OUTPUT_COMPUTE_T attn_mask_val = s > target_seq_idx ? OUTPUT_COMPUTE_VAL_MIN : OUTPUT_COMPUTE_VAL_ZERO;
#elif !IS_CAUSAL && HAS_ATTN_MASK_INPUT
uint attn_mask_offset = INPUT3_GET_INDEX_SAFE(b0, b1, target_seq_idx, s);
- OUTPUT_TYPE attn_mask_val = attn_mask[attn_mask_offset];
+ OUTPUT_COMPUTE_T attn_mask_val = LOAD_INPUT3(attn_mask[attn_mask_offset]);
#elif defined(STATIC_SCALAR_ATTN_MASK_VALUE)
- OUTPUT_TYPE attn_mask_val = TO_OUTPUT_TYPE(STATIC_SCALAR_ATTN_MASK_VALUE);
+ OUTPUT_COMPUTE_T attn_mask_val = TO_OUTPUT_COMPUTE_T(STATIC_SCALAR_ATTN_MASK_VALUE);
#else
- OUTPUT_TYPE attn_mask_val = OUTPUT_VAL_ZERO;
+ OUTPUT_COMPUTE_T attn_mask_val = OUTPUT_COMPUTE_VAL_ZERO;
#endif
- OUTPUT_TYPE qk_val = tmp_buf[tmp_buf_offset] + attn_mask_val;
+ OUTPUT_COMPUTE_T qk_val = tmp_buf[tmp_buf_offset] + attn_mask_val;
tmp_buf[tmp_buf_offset] = qk_val;
qk_max = ACCUMULATOR_MAX_FUNC(qk_max, TO_ACCUMULATOR_TYPE(qk_val));
@@ -253,11 +319,11 @@ KERNEL(sdpa_ref)(
b1 * (TARGET_SEQ_LEN * SOURCE_SEQ_LEN) +
target_seq_idx * (SOURCE_SEQ_LEN) + s;
- OUTPUT_TYPE qk_val = tmp_buf[tmp_buf_offset];
- ACCUMULATOR_TYPE val = native_exp(TO_ACCUMULATOR_TYPE(qk_val) - qk_max);
+ ACCUMULATOR_TYPE qk_val = tmp_buf[tmp_buf_offset];
+ ACCUMULATOR_TYPE val = native_exp(qk_val - qk_max);
exp_sum += val;
- tmp_buf[tmp_buf_offset] = TO_OUTPUT_TYPE(val);
+ tmp_buf[tmp_buf_offset] = val;
}
#ifdef HAS_SINK_INPUT
ACCUMULATOR_TYPE val = native_exp(TO_ACCUMULATOR_TYPE(sink_ptr[b1] - qk_max));
@@ -270,15 +336,15 @@ KERNEL(sdpa_ref)(
b1 * (TARGET_SEQ_LEN * SOURCE_SEQ_LEN) +
target_seq_idx * (SOURCE_SEQ_LEN) + s;
- OUTPUT_TYPE qk_val = tmp_buf[tmp_buf_offset];
- ACCUMULATOR_TYPE val = TO_ACCUMULATOR_TYPE(qk_val) * inv_sum;
- tmp_buf[tmp_buf_offset] = TO_OUTPUT_TYPE(val);
+ ACCUMULATOR_TYPE qk_val = tmp_buf[tmp_buf_offset];
+ ACCUMULATOR_TYPE val = qk_val * inv_sum;
+ tmp_buf[tmp_buf_offset] = val;
}
}
barrier(CLK_GLOBAL_MEM_FENCE);
- OUTPUT_TYPE acc = 0;
+ OUTPUT_COMPUTE_T acc = OUTPUT_COMPUTE_VAL_ZERO;
for (uint s = 0; s < SOURCE_SEQ_LEN /* seq_len */; s++) {
uint tmp_buf_offset = b0 * (NUM_HEADS * TARGET_SEQ_LEN * SOURCE_SEQ_LEN) +
b1 * (TARGET_SEQ_LEN * SOURCE_SEQ_LEN) +
@@ -305,12 +371,12 @@ KERNEL(sdpa_ref)(
#endif
VALUE_COMPRESSION_SCALE_TYPE value = ((value_packed - comp_zp) * comp_scale);
#else
- INPUT2_TYPE value = value_packed;
+ INPUT2_COMPUTE_T value = LOAD_INPUT2(value_packed);
#endif
acc += tmp_buf[tmp_buf_offset] * value;
}
uint output_offset = OUTPUT_GET_INDEX(b0, b1, target_seq_idx, head_size_idx);
- output[output_offset] = acc;
+ output[output_offset] = STORE_OUTPUT(acc);
}
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cpp
index 329b1b51bca..369c88a2b92 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cpp
@@ -303,6 +303,8 @@ JitConstants make_type_jit_constants(const std::string& name, const ov::element:
break;
case ov::element::bf16:
type = "ushort";
+ max_val = "FLT_MAX";
+ min_val = "-" + name + "_VAL_MAX";
val_one = "(ushort) 1";
val_zero = "(ushort) 0";
to_type = "_convert_bfloat16_as_ushort(v)";
@@ -311,6 +313,9 @@ JitConstants make_type_jit_constants(const std::string& name, const ov::element:
to_compute_type = "convert_float(v)";
decode_compute_type = "_convert_as_bfloat16_float(v)";
decode_compute_vector_type = "CONVERT_AS_BFLOAT16_FLOAT(v, size)";
+ max_func = "fmax";
+ min_func = "fmin";
+ abs_func = "fabs";
type_size = "2";
is_fp = false;
is_bf16 = true;
@@ -613,6 +618,8 @@ std::string to_ocl_type(ov::element::Type_t et) {
return get_ocl_type_name<int64_t>();
case ov::element::Type_t::f16:
return get_ocl_type_name<ov::float16>();
+ case ov::element::Type_t::bf16:
+ return get_ocl_type_name<ov::bfloat16>();
case ov::element::Type_t::f32:
return get_ocl_type_name<float>();
default:
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.hpp
index 02580950a58..c13cec1c2f3 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.hpp
@@ -62,6 +62,10 @@ inline std::string get_ocl_type_name<ov::float16>() {
return "half";
}
template <>
+inline std::string get_ocl_type_name<ov::bfloat16>() {
+ return "ushort";
+}
+template <>
inline std::string get_ocl_type_name<double>() {
return "double";
}
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl
index 84d4fa5f344..62c909435eb 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/tile_ops.cl
@@ -174,6 +174,33 @@ DEF_BLOCK2D_LOAD_STORE(half, ushort, 16, 16, u16_m8k32v1, 32, 8)
= __builtin_convertvector(t.x[i], __typeof__(t_new.x[i])); \
} while (0)
+#define tile_copy_to_bf16x2(t, t_new) \
+ do { \
+ _Pragma("unroll") for (int i = 0; i < sizeof(t.x) / sizeof(t.x[0]); \
+ i++) { \
+ _Pragma("unroll") for (int s = 0; \
+ s < sizeof(t.x[0]) / sizeof(t.x[0][0]) / 2; \
+ s++) { \
+ float2 fv = {t.x[i][2 * s], t.x[i][2 * s + 1]}; \
+ t_new.x[i][s] = as_uint(_convert_bfloat162_as_ushort2(fv)); \
+ } \
+ } \
+ } while (0)
+
+// Reinterpret each element of a half tile as bf16 bits and convert to float,
+// storing the result into a float tile of matching shape.
+#define tile_copy_bf16bits_to_float(t, t_new) \
+ do { \
+ _Pragma("unroll") for (int i = 0; i < sizeof(t.x) / sizeof(t.x[0]); \
+ i++) { \
+ _Pragma("unroll") for (int s = 0; \
+ s < sizeof(t.x[0]) / sizeof(t.x[0][0]); \
+ s++) { \
+ t_new.x[i][s] = _convert_as_bfloat16_float(as_ushort(t.x[i][s])); \
+ } \
+ } \
+ } while (0)
+
#define tile_copy_to_half2(t, t_new) \
do { \
_Pragma("unroll") for (int i = 0; i < sizeof(t.x) / sizeof(t.x[0]); \
@@ -477,6 +504,18 @@ DEF_BLOCK2D_LOAD_STORE(half, ushort, 16, 16, u16_m8k32v1, 32, 8)
} \
}
+#define DECLARE_2D_TILE_COPY_REBLOCK_TO_BF16BITS(tile_type0, sg0, br0, bc0, nbr0, nbc0, \
+ tile_type1, sg1, br1, bc1, nbr1, nbc1) \
+ __attribute__((overloadable)) void tile_copy_reblock_to_bf16bits( \
+ tile_type0 t0, tile_type1 *t1) { \
+ _Pragma("unroll") for (int j = 0; j < bc0 * nbc0; j++) { \
+ _Pragma("unroll") for (int i0 = 0; i0 < br0 * nbr0; i0 += sg0) { \
+ tile_access(*t1, i0, j, sg1, br1, bc1, nbr1) \
+ = as_half(_convert_bfloat16_as_ushort(convert_float(tile_access(t0, i0, j, sg0, br0, bc0, nbr0)))); \
+ } \
+ } \
+ }
+
#define DECLARE_2D_TILE(tile_type, element_type, sg, br, bc, nbr, nbc) \
typedef element_type __attribute__((ext_vector_type(br * bc / sg))) \
_e_##tile_type; \
diff --git a/src/plugins/intel_gpu/src/plugin/common_utils.cpp b/src/plugins/intel_gpu/src/plugin/common_utils.cpp
index be11a4b097a..5fa34249a98 100644
--- a/src/plugins/intel_gpu/src/plugin/common_utils.cpp
+++ b/src/plugins/intel_gpu/src/plugin/common_utils.cpp
@@ -169,6 +169,9 @@ void convert_and_copy(const void* src_ptr, ov::element::Type src_et, void* dst_p
CASE(ov::element::f16, ov::element::f16, ov::float16, ov::float16);
CASE(ov::element::bf16, ov::element::f32, ov::bfloat16, float);
CASE(ov::element::bf16, ov::element::f16, ov::bfloat16, ov::float16);
+ CASE(ov::element::bf16, ov::element::bf16, ov::bfloat16, ov::bfloat16);
+ CASE(ov::element::f32, ov::element::bf16, float, ov::bfloat16);
+ CASE(ov::element::f16, ov::element::bf16, ov::float16, ov::bfloat16);
CASE(ov::element::boolean, ov::element::u8, bool, uint8_t);
OPENVINO_THROW("[GPU] Unsupported element types combination for copy: ", src_et, " -> ", dst_et);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index bebd45f508d..02832802fa8 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -551,8 +551,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
case ov::element::f16: return device_info.supports_fp16;
case ov::element::f32: return true; // assume that all GPUs support f32 data type
case ov::element::f64: return device_info.supports_fp64;
- // TODO: Remove get_use_onednn() guard once OCL kernels support bf16
- case ov::element::bf16: return device_info.supports_immad && config.get_use_onednn();
+ case ov::element::bf16: return device_info.supports_immad;
default: return false;
}
return false;
@@ -942,11 +941,12 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
const auto& value_ps = sdpa->get_input_partial_shape(2);
// Known limitations:
- // - The data type of SDPA should be fp16
- if (sdpa->get_output_element_type(0) != ov::element::f16)
+ // - The data type of SDPA should be fp16 or bf16
+ if (sdpa->get_output_element_type(0) != ov::element::f16 &&
+ sdpa->get_output_element_type(0) != ov::element::bf16)
return false;
- // - The attn mask type of SDPA should be fp16
+ // - The attn mask type of SDPA should be fp16 or bf16
if (!sdpa->get_causal() && sdpa->get_input_size() >= 4 && sdpa->get_input_element_type(3) == ov::element::boolean) {
return false;
}
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 1add0b08c69..0bb3e27b58c 100644
--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -239,7 +239,7 @@ const std::vector<std::regex>& disabled_test_patterns() {
patterns.push_back(std::regex(R"(.*smoke_MatMul_NoTranspose/MatMulLayerTest.Inference/IS=.*)"));
patterns.push_back(std::regex(R"(.*smoke_GroupConvolutionLayerGPUTest_dynamic1DSymPad_Disabled/GroupConvo.*)"));
patterns.push_back(std::regex(R"(.*smoke_static_conv_n_dynamic_concat/ConvStaticConcatDynamicGPUTestDynam.*)"));
- patterns.push_back(std::regex(R"(.*smoke_ScaledAttnStatic_GPU/ScaledAttnLayerGPUTest.CompareWithRefs/netP.*)"));
+ patterns.push_back(std::regex(R"(.*smoke_ScaledAttnStatic_GPU/ScaledAttnLayerGPUTest.CompareWithRefs/netPRC=(f16|f32)_.*)"));
patterns.push_back(std::regex(R"(.*smoke_FC_3D/MatMulLayerGPUTest.Inference/.*)"));
patterns.push_back(std::regex(R"(.*smoke_ConvolutionLayerGPUTest_3D_tensor_basic/ConvolutionLayerGPUTest..*)"));
patterns.push_back(std::regex(R"(.*smoke_MatmulWeightsDecompressionQuantizeConvolution_basic.*)"));
diff --git a/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp b/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp
index 8f41d5f56f5..6eeb16ca4da 100644
--- a/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp
+++ b/src/plugins/intel_gpu/tests/functional/single_layer_tests/dynamic/scaled_dot_product_attention.cpp
@@ -81,6 +81,13 @@ void ScaledAttnLayerGPUTest::SetUp() {
targetDevice = ov::test::utils::DEVICE_GPU;
const auto& [inType, _inputShapes, _is_causal, _has_attn, _is_attn_const, _has_scale, _is_scale_const, input_transpose, _has_sink] = this->GetParam();
+ if (inType == ov::element::bf16) {
+ const auto capabilities = core->get_property(targetDevice, ov::device::capabilities);
+ if (std::find(capabilities.cbegin(), capabilities.cend(), ov::intel_gpu::capability::HW_MATMUL) == capabilities.cend())
+ GTEST_SKIP();
+ configuration.insert(ov::hint::inference_precision(ov::element::bf16));
+ }
+
is_causal = _is_causal;
has_attn = _has_attn;
is_attn_const = _is_attn_const;
@@ -175,7 +182,7 @@ void ScaledAttnLayerGPUTest::SetUp() {
if (has_sink) {
size_t num_heads = inputDynamicShapes[0][1].get_length();
ov::test::utils::InputGenerateData data(0, 5, 100);
- auto sink_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, ov::Shape{1, num_heads, 1, 1}, data);
+ auto sink_tensor = ov::test::utils::create_and_fill_tensor(inType, ov::Shape{1, num_heads, 1, 1}, data);
auto sink_const = std::make_shared<ov::op::v0::Constant>(sink_tensor);
sink_const->set_friendly_name("sink");
inputs.push_back(sink_const);
@@ -230,6 +237,18 @@ void ScaledAttnLayerGPUTest::SetUp() {
abs_threshold = 0.005;
rel_threshold = 0.005;
}
+ } else if (inType == ov::element::bf16) {
+ // bf16 has ~3 bits fewer mantissa than fp16, so loosen thresholds accordingly.
+ if (has_sink || (has_diff_head_size && !has_scale)) {
+ abs_threshold = 0.1;
+ rel_threshold = 0.1;
+ } else if (has_long_seq || has_non_pow2_head) {
+ abs_threshold = 0.050;
+ rel_threshold = 0.050;
+ } else {
+ abs_threshold = 0.025;
+ rel_threshold = 0.025;
+ }
}
}
@@ -277,7 +296,8 @@ void ScaledAttnLayerGPUTest::generate_inputs(const std::vector<ov::Shape>& targe
for (int i = 0; i < 3; ++i) {
shapes[i] = targetInputStaticShapes[i];
ov::test::utils::InputGenerateData data(0, 8, 32);
- ov::Tensor data_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[i], data);
+ const auto elem_type = model_inputs[i].get_element_type();
+ ov::Tensor data_tensor = ov::test::utils::create_and_fill_tensor(elem_type, shapes[i], data);
inputs.insert({model_inputs[i].get_node_shared_ptr(), data_tensor});
}
}
@@ -285,23 +305,24 @@ void ScaledAttnLayerGPUTest::generate_inputs(const std::vector<ov::Shape>& targe
ov::test::utils::InputGenerateData scale_data(0.1f, 1, 10);
if (!has_attn && has_scale) {
shapes.push_back(ov::Shape{});
- ov::Tensor attn_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[3], attn_data);
+ ov::Tensor attn_tensor = ov::test::utils::create_and_fill_tensor(model_inputs[3].get_element_type(), shapes[3], attn_data);
inputs.insert({model_inputs[3].get_node_shared_ptr(), attn_tensor});
if (!is_scale_const) {
shapes.push_back(ov::Shape{1});
- ov::Tensor scale_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[4], scale_data);
+ ov::Tensor scale_tensor = ov::test::utils::create_and_fill_tensor(model_inputs[4].get_element_type(), shapes[4], scale_data);
inputs.insert({model_inputs[4].get_node_shared_ptr(), scale_tensor});
}
} else {
int idx = 3;
if (has_attn && !is_attn_const) {
shapes.push_back(targetInputStaticShapes[3]);
- ov::Tensor attn_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[idx], attn_data);
- inputs.insert({model_inputs[idx++].get_node_shared_ptr(), attn_tensor});
+ ov::Tensor attn_tensor = ov::test::utils::create_and_fill_tensor(model_inputs[idx].get_element_type(), shapes[idx], attn_data);
+ inputs.insert({model_inputs[idx].get_node_shared_ptr(), attn_tensor});
+ idx++;
}
if (has_scale && !is_scale_const) {
shapes.push_back(ov::Shape{1});
- ov::Tensor scale_tensor = ov::test::utils::create_and_fill_tensor(ov::element::f16, shapes[idx], scale_data);
+ ov::Tensor scale_tensor = ov::test::utils::create_and_fill_tensor(model_inputs[idx].get_element_type(), shapes[idx], scale_data);
inputs.insert({model_inputs[idx].get_node_shared_ptr(), scale_tensor});
}
}
@@ -691,7 +712,7 @@ const std::vector<std::vector<InputShape>> dynamic_shapes_4D {
const std::vector<std::vector<int64_t>> transpose_value{{0, 1, 2, 3}, {0, 1, 2, 3}, {0, 2, 1, 3}};
const std::vector<std::vector<int64_t>> transpose_all_4D{{0, 2, 1, 3}, {0, 2, 1, 3}, {0, 2, 1, 3}};
-const auto dynamic_shape_params_4D = testing::Combine(testing::Values(ov::element::f16 /*, ov::element::f32 */),
+const auto dynamic_shape_params_4D = testing::Combine(testing::Values(ov::element::f16, ov::element::bf16 /*, ov::element::f32 */),
testing::ValuesIn(dynamic_shapes_4D),
testing::Values(true, false),
testing::Values(true, false),
@@ -932,7 +953,7 @@ const std::vector<std::vector<InputShape>> static_shapes{
},
};
-const auto static_shape_params = testing::Combine(testing::Values(ov::element::f16),
+const auto static_shape_params = testing::Combine(testing::Values(ov::element::f16, ov::element::bf16),
testing::ValuesIn(static_shapes),
testing::Values(true, false),
testing::Values(true, false),
@@ -946,4 +967,5 @@ INSTANTIATE_TEST_SUITE_P(smoke_ScaledAttnStatic_GPU,
ScaledAttnLayerGPUTest,
static_shape_params,
ScaledAttnLayerGPUTest::getTestCaseName);
+
} // namespace
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cpp
index 0f11c1ceb8d..d60a87add8d 100644
--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cpp
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/kv_cache_sdpa.cpp
@@ -47,13 +47,19 @@ public:
#endif
auto core = ov::test::utils::PluginCache::get().core();
- ov::AnyMap properties = {ov::hint::inference_precision(ov::element::f16),
+ if (p.model_element_type == ov::element::bf16) {
+ const auto capabilities = core->get_property(ov::test::utils::DEVICE_GPU, ov::device::capabilities);
+ if (std::find(capabilities.cbegin(), capabilities.cend(), ov::intel_gpu::capability::HW_MATMUL) == capabilities.cend())
+ GTEST_SKIP();
+ }
+
+ ov::AnyMap properties = {ov::hint::inference_precision(p.model_element_type),
ov::intel_gpu::hint::enable_sdpa_optimization(true)};
if (p.compressed) {
properties.emplace(ov::hint::kv_cache_precision(ov::element::i8));
} else {
- properties.emplace(ov::hint::kv_cache_precision(ov::element::f16));
+ properties.emplace(ov::hint::kv_cache_precision(p.model_element_type));
}
const size_t n_heads = 16;
@@ -409,6 +415,63 @@ std::vector<Params> get_test_params() {
// Compressed beam search (batch > 1 exercises indirect sdpa_opt path on IMMAD)
p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 2, ov::element::Type_t::f16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+ // BF16 mirrors of the f16 coverage above (exercises sdpa_micro.cl and sdpa_opt.cl for bf16).
+ p.push_back({with_rearrange, !with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 1, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+ p.push_back({!with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+ p.push_back({!with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {1, 2, 0, 3}});
+
+ // Multi heads
+ p.push_back({with_rearrange, !with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 1, 128, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 128, 96, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 5, 1, 96, 128, 1, {0, 2, 1, 3}});
+ p.push_back({!with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 128, 96, 1, {0, 2, 1, 3}});
+ p.push_back({!with_rearrange, with_mask, !with_scale, !causal, !compressed, 1, ov::element::Type_t::bf16, 19, 4, 192, 64, 1, {1, 2, 0, 3}});
+
+ // Beam search
+ p.push_back({with_rearrange, !with_mask, !with_scale, !causal, !compressed, 2, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, !with_mask, !with_scale, !causal, !compressed, 4, ov::element::Type_t::bf16, 5, 16, 64, 64, 1, {0, 2, 1, 3}});
+
+ p.push_back({with_rearrange, !with_mask, !with_scale, !causal, !compressed, 2, ov::element::Type_t::bf16, 10, 4, 128, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, !with_mask, !with_scale, !causal, !compressed, 4, ov::element::Type_t::bf16, 5, 16, 192, 96, 1, {0, 2, 1, 3}});
+
+ // Compressed
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 1, ov::element::Type_t::bf16, 10, 1, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {1, 2, 0, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 128, 96, 1, {1, 2, 0, 3}});
+
+ // Compressed beam search (batch > 1 exercises indirect sdpa_opt path on IMMAD)
+ p.push_back({with_rearrange, with_mask, !with_scale, !causal, compressed, 2, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+
+ /* -- causal mask -- */
+
+ p.push_back({with_rearrange, !with_mask, !with_scale, causal, !compressed, 1, ov::element::Type_t::bf16, 10, 1, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+ p.push_back({!with_rearrange, with_mask, !with_scale, causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, !compressed, 1, ov::element::Type_t::bf16, 10, 4, 128, 64, 1, {0, 2, 1, 3}});
+ p.push_back({!with_rearrange, with_mask, !with_scale, causal, !compressed, 1, ov::element::Type_t::bf16, 5, 4, 64, 96, 1, {0, 1, 2, 3}});
+
+ // Beam search
+ p.push_back({with_rearrange, !with_mask, !with_scale, causal, !compressed, 2, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, !with_mask, !with_scale, causal, !compressed, 4, ov::element::Type_t::bf16, 5, 16, 64, 64, 1, {0, 2, 1, 3}});
+ p.push_back({with_rearrange, !with_mask, !with_scale, causal, !compressed, 2, ov::element::Type_t::bf16, 10, 4, 96, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, !with_mask, !with_scale, causal, !compressed, 4, ov::element::Type_t::bf16, 5, 16, 96, 64, 1, {0, 2, 1, 3}});
+
+ // Compressed
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::bf16, 10, 1, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 128, 96, 1, {1, 2, 0, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 128, 64, 1, {0, 1, 2, 3}});
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 1, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {1, 2, 0, 3}});
+
+ // Compressed beam search (batch > 1 exercises indirect sdpa_opt path on IMMAD)
+ p.push_back({with_rearrange, with_mask, !with_scale, causal, compressed, 2, ov::element::Type_t::bf16, 10, 4, 64, 64, 1, {0, 2, 1, 3}});
+
return p;
}
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/sdpa.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/sdpa.cpp
index a938475c70d..5603eec4074 100644
--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/sdpa.cpp
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/sdpa.cpp
@@ -38,7 +38,8 @@ enum class GQAMode {
};
// validate the batch axis padding for sdpa_micro kernel.
-class SDPA : virtual public ov::test::SubgraphBaseStaticTest {
+class SDPA : virtual public ov::test::SubgraphBaseStaticTest,
+ public testing::WithParamInterface<ov::element::Type> {
protected:
void SetUp() override {
targetDevice = ov::test::utils::DEVICE_GPU;
@@ -47,7 +48,10 @@ protected:
if (std::find(capabilities.cbegin(), capabilities.cend(), ov::intel_gpu::capability::HW_MATMUL) == capabilities.cend())
GTEST_SKIP();
}
- auto inType = ov::element::f16;
+ auto inType = GetParam(); // Get data type from parameter
+ if (inType == ov::element::bf16) {
+ configuration.insert(ov::hint::inference_precision(ov::element::bf16));
+ }
ov::Shape inputShape{3, 4, 8, 16};
auto constant1 = ov::op::v0::Constant::create(ov::element::i32, {4}, {1, 4, 8, 16});
auto constant2 = ov::op::v0::Constant::create(ov::element::i32, {4}, {1, 4, 8, 16});
@@ -73,20 +77,32 @@ protected:
manager.register_pass<ov::pass::ScaledDotProductAttentionDecomposition>();
manager.run_passes(functionRefs);
+ // Set thresholds based on data type and sequence length
bool has_long_seq = inputShape[2] >= 384 || inputShape[3] >= 128;
if (inType == ov::element::f16) {
- if (has_long_seq) {
- abs_threshold = 0.025;
- rel_threshold = 0.025;
- } else {
- abs_threshold = 0.005;
- rel_threshold = 0.005;
- }
+ abs_threshold = has_long_seq ? 0.025 : 0.005;
+ rel_threshold = has_long_seq ? 0.025 : 0.005;
+ } else if (inType == ov::element::bf16) {
+ abs_threshold = has_long_seq ? 0.050 : 0.025;
+ rel_threshold = has_long_seq ? 0.050 : 0.025;
}
}
};
-// Validate that non-PA SDPA with f16 K/V inputs is not incorrectly blocked
+// Instantiate parameterized tests for SDPA with different data types
+INSTANTIATE_TEST_SUITE_P(
+ smoke_SDPA_DataTypes,
+ SDPA,
+ ::testing::Values(
+ ov::element::f16,
+ ov::element::bf16
+ ),
+ [](const testing::TestParamInfo<ov::element::Type>& info) {
+ return info.param.get_type_name(); // Test names: f16, bf16
+ }
+);
+
+// Validate that non-PA SDPA with f16/bf16 K/V inputs is not incorrectly blocked
// from using micro kernel when KV_CACHE_PRECISION is globally set to u4.
// This simulates a Vision Encoder SDPA node running alongside a PA-based LLM
// that uses INT4 KV cache. The global config should not affect the Vision Encoder path.
@@ -94,42 +110,60 @@ class SDPAWithInt4KVCacheConfig : public SDPA {
protected:
void SetUp() override {
SDPA::SetUp();
+ if (IsSkipped()) {
+ return;
+ }
+
+ // Set INT4 KV cache precision config
configuration.insert(ov::hint::kv_cache_precision(ov::element::u4));
}
};
-TEST_F(SDPAWithInt4KVCacheConfig, smoke_Inference) {
- run();
-}
+// Instantiate parameterized tests for SDPAWithInt4KVCacheConfig
+INSTANTIATE_TEST_SUITE_P(
+ smoke_SDPA_INT4Config_DataTypes,
+ SDPAWithInt4KVCacheConfig,
+ ::testing::Values(
+ ov::element::f16,
+ ov::element::bf16
+ ),
+ [](const testing::TestParamInfo<ov::element::Type>& info) {
+ return info.param.get_type_name(); // Test names: f16, bf16
+ }
+);
class SDPAFusion : virtual public ov::test::SubgraphBaseStaticTest,
- public testing::WithParamInterface<std::tuple<ov::PartialShape, // 0: query shape
- ov::Shape, // 1: query reshape shape
- ov::PartialShape, // 2: key shape
- ov::Shape, // 3: key reshape shape
- ov::PartialShape, // 4: value shape
- ov::Shape, // 5: value reshape shape
- ov::PartialShape, // 6: mask shape
- float, // 7: scale value
- float, // 8: abs_threshold
- float, // 9: rel_threshold
- GQAMode>> // 10: gqa_mode
+ public testing::WithParamInterface<std::tuple<ov::element::Type, // 0: data type
+ ov::PartialShape, // 1: query shape
+ ov::Shape, // 2: query reshape shape
+ ov::PartialShape, // 3: key shape
+ ov::Shape, // 4: key reshape shape
+ ov::PartialShape, // 5: value shape
+ ov::Shape, // 6: value reshape shape
+ ov::PartialShape, // 7: mask shape
+ float, // 8: scale value
+ float, // 9: abs_threshold
+ float, // 10 rel_threshold
+ GQAMode>> // 11: gqa_mode
{
protected:
void create_model() {
auto params = GetParam();
targetDevice = ov::test::utils::DEVICE_GPU;
- inType = ov::element::f16;
+ inType = std::get<0>(params);
+ if (inType == ov::element::bf16) {
+ configuration.insert(ov::hint::inference_precision(ov::element::bf16));
+ }
bool reshape = false;
- const ov::PartialShape query_shape = std::get<0>(params);
- const ov::Shape query_reshape_shape = std::get<1>(params);
- const ov::PartialShape key_shape = std::get<2>(params);
- const ov::Shape key_reshape_shape = std::get<3>(params);
- const ov::PartialShape value_shape = std::get<4>(params);
- const ov::Shape value_reshape_shape = std::get<5>(params);
- const ov::PartialShape attention_mask_shape = std::get<6>(params);
- GQAMode gqa_mode = std::get<10>(params);
+ const ov::PartialShape query_shape = std::get<1>(params);
+ const ov::Shape query_reshape_shape = std::get<2>(params);
+ const ov::PartialShape key_shape = std::get<3>(params);
+ const ov::Shape key_reshape_shape = std::get<4>(params);
+ const ov::PartialShape value_shape = std::get<5>(params);
+ const ov::Shape value_reshape_shape = std::get<6>(params);
+ const ov::PartialShape attention_mask_shape = std::get<7>(params);
+ GQAMode gqa_mode = std::get<11>(params);
const auto query = std::make_shared<ov::op::v0::Parameter>(inType, query_shape);
std::shared_ptr<ov::op::v1::Reshape> query_reshaped;
@@ -263,7 +297,7 @@ protected:
const auto mask = std::make_shared<ov::op::v0::Parameter>(inType, attention_mask_shape);
model_params.push_back(mask);
- const auto scale_const = ov::op::v0::Constant::create(inType, {}, std::vector<float>{std::get<7>(params)});
+ const auto scale_const = ov::op::v0::Constant::create(inType, {}, std::vector<float>{std::get<8>(params)});
std::shared_ptr<ov::op::v0::MatMul> qk;
if (reshape) {
qk = std::make_shared<ov::op::v0::MatMul>(query_reshaped, key_input, false, true);
@@ -292,8 +326,8 @@ protected:
functionRefs = function->clone();
- abs_threshold = std::get<8>(params);
- rel_threshold = std::get<9>(params);
+ abs_threshold = std::get<9>(params);
+ rel_threshold = std::get<10>(params);
}
void check_results() {
@@ -332,7 +366,11 @@ protected:
}
};
-TEST_F(SDPA, smoke_Inference) {
+TEST_P(SDPA, smoke_Inference) {
+ run();
+}
+
+TEST_P(SDPAWithInt4KVCacheConfig, smoke_Inference) {
run();
}
@@ -345,82 +383,100 @@ TEST_P(SDPAFusion, Inference) {
INSTANTIATE_TEST_SUITE_P(SDPAFusionTests,
SDPAFusion,
- ::testing::Values(std::make_tuple(ov::PartialShape{10, 1024, 64},
- ov::Shape{10, 1024, 64},
- ov::PartialShape{10, 77, 64},
- ov::Shape{10, 77, 64},
- ov::PartialShape{10, 77, 64},
- ov::Shape{10, 77, 64},
- ov::PartialShape{1024, 77},
- 1.0f,
- 0.025f,
- 0.025f,
- GQAMode::Default),
- std::make_tuple(ov::PartialShape{1, 10, 1024, 64},
- ov::Shape{10, 1024, 64},
- ov::PartialShape{1, 10, 77, 64},
- ov::Shape{10, 77, 64},
- ov::PartialShape{1, 10, 77, 64},
- ov::Shape{10, 77, 64},
- ov::PartialShape{10, 1024, 77},
- 1.0f,
- 0.025f,
- 0.025f,
- GQAMode::Default),
- std::make_tuple(ov::PartialShape{1, 10, 1024, 64},
- ov::Shape{10, 1024, 64},
- ov::PartialShape{1, 10, 1024, 64},
- ov::Shape{10, 1024, 64},
- ov::PartialShape{1, 10, 1024, 64},
- ov::Shape{10, 1024, 64},
- ov::PartialShape{10, 1024, 1024},
- 1.0f,
- 0.025f,
- 0.025f,
- GQAMode::Default),
- std::make_tuple(ov::PartialShape{1, 10, 77, 64},
- ov::Shape{10, 77, 64},
- ov::PartialShape{1, 10, 77, 64},
- ov::Shape{10, 77, 64},
- ov::PartialShape{1, 10, 77, 64},
- ov::Shape{10, 77, 64},
- ov::PartialShape{77, 77},
- 1.0f,
- 0.025f,
- 0.025f,
- GQAMode::Default),
- std::make_tuple(ov::PartialShape{1, 10, 1024, 64},
- ov::Shape{1, 10, 1024, 64},
- ov::PartialShape{1, 10, 1024, 64},
- ov::Shape{1, 10, 1024, 64},
- ov::PartialShape{1, 10, 1024, 64},
- ov::Shape{1, 10, 1024, 64},
- ov::PartialShape{10, 1024, 1024},
- 1.0f,
- 0.025f,
- 0.025f,
- GQAMode::Default),
- std::make_tuple(ov::PartialShape{1, 8, 10, 256},
- ov::Shape{1, 8, 10, 256},
- ov::PartialShape{1, 1, 10, 256},
- ov::Shape{1, 1, 10, 256},
- ov::PartialShape{1, 1, 10, 256},
- ov::Shape{1, 1, 10, 256},
- ov::PartialShape{10, 842},
- 1.0f,
- 0.025f,
- 0.025f,
- GQAMode::DirectBroadcast),
- std::make_tuple(ov::PartialShape{1, 8, 60, 256},
- ov::Shape{1, 8, 60, 256},
- ov::PartialShape{1, 1, 60, 256},
- ov::Shape{1, 1, 60, 256},
- ov::PartialShape{1, 1, 60, 256},
- ov::Shape{1, 1, 60, 256},
- ov::PartialShape{60, 20},
- 1.0f,
- 0.025f,
- 0.025f,
- GQAMode::ConcatBasedBroadcast)));
-
+ ::testing::Values(std::make_tuple(ov::element::f16,
+ ov::PartialShape{10, 1024, 64},
+ ov::Shape{10, 1024, 64},
+ ov::PartialShape{10, 77, 64},
+ ov::Shape{10, 77, 64},
+ ov::PartialShape{10, 77, 64},
+ ov::Shape{10, 77, 64},
+ ov::PartialShape{1024, 77},
+ 1.0f,
+ 0.025f,
+ 0.025f,
+ GQAMode::Default),
+ std::make_tuple(ov::element::f16,
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{10, 1024, 64},
+ ov::PartialShape{1, 10, 77, 64},
+ ov::Shape{10, 77, 64},
+ ov::PartialShape{1, 10, 77, 64},
+ ov::Shape{10, 77, 64},
+ ov::PartialShape{10, 1024, 77},
+ 1.0f,
+ 0.025f,
+ 0.025f,
+ GQAMode::Default),
+ std::make_tuple(ov::element::f16,
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{10, 1024, 64},
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{10, 1024, 64},
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{10, 1024, 64},
+ ov::PartialShape{10, 1024, 1024},
+ 1.0f,
+ 0.025f,
+ 0.025f,
+ GQAMode::Default),
+ std::make_tuple(ov::element::f16,
+ ov::PartialShape{1, 10, 77, 64},
+ ov::Shape{10, 77, 64},
+ ov::PartialShape{1, 10, 77, 64},
+ ov::Shape{10, 77, 64},
+ ov::PartialShape{1, 10, 77, 64},
+ ov::Shape{10, 77, 64},
+ ov::PartialShape{77, 77},
+ 1.0f,
+ 0.025f,
+ 0.025f,
+ GQAMode::Default),
+ std::make_tuple(ov::element::f16,
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{1, 10, 1024, 64},
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{1, 10, 1024, 64},
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{1, 10, 1024, 64},
+ ov::PartialShape{10, 1024, 1024},
+ 1.0f,
+ 0.025f,
+ 0.025f,
+ GQAMode::Default),
+ std::make_tuple(ov::element::f16,
+ ov::PartialShape{1, 8, 10, 256},
+ ov::Shape{1, 8, 10, 256},
+ ov::PartialShape{1, 1, 10, 256},
+ ov::Shape{1, 1, 10, 256},
+ ov::PartialShape{1, 1, 10, 256},
+ ov::Shape{1, 1, 10, 256},
+ ov::PartialShape{10, 842},
+ 1.0f,
+ 0.025f,
+ 0.025f,
+ GQAMode::DirectBroadcast),
+ std::make_tuple(ov::element::bf16,
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{1, 10, 1024, 64},
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{1, 10, 1024, 64},
+ ov::PartialShape{1, 10, 1024, 64},
+ ov::Shape{1, 10, 1024, 64},
+ ov::PartialShape{10, 1024, 1024},
+ 1.0f,
+ 0.1f,
+ 0.1f,
+ GQAMode::Default),
+ std::make_tuple(ov::element::bf16,
+ ov::PartialShape{1, 8, 10, 256},
+ ov::Shape{1, 8, 10, 256},
+ ov::PartialShape{1, 1, 10, 256},
+ ov::Shape{1, 1, 10, 256},
+ ov::PartialShape{1, 1, 10, 256},
+ ov::Shape{1, 1, 10, 256},
+ ov::PartialShape{10, 842},
+ 1.0f,
+ 0.1f,
+ 0.1f,
+ GQAMode::DirectBroadcast)));
} // namespace
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/sdpa_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/sdpa_gpu_test.cpp
index 0e2165b74ad..2635032b43b 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/sdpa_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/sdpa_gpu_test.cpp
@@ -43,21 +43,22 @@ struct sdpa_test_params {
float scale_val;
bool use_scalar_attn_mask;
float attn_mask_val;
+ data_types dt;
// Constructor for basic tests (backward compatibility)
sdpa_test_params(int h_size, int n_heads, int seq_q, int seq_kv, int b,
- bool dynamic_shape)
+ bool dynamic_shape, data_types dt = data_types::f16)
: head_size(h_size), num_heads(n_heads), sequence_length_q(seq_q),
sequence_length_kv(seq_kv), batch(b), dynamic(dynamic_shape),
use_scalar_scale_val(false), scale_val(1.0f), use_scalar_attn_mask(false),
- attn_mask_val(0.0f) {}
+ attn_mask_val(0.0f), dt(dt) {}
// Constructor for advanced caching tests
sdpa_test_params(int h_size, int n_heads, int seq_q, int seq_kv, int b,
- bool use_scale, float scale, bool use_mask, float mask)
+ bool use_scale, float scale, bool use_mask, float mask, data_types dt = data_types::f16)
: head_size(h_size), num_heads(n_heads), sequence_length_q(seq_q), sequence_length_kv(seq_kv),
batch(b), dynamic(true), use_scalar_scale_val(use_scale),
- scale_val(scale), use_scalar_attn_mask(use_mask), attn_mask_val(mask) {}
+ scale_val(scale), use_scalar_attn_mask(use_mask), attn_mask_val(mask), dt(dt) {}
};
struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
@@ -67,11 +68,16 @@ struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
rg.set_seed(GET_SUITE_NAME);
}
- void load_input(cldnn::memory::ptr mem, size_t idx) {
+ void load_input(cldnn::memory::ptr mem, size_t idx, data_types dt = data_types::f16) {
auto shapes = mem->get_layout().get_shape();
size_t size = ov::shape_size(shapes);
- auto input_data = rg.generate_random_1d<ov::float16>(size, -1.0f, 1.0f);
- set_values(mem, input_data);
+ if (dt == data_types::bf16) {
+ auto input_data = rg.generate_random_1d<ov::bfloat16>(size, -1.0f, 1.0f);
+ set_values(mem, input_data);
+ } else {
+ auto input_data = rg.generate_random_1d<ov::float16>(size, -1.0f, 1.0f);
+ set_values(mem, input_data);
+ }
}
std::tuple<cldnn::memory::ptr, cldnn::network::ptr> run_network(bool is_caching_test, bool use_optimized_sdpa,
@@ -86,7 +92,8 @@ struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
bool use_scalar_scale_val = false,
float scale_val = 1.0f,
bool use_scalar_attn_mask = false,
- float attn_mask_val = 0.0f) {
+ float attn_mask_val = 0.0f,
+ data_types dt = data_types::f16) {
auto& engine = get_test_engine();
topology topo;
topo.add(input_layout("input0", input0_layout));
@@ -106,7 +113,7 @@ struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
}
topo.add(sdpa_prim);
- topo.add(reorder("result",input_info("sdpa"), format::bfyx, data_types::f16));
+ topo.add(reorder("result",input_info("sdpa"), format::bfyx, dt));
ExecutionConfig config = get_test_default_config(engine);
config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
@@ -155,33 +162,33 @@ struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
cldnn::layout input0_static_layout, input1_static_layout, input2_static_layout, input3_static_layout;
if (p.dynamic) {
- input0_layout = cldnn::layout({-1, -1, num_heads, head_size}, data_types::f16, format::bfyx);
- input1_layout = cldnn::layout({-1, -1, num_heads, head_size}, data_types::f16, format::bfyx);
- input2_layout = cldnn::layout({-1, -1, num_heads, head_size}, data_types::f16, format::bfyx);
+ input0_layout = cldnn::layout({-1, -1, num_heads, head_size}, p.dt, format::bfyx);
+ input1_layout = cldnn::layout({-1, -1, num_heads, head_size}, p.dt, format::bfyx);
+ input2_layout = cldnn::layout({-1, -1, num_heads, head_size}, p.dt, format::bfyx);
if (test_two_rank_mask) {
- input3_layout = cldnn::layout({ -1, -1}, data_types::f16, format::bfyx);
+ input3_layout = cldnn::layout({ -1, -1}, p.dt, format::bfyx);
} else {
- input3_layout = cldnn::layout({-1, num_heads, -1, -1}, data_types::f16, format::bfyx);
+ input3_layout = cldnn::layout({-1, num_heads, -1, -1}, p.dt, format::bfyx);
}
- input0_static_layout = cldnn::layout({batch, seq_length_q, num_heads, head_size}, data_types::f16, format::bfyx);
- input1_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, data_types::f16, format::bfyx);
- input2_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, data_types::f16, format::bfyx);
+ input0_static_layout = cldnn::layout({batch, seq_length_q, num_heads, head_size}, p.dt, format::bfyx);
+ input1_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, p.dt, format::bfyx);
+ input2_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, p.dt, format::bfyx);
if (test_two_rank_mask) {
- input3_static_layout = cldnn::layout({seq_length_q, seq_length_kv}, data_types::f32, format::bfyx);
+ input3_static_layout = cldnn::layout({seq_length_q, seq_length_kv}, p.dt, format::bfyx);
} else {
- input3_static_layout = cldnn::layout({batch, num_heads, 1, seq_length_kv}, data_types::f16, format::bfyx);
+ input3_static_layout = cldnn::layout({batch, num_heads, 1, seq_length_kv}, p.dt, format::bfyx);
}
} else {
- input0_static_layout = cldnn::layout({batch, seq_length_q, num_heads, head_size}, data_types::f16, format::bfyx);
- input1_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, data_types::f16, format::bfyx);
- input2_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, data_types::f16, format::bfyx);
+ input0_static_layout = cldnn::layout({batch, seq_length_q, num_heads, head_size}, p.dt, format::bfyx);
+ input1_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, p.dt, format::bfyx);
+ input2_static_layout = cldnn::layout({batch, seq_length_kv, num_heads, head_size}, p.dt, format::bfyx);
if (test_two_rank_mask) {
- input3_static_layout = cldnn::layout({seq_length_q, seq_length_kv}, data_types::f16, format::bfyx);
+ input3_static_layout = cldnn::layout({seq_length_q, seq_length_kv}, p.dt, format::bfyx);
} else {
- input3_static_layout = cldnn::layout({batch, num_heads, 1, seq_length_kv}, data_types::f16, format::bfyx);
+ input3_static_layout = cldnn::layout({batch, num_heads, 1, seq_length_kv}, p.dt, format::bfyx);
}
input0_layout = input0_static_layout;
@@ -195,19 +202,19 @@ struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
auto input2 = engine.allocate_memory(input2_static_layout);
auto input3 = engine.allocate_memory(input3_static_layout);
- load_input(input0, 0);
- load_input(input1, 1);
- load_input(input2, 2);
- load_input(input3, 3);
+ load_input(input0, 0, p.dt);
+ load_input(input1, 1, p.dt);
+ load_input(input2, 2, p.dt);
+ load_input(input3, 3, input3_static_layout.data_type);
auto [mem_ref_ptr, net_ref_ptr] = run_network(is_caching_test, false,
input0_layout, input1_layout, input2_layout, input3_layout,
input0, input1, input2, input3,
- use_scalar_scale_val, scale_val, use_scalar_attn_mask, attn_mask_val);
+ use_scalar_scale_val, scale_val, use_scalar_attn_mask, attn_mask_val, p.dt);
auto [mem_opt_ptr, net_opt_ptr] = run_network(is_caching_test, true,
input0_layout, input1_layout, input2_layout, input3_layout,
input0, input1, input2, input3,
- use_scalar_scale_val, scale_val, use_scalar_attn_mask, attn_mask_val);
+ use_scalar_scale_val, scale_val, use_scalar_attn_mask, attn_mask_val, p.dt);
if (is_caching_test) {
auto inst = net_opt_ptr->get_primitive("sdpa");
@@ -224,9 +231,17 @@ struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
}
}
- cldnn::mem_lock<ov::float16, mem_lock_type::read> ref_data(mem_ref_ptr, get_test_stream());
- cldnn::mem_lock<ov::float16, mem_lock_type::read> opt_data(mem_opt_ptr, get_test_stream());
- {
+ if (p.dt == data_types::bf16) {
+ cldnn::mem_lock<ov::bfloat16, mem_lock_type::read> ref_data(mem_ref_ptr, get_test_stream());
+ cldnn::mem_lock<ov::bfloat16, mem_lock_type::read> opt_data(mem_opt_ptr, get_test_stream());
+ for (size_t idx = 0; idx < ref_data.size(); idx++) {
+ ASSERT_FALSE(std::isnan(static_cast<float>(opt_data[idx])) || std::isnan(static_cast<float>(ref_data[idx]))) << "NaN found at index " << idx;
+ }
+ auto ret = cosineSimilarity(ref_data, opt_data);
+ ASSERT_GE(ret, 0.95f);
+ } else {
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> ref_data(mem_ref_ptr, get_test_stream());
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> opt_data(mem_opt_ptr, get_test_stream());
for (size_t idx = 0; idx < ref_data.size(); idx++) {
ASSERT_FALSE(std::isnan(opt_data[idx]) || std::isnan(ref_data[idx])) << "NaN found at index " << idx;
}
@@ -255,6 +270,10 @@ struct sdpa_gpu_test : public ::testing::TestWithParam<sdpa_test_params> {
result += "_static";
}
+ if (info.param.dt == data_types::bf16) {
+ result += "_bf16";
+ }
+
return result;
}
};
@@ -271,7 +290,10 @@ INSTANTIATE_TEST_SUITE_P(
sdpa_test_params{64, 10, 77, 77, 1, false}, // two ranks mask
sdpa_test_params{64, 32, 128, 128, 2, true, 0.125f, false, 0.0f}, // scale_val only
sdpa_test_params{64, 32, 128, 128, 2, false, 1.0f, true, 0.5f}, // attn_mask only
- sdpa_test_params{512, 8, 1, 1024, 2, true}
+ sdpa_test_params{512, 8, 1, 1024, 2, true},
+ sdpa_test_params{64, 32, 128, 128, 2, true, data_types::bf16}, // bf16 dynamic
+ sdpa_test_params{64, 32, 128, 128, 2, false, data_types::bf16}, // bf16 static
+ sdpa_test_params{64, 10, 77, 77, 1, true, data_types::bf16} // bf16 two ranks mask
),
sdpa_gpu_test::PrintToStringParamName
);
diff --git a/src/plugins/intel_gpu/tests/unit/test_utils/test_utils.h b/src/plugins/intel_gpu/tests/unit/test_utils/test_utils.h
index 2ecd8ba88b5..3ba9f8beed2 100644
--- a/src/plugins/intel_gpu/tests/unit/test_utils/test_utils.h
+++ b/src/plugins/intel_gpu/tests/unit/test_utils/test_utils.h
@@ -409,7 +409,8 @@ std::string vec2str(const std::vector<vecElementType>& vec) {
return "()";
}
-inline float cosineSimilarity(cldnn::mem_lock<ov::float16, cldnn::mem_lock_type::read>& vec1, cldnn::mem_lock<ov::float16, cldnn::mem_lock_type::read>& memLockVec2) {
+template <typename T>
+inline float cosineSimilarity(cldnn::mem_lock<T, cldnn::mem_lock_type::read>& vec1, cldnn::mem_lock<T, cldnn::mem_lock_type::read>& memLockVec2) {
if (vec1.size() != memLockVec2.size()) {
return -1.0f;
}
diff --git a/src/tests/test_utils/common_test_utils/src/subgraph_builders/llm_builders.cpp b/src/tests/test_utils/common_test_utils/src/subgraph_builders/llm_builders.cpp
index 11b780ac660..8f287382dc2 100644
--- a/src/tests/test_utils/common_test_utils/src/subgraph_builders/llm_builders.cpp
+++ b/src/tests/test_utils/common_test_utils/src/subgraph_builders/llm_builders.cpp
@@ -12,6 +12,7 @@
#include "openvino/core/model.hpp"
#include "openvino/core/node_vector.hpp"
#include "openvino/core/partial_shape.hpp"
+#include "openvino/core/type/bfloat16.hpp"
#include "openvino/core/type/element_type.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
@@ -78,6 +79,9 @@ std::shared_ptr<ov::Node> make_attention_mask(ov::Output<ov::Node> q,
else if (element_type == ov::element::f16)
minus_inf =
ov::op::v0::Constant::create(element_type, ov::Shape{}, {std::numeric_limits<ov::float16>::lowest()});
+ else if (element_type == ov::element::bf16)
+ minus_inf =
+ ov::op::v0::Constant::create(element_type, ov::Shape{}, {std::numeric_limits<ov::bfloat16>::lowest()});
auto q_shape = std::make_shared<ov::op::v3::ShapeOf>(q, ov::element::i32);
auto k_shape = std::make_shared<ov::op::v3::ShapeOf>(k, ov::element::i32);