label.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/node.hpp"
20 #include "ngraph/pattern/op/pattern.hpp"
21 
22 namespace ngraph
23 {
24  namespace pattern
25  {
26  namespace op
27  {
28  /// Fails if the predicate returns false on the graph value.
29  ///
30  /// The graph value is added to the matched values list. If the Label is already
31  /// associated with a value, the match succeeds if the value is the same as the graph
32  /// value. Otherwise, the label is associated with the graph value and the match
33  /// succeeds if the pattern input matches the graph value.
34  ///
35  /// DEPRECATED: If no inputs are given to Label, a True node is serves as the input. If
36  /// more than one inputs are given, an Or pattern of the inputs serves as the input.
37  class NGRAPH_API Label : public Pattern
38  {
39  public:
40  static constexpr NodeTypeInfo type_info{"patternLabel", 0};
41  const NodeTypeInfo& get_type_info() const override;
42  /// \brief creates a Label node containing a sub-pattern described by \sa type and
43  /// \sa shape.
44  ///
45  /// this Label node can be bound only to the nodes in the input graph
46  /// that match the pattern specified by \sa wrapped_nodes
47  /// Example:
48  /// \code{.cpp}
49  /// auto add = a + b; // a and b are op::Parameter in this example
50  /// auto label = std::make_shared<pattern::op::Label>(element::f32,
51  /// Shape{2,2},
52  /// nullptr,
53  /// OutputVector{add});
54  /// \endcode
55  Label(const element::Type& type,
56  const PartialShape& s,
57  const ValuePredicate pred,
58  const OutputVector& wrapped_values)
59  : Pattern(OutputVector{wrap_values(wrapped_values)}, pred)
60  {
61  set_output_type(0, type, s);
62  }
63 
64  explicit Label(const element::Type& type = element::dynamic,
65  const PartialShape& s = PartialShape::dynamic())
66  : Label(type, s, [](const Output<Node>&) { return true; }, OutputVector())
67  {
68  }
69 
70  Label(const element::Type& type, const PartialShape& s, ValuePredicate pred)
71  : Label(type, s, pred, OutputVector{})
72  {
73  }
74 
75  Label(const element::Type& type, const PartialShape& s, NodePredicate pred)
76  : Label(type, s, as_value_predicate(pred), OutputVector{})
77  {
78  }
79 
80  Label(const element::Type& type,
81  const PartialShape& s,
82  const NodePredicate pred,
83  const NodeVector& wrapped_values)
84  : Label(type, s, as_value_predicate(pred), as_output_vector(wrapped_values))
85  {
86  }
87 
88  /// \brief creates a Label node containing a sub-pattern described by the type and
89  /// shape of \sa node.
90  ///
91  /// this Label node can be bound only to the nodes in the input graph
92  /// that match the pattern specified by \sa wrapped_values
93  /// Example:
94  /// \code{.cpp}
95  /// auto add = a + b; // a and b are op::Parameter in this example
96  /// auto label = std::make_shared<pattern::op::Label>(add,
97  /// nullptr,
98  /// OutputVector{add});
99  /// \endcode
100  Label(const Output<Node>& value,
101  const ValuePredicate pred,
102  const OutputVector& wrapped_values)
103  : Label(
104  value.get_element_type(), value.get_partial_shape(), pred, wrapped_values)
105  {
106  }
107  Label(const Output<Node>& value, const ValuePredicate pred)
108  : Label(
109  value.get_element_type(), value.get_partial_shape(), pred, OutputVector{})
110  {
111  }
112 
113  Label(const Output<Node>& value, const NodePredicate pred)
114  : Label(value.get_element_type(),
115  value.get_partial_shape(),
116  as_value_predicate(pred),
117  OutputVector{})
118  {
119  }
120  Label(const Output<Node>& value)
121  : Label(value.get_element_type(),
122  value.get_partial_shape(),
123  [](const Output<Node>&) { return true; },
124  OutputVector{})
125  {
126  }
127  Label(const Output<Node>& node,
128  const NodePredicate pred,
129  const NodeVector& wrapped_values)
130  : Label(node.get_element_type(),
131  node.get_partial_shape(),
132  as_value_predicate(pred),
133  as_output_vector(wrapped_values))
134  {
135  }
136 
137  bool match_value(Matcher* matcher,
138  const Output<Node>& pattern_value,
139  const Output<Node>& graph_value) override;
140 
141  protected:
142  static Output<Node> wrap_values(const OutputVector& wrapped_values);
143  };
144  }
145 
146  NGRAPH_API
147  std::shared_ptr<Node> any_input();
148 
149  NGRAPH_API
150  std::shared_ptr<Node> any_input(const pattern::op::ValuePredicate& pred);
151  }
152 }
ngraph::pattern::op::Label::get_type_info
const NodeTypeInfo & get_type_info() const override
ngraph::pattern::op::Label
Definition: label.hpp:38
ngraph::element::Type
Definition: element_type.hpp:61
ngraph::pattern::op::Pattern
Definition: pattern.hpp:79
ngraph::pattern::op::Label::Label
Label(const element::Type &type, const PartialShape &s, const ValuePredicate pred, const OutputVector &wrapped_values)
creates a Label node containing a sub-pattern described by
Definition: label.hpp:55
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::pattern::op::Label::Label
Label(const Output< Node > &value, const ValuePredicate pred, const OutputVector &wrapped_values)
creates a Label node containing a sub-pattern described by the type and shape of
Definition: label.hpp:100
ngraph::PartialShape::dynamic
static PartialShape dynamic(Rank r=Rank::dynamic())
Construct a PartialShape with the given rank and all dimensions (if any) dynamic.