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 MatMulIntegerToFloat from com.microsoft domain (#27867)
Vatsala Shanubhag19f59c363cea
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/matmul_integer_to_float.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/matmul_integer_to_float.cppnew file mode 100644index 00000000000..d7f1a69db94--- /dev/null+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/matmul_integer_to_float.cpp@@ -0,0 +1,93 @@+// Copyright (C) 2018-2024 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "core/operator_set.hpp"+#include "exceptions.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/matmul.hpp"+#include "openvino/op/multiply.hpp"+#include "openvino/op/subtract.hpp"+#include "utils/common.hpp"++using namespace ov::op;++namespace ov {+namespace frontend {+namespace onnx {+namespace com_microsoft {+namespace opset_1 {++ov::OutputVector matmulintegertofloat(const ov::frontend::onnx::Node& node) {+ common::default_op_checks(node, 4);+ // Original documentation:+ // https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#commicrosoftmatmulintegertofloat+ const auto inputs = node.get_ov_inputs();+ const auto& a_int = inputs[0];+ const auto& b_int = inputs[1];+ const auto& a_scale = inputs[2];+ const auto& b_scale = inputs[3];++ ov::Output<ov::Node> a_zero_point =+ (inputs.size() > 4)+ ? inputs[4]+ : std::make_shared<ov::op::v0::Constant>(a_int.get_element_type(), ov::Shape{}, std::vector<int8_t>{0});+ ov::Output<ov::Node> b_zero_point =+ (inputs.size() > 5)+ ? inputs[5]+ : std::make_shared<ov::op::v0::Constant>(a_int.get_element_type(), ov::Shape{}, std::vector<int8_t>{0});++ CHECK_VALID_NODE(node,+ a_int.get_element_type() == ov::element::i8 || a_int.get_element_type() == ov::element::u8,+ "Unsupported input A type. Expected int8 or uint8, got: ",+ a_int.get_element_type());++ CHECK_VALID_NODE(node,+ b_int.get_element_type() == ov::element::i8 || b_int.get_element_type() == ov::element::u8,+ "Unsupported input B type. Expected int8 or uint8, got: ",+ b_int.get_element_type());++ const auto a_dequantized = std::make_shared<ov::op::v1::Subtract>(a_int, a_zero_point);+ const auto b_dequantized = std::make_shared<ov::op::v1::Subtract>(b_int, b_zero_point);++ const auto a_dequantized_converted =+ std::make_shared<ov::op::v0::Convert>(a_dequantized, a_scale.get_element_type());+ const auto b_dequantized_converted =+ std::make_shared<ov::op::v0::Convert>(b_dequantized, b_scale.get_element_type());++ const auto a_scaled = std::make_shared<ov::op::v1::Multiply>(a_dequantized_converted, a_scale);+ const auto b_scaled = std::make_shared<ov::op::v1::Multiply>(b_dequantized_converted, b_scale);++ const auto matmul_result = std::make_shared<ov::op::v0::MatMul>(a_scaled, b_scaled);++ if (inputs.size() > 6) {+ auto& bias = inputs[6];+ CHECK_VALID_NODE(node,+ bias.get_partial_shape().rank().get_length() == 1,+ "Bias tensor must be 1D. Got shape: ",+ bias.get_partial_shape());++ const auto b_last_dim = b_int.get_partial_shape().get_shape().back();+ const auto bias_dim = bias.get_partial_shape()[0].get_length();+ CHECK_VALID_NODE(node,+ static_cast<long int>(b_int.get_partial_shape().get_shape().back()) == bias_dim,+ "Bias dimension must match the last dimension of B. Expected: ",+ b_last_dim,+ ", but got: ",+ bias_dim);++ return {std::make_shared<ov::op::v1::Add>(matmul_result, bias)};+ }++ return {matmul_result};+}++ONNX_OP("MatMulIntegerToFloat", OPSET_SINCE(1), com_microsoft::opset_1::matmulintegertofloat, MICROSOFT_DOMAIN);++} // namespace opset_1+} // namespace com_microsoft+} // namespace onnx+} // namespace frontend+} // namespace ov\ No newline at end of filediff --git a/src/frontends/onnx/tests/models/com.microsoft/matmul_integer_to_float.prototxt b/src/frontends/onnx/tests/models/com.microsoft/matmul_integer_to_float.prototxtnew file mode 100644index 00000000000..e633480f274--- /dev/null+++ b/src/frontends/onnx/tests/models/com.microsoft/matmul_integer_to_float.prototxt@@ -0,0 +1,149 @@+ir_version: 3+producer_name: "OpenVINO ONNX Frontend"+producer_version: ""+model_version: 0+graph {+ name: "test_matmul_integer_to_float"++ # Node definition for MatMulNBits operation+ node {+ input: "a"+ input: "b"+ input: "a_scale"+ input: "b_scale"+ input: "a_zero_point"+ input: "b_zero_point"+ input: "bias"+ output: "c"+ op_type: "MatMulIntegerToFloat"+ domain: "com.microsoft"+ }+ input {+ name: "a"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }++ input {+ name: "b"+ type {+ tensor_type {+ elem_type: 3+ shape {+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }++ input {+ name: "a_scale"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ input {+ name: "b_scale"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ }+ }+ }+ }++ # Zero point for A (1D tensor)+ input {+ name: "a_zero_point"+ type {+ tensor_type {+ elem_type: 3 # Tensor type for T1 (int8 or uint8)+ shape {+ dim {+ dim_value: 3 # Per-column zero point for A (1D tensor)+ }+ }+ }+ }+ }++ input {+ name: "b_zero_point"+ type {+ tensor_type {+ elem_type: 3 # Tensor type for T2 (int8 or uint8)+ shape {+ dim {+ dim_value: 2+ }+ }+ }+ }+ }++ input {+ name: "bias"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ }+ }+ }+ }++ output {+ name: "c"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+}++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 ca805f4c194..333c06e24a8 100644--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp@@ -1456,3 +1456,35 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_fusedmatmul_2x5x3x6x4_2x6x3x4xtest_case.run();}++OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_matmul_integer_to_float) {+ const auto model = convert_model("com.microsoft/matmul_integer_to_float.onnx");+ auto test_case = ov::test::TestCase(model, s_device);++ const std::vector<int8_t> data_A{10, 20, 30, 40, 50, 60};+ const std::vector<int8_t> data_B{70, 80, 90, 100, 110, 120};++ const std::vector<float> a_scale{0.1f, 0.1f, 0.1f};+ const std::vector<float> b_scale{0.1f, 0.1f};++ const std::vector<int8_t> a_zero_point{1, 0, 2};+ const std::vector<int8_t> b_zero_point{0, 1};++ const std::vector<float> bias{0.5f, 0.25f};++ const std::vector<float> expected_output{55.59999847412109f,+ 60.48000335693359f,+ 136.6000061035156f,+ 149.5800170898438f};++ test_case.add_input<int8_t>(Shape{2, 3}, data_A);+ test_case.add_input<int8_t>(Shape{3, 2}, data_B);+ test_case.add_input<float>(Shape{3}, a_scale);+ test_case.add_input<float>(Shape{2}, b_scale);+ test_case.add_input<int8_t>(Shape{3}, a_zero_point);+ test_case.add_input<int8_t>(Shape{2}, b_zero_point);+ test_case.add_input<float>(Shape{2}, bias);+ test_case.add_expected_output<float>(Shape{2, 2}, expected_output);++ test_case.run();+}