shape_of.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 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 constant_fold(OutputVector& output_values,
59  const OutputVector& input_values) override;
60 
61  private:
62  bool m_is_foldable = true;
63  element::Type m_output_type;
64  };
65  }
66 
67  namespace v0
68  {
69  /// \brief Operation that returns the shape of its input argument as a tensor.
70  class NGRAPH_API ShapeOf : public Op
71  {
72  public:
73  NGRAPH_RTTI_DECLARATION;
74  ShapeOf() = default;
75  /// \brief Constructs a shape-of operation.
76  ShapeOf(const Output<Node>& arg);
77 
78  bool visit_attributes(AttributeVisitor& visitor) override;
79  virtual std::shared_ptr<Node>
80  clone_with_new_inputs(const OutputVector& new_args) const override;
81 
82  void validate_and_infer_types() override;
83 
84  // FOR CONSTANT FOLDING INTERNAL USAGE ONLY
85  // Constant folding for cases with static rank but dynamic shape create a subgraph
86  // which contains a Shape of.
87  // In this case we need to prevent constant folding from endless creation of these
88  // subgraphs.
89  // These metods should be removed if better solution will be designed.
90  void set_is_foldable(bool is_foldable) { m_is_foldable = is_foldable; }
91  bool get_is_foldable() const { return m_is_foldable; }
92  bool evaluate(const HostTensorVector& output_values,
93  const HostTensorVector& input_values) const override;
94  bool constant_fold(OutputVector& output_values,
95  const OutputVector& input_values) override;
96 
97  private:
98  bool m_is_foldable = true;
99  };
100  }
101  using v0::ShapeOf;
102  }
103 }
ngraph::op::v3::ShapeOf::get_type_info
const NodeTypeInfo & get_type_info() const override
Definition: shape_of.hpp:32
ngraph::op::v0::ShapeOf::ShapeOf
ShapeOf(const Output< Node > &arg)
Constructs a shape-of operation.
ngraph::element::Type
Definition: element_type.hpp:61
ngraph::op::v3::ShapeOf::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
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::AttributeVisitor
Visits the attributes of a node, primarily for serialization-like tasks.
Definition: attribute_visitor.hpp:70
ngraph::op::v0::ShapeOf
Operation that returns the shape of its input argument as a tensor.
Definition: shape_of.hpp:71
ngraph::op::v3::ShapeOf
Operation that returns the shape of its input argument as a tensor.
Definition: shape_of.hpp:29
ngraph::op::v0::ShapeOf::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::v3::ShapeOf::ShapeOf
ShapeOf(const Output< Node > &arg, const element::Type output_type=element::i64)
Constructs a shape-of operation.
ngraph::op::Op
Root of all actual ops.
Definition: op.hpp:29