Skip to content

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

[Core][ONNX FE] FC Compressed sliced Zero point (#39199)

Yaru Du30ee17a04a9f
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp
index e7cdc051c82..6a0e3778f53 100644
--- a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp
@@ -4,8 +4,11 @@
#include <algorithm>
#include <cmath>
+#include <cstdint>
#include <numeric>
#include <optional>
+#include <string>
+#include <unordered_set>
#include <vector>
#include "core/null_node.hpp"
@@ -32,12 +35,17 @@ using namespace ov::op;
namespace ov::frontend::onnx::com_microsoft::opset_1 {
namespace {
// MatMulNBits repacks B/scales/zero_point initializers into new low-bit Constants. Preserve the
-// original ONNX initializer's friendly name and tensor names on the repacked Constant so weight
-// sharing / weights-as-input can still identify it as the same external weight.
+// original ONNX initializer's friendly name and tensor names, optionally with a suffix for a
+// differently shaped representation.
void preserve_initializer_name(const std::shared_ptr<v0::Constant>& repacked,
- const std::shared_ptr<v0::Constant>& original) {
- repacked->set_friendly_name(original->get_friendly_name());
- repacked->get_output_tensor(0).set_names(original->get_output_tensor(0).get_names());
+ const std::shared_ptr<v0::Constant>& original,
+ const std::string& suffix = {}) {
+ repacked->set_friendly_name(original->get_friendly_name() + suffix);
+ std::unordered_set<std::string> names;
+ for (const auto& name : original->get_output_tensor(0).get_names()) {
+ names.insert(name + suffix);
+ }
+ repacked->get_output_tensor(0).set_names(names);
}
// A static shape carries an explicit 2-axis order hint when it is rank 2, or rank 3 with a trailing
@@ -426,12 +434,30 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
preserve_initializer_name(casted_zp_org, zero_points_const);
converted_zero_points = std::make_shared<v0::Convert>(casted_zp_org, a.get_element_type());
if (pack_dim != num_elements_aligned) {
- // if not aligned, e.g. pack_dim is 13, bits is 2, num_per_byte is 4: packed into 4
- // bytes, so make a constant {outer_dim, 16} then slice to {outer_dim, 13}
- const auto num_elements =
- std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, static_cast<int32_t>(pack_dim));
- converted_zero_points =
- std::make_shared<v8::Slice>(converted_zero_points, zero, num_elements, one, axis);
+ // The ONNX buffer byte-aligns each row. Unpack only the valid values and let Constant
+ // repack them contiguously, so the graph does not need a padding-trimming Slice.
+ const auto packed_data = static_cast<const uint8_t*>(zero_points_const->get_data_ptr());
+ const auto value_mask = (1u << bits) - 1;
+ std::vector<uint8_t> zero_point_values(static_cast<size_t>(outer_dim * pack_dim));
+ for (uint64_t outer = 0; outer < outer_dim; ++outer) {
+ for (uint64_t i = 0; i < pack_dim; ++i) {
+ const auto byte_index = outer * num_byte + i / num_per_byte;
+ const auto bit_shift = (i % num_per_byte) * bits;
+ zero_point_values[static_cast<size_t>(outer * pack_dim + i)] =
+ static_cast<uint8_t>((packed_data[byte_index] >> bit_shift) & value_mask);
+ }
+ }
+ auto unpacked_zp = std::make_shared<v0::Constant>(
+ zp_element_type,
+ ov::Shape{static_cast<size_t>(outer_dim), static_cast<size_t>(pack_dim), 1},
+ zero_point_values);
+ // This Constant omits the ONNX per-row byte padding, so give it a distinct name
+ // from the packed initializer used by consumers that promote weights by name.
+ preserve_initializer_name(unpacked_zp, zero_points_const, "_aligned");
+ converted_zero_points = std::make_shared<v0::Convert>(unpacked_zp, a.get_element_type());
+ } else {
+ preserve_initializer_name(casted_zp_org, zero_points_const);
+ converted_zero_points = std::make_shared<v0::Convert>(casted_zp_org, a.get_element_type());
}
} else {
FRONT_END_THROW("Unexpected zero point type");
diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
index d7451ae5ac1..87e8ba61bb8 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -2464,6 +2464,21 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_3x32_zp) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_3x48_zp_unaligned) {
+ // Three 4-bit zero points per row occupy two bytes, with one padding nibble per row.
+ // Ensure unpacking at import preserves the valid values and inference result.
+ const auto model = convert_model("com.microsoft/matmulnbits_3x48_zp_unaligned.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ test_case.add_input<float>({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4,
+ 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8,
+ 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6,
+ 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
+ test_case.add_input<float>({1, 1, 1, 1, 1, 1, 1, 1, 1});
+ test_case.add_expected_output<float>(Shape{2, 3}, {352, 24, -72, 344, 28, -100});
+ test_case.run_with_tolerance_as_fp(0.01f);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_zp_size_mismatch) {
// An undersized packed uint8 zero_points (1 byte where 6 are needed) must be rejected before the
// Constant copy reads past it.
diff --git a/src/frontends/onnx/tests/onnx_tensor_names.cpp b/src/frontends/onnx/tests/onnx_tensor_names.cpp
index e8cf1e9a954..70b6dc66432 100644
--- a/src/frontends/onnx/tests/onnx_tensor_names.cpp
+++ b/src/frontends/onnx/tests/onnx_tensor_names.cpp
@@ -15,6 +15,7 @@
#include "openvino/op/parameter.hpp"
#include "openvino/op/relu.hpp"
#include "openvino/op/result.hpp"
+#include "openvino/op/slice.hpp"
#include "openvino/op/util/topk_base.hpp"
using namespace ov;
@@ -185,8 +186,7 @@ OPENVINO_TEST(onnx_tensor_names, matmulnbits_b_name_preserved) {
}
OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved) {
- // MatMulNBits repacks the zero-point into a low-bit Constant; it must keep the ONNX initializer name
- // "b_zp" so weight sharing / weights-as-input can identify it (mirrors matmulnbits_b_name_preserved).
+ // MatMulNBits repacks the zero-point into a low-bit Constant; it must retain the ONNX initializer name "b_zp".
const auto model = convert_model("com.microsoft/matmulnbits_3x32_zp.onnx");
const auto ops = model->get_ordered_ops();
@@ -202,18 +202,21 @@ OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved) {
OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved_unaligned) {
// Same as matmulnbits_zp_name_preserved, but n_blocks_per_col (3) is not a multiple of num_per_byte
- // (2 for bits=4), so the packed zero-point takes the Slice path. The repacked Constant must still
- // keep the ONNX initializer name - this is the bug fixed after e07506d308 moved the name-copy into
- // the aligned-only else branch.
+ // (2 for bits=4). The unpacked Constant has a distinct suffixed name and no padding Slice.
const auto model = convert_model("com.microsoft/matmulnbits_3x48_zp_unaligned.onnx");
const auto ops = model->get_ordered_ops();
- EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_zp", {"b_zp"}));
+ EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_zp_aligned", {"b_zp_aligned"}));
- const auto packed_zp = find_by_friendly_name<op::v0::Constant>(ops, "b_zp");
- ASSERT_NE(packed_zp, nullptr);
- EXPECT_EQ(packed_zp->get_element_type(), element::u4);
+ const auto repacked_zp = find_by_friendly_name<op::v0::Constant>(ops, "b_zp_aligned");
+ ASSERT_NE(repacked_zp, nullptr);
+ EXPECT_EQ(repacked_zp->get_element_type(), element::u4);
+ const Shape expected_zp_shape{3, 3, 1};
+ EXPECT_EQ(repacked_zp->get_shape(), expected_zp_shape);
+ EXPECT_FALSE(std::any_of(ops.begin(), ops.end(), [](const std::shared_ptr<Node>& node) {
+ return ov::as_type_ptr<op::v8::Slice>(node) != nullptr;
+ }));
}
OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved_sametype) {
@@ -233,7 +236,7 @@ OPENVINO_TEST(onnx_tensor_names, matmulnbits_zp_name_preserved_sametype) {
OPENVINO_TEST(onnx_tensor_names, matmulnbits_shared_b_name_collision_resolved) {
// One B feeding two MatMulNBits creates two same-named Constants; ResolveNameCollisions (in
- // normalize) keeps "b_Q4" on one and suffixes the other - no manual postfix needed.
+ // normalize) keeps "b_Q4" on one and suffixes the other.
const auto model = convert_model("com.microsoft/matmulnbits_shared_b.onnx");
const auto ops = model->get_ordered_ops();