Skip to content

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

[ONNX] Reject undersized MatMulNBits packed zero_points (#37991)

mingmingtasdf93969d50d2b
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 0be8e3c747b..d8b20fc0d6d 100644
--- a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp
@@ -11,6 +11,7 @@
#include "core/null_node.hpp"
#include "core/operator_set.hpp"
#include "exceptions.hpp"
+#include "openvino/core/memory_util.hpp"
#include "openvino/decompositions/low_precision_dequantize.hpp"
#include "openvino/frontend/exception.hpp"
#include "openvino/op/add.hpp"
@@ -403,6 +404,23 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {
ov::Shape casted_zp_shape =
ov::Shape{static_cast<size_t>(outer_dim), static_cast<size_t>(num_elements_aligned), 1};
+ if (!b_is_reordered) {
+ // Standard layout: the Constant below copies required_zp_bytes from the source pointer;
+ // reject an undersized initializer to avoid reading past it (a larger source is fine).
+ // The reordered layout is already size-validated above.
+ const auto required_zp_bytes = ov::util::get_memory_size_safe(zp_element_type, casted_zp_shape);
+ CHECK_VALID_NODE(node,
+ required_zp_bytes.has_value(),
+ "MatMulNBits limitation: cannot compute packed uint8 zero_points size for shape ",
+ casted_zp_shape);
+ CHECK_VALID_NODE(node,
+ zero_points_const->get_byte_size() >= *required_zp_bytes,
+ "MatMulNBits limitation: packed uint8 zero_points is too small for shape "
+ "[N][CeilDiv(n_blocks_per_col * bits, 8)], need at least ",
+ *required_zp_bytes,
+ " bytes, got: ",
+ zero_points_const->get_byte_size());
+ }
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
diff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_zp_size_mismatch.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_zp_size_mismatch.prototxt
new file mode 100644
index 00000000000..b1799dbba00
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_zp_size_mismatch.prototxt
@@ -0,0 +1,107 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+producer_version: ""
+model_version: 0
+graph {
+ name: "test_matmul_2d"
+ node {
+ input: "a"
+ input: "b_Q4"
+ input: "b_scales"
+ input: "b_zp"
+ output: "c"
+ op_type: "MatMulNBits"
+ attribute {
+ name: "K"
+ i: 48
+ type: INT
+ }
+ attribute {
+ name: "N"
+ i: 3
+ type: INT
+ }
+ attribute {
+ name: "accuracy_level"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "bits"
+ i: 4
+ type: INT
+ }
+ attribute {
+ name: "block_size"
+ i: 16
+ type: INT
+ }
+ domain: "com.microsoft"
+ }
+ initializer {
+ dims: 3
+ dims: 3
+ dims: 8
+ data_type: 2
+ name: "b_Q4"
+ raw_data: "\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87\x21\x43\x65\x87"
+ }
+ initializer {
+ dims: 1
+ data_type: 2
+ name: "b_zp"
+ raw_data: "\x23"
+ }
+ input {
+ name: "a"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 48
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "b_scales"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 9
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "c"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ 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 ad2a2cd82c1..63a0913fb92 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -2462,6 +2462,19 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_3x32_zp) {
test_case.run();
}
+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.
+ try {
+ convert_model("com.microsoft/matmulnbits_zp_size_mismatch.onnx");
+ FAIL() << "ONNX Importer did not reject an undersized packed zero_points";
+ } catch (const std::exception& e) {
+ EXPECT_THAT(e.what(), testing::HasSubstr("packed uint8 zero_points is too small"));
+ } catch (...) {
+ FAIL() << "Unexpected exception type thrown";
+ }
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_no_zp_block_size) {
const auto model = convert_model("com.microsoft/matmulnbits_no_zp_block_size.onnx");
auto test_case = ov::test::TestCase(model, s_device);