Skip to content

Commit 44e4e5d

Browse files
authored
[ONNX] Change translator registration approach (#25166)
### Details: - Moved to VersionRange usage - Moved a translators registration near a translator code - Simplified file structure - Implemented support of *.lib build - Implemented a supported op customization by removing op-files from the build ### Tickets: - N/A
1 parent 70080bd commit 44e4e5d

File tree

334 files changed

+1759
-5317
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

334 files changed

+1759
-5317
lines changed

src/frontends/onnx/frontend/CMakeLists.txt

+71
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,73 @@
22
# SPDX-License-Identifier: Apache-2.0
33
#
44

5+
if(NOT BUILD_SHARED_LIBS)
6+
file(GLOB_RECURSE op_list "src/op/*.cpp")
7+
set(static_reg_file "src/static_reg.hpp")
8+
file(WRITE ${static_reg_file} "// Copyright (C) 2018-2024 Intel Corporation\n// SPDX-License-Identifier: Apache-2.0\n// Auto generated file, DO NOT EDIT INLINE\n\n")
9+
file(APPEND ${static_reg_file} "#include \"core/operator_set.hpp\"\n\n")
10+
file(APPEND ${static_reg_file} "#define ONNX_DECL_OP(op) extern ov::OutputVector op(const Node&)\n\n")
11+
file(APPEND ${static_reg_file} "namespace ov {\nnamespace frontend {\nnamespace onnx {\n")
12+
foreach(src ${op_list})
13+
file(READ ${src} source_code)
14+
string(REGEX MATCHALL "ONNX_OP([^;]+);" matches "${source_code}")
15+
foreach(match ${matches})
16+
if(${match} MATCHES "([a-z0-9_]+)::([a-z0-9_]+)::([a-z0-9_]+)")
17+
list(APPEND declarations ${CMAKE_MATCH_0})
18+
endif()
19+
list(APPEND registrations ${match})
20+
endforeach()
21+
endforeach()
22+
list(APPEND declarations "com_microsoft::opset_1::register_multiple_translators")
23+
list(APPEND registrations "com_microsoft::opset_1::register_multiple_translators()")
24+
list(SORT declarations)
25+
set(domain "")
26+
set(opset "")
27+
set(op_name, "")
28+
foreach(decl ${declarations})
29+
string(REGEX MATCH "([a-z0-9_]+)::([a-z0-9_]+)::([a-z0-9_]+)" matches ${decl})
30+
if(NOT domain STREQUAL CMAKE_MATCH_1)
31+
if(NOT opset STREQUAL "")
32+
file(APPEND ${static_reg_file} "} // namespace ${opset}\n")
33+
endif()
34+
if(NOT domain STREQUAL "")
35+
file(APPEND ${static_reg_file} "} // namespace ${domain}\n")
36+
endif()
37+
set(domain ${CMAKE_MATCH_1})
38+
set(opset "")
39+
file(APPEND ${static_reg_file} "namespace ${domain} {\n")
40+
endif()
41+
if(NOT opset STREQUAL CMAKE_MATCH_2)
42+
if(NOT opset STREQUAL "")
43+
file(APPEND ${static_reg_file} "} // namespace ${opset}\n")
44+
endif()
45+
set(opset ${CMAKE_MATCH_2})
46+
file(APPEND ${static_reg_file} "namespace ${opset} {\n")
47+
endif()
48+
if(NOT op_name STREQUAL CMAKE_MATCH_3)
49+
set(op_name ${CMAKE_MATCH_3})
50+
if(NOT op_name STREQUAL "register_multiple_translators")
51+
file(APPEND ${static_reg_file} "ONNX_DECL_OP(${CMAKE_MATCH_3});\n")
52+
else()
53+
file(APPEND ${static_reg_file} "extern bool ${CMAKE_MATCH_3}(void);\n")
54+
endif()
55+
endif()
56+
endforeach()
57+
if(NOT opset STREQUAL "")
58+
file(APPEND ${static_reg_file} "} // namespace ${opset}\n")
59+
endif()
60+
if(NOT domain STREQUAL "")
61+
file(APPEND ${static_reg_file} "} // namespace ${domain}\n")
62+
endif()
63+
file(APPEND ${static_reg_file} "\nvoid static_lib_registration(void) {\n")
64+
foreach(reg ${registrations})
65+
string(REPLACE "ONNX_OP(" "ONNX_OP_M(" reg ${reg})
66+
file(APPEND ${static_reg_file} " ${reg};\n")
67+
endforeach()
68+
file(APPEND ${static_reg_file} "}\n")
69+
file(APPEND ${static_reg_file} "} // namespace onnx\n} // namespace frontend\n} // namespace ov\n#undef ONNX_DECL_OP\n")
70+
endif()
71+
572
ov_add_frontend(NAME onnx
673
LINKABLE_FRONTEND
774
PROTOBUF_REQUIRED
@@ -13,6 +80,10 @@ ov_add_frontend(NAME onnx
1380
set(ONNX_OPSET_VERSION 20 CACHE INTERNAL "Supported version of ONNX operator set")
1481
target_compile_definitions(${TARGET_NAME} PRIVATE ONNX_OPSET_VERSION=${ONNX_OPSET_VERSION})
1582

83+
if(BUILD_SHARED_LIBS)
84+
target_compile_definitions(${TARGET_NAME} PRIVATE ONNX_BUILD_SHARED=1)
85+
endif()
86+
1687
ov_ncc_naming_style(FOR_TARGET ${TARGET_NAME}
1788
SOURCE_DIRECTORIES "${${TARGET_NAME}_INCLUDE_DIR}"
1889
DEFINITIONS

src/frontends/onnx/frontend/src/core/operator_set.hpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,44 @@
55
#pragma once
66

77
#include <functional>
8+
#include <map>
89
#include <string>
910
#include <unordered_map>
1011

1112
#include "core/node.hpp"
13+
#include "version_range.hpp"
1214

1315
namespace ov {
1416
namespace frontend {
1517
namespace onnx {
16-
/// \brief Function which transforms single ONNX operator to OV sub-graph.
1718

19+
/// \brief Function which transforms single ONNX operator to OV sub-graph.
1820
using Operator = std::function<OutputVector(const Node&)>;
1921

2022
/// \brief Map which contains ONNX operators accessible by std::string value as a key.
2123
using OperatorSet = std::unordered_map<std::string, Operator>;
2224

25+
/// \brief Map with map of versioned operators, accessible like map["Operation"][Version]
26+
using DomainOpset = std::unordered_map<std::string, std::map<std::int64_t, Operator>>;
27+
28+
extern const char* OPENVINO_ONNX_DOMAIN;
29+
extern const char* MICROSOFT_DOMAIN;
30+
extern const char* PYTORCH_ATEN_DOMAIN;
31+
extern const char* MMDEPLOY_DOMAIN;
32+
33+
/// \brief Registering a versions range of translator in global map of translators (preferred to use)
34+
extern bool register_translator(const std::string name,
35+
const VersionRange range,
36+
const Operator fn,
37+
const std::string domain = "");
38+
39+
#define OPSET_RANGE(_in, _until) \
40+
VersionRange { _in, _until }
41+
#define OPSET_SINCE(_since) VersionRange::since(_since)
42+
#define OPSET_IN(_in) VersionRange::in(_in)
43+
#define ONNX_OP_M(name, range, ...) register_translator(name, range, __VA_ARGS__)
44+
#define ONNX_OP(name, range, ...) static bool onnx_op_reg = ONNX_OP_M(name, range, __VA_ARGS__)
45+
2346
} // namespace onnx
2447
} // namespace frontend
2548
} // namespace ov

src/frontends/onnx/frontend/src/op/abs.hpp src/frontends/onnx/frontend/src/op/abs.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5-
#pragma once
5+
#include "openvino/op/abs.hpp"
66

7-
#include "core/node.hpp"
7+
#include "core/operator_set.hpp"
88
#include "exceptions.hpp"
9-
#include "openvino/op/abs.hpp"
109

1110
namespace ov {
1211
namespace frontend {
1312
namespace onnx {
14-
namespace op {
15-
namespace set_1 {
16-
inline ov::OutputVector abs(const ov::frontend::onnx::Node& node) {
13+
namespace ai_onnx {
14+
namespace opset_1 {
15+
ov::OutputVector abs(const ov::frontend::onnx::Node& node) {
1716
CHECK_VALID_NODE(node,
1817
!node.has_attribute("consumed_inputs"),
1918
"consumed_inputs legacy attribute of Abs op is not supported");
2019
return {std::make_shared<ov::op::v0::Abs>(node.get_ov_inputs().at(0))};
2120
}
22-
} // namespace set_1
21+
ONNX_OP("Abs", OPSET_RANGE(1, 5), ai_onnx::opset_1::abs);
22+
} // namespace opset_1
2323

24-
namespace set_6 {
25-
using set_1::abs;
26-
} // namespace set_6
24+
namespace opset_6 {
25+
ONNX_OP("Abs", OPSET_RANGE(6, 12), ai_onnx::opset_1::abs);
26+
} // namespace opset_6
2727

28-
namespace set_13 {
29-
using set_6::abs;
30-
} // namespace set_13
31-
} // namespace op
28+
namespace opset_13 {
29+
ONNX_OP("Abs", OPSET_SINCE(13), ai_onnx::opset_1::abs);
30+
} // namespace opset_13
31+
} // namespace ai_onnx
3232
} // namespace onnx
3333
} // namespace frontend
3434
} // namespace ov

src/frontends/onnx/frontend/src/op/acos.hpp src/frontends/onnx/frontend/src/op/acos.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5-
#pragma once
6-
7-
#include "core/node.hpp"
85
#include "openvino/op/acos.hpp"
96

7+
#include "core/operator_set.hpp"
8+
109
namespace ov {
1110
namespace frontend {
1211
namespace onnx {
13-
namespace op {
14-
namespace set_7 {
15-
inline ov::OutputVector acos(const ov::frontend::onnx::Node& node) {
12+
namespace ai_onnx {
13+
namespace opset_7 {
14+
ov::OutputVector acos(const ov::frontend::onnx::Node& node) {
1615
return {std::make_shared<ov::op::v0::Acos>(node.get_ov_inputs().at(0))};
1716
}
18-
} // namespace set_7
19-
} // namespace op
17+
ONNX_OP("Acos", OPSET_SINCE(1), ai_onnx::opset_7::acos);
18+
} // namespace opset_7
19+
} // namespace ai_onnx
2020
} // namespace onnx
2121
} // namespace frontend
2222
} // namespace ov

src/frontends/onnx/frontend/src/op/acosh.hpp src/frontends/onnx/frontend/src/op/acosh.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5-
#pragma once
6-
7-
#include "core/node.hpp"
85
#include "openvino/op/acosh.hpp"
96

7+
#include "core/operator_set.hpp"
8+
109
namespace ov {
1110
namespace frontend {
1211
namespace onnx {
13-
namespace op {
14-
namespace set_9 {
15-
inline ov::OutputVector acosh(const ov::frontend::onnx::Node& node) {
12+
namespace ai_onnx {
13+
namespace opset_9 {
14+
ov::OutputVector acosh(const ov::frontend::onnx::Node& node) {
1615
return {std::make_shared<ov::op::v3::Acosh>(node.get_ov_inputs().at(0))};
1716
}
18-
} // namespace set_9
19-
} // namespace op
17+
ONNX_OP("Acosh", OPSET_SINCE(1), ai_onnx::opset_9::acosh);
18+
} // namespace opset_9
19+
} // namespace ai_onnx
2020
} // namespace onnx
2121
} // namespace frontend
2222
} // namespace ov

src/frontends/onnx/frontend/src/op/adaptive_avg_pooling2d.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5-
#include "op/adaptive_avg_pooling2d.hpp"
6-
5+
#include "core/operator_set.hpp"
76
#include "exceptions.hpp"
87
#include "openvino/op/adaptive_avg_pool.hpp"
98

@@ -12,8 +11,8 @@ using namespace ov::op;
1211
namespace ov {
1312
namespace frontend {
1413
namespace onnx {
15-
namespace op {
16-
namespace set_1 {
14+
namespace ai_onnx {
15+
namespace opset_1 {
1716
ov::OutputVector adaptive_avg_pooling2d(const ov::frontend::onnx::Node& node) {
1817
const auto inputs = node.get_ov_inputs();
1918
const auto num_inputs = inputs.size();
@@ -22,9 +21,9 @@ ov::OutputVector adaptive_avg_pooling2d(const ov::frontend::onnx::Node& node) {
2221

2322
return {std::make_shared<v8::AdaptiveAvgPool>(inputs[0], inputs[1])};
2423
}
25-
26-
} // namespace set_1
27-
} // namespace op
24+
ONNX_OP("adaptive_avg_pool2d", OPSET_SINCE(1), ai_onnx::opset_1::adaptive_avg_pooling2d, PYTORCH_ATEN_DOMAIN);
25+
} // namespace opset_1
26+
} // namespace ai_onnx
2827
} // namespace onnx
2928
} // namespace frontend
3029
} // namespace ov

src/frontends/onnx/frontend/src/op/adaptive_avg_pooling2d.hpp

-18
This file was deleted.

src/frontends/onnx/frontend/src/op/add.cpp

+22-10
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,51 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5-
#include "op/add.hpp"
5+
#include "openvino/op/add.hpp"
66

7+
#include "core/operator_set.hpp"
78
#include "exceptions.hpp"
8-
#include "openvino/op/add.hpp"
99
#include "utils/common.hpp"
1010

1111
using namespace ov::op;
1212

1313
namespace ov {
1414
namespace frontend {
1515
namespace onnx {
16-
namespace op {
17-
namespace set_1 {
16+
namespace ai_onnx {
17+
namespace opset_1 {
1818
ov::OutputVector add(const ov::frontend::onnx::Node& node) {
1919
CHECK_VALID_NODE(node,
2020
!node.has_attribute("consumed_inputs"),
2121
"consumed_inputs legacy attribute of Add op is not supported");
2222
return common::handle_opset6_binary_op<v1::Add>(node);
2323
}
24-
} // namespace set_1
24+
ONNX_OP("Add", OPSET_RANGE(1, 5), ai_onnx::opset_1::add);
25+
} // namespace opset_1
2526

26-
namespace set_6 {
27+
namespace opset_6 {
2728
ov::OutputVector add(const ov::frontend::onnx::Node& node) {
2829
return common::handle_opset6_binary_op<v1::Add>(node);
2930
}
30-
} // namespace set_6
31+
ONNX_OP("Add", OPSET_IN(6), ai_onnx::opset_6::add);
32+
} // namespace opset_6
3133

32-
namespace set_7 {
34+
namespace opset_7 {
3335
ov::OutputVector add(const ov::frontend::onnx::Node& node) {
3436
return {std::make_shared<v1::Add>(node.get_ov_inputs().at(0), node.get_ov_inputs().at(1))};
3537
}
36-
} // namespace set_7
37-
} // namespace op
38+
ONNX_OP("Add", OPSET_RANGE(7, 12), ai_onnx::opset_7::add);
39+
} // namespace opset_7
40+
41+
namespace opset_13 {
42+
ONNX_OP("Add", OPSET_IN(13), ai_onnx::opset_7::add);
43+
} // namespace opset_13
44+
45+
namespace opset_14 {
46+
ONNX_OP("Add", OPSET_SINCE(14), ai_onnx::opset_7::add);
47+
} // namespace opset_14
48+
49+
} // namespace ai_onnx
3850
} // namespace onnx
3951
} // namespace frontend
4052
} // namespace ov

src/frontends/onnx/frontend/src/op/add.hpp

-38
This file was deleted.

0 commit comments

Comments
 (0)