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
[ONNX] Added handling of MatMulNBits with uint8 zero point packed case and additional shape support (#30845)
bopeng1234dc8edb01b4a4
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cppindex 18c306e1aae..44768c81f83 100644--- a/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/matmulnbits.cpp@@ -47,16 +47,24 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {const uint64_t n_blocks_per_col = (K + block_size - 1) / block_size;const auto blob_size = (block_size * bits + 7) / 8;+ const uint64_t expected_b_size = N * n_blocks_per_col * blob_size;+ const auto& b_shape = b_quantized.get_partial_shape();+ uint64_t actual_b_size = 1;+ for (const auto& d : b_shape) {+ actual_b_size *= d.get_length();+ }+CHECK_VALID_NODE(node, n_blocks_per_col > 0, "Wrong blocks count: ", n_blocks_per_col);CHECK_VALID_NODE(node, blob_size > 0, "Wrong blob size: ", blob_size);// in documentation: ...Input B is a 2D constant Matrix.CHECK_VALID_NODE(node,ov::as_type<v0::Constant>(b_quantized.get_node()) != nullptr,"MatMulNBits limitation: accepting only a constant as a B input");- CHECK_VALID_NODE(node,- b_quantized.get_partial_shape().rank() == 3,- "Expected rank of quantized weights is 3 [N][n_blocks_per_col][blob_size], got: ",- b_quantized.get_partial_shape().rank());+ CHECK_VALID_NODE(+ node,+ b_shape.is_static() && 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);CHECK_VALID_NODE(node,a.get_element_type() == ov::element::f16 || a.get_element_type() == ov::element::f32 ||a.get_element_type() == ov::element::dynamic,@@ -108,6 +116,10 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {"], got: ",bias.get_partial_shape());}+ const auto zero = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, 0);+ const auto one = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, 1);+ const auto elements = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, static_cast<int32_t>(K));+ const auto axis = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, 1);ov::Output<ov::Node> mm_output;{@@ -116,6 +128,7 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {ov::Output<ov::Node> casted_b;ov::Shape casted_b_shape;ov::Output<ov::Node> default_zp;+ ov::element::Type zp_element_type;// 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]@@ -126,6 +139,7 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {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),@@ -133,6 +147,7 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {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),@@ -140,22 +155,73 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {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;break;default:FRONT_END_THROW("Unsupported bits count");break;}+ ov::Output<ov::Node> converted_zero_points;if (!zero_points.get_node_shared_ptr()) {- zero_points = default_zp;+ converted_zero_points = std::make_shared<v0::Convert>(default_zp, a.get_element_type());} else {// https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.MatMulNBits// according to the link, zero point are:// Constrain quantized zero point types to uint8/int32/float16/float.- // Input zero_points is stored as uint8_t or same as type(A). It has the same packing method as input B- zero_points =- op::util::reshape(zero_points,- ov::Shape{static_cast<size_t>(N), static_cast<size_t>(n_blocks_per_col), 1});+ // If zero_points has same type as A+ // it's not packed and has the same shape as Scales. [N * n_blocks_per_col]+ // If zero_points is stored as uint8_t.+ // It has the same packing method as input B. [N * CeilDiv(n_blocks_per_col * bits, 8)]+ //+ // no matter which package method, the outputs of this section will be+ // {A type, [N, n_blocks_per_col, 1]}+ CHECK_VALID_NODE(node,+ ov::as_type<v0::Constant>(zero_points.get_node()) != nullptr,+ "MatMulNBits limitation: accepting only a constant as a zero_points");++ const auto zero_points_const = ov::as_type_ptr<v0::Constant>(zero_points.get_node_shared_ptr());+ if (zero_points.get_element_type() == a.get_element_type()) {+ const uint64_t expected_zp_size = N * n_blocks_per_col * 1;+ const auto& zp_shape = zero_points.get_partial_shape();+ uint64_t actual_zp_size = 1;+ for (const auto& d : zp_shape) {+ actual_zp_size *= d.get_length();+ }+ CHECK_VALID_NODE(node,+ zp_shape.is_static() && actual_zp_size == expected_zp_size,+ "Expected input Zero Point shape is static and compatible with shape "+ "[N][n_blocks_per_col][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};+ converted_zero_points = std::make_shared<v0::Constant>(a.get_element_type(),+ casted_zp_shape,+ zero_points_const->get_data_ptr());+ } 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;+ ov::Shape casted_zp_shape =+ ov::Shape{static_cast<size_t>(N), 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());+ 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));+ converted_zero_points =+ std::make_shared<v8::Slice>(converted_zero_points, zero, num_elements, one, axis);+ }+ } else {+ FRONT_END_THROW("Unexpected zero point type");+ }}// Possible issue with slice implementation, had to move convertion before slice, instead of slicing uint4@@ -170,7 +236,6 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {// convert b to fp16auto converted_b = std::make_shared<v0::Convert>(casted_b, a.get_element_type());- auto converted_zero_points = std::make_shared<v0::Convert>(zero_points, a.get_element_type());// sub and scaleconst auto sub_b = std::make_shared<v1::Subtract>(converted_b, converted_zero_points);@@ -188,10 +253,6 @@ ov::OutputVector matmulnbits(const ov::frontend::onnx::Node& node) {// to produce b = [N, K]const bool slice_needed = (K % block_size != 0);if (slice_needed) {- const auto zero = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, 0);- const auto one = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, 1);- const auto elements = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, static_cast<int32_t>(K));- const auto axis = std::make_shared<v0::Constant>(ov::element::i32, Shape{1}, 1);b = std::make_shared<v8::Slice>(reshaped_b, zero, elements, one, axis);} else {b = reshaped_b;diff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_3x17_solid_b.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_3x17_solid_b.prototxtnew file mode 100644index 00000000000..e38f58e3e3c--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_3x17_solid_b.prototxt@@ -0,0 +1,97 @@+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: "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"+ }+ initializer {+ dims: 48+ data_type: 2+ name: "b_Q4"+ raw_data: "G\2025`\024G\2025\200\000\000\000\000\000\000\000Fq$X\003Fq$\210\000\000\000\000\000\000\0005`\024G\2025`\024\200\000\000\000\000\000\000\000"+ }+ initializer {+ dims: 6+ data_type: 1+ name: "b_scales"+ raw_data: "\000\000\220\277\000\000\220\277\000\000\220\277\000\000\000\200\000\000\220\277\000\000\000\276"+ }+ initializer {+ dims: 6+ data_type: 2+ name: "zp"+ raw_data: "\x00\x40\x08\x00\x00\x00"+ }+ 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 {+ version: 7+}+opset_import {+ version: 1+}\ No newline at end of filediff --git a/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_3x32_zp.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_3x32_zp.prototxtnew file mode 100644index 00000000000..0673676d6f9--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/matmulnbits_3x32_zp.prototxt@@ -0,0 +1,106 @@+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: 32+ 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: 2+ 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"+ }+ initializer {+ dims: 3+ data_type: 2+ name: "b_zp"+ raw_data: "\x23\x45\x56"+ }+ input {+ name: "a"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 32+ }+ }+ }+ }+ }+ input {+ name: "b_scales"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 6+ }+ }+ }+ }+ }+ output {+ name: "c"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+}+opset_import {+ version: 7+}+opset_import {+ 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.cppindex 88c47e764e4..e4f8bbfebc0 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -1331,6 +1331,40 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_3x17) {test_case.run();}+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_3x17_solid_b) {+ const auto model = convert_model("com.microsoft/matmulnbits_3x17_solid_b.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, 9, 10, 1});++ test_case.add_expected_output<float>(Shape{3, 3},+ {-322.52954f,+ -312.34253f,+ 345.20667f,+ -381.87994f,+ -343.7008f,+ 472.23425f,+ -509.08466f,+ -420.32483f,+ 532.11615f});++ test_case.run_with_tolerance_as_fp(1.f);+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmulnbits_3x32_zp) {+ const auto model = convert_model("com.microsoft/matmulnbits_3x32_zp.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, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4});+ test_case.add_input<float>({1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f});++ test_case.add_expected_output<float>(Shape{2, 3}, {312, -24, -192, 370, 26, -146});+ test_case.run();+}+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);