Skip to content

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

[ONNX FE] MatMulNBits: support reordered B/scales/zero_points layout (#38000)

Ankit Maheshkarc87a770366c3
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 ded9cd4cf94..0be8e3c747b 100644
--- a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp
@@ -5,6 +5,8 @@
#include <algorithm>
#include <cmath>
#include <numeric>
+#include <optional>
+#include <vector>
#include "core/null_node.hpp"
#include "core/operator_set.hpp"
@@ -20,6 +22,7 @@
#include "openvino/op/reshape.hpp"
#include "openvino/op/slice.hpp"
#include "openvino/op/subtract.hpp"
+#include "openvino/op/transpose.hpp"
#include "utils/common.hpp"
#include "utils/reshape.hpp"
@@ -39,6 +42,27 @@ void preserve_initializer_name(const std::shared_ptr<v0::Constant>& repacked,
repacked->set_friendly_name(original->get_friendly_name());
repacked->get_output_tensor(0).set_names(original->get_output_tensor(0).get_names());
}
+
+// A static shape carries an explicit 2-axis order hint when it is rank 2, or rank 3 with a trailing
+// size-1 axis (e.g. the documented [N][n_blocks_per_col][1] zero_points layout). Rank 1 (a flattened
+// buffer) carries no such hint - both axis orders flatten to the same buffer, so shape alone cannot
+// confirm or refute an assumed order in that case.
+std::optional<std::pair<uint64_t, uint64_t>> get_ordered_dims(const ov::PartialShape& shape) {
+ if (!shape.rank().is_static()) {
+ return std::nullopt;
+ }
+ const auto rank = shape.size();
+ if (rank == 2 && shape[0].is_static() && shape[1].is_static()) {
+ return std::make_pair(static_cast<uint64_t>(shape[0].get_length()),
+ static_cast<uint64_t>(shape[1].get_length()));
+ }
+ if (rank == 3 && shape[0].is_static() && shape[1].is_static() && shape[2].is_static() &&
+ shape[2].get_length() == 1) {
+ return std::make_pair(static_cast<uint64_t>(shape[0].get_length()),
+ static_cast<uint64_t>(shape[1].get_length()));
+ }
+ return std::nullopt;
+}
} // namespace
ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
@@ -106,6 +130,28 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
actual_b_size == expected_b_size,
"Expected input B shape is static and compatible with shape [N][n_blocks_per_col][blob_size], got: ",
b_shape);
+
+ // Some exporters store B (and, consistently, scales/zero_points) with n_blocks_per_col as the
+ // outermost axis instead of N, i.e. [n_blocks_per_col][N][blob_size] rather than the documented
+ // [N][n_blocks_per_col][blob_size]. Detect this from B's own static shape. When N == n_blocks_per_col
+ // the two layouts are shape-identical and indistinguishable (the MatMulNBits schema carries no
+ // layout-hint attribute) - default to the standard-layout assumption in that case, exactly as before
+ // this reordered-layout support existed, so no previously-working model changes behavior.
+ const bool b_is_reordered =
+ b_shape_static.size() == 3 && b_shape_static[0] == static_cast<size_t>(n_blocks_per_col) &&
+ b_shape_static[1] == static_cast<size_t>(N) && n_blocks_per_col != static_cast<uint64_t>(N);
+ if (b_is_reordered) {
+ const auto& scales_shape = scales.get_partial_shape();
+ if (const auto hint = get_ordered_dims(scales_shape)) {
+ CHECK_VALID_NODE(node,
+ hint->first == n_blocks_per_col && hint->second == static_cast<uint64_t>(N),
+ "MatMulNBits: B is stored in the reordered [n_blocks_per_col][N][blob_size] layout, "
+ "but scales shape does not match the expected reordered [n_blocks_per_col][N] "
+ "layout, got: ",
+ scales_shape);
+ }
+ }
+
CHECK_VALID_NODE(node,
a.get_element_type() == ov::element::f16 || a.get_element_type() == ov::element::f32 ||
a.get_element_type() == ov::element::bf16 || a.get_element_type() == ov::element::dynamic,
@@ -172,6 +218,10 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
", ",
*minmax.second,
"]");
+ CHECK_VALID_NODE(node,
+ !b_is_reordered,
+ "MatMulNBits limitation: reordered B layout ([n_blocks_per_col][N][blob_size]) combined "
+ "with group_idx (non-sequential grouping) is not supported");
}
if (common::is_input_valid(node, 5)) {
@@ -205,27 +255,25 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
// Casting/converting data of source constant.
// For further calculations (sub and/or multiply) we need to reshape
// b -> [N][n_blocks_per_col][block_size]
+ // b's leading two axes are [N][n_blocks_per_col] normally, or the reordered
+ // [n_blocks_per_col][N] when b_is_reordered - either way the axis sizes are known up front.
+ const size_t b_dim0 = b_is_reordered ? static_cast<size_t>(n_blocks_per_col) : static_cast<size_t>(N);
+ const size_t b_dim1 = b_is_reordered ? static_cast<size_t>(N) : static_cast<size_t>(n_blocks_per_col);
switch (bits) {
case 2:
- casted_b_shape = ov::Shape{static_cast<size_t>(N),
- static_cast<size_t>(n_blocks_per_col),
- static_cast<size_t>(blob_size * 4)};
+ casted_b_shape = ov::Shape{b_dim0, b_dim1, static_cast<size_t>(blob_size * 4)};
casted_b = std::make_shared<v0::Constant>(ov::element::u2, casted_b_shape, b_const->get_data_ptr());
default_zp = std::make_shared<v0::Constant>(ov::element::u2, Shape{1}, 2);
zp_element_type = ov::element::u2;
break;
case 4:
- casted_b_shape = ov::Shape{static_cast<size_t>(N),
- static_cast<size_t>(n_blocks_per_col),
- static_cast<size_t>(blob_size * 2)};
+ casted_b_shape = ov::Shape{b_dim0, b_dim1, static_cast<size_t>(blob_size * 2)};
casted_b = std::make_shared<v0::Constant>(ov::element::u4, casted_b_shape, b_const->get_data_ptr());
default_zp = std::make_shared<v0::Constant>(ov::element::u4, Shape{1}, 8);
zp_element_type = ov::element::u4;
break;
case 8:
- casted_b_shape = ov::Shape{static_cast<size_t>(N),
- static_cast<size_t>(n_blocks_per_col),
- static_cast<size_t>(blob_size)};
+ casted_b_shape = ov::Shape{b_dim0, b_dim1, static_cast<size_t>(blob_size)};
casted_b = std::make_shared<v0::Constant>(ov::element::u8, casted_b_shape, b_const->get_data_ptr());
default_zp = std::make_shared<v0::Constant>(ov::element::u8, Shape{1}, 128);
zp_element_type = ov::element::u8;
@@ -266,7 +314,8 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
CHECK_VALID_NODE(node,
zp_shape.is_static(),
"Expected input Zero Point shape is static and compatible with shape "
- "[N][n_blocks_per_col][1], got: ",
+ "[N][n_blocks_per_col][1] (or, if B is reordered, "
+ "[n_blocks_per_col][N][1]), got: ",
zp_shape);
const auto zp_shape_static = zp_shape.get_shape();
@@ -276,11 +325,23 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
}
CHECK_VALID_NODE(node,
actual_zp_size == expected_zp_size,
- "Expected input Zero Point shape is compatible with shape [N][n_blocks_per_col][1], "
- "got: ",
+ "Expected input Zero Point shape is compatible with shape [N][n_blocks_per_col][1] "
+ "(or, if B is reordered, [n_blocks_per_col][N][1]), got: ",
zp_shape);
- ov::Shape casted_zp_shape = ov::Shape{static_cast<size_t>(N), static_cast<size_t>(n_blocks_per_col), 1};
+ if (b_is_reordered) {
+ if (const auto hint = get_ordered_dims(zp_shape)) {
+ CHECK_VALID_NODE(node,
+ hint->first == n_blocks_per_col && hint->second == static_cast<uint64_t>(N),
+ "MatMulNBits: B is stored in the reordered [n_blocks_per_col][N][blob_size] "
+ "layout, but zero_points shape does not match the expected reordered "
+ "[n_blocks_per_col][N] layout, got: ",
+ zp_shape);
+ }
+ }
+ ov::Shape casted_zp_shape =
+ b_is_reordered ? ov::Shape{static_cast<size_t>(n_blocks_per_col), static_cast<size_t>(N), 1}
+ : ov::Shape{static_cast<size_t>(N), static_cast<size_t>(n_blocks_per_col), 1};
converted_zero_points = std::make_shared<v0::Constant>(a.get_element_type(),
casted_zp_shape,
zero_points_const->get_data_ptr());
@@ -291,28 +352,70 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
preserve_initializer_name(casted_zp_const, zero_points_const);
}
} else if (zero_points.get_element_type() == ov::element::u8) {
- // for alignment, n_blocks_per_col might not aligned to num_per_byte
- uint64_t num_per_byte = 8 / bits;
- uint64_t num_byte = (n_blocks_per_col + (num_per_byte - 1)) / num_per_byte;
- uint64_t num_elements_aligned = num_byte * num_per_byte;
+ // Packed along whichever axis is innermost in the chosen layout: n_blocks_per_col for the
+ // documented [N][n_blocks_per_col][blob_size] layout, N for the reordered
+ // [n_blocks_per_col][N][blob_size] layout. outer_dim is the other axis, kept as the
+ // leading dim of the repacked Constant.
+ const uint64_t num_per_byte = 8 / bits;
+ const uint64_t outer_dim = b_is_reordered ? n_blocks_per_col : static_cast<uint64_t>(N);
+ const uint64_t pack_dim = b_is_reordered ? static_cast<uint64_t>(N) : n_blocks_per_col;
+ // for alignment, pack_dim might not be aligned to num_per_byte
+ const uint64_t num_byte = (pack_dim + (num_per_byte - 1)) / num_per_byte;
+ const uint64_t num_elements_aligned = num_byte * num_per_byte;
+
+ if (b_is_reordered) {
+ // The producer unpacks the standard [N][ceil(n_blocks_per_col*bits/8)] buffer,
+ // transposes to [n_blocks_per_col][N], then repacks along the new last axis (N).
+ // Validate both the buffer's actual byte size and, when known, its per-axis shape
+ // before constructing the Constant - the ONNX op itself does not guarantee N is a
+ // multiple of 8/bits, and a same-byte-count buffer stored in the standard layout must
+ // not be silently reinterpreted with the reordered layout's different row boundaries.
+ const auto& zp_shape_dyn = zero_points.get_partial_shape();
+ CHECK_VALID_NODE(node,
+ zp_shape_dyn.is_static(),
+ "Expected input Zero Point shape is static and compatible with the "
+ "reordered [n_blocks_per_col][ceil(N*bits/8)] packed layout, got: ",
+ zp_shape_dyn);
+ const auto zp_shape_static = zp_shape_dyn.get_shape();
+ if (const auto hint = get_ordered_dims(zp_shape_dyn)) {
+ CHECK_VALID_NODE(node,
+ hint->first == outer_dim && hint->second == num_byte,
+ "MatMulNBits: B is stored in the reordered "
+ "[n_blocks_per_col][N][blob_size] layout, but zero_points shape does "
+ "not match the expected reordered [n_blocks_per_col][ceil(N*bits/8)] "
+ "packed layout, got: ",
+ zp_shape_dyn);
+ }
+ const uint64_t actual_zp_bytes = std::accumulate(zp_shape_static.begin(),
+ zp_shape_static.end(),
+ uint64_t{1},
+ std::multiplies<uint64_t>{});
+ const uint64_t expected_zp_bytes = outer_dim * num_byte;
+ CHECK_VALID_NODE(node,
+ actual_zp_bytes == expected_zp_bytes,
+ "MatMulNBits: reordered zero_points buffer size (",
+ actual_zp_bytes,
+ ") does not match expected packed size n_blocks_per_col * ceil(N * bits / 8) "
+ "(",
+ expected_zp_bytes,
+ ")");
+ }
+
ov::Shape casted_zp_shape =
- ov::Shape{static_cast<size_t>(N), static_cast<size_t>(num_elements_aligned), 1};
+ ov::Shape{static_cast<size_t>(outer_dim), static_cast<size_t>(num_elements_aligned), 1};
auto casted_zp_org =
std::make_shared<v0::Constant>(zp_element_type, casted_zp_shape, zero_points_const->get_data_ptr());
- // Preserve the original zero_point name on the repacked Constant (as done for B) so weight
- // sharing can promote it. The packed Constant keeps the source uint8 byte count, so the
- // promoted input's tensor matches the external weight at runtime - true whether or not the
- // Slice below is inserted, so name it unconditionally.
+ // Preserve the original zero_point name on the repacked Constant (as done for B) so
+ // weight sharing can promote it. The packed Constant keeps the source uint8 byte count, so
+ // the promoted input's tensor matches the external weight at runtime - true whether or not
+ // the Slice below is inserted, so name it unconditionally.
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 (n_blocks_per_col != num_elements_aligned) {
- // if not align
- // for example, n_blocks_per_col is 13, bits is 2, num_per_byte is 4, it will packed into 4 bytes
- // need to make a constant: uint2, {N, 16}
- // then slice to: uint2, {N, 13}
- const auto num_elements = std::make_shared<v0::Constant>(ov::element::i32,
- Shape{1},
- static_cast<int32_t>(n_blocks_per_col));
+ 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);
}
@@ -372,14 +475,25 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
b = dequant;
}
} else {
- // sub and scale via the shared low-precision dequantization helper
+ // sub and scale via the shared low-precision dequantization helper. When B is reordered,
+ // scales are reordered the same way, so reshape to match instead of transposing.
const auto scales_reshaped =
- op::util::reshape(scales_converted,
- ov::Shape{static_cast<size_t>(N), static_cast<size_t>(n_blocks_per_col), 1});
+ b_is_reordered
+ ? op::util::reshape(scales_converted,
+ ov::Shape{static_cast<size_t>(n_blocks_per_col), static_cast<size_t>(N), 1})
+ : op::util::reshape(scales_converted,
+ ov::Shape{static_cast<size_t>(N), static_cast<size_t>(n_blocks_per_col), 1});
- auto scaled_b =
+ ov::Output<ov::Node> scaled_b =
ov::decomposition::low_precision_dequantize(casted_b, scales_reshaped, converted_zero_points);
+ if (b_is_reordered) {
+ // scaled_b is [n_blocks_per_col, N, block_size]; transpose the leading two axes back to
+ // the documented [N, n_blocks_per_col, block_size] before flattening to [N, K].
+ const auto perm = v0::Constant::create(ov::element::i64, ov::Shape{3}, std::vector<int64_t>{1, 0, 2});
+ scaled_b = std::make_shared<v1::Transpose>(scaled_b, perm);
+ }
+
// reshape b to [N, K]
auto shape_b = v0::Constant::create(ov::element::i32, ov::Shape{2}, {0, -1});
auto reshaped_b = std::make_shared<v1::Reshape>(scaled_b, shape_b, true);
diff --git a/src/frontends/onnx/tests/convert_tests.cpp b/src/frontends/onnx/tests/convert_tests.cpp
index cbfd5e9b872..0b04f8ab16b 100644
--- a/src/frontends/onnx/tests/convert_tests.cpp
+++ b/src/frontends/onnx/tests/convert_tests.cpp
@@ -90,6 +90,12 @@ TEST(ONNXFeConvertException, exception_if_matmulnbits_group_idx_out_of_range) {
testing::HasSubstr("group_idx values must be within"));
}
+TEST(ONNXFeConvertException, exception_if_matmulnbits_reordered_layout_with_group_idx_unsupported) {
+ OV_EXPECT_THROW(convert_model("com.microsoft/matmulnbits_reordered_group_idx_unsupported.onnx"),
+ ov::AssertFailure,
+ testing::HasSubstr("reordered B layout"));
+}
+
TEST(ONNXFeConvertException, exception_if_scan_num_scan_inputs_exceeds_body_inputs) {
OV_EXPECT_THROW(convert_model("scan15_num_scan_inputs_exceeds_body_inputs.onnx"),
ov::frontend::OpConversionFailure,
diff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_2bit_zp_u8_unaligned.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_2bit_zp_u8_unaligned.prototxt
new file mode 100644
index 00000000000..1c6b8955fe4
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_2bit_zp_u8_unaligned.prototxt
@@ -0,0 +1,101 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "a"
+ input: "b_q"
+ input: "b_scales"
+ input: "b_zp"
+ output: "c"
+ op_type: "MatMulNBits"
+ attribute {
+ name: "K"
+ i: 32
+ type: INT
+ }
+ attribute {
+ name: "N"
+ i: 5
+ type: INT
+ }
+ attribute {
+ name: "accuracy_level"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "bits"
+ i: 2
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 16
+ type: INT
+ }
+ domain: "com.microsoft"
+ }
+ name: "matmulnbits_reordered_2bit_zp_u8_unaligned"
+ initializer {
+ dims: 2
+ dims: 5
+ dims: 4
+ data_type: 2
+ name: "b_q"
+ raw_data: "\034\307R\247-\324\263\315\367\250B\2058\346cmr\334}\220\017\221k\272\255\t\342\253%Y\205\325\320\337eH\215\332\224\226"
+ }
+ initializer {
+ dims: 2
+ dims: 5
+ data_type: 1
+ name: "b_scales"
+ raw_data: "\373#\311>\323\\f>\312\351;?w\361\377=\031\274\035?\256\3272?6\"\375>=\266\254>\033oM?\322\2166?"
+ }
+ initializer {
+ dims: 2
+ dims: 2
+ data_type: 2
+ name: "b_zp"
+ raw_data: "\'\003\267\001"
+ }
+ input {
+ name: "a"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 5
+ }
+ dim {
+ dim_value: 32
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "c"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 5
+ }
+ dim {
+ dim_value: 5
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 7
+}
+opset_import {
+ domain: "com.microsoft"
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_4bit_zp_sametype.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_4bit_zp_sametype.prototxt
new file mode 100644
index 00000000000..49e39c57271
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_4bit_zp_sametype.prototxt
@@ -0,0 +1,101 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "a"
+ input: "b_q"
+ input: "b_scales"
+ input: "b_zp"
+ output: "c"
+ op_type: "MatMulNBits"
+ attribute {
+ name: "K"
+ i: 17
+ type: INT
+ }
+ attribute {
+ name: "N"
+ i: 3
+ type: INT
+ }
+ attribute {
+ name: "accuracy_level"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "bits"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 16
+ type: INT
+ }
+ domain: "com.microsoft"
+ }
+ name: "matmulnbits_reordered_4bit_zp_sametype"
+ initializer {
+ dims: 2
+ dims: 3
+ dims: 8
+ data_type: 2
+ name: "b_q"
+ raw_data: "\273\373\377\360\250\376\217e\030(T[\177\2031\023M\302\353\374\017\365\213G;-\327\3458\035EOX\313e>\371\303xX\323\203\317~0Ct\222"
+ }
+ initializer {
+ dims: 2
+ dims: 3
+ data_type: 1
+ name: "b_scales"
+ raw_data: "\017\224`?\270\0275?[\036C?\032\234`?\223\2421?J\330\231>"
+ }
+ initializer {
+ dims: 2
+ dims: 3
+ data_type: 1
+ name: "b_zp"
+ raw_data: "\000\000PA\000\000@A\000\000\340@\000\000\000@\000\000PA\000\000\000\000"
+ }
+ input {
+ name: "a"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 17
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "c"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 7
+}
+opset_import {
+ domain: "com.microsoft"
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_8bit_zp_u8packed.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_8bit_zp_u8packed.prototxt
new file mode 100644
index 00000000000..53af9847071
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_8bit_zp_u8packed.prototxt
@@ -0,0 +1,101 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "a"
+ input: "b_q"
+ input: "b_scales"
+ input: "b_zp"
+ output: "c"
+ op_type: "MatMulNBits"
+ attribute {
+ name: "K"
+ i: 32
+ type: INT
+ }
+ attribute {
+ name: "N"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "accuracy_level"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "bits"
+ i: 8
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 16
+ type: INT
+ }
+ domain: "com.microsoft"
+ }
+ name: "matmulnbits_reordered_8bit_zp_u8packed"
+ initializer {
+ dims: 2
+ dims: 4
+ dims: 16
+ data_type: 2
+ name: "b_q"
+ raw_data: "\334{\nR\225\272Nj\347\350o\205\312&\362\240\333\014\354Ve\374\311\022p\350\362\257\2073\300\344!\370&J\370\214\356\210\n\264a\327\364\275\271\363\333\251\227\273\203\346\262\022_(\301\352\302\267\r\332\211\022\360Wh\210\327\356\341\260\211\337\237U\273\251\356T\306\215W\344 G\311E\372\312\003`\341\265\360\205r\324\35399\022;0\3056\302xT\371w\022l\213\316\207=\220\207\211\005/\370\330\260$"
+ }
+ initializer {
+ dims: 2
+ dims: 4
+ data_type: 1
+ name: "b_scales"
+ raw_data: "\271\035\035?\270Oa>\020\335\017>\336o\034?\240]C?Ef\034?*G\253>\250y\370>"
+ }
+ initializer {
+ dims: 2
+ dims: 4
+ data_type: 2
+ name: "b_zp"
+ raw_data: "\317/O\231\236\033\223H"
+ }
+ input {
+ name: "a"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 32
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "c"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 7
+}
+opset_import {
+ domain: "com.microsoft"
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_group_idx_unsupported.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_group_idx_unsupported.prototxt
new file mode 100644
index 00000000000..50c730abaaa
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_reordered_group_idx_unsupported.prototxt
@@ -0,0 +1,101 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "a"
+ input: "b_q"
+ input: "b_scales"
+ input: ""
+ input: "group_idx"
+ output: "c"
+ op_type: "MatMulNBits"
+ attribute {
+ name: "K"
+ i: 17
+ type: INT
+ }
+ attribute {
+ name: "N"
+ i: 3
+ type: INT
+ }
+ attribute {
+ name: "accuracy_level"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "bits"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 16
+ type: INT
+ }
+ domain: "com.microsoft"
+ }
+ name: "matmulnbits_reordered_group_idx_unsupported"
+ initializer {
+ dims: 2
+ dims: 3
+ dims: 8
+ data_type: 2
+ name: "b_q"
+ raw_data: "\177\325\030\306\205)w!G&\317,\255$\202\356h\232\227\343|\226\206=\217$\276\272\215\024\372\301-\031\310$q\352p\227\317\002\310\213\341\3729\233"
+ }
+ initializer {
+ dims: 2
+ dims: 3
+ data_type: 1
+ name: "b_scales"
+ raw_data: "h\307\017?\304\302\010?\tt\014>]\251,?#H\270>\325`\211>"
+ }
+ initializer {
+ dims: 17
+ data_type: 6
+ name: "group_idx"
+ raw_data: "\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000"
+ }
+ input {
+ name: "a"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 17
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "c"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 7
+}
+opset_import {
+ domain: "com.microsoft"
+ version: 1
+}
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 b5d21a929c3..ad2a2cd82c1 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -2551,6 +2551,146 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_bf16) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_reordered_4bit_zp_sametype) {
+ // B/scales/zero_points stored with n_blocks_per_col as the outermost axis instead of N (some
+ // exporters produce this layout). zero_points here has the same float type as A (unpacked).
+ const auto model = convert_model("com.microsoft/matmulnbits_reordered_4bit_zp_sametype.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ test_case.add_input<float>(
+ {0.7347556352615356f, 0.6849520802497864f, 0.4440719187259674f, -1.7594507932662964f,
+ 1.9110771417617798f, -0.24419349431991577f, 0.1303800791501999f, -1.9874708652496338f,
+ -0.9949315786361694f, 1.4339617490768433f, -0.2988066077232361f, 0.9432759881019592f,
+ 1.6881728172302246f, -1.3861032724380493f, 1.9690369367599487f, -1.2706729173660278f,
+ 1.7604515552520752f, -1.6524677276611328f, -0.12715713679790497f, 1.3159568309783936f,
+ -0.8757909536361694f, 1.4763660430908203f, 1.9056663513183594f, 1.3668546676635742f,
+ -0.20450559258460999f, -0.5179662704467773f, -0.06932225078344345f, -1.631273865699768f,
+ -1.093267560005188f, 0.1462649405002594f, 0.932926595211029f, -0.204356849193573f,
+ -0.7779968976974487f, 1.7894383668899536f, 1.0041353702545166f, -0.11180616170167923f,
+ -0.1708964854478836f, 0.9853512048721313f, 1.4215995073318481f, -0.8259937763214111f,
+ 0.9332579970359802f, 1.2353228330612183f, 1.5483479499816895f, -1.9006671905517578f,
+ 0.0501977913081646f, 0.45114272832870483f, -0.8786041736602783f, -0.1914730966091156f,
+ 0.12488435953855515f, 1.9545382261276245f, -1.9760873317718506f});
+ test_case.add_expected_output<float>(
+ Shape{3, 3},
+ {10.503861f, -17.093567f, -12.628984f, 4.87547f, -2.9147573f, 0.59264785f, -37.1062f, -11.870137f, 33.450912f});
+ test_case.run_with_tolerance_as_fp(0.01f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_reordered_2bit_zp_u8_unaligned) {
+ // Same reordered layout, but zero_points is u8-packed and N=5 is not a multiple of 8/bits (4),
+ // exercising the alignment/padding handling in the reordered zero_points decode path.
+ const auto model = convert_model("com.microsoft/matmulnbits_reordered_2bit_zp_u8_unaligned.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ test_case.add_input<float>(
+ {0.9393449425697327f, 0.4145110547542572f, 0.589850127696991f, 0.7279549837112427f,
+ 1.4777202606201172f, -1.3813774585723877f, 0.1258975863456726f, -1.0826631784439087f,
+ -1.9321128129959106f, -1.0586965084075928f, 1.461916208267212f, 0.005173746030777693f,
+ 1.9516717195510864f, -1.2592782974243164f, 1.4825961589813232f, 1.077378511428833f,
+ -0.23132336139678955f, 1.1484136581420898f, -0.5731927156448364f, 1.9730037450790405f,
+ 1.3979188203811646f, -1.730517864227295f, 1.8215359449386597f, 1.7163277864456177f,
+ -0.8812527060508728f, -0.9456503987312317f, -0.15320809185504913f, -0.9671792984008789f,
+ 0.1935875564813614f, 0.39854925870895386f, -1.9233468770980835f, -1.9077321290969849f,
+ 1.3272274732589722f, 0.4214099943637848f, 1.8763823509216309f, 1.6511989831924438f,
+ 1.7225196361541748f, -0.28912463784217834f, -0.3920738399028778f, -0.5792192220687866f,
+ -0.273803174495697f, 0.1868472695350647f, 1.0479730367660522f, 1.9404098987579346f,
+ 0.7529048323631287f, 0.7264688014984131f, -1.4465574026107788f, -0.2037639170885086f,
+ -1.4193133115768433f, -0.008727134205400944f, 0.007322024088352919f, 0.7359938025474548f,
+ 0.9065641164779663f, 0.8828064799308777f, -1.2883601188659668f, 0.6907457709312439f,
+ -0.07793418318033218f, -1.8899110555648804f, 0.63033127784729f, 1.6184509992599487f,
+ 1.5314438343048096f, 0.7897061705589294f, -0.6204106211662292f, -1.2984071969985962f,
+ 1.6256135702133179f, -0.6808561682701111f, 0.24426232278347015f, -0.8946446776390076f,
+ 1.7736856937408447f, 1.3145933151245117f, -0.5624397397041321f, 1.7904421091079712f,
+ 0.3683362305164337f, 0.4287474453449249f, 1.4206398725509644f, 1.0034373998641968f,
+ 1.491148591041565f, 0.5930879712104797f, 0.9532124400138855f, -1.0964913368225098f,
+ -0.1217593178153038f, 0.08213547617197037f, -1.1704884767532349f, -0.6563259959220886f,
+ -1.2647346258163452f, 1.878732681274414f, -1.5235087871551514f, 1.5251582860946655f,
+ -1.5020241737365723f, -0.6345223188400269f, 0.7475405335426331f, -0.43893158435821533f,
+ -1.2588474750518799f, -0.5142114758491516f, -1.7941802740097046f, 1.4452804327011108f,
+ -0.6751466393470764f, 1.0655535459518433f, -1.4772944450378418f, 0.37860652804374695f,
+ -1.3910104036331177f, 1.7929937839508057f, -0.052088662981987f, -0.5905745029449463f,
+ -0.8256439566612244f, 1.431290864944458f, -0.18851439654827118f, -1.530277132987976f,
+ 0.24108967185020447f, -1.2554590702056885f, 1.7233737707138062f, 1.9318631887435913f,
+ -0.9324491024017334f, 1.7086201906204224f, 1.9475188255310059f, -0.7954144477844238f,
+ -0.46198606491088867f, 0.13581307232379913f, 0.7248129844665527f, 0.8592182993888855f,
+ -0.22978226840496063f, -1.146733045578003f, 1.7974395751953125f, -0.4036639630794525f,
+ 1.11644446849823f, 1.7458794116973877f, -0.6327758431434631f, -0.24722008407115936f,
+ -0.9954797625541687f, -1.3521196842193604f, -0.9339923858642578f, -1.452980399131775f,
+ -0.3077758848667145f, 0.7100892066955566f, 0.5511314868927002f, -0.16431310772895813f,
+ 1.313930869102478f, 0.16474290192127228f, -1.4019960165023804f, -0.8142822980880737f,
+ -0.39374247193336487f, -0.6187075972557068f, -1.1146999597549438f, -1.4532122611999512f,
+ 1.2026331424713135f, -0.42690524458885193f, -0.6195151209831238f, -0.14084164798259735f,
+ -0.04786252602934837f, 0.49286434054374695f, -1.8038541078567505f, -1.3971035480499268f,
+ 1.3772882223129272f, -1.6693127155303955f, -0.13855737447738647f, 1.4163470268249512f,
+ -1.8417116403579712f, 0.3077119290828705f, -0.09587756544351578f, -0.5739620327949524f});
+ test_case.add_expected_output<float>(Shape{5, 5}, {-2.6197639f, -4.3346739f, -6.653666f, 6.835f, -2.144124f,
+ -12.131384f, 5.7502222f, -2.7676144f, 7.0302262f, -5.0682445f,
+ -0.9029713f, -1.2090427f, -3.225337f, 11.410535f, -3.2802541f,
+ -5.6829667f, 2.9211998f, -5.965548f, -3.7119355f, 4.0489678f,
+ 9.8185177f, 0.42868173f, 6.5680284f, 1.1895915f, 4.6801758f});
+ test_case.run_with_tolerance_as_fp(0.01f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_reordered_8bit_zp_u8packed) {
+ // Same reordered layout, bits=8: exercises the reordered b_dim0/b_dim1 selection for the 8-bit
+ // switch case, plus the u8-packed zero_points path with num_per_byte==1 (no sub-byte packing).
+ const auto model = convert_model("com.microsoft/matmulnbits_reordered_8bit_zp_u8packed.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ test_case.add_input<float>(
+ {0.011297237128019333f, 0.2639770209789276f, 1.9155173301696777f, -1.046911597251892f,
+ 1.007254719734192f, 1.077770471572876f, -1.2379648685455322f, -1.654956579208374f,
+ 1.0455840826034546f, -1.0971945524215698f, 0.26531603932380676f, 0.9556319117546082f,
+ 0.6515865921974182f, -1.575122594833374f, 1.7557649612426758f, 0.301928848028183f,
+ -0.3901387155056f, -1.2708162069320679f, -1.9457541704177856f, -0.4780287742614746f,
+ 0.07643675804138184f, -1.1953157186508179f, 0.9984717965126038f, 1.2307238578796387f,
+ 1.769924283027649f, -0.9672298431396484f, -1.114872932434082f, -1.7504851818084717f,
+ 1.8185904026031494f, -1.3669118881225586f, -1.4827699661254883f, 0.7502116560935974f,
+ 0.6779801845550537f, -0.7912059426307678f, -0.5691749453544617f, 1.3446134328842163f,
+ 1.9378172159194946f, 1.4788663387298584f, -1.1805678606033325f, -1.0565876960754395f,
+ -1.6502262353897095f, -1.6139888763427734f, 1.0596168041229248f, 1.436097502708435f,
+ -1.6684778928756714f, -0.8003507256507874f, -1.2739101648330688f, 1.948478102684021f,
+ -0.8803635835647583f, 1.680298089981079f, 1.6412568092346191f, 1.744085431098938f,
+ 1.653067946434021f, 0.671858549118042f, 0.7395996451377869f, -1.6411906480789185f,
+ 1.1794357299804688f, -0.7903971076011658f, -1.4694697856903076f, -1.6090750694274902f,
+ -1.9016247987747192f, 0.8935728073120117f, -0.7256792187690735f, -1.4603513479232788f,
+ 1.1751266717910767f, -0.8280715346336365f, -0.4045090973377228f, -0.38028696179389954f,
+ 0.3611117899417877f, 0.7514155507087708f, -0.4639444649219513f, -0.979056715965271f,
+ 0.02295805513858795f, -0.38554301857948303f, 1.355053186416626f, -1.2538501024246216f,
+ -0.8159182071685791f, -1.9413402080535889f, -1.5417163372039795f, -0.3452632427215576f,
+ 0.10185366123914719f, -0.21680419147014618f, -1.4182894229888916f, 1.698769450187683f,
+ -1.2998188734054565f, 1.3773936033248901f, -1.6097759008407593f, -1.8518799543380737f,
+ 1.555879831314087f, 0.6866942048072815f, -0.11544779688119888f, -1.4777882099151611f,
+ -1.1849554777145386f, 1.5360894203186035f, 0.6722585558891296f, 1.9999690055847168f,
+ -0.8912732601165771f, -1.7976980209350586f, -0.20894251763820648f, 1.0874147415161133f,
+ 0.33108022809028625f, 0.6654966473579407f, 0.47423359751701355f, -1.4408575296401978f,
+ -1.2941867113113403f, -1.631998062133789f, 0.7517695426940918f, -1.2158950567245483f,
+ 1.4113619327545166f, 0.4747319519519806f, -1.2311383485794067f, 1.9999244213104248f,
+ -1.2012220621109009f, -0.9066212177276611f, 0.8633500337600708f, 1.6727581024169922f,
+ 1.8064895868301392f, 0.4027697741985321f, -1.9474372863769531f, -1.0875697135925293f,
+ 0.40542346239089966f, -1.775869607925415f, 0.7418099045753479f, 1.4230223894119263f,
+ 1.140905499458313f, -1.8906694650650024f, -0.674405038356781f, -1.9228813648223877f});
+ test_case.add_expected_output<float>(Shape{4, 4},
+ {356.887939453125f,
+ -621.6118774414062f,
+ 78.72721099853516f,
+ 109.37584686279297f,
+ -480.3854675292969f,
+ -10.916654586791992f,
+ 7.0790696144104f,
+ 613.084228515625f,
+ 8.069732666015625f,
+ 389.91632080078125f,
+ -31.405590057373047f,
+ 231.06405639648438f,
+ -134.7124786376953f,
+ 13.741459846496582f,
+ 150.95919799804688f,
+ 515.7687377929688f});
+ test_case.run_with_tolerance_as_fp(0.01f);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_quickgelu) {
const auto model = convert_model("com.microsoft/quick_gelu.onnx");
auto test_case = ov::test::TestCase(model, s_device);