Skip to content

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

[ONNX] Fix DynamicQuantizeMatMul when bias is NullNode (#33546)

Maxim Vafind6dfa6f2f2ae
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/dynamic_quantize_matmul.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/dynamic_quantize_matmul.cpp
index 7f5fa98a3ae..d904d955b65 100644
--- a/src/frontends/onnx/frontend/src/op/com.microsoft/dynamic_quantize_matmul.cpp
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/dynamic_quantize_matmul.cpp
@@ -34,13 +34,6 @@ ov::OutputVector dynamic_quantize_matmul(const ov::frontend::onnx::Node& node) {
ov::Output<ov::Node> b_zero_point; // optional, input[3]
ov::Output<ov::Node> bias; // optional, input[4]
- // Constrain input matrix A to T1 type (float tensor)
- auto element_type_A = A.get_element_type();
- CHECK_VALID_NODE(node,
- element_type_A == ov::element::f32,
- "Unsupported input A type, accepted FP32 but got: ",
- element_type_A);
-
// Constrain input matrix B to T2 type (int8 tensor, uint8 tensor)
auto element_type_B = B.get_element_type();
CHECK_VALID_NODE(node,
@@ -56,7 +49,7 @@ ov::OutputVector dynamic_quantize_matmul(const ov::frontend::onnx::Node& node) {
element_type_b_scale);
// Check for the optional inputs
- if (inputs.size() > 3) {
+ if (common::is_input_valid(node, 3)) {
// Constrain input b_zero_point to T2 type (int8 tensor, uint8 tensor)
b_zero_point = inputs[3];
auto element_type_b_zero_point = b_zero_point.get_element_type();
@@ -66,7 +59,7 @@ ov::OutputVector dynamic_quantize_matmul(const ov::frontend::onnx::Node& node) {
element_type_b_zero_point);
}
- if (inputs.size() > 4) {
+ if (common::is_input_valid(node, 4)) {
// Constrain input bias to T1 type (float tensor)
bias = inputs[4];
auto element_type_bias = bias.get_element_type();
@@ -83,8 +76,10 @@ ov::OutputVector dynamic_quantize_matmul(const ov::frontend::onnx::Node& node) {
// - b_zero_point) * b_scale
ov::Output<ov::Node> B_dequantized = std::make_shared<v0::Convert>(B, b_scale.get_element_type());
- b_zero_point = std::make_shared<v0::Convert>(b_zero_point, b_scale.get_element_type());
- B_dequantized = std::make_shared<v1::Subtract>(B_dequantized, b_zero_point);
+ if (b_zero_point.get_node_shared_ptr()) {
+ b_zero_point = std::make_shared<v0::Convert>(b_zero_point, b_scale.get_element_type());
+ B_dequantized = std::make_shared<v1::Subtract>(B_dequantized, b_zero_point);
+ }
B_dequantized = std::make_shared<v1::Multiply>(B_dequantized, b_scale);
// A, B are N-dimensional matrices. According to example ONNX models for this operator, the suboperations pass input
diff --git a/src/frontends/onnx/tests/models/com.microsoft/dynamic_quantize_matmul_null_bias_zp.prototxt b/src/frontends/onnx/tests/models/com.microsoft/dynamic_quantize_matmul_null_bias_zp.prototxt
new file mode 100644
index 00000000000..4fb21cfbf55
--- /dev/null
+++ b/src/frontends/onnx/tests/models/com.microsoft/dynamic_quantize_matmul_null_bias_zp.prototxt
@@ -0,0 +1,79 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "A"
+ input: "B"
+ input: "b_scale"
+ input: ""
+ input: ""
+ output: "Y"
+ op_type: "DynamicQuantizeMatMul"
+ domain: "com.microsoft"
+ }
+ name: "test_dqmm_example"
+ input {
+ name: "A"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "B"
+ type {
+ tensor_type {
+ elem_type: 3
+ shape {
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "b_scale"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 3
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+}
+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.cpp
index 30a8fb2472e..53528cd06ed 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -1537,6 +1537,35 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_dynamic_quantize_matmul_bias)
}
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_com_microsoft_dynamic_quantize_matmul_null_bias_zp) {
+ const auto model = convert_model("com.microsoft/dynamic_quantize_matmul_null_bias_zp.onnx");
+ auto test_case = ov::test::TestCase(model, s_device);
+
+ // Fill test case here
+ const std::vector<float> input_A{1.29292f, 2.47473f, 3.291903f, 4.1728944f, 5.213912031f, 6.12931230f};
+ const std::vector<int8_t> input_B{-2, -78, -5, 61, -78, -7};
+ const std::vector<float> b_scale{0.0031372549019608f, 0.0015686274509804f, 3.92156862745098e-4f};
+
+ const std::vector<float> expected{0.46564865f,
+ -0.46172738f,
+ -0.00934125f,
+ 0.77972585f,
+ -0.91463196f,
+ -0.01793784f,
+ 1.1402547f,
+ -1.3881232f,
+ -0.02705287f};
+
+ // add_input needs to be called in order of model inputs (order matters)
+ test_case.add_input<float>(Shape{3, 2}, input_A);
+ test_case.add_input<int8_t>(Shape{2, 3}, input_B);
+ test_case.add_input<float>(Shape{3}, b_scale);
+
+ test_case.add_expected_output<float>(Shape{3, 3}, expected);
+
+ test_case.run_with_tolerance_as_fp(0.0055f);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_skip_simplified_layer_normalization) {
const auto model = convert_model("com.microsoft/skip_simplified_layer_normalization.onnx");
auto test_case = ov::test::TestCase(model, s_device);