Skip to content

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

[GPU] Reorder support for uint2 (#37243)

vmadananth0f67a058c6b3
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/int2_utils.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/int2_utils.cl
index 297256523b0..58ec363ec81 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/int2_utils.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/include/batch_headers/int2_utils.cl
@@ -93,6 +93,12 @@ inline half8 unpack_to_half(uint2x8_t v) __attribute__((overloadable)) {
}
#endif
+// Extract a single U2 value from a packed byte by index (0..3)
+inline float convert_as_uint2_float(uchar source, uint index) {
+ uint shift = (index % 4) * 2;
+ return (float)((source >> shift) & 0x03);
+}
+
// Macros for generic unpacking
#define UNPACK_UINT2x4(target_type, v) CAT(unpack_to_, target_type)(v)
#define UNPACK_UINT2x8(target_type, v) CAT(unpack_to_, target_type)(v)
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.cl
index 36bd885b8ec..3cabda62b90 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reorder_data.cl
@@ -165,6 +165,9 @@ KERNEL (reorder_data)(
#elif defined UINT4_INPUT
const uint uint4_idx = input_idx >> 1;
OUTPUT_COMPUTE_TYPE res_tmp = TO_OUTPUT_REORDER_COMPUTE_TYPE(convert_as_uint4_float(input[uint4_idx], input_idx));
+ #elif defined UINT2_INPUT
+ const uint uint2_byte_idx = input_idx >> 2;
+ OUTPUT_COMPUTE_TYPE res_tmp = TO_OUTPUT_REORDER_COMPUTE_TYPE(convert_as_uint2_float(input[uint2_byte_idx], input_idx));
#elif (F8E5M2_INPUT || F8E4M3_INPUT || F8E8M0_INPUT)
OUTPUT_COMPUTE_TYPE res_tmp = TO_OUTPUT_REORDER_COMPUTE_TYPE(_convert_float(input[input_idx]));
#elif F4E2M1_INPUT
@@ -277,6 +280,21 @@ KERNEL (reorder_data)(
atomic_and(&output_u32[main_idx_out], ~(0x0F << shift_u32_out));
atomic_or(&output_u32[main_idx_out], (val_u32_out << shift_u32_out));
+ #elif defined(UINT2_OUTPUT)
+ OUTPUT_TYPE val_char = __TO_OUTPUT_REORDER_TYPE(res_tmp);
+ int val_i32 = convert_int(val_char);
+ #if !CONVERT_TRUNCATE
+ val_i32 = clamp(val_i32, 0, 3);
+ #endif
+ uint val_u32 = (uint)(val_i32 & 0x03);
+
+ volatile __global uint* output_u32 = (volatile __global uint*)output;
+ uint main_idx = output_idx / 16;
+ uint sub_idx = output_idx % 16;
+ uint shift = sub_idx * 2;
+
+ atomic_and(&output_u32[main_idx], ~(0x03u << shift));
+ atomic_or(&output_u32[main_idx], (val_u32 << shift));
#elif defined(INT4_OUTPUT) || defined(UINT4_OUTPUT)
OUTPUT_TYPE val_char = __TO_OUTPUT_REORDER_TYPE(res_tmp);
int val_i32 = convert_int(val_char);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_params.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_params.cpp
index fa2821e09ea..9830f63ecf5 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_params.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_params.cpp
@@ -47,6 +47,9 @@ DeviceFeaturesKey EngineInfo::get_supported_device_features_key() const {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ParamsKey::EnableInputDataType(Datatype dt) {
switch (dt) {
+ case Datatype::UINT2:
+ key.inputType.val.uint2 = 1;
+ break;
case Datatype::INT4:
key.inputType.val.int4 = 1;
break;
@@ -104,6 +107,9 @@ void ParamsKey::EnableAllInputDataType() { key.inputType.raw = 0xffffffff; }
void ParamsKey::EnableOutputDataType(Datatype dt) {
switch (dt) {
+ case Datatype::UINT2:
+ key.outputType.val.uint2 = 1;
+ break;
case Datatype::INT4:
key.outputType.val.int4 = 1;
break;
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cpp
index f9c38c1ed7e..3e21f54733d 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_bf_tiled.cpp
@@ -305,6 +305,7 @@ bool FullyConnected_bf_tiled::Validate(const Params& params) const {
}
const auto& fc_params = static_cast<const fully_connected_params&>(params);
+
const auto& input = fc_params.inputs[0];
const auto& output = fc_params.outputs[0];
const auto& weights = fc_params.weights;
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cpp
index 6a5a8aa9517..b5db28d106a 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/reorder/reorder_kernel.cpp
@@ -14,6 +14,7 @@ ParamsKey ReorderKernelRef::GetSupportedKey() const {
k.EnableInputDataType(Datatype::UINT32);
k.EnableInputDataType(Datatype::UINT4);
k.EnableInputDataType(Datatype::INT4);
+ k.EnableInputDataType(Datatype::UINT2);
k.EnableInputDataType(Datatype::INT8);
k.EnableInputDataType(Datatype::INT16);
k.EnableInputDataType(Datatype::INT32);
@@ -35,6 +36,7 @@ ParamsKey ReorderKernelRef::GetSupportedKey() const {
k.EnableOutputDataType(Datatype::UINT32);
k.EnableOutputDataType(Datatype::UINT4);
k.EnableOutputDataType(Datatype::INT4);
+ k.EnableOutputDataType(Datatype::UINT2);
k.EnableOutputDataType(Datatype::BF16);
k.EnableOutputDataType(Datatype::F4E2M1);
k.EnableOutputDataType(Datatype::F8E4M3);
@@ -81,6 +83,10 @@ JitConstants ReorderKernelRef::GetJitConstants(const reorder_params& params) con
jit.AddConstant(MakeJitConstant("UINT4_INPUT", true));
}
+ if ( params.inputs[0].GetDType() == Datatype::UINT2 ) {
+ jit.AddConstant(MakeJitConstant("UINT2_INPUT", true));
+ }
+
if ( params.outputs[0].GetDType() == Datatype::UINT4 ) {
jit.AddConstant(MakeJitConstant("UINT4_OUTPUT", true));
}
@@ -89,6 +95,10 @@ JitConstants ReorderKernelRef::GetJitConstants(const reorder_params& params) con
jit.AddConstant(MakeJitConstant("INT4_OUTPUT", true));
}
+ if ( params.outputs[0].GetDType() == Datatype::UINT2 ) {
+ jit.AddConstant(MakeJitConstant("UINT2_OUTPUT", true));
+ }
+
jit.AddConstant(MakeJitConstant("F4E2M1_INPUT", params.inputs[0].GetDType() == Datatype::F4E2M1 ? 1 : 0));
jit.AddConstant(MakeJitConstant("F8E5M2_INPUT", params.inputs[0].GetDType() == Datatype::F8E5M2 ? 1 : 0));
jit.AddConstant(MakeJitConstant("F8E4M3_INPUT", params.inputs[0].GetDType() == Datatype::F8E4M3 ? 1 : 0));
diff --git a/src/plugins/intel_gpu/src/plugin/common_utils.cpp b/src/plugins/intel_gpu/src/plugin/common_utils.cpp
index 506a2c758ae..02be538f006 100644
--- a/src/plugins/intel_gpu/src/plugin/common_utils.cpp
+++ b/src/plugins/intel_gpu/src/plugin/common_utils.cpp
@@ -198,7 +198,7 @@ bool is_supported(ov::element::Type_t et) {
case ov::element::Type_t::i32: return true;
case ov::element::Type_t::i64: return true; // converted to i32
case ov::element::Type_t::u1: return true;
- case ov::element::Type_t::u2: return false;
+ case ov::element::Type_t::u2: return true;
case ov::element::Type_t::u3: return false;
case ov::element::Type_t::u4: return true;
case ov::element::Type_t::u6: return true;
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cpp
index 07ee2e77f5b..763b0e52200 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/reorder_gpu_test.cpp
@@ -4998,6 +4998,135 @@ TEST(reorder_gpu_i4, basic_uint4_bf16)
run_reorder_uint4<ov::bfloat16>({32, 1, 1, 1});
}
+template <typename T>
+static void run_reorder_uint2(const ov::Shape in_shape) {
+ auto& engine = get_test_engine();
+
+ layout in_layout({in_shape, data_types::u2, format::bfyx});
+ auto input = engine.allocate_memory(in_layout);
+
+ // Pack 4 values (2 bits each, cycling 0..3) per byte so input/expected data scale with in_shape.
+ const size_t num_elements = ov::shape_size(in_shape);
+ std::vector<uint8_t> input_data((num_elements + 3) / 4, 0);
+ std::vector<T> expected_data(num_elements);
+ for (size_t idx = 0; idx < num_elements; idx++) {
+ const uint8_t val = static_cast<uint8_t>((idx * 3 + 1) % 4);
+ expected_data[idx] = static_cast<T>(val);
+ input_data[idx / 4] |= static_cast<uint8_t>(val << ((idx % 4) * 2));
+ }
+
+ set_values(input, input_data);
+
+ topology topology(
+ input_layout("input", input->get_layout()),
+ reorder("reorder", input_info("input"), format::bfyx, element_type_to_data_type(ov::element::from<T>())));
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::optimize_data(true));
+
+ network network(engine, topology, config);
+ network.set_input_data("input", input);
+
+ auto outputs = network.execute();
+ ASSERT_EQ(outputs.size(), size_t(1));
+ ASSERT_EQ(outputs.begin()->first, "reorder");
+
+ auto output = outputs.begin()->second.get_memory();
+
+ cldnn::mem_lock<T> output_ptr(output, get_test_stream());
+
+ ASSERT_EQ(expected_data.size(), output_ptr.size());
+ for (size_t idx = 0; idx < output_ptr.size(); idx++)
+ ASSERT_EQ(expected_data[idx], output_ptr[idx]);
+}
+
+TEST(reorder_gpu_u2, basic_uint2)
+{
+ run_reorder_uint2<ov::float16>({32, 1, 1, 1});
+}
+
+TEST(reorder_gpu_u2, basic_uint2_bf16)
+{
+ run_reorder_uint2<ov::bfloat16>({32, 1, 1, 1});
+}
+
+TEST(reorder_gpu_u2, basic_uint2_non_primitive_shape)
+{
+ run_reorder_uint2<ov::float16>({1, 1, 2, 3});
+}
+
+TEST(reorder_gpu_u2, basic_uint2_non_primitive_shape_bf16)
+{
+ run_reorder_uint2<ov::bfloat16>({1, 1, 2, 3});
+}
+
+// Packing: val[0] in bits[1:0], val[1] in bits[3:2], val[2] in bits[5:4], val[3] in bits[7:6]
+template <typename T>
+void run_reorder_test_to_u2(data_types input_type, const ov::Shape& in_shape) {
+ auto& engine = get_test_engine();
+
+ // Values (cycling 0..3, same formula as run_reorder_uint2) and their expected packed bytes scale with in_shape.
+ const size_t num_elements = ov::shape_size(in_shape);
+ std::vector<T> input_data(num_elements);
+ std::vector<uint8_t> expected((num_elements + 3) / 4, 0);
+ for (size_t idx = 0; idx < num_elements; idx++) {
+ const uint8_t val = static_cast<uint8_t>((idx * 3 + 1) % 4);
+ input_data[idx] = static_cast<T>(val);
+ expected[idx / 4] |= static_cast<uint8_t>(val << ((idx % 4) * 2));
+ }
+
+ layout in_layout({in_shape, input_type, format::bfyx});
+ layout out_layout({in_shape, data_types::u2, format::bfyx});
+
+ memory::ptr input_mem = engine.allocate_memory(in_layout);
+ set_values(input_mem, input_data);
+
+ topology topology(input_layout("input", in_layout), reorder("reorder", input_info("input"), out_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::optimize_data(true));
+ ov::intel_gpu::ImplementationDesc reorder_impl = {format::bfyx, "reorder_data"};
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"reorder", reorder_impl}}));
+
+ network network(engine, topology, config);
+ network.set_input_data("input", input_mem);
+
+ auto outputs = network.execute();
+ auto output_mem = outputs.at("reorder").get_memory();
+ cldnn::mem_lock<uint8_t, mem_lock_type::read> output_ptr(output_mem, get_test_stream());
+
+ ASSERT_EQ(expected.size(), output_ptr.size());
+ for (size_t i = 0; i < expected.size(); ++i) {
+ ASSERT_EQ(expected[i], output_ptr[i]) << "mismatch at byte " << i;
+ }
+}
+
+TEST(reorder_gpu_u2, fp16_to_u2) {
+ run_reorder_test_to_u2<ov::float16>(data_types::f16, {1, 1, 4, 4});
+}
+
+TEST(reorder_gpu_u2, bf16_to_u2) {
+ run_reorder_test_to_u2<ov::bfloat16>(data_types::bf16, {1, 1, 4, 4});
+}
+
+TEST(reorder_gpu_u2, fp16_to_u2_non_primitive_shape) {
+ run_reorder_test_to_u2<ov::float16>(data_types::f16, {1, 1, 2, 3});
+}
+
+TEST(reorder_gpu_u2, bf16_to_u2_non_primitive_shape) {
+ run_reorder_test_to_u2<ov::bfloat16>(data_types::bf16, {1, 1, 2, 3});
+}
+
+TEST(reorder_gpu_u2, fp16_to_u2_prime_shape) {
+ run_reorder_test_to_u2<ov::float16>(data_types::f16, {1, 1, 1, 7});
+}
+
+TEST(reorder_gpu_u2, bf16_to_u2_prime_shape) {
+ run_reorder_test_to_u2<ov::bfloat16>(data_types::bf16, {1, 1, 1, 7});
+}
+
static uint8_t pack_int4(int8_t a, int8_t b) {
uint8_t packed_a = (a & 0xF) << 4;
uint8_t packed_b = (b & 0xF);