model.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 <onnx/onnx_pb.h>
20 #include <ostream>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "operator_set.hpp"
25 
26 namespace ngraph
27 {
28  namespace onnx_import
29  {
30  std::string get_node_domain(const ONNX_NAMESPACE::NodeProto& node_proto);
31 
32  class Model
33  {
34  public:
35  Model() = delete;
36  explicit Model(const ONNX_NAMESPACE::ModelProto& model_proto);
37 
38  Model(const Model&) = default;
39  Model(Model&&) = default;
40 
41  Model& operator=(const Model&) = delete;
42  Model& operator=(Model&&) = delete;
43 
44  const std::string& get_producer_name() const { return m_model_proto->producer_name(); }
45  const ONNX_NAMESPACE::GraphProto& get_graph() const { return m_model_proto->graph(); }
46  std::int64_t get_model_version() const { return m_model_proto->model_version(); }
47  const std::string& get_producer_version() const
48  {
49  return m_model_proto->producer_version();
50  }
51 
52  /// \brief Access an operator object by its type name and domain name
53  /// The function will return the operator object if it exists, or report an error
54  /// in case of domain or operator absence.
55  /// \param name type name of the operator object,
56  /// \param domain domain name of the operator object.
57  /// \return Reference to the operator object.
58  /// \throw error::UnknownDomain there is no operator set defined for the given
59  /// domain,
60  /// \throw error::UnknownOperator the given operator type name does not exist in
61  /// operator set.
62  const Operator& get_operator(const std::string& name, const std::string& domain) const;
63 
64  /// \brief Check availability of operator base on NodeProto.
65  /// \return `true` if the operator is available, otherwise it returns `false`.
66  bool is_operator_available(const ONNX_NAMESPACE::NodeProto& node_proto) const;
67 
68  /// \brief Enable operators from provided domain to use by this model.
69  ///
70  /// \note This function makes visible all currently registered in provided domain
71  /// operators for use in this model.
72  ///
73  /// \param[in] domain The domain name.
74  ///
75  void enable_opset_domain(const std::string& domain);
76 
77  private:
78  const ONNX_NAMESPACE::ModelProto* m_model_proto;
79  std::unordered_map<std::string, OperatorSet> m_opset;
80  };
81 
82  inline std::ostream& operator<<(std::ostream& outs, const Model& model)
83  {
84  return (outs << "<Model: " << model.get_producer_name() << ">");
85  }
86 
87  } // namespace onnx_import
88 
89 } // namespace ngraph
ngraph::onnx_import::Model::enable_opset_domain
void enable_opset_domain(const std::string &domain)
Enable operators from provided domain to use by this model.
ngraph::onnx_import::Model::is_operator_available
bool is_operator_available(const ONNX_NAMESPACE::NodeProto &node_proto) const
Check availability of operator base on NodeProto.
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::Model
Definition: model.hpp:33
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::onnx_import::Model::get_operator
const Operator & get_operator(const std::string &name, const std::string &domain) const
Access an operator object by its type name and domain name The function will return the operator obje...