node.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 <cstddef>
20 #include <string>
21 
22 #include "ngraph/except.hpp"
23 #include "ngraph/node.hpp"
24 #include "onnx_import/utils/onnx_importer_visibility.hpp"
25 
26 namespace ONNX_NAMESPACE
27 {
28  // forward declaration
29  class NodeProto;
30 }
31 
32 namespace ngraph
33 {
34  namespace onnx_import
35  {
36  namespace error
37  {
38  namespace node
39  {
40  struct UnknownAttribute : ngraph_error
41  {
42  explicit UnknownAttribute(const std::string& node, const std::string& name)
43  : ngraph_error{"Node (" + node + "): unknown attribute \'" + name + "\'"}
44  {
45  }
46  };
47 
48  } // namespace node
49 
50  } // namespace error
51 
52  // forward declaration
53  class Graph;
54 
55  class ONNX_IMPORTER_API Node
56  {
57  public:
58  Node() = delete;
59  Node(const ONNX_NAMESPACE::NodeProto& node_proto, const Graph& graph);
60 
61  Node(Node&&) noexcept;
62  Node(const Node&);
63 
64  Node& operator=(Node&&) noexcept = delete;
65  Node& operator=(const Node&) = delete;
66 
67  OutputVector get_ng_inputs() const;
68  OutputVector get_ng_nodes() const;
69  const std::string& domain() const;
70  const std::string& op_type() const;
71  const std::string& get_name() const;
72 
73  /// \brief Describe the ONNX Node to make debugging graphs easier
74  /// Function will return the Node's name if it has one, or the names of its outputs.
75  /// \return Description of Node
76  const std::string& get_description() const;
77 
78  const std::vector<std::reference_wrapper<const std::string>>& get_output_names() const;
79  const std::string& output(int index) const;
80  std::size_t get_outputs_size() const;
81 
82  bool has_attribute(const std::string& name) const;
83 
84  template <typename T>
85  T get_attribute_value(const std::string& name, T default_value) const;
86 
87  template <typename T>
88  T get_attribute_value(const std::string& name) const;
89 
90  private:
91  class Impl;
92  // In this case we need custom deleter, because Impl is an incomplete
93  // type. Node's are elements of std::vector. Without custom deleter
94  // compilation fails; the compiler is unable to parameterize an allocator's
95  // default deleter due to incomple type.
96  std::unique_ptr<Impl, void (*)(Impl*)> m_pimpl;
97  };
98 
99  inline std::ostream& operator<<(std::ostream& outs, const Node& node)
100  {
101  return (outs << "<Node(" << node.op_type() << "): " << node.get_description() << ">");
102  }
103 
104  } // namespace onnx_import
105 
106 } // namespace ngraph
ngraph::onnx_import::Node
Definition: node.hpp:56
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::onnx_import::error::node::UnknownAttribute
Definition: node.hpp:41
ngraph::onnx_import::Node::get_description
const std::string & get_description() const
Describe the ONNX Node to make debugging graphs easier Function will return the Node's name if it has...
ngraph::onnx_import::Graph
Definition: graph.hpp:36