Skip to content

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

[GPU] Convert scalar i4 weights zero point to i8 for oneDNN FC (#38658)

Vladislav Golubev344d28c8dfd6
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
index 8d01b03d82d..659f09c606a 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_fc_to_compressed.cpp
@@ -92,10 +92,10 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
return new_constant;
};
- auto convert_const_to_u8 = [&](std::shared_ptr<ov::Node> node) {
+ auto convert_zp_const = [&](std::shared_ptr<ov::Node> node) {
auto constant = ov::as_type_ptr<ov::op::v0::Constant>(node);
std::shared_ptr<ov::Node> result = nullptr;
- // Convert ZP to u8
+ // Convert ZP to u8 (or i8 for a scalar i4 ZP)
if (constant->get_element_type() == ov::element::u8) {
result = constant;
} else if (constant->get_element_type() == ov::element::u4 || constant->get_element_type() == ov::element::u2) {
@@ -103,6 +103,9 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
// Only unsigned ZP types can be converted to u8.
} else if (weight_u8 && sub_with_convert && !constant->get_element_type().is_signed()) {
result = std::make_shared<ov::op::v0::Convert>(node, ov::element::u8);
+ } else if (constant->get_element_type() == ov::element::i4 && ov::shape_size(constant->get_shape()) == 1) {
+ // oneDNN doesn't support sub-byte per-tensor (common) zero points, so a scalar i4 ZP is converted to i8.
+ result = std::make_shared<ov::op::v0::Convert>(node, ov::element::i8);
} else {
result = constant;
}
@@ -117,7 +120,7 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
const bool with_zero_point = pattern_map.count(sub_no_convert_m) > 0 || pattern_map.count(sub_with_convert_m) > 0;
if (with_zero_point) {
- optional_zero_point = convert_const_to_u8(reshape_const(pattern_map.at(sub_const_m).get_node_shared_ptr()));
+ optional_zero_point = convert_zp_const(reshape_const(pattern_map.at(sub_const_m).get_node_shared_ptr()));
}
std::shared_ptr<ov::Node> fc_input_b = pattern_map.count(weights_const_m)
@@ -128,6 +131,9 @@ ConvertFullyConnectedToFullyConnectedCompressed::ConvertFullyConnectedToFullyCon
std::shared_ptr<ov::Node> fc_input_zp = optional_zero_point;
std::shared_ptr<ov::Node> fc_input_bias = pattern_map.at(bias_m).get_node_shared_ptr();
std::vector<std::shared_ptr<ov::Node>> result_nodes = {};
+ if (with_zero_point && ov::is_type<ov::op::v0::Convert>(optional_zero_point)) {
+ result_nodes.push_back(optional_zero_point);
+ }
if (fc_input_b->get_output_partial_shape(0).size() != fc_input_scale->get_shape().size()) {
OPENVINO_ASSERT(!pattern_map.count(weights_const_m));
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cpp
index ddd2431505a..10958f3fb3d 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/convert_fc_to_compressed_test.cpp
@@ -662,6 +662,36 @@ TEST_F(TransformationTestsF, ConvertFCToCompressed16_i8ZpNotConvertedToU8) {
}
}
+TEST_F(TransformationTestsF, ConvertFCToCompressed16_i4ScalarZpConvertedToI8) {
+ {
+ auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{ -1, 16 });
+ auto weights_const = ov::op::v0::Constant::create(ov::element::i4, ov::Shape{ 32, 16 }, { 1 });
+ auto convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f16);
+ auto zp_const = ov::op::v0::Constant::create(ov::element::i4, ov::Shape{ 1 }, { -3 });
+ auto zp_convert = std::make_shared<ov::op::v0::Convert>(zp_const, ov::element::f16);
+ auto sub = std::make_shared<ov::op::v1::Subtract>(convert, zp_convert);
+ auto scale_const = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{ 32, 1 }, { 1 });
+ auto scale = std::make_shared<ov::op::v1::Multiply>(sub, scale_const);
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input1, scale, no_bias);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input1});
+ manager.register_pass<ConvertFullyConnectedToFullyConnectedCompressed>();
+ }
+ {
+ // oneDNN rejects sub-byte per-tensor zero points, so the scalar i4 ZP must be widened to i8.
+ auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{ -1, 16 });
+ auto weights_const = ov::op::v0::Constant::create(ov::element::i4, ov::Shape{ 32, 16 }, { 1 });
+ auto scale_const = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{ 32, 1 }, { 1 });
+ auto zp_const = ov::op::v0::Constant::create(ov::element::i4, ov::Shape{ 1 }, { -3 });
+ auto zp_convert = std::make_shared<ov::op::v0::Convert>(zp_const, ov::element::i8);
+ auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>();
+ auto fc_compressed = std::make_shared<ov::intel_gpu::op::FullyConnectedCompressed>(input1, weights_const, no_bias, scale_const, zp_convert);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{fc_compressed}, ov::ParameterVector{input1});
+ }
+}
+
// 4D non-grouped weight constant: leading dims are unit, only last two are
// real (K, N). is_weight_3d is false, so the reshape branch must collapse
// dims [d0, d1, d2, d3] -> [d0*d1*d2, d3] for weight/scale/zp.