shape_of.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 v3
14  {
15  /// \brief Operation that returns the shape of its input argument as a tensor.
16  class NGRAPH_API ShapeOf : public Op
17  {
18  public:
19  static constexpr NodeTypeInfo type_info{"ShapeOf", 3};
20  const NodeTypeInfo& get_type_info() const override { return type_info; }
21  ShapeOf() = default;
22  /// \brief Constructs a shape-of operation.
23  ShapeOf(const Output<Node>& arg, const element::Type output_type = element::i64);
24 
25  bool visit_attributes(AttributeVisitor& visitor) override;
26  virtual std::shared_ptr<Node>
27  clone_with_new_inputs(const OutputVector& new_args) const override;
28 
29  void validate_and_infer_types() override;
30 
31  element::Type get_output_type() const { return m_output_type; }
32  void set_output_type(element::Type output_type) { m_output_type = output_type; }
33  // Overload collision with method on Node
34  using Node::set_output_type;
35 
36  // FOR CONSTANT FOLDING INTERNAL USAGE ONLY
37  // Constant folding for cases with static rank but dynamic shape create a subgraph
38  // which contains a Shape of.
39  // In this case we need to prevent constant folding from endless creation of these
40  // subgraphs.
41  // These metods should be removed if better solution will be designed.
42  void set_is_foldable(bool is_foldable) { m_is_foldable = is_foldable; }
43  bool get_is_foldable() const { return m_is_foldable; }
44  bool evaluate(const HostTensorVector& output_values,
45  const HostTensorVector& input_values) const override;
46  bool has_evaluate() const override;
47  bool evaluate_lower(const HostTensorVector& output_values) const override;
48  bool evaluate_upper(const HostTensorVector& output_values) const override;
49  bool constant_fold(OutputVector& output_values,
50  const OutputVector& input_values) override;
51 
52  private:
53  bool m_is_foldable = true;
54  element::Type m_output_type;
55  };
56  } // namespace v3
57 
58  namespace v0
59  {
60  /// \brief Operation that returns the shape of its input argument as a tensor.
61  class NGRAPH_API ShapeOf : public Op
62  {
63  public:
64  NGRAPH_RTTI_DECLARATION;
65  ShapeOf() = default;
66  /// \brief Constructs a shape-of operation.
67  ShapeOf(const Output<Node>& arg);
68 
69  bool visit_attributes(AttributeVisitor& visitor) override;
70  virtual std::shared_ptr<Node>
71  clone_with_new_inputs(const OutputVector& new_args) const override;
72 
73  void validate_and_infer_types() override;
74 
75  // FOR CONSTANT FOLDING INTERNAL USAGE ONLY
76  // Constant folding for cases with static rank but dynamic shape create a subgraph
77  // which contains a Shape of.
78  // In this case we need to prevent constant folding from endless creation of these
79  // subgraphs.
80  // These metods should be removed if better solution will be designed.
81  void set_is_foldable(bool is_foldable) { m_is_foldable = is_foldable; }
82  bool get_is_foldable() const { return m_is_foldable; }
83  bool evaluate(const HostTensorVector& output_values,
84  const HostTensorVector& input_values) const override;
85  bool has_evaluate() const override;
86  bool evaluate_lower(const HostTensorVector& output_values) const override;
87  bool evaluate_upper(const HostTensorVector& output_values) const override;
88  bool constant_fold(OutputVector& output_values,
89  const OutputVector& input_values) override;
90 
91  private:
92  bool m_is_foldable = true;
93  };
94  } // namespace v0
95  using v0::ShapeOf;
96  } // namespace op
97 } // namespace ngraph
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:59
A handle for one of a node's outputs.
Definition: node_output.hpp:33
Definition: element_type.hpp:51
Root of all actual ops.
Definition: op.hpp:17
Operation that returns the shape of its input argument as a tensor.
Definition: shape_of.hpp:62
ShapeOf(const Output< Node > &arg)
Constructs a shape-of operation.
bool has_evaluate() const override
Allows to get information about availability of evaluate method for the current operation.
bool evaluate(const HostTensorVector &output_values, const HostTensorVector &input_values) const override
Evaluates the op on input_values putting results in output_values.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
Operation that returns the shape of its input argument as a tensor.
Definition: shape_of.hpp:17
const NodeTypeInfo & get_type_info() const override
Definition: shape_of.hpp:20
bool has_evaluate() const override
Allows to get information about availability of evaluate method for the current operation.
void validate_and_infer_types() override
Verifies that attributes and inputs are consistent and computes output shapes and element types....
ShapeOf(const Output< Node > &arg, const element::Type output_type=element::i64)
Constructs a shape-of operation.
bool evaluate(const HostTensorVector &output_values, const HostTensorVector &input_values) const override
Evaluates the op on input_values putting results in output_values.
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16
Definition: type.hpp:27