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