ops_bridge.hpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include <cstdint>
20 #include <map>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "ngraph/except.hpp"
25 #include "onnx_import/core/operator_set.hpp"
26 
27 namespace ngraph
28 {
29  namespace onnx_import
30  {
31  namespace error
32  {
33  struct UnknownOperator : ngraph_error
34  {
35  UnknownOperator(const std::string& name, const std::string& domain)
36  : ngraph_error{(domain.empty() ? "" : domain + ".") + name}
37  {
38  }
39  };
40 
41  struct UnknownDomain : ngraph_error
42  {
43  explicit UnknownDomain(const std::string& domain)
44  : ngraph_error{domain}
45  {
46  }
47  };
48 
49  struct UnsupportedVersion : ngraph_error
50  {
51  UnsupportedVersion(const std::string& name,
52  std::int64_t version,
53  const std::string& domain)
54  : ngraph_error{"Unsupported operator version: " +
55  (domain.empty() ? "" : domain + ".") + name + ":" +
56  std::to_string(version)}
57  {
58  }
59  };
60 
61  } // namespace error
62 
64  {
65  public:
66  static constexpr const int LATEST_SUPPORTED_ONNX_OPSET_VERSION = ONNX_OPSET_VERSION;
67 
68  OperatorsBridge(const OperatorsBridge&) = delete;
69  OperatorsBridge& operator=(const OperatorsBridge&) = delete;
70  OperatorsBridge(OperatorsBridge&&) = delete;
71  OperatorsBridge& operator=(OperatorsBridge&&) = delete;
72 
73  static OperatorSet get_operator_set(const std::string& domain,
74  std::int64_t version = -1)
75  {
76  return instance()._get_operator_set(domain, version);
77  }
78 
79  static void register_operator(const std::string& name,
80  std::int64_t version,
81  const std::string& domain,
82  Operator fn)
83  {
84  instance()._register_operator(name, version, domain, std::move(fn));
85  }
86 
87  static bool is_operator_registered(const std::string& name,
88  std::int64_t version,
89  const std::string& domain)
90  {
91  return instance()._is_operator_registered(name, version, domain);
92  }
93 
94  private:
95  // Registered operators structure
96  // {
97  // domain_1: {
98  // op_type_1: {
99  // version_1: {func_handle},
100  // version_2: {func_handle},
101  // ...
102  // },
103  // op_type_2: { ... }
104  // ...
105  // },
106  // domain_2: { ... },
107  // ...
108  // }
109  std::unordered_map<std::string,
110  std::unordered_map<std::string, std::map<std::int64_t, Operator>>>
111  m_map;
112 
113  OperatorsBridge();
114 
115  static OperatorsBridge& instance()
116  {
117  static OperatorsBridge instance;
118  return instance;
119  }
120 
121  void _register_operator(const std::string& name,
122  std::int64_t version,
123  const std::string& domain,
124  Operator fn);
125  OperatorSet _get_operator_set(const std::string& domain, std::int64_t version);
126 
127  bool _is_operator_registered(const std::string& name,
128  std::int64_t version,
129  const std::string& domain);
130  };
131 
132  const std::string OPENVINO_ONNX_DOMAIN = "org.openvinotoolkit";
133 
134  } // namespace onnx_import
135 
136 } // namespace ngraph
ngraph::onnx_import::error::UnknownDomain
Definition: ops_bridge.hpp:42
ngraph::onnx_import::error::UnsupportedVersion
Definition: ops_bridge.hpp:50
ngraph::onnx_import::Operator
std::function< OutputVector(const Node &)> Operator
Function which transforms single ONNX operator to nGraph sub-graph.
Definition: operator_set.hpp:31
ngraph::onnx_import::OperatorsBridge
Definition: ops_bridge.hpp:64
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::onnx_import::error::UnknownOperator
Definition: ops_bridge.hpp:34
ngraph::onnx_import::OperatorSet
std::unordered_map< std::string, std::reference_wrapper< const Operator > > OperatorSet
Map which contains ONNX operators accessible by std::string value as a key.
Definition: operator_set.hpp:34