Skip to content

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

[GPU] Support u2 compressed dictionaries in GatherCompressed (#39057)

Cyberpunk1210bfbb23500507
diff --git a/src/common/transformations/include/transformations/op_conversions/convert_gather_to_compressed.hpp b/src/common/transformations/include/transformations/op_conversions/convert_gather_to_compressed.hpp
index bd7184d5177..81cae3feb6d 100644
--- a/src/common/transformations/include/transformations/op_conversions/convert_gather_to_compressed.hpp
+++ b/src/common/transformations/include/transformations/op_conversions/convert_gather_to_compressed.hpp
@@ -4,6 +4,9 @@
#pragma once
+#include <vector>
+
+#include "openvino/core/type/element_type.hpp"
#include "openvino/pass/graph_rewrite.hpp"
#include "openvino/pass/matcher_pass.hpp"
#include "transformations_visibility.hpp"
@@ -38,7 +41,8 @@ class TRANSFORMATIONS_API MoveDecompressionAfterGather;
class ov::pass::ConvertGatherToGatherCompressed : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("ConvertGatherToGatherCompressed");
- ConvertGatherToGatherCompressed();
+ explicit ConvertGatherToGatherCompressed(const std::vector<ov::element::Type>& supported_weights_types =
+ {ov::element::u8, ov::element::i8, ov::element::u4, ov::element::i4});
};
/*
diff --git a/src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp b/src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp
index cc8e4a3a2b2..680a35418f2 100644
--- a/src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp
+++ b/src/common/transformations/src/transformations/op_conversions/convert_gather_to_compressed.cpp
@@ -29,12 +29,10 @@ using ov::pass::pattern::op::Or;
namespace v0 = ov::op::v0;
namespace v1 = ov::op::v1;
namespace v8 = ov::op::v8;
-ov::pass::ConvertGatherToGatherCompressed::ConvertGatherToGatherCompressed() {
- auto compressed_constant = [](const ov::Output<ov::Node>& output) {
- return (output.get_element_type() == ov::element::u8 || output.get_element_type() == ov::element::i8 ||
- output.get_element_type() == ov::element::u4 || output.get_element_type() == ov::element::i4) &&
- (output.get_shape().size() == 2 || output.get_shape().size() == 3);
- };
+ov::pass::ConvertGatherToGatherCompressed::ConvertGatherToGatherCompressed(
+ const std::vector<ov::element::Type>& supported_weights_types) {
+ auto compressed_constant = ov::pass::pattern::type_matches_any(supported_weights_types) &&
+ (ov::pass::pattern::rank_equals(2) || ov::pass::pattern::rank_equals(3));
auto reshape_3d_to_2d = [](const ov::Output<ov::Node>& output) {
auto in_ps = output.get_node()->get_input_partial_shape(0);
diff --git a/src/common/transformations/tests/op_conversions/convert_gather_to_compressed_test.cpp b/src/common/transformations/tests/op_conversions/convert_gather_to_compressed_test.cpp
index 65b3a9ed6db..298a3b7bcd2 100644
--- a/src/common/transformations/tests/op_conversions/convert_gather_to_compressed_test.cpp
+++ b/src/common/transformations/tests/op_conversions/convert_gather_to_compressed_test.cpp
@@ -153,6 +153,56 @@ TEST_F(TransformationTestsF, ConvertGatherToCompressed4) {
}
}
+TEST_F(TransformationTestsF, ConvertGatherToCompressedU2NotMatchedByDefault) {
+ // u2 is not in the default type list, so the graph must stay as is.
+ auto input1 = std::make_shared<v0::Parameter>(ov::element::i32, ov::PartialShape{-1, 16});
+ auto axis_const = v0::Constant::create(ov::element::i32, ov::Shape{1}, {1});
+ auto weights_const = v0::Constant::create(ov::element::u2, ov::Shape{32, 16}, {1});
+ auto convert = std::make_shared<v0::Convert>(weights_const, ov::element::f32);
+ auto scale_const = v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1});
+ auto scale = std::make_shared<v1::Multiply>(convert, scale_const);
+ auto gather = std::make_shared<v8::Gather>(scale, input1, axis_const);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{gather}, ov::ParameterVector{input1});
+ manager.register_pass<ConvertGatherToGatherCompressed>();
+}
+
+TEST_F(TransformationTestsF, ConvertGatherToCompressedU2) {
+ {
+ auto input1 = std::make_shared<v0::Parameter>(ov::element::i32, ov::PartialShape{-1, 16});
+ auto axis_const = v0::Constant::create(ov::element::i32, ov::Shape{1}, {1});
+ auto weights_const = v0::Constant::create(ov::element::u2, ov::Shape{32, 16}, {1});
+ auto convert = std::make_shared<v0::Convert>(weights_const, ov::element::f32);
+ auto zp_const = v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1});
+ auto sub = std::make_shared<v1::Subtract>(convert, zp_const);
+ auto scale_const = v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1});
+ auto scale = std::make_shared<v1::Multiply>(sub, scale_const);
+ auto gather = std::make_shared<v8::Gather>(scale, input1, axis_const);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{gather}, ov::ParameterVector{input1});
+ manager.register_pass<ConvertGatherToGatherCompressed>(std::vector<ov::element::Type>{ov::element::u8,
+ ov::element::i8,
+ ov::element::u4,
+ ov::element::i4,
+ ov::element::u2});
+ }
+ {
+ auto input1 = std::make_shared<v0::Parameter>(ov::element::i32, ov::PartialShape{-1, 16});
+ auto axis_const = v0::Constant::create(ov::element::i32, ov::Shape{1}, {1});
+ auto weights_const = v0::Constant::create(ov::element::u2, ov::Shape{32, 16}, {1});
+ auto scale_const = v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1});
+ auto zp_const = v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1});
+ auto gather_compressed = std::make_shared<ov::op::internal::GatherCompressed>(weights_const,
+ input1,
+ axis_const,
+ 0,
+ scale_const,
+ zp_const);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{gather_compressed}, ov::ParameterVector{input1});
+ }
+}
+
TEST_F(TransformationTestsF, ConvertGatherToCompressedFP16) {
{
auto input1 = std::make_shared<v0::Parameter>(ov::element::i32, ov::PartialShape{-1, 16});
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp
index 09828dd81ad..393c417465c 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/gather.cpp
@@ -215,6 +215,7 @@ attach_gather_impl::attach_gather_impl() {
data_types::u8,
data_types::i4,
data_types::u4,
+ data_types::u2,
data_types::i32,
data_types::f8e4m3};
@@ -261,6 +262,7 @@ attach_gather_impl::attach_gather_impl() {
std::make_tuple(data_types::u8, format::bfyx),
std::make_tuple(data_types::i4, format::bfyx),
std::make_tuple(data_types::u4, format::bfyx),
+ std::make_tuple(data_types::u2, format::bfyx),
std::make_tuple(data_types::f32, format::bfzyx),
std::make_tuple(data_types::f16, format::bfzyx),
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_ref.cl
index bd68d0fdcaa..162f128f876 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_ref.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_ref.cl
@@ -4,6 +4,7 @@
#include "include/batch_headers/fetch_data.cl"
#include "include/batch_headers/int4_utils.cl"
+#include "include/batch_headers/int2_utils.cl"
#if INPUT0_IS_F8E4M3
#include "include/f8_utils.cl" // fp8e4m3_t typedef
#endif
@@ -98,6 +99,13 @@ KERNEL(gather_ref)(
OUTPUT_COMPUTE_TYPE val_compressed = ((OUTPUT_COMPUTE_TYPE*)(&val_unpacked))[dictionary_idx % 2];
val_compute = (val_compressed - zp) * scale;
+ #elif COMPRESSED_WEIGHTS_UINT2
+ INPUT0_TYPE val_packed = dictionary[dictionary_idx / 4];
+ MAKE_VECTOR_TYPE(OUTPUT_COMPUTE_TYPE, 4) val_unpacked =
+ UNPACK_UINT2x4(OUTPUT_COMPUTE_TYPE, *((UINT2_PACKED_TYPE*)&val_packed));
+
+ OUTPUT_COMPUTE_TYPE val_compressed = ((OUTPUT_COMPUTE_TYPE*)(&val_unpacked))[dictionary_idx % 4];
+ val_compute = (val_compressed - zp) * scale;
#endif
}
// Variable val for fused ops
diff --git a/src/plugins/intel_gpu/src/kernel_selector/jitter.cpp b/src/plugins/intel_gpu/src/kernel_selector/jitter.cpp
index d811dccc85a..1f21354e5ac 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/jitter.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/jitter.cpp
@@ -138,6 +138,7 @@ std::string toCLType(WeightsType wType) {
case WeightsType::INT4:
case WeightsType::INT8:
return GetTypeName<int8_t>();
+ case WeightsType::UINT2:
case WeightsType::UINT4:
case WeightsType::UINT8:
return GetTypeName<uint8_t>();
@@ -159,6 +160,7 @@ std::string toCLType(Datatype dType) {
case Datatype::INT4:
case Datatype::INT8:
return GetTypeName<int8_t>();
+ case Datatype::UINT2:
case Datatype::UINT4:
case Datatype::UINT8:
return GetTypeName<uint8_t>();
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp
index 2782ccf9993..63e05112ebb 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/gather/gather_kernel_ref.cpp
@@ -46,6 +46,7 @@ ParamsKey GatherKernelRef::GetSupportedKey() const {
k.EnableInputDataType(Datatype::UINT4);
k.EnableInputDataType(Datatype::INT4);
k.EnableInputDataType(Datatype::F8E4M3);
+ k.EnableInputDataType(Datatype::UINT2);
k.EnableOutputDataType(Datatype::F16);
k.EnableOutputDataType(Datatype::BF16);
@@ -292,6 +293,8 @@ JitConstants GatherKernelRef::GetJitConstants(const gather_params& params) const
jit.AddConstants({MakeJitConstant("COMPRESSED_WEIGHTS_INT8", 1)});
} else if (params.inputs[0].GetDType() == Datatype::INT4 || params.inputs[0].GetDType() == Datatype::UINT4) {
jit.AddConstants({MakeJitConstant("COMPRESSED_WEIGHTS_INT4", 1)});
+ } else if (params.inputs[0].GetDType() == Datatype::UINT2) {
+ jit.AddConstants({MakeJitConstant("COMPRESSED_WEIGHTS_UINT2", 1)});
}
auto wt = params.inputs[0].GetDType();
@@ -299,6 +302,8 @@ JitConstants GatherKernelRef::GetJitConstants(const gather_params& params) const
jit.Merge(make_sub_byte_packed_type_jit_constant("INT4_PACKED_TYPE", WeightsType::UINT4, 2));
} else if (wt == Datatype::INT4) {
jit.Merge(make_sub_byte_packed_type_jit_constant("INT4_PACKED_TYPE", WeightsType::INT4, 2));
+ } else if (wt == Datatype::UINT2) {
+ jit.Merge(make_sub_byte_packed_type_jit_constant("UINT2_PACKED_TYPE", WeightsType::UINT2, 4));
}
const size_t scale_groups_num = params.decompression_scale.LogicalSize();
@@ -338,6 +343,11 @@ bool GatherKernelRef::Validate(const Params& p) const {
}
}
+ // u2 dictionaries are only unpacked on the compressed path.
+ if (params.inputs[0].GetDType() == Datatype::UINT2 && !params.compressed) {
+ DO_NOT_USE_THIS_KERNEL(p.layerID);
+ }
+
if (params.outputs[0].is_dynamic()) {
auto supported_tensor_layout = [](const DataTensor& t) -> bool {
return t.GetLayout() == DataLayout::bfyx || t.GetLayout() == DataLayout::byxf || t.GetLayout() == DataLayout::bfzyx ||
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index d013a283545..a82d1f1e092 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -1978,7 +1978,8 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
manager.register_pass<ov::pass::ConstantFolding>();
manager.register_pass<ov::pass::SDPAScaleFusion>();
- manager.register_pass<ov::pass::ConvertGatherToGatherCompressed>();
+ manager.register_pass<ov::pass::ConvertGatherToGatherCompressed>(
+ std::vector<ov::element::Type>{ov::element::u8, ov::element::i8, ov::element::u4, ov::element::i4, ov::element::u2});
auto pass_config = manager.get_pass_config();
pass_config->set_callback<ov::intel_gpu::KVCacheFusionMatcher>([](const_node_ptr& node) -> bool {
const auto& rank = node->input(0).get_partial_shape().rank().get_length();
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp
index 6a494287d2e..2e452460465 100644
--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/subgraph_tests/gather_weights_decompression.cpp
@@ -18,7 +18,8 @@ const std::vector<ov::element::Type> output_precisions = {ov::element::f32, ov::
const std::vector<ov::element::Type> weights_precisions = {ov::element::u8,
ov::element::i8,
ov::element::u4,
- ov::element::i4};
+ ov::element::i4,
+ ov::element::u2};
const std::vector<GatherDecompressionShapeParams> input_shapes_basic = {
{{2, 5}, {{-1, -1}, {{2, 3}}}, 1, 1},
{{15, 32}, {{-1, -1}, {{2, 3}}}, 1, 0, 16},
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp
index 1c162fccce6..3d3261b2c08 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/gather_gpu_test.cpp
@@ -2634,8 +2634,114 @@ public:
ASSERT_FLOAT_EQ(expected_result[i], output_ptr[i]) << "i = " << i;
}
}
+
+ // Every decoded u2 level (0..3) appears, so a wrong unpack shift is visible.
+ void test_compressed_scale_zp_u2(bool is_caching_test, bool use_zp, bool is_dynamic = false) {
+ auto& engine = get_test_engine();
+
+ auto input_mem = engine.allocate_memory({{2, 3}, data_types::i32, format::bfyx});
+ auto weights_mem = engine.allocate_memory({{2, 8}, data_types::u2, format::bfyx});
+ auto scale_mem = engine.allocate_memory({{2, 1}, data_types::f32, format::bfyx});
+ auto zp_mem = use_zp ? engine.allocate_memory({{2, 1}, data_types::f32, format::bfyx}) : nullptr;
+
+ set_values(input_mem, {0, 2, 7,
+ 7, 1, 0});
+ // u2 is 4 values per byte, LSB-first: 0x39 -> {1,2,3,0} and 0x1B -> {3,2,1,0}. Both rows
+ // repeat their byte so index i and i+4 must decode identically.
+ set_values<uint8_t>(weights_mem, {0x39, 0x39,
+ 0x1B, 0x1B});
+ set_values(scale_mem, {2.0f, 4.0f});
+ if (use_zp)
+ set_values(zp_mem, {1.0f, 2.0f});
+
+ auto in_layout = is_dynamic ? layout{ov::PartialShape::dynamic(2), data_types::i32, format::bfyx}
+ : input_mem->get_layout();
+ topology topology(input_layout("input", in_layout),
+ data("weights", weights_mem),
+ data("scale", scale_mem));
+ if (use_zp) {
+ topology.add(data("zp", zp_mem));
+ }
+ topology.add(gather("gather_prim",
+ input_info("weights"),
+ input_info("input"),
+ 1,
+ input_info("scale"),
+ use_zp ? input_info("zp") : input_info(""),
+ data_types::f32,
+ 2,
+ is_dynamic ? ov::Shape{} : ov::Shape{2, 3},
+ 1));
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ network::ptr network = get_network(engine, topology, config, get_test_stream_ptr(), is_caching_test);
+ network->set_input_data("input", input_mem);
+
+ if (is_dynamic) {
+ auto impl = network->get_primitive("gather_prim")->get_impl();
+ ASSERT_TRUE(impl != nullptr);
+ ASSERT_TRUE(impl->is_dynamic());
+ }
+
+ auto outputs = network->execute();
+ ASSERT_EQ(outputs.size(), size_t(1));
+ ASSERT_EQ(outputs.begin()->first, "gather_prim");
+
+ auto output_mem = outputs.begin()->second.get_memory();
+ cldnn::mem_lock<float> output_ptr(output_mem, get_test_stream());
+
+ ov::PartialShape expected_shape{2, 3};
+ ASSERT_EQ(expected_shape, output_mem->get_layout().get_partial_shape());
+
+ // row 0 = {1,2,3,0,1,2,3,0} * 2 - zp*2 ; row 1 = {3,2,1,0,3,2,1,0} * 4 - zp*4
+ // indices are per-batch (batch_dim 1): row 0 takes {0,2,7}, row 1 takes {7,1,0}.
+ std::vector<float> expected_result = use_zp ? std::vector<float>{0.0f, 4.0f, -2.0f, -8.0f, 0.0f, 4.0f}
+ : std::vector<float>{2.0f, 6.0f, 0.0f, 0.0f, 8.0f, 12.0f};
+
+ for (size_t i = 0; i < expected_result.size(); i++) {
+ ASSERT_FLOAT_EQ(expected_result[i], output_ptr[i]) << "i = " << i;
+ }
+ }
};
+TEST_F(gather_gpu_tests, compressed_scale_zp_u2) {
+ this->test_compressed_scale_zp_u2(false, true);
+}
+
+TEST_F(gather_gpu_tests, compressed_scale_zp_u2_cached) {
+ this->test_compressed_scale_zp_u2(true, true);
+}
+
+TEST_F(gather_gpu_tests, compressed_scale_u2) {
+ this->test_compressed_scale_zp_u2(false, false);
+}
+
+TEST_F(gather_gpu_tests, compressed_scale_zp_u2_dynamic) {
+ this->test_compressed_scale_zp_u2(false, true, true);
+}
+
+TEST_F(gather_gpu_tests, compressed_scale_u2_dynamic) {
+ this->test_compressed_scale_zp_u2(false, false, true);
+}
+
+TEST(gather_gpu, u2_uncompressed_not_supported) {
+ auto& engine = get_test_engine();
+
+ auto weights_mem = engine.allocate_memory({{2, 8}, data_types::u2, format::bfyx});
+ auto input_mem = engine.allocate_memory({{2, 3}, data_types::i32, format::bfyx});
+
+ topology topology(input_layout("input", input_mem->get_layout()),
+ data("weights", weights_mem),
+ gather("gather_prim", input_info("weights"), input_info("input"), 1, 2, ov::Shape{2, 3}, 1));
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+
+ EXPECT_ANY_THROW({ network net(engine, topology, config); });
+}
+
TEST_F(gather_gpu_tests, compressed_scale_zp) {
this->test_compressed_scale_zp(false);
}
diff --git a/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cpp b/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cpp
index ed031b4bb84..94987e1b6d6 100644
--- a/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cpp
+++ b/src/tests/test_utils/common_test_utils/src/subgraph_builders/weights_decompression_builders.cpp
@@ -641,7 +641,7 @@ std::shared_ptr<ov::Node> initGatherDecompressionSubgraph(const ov::Shape& data_
1.f,
1);
} else {
- const auto up_to = data_precision == ov::element::i4 ? 7 : 15;
+ const auto up_to = data_precision == ov::element::u2 ? 3 : data_precision == ov::element::i4 ? 7 : 15;
ov::test::utils::InputGenerateData generate_data(0, up_to);
if (data_precision.is_signed())
generate_data.start_from = -1;
@@ -681,12 +681,14 @@ std::shared_ptr<ov::Node> initGatherDecompressionSubgraph(const ov::Shape& data_
if (decompression_subtract_type != DecompressionType::empty) {
const bool scalar_zp = decompression_subtract_type == DecompressionType::scalar;
auto shift_tensor_shape = scalar_zp ? ov::Shape{1} : scaleshift_const_shape;
- const auto up_to = data_precision == ov::element::i4 ? 7 : 15;
+ const auto up_to = data_precision == ov::element::u2 ? 3 : data_precision == ov::element::i4 ? 7 : 15;
auto shift_tensor = ov::test::utils::create_and_fill_tensor(data_precision,
shift_tensor_shape,
ov::test::utils::InputGenerateData(0, up_to));
if (scalar_zp && data_precision.bitwidth() == 4) {
static_cast<uint8_t*>(shift_tensor.data())[0] = 0x88;
+ } else if (scalar_zp && data_precision == ov::element::u2) {
+ static_cast<uint8_t*>(shift_tensor.data())[0] = 0xAA;
}
auto shift_const = std::make_shared<ov::op::v0::Constant>(shift_tensor);
std::shared_ptr<ov::Node> shift_convert = std::make_shared<ov::op::v0::Convert>(shift_const, output_precision);