reverse.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 "ngraph/op/op.hpp"
20 
21 namespace ngraph
22 {
23  namespace op
24  {
25  namespace v0
26  {
27  // clang-format off
28  /// \brief Axis-reverse operation.
29  ///
30  /// Reverses the direction of zero or more axes in a tensor, where "reversing" an axis means
31  /// that at the output tensor.
32  ///
33  /// ## Parameters
34  ///
35  /// | | Description |
36  /// | --------------- | ------------------------ |
37  /// | `reversed_axes` | The axes to be reversed. |
38  ///
39  /// ## Inputs
40  ///
41  /// | | Type | Description |
42  /// | ----- | --------------------------------- | -------------------------------------- |
43  /// | `arg` | \f$E[d_1,\dots,d_n]~(n \geq 0)\f$ | An input tensor of any type and shape. |
44  ///
45  /// ## Output
46  ///
47  /// | Type | Description |
48  /// | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
49  /// | \f$E[d_1,\dots,d_n]\f$ | The tensor \f$T\f$, where \f$T[i_1,\dots,i_n] = \texttt{arg}[j_1,\dots,j_n]\f$ and \f$j_k = d_k - i_k - 1\f$ if axis \f$k\f$ is in the reverse set; else \f$j_k = i_k\f$. |
50  // clang-format on
51  class NGRAPH_DEPRECATED(
52  "This operation is deprecated and will be removed soon. "
53  "Use v1::Reverse instead of it.") NGRAPH_API Reverse : public Op
54  {
55  NGRAPH_SUPPRESS_DEPRECATED_START
56  public:
57  static constexpr NodeTypeInfo type_info{"Reverse", 0};
58  const NodeTypeInfo& get_type_info() const override { return type_info; }
59  Reverse() = default;
60  /// \brief Constructs a reverse operation.
61  ///
62  /// \param arg The input tensor, some of whose axes are to be reversed.
63  /// \param reversed_axes The axes to reverse.
64  Reverse(const Output<Node>& arg, const AxisSet& reversed_axes);
65 
66  void validate_and_infer_types() override;
67 
68  virtual std::shared_ptr<Node>
69  clone_with_new_inputs(const OutputVector& new_args) const override;
70 
71  /// \return The set of axes to reverse.
72  const AxisSet& get_reversed_axes() const { return m_reversed_axes; }
73  void set_reversed_axes(const AxisSet& reversed_axes)
74  {
75  m_reversed_axes = reversed_axes;
76  }
77  bool evaluate(const HostTensorVector& outputs,
78  const HostTensorVector& inputs) const override;
79 
80  protected:
81  AxisSet m_reversed_axes;
82  NGRAPH_SUPPRESS_DEPRECATED_END
83  };
84  }
85 
86  namespace v1
87  {
88  class NGRAPH_API Reverse : public Op
89  {
90  public:
91  enum class Mode
92  {
93  INDEX,
94  MASK
95  };
96 
97  static constexpr NodeTypeInfo type_info{"Reverse", 1};
98  const NodeTypeInfo& get_type_info() const override { return type_info; }
99  Reverse() = default;
100  /// \brief Constructs a reverse operation.
101  ///
102  /// \param data The input tensor, some of whose axes are to be reversed.
103  /// \param reversed_axes The axes to reverse in a form of a set of indices or
104  /// boolean mask.
105  /// \param mode The way reversed_axes should be interpreted - a set or a mask.
106  Reverse(const Output<Node>& data,
107  const Output<Node>& reversed_axes,
108  const std::string& mode);
109 
110  Reverse(const Output<Node>& data,
111  const Output<Node>& reversed_axes,
112  const Mode mode);
113 
114  bool visit_attributes(AttributeVisitor& visitor) override;
115  void validate_and_infer_types() override;
116 
117  virtual std::shared_ptr<Node>
118  clone_with_new_inputs(const OutputVector& new_args) const override;
119 
120  /// \return The second input data interpretation mode.
121  Mode get_mode() const { return m_mode; }
122  void set_mode(const Mode mode) { m_mode = mode; }
123  virtual size_t get_version() const override { return 1; }
124  bool evaluate(const HostTensorVector& outputs,
125  const HostTensorVector& inputs) const override;
126 
127  protected:
128  Mode mode_from_string(const std::string& mode) const;
129 
130  /// \brief Indicates how the values from the second input should be interpreted.
131  ///
132  /// The second input can contain a set of indices pointing to axes in the data
133  /// tensor shape.
134  /// Alternatively it can contain a boolean mask that indicates which axes should be
135  /// reversed.
136  Mode m_mode;
137  };
138  }
139  // default opset version
140  NGRAPH_SUPPRESS_DEPRECATED_START
141  using v0::Reverse;
142  NGRAPH_SUPPRESS_DEPRECATED_END
143  }
144 
145  NGRAPH_API
146  std::ostream& operator<<(std::ostream& s, const op::v1::Reverse::Mode& type);
147 
148  template <>
149  class NGRAPH_API AttributeAdapter<op::v1::Reverse::Mode>
150  : public EnumAttributeAdapterBase<op::v1::Reverse::Mode>
151  {
152  public:
153  AttributeAdapter(op::v1::Reverse::Mode& value)
154  : EnumAttributeAdapterBase<op::v1::Reverse::Mode>(value)
155  {
156  }
157 
158  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::v1::Reverse::Mode>", 1};
159  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
160  };
161 }
ngraph::op::v1::Reverse
Definition: reverse.hpp:89
ngraph::op::v1::Reverse::get_version
virtual size_t get_version() const override
Definition: reverse.hpp:123
ngraph::op::v1::Reverse::validate_and_infer_types
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ngraph::op::v1::Reverse::get_type_info
const NodeTypeInfo & get_type_info() const override
Definition: reverse.hpp:98
ngraph::DiscreteTypeInfo
Definition: type.hpp:39
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::op::v1::Reverse::Reverse
Reverse(const Output< Node > &data, const Output< Node > &reversed_axes, const std::string &mode)
Constructs a reverse operation.
ngraph::AttributeVisitor
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:70
ngraph::op::v1::Reverse::m_mode
Mode m_mode
Indicates how the values from the second input should be interpreted.
Definition: reverse.hpp:136
ngraph::op::v1::Reverse::get_mode
Mode get_mode() const
Definition: reverse.hpp:121
ngraph::op::Op
Root of all actual ops.
Definition: op.hpp:29