graph.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 <memory>
20 #include <onnx/onnx_pb.h>
21 #include <string>
22 #include <vector>
23 
24 #include "graph_cache.hpp"
25 #include "model.hpp"
26 #include "ngraph/op/parameter.hpp"
27 #include "onnx_import/default_opset.hpp"
28 #include "operator_set.hpp"
29 #include "value_info.hpp"
30 
31 namespace ngraph
32 {
33  namespace onnx_import
34  {
35  class Graph
36  {
37  public:
38  Graph(const ONNX_NAMESPACE::GraphProto& proto, Model& model);
39  const std::vector<Node>& get_nodes() const { return m_nodes; }
40  const std::vector<ValueInfo>& get_inputs() const { return m_inputs; }
41  const std::vector<ValueInfo>& get_outputs() const { return m_outputs; }
42  OutputVector get_ng_outputs() const;
43  const ParameterVector& get_ng_parameters() const { return m_parameters; }
44  bool is_node_in_cache(const std::string& name) const;
45  Output<ngraph::Node> get_ng_node_from_cache(const std::string& name) const;
46  const std::string& get_name() const { return m_graph_proto->name(); }
47  OutputVector make_ng_nodes(const Node& onnx_node) const;
48  const GraphCache& get_graph_cache() const;
49 
50  protected:
51  Graph(const ONNX_NAMESPACE::GraphProto& proto,
52  Model& model,
53  std::unique_ptr<GraphCache>&& cache);
54 
55  void set_friendly_names(const Node& onnx_node,
56  const OutputVector& ng_node_vector) const;
57 
58  void add_provenance_tag_to_initializer(
59  const Tensor& initializer, std::shared_ptr<default_opset::Constant> node) const;
60 
61  void add_provenance_tag_to_input(const ValueInfo& input,
62  std::shared_ptr<ngraph::Node> node) const;
63 
64  void add_provenance_tags(const Node& onnx_node,
65  const OutputVector& ng_node_vector) const;
66 
67  protected:
68  ParameterVector m_parameters;
69 
70  private:
71  const ONNX_NAMESPACE::GraphProto* m_graph_proto;
72  std::unique_ptr<GraphCache> m_cache;
73  std::vector<Node> m_nodes;
74  std::vector<ValueInfo> m_inputs;
75  std::vector<ValueInfo> m_outputs;
76  Model* m_model;
77  };
78 
79  /// \brief Representation of ONNX subgraph. It is used for example by ONNX Loop op.
80  /// It has access for initializers both from subgraph and from parent graph
81  /// cache.
82  class Subgraph : public Graph
83  {
84  public:
85  /// \brief Subgraph a GraphCache class object.
86  ///
87  /// \param[in] proto The ONNX protobuf graph representation.
88  /// \param[in] model The ONNX model object.
89  /// \param[in] parent_graph The reference to the parent graph.
90  Subgraph(const ONNX_NAMESPACE::GraphProto& proto,
91  Model& model,
92  const Graph& parent_graph);
93  };
94 
95  inline std::ostream& operator<<(std::ostream& outs, const Graph& graph)
96  {
97  return (outs << "<Graph: " << graph.get_name() << ">");
98  }
99 
100  } // namespace onnx_import
101 
102 } // namespace ngraph
ngraph::onnx_import::Tensor
Definition: tensor.hpp:383
ngraph::onnx_import::Subgraph::Subgraph
Subgraph(const ONNX_NAMESPACE::GraphProto &proto, Model &model, const Graph &parent_graph)
Subgraph a GraphCache class object.
ngraph::onnx_import::Node
Definition: node.hpp:56
ngraph::onnx_import::Model
Definition: model.hpp:33
ngraph::onnx_import::Subgraph
Representation of ONNX subgraph. It is used for example by ONNX Loop op. It has access for initialize...
Definition: graph.hpp:83
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::onnx_import::ValueInfo
Definition: value_info.hpp:49
ngraph::onnx_import::GraphCache
GraphCache stores and provides access to ONNX graph initializers.
Definition: graph_cache.hpp:30
ngraph::onnx_import::Graph
Definition: graph.hpp:36