attribute_visitor.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include <string>
8 #include <unordered_map>
9 #include <utility>
10 
11 #include "ngraph/partial_shape.hpp"
12 #include "ngraph/type.hpp"
13 #include "ngraph/type/element_type.hpp"
14 
15 namespace ngraph
16 {
17  template <typename T>
18  class ValueAccessor;
19  class VisitorAdapter;
20  class Node;
21  class Function;
22 
23  /// \brief Visits the attributes of a node, primarily for serialization-like tasks.
24  ///
25  /// Attributes are the node parameters that are always compile-time constants.
26  /// Values computed from the graph topology and attributes during compilation are not
27  /// attributes.
28  ///
29  /// Attributes have a wide variety of types, but serialization formats are more restricted.
30  /// We asume serialation easily supports scalar types of bool 64-bit signed, string, and double,
31  /// and has specialized ways to support numeric arrays and raw data+size. The visitor and
32  /// adapter convert between the limited serialization types and the unlimited attribute types.
33  ///
34  /// A visitor is passed to an op's visit_attributes method. The visit_attributes method calls
35  /// the template method visitor.on_attribute<AT>(const std::string& name, AT& value) on each
36  /// attribute. The visitor can read or write the attribute's value. The on_attribute
37  /// method creates an AttributeAdapter<AT> for the value and passes it to one of the visitors
38  /// on_adapter methods. The on_adapter methods expect a reference to a ValueAccessor<VAT> or a
39  /// VisitorAdapter. A ValueAccessor<VAT> has get/set methods that can be used to read/write the
40  /// attribute value as type VAT. These methods are triggered by deriving AttributeAdapter<AT>
41  /// from ValueAccessor<VAT>. For more complex cases, such as structs, the on_adapter method for
42  /// VisitorAdapter passes the name and visitor to the adapter, so that the adapter can perform
43  /// additional work such as visiting struct members or sequence values.
44  ///
45  /// When a node visits an attribute with structure, the node's on_attribute passes a name for
46  /// the entire attribute, but the struct will have its own methods to be visited. Similarly, a
47  /// vector will have a sequence of members to be visited. The adapter may use the visitor
48  /// methods start_struct/finish_struct and start_vector/next_vector/finish_vector to inidicate
49  /// nexted members.
50  ///
51  /// The visitor method get_name_with_context creates a generic nested version of the name.
52  /// Visitors can override according to their serialization requirements.
53  ///
54  /// Attributes that are shared_ptr<Node> are special. They must have been already been
55  /// registered with the visitor using register_node, which needs a shared pointer to a node and
56  /// a string ID. The ID string will be used to serialize the node or find the node during
57  /// deserialization.
58  class NGRAPH_API AttributeVisitor
59  {
60  public:
61  virtual ~AttributeVisitor() {}
62  // Must implement these methods
63  /// \brief handles all specialized on_adapter methods implemented by the visitor.
64  ///
65  /// The adapter implements get_type_info(), which can be used to determine the adapter
66  /// directly
67  /// or via is_type and as_type on any platform
68  virtual void on_adapter(const std::string& name, ValueAccessor<void>& adapter) = 0;
69  // The remaining adapter methods fall back on the void adapter if not implemented
70  virtual void on_adapter(const std::string& name, ValueAccessor<void*>& adapter);
71  virtual void on_adapter(const std::string& name, ValueAccessor<std::string>& adapter);
72  virtual void on_adapter(const std::string& name, ValueAccessor<bool>& adapter);
73  virtual void on_adapter(const std::string& name, ValueAccessor<int8_t>& adapter);
74  virtual void on_adapter(const std::string& name, ValueAccessor<int16_t>& adapter);
75  virtual void on_adapter(const std::string& name, ValueAccessor<int32_t>& adapter);
76  virtual void on_adapter(const std::string& name, ValueAccessor<int64_t>& adapter);
77  virtual void on_adapter(const std::string& name, ValueAccessor<uint8_t>& adapter);
78  virtual void on_adapter(const std::string& name, ValueAccessor<uint16_t>& adapter);
79  virtual void on_adapter(const std::string& name, ValueAccessor<uint32_t>& adapter);
80  virtual void on_adapter(const std::string& name, ValueAccessor<uint64_t>& adapter);
81  virtual void on_adapter(const std::string& name, ValueAccessor<float>& adapter);
82  virtual void on_adapter(const std::string& name, ValueAccessor<double>& adapter);
83  virtual void on_adapter(const std::string& name,
84  ValueAccessor<std::vector<int8_t>>& adapter);
85  virtual void on_adapter(const std::string& name,
86  ValueAccessor<std::vector<int16_t>>& adapter);
87  virtual void on_adapter(const std::string& name,
88  ValueAccessor<std::vector<int32_t>>& adapter);
89  virtual void on_adapter(const std::string& name,
90  ValueAccessor<std::vector<int64_t>>& adapter);
91  virtual void on_adapter(const std::string& name,
92  ValueAccessor<std::vector<uint8_t>>& adapter);
93  virtual void on_adapter(const std::string& name,
94  ValueAccessor<std::vector<uint16_t>>& adapter);
95  virtual void on_adapter(const std::string& name,
96  ValueAccessor<std::vector<uint32_t>>& adapter);
97  virtual void on_adapter(const std::string& name,
98  ValueAccessor<std::vector<uint64_t>>& adapter);
99  virtual void on_adapter(const std::string& name,
100  ValueAccessor<std::vector<float>>& adapter);
101  virtual void on_adapter(const std::string& name,
102  ValueAccessor<std::vector<double>>& adapter);
103  virtual void on_adapter(const std::string& name,
104  ValueAccessor<std::vector<std::string>>& adapter);
105  /// \brief Hook for adapters that need visitor access
106  virtual void on_adapter(const std::string& name, VisitorAdapter& adapter);
107 
108  /// \brief Provides API to handle nGraph Function attribute type, accessed as ValueAccessor
109  /// \param name attribute name
110  /// \param adapter reference to a Function ValueAccessor<VAT>
111  virtual void on_adapter(const std::string& name,
112  ValueAccessor<std::shared_ptr<Function>>& adapter);
113 
114  /// The generic visitor. There must be a definition of AttributeAdapter<T> that can convert
115  /// to a ValueAccessor<U> for one of the on_adpater methods.
116  template <typename AT>
117  void on_attribute(const std::string& name, AT& value)
118  {
119  AttributeAdapter<AT> adapter(value);
120  start_structure(name);
121  on_adapter(get_name_with_context(), adapter);
122  finish_structure();
123  }
124  /// \returns The nested context of visits
125  const std::vector<std::string>& get_context() const { return m_context; }
126  /// \returns context prepended to names
127  virtual std::string get_name_with_context();
128  /// \brief Start visiting a nested structure
129  virtual void start_structure(const std::string& name);
130  /// \brief Finish visiting a nested structure
131  virtual std::string finish_structure();
132  using node_id_t = std::string;
133  static const node_id_t invalid_node_id;
134  /// \brief Associate a node with an id.
135  ///
136  /// No node may be used as an attribute unless it has already been registered with an ID.
137  /// References to nodes are visited with a ValueAccessor of their ID.
138  virtual void register_node(const std::shared_ptr<Node>& node,
139  node_id_t id = invalid_node_id);
140  /// Returns the node with the given id, or nullptr if there is no registered node
141  virtual std::shared_ptr<Node> get_registered_node(node_id_t id);
142  /// Returns the id for the node, or -1 if the node is not registered
143  virtual node_id_t get_registered_node_id(const std::shared_ptr<Node>& node);
144 
145  protected:
146  std::vector<std::string> m_context;
147  std::unordered_map<std::shared_ptr<Node>, node_id_t> m_node_id_map;
148  std::unordered_map<node_id_t, std::shared_ptr<Node>> m_id_node_map;
149  };
150 } // namespace ngraph
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:161
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:59
virtual void on_adapter(const std::string &name, VisitorAdapter &adapter)
Hook for adapters that need visitor access.
virtual void start_structure(const std::string &name)
Start visiting a nested structure.
void on_attribute(const std::string &name, AT &value)
Definition: attribute_visitor.hpp:117
virtual void on_adapter(const std::string &name, ValueAccessor< std::shared_ptr< Function >> &adapter)
Provides API to handle nGraph Function attribute type, accessed as ValueAccessor.
virtual std::string get_name_with_context()
const std::vector< std::string > & get_context() const
Definition: attribute_visitor.hpp:125
virtual std::shared_ptr< Node > get_registered_node(node_id_t id)
Returns the node with the given id, or nullptr if there is no registered node.
virtual void on_adapter(const std::string &name, ValueAccessor< void > &adapter)=0
handles all specialized on_adapter methods implemented by the visitor.
virtual std::string finish_structure()
Finish visiting a nested structure.
virtual void register_node(const std::shared_ptr< Node > &node, node_id_t id=invalid_node_id)
Associate a node with an id.
virtual node_id_t get_registered_node_id(const std::shared_ptr< Node > &node)
Returns the id for the node, or -1 if the node is not registered.
ValueAccessor<void> provides an accessor for values that do not have get/set methonds via AttributeVi...
Definition: attribute_adapter.hpp:30
Definition: attribute_adapter.hpp:59
Adapters will see visitor.
Definition: attribute_adapter.hpp:185
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16