graph_cache.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 <map>
20 #include <memory>
21 #include <string>
22 
23 #include "ngraph/node.hpp"
24 namespace ngraph
25 {
26  namespace onnx_import
27  {
28  /// \brief GraphCache stores and provides access to ONNX graph initializers.
29  class GraphCache
30  {
31  public:
32  /// \brief Add node to the cache or override the existing one.
33  ///
34  /// \note GraphCahce takes ownership of the node.
35  ///
36  /// \param[in] name The name of node added to the cache.
37  /// \param[in] node The node added to the cache.
38  void emplace_node(const std::string& name, Output<ngraph::Node>&& node);
39 
40  /// \brief Get the node from the cache
41  ///
42  /// \note If the node is not found the ngraph_error exception is thrown.
43  ///
44  /// \param[in] name The name of the node.
45  ///
46  /// \return The node named `name`.
47  virtual Output<ngraph::Node> get_node(const std::string& name) const;
48 
49  /// \brief Return true if the node named `name` exist in the cache.
50  ///
51  /// \param[in] name The name of the node.
52  ///
53  /// \return true if the node named `name` exist in the cache, false otherwise.
54  virtual bool contains(const std::string& name) const;
55 
56  private:
57  std::map<std::string, Output<ngraph::Node>> m_graph_cache_map;
58  };
59 
60  class SubgraphCache : public GraphCache
61  {
62  public:
63  /// \brief Constructs a SubgraphCache class object.
64  ///
65  /// \param[in] parent_graph_cache The reference to the parent graph.
66  SubgraphCache(const GraphCache& parent_graph_cache);
67 
68  /// \brief Get the node from the cache (subgraph or parent graph)
69  ///
70  /// \note If the node is not found the ngraph_error exception is thrown.
71  ///
72  /// \param[in] name The name of the node.
73  ///
74  /// \return The node named `name` from subgraph (as present) or from parent graph.
75  Output<ngraph::Node> get_node(const std::string& name) const override;
76 
77  /// \brief Return true if the node named `name` exist in the cache.
78  ///
79  /// \param[in] name The name of the node.
80  ///
81  /// \return true if the node named `name` exist in the cache
82  /// (subgraph or parent graph), false otherwise.
83  bool contains(const std::string& name) const override;
84 
85  private:
86  const GraphCache* m_parent_graph_cache;
87  };
88 
89  } // namespace onnx_import
90 } // namespace ngraph
ngraph::onnx_import::GraphCache::contains
virtual bool contains(const std::string &name) const
Return true if the node named name exist in the cache.
ngraph::onnx_import::GraphCache::emplace_node
void emplace_node(const std::string &name, Output< ngraph::Node > &&node)
Add node to the cache or override the existing one.
ngraph::onnx_import::SubgraphCache::SubgraphCache
SubgraphCache(const GraphCache &parent_graph_cache)
Constructs a SubgraphCache class object.
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::onnx_import::SubgraphCache::contains
bool contains(const std::string &name) const override
Return true if the node named name exist in the cache.
ngraph::onnx_import::GraphCache::get_node
virtual Output< ngraph::Node > get_node(const std::string &name) const
Get the node from the cache.
ngraph::onnx_import::SubgraphCache::get_node
Output< ngraph::Node > get_node(const std::string &name) const override
Get the node from the cache (subgraph or parent graph)
ngraph::onnx_import::GraphCache
GraphCache stores and provides access to ONNX graph initializers.
Definition: graph_cache.hpp:30
ngraph::onnx_import::SubgraphCache
Definition: graph_cache.hpp:61