Skip to content

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

[GPU] Fix bf16 support for the compressed (i8) KV cache path (#38532)

Chon Ming Leee1fca238e94b
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/dynamic_quantize.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/dynamic_quantize.cpp
index fc2293c9018..c740bc8a074 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/dynamic_quantize.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/dynamic_quantize.cpp
@@ -77,6 +77,7 @@ namespace detail {
attach_dynamic_quantize_impl::attach_dynamic_quantize_impl() {
auto types = {
data_types::f16,
+ data_types::bf16,
data_types::i8,
data_types::u8,
data_types::f4e2m1,
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cpp
index d9d7f811c2e..ba4f19418f8 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/sdpa/sdpa_opt.cpp
@@ -198,6 +198,33 @@ public:
}
};
+bool SDPAOpt::has_bf16_compressed_kv_zero_point(const kernel_impl_params& params) {
+#ifdef ENABLE_ONEDNN_FOR_GPU
+ const auto desc = params.typed_desc<scaled_dot_product_attention>();
+ if (!desc->is_kv_compressed || desc->quantization_attributes.quantization_type != ov::op::internal::DynamicQuantize::QuantizationType::Asymmetric ||
+ desc->quantization_attributes.output_storage_type != ov::op::internal::DynamicQuantize::OutputStorageType::Planar) {
+ return false;
+ }
+
+ const auto& k_layout = params.get_input_layout(1);
+ if (!data_type_traits::is_i8_u8(k_layout.data_type) && !data_type_traits::is_i4_u4(k_layout.data_type)) {
+ return false;
+ }
+
+ // Per-channel compressed KV is only supported by the micro kernel, so it cannot be taken over by the
+ // regular stages and is not supposed to match here.
+ if (has_per_channel_compressed_kv(params)) {
+ return false;
+ }
+
+ const auto data_inputs_num = get_data_inputs_num(*desc);
+ return params.get_input_layout(data_inputs_num + 2).data_type == ov::element::bf16;
+#else
+ UNUSED(params);
+ return false;
+#endif
+}
+
bool SDPAOpt::supports_micro_sdpa(const RuntimeParams& params) {
#ifdef ENABLE_ONEDNN_FOR_GPU
auto& engine = params.get_program().get_engine();
@@ -261,6 +288,13 @@ bool SDPAOpt::supports_micro_sdpa(const RuntimeParams& params) {
// Known limitation: In vision encoding model of qwen-vl, when the shape of sdpa is 3D and num_heads is 1,
// there is an accuracy issue with sdpa_micro kernel. Therefore, it is currently restricted to execute with sdpa_opt kernel.
const bool is_output_rank_4d = desc->output_transpose_order.size() == 4;
+
+ // Asymmetric compressed KV with a bf16 zero-point buffer must not use the oneDNN micro SDPA stage:
+ // It emits NaN output and oneDNN own tests haven't test this combination yet
+ if (has_bf16_compressed_kv_zero_point(params)) {
+ return false;
+ }
+
return is_output_rank_4d;
#else
return false;
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 8d997c253e8..a66be4c748f 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
@@ -30,6 +30,9 @@ struct SDPAOpt : public ImplementationManager {
[[nodiscard]] std::unique_ptr<primitive_impl> create_impl(const program_node& node, const RuntimeParams& params) const override;
[[nodiscard]] static bool supports_micro_sdpa(const kernel_impl_params& params);
[[nodiscard]] static bool has_per_channel_compressed_kv(const kernel_impl_params& params);
+ // Asymmetric planar compressed KV whose zero-point buffer is bf16: the layout the oneDNN micro SDPA
+ // stage cannot handle, so it has to run on the regular sdpa_opt stages instead.
+ [[nodiscard]] static bool has_bf16_compressed_kv_zero_point(const kernel_impl_params& params);
[[nodiscard]] bool validate_impl(const program_node& node) const override {
const auto desc = node.as<scaled_dot_product_attention>().get_primitive();
const auto& supported_precisions = ov::intel_gpu::op::SDPA::get_supported_precisions();
@@ -77,7 +80,10 @@ struct SDPAOpt : public ImplementationManager {
if (has_per_channel_compressed_kv(*p) && !supports_micro_sdpa(*p)) {
return false;
}
- return !use_asymmetric_quantization || combine_scales_and_zp || supports_micro_sdpa(*p);
+ // Asymmetric planar compressed KV is normally handled by the micro kernel. The one layout the micro
+ // kernel cannot handle (an asymmetric bf16 zero-point buffer) is taken over by the regular
+ // sdpa_opt multi-token/single-token stages, so keep the implementation selectable for it as well.
+ return !use_asymmetric_quantization || combine_scales_and_zp || supports_micro_sdpa(*p) || has_bf16_compressed_kv_zero_point(*p);
}
};
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 35495e50484..7321275b813 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
@@ -10,6 +10,18 @@
#include "include/batch_headers/sub_group_block_write.cl"
#include "include/batch_headers/sub_group_shuffle.cl"
+#if IS_KV_COMPRESSED
+#define KV_DEQUANT_TYPE INPUT0_COMPUTE_TYPE
+#define TO_KV_DEQUANT_TYPE(val) CAT(convert_, KV_DEQUANT_TYPE)(val)
+#define V_DEQUANT_TYPE KV_DEQUANT_TYPE
+#define TO_V_DEQUANT_TYPE(val) TO_KV_DEQUANT_TYPE(val)
+#else
+#define KV_DEQUANT_TYPE INPUT1_COMPUTE_TYPE
+#define TO_KV_DEQUANT_TYPE(val) DECODE_INPUT1_COMPUTE_TYPE(val)
+#define V_DEQUANT_TYPE INPUT2_COMPUTE_TYPE
+#define TO_V_DEQUANT_TYPE(val) DECODE_INPUT2_COMPUTE_TYPE(val)
+#endif
+
// query_input [batch, heads_num, q_len, head_size]
// key_input [batch, kv_heads_num, kv_len, head_size]
// value_input [batch, kv_heads_num, kv_len, head_size]
@@ -147,10 +159,17 @@ inline uint FUNC(get_bt_index_value)(OPTIONAL_SHAPE_INFO_ARG uint b, uint f, uin
#endif
#define HAS_KV_CACHE_ZP_INPUT (USE_ASYMMETRIC_QUANTIZATION && !COMBINE_SCALES_AND_ZP)
#define GET_SCALE(zp, scale, comp_offset) ((scale)[(comp_offset)])
+ // Scale and zero-point tensors use storage types; decode them before arithmetic.
+ #define DECODE_KEY_SCALE(val) DECODE_KEY_COMPRESSION_SCALE_COMPUTE_TYPE(val)
+ #define DECODE_VALUE_SCALE(val) DECODE_VALUE_COMPRESSION_SCALE_COMPUTE_TYPE(val)
#if HAS_KV_CACHE_ZP_INPUT
#define GET_ZP(zp, scale, comp_offset) ((zp)[(comp_offset)])
+ #define DECODE_KEY_ZP(val) TO_KEY_COMPRESSION_SCALE_COMPUTE_TYPE(DECODE_KEY_COMPRESSION_ZP_COMPUTE_TYPE(val))
+ #define DECODE_VALUE_ZP(val) TO_VALUE_COMPRESSION_SCALE_COMPUTE_TYPE(DECODE_VALUE_COMPRESSION_ZP_COMPUTE_TYPE(val))
#else
#define GET_ZP(zp, scale, comp_offset) ((scale)[(comp_offset) + 1])
+ #define DECODE_KEY_ZP(val) DECODE_KEY_SCALE(val)
+ #define DECODE_VALUE_ZP(val) DECODE_VALUE_SCALE(val)
#endif
#if USE_ASYMMETRIC_QUANTIZATION
#define DEQUANTIZE_KV(value, zp, scale) (((value) - (zp)) * (scale))
@@ -341,9 +360,9 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + seq_len, 0);
- KEY_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(key_zp, key_scale, comp_offset);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_KEY_SCALE(GET_SCALE(key_zp, key_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- KEY_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(key_zp, key_scale, comp_offset);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_KEY_ZP(GET_ZP(key_zp, key_scale, comp_offset));
#endif
#endif
@@ -354,8 +373,8 @@ KERNEL(sdpa_opt)(
INPUT1_TYPE packed_byte = KEY_BLOCK_READ_1(key_input, key_offset + head_idx_index / 2);
char2 unpacked = unpack_to_char(*(uint4x2_t*)&packed_byte);
- KEY_COMPRESSION_SCALE_TYPE key_val0 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s0)), comp_zp, comp_scale);
- KEY_COMPRESSION_SCALE_TYPE key_val1 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s1)), comp_zp, comp_scale);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_val0 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_COMPUTE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s0)), comp_zp, comp_scale);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_val1 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_COMPUTE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s1)), comp_zp, comp_scale);
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;
@@ -375,9 +394,9 @@ KERNEL(sdpa_opt)(
? KEY_BLOCK_READ_1(key_input, key_offset + head_idx_index / 2) : (INPUT1_TYPE)0;
char2 unpacked = unpack_to_char(*(uint4x2_t*)&packed_byte);
- KEY_COMPRESSION_SCALE_TYPE key_val0 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s0)), comp_zp, comp_scale);
- KEY_COMPRESSION_SCALE_TYPE key_val1 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s1)), comp_zp, comp_scale);
- KEY_COMPRESSION_SCALE_TYPE lane_mask = (head_idx_index + 2 * sglid < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_TYPE)1 : (KEY_COMPRESSION_SCALE_TYPE)0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_val0 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_COMPUTE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s0)), comp_zp, comp_scale);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_val1 = DEQUANTIZE_KV(CAT(convert_, KEY_COMPRESSION_SCALE_COMPUTE_TYPE)(DECODE_INT4_NIBBLE(unpacked.s1)), comp_zp, comp_scale);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE lane_mask = (head_idx_index + 2 * sglid < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)1 : (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)0;
key_val0 *= lane_mask;
key_val1 *= lane_mask;
@@ -403,9 +422,9 @@ KERNEL(sdpa_opt)(
for (; head_idx_index + (KEY_BLOCK_SIZE * SUBGROUP_SIZE) <= K_HEAD_SIZE; head_idx_index += SUBGROUP_SIZE * KEY_BLOCK_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, KEY_BLOCK_SIZE, ptr, offset);
#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 KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KV_DEQUANT_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(KV_DEQUANT_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
@@ -438,9 +457,9 @@ KERNEL(sdpa_opt)(
for (; head_idx_index + (KEY_BLOCK_SIZE * SUBGROUP_SIZE) <= K_HEAD_SIZE; head_idx_index += SUBGROUP_SIZE * KEY_BLOCK_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, KEY_BLOCK_SIZE, ptr, offset);
#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 KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KV_DEQUANT_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(KV_DEQUANT_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
@@ -473,9 +492,9 @@ KERNEL(sdpa_opt)(
for (; head_idx_index + (KEY_BLOCK_SIZE * SUBGROUP_SIZE) <= K_HEAD_SIZE; head_idx_index += SUBGROUP_SIZE * KEY_BLOCK_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, KEY_BLOCK_SIZE, ptr, offset);
#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 KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KV_DEQUANT_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(KV_DEQUANT_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
@@ -508,9 +527,9 @@ KERNEL(sdpa_opt)(
for (; head_idx_index + (KEY_BLOCK_SIZE * SUBGROUP_SIZE) <= K_HEAD_SIZE; head_idx_index += SUBGROUP_SIZE * KEY_BLOCK_SIZE) {
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, KEY_BLOCK_SIZE, ptr, offset);
#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 KEY_BLOCK_UNCOMPRESSED MAKE_VECTOR_TYPE(KV_DEQUANT_TYPE, KEY_BLOCK_SIZE)
#define TO_KEY_BLOCK_UNCOMPRESSED_TYPE(val) CAT(convert_, KEY_BLOCK_UNCOMPRESSED)(val)
- #define QUERY_BLOCK MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, KEY_BLOCK_SIZE)
+ #define QUERY_BLOCK MAKE_VECTOR_TYPE(KV_DEQUANT_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
@@ -827,9 +846,9 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + (seq_len * SUBGROUP_SIZE) + sglid, 0);
- VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_VALUE_SCALE(GET_SCALE(val_zp, val_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_VALUE_ZP(GET_ZP(val_zp, val_scale, comp_offset));
#endif
#endif
@@ -854,14 +873,14 @@ KERNEL(sdpa_opt)(
// INT4 adjacent packing: shuffle to get correct byte, select nibble by lane parity
INPUT2_TYPE needed_byte = intel_sub_group_shuffle(value_packed, shuffle_src_p0);
char2 v_unpacked = unpack_to_char(*(uint4x2_t*)&needed_byte);
- VALUE_COMPRESSION_SCALE_TYPE value_val = (nibble_sel_p0 == 0 ?
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s0)) :
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s1)));
+ V_DEQUANT_TYPE value_val = (nibble_sel_p0 == 0 ?
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s0)) :
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s1)));
value_val = DEQUANTIZE_KV(value_val, sub_group_broadcast(comp_zp, i), sub_group_broadcast(comp_scale, i));
#elif IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
+ V_DEQUANT_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
#elif IS_KV_COMPRESSED
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
+ V_DEQUANT_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
@@ -905,9 +924,9 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + seq_len, 0);
- VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_VALUE_SCALE(GET_SCALE(val_zp, val_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_VALUE_ZP(GET_ZP(val_zp, val_scale, comp_offset));
#endif
#endif
@@ -926,14 +945,14 @@ KERNEL(sdpa_opt)(
// INT4 adjacent packing: shuffle to get correct byte, select nibble by lane parity
INPUT2_TYPE needed_byte = intel_sub_group_shuffle(value_packed, shuffle_src_p0);
char2 v_rem_unpacked = unpack_to_char(*(uint4x2_t*)&needed_byte);
- VALUE_COMPRESSION_SCALE_TYPE value_val = (nibble_sel_p0 == 0 ?
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_rem_unpacked.s0)) :
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_rem_unpacked.s1)));
+ V_DEQUANT_TYPE value_val = (nibble_sel_p0 == 0 ?
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_rem_unpacked.s0)) :
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_rem_unpacked.s1)));
value_val = DEQUANTIZE_KV(value_val, comp_zp, comp_scale);
#elif IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- const VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - comp_zp) * comp_scale;
+ const V_DEQUANT_TYPE value_val = (value_packed - comp_zp) * comp_scale;
#elif IS_KV_COMPRESSED
- const VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * comp_scale);
+ const V_DEQUANT_TYPE value_val = (value_packed * comp_scale);
#else
const INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
@@ -1712,9 +1731,9 @@ KERNEL(sdpa_opt)(
if (seq_len_calc_size >= SUBGROUP_SIZE) {
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, seq_len + sglid, 0);
- KEY_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(key_zp, key_scale, comp_offset);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_KEY_SCALE(GET_SCALE(key_zp, key_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- KEY_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(key_zp, key_scale, comp_offset);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_KEY_ZP(GET_ZP(key_zp, key_scale, comp_offset));
#endif
#endif
#if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
@@ -1739,8 +1758,8 @@ KERNEL(sdpa_opt)(
const INPUT1_TYPE packed_byte = KEY_BLOCK_READ(key_input, key_offset + key_row_idx * key_pitch + hi / 2);
#endif
char2 unpacked = unpack_to_char(*(uint4x2_t*)&packed_byte);
- KEY_COMPRESSION_SCALE_TYPE key_lo = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
- KEY_COMPRESSION_SCALE_TYPE key_hi = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_lo = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_hi = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
qk_acc[key_row_idx] = mad(TO_INPUT0_COMPUTE_TYPE(sub_group_broadcast(key_lo, i)), qvec_lo[i], qk_acc[key_row_idx]);
qk_acc[key_row_idx] = mad(TO_INPUT0_COMPUTE_TYPE(sub_group_broadcast(key_hi, i)), qvec_hi[i], qk_acc[key_row_idx]);
@@ -1767,10 +1786,10 @@ KERNEL(sdpa_opt)(
? KEY_BLOCK_READ(key_input, key_offset + key_row_idx * key_pitch + hi / 2) : (INPUT1_TYPE)0;
#endif
char2 unpacked = unpack_to_char(*(uint4x2_t*)&packed_byte);
- KEY_COMPRESSION_SCALE_TYPE key_lo = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
- KEY_COMPRESSION_SCALE_TYPE key_hi = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
- KEY_COMPRESSION_SCALE_TYPE lo_mask = (hi + 2 * sglid < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_TYPE)1 : (KEY_COMPRESSION_SCALE_TYPE)0;
- KEY_COMPRESSION_SCALE_TYPE hi_mask = (hi + 2 * sglid + 1 < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_TYPE)1 : (KEY_COMPRESSION_SCALE_TYPE)0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_lo = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_hi = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE lo_mask = (hi + 2 * sglid < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)1 : (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE hi_mask = (hi + 2 * sglid + 1 < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)1 : (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)0;
key_lo *= lo_mask;
key_hi *= hi_mask;
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
@@ -1803,9 +1822,9 @@ KERNEL(sdpa_opt)(
#endif
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- KEY_COMPRESSION_SCALE_TYPE key_vals = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);
+ KV_DEQUANT_TYPE key_vals = (TO_KV_DEQUANT_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);
#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));
+ KV_DEQUANT_TYPE key_vals = (TO_KV_DEQUANT_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
#else
INPUT1_COMPUTE_TYPE key_vals = DECODE_INPUT1_COMPUTE_TYPE(key_packed);
#endif
@@ -1829,9 +1848,9 @@ KERNEL(sdpa_opt)(
INPUT1_TYPE key_packed = (sglid < K_HEAD_SIZE_LEFTOVER) ? key_input[key_offset + key_row_idx * key_pitch + head_idx_index + sglid] : INPUT1_VAL_ZERO;
#endif
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- KEY_COMPRESSION_SCALE_TYPE key_vals = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);
+ KV_DEQUANT_TYPE key_vals = (TO_KV_DEQUANT_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);
#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));
+ KV_DEQUANT_TYPE key_vals = (TO_KV_DEQUANT_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
#else
INPUT1_COMPUTE_TYPE key_vals = DECODE_INPUT1_COMPUTE_TYPE(key_packed);
#endif
@@ -1844,9 +1863,9 @@ KERNEL(sdpa_opt)(
} else if (seq_len_calc_size > 0) {
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, seq_len + min(sglid, (uint)seq_len_calc_size - 1), 0);
- KEY_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(key_zp, key_scale, comp_offset);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_KEY_SCALE(GET_SCALE(key_zp, key_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- KEY_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(key_zp, key_scale, comp_offset);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_KEY_ZP(GET_ZP(key_zp, key_scale, comp_offset));
#endif
#endif
#if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
@@ -1862,8 +1881,8 @@ KERNEL(sdpa_opt)(
qvec_hi[i] = slm_query[(hi + 2 * i + 1) * TARGET_SEQ_LEN_BLOCK_SIZE + sglid];
}
unroll_for (uint key_row_idx = 0; key_row_idx < TARGET_SEQ_LEN_BLOCK_SIZE; key_row_idx++) {
- KEY_COMPRESSION_SCALE_TYPE key_lo = 0;
- KEY_COMPRESSION_SCALE_TYPE key_hi = 0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_lo = 0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_hi = 0;
if (key_row_idx < seq_len_calc_size) {
#ifdef BEAM_TABLE_TYPE
const INPUT1_TYPE packed_byte = KEY_BLOCK_READ(key_input, sub_group_broadcast(key_offset, key_row_idx) + hi / 2);
@@ -1871,8 +1890,8 @@ KERNEL(sdpa_opt)(
const INPUT1_TYPE packed_byte = KEY_BLOCK_READ(key_input, key_offset + key_row_idx * key_pitch_int4 + hi / 2);
#endif
char2 unpacked = unpack_to_char(*(uint4x2_t*)&packed_byte);
- key_lo = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
- key_hi = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ key_lo = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ key_hi = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
}
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
qk_acc[key_row_idx] = mad(TO_INPUT0_COMPUTE_TYPE(sub_group_broadcast(key_lo, i)), qvec_lo[i], qk_acc[key_row_idx]);
@@ -1892,8 +1911,8 @@ KERNEL(sdpa_opt)(
slm_query[(hi + 2 * i + 1) * TARGET_SEQ_LEN_BLOCK_SIZE + sglid] : (INPUT0_TYPE)0;
}
unroll_for (uint key_row_idx = 0; key_row_idx < TARGET_SEQ_LEN_BLOCK_SIZE; key_row_idx++) {
- KEY_COMPRESSION_SCALE_TYPE key_lo = 0;
- KEY_COMPRESSION_SCALE_TYPE key_hi = 0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_lo = 0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE key_hi = 0;
if (key_row_idx < seq_len_calc_size) {
#ifdef BEAM_TABLE_TYPE
const INPUT1_TYPE packed_byte = (hi / 2 + sglid < K_HEAD_SIZE / 2)
@@ -1903,11 +1922,11 @@ KERNEL(sdpa_opt)(
? KEY_BLOCK_READ(key_input, key_offset + key_row_idx * key_pitch_int4 + hi / 2) : (INPUT1_TYPE)0;
#endif
char2 unpacked = unpack_to_char(*(uint4x2_t*)&packed_byte);
- key_lo = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
- key_hi = DEQUANTIZE_KV(TO_KEY_COMPRESSION_SCALE_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ key_lo = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s0)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
+ key_hi = DEQUANTIZE_KV(TO_KV_DEQUANT_TYPE(DECODE_INT4_NIBBLE(unpacked.s1)), sub_group_broadcast(comp_zp, key_row_idx), sub_group_broadcast(comp_scale, key_row_idx));
}
- KEY_COMPRESSION_SCALE_TYPE lo_mask = (hi + 2 * sglid < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_TYPE)1 : (KEY_COMPRESSION_SCALE_TYPE)0;
- KEY_COMPRESSION_SCALE_TYPE hi_mask = (hi + 2 * sglid + 1 < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_TYPE)1 : (KEY_COMPRESSION_SCALE_TYPE)0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE lo_mask = (hi + 2 * sglid < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)1 : (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE hi_mask = (hi + 2 * sglid + 1 < K_HEAD_SIZE) ? (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)1 : (KEY_COMPRESSION_SCALE_COMPUTE_TYPE)0;
key_lo *= lo_mask;
key_hi *= hi_mask;
unroll_for (uint i = 0; i < SUBGROUP_SIZE; i++) {
@@ -1925,9 +1944,9 @@ KERNEL(sdpa_opt)(
#define KEY_BLOCK_READ(ptr, offset) BLOCK_READN(INPUT1_TYPE, 1, ptr, offset)
#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)
+ #define KEY_UNPACKED_TYPE KV_DEQUANT_TYPE
+ #define KEY_UNPACKED_VEC_TYPE MAKE_VECTOR_TYPE(KV_DEQUANT_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
+ #define TO_KEY_UNPACKED_TYPE(val) TO_KV_DEQUANT_TYPE(val)
#else
#define KEY_UNPACKED_TYPE INPUT1_COMPUTE_TYPE
#define KEY_UNPACKED_VEC_TYPE MAKE_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, TARGET_SEQ_LEN_BLOCK_SIZE)
@@ -1995,9 +2014,9 @@ KERNEL(sdpa_opt)(
const INPUT1_TYPE key_packed = (sglid < K_HEAD_SIZE_LEFTOVER) ? key_input[key_offset + key_row_idx * key_pitch + head_idx_index + sglid] : INPUT1_VAL_ZERO;
#endif
#if IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- KEY_COMPRESSION_SCALE_TYPE key_val = (TO_KEY_COMPRESSION_SCALE_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);
+ KV_DEQUANT_TYPE key_val = (TO_KV_DEQUANT_TYPE(key_packed) - sub_group_broadcast(comp_zp, key_row_idx)) * sub_group_broadcast(comp_scale, key_row_idx);
#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));
+ KV_DEQUANT_TYPE key_val = (TO_KV_DEQUANT_TYPE(key_packed) * sub_group_broadcast(comp_scale, key_row_idx));
#else
INPUT1_COMPUTE_TYPE key_val = DECODE_INPUT1_COMPUTE_TYPE(key_packed);
#endif
@@ -2344,9 +2363,9 @@ KERNEL(sdpa_opt)(
}
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + seq_len + sglid, 0);
- VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_VALUE_SCALE(GET_SCALE(val_zp, val_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_VALUE_ZP(GET_ZP(val_zp, val_scale, comp_offset));
#endif
#endif
#ifdef V_HEAD_SIZE_LEFTOVER
@@ -2364,14 +2383,14 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
INPUT2_TYPE needed_byte = intel_sub_group_shuffle(value_packed, shuffle_src_s1);
char2 v_unpacked = unpack_to_char(*(uint4x2_t*)&needed_byte);
- VALUE_COMPRESSION_SCALE_TYPE value_val = (nibble_sel_s1 == 0 ?
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s0)) :
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s1)));
+ V_DEQUANT_TYPE value_val = (nibble_sel_s1 == 0 ?
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s0)) :
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s1)));
value_val = DEQUANTIZE_KV(value_val, sub_group_broadcast(comp_zp, i), sub_group_broadcast(comp_scale, i));
#elif IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
+ V_DEQUANT_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
#elif IS_KV_COMPRESSED
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
+ V_DEQUANT_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
@@ -2394,14 +2413,14 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
INPUT2_TYPE needed_byte_elt = intel_sub_group_shuffle(value_packed, shuffle_src_s1);
char2 v_unpacked_elt = unpack_to_char(*(uint4x2_t*)&needed_byte_elt);
- VALUE_COMPRESSION_SCALE_TYPE value_val = (nibble_sel_s1 == 0 ?
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s0)) :
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s1)));
+ V_DEQUANT_TYPE value_val = (nibble_sel_s1 == 0 ?
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s0)) :
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s1)));
value_val = DEQUANTIZE_KV(value_val, sub_group_broadcast(comp_zp, i), sub_group_broadcast(comp_scale, i));
#elif IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
+ V_DEQUANT_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
#elif IS_KV_COMPRESSED
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
+ V_DEQUANT_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
@@ -2455,9 +2474,9 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + (seq_len * SUBGROUP_SIZE) + sglid, 0);
- VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_VALUE_SCALE(GET_SCALE(val_zp, val_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_VALUE_ZP(GET_ZP(val_zp, val_scale, comp_offset));
#endif
#endif // IS_KV_COMPRESSED
@@ -2480,14 +2499,14 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
INPUT2_TYPE needed_byte = intel_sub_group_shuffle(value_packed, shuffle_src_s1);
char2 v_unpacked = unpack_to_char(*(uint4x2_t*)&needed_byte);
- VALUE_COMPRESSION_SCALE_TYPE value_val = (nibble_sel_s1 == 0 ?
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s0)) :
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s1)));
+ V_DEQUANT_TYPE value_val = (nibble_sel_s1 == 0 ?
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s0)) :
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked.s1)));
value_val = DEQUANTIZE_KV(value_val, sub_group_broadcast(comp_zp, i), sub_group_broadcast(comp_scale, i));
#elif IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
+ V_DEQUANT_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
#elif IS_KV_COMPRESSED
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
+ V_DEQUANT_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
@@ -2510,14 +2529,14 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
INPUT2_TYPE needed_byte_elt = intel_sub_group_shuffle(value_packed, shuffle_src_s1);
char2 v_unpacked_elt = unpack_to_char(*(uint4x2_t*)&needed_byte_elt);
- VALUE_COMPRESSION_SCALE_TYPE value_val = (nibble_sel_s1 == 0 ?
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s0)) :
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s1)));
+ V_DEQUANT_TYPE value_val = (nibble_sel_s1 == 0 ?
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s0)) :
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_unpacked_elt.s1)));
value_val = DEQUANTIZE_KV(value_val, sub_group_broadcast(comp_zp, i), sub_group_broadcast(comp_scale, i));
#elif IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
+ V_DEQUANT_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, i)) * sub_group_broadcast(comp_scale, i);
#elif IS_KV_COMPRESSED
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
+ V_DEQUANT_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, i));
#else
INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#endif
@@ -2574,9 +2593,9 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1_idx / BROADCAST_GROUP_SIZE, start_partition_idx + min(seq_len_leftovers_start + sglid, seq_len_end - 1), 0);
- VALUE_COMPRESSION_SCALE_TYPE comp_scale = GET_SCALE(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_VALUE_SCALE(GET_SCALE(val_zp, val_scale, comp_offset));
#if USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = GET_ZP(val_zp, val_scale, comp_offset);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_VALUE_ZP(GET_ZP(val_zp, val_scale, comp_offset));
#endif
#endif
@@ -2604,14 +2623,14 @@ KERNEL(sdpa_opt)(
#if IS_KV_COMPRESSED && IS_INT4_COMPRESSED
INPUT2_TYPE needed_byte = intel_sub_group_shuffle(value_packed, shuffle_src_s1);
char2 v_left_unpacked = unpack_to_char(*(uint4x2_t*)&needed_byte);
- VALUE_COMPRESSION_SCALE_TYPE value_val = (nibble_sel_s1 == 0 ?
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_left_unpacked.s0)) :
- CAT(convert_, VALUE_COMPRESSION_SCALE_TYPE)(DECODE_INT4_NIBBLE(v_left_unpacked.s1)));
+ V_DEQUANT_TYPE value_val = (nibble_sel_s1 == 0 ?
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_left_unpacked.s0)) :
+ CAT(convert_, V_DEQUANT_TYPE)(DECODE_INT4_NIBBLE(v_left_unpacked.s1)));
value_val = DEQUANTIZE_KV(value_val, sub_group_broadcast(comp_zp, seq_len_idx), sub_group_broadcast(comp_scale, seq_len_idx));
#elif IS_KV_COMPRESSED && USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, seq_len_idx)) * sub_group_broadcast(comp_scale, seq_len_idx);
+ V_DEQUANT_TYPE value_val = (value_packed - sub_group_broadcast(comp_zp, seq_len_idx)) * sub_group_broadcast(comp_scale, seq_len_idx);
#elif IS_KV_COMPRESSED
- VALUE_COMPRESSION_SCALE_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, seq_len_idx));
+ V_DEQUANT_TYPE value_val = (value_packed * sub_group_broadcast(comp_scale, seq_len_idx));
#else
INPUT2_COMPUTE_TYPE value_val = DECODE_INPUT2_COMPUTE_TYPE(value_packed);
#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 251c7ba5a0a..70ff2bed74b 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
@@ -260,16 +260,16 @@ KERNEL(sdpa_ref)(
INPUT1_TYPE k_val_packed = key_input[key_offset];
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(KEY_COMPRESSION_SCALE, b_idx, b1 / BROADCAST_GROUP_SIZE, s, 0);
- KEY_COMPRESSION_SCALE_TYPE comp_scale = key_scale[comp_offset];
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_KEY_COMPRESSION_SCALE_COMPUTE_TYPE(key_scale[comp_offset]);
#if USE_ASYMMETRIC_QUANTIZATION && HAS_KV_CACHE_ZP_INPUT
- KEY_COMPRESSION_SCALE_TYPE comp_zp = key_zp[comp_offset];
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = TO_KEY_COMPRESSION_SCALE_COMPUTE_TYPE(DECODE_KEY_COMPRESSION_ZP_COMPUTE_TYPE(key_zp[comp_offset]));
#elif USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = key_scale[comp_offset + 1];
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_KEY_COMPRESSION_SCALE_COMPUTE_TYPE(key_scale[comp_offset + 1]);
#else
- KEY_COMPRESSION_SCALE_TYPE comp_zp = 0;
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = 0;
#endif
- KEY_COMPRESSION_SCALE_TYPE k_val = ((k_val_packed - comp_zp) * comp_scale);
+ KEY_COMPRESSION_SCALE_COMPUTE_TYPE k_val = (k_val_packed - comp_zp) * comp_scale;
#else
INPUT1_COMPUTE_T k_val = LOAD_INPUT1(k_val_packed);
@@ -368,16 +368,16 @@ KERNEL(sdpa_ref)(
const INPUT2_TYPE value_packed = value_input[value_offset];
#if IS_KV_COMPRESSED
const uint comp_offset = GET_COMPRESSION_INDEX(VALUE_COMPRESSION_SCALE, b_idx, b1 / BROADCAST_GROUP_SIZE, s, 0);
- VALUE_COMPRESSION_SCALE_TYPE comp_scale = val_scale[comp_offset];
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_scale = DECODE_VALUE_COMPRESSION_SCALE_COMPUTE_TYPE(val_scale[comp_offset]);
#if USE_ASYMMETRIC_QUANTIZATION && HAS_KV_CACHE_ZP_INPUT
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_zp[comp_offset];
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = TO_VALUE_COMPRESSION_SCALE_COMPUTE_TYPE(DECODE_VALUE_COMPRESSION_ZP_COMPUTE_TYPE(val_zp[comp_offset]));
#elif USE_ASYMMETRIC_QUANTIZATION
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = val_scale[comp_offset + 1];
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = DECODE_VALUE_COMPRESSION_SCALE_COMPUTE_TYPE(val_scale[comp_offset + 1]);
#else
- VALUE_COMPRESSION_SCALE_TYPE comp_zp = 0;
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE comp_zp = 0;
#endif
- VALUE_COMPRESSION_SCALE_TYPE value = ((value_packed - comp_zp) * comp_scale);
+ VALUE_COMPRESSION_SCALE_COMPUTE_TYPE value = (value_packed - comp_zp) * comp_scale;
#else
INPUT2_COMPUTE_T value = LOAD_INPUT2(value_packed);
#endif
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_kv_cache.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_kv_cache.cl
index 22e68ba1dd5..5bbf43281b3 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_kv_cache.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_kv_cache.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"
@@ -66,15 +67,15 @@ KERNEL(dynamic_quantize_gpu_kv_cache)(
// The innermost dimension is always processed in the loop inside the kernel
const uint x = 0;
- half grp_max = 0.001h;
- half max_value = INPUT0_VAL_MIN;
- half min_value = INPUT0_VAL_MAX;
+ const INPUT0_COMPUTE_TYPE grp_max = TO_INPUT0_COMPUTE_TYPE(0.001f);
+ INPUT0_COMPUTE_TYPE max_value = INPUT0_VAL_MIN;
+ INPUT0_COMPUTE_TYPE min_value = INPUT0_VAL_MAX;
- half val[INNERMOST_DIM_VALUE / SUBGROUP_SIZE];
+ INPUT0_COMPUTE_TYPE val[INNERMOST_DIM_VALUE / SUBGROUP_SIZE];
const uint input_offset = INPUT0_GET_INDEX(b, f, y, x);
unroll_for (uint i = 0; i < INNERMOST_DIM_VALUE / SUBGROUP_SIZE; i++) {
- val[i] = INPUT_BLOCK_READ(input, input_offset + i * SUBGROUP_SIZE);
+ val[i] = DECODE_INPUT0_COMPUTE_TYPE(INPUT_BLOCK_READ(input, input_offset + i * SUBGROUP_SIZE));
#if ASYMMETRIC_QUANTIZATION
max_value = fmax(max_value, val[i]);
min_value = fmin(min_value, val[i]);
@@ -136,15 +137,15 @@ KERNEL(dynamic_quantize_gpu_kv_cache)(
const uint scale_idx = FUNC_CALL(get_scales_offset)(OPTIONAL_SHAPE_INFO_TENSOR b, f, y, x);
if (grouped_indexes == 0 && sglid == 0) {
- output_scale[scale_idx] = (OUTPUT1_TYPE)(1.0f / scale_tmp); // dequant scale
+ output_scale[scale_idx] = TO_OUTPUT1_TYPE(1.0f / scale_tmp); // dequant scale
#if ASYMMETRIC_QUANTIZATION && !GROUP_SCALES_WITH_ZP
#if OUTPUT2_IS_FP
- output_zp[scale_idx] = (OUTPUT2_TYPE)(zp_tmp);
+ output_zp[scale_idx] = TO_OUTPUT2_TYPE(zp_tmp);
#else
output_zp[scale_idx] = convert_char_rte(zp_tmp);
#endif
#else
- output_scale[scale_idx + 1] = (OUTPUT1_TYPE)(zp_tmp); // zero-point (interleaved)
+ output_scale[scale_idx + 1] = TO_OUTPUT1_TYPE(zp_tmp); // zero-point (interleaved)
#endif
}
@@ -158,12 +159,12 @@ KERNEL(dynamic_quantize_gpu_kv_cache)(
ACCUMULATOR_TYPE diff_value = max_value == min_value ? (grp_max) : (max_value - min_value);
ACCUMULATOR_TYPE scale_tmp = (ACCUMULATOR_TYPE)((CHAR_MAX - CHAR_MIN) / diff_value);
ACCUMULATOR_TYPE zp_tmp = (ACCUMULATOR_TYPE)(-min_value * scale_tmp) + CHAR_MIN;
- OUTPUT1_TYPE scale = (OUTPUT1_TYPE)(scale_tmp);
- OUTPUT1_TYPE zp = (OUTPUT1_TYPE)(zp_tmp);
+ OUTPUT1_COMPUTE_TYPE scale = TO_OUTPUT1_COMPUTE_TYPE(scale_tmp);
+ OUTPUT1_COMPUTE_TYPE zp = TO_OUTPUT1_COMPUTE_TYPE(zp_tmp);
#else
max_value = work_group_reduce_max(max_value);
- OUTPUT1_TYPE scale = 127.0h / max_value;
+ OUTPUT1_COMPUTE_TYPE scale = TO_OUTPUT1_COMPUTE_TYPE(127.0h / max_value);
#endif
const uint output_offset = OUTPUT_GET_INDEX(b, f, y, x);
@@ -180,20 +181,20 @@ KERNEL(dynamic_quantize_gpu_kv_cache)(
if (grouped_indexes == 0 && sglid == 0) {
#if ASYMMETRIC_QUANTIZATION
- output_scale[scale_idx] = 1.0h / scale;
+ output_scale[scale_idx] = TO_OUTPUT1_TYPE(1.0h / scale);
#if GROUP_SCALES_WITH_ZP
- output_scale[scale_idx + 1] = zp;
+ output_scale[scale_idx + 1] = TO_OUTPUT1_TYPE(zp);
#else
#if OUTPUT2_IS_FP
- output_zp[scale_idx] = zp;
+ output_zp[scale_idx] = TO_OUTPUT2_TYPE(zp);
#else
output_zp[scale_idx] = convert_char_rte(zp);
#endif
#endif
#else
- output_scale[scale_idx] = 1.0h / scale;
+ output_scale[scale_idx] = TO_OUTPUT1_TYPE(1.0h / scale);
#endif
}
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.cl
index 9081abe3cb1..1be5dcbe020 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_opt.cl
@@ -6,6 +6,7 @@
#define IS_F8_F4 (IS_F8 || F4E2M1_OUTPUT)
#include "include/batch_headers/fetch_data.cl"
+#include "include/batch_headers/bf16_utils.cl"
#if IS_F8_F4
#include "include/batch_headers/common.cl"
#include "include/f8_utils.cl"
@@ -33,8 +34,8 @@
#define TO_TYPE_N_(type, n, x) convert_##type##n(x)
#define TO_TYPE_N_SAT_(type, n, x) _convert_##type##n##_sat(x)
#else
- #define SCALE_TYPE half
- #define TO_SCALE_TYPE(x) convert_half(x)
+ #define SCALE_TYPE INPUT0_COMPUTE_TYPE
+ #define TO_SCALE_TYPE(x) TO_INPUT0_COMPUTE_TYPE(x)
#define ACT_MIN_VAL 0.003h // Too small value may generate inf during 127/ACT_MIN_VAL
#define TO_TYPE_N_(type, n, x) convert_##type##n(x)
#define TO_TYPE_N_SAT_(type, n, x) convert_##type##n##_sat(x)
@@ -51,6 +52,8 @@
#define AS_TYPE_N(type, n, x) AS_TYPE_N_(type, n, x)
#define AS_INPUT_TYPE_N(x) AS_TYPE_N(INPUT0_TYPE, VEC_SIZE, x)
+#define INPUT_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, VEC_SIZE)
+
#if F4E2M1_OUTPUT
#if VEC_SIZE == 2
#define VSTORE_F4(vec, off, ptr) (*((ptr) + (off)) = (vec))
@@ -105,16 +108,16 @@ KERNEL(dynamic_quantize_gpu_opt)(
#endif
const uint quantize_block = QUANTIZE_GROUP_SIZE / 4;
- half4 input_0[quantize_block];
+ MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, 4) input_0[quantize_block];
MAKE_VECTOR_TYPE(OUTPUT_TYPE, 4) quantized_value[quantize_block];
- half max[quantize_block];
+ INPUT0_COMPUTE_TYPE max[quantize_block];
unroll_for (uint i = 0 ; i < quantize_block; ++i) {
- input_0[i] = vload4(0, &input[input_offset + i * 4]);
+ input_0[i] = DECODE_INPUT0_COMPUTE_VECTOR_TYPE(vload4(0, &input[input_offset + i * 4]), 4);
max[i] = fmax(fmax(fabs(input_0[i][0]), fabs(input_0[i][1])), fmax(fabs(input_0[i][2]), fabs(input_0[i][3])));
}
- half max_value = fmax(ACT_MIN_VAL, max[0]);
+ INPUT0_COMPUTE_TYPE max_value = fmax((INPUT0_COMPUTE_TYPE)ACT_MIN_VAL, max[0]);
for (uint i = 1; i < quantize_block; i++) {
max_value = fmax(max_value, max[i]);
}
@@ -134,7 +137,7 @@ KERNEL(dynamic_quantize_gpu_opt)(
quantized_value[i] = DQ_COMPUTE_OUTPUT_VALUE_N(4, input_0[i], quan_scale);
vstore4(quantized_value[i].data, 0, (char*)(&output[output_offset + i * 4]));
#else
- quantized_value[i] = convert_char4_rte(input_0[i] * (half4)quan_scale);
+ quantized_value[i] = convert_char4_rte(input_0[i] * (MAKE_VECTOR_TYPE(SCALE_TYPE, 4))quan_scale);
FOR_PRECOMPUTED_REDUCTION(precomputed_reduction += quantized_value[i][0] + quantized_value[i][1] + quantized_value[i][2] + quantized_value[i][3]);
vstore4(quantized_value[i], 0, &output[output_offset + i * 4]);
#endif // IS_F8
@@ -199,23 +202,23 @@ KERNEL(dynamic_quantize_gpu_opt)(
#if BLOCKS_PER_GROUP > 1
const uint local_id = get_local_id(1);
- __local half local_mem_max[BLOCK_NUM];
- __local half local_mem_min[BLOCK_NUM];
+ __local INPUT0_COMPUTE_TYPE local_mem_max[BLOCK_NUM];
+ __local INPUT0_COMPUTE_TYPE local_mem_min[BLOCK_NUM];
FOR_PRECOMPUTED_REDUCTION(__local int local_mem_reduction[BLOCK_NUM]);
#endif
- MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_SIZE) val;
- MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_SIZE) abs_val;
- half grp_max = ACT_MIN_VAL;
- half grp_min = ACT_MIN_VAL;
- half max_value = 0.0h;
- half min_value = 0.0h;
+ INPUT_VEC_TYPE val;
+ INPUT_VEC_TYPE abs_val;
+ INPUT0_COMPUTE_TYPE grp_max = ACT_MIN_VAL;
+ INPUT0_COMPUTE_TYPE grp_min = ACT_MIN_VAL;
+ INPUT0_COMPUTE_TYPE max_value = 0.0h;
+ INPUT0_COMPUTE_TYPE min_value = 0.0h;
if (is_valid_block) {
- val = AS_INPUT_TYPE_N(VLOAD_N(0, input + input_offset + (blockid * block_size)));
+ val = DECODE_INPUT0_COMPUTE_VECTOR_TYPE(VLOAD_N(0, input + input_offset + (blockid * block_size)), VEC_SIZE);
} else {
// Initialize with zero for skipped blocks
- val = (MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_SIZE))(0);
+ val = (INPUT_VEC_TYPE)(0);
}
#if ASYMMETRIC_QUANTIZATION
@@ -383,21 +386,21 @@ KERNEL(dynamic_quantize_gpu_opt)(
const uint iteration = ALIGNED_BLOCK_NUM / BLOCK_NUM;
- __local half local_mem_max[BLOCK_NUM];
- __local half local_mem_min[BLOCK_NUM];
+ __local INPUT0_COMPUTE_TYPE local_mem_max[BLOCK_NUM];
+ __local INPUT0_COMPUTE_TYPE local_mem_min[BLOCK_NUM];
- MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_SIZE) val[iteration];
- MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_SIZE) abs_val;
- half grp_max = ACT_MIN_VAL;
- half grp_min = ACT_MIN_VAL;
- half max_value = 0.0h;
- half min_value = 0.0h;
+ INPUT_VEC_TYPE val[iteration];
+ INPUT_VEC_TYPE abs_val;
+ INPUT0_COMPUTE_TYPE grp_max = ACT_MIN_VAL;
+ INPUT0_COMPUTE_TYPE grp_min = ACT_MIN_VAL;
+ INPUT0_COMPUTE_TYPE max_value = 0.0h;
+ INPUT0_COMPUTE_TYPE min_value = 0.0h;
unroll_for(int i = 0; i < iteration; ++i) {
if ((local_id * iteration + i) >= TOTAL_BLOCK_NUM)
continue;
- val[i] = AS_INPUT_TYPE_N(VLOAD_N(0, input + offset + ((local_id * iteration + i) * block_size)));
+ val[i] = DECODE_INPUT0_COMPUTE_VECTOR_TYPE(VLOAD_N(0, input + offset + ((local_id * iteration + i) * block_size)), VEC_SIZE);
#if ASYMMETRIC_QUANTIZATION
unroll_for (int j = 0; j < VEC_SIZE; j++) {
max_value = fmax(max_value, val[i][j]);
@@ -457,11 +460,11 @@ MAKE_VECTOR_TYPE(SCALE_TYPE, VEC_SIZE) val_scaled = TO_TYPE_N(SCALE_TYPE, VEC_SI
MAKE_VECTOR_TYPE(OUTPUT_TYPE, VEC_SIZE) out = DQ_COMPUTE_OUTPUT_VALUE_N(VEC_SIZE, val[i], scale);
VSTORE_N(out.data, 0, (char*)(&output[offset + ((local_id * iteration + i) * block_size)]));
#elif ASYMMETRIC_QUANTIZATION
- val[i] = TO_TYPE_N(INPUT0_TYPE, VEC_SIZE, val_scaled);
+ val[i] = TO_TYPE_N(INPUT0_COMPUTE_TYPE, VEC_SIZE, val_scaled);
val[i] += zp;
VSTORE_N(CAT(CONVERT_UCHAR_N, _rte)(val[i]), 0, output + offset + ((local_id * iteration + i) * block_size));
#else // i8 symmetric
- val[i] = TO_TYPE_N(INPUT0_TYPE, VEC_SIZE, val_scaled);
+ val[i] = TO_TYPE_N(INPUT0_COMPUTE_TYPE, VEC_SIZE, val_scaled);
VSTORE_N(CAT(CONVERT_CHAR_N, _rte)(val[i]), 0, output + offset + ((local_id * iteration + i) * block_size));
#endif
}
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_ref.cl
index f14d0d874e8..9137713eb99 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_ref.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/dynamic_quantize_gpu_ref.cl
@@ -6,6 +6,7 @@
#define IS_F8_F4 (IS_F8 || F4E2M1_OUTPUT)
#include "include/batch_headers/fetch_data.cl"
+#include "include/batch_headers/bf16_utils.cl"
#if IS_F8_F4
#include "include/f8_utils.cl"
#include "include/dynamic_quantize_utils.cl"
@@ -17,15 +18,17 @@
#define UINT64_MAX 0xFFFFFFFFFFFFFFFF
+#define INPUT_VEC8_TYPE MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, 8)
+
#if IS_F8_F4
#define SCALE_TYPE float
#define TO_SCALE_TYPE(x) _convert_float(x)
#define TO_SCALE_TYPE_8(x) convert_float8(x)
#define ACT_MIN_VAL 0.000000059604645h // min half dtype val
#else
- #define SCALE_TYPE half
- #define TO_SCALE_TYPE(x) convert_half(x)
- #define TO_SCALE_TYPE_8(x) convert_half8(x)
+ #define SCALE_TYPE INPUT0_COMPUTE_TYPE
+ #define TO_SCALE_TYPE(x) TO_INPUT0_COMPUTE_TYPE(x)
+ #define TO_SCALE_TYPE_8(x) CAT(convert_, MAKE_VECTOR_TYPE(INPUT0_COMPUTE_TYPE, 8))(x)
#define ACT_MIN_VAL 0.003h // Too small value may generate inf during 127/ACT_MIN_VAL
#endif
@@ -99,29 +102,29 @@ KERNEL(dynamic_quantize_gpu_ref)(
const uint scale_idx = OUTPUT1_GET_INDEX_SAFE(b, f, out_y, x);
#endif
- half grp_max = ACT_MIN_VAL;
- half max_val = INPUT0_VAL_MIN;
- half min_val = INPUT0_VAL_MAX;
+ INPUT0_COMPUTE_TYPE grp_max = ACT_MIN_VAL;
+ INPUT0_COMPUTE_TYPE max_val = INPUT0_VAL_MIN;
+ INPUT0_COMPUTE_TYPE min_val = INPUT0_VAL_MAX;
for (int b_off = 0; b_off < (GROUP_SIZE_DIM0 == 1 ? 1 : INPUT0_BATCH_NUM); b_off++) {
for (int f_off = 0; f_off < (GROUP_SIZE_DIM1 == 1 ? 1 : INPUT0_FEATURE_NUM); f_off++) {
for (int y_off = 0; y_off < (GROUP_SIZE_DIM2 == UINT64_MAX ? INPUT0_SIZE_Y : GROUP_SIZE_DIM2); y_off++) {
// It is assumed that grouped quantization happens only for 3d input case where we don't have x axis
#if GROUP_SIZE_DIM3 == 1
const uint offset = INPUT0_GET_INDEX(b + b_off, f + f_off, y + y_off, x);
- half val = input[offset];
+ INPUT0_COMPUTE_TYPE val = DECODE_INPUT0_COMPUTE_TYPE(input[offset]);
#if ASYMMETRIC_QUANTIZATION
max_val = fmax(max_val, val);
min_val = fmin(min_val, val);
#else
- half abs_val = fabs(val);
+ INPUT0_COMPUTE_TYPE abs_val = fabs(val);
max_val = fmax(max_val, abs_val);
#endif
#else
const uint offset = INPUT0_GET_INDEX(b + b_off, f + f_off, y + y_off, 0);
int x;
for (x = 0; x < INPUT0_SIZE_X / 8; x++) {
- half8 val = as_half8(vload8(0, (ushort*)input + offset + x * 8));
- half8 abs_val = fabs(val);
+ INPUT_VEC8_TYPE val = DECODE_INPUT0_COMPUTE_VECTOR_TYPE(vload8(0, &input[offset + x * 8]), 8);
+ INPUT_VEC8_TYPE abs_val = fabs(val);
for (int j = 0; j < 8; j++) {
#if ASYMMETRIC_QUANTIZATION
max_val = fmax(max_val, val[j]);
@@ -133,7 +136,7 @@ KERNEL(dynamic_quantize_gpu_ref)(
}
x *= 8;
for (; x < INPUT0_SIZE_X; x++) {
- half val = input[offset + x];
+ INPUT0_COMPUTE_TYPE val = DECODE_INPUT0_COMPUTE_TYPE(input[offset + x]);
#if ASYMMETRIC_QUANTIZATION
max_val = fmax(max_val, val);
min_val = fmin(min_val, val);
@@ -158,8 +161,8 @@ KERNEL(dynamic_quantize_gpu_ref)(
# else // !UNSIGNED_OUTPUT
ACCUMULATOR_TYPE zp_tmp = (ACCUMULATOR_TYPE)(-min_val * scale_tmp) + CHAR_MIN;
# endif
- OUTPUT1_TYPE scale = (OUTPUT1_TYPE)(scale_tmp);
- OUTPUT1_TYPE zp = (OUTPUT1_TYPE)(zp_tmp);
+ OUTPUT1_COMPUTE_TYPE scale = TO_OUTPUT1_COMPUTE_TYPE(scale_tmp);
+ OUTPUT1_COMPUTE_TYPE zp = TO_OUTPUT1_COMPUTE_TYPE(zp_tmp);
#else // !ASYMMETRIC_QUANTIZATION
#if IS_MXFP
SCALE_TYPE scale = (SCALE_TYPE)(DQ_COMPUTE_MXFP_SCALE(max_val));
@@ -177,7 +180,7 @@ KERNEL(dynamic_quantize_gpu_ref)(
const uint in_offset = INPUT0_GET_INDEX(b + b_off, f + f_off, y + y_off, x);
const uint out_offset = OUTPUT_GET_INDEX(b + b_off, f + f_off, y + y_off, x);
- half val = input[in_offset];
+ INPUT0_COMPUTE_TYPE val = DECODE_INPUT0_COMPUTE_TYPE(input[in_offset]);
val *= scale;
#if ASYMMETRIC_QUANTIZATION
val += zp;
@@ -203,8 +206,8 @@ KERNEL(dynamic_quantize_gpu_ref)(
const uint byte_offset = out_offset / ELEMENTS_PER_BYTE;
int x;
for (x = 0; x < INPUT0_SIZE_X / 8; x++) {
- half8 val = as_half8(vload8(0, (ushort*)input + in_offset + x * 8));
- val = convert_half8(TO_SCALE_TYPE_8(val) * (MAKE_VECTOR_TYPE(SCALE_TYPE, 8))scale);
+ INPUT_VEC8_TYPE val = DECODE_INPUT0_COMPUTE_VECTOR_TYPE(vload8(0, &input[in_offset + x * 8]), 8);
+ val = CAT(convert_, INPUT_VEC8_TYPE)(TO_SCALE_TYPE_8(val) * (MAKE_VECTOR_TYPE(SCALE_TYPE, 8))scale);
#if ASYMMETRIC_QUANTIZATION
val += zp;
#endif
@@ -220,7 +223,7 @@ KERNEL(dynamic_quantize_gpu_ref)(
}
x *= 8;
for (; x < INPUT0_SIZE_X; x++) {
- half val = input[in_offset + x];
+ INPUT0_COMPUTE_TYPE val = DECODE_INPUT0_COMPUTE_TYPE(input[in_offset + x]);
val *= scale;
#if ASYMMETRIC_QUANTIZATION
val += zp;
@@ -250,10 +253,10 @@ KERNEL(dynamic_quantize_gpu_ref)(
output_scale[scale_idx] = TO_OUTPUT1_TYPE(1.0f / scale);
FOR_PRECOMPUTED_REDUCTION(output_precomputed_reduction[scale_idx] = precomputed_reduction);
#if ASYMMETRIC_QUANTIZATION && GROUP_SCALES_WITH_ZP
- output_scale[scale_idx + 1] = zp;
+ output_scale[scale_idx + 1] = TO_OUTPUT1_TYPE(zp);
#elif ASYMMETRIC_QUANTIZATION
#if OUTPUT2_IS_FP
- output_zp[scale_idx] = zp;
+ output_zp[scale_idx] = TO_OUTPUT2_TYPE(zp);
#elif UNSIGNED_OUTPUT
output_zp[scale_idx] = convert_uchar_rte(zp);
#else
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cpp
index 32654b4f47f..34160e58565 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt.cpp
@@ -79,6 +79,7 @@ static size_t get_match_vector_size(const dynamic_quantize_params& params) {
ParamsKey DynamicQuantizeKernelOpt::GetSupportedKey() const {
ParamsKey k;
k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::BF16);
k.EnableOutputDataType(Datatype::UINT8);
k.EnableOutputDataType(Datatype::INT8);
k.EnableOutputDataType(Datatype::F4E2M1);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt_kv_cache.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt_kv_cache.cpp
index f02faaa6ed7..3c137d3b6a3 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt_kv_cache.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_opt_kv_cache.cpp
@@ -89,6 +89,7 @@ static size_t get_per_iter_elements_number(const dynamic_quantize_params& params
ParamsKey DynamicQuantizeKernelKVCache::GetSupportedKey() const {
ParamsKey k;
k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::BF16);
k.EnableOutputDataType(Datatype::INT8);
k.EnableOutputDataType(Datatype::UINT8);
k.EnableOutputDataType(Datatype::INT4);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_ref.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_ref.cpp
index 1290c95e836..b036eee226a 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_ref.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/dynamic_quantize/dynamic_quantize_kernel_ref.cpp
@@ -10,6 +10,7 @@ namespace kernel_selector {
ParamsKey DynamicQuantizeKernelRef::GetSupportedKey() const {
ParamsKey k;
k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::BF16);
k.EnableOutputDataType(Datatype::INT8);
k.EnableOutputDataType(Datatype::UINT8);
k.EnableOutputDataType(Datatype::F4E2M1);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/kv_cache_compression.cpp b/src/plugins/intel_gpu/src/plugin/transformations/kv_cache_compression.cpp
index dcaa3f96877..a4686376612 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/kv_cache_compression.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/kv_cache_compression.cpp
@@ -231,7 +231,8 @@ KVCacheCompressionMatcher::KVCacheCompressionMatcher(ov::element::Type compressi
config.quantization_type = quantization_type;
config.group_sizes = get_shape_group_sizes(sdpa_node->get_input1_transpose_order());
config.quantization_dt = element::i8;
- config.scale_dt = query_node->get_output_element_type(0);
+ const auto scale_dt = query_node->get_output_element_type(0);
+ config.scale_dt = scale_dt;
config.scales_zp_output_order = get_scales_output_order(sdpa_node->get_input1_transpose_order());
config.output_storage_type = output_storage_type;
@@ -241,7 +242,7 @@ KVCacheCompressionMatcher::KVCacheCompressionMatcher(ov::element::Type compressi
// introduces a large systematic bias: error = (zp - round(zp)) * scale
// applied uniformly to every element in the group.
bool is_int4 = ov::element::Type(compression_dt).bitwidth() == 4;
- config.zp_dt = (supports_immad && !is_int4) ? element::i8 : query_node->get_output_element_type(0);
+ config.zp_dt = (supports_immad && !is_int4) ? element::i8 : scale_dt;
}
key_past_rv_node = update_past_read_value(key_past_rv_node, config);
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cpp
index 5ee68d78e86..7b92b7852c9 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/dynamic_quantize_gpu_test.cpp
@@ -19,6 +19,7 @@
#include "fully_connected_inst.h"
#include <cmath>
+#include <limits>
using namespace cldnn;
using namespace ::tests;
@@ -565,3 +566,309 @@ TEST_F(dynamic_quantization_gpu_tests, dynamic_quantize_opt_gs16) {
data_types::i8, data_types::f16, data_types::dynamic, OutputStorageType::Planar,
"dynamic_quantize_gpu_opt");
}
+
+// For bf16 the input storage type is a raw ushort, so the kernels have to decode bf16 (instead of
+// relying on an implicit type conversion, which would reinterpret the bit pattern, e.g. 0.5 -> 16128).
+// The scale is derived from the decoded group min/max, so it is compared against a value computed on
+// the host from the very same bf16 data (comparing GPU kernels against each other would not catch a
+// decoding bug shared by all of them).
+// With beyond_f16_range some elements exceed the f16 range (only representable for bf16 input), so the
+// kernels must not narrow the decoded bf16 values to half (|x| > 65504 would turn into inf and give inf/NaN
+// scales). The resulting scales (max(|x|) / 127) still fit into the f16 scale storage.
+static void test_scale_is_computed_from_decoded_values(const std::string& impl_name,
+ ov::element::Type input_type,
+ uint64_t group_size,
+ bool beyond_f16_range) {
+ auto& engine = get_test_engine();
+ tests::random_generator rg(GET_SUITE_NAME);
+
+ const ov::Shape data_shape = {1, 1, 4096};
+ const bool per_token = group_size == std::numeric_limits<uint64_t>::max();
+ const size_t elements_per_group = per_token ? data_shape.back() : static_cast<size_t>(group_size);
+ const size_t groups_num = ov::shape_size(data_shape) / elements_per_group;
+
+ // Round trip the data through the input precision first so that the host reference below
+ // sees exactly the values the kernel reads
+ auto round_to_input_type = [&](float value) {
+ return input_type == ov::element::bf16 ? static_cast<float>(ov::bfloat16(value))
+ : static_cast<float>(ov::float16(value));
+ };
+ std::vector<float> host_values;
+ for (auto value : rg.generate_random_1d<float>(ov::shape_size(data_shape), -16.0f, 20.0f)) {
+ host_values.push_back(round_to_input_type(value));
+ }
+ // Make sure the groups have different maxima so that misaligned scales are detectable
+ for (size_t g = 0; g < groups_num; g++) {
+ host_values[g * elements_per_group] = 1.0f + static_cast<float>(g);
+ }
+ if (beyond_f16_range) {
+ // Put a few values beyond the f16 range (max 65504) into each even-indexed group, with both signs
+ for (size_t g = 0; g < groups_num; g += 2) {
+ const float magnitude = 1.0e5f + 2.0e5f * static_cast<float>(g) / static_cast<float>(groups_num);
+ host_values[g * elements_per_group + 1] = round_to_input_type(magnitude);
+ host_values[g * elements_per_group + elements_per_group - 1] = round_to_input_type(-1.5f * magnitude);
+ }
+ }
+
+ const data_types input_dt = input_type == ov::element::bf16 ? data_types::bf16 : data_types::f16;
+
+ auto input_mem = engine.allocate_memory({data_shape, input_dt, format::bfyx});
+ if (input_type == ov::element::bf16) {
+ std::vector<ov::bfloat16> data;
+ for (auto value : host_values) {
+ data.emplace_back(value);
+ }
+ set_values(input_mem, data);
+ } else {
+ std::vector<ov::float16> data;
+ for (auto value : host_values) {
+ data.emplace_back(value);
+ }
+ set_values(input_mem, data);
+ }
+
+ // Symmetric i8 per-group quantization: one scale per group holding max(|x|) / 127
+ std::vector<float> expected_scales(groups_num, 0.0f);
+ for (size_t g = 0; g < groups_num; g++) {
+ float group_max = 0.0f;
+ for (size_t e = 0; e < elements_per_group; e++) {
+ group_max = std::max(group_max, std::abs(host_values[g * elements_per_group + e]));
+ }
+ expected_scales[g] = group_max / 127.0f;
+ }
+
+ dynamic_quantize::Attributes dq_config;
+ dq_config.quantization_type = QuantizationType::Symmetric;
+ dq_config.quantization_dt = data_types::i8;
+ dq_config.scale_dt = data_types::f16;
+ dq_config.zp_dt = data_types::dynamic;
+ dq_config.group_sizes = {1, 1, group_size};
+ dq_config.scales_zp_output_order = {0, 1, 2};
+ dq_config.output_storage_type = OutputStorageType::Planar;
+
+ topology topology(
+ input_layout("input", layout{data_shape, input_dt, format::bfyx}),
+ dynamic_quantize("dyn_quan_prim", input_info("input"), dq_config),
+ reorder("out_data", input_info("dyn_quan_prim", 0), layout{ov::PartialShape::dynamic(3), data_types::f32, format::bfyx}),
+ reorder("out_scale", input_info("dyn_quan_prim", 1), layout{ov::PartialShape::dynamic(3), data_types::f32, format::bfyx}));
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{
+ {"dyn_quan_prim", { format::bfyx, impl_name, impl_types::ocl }}}));
+
+ network network(engine, topology, config);
+ network.set_input_data("input", input_mem);
+
+ auto outputs = network.execute();
+ ASSERT_TRUE(outputs.count("out_scale") > 0);
+ ASSERT_TRUE(outputs.count("out_data") > 0);
+ auto scale_mem = outputs.at("out_scale").get_memory();
+ auto data_mem = outputs.at("out_data").get_memory();
+ cldnn::mem_lock<float, mem_lock_type::read> scale_ptr(scale_mem, get_test_stream());
+ cldnn::mem_lock<float, mem_lock_type::read> data_ptr(data_mem, get_test_stream());
+
+ const auto scale_layout = outputs.at("out_scale").get_layout();
+ ASSERT_GE(scale_ptr.size(), groups_num)
+ << "impl: " << impl_name << ", scale layout: " << scale_layout.to_string();
+ ASSERT_EQ(data_ptr.size(), host_values.size());
+ for (size_t g = 0; g < groups_num; g++) {
+ const float actual_scale = static_cast<float>(scale_ptr[g]);
+ ASSERT_TRUE(std::isfinite(actual_scale))
+ << "impl: " << impl_name << ", group: " << g << ", actual: " << actual_scale;
+ ASSERT_NEAR(actual_scale, expected_scales[g], expected_scales[g] * 0.05f)
+ << "impl: " << impl_name << ", group: " << g << ", actual: " << actual_scale
+ << ", expected: " << expected_scales[g];
+ // The quantized values have to match the host values scaled by the expected scale
+ for (size_t e = 0; e < elements_per_group; e++) {
+ const size_t idx = g * elements_per_group + e;
+ ASSERT_NEAR(data_ptr[idx], host_values[idx] / expected_scales[g], 1.0f)
+ << "impl: " << impl_name << ", group: " << g << ", element: " << e;
+ }
+ }
+}
+
+class dynamic_quantization_bf16_input_tests : public dynamic_quantization_gpu_tests,
+ public ::testing::WithParamInterface<std::tuple<std::string, ov::element::Type>> {};
+
+TEST_P(dynamic_quantization_bf16_input_tests, scale_is_computed_from_decoded_values) {
+ test_scale_is_computed_from_decoded_values(std::get<0>(GetParam()), std::get<1>(GetParam()), 64, false);
+}
+
+INSTANTIATE_TEST_SUITE_P(bf16_input,
+ dynamic_quantization_bf16_input_tests,
+ ::testing::Combine(::testing::Values("dynamic_quantize_gpu_ref", "dynamic_quantize_gpu_opt"),
+ ::testing::Values(ov::element::f16, ov::element::bf16)),
+ [](const ::testing::TestParamInfo<std::tuple<std::string, ov::element::Type>>& info) {
+ return (std::get<0>(info.param).find("_ref") != std::string::npos ? "ref_" : "opt_") +
+ std::get<1>(info.param).get_type_name();
+ });
+
+// bf16 only: values beyond the f16 range. Group sizes cover the small group, large group and per token
+// paths of the opt kernel.
+class dynamic_quantization_bf16_beyond_f16_range_tests : public dynamic_quantization_gpu_tests,
+ public ::testing::WithParamInterface<std::tuple<std::string, uint64_t>> {};
+
+TEST_P(dynamic_quantization_bf16_beyond_f16_range_tests, scale_is_finite) {
+ test_scale_is_computed_from_decoded_values(std::get<0>(GetParam()), ov::element::bf16, std::get<1>(GetParam()), true);
+}
+
+INSTANTIATE_TEST_SUITE_P(bf16_input,
+ dynamic_quantization_bf16_beyond_f16_range_tests,
+ ::testing::Combine(::testing::Values("dynamic_quantize_gpu_ref", "dynamic_quantize_gpu_opt"),
+ ::testing::Values(uint64_t{32}, uint64_t{64}, std::numeric_limits<uint64_t>::max())),
+ [](const ::testing::TestParamInfo<std::tuple<std::string, uint64_t>>& info) {
+ const auto group_size = std::get<1>(info.param);
+ return std::string(std::get<0>(info.param).find("_ref") != std::string::npos ? "ref_" : "opt_") +
+ (group_size == std::numeric_limits<uint64_t>::max() ? std::string("per_token")
+ : "gs" + std::to_string(group_size));
+ });
+
+// bf16 input with bf16 scales (and fp zero points), as used for the KV cache of bf16 models. bf16 scale/zp
+// buffers are stored as ushort, so the kernels have to encode them as bf16 (not with a numeric cast).
+// Groups alternate between large magnitudes (up to 1e7, f16 scales would overflow or lose all precision)
+// and small ones; scales, zero points and dequantized values are checked against the host data.
+struct dq_bf16_scales_params {
+ std::string impl_name;
+ QuantizationType quantization_type;
+ OutputStorageType storage_type;
+ data_types zp_dt;
+};
+
+class dynamic_quantization_bf16_scales_tests : public dynamic_quantization_gpu_tests,
+ public ::testing::WithParamInterface<dq_bf16_scales_params> {};
+
+TEST_P(dynamic_quantization_bf16_scales_tests, scales_and_zp_are_stored_as_bf16) {
+ const auto& p = GetParam();
+ auto& engine = get_test_engine();
+ tests::random_generator rg(GET_SUITE_NAME);
+
+ const bool is_asym = p.quantization_type == QuantizationType::Asymmetric;
+ const bool is_interleaved = p.storage_type == OutputStorageType::InterleavedScalesZP;
+ const bool has_zp_output = is_asym && !is_interleaved;
+
+ const ov::Shape data_shape = {1, 4, 8, 64};
+ const size_t elements_per_group = data_shape.back();
+ const size_t groups_num = ov::shape_size(data_shape) / elements_per_group;
+ const std::vector<float> magnitudes = {1.0e5f, 2.5e5f, 1.0e6f, 7.0e6f, 1.0e7f, 3.0f, 0.5f, 20.0f};
+
+ // Asymmetric groups are shifted, so that the zero point is not trivial
+ std::vector<float> host_values;
+ for (size_t g = 0; g < groups_num; g++) {
+ const float magnitude = magnitudes[g % magnitudes.size()];
+ const float low = is_asym ? -0.5f * magnitude : -magnitude;
+ for (auto value : rg.generate_random_1d<float>(elements_per_group, low, magnitude)) {
+ host_values.push_back(static_cast<float>(ov::bfloat16(value)));
+ }
+ }
+
+ auto input_mem = engine.allocate_memory({data_shape, data_types::bf16, format::bfyx});
+ std::vector<ov::bfloat16> input_data;
+ for (auto value : host_values) {
+ input_data.emplace_back(value);
+ }
+ set_values(input_mem, input_data);
+
+ dynamic_quantize::Attributes dq_config;
+ dq_config.quantization_type = p.quantization_type;
+ dq_config.quantization_dt = data_types::i8;
+ dq_config.scale_dt = data_types::bf16;
+ dq_config.zp_dt = is_asym ? p.zp_dt : data_types::dynamic;
+ dq_config.group_sizes = {1, 1, 1, UINT64_MAX};
+ dq_config.scales_zp_output_order = {0, 1, 2, 3};
+ dq_config.output_storage_type = p.storage_type;
+
+ const auto dyn_f32_layout = layout{ov::PartialShape::dynamic(4), data_types::f32, format::bfyx};
+ topology topology(input_layout("input", layout{{-1, 4, -1, 64}, data_types::bf16, format::bfyx}),
+ dynamic_quantize("dyn_quan_prim", input_info("input"), dq_config),
+ reorder("out_data", input_info("dyn_quan_prim", 0), dyn_f32_layout),
+ reorder("out_scale", input_info("dyn_quan_prim", 1), dyn_f32_layout));
+ if (has_zp_output) {
+ topology.add(reorder("out_zp", input_info("dyn_quan_prim", 2), dyn_f32_layout));
+ }
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{
+ {"dyn_quan_prim", {format::bfyx, p.impl_name, impl_types::ocl}}}));
+
+ network network(engine, topology, config);
+ network.set_input_data("input", input_mem);
+ auto outputs = network.execute();
+
+ auto data_mem = outputs.at("out_data").get_memory();
+ auto scale_mem = outputs.at("out_scale").get_memory();
+ cldnn::mem_lock<float, mem_lock_type::read> data_ptr(data_mem, get_test_stream());
+ cldnn::mem_lock<float, mem_lock_type::read> scale_ptr(scale_mem, get_test_stream());
+ ASSERT_EQ(data_ptr.size(), host_values.size());
+ ASSERT_EQ(scale_ptr.size(), groups_num * (is_interleaved ? 2 : 1));
+
+ std::vector<float> zp_values(groups_num, 0.0f);
+ if (has_zp_output) {
+ auto zp_mem = outputs.at("out_zp").get_memory();
+ cldnn::mem_lock<float, mem_lock_type::read> zp_ptr(zp_mem, get_test_stream());
+ ASSERT_EQ(zp_ptr.size(), groups_num);
+ for (size_t g = 0; g < groups_num; g++) {
+ zp_values[g] = zp_ptr[g];
+ }
+ } else if (is_interleaved) {
+ for (size_t g = 0; g < groups_num; g++) {
+ zp_values[g] = scale_ptr[g * 2 + 1];
+ }
+ }
+
+ for (size_t g = 0; g < groups_num; g++) {
+ float group_min = std::numeric_limits<float>::max();
+ float group_max = std::numeric_limits<float>::lowest();
+ float group_abs_max = 0.0f;
+ for (size_t e = 0; e < elements_per_group; e++) {
+ const float value = host_values[g * elements_per_group + e];
+ group_min = std::min(group_min, value);
+ group_max = std::max(group_max, value);
+ group_abs_max = std::max(group_abs_max, std::abs(value));
+ }
+
+ // Dequantization scale and zero point: x ~= (q - zp) * scale
+ const float expected_scale = is_asym ? (group_max - group_min) / 255.0f : group_abs_max / 127.0f;
+ const float expected_zp = is_asym ? -group_min / expected_scale - 128.0f : 0.0f;
+ const float actual_scale = scale_ptr[is_interleaved ? g * 2 : g];
+ const float actual_zp = zp_values[g];
+
+ ASSERT_TRUE(std::isfinite(actual_scale)) << "group: " << g;
+ // bf16 has an 8 bit mantissa (relative rounding error <= 2^-9)
+ ASSERT_NEAR(actual_scale, expected_scale, expected_scale * 0.01f)
+ << "group: " << g << ", actual: " << actual_scale << ", expected: " << expected_scale;
+ // fp zero points are rounded to bf16 (step 0.5 for |zp| in [64, 128)), i8 zero points to an integer
+ ASSERT_NEAR(actual_zp, expected_zp, 1.0f)
+ << "group: " << g << ", actual: " << actual_zp << ", expected: " << expected_zp;
+
+ for (size_t e = 0; e < elements_per_group; e++) {
+ const size_t idx = g * elements_per_group + e;
+ const float dequantized = (data_ptr[idx] - actual_zp) * actual_scale;
+ ASSERT_NEAR(dequantized, host_values[idx], 2.0f * expected_scale)
+ << "group: " << g << ", element: " << e << ", q: " << data_ptr[idx];
+ }
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ bf16_scales,
+ dynamic_quantization_bf16_scales_tests,
+ ::testing::Values(
+ dq_bf16_scales_params{"dynamic_quantize_gpu_kv_cache", QuantizationType::Symmetric, OutputStorageType::Planar, data_types::dynamic},
+ dq_bf16_scales_params{"dynamic_quantize_gpu_kv_cache", QuantizationType::Asymmetric, OutputStorageType::Planar, data_types::bf16},
+ dq_bf16_scales_params{"dynamic_quantize_gpu_kv_cache", QuantizationType::Asymmetric, OutputStorageType::Planar, data_types::i8},
+ dq_bf16_scales_params{"dynamic_quantize_gpu_kv_cache", QuantizationType::Asymmetric, OutputStorageType::InterleavedScalesZP, data_types::bf16},
+ dq_bf16_scales_params{"dynamic_quantize_gpu_ref", QuantizationType::Symmetric, OutputStorageType::Planar, data_types::dynamic},
+ dq_bf16_scales_params{"dynamic_quantize_gpu_ref", QuantizationType::Asymmetric, OutputStorageType::Planar, data_types::bf16},
+ dq_bf16_scales_params{"dynamic_quantize_gpu_ref", QuantizationType::Asymmetric, OutputStorageType::Planar, data_types::i8},
+ dq_bf16_scales_params{"dynamic_quantize_gpu_ref", QuantizationType::Asymmetric, OutputStorageType::InterleavedScalesZP, data_types::bf16}),
+ [](const ::testing::TestParamInfo<dq_bf16_scales_params>& info) {
+ const auto& p = info.param;
+ std::string name = p.impl_name.find("_ref") != std::string::npos ? "ref" : "kv_cache";
+ if (p.quantization_type == QuantizationType::Symmetric) {
+ return name + "_sym";
+ }
+ name += p.storage_type == OutputStorageType::InterleavedScalesZP ? "_asym_interleaved" : "_asym_planar";
+ return name + (p.zp_dt == data_types::i8 ? "_i8_zp" : "_bf16_zp");
+ });
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 f36ca28b39d..45ed04a24c8 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
@@ -34,6 +34,7 @@
#include "openvino/op/parameter.hpp"
#include <cstddef>
+#include <type_traits>
#include <vector>
using namespace cldnn;
@@ -71,7 +72,7 @@ struct sdpa_test_params {
: head_size(h_size), num_heads(n_heads), kv_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), bit_width(0), asymmetric(false), dt(dt){}
-
+
// constructor for quantization tests
sdpa_test_params(int h_size, int q_heads, int kv_heads, int seq_q, int seq_kv, int b, int bits, bool asym, data_types dt = data_types::f16)
: head_size(h_size), num_heads(q_heads), kv_num_heads(kv_heads), sequence_length_q(seq_q),
@@ -307,6 +308,7 @@ INSTANTIATE_TEST_SUITE_P(
sdpa_test_params{64, 10, 77, 77, 1, true}, // two ranks mask
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, true, 0.125f, false, 0.0f, data_types::bf16}, // bf16 scale_val
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{64, 32, 128, 128, 2, true, data_types::bf16}, // bf16 dynamic
@@ -949,11 +951,12 @@ TEST(sdpa_gpu_micro, transposed_v_matches_non_transposed_v_multi_tile) {
// host-dequantized KV, so the two networks operate on identical effective KV
// and any divergence isolates the kernel's dequant + attention math.
// ---------------------------------------------------------------------------
+template <typename T>
struct kv_quant_result {
std::vector<int8_t> packed; // quantized (INT4: two nibbles per byte)
- std::vector<ov::float16> dequantized; // (q - zp) * scale, full head_size
- std::vector<ov::float16> scales;
- std::vector<ov::float16> zero_points;
+ std::vector<T> dequantized; // (q - zp) * scale, full head_size
+ std::vector<T> scales;
+ std::vector<T> zero_points;
};
enum class kv_quant_granularity {
@@ -989,14 +992,15 @@ static std::vector<T> load_bin_as(const std::string& path) {
// provided_scale, when non-null, supplies the per-token scales ([batch, heads, seq])
// instead of deriving (and randomizing) them; the zero-point is still derived for the asymmetric
// case so it matches the given scale.
-static kv_quant_result quantize_kv_per_token(const std::vector<ov::float16>& src,
+template <typename T>
+static kv_quant_result<T> quantize_kv_per_token(const std::vector<T>& src,
int batch,
int seq,
int heads,
int head_size,
int bit_width,
bool seq_major = true,
- const std::vector<ov::float16>* provided_scale = nullptr,
+ const std::vector<T>* provided_scale = nullptr,
bool symmetric = false) {
// Symmetric uses a signed range centered on zero (no zero-point); asymmetric int4 uses
// unsigned nibbles [0, 15] with a zero-point.
@@ -1005,12 +1009,12 @@ static kv_quant_result quantize_kv_per_token(const std::vector<ov::float16>& src
const int packed_hs = bit_width == 4 ? head_size / 2 : head_size;
const size_t token_count = static_cast<size_t>(batch) * heads * seq;
- kv_quant_result r;
+ kv_quant_result<T> r;
r.packed.assign(static_cast<size_t>(batch) * seq * heads * packed_hs, 0);
- r.dequantized.assign(static_cast<size_t>(batch) * seq * heads * head_size, ov::float16(0.0f));
- r.scales.assign(token_count, ov::float16(0.0f));
+ r.dequantized.assign(static_cast<size_t>(batch) * seq * heads * head_size, T(0.0f));
+ r.scales.assign(token_count, T(0.0f));
if (!symmetric)
- r.zero_points.assign(token_count, ov::float16(0.0f));
+ r.zero_points.assign(token_count, T(0.0f));
const auto elem_base = [&](int b, int s, int h) -> size_t {
return seq_major ? ((static_cast<size_t>(b) * seq + s) * heads + h) * head_size : ((static_cast<size_t>(b) * heads + h) * seq + s) * head_size;
@@ -1062,10 +1066,10 @@ static kv_quant_result quantize_kv_per_token(const std::vector<ov::float16>& src
}
if (scale == 0.0f)
scale = 1.0f; // degenerate (constant) token: avoid div-by-zero
- r.scales[token] = ov::float16(scale);
+ r.scales[token] = T(scale);
if (!symmetric) {
// deq = (q - zp) * scale, so min_v maps to q_min: zp = q_min - min_v / scale.
- r.zero_points[token] = ov::float16(std::round(static_cast<float>(q_min) - min_v / scale));
+ r.zero_points[token] = T(std::round(static_cast<float>(q_min) - min_v / scale));
}
}
}
@@ -1085,7 +1089,7 @@ static kv_quant_result quantize_kv_per_token(const std::vector<ov::float16>& src
const float v = static_cast<float>(src[base + d]);
int q = static_cast<int>(std::lround(v / scale + zp));
q = std::max(q_min, std::min(q_max, q));
- r.dequantized[base + d] = ov::float16((static_cast<float>(q) - zp) * scale);
+ r.dequantized[base + d] = T((static_cast<float>(q) - zp) * scale);
pack_value(packed_base, d, q);
}
}
@@ -1096,26 +1100,27 @@ static kv_quant_result quantize_kv_per_token(const std::vector<ov::float16>& src
// Per-channel KV quantization: one scale/zp per (batch, head, channel), shared by all tokens.
// The compression buffers are laid out as [batch, heads, 1, head_size].
-static kv_quant_result quantize_kv_per_channel(const std::vector<ov::float16>& src,
+template <typename T>
+static kv_quant_result<T> quantize_kv_per_channel(const std::vector<T>& src,
int batch,
int seq,
int heads,
int head_size,
int bit_width,
bool seq_major = true,
- const std::vector<ov::float16>* provided_scale = nullptr,
+ const std::vector<T>* provided_scale = nullptr,
bool symmetric = false) {
const int q_min = symmetric ? -(bit_width == 4 ? 7 : 127) : (bit_width == 4 ? 0 : -128);
const int q_max = symmetric ? (bit_width == 4 ? 7 : 127) : (bit_width == 4 ? 15 : 127);
const int packed_hs = bit_width == 4 ? head_size / 2 : head_size;
const size_t channel_count = static_cast<size_t>(batch) * heads * head_size;
- kv_quant_result r;
+ kv_quant_result<T> r;
r.packed.assign(static_cast<size_t>(batch) * seq * heads * packed_hs, 0);
- r.dequantized.assign(static_cast<size_t>(batch) * seq * heads * head_size, ov::float16(0.0f));
- r.scales.assign(channel_count, ov::float16(0.0f));
+ r.dequantized.assign(static_cast<size_t>(batch) * seq * heads * head_size, T(0.0f));
+ r.scales.assign(channel_count, T(0.0f));
if (!symmetric)
- r.zero_points.assign(channel_count, ov::float16(0.0f));
+ r.zero_points.assign(channel_count, T(0.0f));
const auto elem_base = [&](int b, int s, int h) -> size_t {
return seq_major ? ((static_cast<size_t>(b) * seq + s) * heads + h) * head_size
@@ -1164,10 +1169,10 @@ static kv_quant_result quantize_kv_per_channel(const std::vector<ov::float16>& s
if (scale == 0.0f)
scale = 1.0f;
- r.scales[channel] = ov::float16(scale);
+ r.scales[channel] = T(scale);
if (!symmetric)
r.zero_points[channel] =
- ov::float16(std::round(static_cast<float>(q_min) - min_v / scale));
+ T(std::round(static_cast<float>(q_min) - min_v / scale));
}
}
}
@@ -1184,7 +1189,7 @@ static kv_quant_result quantize_kv_per_channel(const std::vector<ov::float16>& s
const float v = static_cast<float>(src[base + d]);
int q = static_cast<int>(std::lround(v / scale + zp));
q = std::max(q_min, std::min(q_max, q));
- r.dequantized[base + d] = ov::float16((static_cast<float>(q) - zp) * scale);
+ r.dequantized[base + d] = T((static_cast<float>(q) - zp) * scale);
pack_value(packed_base, d, q);
}
}
@@ -1197,14 +1202,15 @@ static kv_quant_result quantize_kv_per_channel(const std::vector<ov::float16>& s
// Per-channel symmetric quantization that reproduces the GQA decomposition's write path.
// INT4 values use a +8 storage bias and are exposed to SDPA as asymmetric u4 with zero point 8.
// INT8 values retain their signed representation and are exposed as symmetric i8.
-static kv_quant_result quantize_kv_per_channel_gqa_decomp(const std::vector<ov::float16>& src,
+template <typename T>
+static kv_quant_result<T> quantize_kv_per_channel_gqa_decomp(const std::vector<T>& src,
int batch,
int seq,
int heads,
int head_size,
int bit_width,
bool seq_major = true,
- const std::vector<ov::float16>* provided_scale = nullptr,
+ const std::vector<T>* provided_scale = nullptr,
bool symmetric = false) {
OPENVINO_ASSERT(bit_width == 4 || bit_width == 8, "GQA decomposition supports only INT4 and INT8 KV caches");
OPENVINO_ASSERT(symmetric, "GQA decomposition uses symmetric KV quantization");
@@ -1214,12 +1220,12 @@ static kv_quant_result quantize_kv_per_channel_gqa_decomp(const std::vector<ov::
const int packed_hs = bit_width == 4 ? head_size / 2 : head_size;
const size_t channel_count = static_cast<size_t>(batch) * heads * head_size;
- kv_quant_result r;
+ kv_quant_result<T> r;
r.packed.assign(static_cast<size_t>(batch) * seq * heads * packed_hs, 0);
- r.dequantized.assign(static_cast<size_t>(batch) * seq * heads * head_size, ov::float16(0.0f));
- r.scales.assign(channel_count, ov::float16(0.0f));
+ r.dequantized.assign(static_cast<size_t>(batch) * seq * heads * head_size, T(0.0f));
+ r.scales.assign(channel_count, T(0.0f));
if (bit_width == 4)
- r.zero_points.assign(channel_count, ov::float16(8.0f));
+ r.zero_points.assign(channel_count, T(8.0f));
const auto elem_base = [&](int b, int s, int h) -> size_t {
return seq_major ? ((static_cast<size_t>(b) * seq + s) * heads + h) * head_size
@@ -1254,7 +1260,7 @@ static kv_quant_result quantize_kv_per_channel_gqa_decomp(const std::vector<ov::
if (scale == 0.0f)
scale = 1.0f;
- r.scales[channel] = ov::float16(scale);
+ r.scales[channel] = T(scale);
}
}
}
@@ -1270,7 +1276,7 @@ static kv_quant_result quantize_kv_per_channel_gqa_decomp(const std::vector<ov::
const float v = static_cast<float>(src[base + d]);
int q = static_cast<int>(std::nearbyint(v / scale));
q = std::max(q_min, std::min(q_max, q));
- r.dequantized[base + d] = ov::float16(static_cast<float>(q) * scale);
+ r.dequantized[base + d] = T(static_cast<float>(q) * scale);
if (bit_width == 4) {
const int nibble = q + 8;
auto& byte = r.packed[packed_base + d / 2];
@@ -1289,9 +1295,20 @@ static kv_quant_result quantize_kv_per_channel_gqa_decomp(const std::vector<ov::
return r;
}
+template <typename T>
static void run_compressed_kv_sdpa_test(const sdpa_test_params& params,
kv_quant_granularity granularity,
- bool gqa_decomp = false) {
+ bool gqa_decomp = false,
+ const ov::element::Type& zp_et_in = ov::element::dynamic) {
+ // The compressed KV path has to work for f16 and bf16 Q/K/V as well. bf16 scales/zero-points are
+ // stored as raw ushorts, so the SDPA kernels have to decode them instead of using the storage type.
+ constexpr bool is_bf16 = std::is_same_v<T, ov::bfloat16>;
+ const data_types dt = is_bf16 ? data_types::bf16 : data_types::f16;
+ const ov::element::Type et = ov::element::from<T>();
+ const ov::element::Type zp_et = zp_et_in.is_dynamic() ? et : zp_et_in;
+ OPENVINO_ASSERT(zp_et == et || zp_et == ov::element::i8,
+ "Compressed KV SDPA test supports zero-points of the data type or i8 only, got: ", zp_et);
+ const bool zp_is_integral = zp_et == ov::element::i8;
tests::random_generator rg;
rg.set_seed(GET_SUITE_NAME);
auto& engine = get_test_engine();
@@ -1317,18 +1334,18 @@ static void run_compressed_kv_sdpa_test(const sdpa_test_params& params,
const int packed_head_size = bit_width == 4 ? head_size / 2 : head_size;
// Q and original float K/V use [batch, heads, sequence, head_size].
- auto q_data = rg.generate_random_1d<ov::float16>(static_cast<size_t>(batch) * seq_q * q_num_heads * head_size, -1.0f, 1.0f);
- auto k_orig = rg.generate_random_1d<ov::float16>(static_cast<size_t>(batch) * seq_kv * kv_num_heads * head_size, -1.0f, 1.0f);
- auto v_orig = rg.generate_random_1d<ov::float16>(static_cast<size_t>(batch) * seq_kv * kv_num_heads * head_size, -1.0f, 1.0f);
+ auto q_data = rg.generate_random_1d<T>(static_cast<size_t>(batch) * seq_q * q_num_heads * head_size, -1.0f, 1.0f);
+ auto k_orig = rg.generate_random_1d<T>(static_cast<size_t>(batch) * seq_kv * kv_num_heads * head_size, -1.0f, 1.0f);
+ auto v_orig = rg.generate_random_1d<T>(static_cast<size_t>(batch) * seq_kv * kv_num_heads * head_size, -1.0f, 1.0f);
const auto quantize_kv = [&]() {
if (gqa_decomp) {
OPENVINO_ASSERT(granularity == kv_quant_granularity::per_channel && !asymmetric,
"GQA decomposition requires symmetric per-channel quantization");
- return &quantize_kv_per_channel_gqa_decomp;
+ return &quantize_kv_per_channel_gqa_decomp<T>;
}
- return granularity == kv_quant_granularity::per_channel ? quantize_kv_per_channel
- : quantize_kv_per_token;
+ return granularity == kv_quant_granularity::per_channel ? quantize_kv_per_channel<T>
+ : quantize_kv_per_token<T>;
}();
auto k_q = quantize_kv(k_orig,
batch,
@@ -1349,16 +1366,17 @@ static void run_compressed_kv_sdpa_test(const sdpa_test_params& params,
nullptr,
/*symmetric=*/!asymmetric);
- // Q (and therefore the compute type) follows params.dt; KV stays f16 / compressed.
- const layout q_layout({batch, q_num_heads, seq_q, head_size}, params.dt, format::bfyx);
- const layout kv_deq_layout({batch, kv_num_heads, seq_kv, head_size}, data_types::f16, format::bfyx);
+ // Q (and therefore the compute type) follows params.dt, or bf16 for the bf16 test bodies; the dequantized
+ // KV reference stays in the compressed precision (f16, or bf16 for the bf16 bodies).
+ const layout q_layout({batch, q_num_heads, seq_q, head_size}, is_bf16 ? data_types::bf16 : params.dt, format::bfyx);
+ const layout kv_deq_layout({batch, kv_num_heads, seq_kv, head_size}, dt, format::bfyx);
// INT4 stores two adjacent head-dimension values in each byte: [B, H, S, D/2].
const layout kv_packed_layout({batch, kv_num_heads, seq_kv, packed_head_size}, data_types::i8, format::bfyx);
- const layout comp_layout(granularity == kv_quant_granularity::per_channel
- ? ov::PartialShape{batch, kv_num_heads, 1, head_size}
- : ov::PartialShape{batch, kv_num_heads, seq_kv, 1},
- data_types::f16,
- format::bfyx);
+ const ov::PartialShape comp_shape = granularity == kv_quant_granularity::per_channel
+ ? ov::PartialShape{batch, kv_num_heads, 1, head_size}
+ : ov::PartialShape{batch, kv_num_heads, seq_kv, 1};
+ const layout comp_layout(comp_shape, dt, format::bfyx);
+ const layout zp_layout(comp_shape, zp_is_integral ? data_types::i8 : dt, format::bfyx);
const ov::Shape expected_packed_shape = {static_cast<size_t>(batch),
static_cast<size_t>(kv_num_heads),
@@ -1397,7 +1415,7 @@ static void run_compressed_kv_sdpa_test(const sdpa_test_params& params,
{},
false);
topo.add(prim);
- topo.add(reorder("result", input_info("sdpa"), format::bfyx, data_types::f16));
+ topo.add(reorder("result", input_info("sdpa"), format::bfyx, dt));
ExecutionConfig cfg = get_test_default_config(engine);
cfg.set_property(ov::intel_gpu::allow_new_shape_infer(true));
@@ -1415,23 +1433,36 @@ static void run_compressed_kv_sdpa_test(const sdpa_test_params& params,
auto v_mem = engine.allocate_memory(kv_packed_layout);
auto k_comp_mem = engine.allocate_memory(comp_layout);
auto v_comp_mem = engine.allocate_memory(comp_layout);
- auto k_zp_mem = has_storage_zero_point ? engine.allocate_memory(comp_layout) : nullptr;
- auto v_zp_mem = has_storage_zero_point ? engine.allocate_memory(comp_layout) : nullptr;
+ auto k_zp_mem = has_storage_zero_point ? engine.allocate_memory(zp_layout) : nullptr;
+ auto v_zp_mem = has_storage_zero_point ? engine.allocate_memory(zp_layout) : nullptr;
set_values(k_mem, k_q.packed);
set_values(v_mem, v_q.packed);
set_values(k_comp_mem, k_q.scales);
set_values(v_comp_mem, v_q.scales);
if (has_storage_zero_point) {
- set_values(k_zp_mem, k_q.zero_points);
- set_values(v_zp_mem, v_q.zero_points);
+ if (zp_is_integral) {
+ // The quantizer already rounds zero-points to whole numbers; only the storage type of the
+ // zp buffer differs, so converting back to int8 is lossless.
+ const auto to_int8_zp = [](const std::vector<T>& zps) {
+ std::vector<int8_t> out(zps.size());
+ for (size_t i = 0; i < zps.size(); ++i)
+ out[i] = static_cast<int8_t>(std::lround(static_cast<float>(zps[i])));
+ return out;
+ };
+ set_values(k_zp_mem, to_int8_zp(k_q.zero_points));
+ set_values(v_zp_mem, to_int8_zp(v_q.zero_points));
+ } else {
+ set_values(k_zp_mem, k_q.zero_points);
+ set_values(v_zp_mem, v_q.zero_points);
+ }
}
scaled_dot_product_attention::QuantizationAttributes qa;
qa.quantization_type = has_storage_zero_point ? ov::op::internal::DynamicQuantize::QuantizationType::Asymmetric
: ov::op::internal::DynamicQuantize::QuantizationType::Symmetric;
qa.quantization_dt = bit_width == 4 ? (has_storage_zero_point ? ov::element::u4 : ov::element::i4) : ov::element::i8;
- qa.scale_dt = ov::element::f16;
- qa.zp_dt = ov::element::f16;
+ qa.scale_dt = et;
+ qa.zp_dt = zp_et;
qa.group_sizes = granularity == kv_quant_granularity::per_channel
? std::vector<uint64_t>{1, 1, UINT64_MAX, 1}
: std::vector<uint64_t>{1, 1, 1, UINT64_MAX};
@@ -1446,15 +1477,15 @@ static void run_compressed_kv_sdpa_test(const sdpa_test_params& params,
topo.add(input_layout("v_scale", comp_layout));
std::vector<input_info> inputs = {input_info("q"), input_info("k"), input_info("v"), input_info("k_scale"), input_info("v_scale")};
if (has_storage_zero_point) {
- topo.add(input_layout("k_zp", comp_layout));
- topo.add(input_layout("v_zp", comp_layout));
+ topo.add(input_layout("k_zp", zp_layout));
+ topo.add(input_layout("v_zp", zp_layout));
inputs.emplace_back("k_zp");
inputs.emplace_back("v_zp");
}
auto prim = scaled_dot_product_attention("sdpa", inputs, true, -1, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, qa, true);
topo.add(prim);
- topo.add(reorder("result", input_info("sdpa"), format::bfyx, data_types::f16));
+ topo.add(reorder("result", input_info("sdpa"), format::bfyx, dt));
ExecutionConfig cfg = get_test_default_config(engine);
cfg.set_property(ov::intel_gpu::allow_new_shape_infer(true));
@@ -1477,42 +1508,64 @@ static void run_compressed_kv_sdpa_test(const sdpa_test_params& params,
auto ref_mem = make_ref_output();
auto opt_mem = make_opt_output();
- cldnn::mem_lock<ov::float16, mem_lock_type::read> ref_ptr(ref_mem, get_test_stream());
- cldnn::mem_lock<ov::float16, mem_lock_type::read> opt_ptr(opt_mem, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> ref_ptr(ref_mem, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> opt_ptr(opt_mem, get_test_stream());
ASSERT_EQ(ref_ptr.size(), opt_ptr.size());
for (size_t i = 0; i < opt_ptr.size(); ++i) {
ASSERT_FALSE(std::isnan(static_cast<float>(opt_ptr[i]))) << "NaN in compressed output at index " << i;
}
const float sim = cosineSimilarity(ref_ptr, opt_ptr);
- ASSERT_GE(sim, 0.999f) << "bit_width=" << bit_width << ", asymmetric=" << asymmetric << " cosine similarity too low: " << sim;
+ // bf16 has a shorter mantissa, so allow a slightly lower similarity for it.
+ const float min_sim = is_bf16 ? 0.99f : 0.999f;
+ ASSERT_GE(sim, min_sim) << "bit_width=" << bit_width << ", asymmetric=" << asymmetric << " cosine similarity too low: " << sim;
}
struct sdpa_gpu_compressed_kv_test_base : public ::testing::TestWithParam<sdpa_test_params> {
static std::string PrintToStringParamName(const testing::TestParamInfo<sdpa_test_params>& info) {
const auto& p = info.param;
+ const std::string shape = "_" + std::to_string(p.sequence_length_q) + "x" + std::to_string(p.sequence_length_kv);
return std::string("int") + std::to_string(p.bit_width) + (p.asymmetric ? "_asymmetric_" : "_symmetric_") +
- (p.num_heads == p.kv_num_heads ? "mha_" : "gqa_") + (p.sequence_length_q == 1 ? "decode" : "prefill") + (p.dt == data_types::f32 ? "_f32" : "");
+ (p.num_heads == p.kv_num_heads ? "mha_" : "gqa_") + (p.sequence_length_q == 1 ? "decode" : "prefill") + shape +
+ (p.dt == data_types::f32 ? "_f32" : "");
}
};
struct sdpa_gpu_compressed_kv_per_channel_test : public sdpa_gpu_compressed_kv_test_base {};
TEST_P(sdpa_gpu_compressed_kv_per_channel_test, compare_with_gpu_dequantized_reference) {
- run_compressed_kv_sdpa_test(GetParam(), kv_quant_granularity::per_channel);
+ run_compressed_kv_sdpa_test<ov::float16>(GetParam(), kv_quant_granularity::per_channel);
}
struct sdpa_gpu_compressed_kv_per_token_test : public sdpa_gpu_compressed_kv_test_base {};
TEST_P(sdpa_gpu_compressed_kv_per_token_test, compare_with_gpu_dequantized_reference) {
- run_compressed_kv_sdpa_test(GetParam(), kv_quant_granularity::per_token);
+ run_compressed_kv_sdpa_test<ov::float16>(GetParam(), kv_quant_granularity::per_token);
+}
+
+struct sdpa_gpu_compressed_kv_bf16_test : public sdpa_gpu_compressed_kv_test_base {};
+
+TEST_P(sdpa_gpu_compressed_kv_bf16_test, compare_with_gpu_dequantized_reference) {
+ run_compressed_kv_sdpa_test<ov::bfloat16>(GetParam(), kv_quant_granularity::per_token);
+}
+
+struct sdpa_gpu_compressed_kv_int4_bf16_per_token_test : public sdpa_gpu_compressed_kv_test_base {};
+
+TEST_P(sdpa_gpu_compressed_kv_int4_bf16_per_token_test, compare_with_gpu_dequantized_reference) {
+ run_compressed_kv_sdpa_test<ov::bfloat16>(GetParam(), kv_quant_granularity::per_token);
+}
+
+struct sdpa_gpu_compressed_kv_bf16_int8_zp_test : public sdpa_gpu_compressed_kv_test_base {};
+
+TEST_P(sdpa_gpu_compressed_kv_bf16_int8_zp_test, compare_with_gpu_dequantized_reference) {
+ run_compressed_kv_sdpa_test<ov::bfloat16>(GetParam(), kv_quant_granularity::per_token, /*gqa_decomp=*/false, ov::element::i8);
}
// Verify the INT4 and INT8 cache encodings produced by the GQA decomposition.
struct sdpa_gpu_gqa_decomp_test : public sdpa_gpu_compressed_kv_test_base {};
TEST_P(sdpa_gpu_gqa_decomp_test, compare_with_gpu_dequantized_reference) {
- run_compressed_kv_sdpa_test(GetParam(), kv_quant_granularity::per_channel, /*gqa_decomp=*/true);
+ run_compressed_kv_sdpa_test<ov::float16>(GetParam(), kv_quant_granularity::per_channel, /*gqa_decomp=*/true);
}
INSTANTIATE_TEST_SUITE_P(smoke_sdpa_gpu_compressed_kv_per_channel,
@@ -1541,16 +1594,50 @@ INSTANTIATE_TEST_SUITE_P(smoke_sdpa_gpu_compressed_kv_per_token,
sdpa_test_params{128, 40, 10, 1, 512, 1, 8, false},
sdpa_test_params{128, 40, 40, 512, 512, 1, 8, false},
sdpa_test_params{128, 40, 40, 1, 512, 1, 8, false},
- sdpa_test_params{128, 40, 10, 512, 512, 1, 8, true},
+ // Asymmetric prefill stays at 128x128: the 512x512 asymmetric case runs out of the
+ // GPU resources on the CI machine (CL_OUT_OF_RESOURCES) while the symmetric one
+ // passes. The 512 long KV is still covered by the decode cases.
+ sdpa_test_params{128, 40, 10, 128, 128, 1, 8, true},
sdpa_test_params{128, 40, 10, 1, 512, 1, 8, true},
- sdpa_test_params{128, 40, 40, 512, 512, 1, 8, true},
+ sdpa_test_params{128, 40, 40, 128, 128, 1, 8, true},
sdpa_test_params{128, 40, 40, 1, 512, 1, 8, true},
// f32 compute: dequantized f16 KV must be cast to the Q compute type
sdpa_test_params{128, 40, 10, 512, 512, 1, 8, false, data_types::f32},
- sdpa_test_params{128, 40, 10, 512, 512, 1, 8, true, data_types::f32},
- sdpa_test_params{128, 40, 40, 512, 512, 1, 8, true, data_types::f32}),
+ sdpa_test_params{128, 40, 10, 128, 128, 1, 8, true, data_types::f32},
+ sdpa_test_params{128, 40, 40, 128, 128, 1, 8, true, data_types::f32}),
sdpa_gpu_compressed_kv_per_token_test::PrintToStringParamName);
+INSTANTIATE_TEST_SUITE_P(smoke_sdpa_gpu_compressed_kv_bf16,
+ sdpa_gpu_compressed_kv_bf16_test,
+ ::testing::Values(sdpa_test_params{128, 40, 10, 512, 512, 1, 8, false},
+ sdpa_test_params{128, 40, 10, 1, 512, 1, 8, false},
+ sdpa_test_params{128, 40, 40, 512, 512, 1, 8, false},
+ sdpa_test_params{128, 40, 40, 1, 512, 1, 8, false},
+ // Asymmetric prefill at 128x128 for the same CI resource reason as the per-token suite.
+ sdpa_test_params{128, 40, 10, 128, 128, 1, 8, true},
+ sdpa_test_params{128, 40, 10, 1, 512, 1, 8, true},
+ sdpa_test_params{128, 40, 40, 128, 128, 1, 8, true},
+ sdpa_test_params{128, 40, 40, 1, 512, 1, 8, true}),
+ sdpa_gpu_compressed_kv_bf16_test::PrintToStringParamName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_sdpa_gpu_compressed_kv_int4_bf16_per_token,
+ sdpa_gpu_compressed_kv_int4_bf16_per_token_test,
+ ::testing::Values(sdpa_test_params{128, 40, 10, 128, 128, 1, 4, true},
+ sdpa_test_params{128, 40, 10, 1, 512, 1, 4, true},
+ sdpa_test_params{128, 40, 40, 128, 128, 1, 4, true},
+ sdpa_test_params{128, 40, 40, 1, 512, 1, 4, true}),
+ sdpa_gpu_compressed_kv_int4_bf16_per_token_test::PrintToStringParamName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_sdpa_gpu_compressed_kv_bf16_int8_zp,
+ sdpa_gpu_compressed_kv_bf16_int8_zp_test,
+ ::testing::Values(sdpa_test_params{128, 40, 10, 256, 256, 1, 8, true},
+ sdpa_test_params{128, 40, 40, 256, 256, 1, 8, true},
+ sdpa_test_params{128, 40, 10, 128, 128, 1, 8, true},
+ sdpa_test_params{128, 40, 10, 1, 512, 1, 8, true},
+ sdpa_test_params{128, 40, 40, 128, 128, 1, 8, true},
+ sdpa_test_params{128, 40, 40, 1, 512, 1, 8, true}),
+ sdpa_gpu_compressed_kv_bf16_int8_zp_test::PrintToStringParamName);
+
INSTANTIATE_TEST_SUITE_P(smoke_sdpa_gpu_gqa_decomp,
sdpa_gpu_gqa_decomp_test,
::testing::Values(sdpa_test_params{128, 40, 10, 512, 512, 1, 4, false},