OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
CVS-188759 : [ONNX FE] MatMulNBits name packed B constant for weight sharing (#36452)
Ankit Maheshkara2c93bf56018
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cppindex 22e4750bac3..183bc29b241 100644--- a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp@@ -176,6 +176,13 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {break;}+ // Preserve the original B initializer name on the repacked weight constant so it can still be+ // identified by name downstream (e.g. for weight sharing). Only B is named.+ if (const auto casted_b_const = ov::as_type_ptr<v0::Constant>(casted_b.get_node_shared_ptr())) {+ casted_b_const->set_friendly_name(b_const->get_friendly_name());+ casted_b_const->get_output_tensor(0).set_names(b_const->get_output_tensor(0).get_names());+ }+ov::Output<ov::Node> converted_zero_points;if (!zero_points.get_node_shared_ptr()) {converted_zero_points = std::make_shared<v0::Convert>(default_zp, a.get_element_type());diff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_shared_b.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_shared_b.prototxtnew file mode 100644index 00000000000..bcf5de65a1e--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_shared_b.prototxt@@ -0,0 +1,141 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+producer_version: ""+model_version: 0+graph {+ name: "test_matmulnbits_shared_b"+ node {+ input: "a"+ input: "b_Q4"+ input: "b_scales"+ output: "c1"+ op_type: "MatMulNBits"+ attribute {+ name: "K"+ i: 4+ 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"+ }+ node {+ input: "a"+ input: "b_Q4"+ input: "b_scales"+ output: "c2"+ op_type: "MatMulNBits"+ attribute {+ name: "K"+ i: 4+ 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"+ }+ initializer {+ dims: 3+ dims: 1+ dims: 8+ data_type: 2+ name: "b_Q4"+ raw_data: "&P\000\000\000\000\000\000\005b\000\000\000\000\000\000\004\204\000\000\000\000\000\000"+ }+ initializer {+ dims: 3+ data_type: 1+ name: "b_scales"+ raw_data: "\000\000 \277\000\000 \277\000\000@\277"+ }+ input {+ name: "a"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ output {+ name: "c1"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ output {+ name: "c2"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+}+opset_import {+ version: 7+}+opset_import {+ version: 1+}diff --git a/src/frontends/onnx/tests/onnx_tensor_names.cpp b/src/frontends/onnx/tests/onnx_tensor_names.cppindex 62e286dde17..7b299a15c43 100644--- a/src/frontends/onnx/tests/onnx_tensor_names.cpp+++ b/src/frontends/onnx/tests/onnx_tensor_names.cpp@@ -8,6 +8,7 @@#include "gtest/gtest.h"#include "onnx_utils.hpp"#include "openvino/op/abs.hpp"+#include "openvino/op/constant.hpp"#include "openvino/op/convolution.hpp"#include "openvino/op/identity.hpp"#include "openvino/op/matmul.hpp"@@ -167,3 +168,53 @@ OPENVINO_TEST(onnx_tensor_names, subgraph_gemm_with_bias) {EXPECT_NE(nullptr, find_by_friendly_name<op::v0::MatMul>(ops, "y/WithoutBiases"));}++OPENVINO_TEST(onnx_tensor_names, matmulnbits_b_name_preserved) {+ // MatMulNBits repacks B into a low-bit Constant; it must keep the ONNX initializer name "b_Q4".+ const auto model = convert_model("com.microsoft/matmulnbits_3x4.onnx");++ const auto ops = model->get_ordered_ops();++ // Repacked weight keeps "b_Q4" as friendly_name and tensor name.+ EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_Q4", {"b_Q4"}));++ // It is the u4 repacked weight, not the original initializer.+ const auto packed_b = find_by_friendly_name<op::v0::Constant>(ops, "b_Q4");+ ASSERT_NE(packed_b, nullptr);+ EXPECT_EQ(packed_b->get_element_type(), element::u4);+}++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.+ const auto model = convert_model("com.microsoft/matmulnbits_shared_b.onnx");++ const auto ops = model->get_ordered_ops();++ // Collect the two named u4 weights directly: counting and uniqueness can't be expressed by the+ // bool/first-match helpers. Unnamed u4 zero-point constants are excluded by the name check.+ std::vector<std::shared_ptr<op::v0::Constant>> packed_weights;+ for (const auto& op : ops) {+ const auto c = ov::as_type_ptr<op::v0::Constant>(op);+ if (c && c->get_element_type() == element::u4 && !c->get_output_tensor(0).get_names().empty()) {+ packed_weights.push_back(c);+ }+ }+ ASSERT_EQ(packed_weights.size(), 2u);++ // Names are unique after normalization.+ std::unordered_set<std::string> friendly_names;+ std::unordered_set<std::string> tensor_names;+ for (const auto& c : packed_weights) {+ friendly_names.insert(c->get_friendly_name());+ const auto& names = c->get_output_tensor(0).get_names();+ tensor_names.insert(names.begin(), names.end());+ }+ EXPECT_EQ(friendly_names.size(), 2u);+ EXPECT_EQ(tensor_names.size(), 2u);++ // Exactly one keeps "b_Q4" so weight sharing still matches.+ EXPECT_TRUE(matching_node_found_in_graph<op::v0::Constant>(ops, "b_Q4", {"b_Q4"}));+ EXPECT_EQ(friendly_names.count("b_Q4"), 1u);+ EXPECT_EQ(tensor_names.count("b_Q4"), 1u);+}