attribute_adapter.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 <type_traits>
9 #include <vector>
10 
11 #include "ngraph/enum_names.hpp"
12 #include "ngraph/type.hpp"
13 
14 ///
15 namespace ngraph
16 {
17  class AttributeVisitor;
18 
19  /// \brief Provides access to an attribute of type AT as a value accessor type VAT
20  template <typename VAT>
21  class ValueAccessor;
22 
23  /// \brief ValueAccessor<void> provides an accessor for values that do not have get/set methonds
24  /// via AttributeVistor.on_adapter.
25  ///
26  /// All ValueAccessors must be derived from ValueAccessor<void> so that an AttributeVisitor
27  /// only needs to implement a subset of the on_adapter methods.
28  template <>
29  class NGRAPH_API ValueAccessor<void>
30  {
31  public:
32  /// \brief type info enables identification of the value accessor, as well as is_type and
33  /// as_type.
34  virtual const DiscreteTypeInfo& get_type_info() const = 0;
35  virtual ~ValueAccessor() {}
36  };
37 
38  /// \brief Provides access to values via get/set methods from an m_value, typically from
39  /// ValueReference
40  ///
41  /// The m_buffer holds a VAT, which may be wider than the attribute AT. For example, serializers
42  /// that only
43  /// support int64_t integers would use a ValueAccessor<vector<int64_t>> to reference a
44  /// vector<int8_t> attribute. Destruction moves the value back to the attribute if it was
45  /// changed.
46  /// \tparam VAT The adapter value type; may be wider than the value being accessed.
47  template <typename VAT>
48  class ValueAccessor : public ValueAccessor<void>
49  {
50  public:
51  /// Returns the value
52  virtual const VAT& get() = 0;
53  /// Sets the value
54  virtual void set(const VAT& value) = 0;
55  };
56 
57  template <>
58  class ValueAccessor<void*> : public ValueAccessor<void>
59  {
60  public:
61  virtual void* get_ptr() = 0;
62  virtual size_t size() = 0;
63  };
64 
65  template <typename AT>
67  {
68  public:
69  DirectValueAccessor(AT& ref)
70  : m_ref(ref)
71  {
72  }
73  const AT& get() override { return m_ref; }
74  void set(const AT& value) override { m_ref = value; }
75 
76  protected:
77  AT& m_ref;
78  };
79 
80  template <typename AT, typename VAT>
82  {
83  public:
85  : m_ref(ref)
86  , m_buffer()
87  {
88  }
89 
90  const VAT& get() override
91  {
92  if (!m_buffer_valid)
93  {
94  m_buffer = static_cast<VAT>(m_ref);
95  m_buffer_valid = true;
96  }
97  return m_buffer;
98  }
99 
100  void set(const VAT& value) override
101  {
102  m_ref = static_cast<AT>(value);
103  m_buffer_valid = false;
104  }
105 
106  protected:
107  AT& m_ref;
108  VAT m_buffer;
109  bool m_buffer_valid{false};
110  };
111 
112  template <typename A, typename B>
113  A copy_from(B& b)
114  {
115  A result(b.size());
116  for (size_t i = 0; i < b.size(); ++i)
117  {
118  result[i] =
119  static_cast<typename std::remove_reference<decltype(result[i])>::type>(b[i]);
120  }
121  return result;
122  }
123 
124  template <typename AT, typename VAT>
126  {
127  public:
129  : m_ref(ref)
130  {
131  }
132 
133  const VAT& get() override
134  {
135  if (!m_buffer_valid)
136  {
137  m_buffer = copy_from<typename std::remove_cv<VAT>::type>(m_ref);
138  m_buffer_valid = true;
139  }
140  return m_buffer;
141  }
142 
143  void set(const VAT& value) override
144  {
145  m_ref = copy_from<AT>(value);
146  m_buffer_valid = false;
147  }
148 
149  operator AT&() { return m_ref; }
150 
151  protected:
152  AT& m_ref;
153  VAT m_buffer;
154  bool m_buffer_valid{false};
155  };
156 
157  /// \brief An AttributeAdapter "captures" an attribute as an AT& and makes it available as a
158  /// ValueAccessor<VAT>.
159  template <typename AT>
161  {
162  };
163 
164  /// \brief Access an enum via a string
165  /// \tparam AT The attribute type enum class
166  template <typename AT>
167  class EnumAttributeAdapterBase : public ValueAccessor<std::string>
168  {
169  public:
170  EnumAttributeAdapterBase(AT& value)
171  : m_ref(value)
172  {
173  }
174 
175  const std::string& get() override { return as_string(m_ref); }
176  void set(const std::string& value) override { m_ref = as_enum<AT>(value); }
177  operator AT&() { return m_ref; }
178 
179  protected:
180  AT& m_ref;
181  };
182 
183  /// Adapters will see visitor
184  class VisitorAdapter : public ValueAccessor<void>
185  {
186  public:
187  virtual bool visit_attributes(AttributeVisitor& visitor) = 0;
188  };
189 
190  template <>
191  class NGRAPH_API AttributeAdapter<float> : public IndirectScalarValueAccessor<float, double>
192  {
193  public:
194  AttributeAdapter(float& value)
196  {
197  }
198 
199  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<float>", 0};
200  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
201  };
202 
203  /// \brief Access a double as a double
204  template <>
205  class NGRAPH_API AttributeAdapter<double> : public DirectValueAccessor<double>
206  {
207  public:
208  AttributeAdapter(double& value)
210  {
211  }
212 
213  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<double>", 0};
214  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
215  };
216 
217  /// \brief Access a string as a string
218  template <>
219  class NGRAPH_API AttributeAdapter<std::string> : public DirectValueAccessor<std::string>
220  {
221  public:
222  AttributeAdapter(std::string& value)
224  {
225  }
226 
227  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<string>", 0};
228  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
229  };
230 
231  /// \brief Access a bool as a bool
232  template <>
233  class NGRAPH_API AttributeAdapter<bool> : public DirectValueAccessor<bool>
234  {
235  public:
236  AttributeAdapter(bool& value)
238  {
239  }
240 
241  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<bool>", 0};
242  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
243  };
244 
245  /// \brief Access an int8_t and an int64_t
246  template <>
247  class NGRAPH_API AttributeAdapter<int8_t> : public IndirectScalarValueAccessor<int8_t, int64_t>
248  {
249  public:
250  AttributeAdapter(int8_t& value)
252  {
253  }
254 
255  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<int8_t>", 0};
256  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
257  };
258 
259  /// \brief Access an int16_t as an int64_t
260  template <>
261  class NGRAPH_API AttributeAdapter<int16_t>
262  : public IndirectScalarValueAccessor<int16_t, int64_t>
263  {
264  public:
265  AttributeAdapter(int16_t& value)
267  {
268  }
269 
270  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<int16_t>", 0};
271  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
272  };
273 
274  /// \brief Access an int32_t as an int64_t
275  template <>
276  class NGRAPH_API AttributeAdapter<int32_t>
277  : public IndirectScalarValueAccessor<int32_t, int64_t>
278  {
279  public:
280  AttributeAdapter(int32_t& value)
282  {
283  }
284 
285  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<int32_t>", 0};
286  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
287  };
288 
289  /// \brief Access an int64_t as an int64_t
290  template <>
291  class NGRAPH_API AttributeAdapter<int64_t> : public DirectValueAccessor<int64_t>
292  {
293  public:
294  AttributeAdapter(int64_t& value)
296  {
297  }
298 
299  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<int64_t>", 0};
300  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
301  };
302 
303  /// \brief Access a uint8_t as an int64_t
304  template <>
305  class NGRAPH_API AttributeAdapter<uint8_t>
306  : public IndirectScalarValueAccessor<uint8_t, int64_t>
307  {
308  public:
309  AttributeAdapter(uint8_t& value)
311  {
312  }
313 
314  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<uint8_t>", 0};
315  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
316  };
317 
318  /// \brief Access a uint16_t as an int64_t
319  template <>
320  class NGRAPH_API AttributeAdapter<uint16_t>
321  : public IndirectScalarValueAccessor<uint16_t, int64_t>
322  {
323  public:
324  AttributeAdapter(uint16_t& value)
326  {
327  }
328 
329  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<uint16_t>", 0};
330  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
331  };
332 
333  /// \brief Access a uint32_t as an int64_t
334  template <>
335  class NGRAPH_API AttributeAdapter<uint32_t>
336  : public IndirectScalarValueAccessor<uint32_t, int64_t>
337  {
338  public:
339  AttributeAdapter(uint32_t& value)
341  {
342  }
343 
344  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<uint32_t>", 0};
345  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
346  };
347 
348  /// \brief Access a uint64_t as an int64_t
349  template <>
350  class NGRAPH_API AttributeAdapter<uint64_t>
351  : public IndirectScalarValueAccessor<uint64_t, int64_t>
352  {
353  public:
354  AttributeAdapter(uint64_t& value)
356  {
357  }
358 
359  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<uint64_t>", 0};
360  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
361  };
362 
363 #ifdef __APPLE__
364  // size_t is one of the uint types on _WIN32
365  template <>
366  class NGRAPH_API AttributeAdapter<size_t> : public IndirectScalarValueAccessor<size_t, int64_t>
367  {
368  public:
369  AttributeAdapter(size_t& value)
370  : IndirectScalarValueAccessor<size_t, int64_t>(value)
371  {
372  }
373 
374  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<size_t>", 0};
375  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
376  };
377 
378  template <>
379  class NGRAPH_API AttributeAdapter<std::vector<size_t>>
380  : public IndirectVectorValueAccessor<std::vector<size_t>, std::vector<int64_t>>
381  {
382  public:
383  AttributeAdapter(std::vector<size_t>& value)
384  : IndirectVectorValueAccessor<std::vector<size_t>, std::vector<int64_t>>(value)
385  {
386  }
387 
388  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<size_t>>", 0};
389  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
390  };
391 #endif
392 
393  /// Note: These class bodies cannot be defined with templates because of interactions
394  /// between dllexport and templates on Windows.
395 
396  /// \brief Access a vector<int8_t>
397  template <>
398  class NGRAPH_API AttributeAdapter<std::vector<int8_t>>
399  : public DirectValueAccessor<std::vector<int8_t>>
400  {
401  public:
402  AttributeAdapter(std::vector<int8_t>& value)
404  {
405  }
406 
407  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<int8_t>>", 0};
408  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
409  };
410 
411  /// \brief Access a vector<int16_t>
412  template <>
413  class NGRAPH_API AttributeAdapter<std::vector<int16_t>>
414  : public DirectValueAccessor<std::vector<int16_t>>
415  {
416  public:
417  AttributeAdapter(std::vector<int16_t>& value)
419  {
420  }
421 
422  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<int16_t>>", 0};
423  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
424  };
425 
426  /// \brief Access a vector<int32_t>
427  template <>
428  class NGRAPH_API AttributeAdapter<std::vector<int32_t>>
429  : public DirectValueAccessor<std::vector<int32_t>>
430  {
431  public:
432  AttributeAdapter(std::vector<int32_t>& value)
434  {
435  }
436 
437  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<int32_t>>", 0};
438  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
439  };
440 
441  /// \brief Access a vector<int64_t>
442  template <>
443  class NGRAPH_API AttributeAdapter<std::vector<int64_t>>
444  : public DirectValueAccessor<std::vector<int64_t>>
445  {
446  public:
447  AttributeAdapter(std::vector<int64_t>& value)
449  {
450  }
451 
452  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<int64_t>>", 0};
453  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
454  };
455 
456  /// \brief Access a vector<uint8_t>
457  template <>
458  class NGRAPH_API AttributeAdapter<std::vector<uint8_t>>
459  : public DirectValueAccessor<std::vector<uint8_t>>
460  {
461  public:
462  AttributeAdapter(std::vector<uint8_t>& value)
464  {
465  }
466 
467  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<uint8_t>>", 0};
468  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
469  };
470 
471  /// \brief Access a vector<uint16_t>
472  template <>
473  class NGRAPH_API AttributeAdapter<std::vector<uint16_t>>
474  : public DirectValueAccessor<std::vector<uint16_t>>
475  {
476  public:
477  AttributeAdapter(std::vector<uint16_t>& value)
479  {
480  }
481 
482  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<uint16_t>>", 0};
483  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
484  };
485 
486  /// \brief Access a vector<uint32_t>
487  template <>
488  class NGRAPH_API AttributeAdapter<std::vector<uint32_t>>
489  : public DirectValueAccessor<std::vector<uint32_t>>
490  {
491  public:
492  AttributeAdapter(std::vector<uint32_t>& value)
494  {
495  }
496 
497  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<uint32_t>>", 0};
498  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
499  };
500 
501  /// \brief Access a vector<uint64_t>
502  template <>
503  class NGRAPH_API AttributeAdapter<std::vector<uint64_t>>
504  : public DirectValueAccessor<std::vector<uint64_t>>
505  {
506  public:
507  AttributeAdapter(std::vector<uint64_t>& value)
509  {
510  }
511 
512  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<uint64_t>>", 0};
513  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
514  };
515 
516  /// \brief Access a vector<float>
517  template <>
518  class NGRAPH_API AttributeAdapter<std::vector<float>>
519  : public DirectValueAccessor<std::vector<float>>
520  {
521  public:
522  AttributeAdapter(std::vector<float>& value)
524  {
525  }
526 
527  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<float>>", 0};
528  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
529  };
530 
531  /// \brief Access a vector<double>
532  template <>
533  class NGRAPH_API AttributeAdapter<std::vector<double>>
534  : public DirectValueAccessor<std::vector<double>>
535  {
536  public:
537  AttributeAdapter(std::vector<double>& value)
539  {
540  }
541 
542  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<double>>", 0};
543  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
544  };
545 
546  /// \brief Access a vector<string>
547  template <>
548  class NGRAPH_API AttributeAdapter<std::vector<std::string>>
549  : public DirectValueAccessor<std::vector<std::string>>
550  {
551  public:
552  AttributeAdapter(std::vector<std::string>& value)
554  {
555  }
556 
557  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<vector<string>>", 0};
558  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
559  };
560 } // 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
Definition: attribute_adapter.hpp:67
void set(const AT &value) override
Sets the value.
Definition: attribute_adapter.hpp:74
const AT & get() override
Returns the value.
Definition: attribute_adapter.hpp:73
Access an enum via a string.
Definition: attribute_adapter.hpp:168
const std::string & get() override
Returns the value.
Definition: attribute_adapter.hpp:175
void set(const std::string &value) override
Sets the value.
Definition: attribute_adapter.hpp:176
Definition: attribute_adapter.hpp:82
void set(const VAT &value) override
Sets the value.
Definition: attribute_adapter.hpp:100
const VAT & get() override
Returns the value.
Definition: attribute_adapter.hpp:90
Definition: attribute_adapter.hpp:126
const VAT & get() override
Returns the value.
Definition: attribute_adapter.hpp:133
void set(const VAT &value) override
Sets the value.
Definition: attribute_adapter.hpp:143
ValueAccessor<void> provides an accessor for values that do not have get/set methonds via AttributeVi...
Definition: attribute_adapter.hpp:30
virtual const DiscreteTypeInfo & get_type_info() const =0
type info enables identification of the value accessor, as well as is_type and as_type.
Provides access to an attribute of type AT as a value accessor type VAT.
Definition: attribute_adapter.hpp:49
virtual void set(const VAT &value)=0
Sets the value.
virtual const VAT & get()=0
Returns the value.
Adapters will see visitor.
Definition: attribute_adapter.hpp:185
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
const std::string & as_string(Value value)
Returns the string matching the enum value.
Definition: enum_names.hpp:80
Definition: type.hpp:27