reverse.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 "ngraph/op/op.hpp"
20 
21 namespace ngraph
22 {
23  namespace op
24  {
25  namespace v1
26  {
27  class NGRAPH_API Reverse : public Op
28  {
29  public:
30  enum class Mode
31  {
32  INDEX,
33  MASK
34  };
35 
36  static constexpr NodeTypeInfo type_info{"Reverse", 1};
37  const NodeTypeInfo& get_type_info() const override { return type_info; }
38  Reverse() = default;
39  /// \brief Constructs a reverse operation.
40  ///
41  /// \param data The input tensor, some of whose axes are to be reversed.
42  /// \param reversed_axes The axes to reverse in a form of a set of indices or
43  /// boolean mask.
44  /// \param mode The way reversed_axes should be interpreted - a set or a mask.
45  Reverse(const Output<Node>& data,
46  const Output<Node>& reversed_axes,
47  const std::string& mode);
48 
49  Reverse(const Output<Node>& data,
50  const Output<Node>& reversed_axes,
51  const Mode mode);
52 
53  bool visit_attributes(AttributeVisitor& visitor) override;
54  void validate_and_infer_types() override;
55 
56  virtual std::shared_ptr<Node>
57  clone_with_new_inputs(const OutputVector& new_args) const override;
58 
59  /// \return The second input data interpretation mode.
60  Mode get_mode() const { return m_mode; }
61  void set_mode(const Mode mode) { m_mode = mode; }
62  virtual size_t get_version() const override { return 1; }
63  bool evaluate(const HostTensorVector& outputs,
64  const HostTensorVector& inputs) const override;
65 
66  protected:
67  Mode mode_from_string(const std::string& mode) const;
68 
69  /// \brief Indicates how the values from the second input should be interpreted.
70  ///
71  /// The second input can contain a set of indices pointing to axes in the data
72  /// tensor shape.
73  /// Alternatively it can contain a boolean mask that indicates which axes should be
74  /// reversed.
75  Mode m_mode;
76 
77  private:
78  bool evaluate_reverse(const HostTensorVector& outputs,
79  const HostTensorVector& inputs) const;
80  };
81  }
82  }
83 
84  NGRAPH_API
85  std::ostream& operator<<(std::ostream& s, const op::v1::Reverse::Mode& type);
86 
87  template <>
88  class NGRAPH_API AttributeAdapter<op::v1::Reverse::Mode>
89  : public EnumAttributeAdapterBase<op::v1::Reverse::Mode>
90  {
91  public:
92  AttributeAdapter(op::v1::Reverse::Mode& value)
94  {
95  }
96 
97  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::v1::Reverse::Mode>", 1};
98  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
99  };
100 }
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
A handle for one of a node's outputs.
Definition: node_output.hpp:42
Root of all actual ops.
Definition: op.hpp:29
Definition: reverse.hpp:28
const NodeTypeInfo & get_type_info() const override
Definition: reverse.hpp:37
Reverse(const Output< Node > &data, const Output< Node > &reversed_axes, const std::string &mode)
Constructs a reverse operation.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
bool evaluate(const HostTensorVector &outputs, const HostTensorVector &inputs) const override
Evaluates the op on input_values putting results in output_values.
Mode get_mode() const
Definition: reverse.hpp:60
virtual size_t get_version() const override
Definition: reverse.hpp:62
Mode m_mode
Indicates how the values from the second input should be interpreted.
Definition: reverse.hpp:75
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: type.hpp:39