attr_types.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 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 <ostream>
21 
22 #include "ngraph/attribute_adapter.hpp"
23 #include "ngraph/ngraph_visibility.hpp"
24 #include "ngraph/type.hpp"
25 
26 namespace ngraph
27 {
28  namespace op
29  {
30  /// \brief Modes for the `Pad` operator.
31  enum class PadMode
32  {
33  CONSTANT = 0,
34  EDGE,
35  REFLECT,
36  SYMMETRIC
37  };
38 
39  NGRAPH_API
40  std::ostream& operator<<(std::ostream& s, const PadMode& type);
41  }
42 
43  template <>
44  class NGRAPH_API AttributeAdapter<op::PadMode> : public EnumAttributeAdapterBase<op::PadMode>
45  {
46  public:
49  {
50  }
51 
52  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::PadMode>", 0};
53  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
54  };
55 
56  namespace op
57  {
58  /// \brief Padding Type used for `Convolution` and `Pooling`
59  ///
60  /// Follows ONNX padding type definitions
61  /// EXPLICIT - Pad dimensions are explicity specified
62  /// SAME_LOWER - Pad dimensions computed to match input shape
63  /// Ceil(num_dims/2) at the beginning and
64  /// Floor(num_dims/2) at the end
65  /// SAME_UPPER - Pad dimensions computed to match input shape
66  /// Floor(num_dims/2) at the beginning and
67  /// Ceil(num_dims/2) at the end
68  /// VALID - No padding
69  /// AUTO - Deprecated. User should not use it in the future
70  /// NOTSET - Deprecated. User should not use it in the future
71 
72  enum class PadType
73  {
74  EXPLICIT = 0,
75  SAME_LOWER,
76  SAME_UPPER,
77  VALID,
78  AUTO = SAME_UPPER,
79  NOTSET = EXPLICIT,
80  };
81 
82  NGRAPH_API
83  std::ostream& operator<<(std::ostream& s, const PadType& type);
84  }
85 
86  template <>
87  class NGRAPH_API AttributeAdapter<op::PadType> : public EnumAttributeAdapterBase<op::PadType>
88  {
89  public:
92  {
93  }
94 
95  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::PadType>", 0};
96  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
97  };
98 
99  namespace op
100  {
101  /// \brief Rounding Type used for `Pooling` operators.
102  enum class RoundingType
103  {
104  FLOOR = 0,
105  CEIL = 1,
106  };
107 
108  NGRAPH_API
109  std::ostream& operator<<(std::ostream& s, const RoundingType& type);
110  }
111 
112  template <>
113  class NGRAPH_API AttributeAdapter<op::RoundingType>
114  : public EnumAttributeAdapterBase<op::RoundingType>
115  {
116  public:
119  {
120  }
121 
122  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::RoundingType>", 0};
123  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
124  };
125 
126  namespace op
127  {
128  /// \brief Specifies the algorithm to use for implicit broadcasting of a tensor
129  /// to align with another tensor
130  ///
131  /// NONE - No implicit broadcasting of tensor
132  /// NUMPY - Numpy-style implicit broadcasting
133  /// (https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)
134  /// Right-align dimensions of the two tensors, with missing dimensions
135  /// treated as size 1 dimensions. After alignment, for each dimension,
136  /// their sizes should either match or one of them should be of size 1.
137  /// Size 1 dimension will be implicitly broadcast to match the other
138  /// size.
139  ///
140  /// E.g.,
141  /// A: Shape(2, 1, 6)
142  /// B: Shape( 3, 1)
143  /// Result: Shape(2, 3, 6)
144  ///
145  /// A: Shape(2, 1, 6)
146  /// B: Shape( 3, 1)
147  /// Result: Shape(2, 3, 6)
148  /// PDPD - PaddlePaddle-style implicit broadcasting
149  /// (https://github.com/PaddlePaddle/Paddle/blob/release/1.5/paddle/
150  /// fluid/operators/elementwise/elementwise_op.h#L126)
151  /// Broadcast B to match the shape of A, where axis is the start
152  /// dimension index to align B with A. If axis is -1 (default), i
153  /// axis = rank(A) - rank(B). The trailing dimensions of size 1 for B
154  /// will be ignored.
155  ///
156  /// E.g.,
157  /// A: Shape(2, 3, 4, 5)
158  /// B: Shape( 3, 4 ) with axis =1
159  /// Result: Shape(2, 3, 4, 5)
160  ///
161  /// A: Shape(2, 3, 4, 5)
162  /// B: Shape( 3, 1 ) with axis = 1
163  /// Result: Shape(2, 3, 4, 5)
164  ///
165  /// TODO: Add more implicit broadcast modes used by frameworks
166  enum class AutoBroadcastType
167  {
168  NONE = 0,
169  EXPLICIT = NONE,
170  NUMPY,
171  PDPD,
172  };
173 
174  NGRAPH_API
175  std::ostream& operator<<(std::ostream& s, const AutoBroadcastType& type);
176  }
177  namespace op
178  {
179  /// \brief BroadcastType specifies rules used for mapping of input tensor axes to output
180  /// shape axes.
181  ///
182  /// \note Broadcasting rules are different for Broadcast op and for element-wise ops.
183  /// AutoBroadcastType::NUMPY is equivalent of BroadcastType::BIDIRECTIONAL
184  /// according to spec.
185  ///
186  /// EXPLICIT - Mapping of the input data shape to output shape
187  /// based on axes_mapping input.
188  /// NUMPY - Numpy broadcasting rules, aligned with ONNX Broadcasting.
189  /// (https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md)
190  /// PDPD - PaddlePaddle-style implicit broadcasting.
191  /// For more informaction see AutoBroadcastType documentation.
192  /// BIDIRECTIONAL - The broadcast rule is similar to
193  /// numpy.array(input) * numpy.ones(target_shape).
194  /// Dimensions are right alignment.
195  enum class BroadcastType
196  {
197  NONE,
198  EXPLICIT = NONE,
199  NUMPY,
200  PDPD,
201  BIDIRECTIONAL
202  };
203 
204  NGRAPH_API
205  std::ostream& operator<<(std::ostream& s, const BroadcastType& type);
206  }
207 
208  template <>
209  class NGRAPH_API AttributeAdapter<op::AutoBroadcastType>
210  : public EnumAttributeAdapterBase<op::AutoBroadcastType>
211  {
212  public:
215  {
216  }
217 
218  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::AutoBroadcastType>", 0};
219  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
220  };
221 
222  template <>
223  class NGRAPH_API AttributeAdapter<op::BroadcastType>
224  : public EnumAttributeAdapterBase<op::BroadcastType>
225  {
226  public:
229  {
230  }
231 
232  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::BroadcastType>", 0};
233  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
234  };
235 
236  namespace op
237  {
238  /// \brief Specifies how eps is combined with L2 value
239  enum class EpsMode
240  {
241  // Add bias to norm
242  ADD,
243  // Calculate max of norm and bias
244  MAX
245  };
246 
247  NGRAPH_API
248  std::ostream& operator<<(std::ostream& s, const EpsMode& type);
249  }
250 
251  template <>
252  class NGRAPH_API AttributeAdapter<op::EpsMode> : public EnumAttributeAdapterBase<op::EpsMode>
253  {
254  public:
257  {
258  }
259 
260  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::EpsMode>", 0};
261  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
262  };
263 
264  namespace op
265  {
266  enum class TopKSortType
267  {
268  // Returned values are not sorte
269  NONE,
270  // Sort result based on element indices
271  SORT_INDICES,
272  // Sort result based on element values
273  SORT_VALUES,
274  };
275 
276  NGRAPH_API
277  std::ostream& operator<<(std::ostream& s, const TopKSortType& type);
278  }
279 
280  template <>
281  class NGRAPH_API AttributeAdapter<op::TopKSortType>
282  : public EnumAttributeAdapterBase<op::TopKSortType>
283  {
284  public:
285  AttributeAdapter(op::TopKSortType& value)
287  {
288  }
289 
290  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::TopKSortType>", 0};
291  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
292  };
293 
294  namespace op
295  {
296  enum class TopKMode
297  {
298  MAX,
299  MIN,
300  };
301 
302  NGRAPH_API
303  std::ostream& operator<<(std::ostream& s, const TopKMode& type);
304  }
305 
306  template <>
307  class NGRAPH_API AttributeAdapter<op::TopKMode> : public EnumAttributeAdapterBase<op::TopKMode>
308  {
309  public:
310  AttributeAdapter(op::TopKMode& value)
312  {
313  }
314 
315  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::TopKMode>", 1};
316  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
317  };
318 
319  namespace op
320  {
321  /// \brief Implicit broadcast specification
322  struct NGRAPH_API AutoBroadcastSpec
323  {
325  : m_type(AutoBroadcastType::NONE)
326  , m_axis(0)
327  {
328  }
330  : m_type(type)
331  , m_axis(0)
332  {
333  }
334  AutoBroadcastSpec(const char* type)
335  : AutoBroadcastSpec(type_from_string(type))
336  {
337  }
338  AutoBroadcastSpec(AutoBroadcastType type, int64_t axis)
339  : m_type(type)
340  , m_axis(axis)
341  {
342  }
343 
344  AutoBroadcastType m_type; // Implicit broadcasting algorithm
345  int64_t m_axis; // Axis to start alignment on
346 
347  bool operator==(const AutoBroadcastSpec& a) const
348  {
349  return a.m_type == m_type && a.m_axis == m_axis;
350  }
351  static const AutoBroadcastSpec NUMPY;
352  static const AutoBroadcastSpec NONE;
353 
354  private:
355  AutoBroadcastType type_from_string(const std::string& type) const;
356  };
357  }
358 
359  template <>
360  class AttributeAdapter<op::AutoBroadcastSpec> : public VisitorAdapter
361  {
362  public:
364  : m_ref(value)
365  {
366  }
367  bool visit_attributes(AttributeVisitor& visitor) override;
368 
369  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::AutoBroadcastSpec>", 0};
370  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
371  protected:
372  op::AutoBroadcastSpec& m_ref;
373  };
374 
375  namespace op
376  {
377  /// \brief Implicit broadcast specification
378  struct NGRAPH_API BroadcastModeSpec
379  {
381  : m_type(BroadcastType::NUMPY)
382  , m_axis(0)
383  {
384  }
386  : m_type(type)
387  , m_axis(0)
388  {
389  }
390  BroadcastModeSpec(const char* type)
391  : BroadcastModeSpec(as_enum<BroadcastType>(type))
392  {
393  }
394  BroadcastModeSpec(BroadcastType type, int64_t axis)
395  : m_type(type)
396  , m_axis(axis)
397  {
398  }
399 
400  BroadcastType m_type; // Implicit broadcasting algorithm
401  int64_t m_axis; // Axis to start alignment on
402 
403  bool operator==(const BroadcastModeSpec& a) const
404  {
405  return a.m_type == m_type && a.m_axis == m_axis;
406  }
407  };
408  }
409 
410  template <>
411  class AttributeAdapter<op::BroadcastModeSpec> : public VisitorAdapter
412  {
413  public:
415  : m_ref(value)
416  {
417  }
418  bool visit_attributes(AttributeVisitor& visitor) override;
419 
420  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::BroadcastModeSpec>", 0};
421  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
422  protected:
423  op::BroadcastModeSpec& m_ref;
424  };
425 
426  namespace op
427  {
428  ///
429  /// \brief This class defines possible recurrent sequence directions.
430  ///
432  {
433  FORWARD,
434  REVERSE,
435  BIDIRECTIONAL
436  };
437 
438  NGRAPH_API
439  std::ostream& operator<<(std::ostream& s, const RecurrentSequenceDirection& direction);
440  }
441 
442  template <>
443  class NGRAPH_API AttributeAdapter<op::RecurrentSequenceDirection>
444  : public EnumAttributeAdapterBase<op::RecurrentSequenceDirection>
445  {
446  public:
449  {
450  }
451 
452  static constexpr DiscreteTypeInfo type_info{
453  "AttributeAdapter<op::RecurrentSequenceDirection>", 1};
454  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
455  };
456 }
const DiscreteTypeInfo & get_type_info() const override
type info enables identification of the value accessor, as well as is_type and as_type.
Definition: attr_types.hpp:370
const DiscreteTypeInfo & get_type_info() const override
type info enables identification of the value accessor, as well as is_type and as_type.
Definition: attr_types.hpp:421
An AttributeAdapter "captures" an attribute as an AT& and makes it available as a ValueAccessor<VAT>.
Definition: attribute_adapter.hpp:171
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:71
Access an enum via a string.
Definition: attribute_adapter.hpp:178
Adapters will see visitor.
Definition: attribute_adapter.hpp:194
RoundingType
Rounding Type used for Pooling operators.
Definition: attr_types.hpp:103
PadType
Padding Type used for Convolution and Pooling
Definition: attr_types.hpp:73
RecurrentSequenceDirection
This class defines possible recurrent sequence directions.
Definition: attr_types.hpp:432
EpsMode
Specifies how eps is combined with L2 value.
Definition: attr_types.hpp:240
AutoBroadcastType
Specifies the algorithm to use for implicit broadcasting of a tensor to align with another tensor.
Definition: attr_types.hpp:167
PadMode
Modes for the Pad operator.
Definition: attr_types.hpp:32
BroadcastType
BroadcastType specifies rules used for mapping of input tensor axes to output shape axes.
Definition: attr_types.hpp:196
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39
Implicit broadcast specification.
Definition: attr_types.hpp:323
Implicit broadcast specification.
Definition: attr_types.hpp:379