reverse.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include "ngraph/op/op.hpp"
8 
9 namespace ngraph
10 {
11  namespace op
12  {
13  namespace v1
14  {
15  class NGRAPH_API Reverse : public Op
16  {
17  public:
18  enum class Mode
19  {
20  INDEX,
21  MASK
22  };
23 
24  static constexpr NodeTypeInfo type_info{"Reverse", 1};
25  const NodeTypeInfo& get_type_info() const override { return type_info; }
26  Reverse() = default;
27  /// \brief Constructs a reverse operation.
28  ///
29  /// \param data The input tensor, some of whose axes are to be reversed.
30  /// \param reversed_axes The axes to reverse in a form of a set of indices or
31  /// boolean mask.
32  /// \param mode The way reversed_axes should be interpreted - a set or a mask.
33  Reverse(const Output<Node>& data,
34  const Output<Node>& reversed_axes,
35  const std::string& mode);
36 
37  Reverse(const Output<Node>& data,
38  const Output<Node>& reversed_axes,
39  const Mode mode);
40 
41  bool visit_attributes(AttributeVisitor& visitor) override;
42  void validate_and_infer_types() override;
43 
44  virtual std::shared_ptr<Node>
45  clone_with_new_inputs(const OutputVector& new_args) const override;
46 
47  /// \return The second input data interpretation mode.
48  Mode get_mode() const { return m_mode; }
49  void set_mode(const Mode mode) { m_mode = mode; }
50  virtual size_t get_version() const override { return 1; }
51  bool evaluate(const HostTensorVector& outputs,
52  const HostTensorVector& inputs) const override;
53  bool has_evaluate() const override;
54 
55  protected:
56  Mode mode_from_string(const std::string& mode) const;
57 
58  /// \brief Indicates how the values from the second input should be interpreted.
59  ///
60  /// The second input can contain a set of indices pointing to axes in the data
61  /// tensor shape.
62  /// Alternatively it can contain a boolean mask that indicates which axes should be
63  /// reversed.
64  Mode m_mode;
65 
66  private:
67  bool evaluate_reverse(const HostTensorVector& outputs,
68  const HostTensorVector& inputs) const;
69  };
70  } // namespace v1
71  } // namespace op
72 
73  NGRAPH_API
74  std::ostream& operator<<(std::ostream& s, const op::v1::Reverse::Mode& type);
75 
76  template <>
77  class NGRAPH_API AttributeAdapter<op::v1::Reverse::Mode>
78  : public EnumAttributeAdapterBase<op::v1::Reverse::Mode>
79  {
80  public:
81  AttributeAdapter(op::v1::Reverse::Mode& value)
83  {
84  }
85 
86  static constexpr DiscreteTypeInfo type_info{"AttributeAdapter<op::v1::Reverse::Mode>", 1};
87  const DiscreteTypeInfo& get_type_info() const override { return type_info; }
88  };
89 } // 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
Access an enum via a string.
Definition: attribute_adapter.hpp:168
A handle for one of a node's outputs.
Definition: node_output.hpp:33
Root of all actual ops.
Definition: op.hpp:17
Definition: reverse.hpp:16
const NodeTypeInfo & get_type_info() const override
Definition: reverse.hpp:25
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:48
virtual size_t get_version() const override
Definition: reverse.hpp:50
Mode m_mode
Indicates how the values from the second input should be interpreted.
Definition: reverse.hpp:64
bool has_evaluate() const override
Allows to get information about availability of evaluate method for the current operation.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Definition: type.hpp:27