Skip to content

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

[GPU] Add BF16 support for quantize operations (#37230)

Prithviraj-R08aff300f5c5
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/quantize.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/quantize.cpp
index 3a645c6bc4b..2965ea297c4 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/quantize.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/quantize.cpp
@@ -106,6 +106,7 @@ namespace detail {
attach_quantize_impl::attach_quantize_impl() {
auto types = {
data_types::f16,
+ data_types::bf16,
data_types::f32,
data_types::i8,
data_types::u8
@@ -147,6 +148,7 @@ attach_quantize_impl::attach_quantize_impl() {
auto keys = implementation_map<quantize>::combine(types, formats);
keys.emplace(data_types::f16, format::yxfb);
+ keys.emplace(data_types::bf16, format::yxfb);
keys.emplace(data_types::f32, format::yxfb);
implementation_map<quantize>::add(impl_types::ocl,
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_ref.cl
index 95944e7cefd..8f1f0672214 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_ref.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_ref.cl
@@ -4,6 +4,8 @@
#include "include/batch_headers/fetch_data.cl"
+#define TO_OUTPUT_TYPE_SAT_RTE(v) CAT(CAT(convert_, OUTPUT_TYPE), _sat_rte)(v)
+
#ifdef SUB_GROUP_SIZE
REQD_SUB_GROUP_SIZE(SUB_GROUP_SIZE)
#endif
@@ -144,13 +146,12 @@ KERNEL(quantize_ref)(
const int output_high_offset = INPUT4_GET_INDEX_SAFE(b, of, y, x);
#endif
- INPUT0_TYPE val = input[input_offset];
-
- INPUT0_TYPE input_low_val = input_low[input_low_offset];
- INPUT0_TYPE input_high_val = input_high[input_high_offset];
- INPUT0_TYPE output_low_val = output_low[output_low_offset];
- INPUT0_TYPE output_high_val = output_high[output_high_offset];
+ INPUT0_COMPUTE_TYPE val = DECODE_INPUT0_COMPUTE_TYPE(input[input_offset]);
+ INPUT0_COMPUTE_TYPE input_low_val = TO_INPUT0_COMPUTE_TYPE(input_low[input_low_offset]);
+ INPUT0_COMPUTE_TYPE input_high_val = TO_INPUT0_COMPUTE_TYPE(input_high[input_high_offset]);
+ INPUT0_COMPUTE_TYPE output_low_val = TO_INPUT0_COMPUTE_TYPE(output_low[output_low_offset]);
+ INPUT0_COMPUTE_TYPE output_high_val = TO_INPUT0_COMPUTE_TYPE(output_high[output_high_offset]);
if (val <= input_low_val)
{
@@ -163,12 +164,13 @@ KERNEL(quantize_ref)(
else
{
#if OUTPUT_IS_FP
- output[output_offset] = TO_OUTPUT_TYPE(round((val - input_low_val) / (input_high_val - input_low_val) * (LEVELS-1))
+ output[output_offset] = TO_OUTPUT_TYPE_SAT(round((val - input_low_val) / (input_high_val - input_low_val) * (LEVELS-1))
* (UNIT_VAL_ONE / (LEVELS-1) * (output_high_val - output_low_val)) + output_low_val);
#else
- // TODO: the outer round should be deleted once output range is correct
- output[output_offset] = TO_OUTPUT_TYPE(round(round((val - input_low_val) / (input_high_val - input_low_val) * (LEVELS-1))
- * (UNIT_VAL_ONE / (LEVELS-1) * (output_high_val - output_low_val)) + output_low_val));
+ output[output_offset] = TO_OUTPUT_TYPE_SAT_RTE(round((val - input_low_val) / (input_high_val - input_low_val) * (LEVELS-1))
+ * (UNIT_VAL_ONE / (LEVELS-1) * (output_high_val - output_low_val)) + output_low_val);
#endif
}
}
+
+#undef TO_OUTPUT_TYPE_SAT_RTE
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_opt.cl
index 2e5b836b2be..ec677a31e9e 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_opt.cl
@@ -131,40 +131,40 @@ KERNEL(quantize_gpu_scale_shift_opt)(OPTIONAL_SHAPE_INFO_ARG
#endif
#if PER_TENSOR_INPUT_SCALE
- INPUT1_TYPE input_scale_val = IN_SCALE_VAL;
+ INPUT1_COMPUTE_TYPE input_scale_val = IN_SCALE_VAL;
#else
- INPUT1_TYPE input_scale_val = input_scale[scales_offset];
+ INPUT1_COMPUTE_TYPE input_scale_val = input_scale[scales_offset];
#endif
#if PER_TENSOR_INPUT_SHIFT
- INPUT1_TYPE input_shift_val = IN_SHIFT_VAL;
+ INPUT1_COMPUTE_TYPE input_shift_val = IN_SHIFT_VAL;
#else
- INPUT1_TYPE input_shift_val = input_shift[scales_offset];
+ INPUT1_COMPUTE_TYPE input_shift_val = input_shift[scales_offset];
#endif
#if PER_TENSOR_OUTPUT_SCALE
- INPUT1_TYPE output_scale_val = OUT_SCALE_VAL;
+ INPUT1_COMPUTE_TYPE output_scale_val = OUT_SCALE_VAL;
#else
- INPUT1_TYPE output_scale_val = output_scale[scales_offset];
+ INPUT1_COMPUTE_TYPE output_scale_val = output_scale[scales_offset];
#endif
#if PER_TENSOR_OUTPUT_SHIFT
- INPUT1_TYPE output_shift_val = OUT_SHIFT_VAL;
+ INPUT1_COMPUTE_TYPE output_shift_val = OUT_SHIFT_VAL;
#else
- INPUT1_TYPE output_shift_val = output_shift[scales_offset];
+ INPUT1_COMPUTE_TYPE output_shift_val = output_shift[scales_offset];
#endif
#if HAS_CLAMP
#if CAN_USE_OUTPUT_RANGE
- INPUT1_TYPE output_low_val = OUT_LO_VAL;
- INPUT1_TYPE output_high_val = OUT_HI_VAL;
+ INPUT1_COMPUTE_TYPE output_low_val = OUT_LO_VAL;
+ INPUT1_COMPUTE_TYPE output_high_val = OUT_HI_VAL;
#else
#if PER_TENSOR_INPUT_RANGE
- INPUT1_TYPE input_low_val = IN_LO_VAL;
- INPUT1_TYPE input_high_val = IN_HI_VAL;
+ INPUT1_COMPUTE_TYPE input_low_val = IN_LO_VAL;
+ INPUT1_COMPUTE_TYPE input_high_val = IN_HI_VAL;
#else
- INPUT1_TYPE input_low_val = input_low[in_range_offset];
- INPUT1_TYPE input_high_val = input_high[in_range_offset];
+ INPUT1_COMPUTE_TYPE input_low_val = input_low[in_range_offset];
+ INPUT1_COMPUTE_TYPE input_high_val = input_high[in_range_offset];
#endif // PER_TENSOR_INPUT_RANGE
#endif // CAN_USE_OUTPUT_RANGE
#endif // HAS_CLAMP
@@ -176,9 +176,9 @@ KERNEL(quantize_gpu_scale_shift_opt)(OPTIONAL_SHAPE_INFO_ARG
#if CAN_USE_OUTPUT_RANGE
#if HAS_PRE_SHIFT
- INPUT1_TYPE val = TO_INPUT1_TYPE(input[input_offset]) * input_scale_val + input_shift_val;
+ INPUT1_COMPUTE_TYPE val = TO_INPUT1_COMPUTE_TYPE(DECODE_INPUT0_COMPUTE_TYPE(input[input_offset])) * input_scale_val + input_shift_val;
#else
- INPUT1_TYPE val = TO_INPUT1_TYPE(input[input_offset]) * input_scale_val;
+ INPUT1_COMPUTE_TYPE val = TO_INPUT1_COMPUTE_TYPE(DECODE_INPUT0_COMPUTE_TYPE(input[input_offset])) * input_scale_val;
#endif
#if HAS_OUTPUT_RANGE_ROUND
@@ -210,9 +210,9 @@ KERNEL(quantize_gpu_scale_shift_opt)(OPTIONAL_SHAPE_INFO_ARG
#else // CAN_USE_OUTPUT_RANGE
#if HAS_CLAMP
- INPUT1_TYPE val = clamp(TO_INPUT1_TYPE(input[input_offset]), input_low_val, input_high_val);
+ INPUT1_COMPUTE_TYPE val = clamp(TO_INPUT1_COMPUTE_TYPE(DECODE_INPUT0_COMPUTE_TYPE(input[input_offset])), input_low_val, input_high_val);
#else
- INPUT1_TYPE val = TO_INPUT1_TYPE(input[input_offset]);
+ INPUT1_COMPUTE_TYPE val = TO_INPUT1_COMPUTE_TYPE(DECODE_INPUT0_COMPUTE_TYPE(input[input_offset]));
#endif
#if HAS_PRE_SHIFT
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_vload8_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_vload8_opt.cl
index f1b55417915..0ab054c4267 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_vload8_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/quantize_gpu_scale_shift_vload8_opt.cl
@@ -4,9 +4,8 @@
#include "include/batch_headers/fetch_data.cl"
#define INPUT0_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, 8)
-#define INPUT1_VEC_TYPE MAKE_VECTOR_TYPE(INPUT1_TYPE, 8)
+#define INPUT1_COMPUTE_VEC_TYPE MAKE_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, 8)
#define OUTPUT_VEC_TYPE MAKE_VECTOR_TYPE(OUTPUT_TYPE, 8)
-#define TO_INPUT1_TYPE CAT(convert_, INPUT1_TYPE)
#define TO_VECTOR_TYPE_IMPL_8(elem_type) CAT(convert_##elem_type, 8)
#define TO_VECTOR_TYPE(elem_type, size) CAT(TO_VECTOR_TYPE_IMPL_, size)(elem_type)
#define TO_VECTOR_TYPE_IMPL_SAT_8(elem_type) CAT(convert_##elem_type, 8##_sat)
@@ -90,11 +89,11 @@ KERNEL(quantize_gpu_scale_shift_vload8_opt)(OPTIONAL_SHAPE_INFO_ARG
OUTPUT_VEC_TYPE res;
#if HAS_CLAMP
#if CAN_USE_OUTPUT_RANGE
- INPUT1_TYPE output_low_val = OUT_LO_VAL;
- INPUT1_TYPE output_high_val = OUT_HI_VAL;
+ INPUT1_COMPUTE_TYPE output_low_val = OUT_LO_VAL;
+ INPUT1_COMPUTE_TYPE output_high_val = OUT_HI_VAL;
#else
- INPUT1_TYPE input_low_val = IN_LO_VAL;
- INPUT1_TYPE input_high_val = IN_HI_VAL;
+ INPUT1_COMPUTE_TYPE input_low_val = IN_LO_VAL;
+ INPUT1_COMPUTE_TYPE input_high_val = IN_HI_VAL;
#endif // CAN_USE_OUTPUT_RANGE
#endif // HAS_CLAMP
@@ -105,9 +104,9 @@ KERNEL(quantize_gpu_scale_shift_vload8_opt)(OPTIONAL_SHAPE_INFO_ARG
#if CAN_USE_OUTPUT_RANGE
#if HAS_PRE_SHIFT
- INPUT1_VEC_TYPE val = TO_VECTOR_TYPE(INPUT1_TYPE, 8)(in0) * TO_INPUT1_TYPE(IN_SCALE_VAL) + TO_INPUT1_TYPE(IN_SHIFT_VAL);
+ INPUT1_COMPUTE_VEC_TYPE val = TO_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, 8)(DECODE_INPUT0_COMPUTE_VECTOR_TYPE(in0, 8)) * TO_INPUT1_COMPUTE_TYPE(IN_SCALE_VAL) + TO_INPUT1_COMPUTE_TYPE(IN_SHIFT_VAL);
#else
- INPUT1_VEC_TYPE val = TO_VECTOR_TYPE(INPUT1_TYPE, 8)(in0) * TO_INPUT1_TYPE(IN_SCALE_VAL);
+ INPUT1_COMPUTE_VEC_TYPE val = TO_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, 8)(DECODE_INPUT0_COMPUTE_VECTOR_TYPE(in0, 8)) * TO_INPUT1_COMPUTE_TYPE(IN_SCALE_VAL);
#endif
#if HAS_OUTPUT_RANGE_ROUND
@@ -115,11 +114,11 @@ KERNEL(quantize_gpu_scale_shift_vload8_opt)(OPTIONAL_SHAPE_INFO_ARG
#endif
#if HAS_POST_SCALE
- val *= TO_INPUT1_TYPE(OUT_SCALE_VAL);
+ val *= TO_INPUT1_COMPUTE_TYPE(OUT_SCALE_VAL);
#endif
#if HAS_POST_SHIFT
- val += TO_INPUT1_TYPE(OUT_SHIFT_VAL);
+ val += TO_INPUT1_COMPUTE_TYPE(OUT_SHIFT_VAL);
#endif
#if HAS_CLAMP
@@ -139,29 +138,29 @@ KERNEL(quantize_gpu_scale_shift_vload8_opt)(OPTIONAL_SHAPE_INFO_ARG
#else // CAN_USE_OUTPUT_RANGE
#if HAS_CLAMP
- INPUT1_VEC_TYPE val = clamp(TO_VECTOR_TYPE(INPUT1_TYPE, 8)(in0), input_low_val, input_high_val);
+ INPUT1_COMPUTE_VEC_TYPE val = clamp(TO_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, 8)(DECODE_INPUT0_COMPUTE_VECTOR_TYPE(in0, 8)), input_low_val, input_high_val);
#else
- INPUT1_VEC_TYPE val = TO_VECTOR_TYPE(INPUT1_TYPE, 8)(in0);
+ INPUT1_COMPUTE_VEC_TYPE val = TO_VECTOR_TYPE(INPUT1_COMPUTE_TYPE, 8)(DECODE_INPUT0_COMPUTE_VECTOR_TYPE(in0, 8));
#endif
#if HAS_PRE_SHIFT
- val = round(val * TO_INPUT1_TYPE(IN_SCALE_VAL) + TO_INPUT1_TYPE(IN_SHIFT_VAL));
+ val = round(val * TO_INPUT1_COMPUTE_TYPE(IN_SCALE_VAL) + TO_INPUT1_COMPUTE_TYPE(IN_SHIFT_VAL));
#else
- val = round(val * TO_INPUT1_TYPE(IN_SCALE_VAL));
+ val = round(val * TO_INPUT1_COMPUTE_TYPE(IN_SCALE_VAL));
#endif
#if HAS_POST_SCALE
- val *= TO_INPUT1_TYPE(OUT_SCALE_VAL);
+ val *= TO_INPUT1_COMPUTE_TYPE(OUT_SCALE_VAL);
#endif
#if HAS_POST_SHIFT
- val += TO_INPUT1_TYPE(OUT_SHIFT_VAL);
+ val += TO_INPUT1_COMPUTE_TYPE(OUT_SHIFT_VAL);
#endif
#endif // CAN_USE_OUTPUT_RANGE
#if OUTPUT_IS_FP
- res = TO_VECTOR_TYPE_SAT(OUTPUT_TYPE, 8)(val);
+ res = TO_OUTPUT_VECTOR_TYPE_SAT(val, 8);
#else
res = TO_VECTOR_TYPE_SAT_RTE(OUTPUT_TYPE, 8)(val);;
#endif
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_ref.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_ref.cpp
index 7a516c8fbb5..21cca54ca32 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_ref.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_ref.cpp
@@ -12,8 +12,10 @@ namespace kernel_selector {
ParamsKey QuantizeKernelRef::GetSupportedKey() const {
ParamsKey k;
k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::BF16);
k.EnableInputDataType(Datatype::F32);
k.EnableOutputDataType(Datatype::F16);
+ k.EnableOutputDataType(Datatype::BF16);
k.EnableOutputDataType(Datatype::F32);
k.EnableOutputDataType(Datatype::UINT8);
k.EnableOutputDataType(Datatype::INT8);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_opt.cpp
index 752d3457fac..7307a54bdd9 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_opt.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_opt.cpp
@@ -16,8 +16,10 @@ ParamsKey QuantizeKernelScaleShift::GetSupportedKey() const {
k.EnableInputDataType(Datatype::UINT8);
k.EnableInputDataType(Datatype::INT8);
k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::BF16);
k.EnableInputDataType(Datatype::F32);
k.EnableOutputDataType(Datatype::F16);
+ k.EnableOutputDataType(Datatype::BF16);
k.EnableOutputDataType(Datatype::F32);
k.EnableOutputDataType(Datatype::UINT8);
k.EnableOutputDataType(Datatype::INT8);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_vload8_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_vload8_opt.cpp
index 37742b8aba0..093e6c36e17 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_vload8_opt.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/quantize/quantize_kernel_scale_shift_vload8_opt.cpp
@@ -21,8 +21,10 @@ ParamsKey QuantizeKernelScaleShift_vload8::GetSupportedKey() const {
k.EnableInputDataType(Datatype::UINT8);
k.EnableInputDataType(Datatype::INT8);
k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::BF16);
k.EnableInputDataType(Datatype::F32);
k.EnableOutputDataType(Datatype::F16);
+ k.EnableOutputDataType(Datatype::BF16);
k.EnableOutputDataType(Datatype::F32);
k.EnableOutputDataType(Datatype::UINT8);
k.EnableOutputDataType(Datatype::INT8);
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp
index b2ca1d78e28..094489f4b77 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/quantize_gpu_test.cpp
@@ -1019,6 +1019,9 @@ struct quantize_random_test : testing::TestWithParam<quantize_random_test_params
case data_types::f16:
fill_random_typed<ov::float16>(mem, -127, 127, 2);
break;
+ case data_types::bf16:
+ fill_random_typed<ov::bfloat16>(mem, -127, 127, 2);
+ break;
case data_types::i8:
fill_random_typed<int8_t>(mem, -127, 127, 1);
break;
@@ -1117,6 +1120,8 @@ struct quantize_random_test : testing::TestWithParam<quantize_random_test_params
fill_typed<float>(input, input_opt);
} else if (params.input_type == data_types::f16) {
fill_typed<ov::float16>(input, input_opt);
+ } else if (params.input_type == data_types::bf16) {
+ fill_typed<ov::bfloat16>(input, input_opt);
} else if (params.input_type == data_types::i8) {
fill_typed<int8_t>(input, input_opt);
} else if (params.input_type == data_types::u8) {
@@ -1153,6 +1158,8 @@ struct quantize_random_test : testing::TestWithParam<quantize_random_test_params
compare_outputs<float>(output, output_opt);
} else if (params.output_type == data_types::f16) {
compare_outputs<ov::float16>(output, output_opt);
+ } else if (params.output_type == data_types::bf16) {
+ compare_outputs<ov::bfloat16>(output, output_opt);
} else if (params.output_type == data_types::i8) {
compare_outputs<int8_t>(output, output_opt);
} else if (params.output_type == data_types::u8) {
@@ -1190,6 +1197,9 @@ INSTANTIATE_TEST_SUITE_P(quantize_smoke,
.simple_params(data_types::f32, data_types::u8, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, 5)
.simple_params(data_types::f32, data_types::u8, format::bfyx, format::bfyx, 5)
.simple_params(data_types::f16, data_types::u8, format::bs_fs_yx_bsv16_fsv32, format::bs_fs_yx_bsv16_fsv32, 5)
+ .simple_params(data_types::bf16, data_types::u8, format::bs_fs_yx_bsv16_fsv32, format::bs_fs_yx_bsv16_fsv32, 5)
+ .simple_params(data_types::f32, data_types::bf16, format::bfyx, format::bfyx, 5)
+ .simple_params(data_types::f16, data_types::bf16, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, 5)
));
#ifdef RUN_ALL_MODEL_CACHING_TESTS