-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ONNX] Added FusedMatMul from com.microsoft domain (#27556)
Details: Microsoft Contrib Operator "FusedMatMul" for ONNX RT Tickets: N/A --------- Co-authored-by: Georgy Krivoruchko <[email protected]>
- Loading branch information
1 parent
adde531
commit 54c2f60
Showing
4 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/frontends/onnx/frontend/src/op/com.microsoft/fusedmatmul.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
#include "cmath" | ||
#include "core/operator_set.hpp" | ||
#include "exceptions.hpp" | ||
#include "openvino/frontend/exception.hpp" | ||
#include "openvino/op/constant.hpp" | ||
#include "openvino/op/matmul.hpp" | ||
#include "openvino/op/multiply.hpp" | ||
#include "openvino/op/transpose.hpp" | ||
#include "utils/common.hpp" | ||
|
||
using namespace ov::op; | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace onnx { | ||
namespace com_microsoft { | ||
namespace opset_1 { | ||
|
||
std::vector<size_t> get_transpose_axes(size_t rank) { | ||
std::vector<size_t> axes(rank); | ||
std::iota(axes.begin(), axes.end(), 0); | ||
|
||
if (rank > 2) { | ||
std::rotate(axes.begin() + 1, axes.begin() + 2, axes.begin() + rank - 1); | ||
} | ||
return axes; | ||
} | ||
|
||
ov::OutputVector fusedmatmul(const ov::frontend::onnx::Node& node) { | ||
common::default_op_checks(node, 2); | ||
|
||
const auto inputs = node.get_ov_inputs(); | ||
auto A = inputs[0]; // required | ||
auto B = inputs[1]; // required | ||
|
||
const auto alpha = node.get_attribute_value<float>("alpha"); // required | ||
const auto transA = node.get_attribute_value<int64_t>("transA"); // required | ||
const auto transB = node.get_attribute_value<int64_t>("transB"); // required | ||
const auto transBatchA = node.get_attribute_value<int64_t>("transBatchA"); // required | ||
const auto transBatchB = node.get_attribute_value<int64_t>("transBatchB"); // required | ||
|
||
CHECK_VALID_NODE(node, | ||
A.get_element_type() == ov::element::f16 || A.get_element_type() == ov::element::f32 || | ||
A.get_element_type() == ov::element::f64 || A.get_element_type() == ov::element::bf16, | ||
"Unsupported input A type, accepted FP16, FP32, FP64, BFP16 got: ", | ||
A.get_element_type()); | ||
CHECK_VALID_NODE(node, | ||
B.get_element_type() == ov::element::f16 || B.get_element_type() == ov::element::f32 || | ||
B.get_element_type() == ov::element::f64 || B.get_element_type() == ov::element::bf16, | ||
"Unsupported input B type, accepted FP16, FP32, FP64, BFP16 got: ", | ||
B.get_element_type()); | ||
|
||
const auto rankA = A.get_shape().size(); | ||
const auto rankB = B.get_shape().size(); | ||
|
||
if (transBatchA) { | ||
A = std::make_shared<v1::Transpose>( | ||
A, | ||
std::make_shared<v0::Constant>(element::i64, Shape{rankA}, get_transpose_axes(rankA))); | ||
} | ||
|
||
if (transBatchB) { | ||
B = std::make_shared<v1::Transpose>( | ||
B, | ||
std::make_shared<v0::Constant>(element::i64, Shape{rankB}, get_transpose_axes(rankB))); | ||
} | ||
|
||
auto matmul_result = std::make_shared<v0::MatMul>(A, B, transA, transB); | ||
|
||
auto alpha_const = std::make_shared<v0::Constant>(A.get_element_type(), Shape{}, std::vector<float>{alpha}); | ||
auto scaled_result = std::make_shared<v1::Multiply>(matmul_result, alpha_const); | ||
|
||
return {scaled_result}; | ||
} | ||
|
||
ONNX_OP("FusedMatMul", OPSET_SINCE(1), com_microsoft::opset_1::fusedmatmul, MICROSOFT_DOMAIN); | ||
|
||
} // namespace opset_1 | ||
} // namespace com_microsoft | ||
} // namespace onnx | ||
} // namespace frontend | ||
} // namespace ov |
93 changes: 93 additions & 0 deletions
93
src/frontends/onnx/tests/models/com.microsoft/fusedmatmul_2D.prototxt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
ir_version: 3 | ||
producer_name: "OpenVINO ONNX Frontend" | ||
model_version: 0 | ||
graph { | ||
name: "FusedMatMulModel" | ||
node { | ||
op_type: "FusedMatMul" | ||
input: "A" | ||
input: "B" | ||
output: "output" | ||
attribute { | ||
name: "alpha" | ||
f: 0.75 | ||
type: FLOAT | ||
} | ||
attribute { | ||
name: "transA" | ||
i: 1 | ||
type: INT | ||
} | ||
attribute { | ||
name: "transB" | ||
i: 0 | ||
type: INT | ||
} | ||
attribute { | ||
name: "transBatchA" | ||
i: 0 | ||
type: INT | ||
} | ||
attribute { | ||
name: "transBatchB" | ||
i: 0 | ||
type: INT | ||
} | ||
domain: "com.microsoft" | ||
} | ||
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: 1 | ||
shape { | ||
dim { | ||
dim_value: 3 | ||
} | ||
dim { | ||
dim_value: 2 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
output { | ||
name: "output" | ||
type { | ||
tensor_type { | ||
elem_type: 1 | ||
shape { | ||
dim { | ||
dim_value: 2 | ||
} | ||
dim { | ||
dim_value: 2 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
opset_import { | ||
version: 7 | ||
} | ||
opset_import { | ||
version: 1 | ||
domain: "com.microsoft" | ||
} |
102 changes: 102 additions & 0 deletions
102
...ontends/onnx/tests/models/com.microsoft/fusedmatmul_trans_and_transbatch_enabled.prototxt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
ir_version: 3 | ||
producer_name: "OpenVINO ONNX Frontend" | ||
model_version: 0 | ||
graph { | ||
name: "FusedMatMulModel" | ||
node { | ||
op_type: "FusedMatMul" | ||
input: "A" | ||
input: "B" | ||
output: "output" | ||
attribute { | ||
name: "alpha" | ||
f: 0.5 | ||
type: FLOAT | ||
} | ||
attribute { | ||
name: "transA" | ||
i: 1 | ||
type: INT | ||
} | ||
attribute { | ||
name: "transB" | ||
i: 1 | ||
type: INT | ||
} | ||
attribute { | ||
name: "transBatchA" | ||
i: 1 | ||
type: INT | ||
} | ||
attribute { | ||
name: "transBatchB" | ||
i: 1 | ||
type: INT | ||
} | ||
domain: "com.microsoft" | ||
} | ||
input { | ||
name: "A" | ||
type { | ||
tensor_type { | ||
elem_type: 1 | ||
shape { | ||
dim { | ||
dim_value: 2 | ||
} | ||
dim { | ||
dim_value: 5 | ||
} | ||
dim { | ||
dim_value: 4 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
input { | ||
name: "B" | ||
type { | ||
tensor_type { | ||
elem_type: 1 | ||
shape { | ||
dim { | ||
dim_value: 2 | ||
} | ||
dim { | ||
dim_value: 3 | ||
} | ||
dim { | ||
dim_value: 5 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
output { | ||
name: "output" | ||
type { | ||
tensor_type { | ||
elem_type: 1 | ||
shape { | ||
dim { | ||
dim_value: 2 | ||
} | ||
dim { | ||
dim_value: 4 | ||
} | ||
dim { | ||
dim_value: 3 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
opset_import { | ||
version: 7 | ||
} | ||
opset_import { | ||
version: 1 | ||
domain: "com.microsoft" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters