wrap_type.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  class NGRAPH_API WrapType : public Pattern
29  {
30  public:
31  static constexpr NodeTypeInfo type_info{"patternAnyType", 0};
32  const NodeTypeInfo& get_type_info() const override;
33 
34  explicit WrapType(NodeTypeInfo wrapped_type,
35  const ValuePredicate& pred =
36  [](const Output<Node>& output) { return true; },
37  const OutputVector& input_values = {})
38  : Pattern(input_values, pred)
39  , m_wrapped_type(wrapped_type)
40  {
41  set_output_type(0, element::Type_t::dynamic, PartialShape::dynamic());
42  }
43 
44  bool match_value(pattern::Matcher* matcher,
45  const Output<Node>& pattern_value,
46  const Output<Node>& graph_value) override;
47 
48  NodeTypeInfo get_wrapped_type() const { return m_wrapped_type; }
49  private:
50  NodeTypeInfo m_wrapped_type;
51  };
52  }
53 
54  template <class T>
55  std::shared_ptr<Node> wrap_type(const OutputVector& inputs,
56  const pattern::op::ValuePredicate& pred)
57  {
58  static_assert(std::is_base_of<Node, T>::value, "Unexpected template type");
59  return std::make_shared<op::WrapType>(T::type_info, pred, inputs);
60  }
61 
62  template <class T>
63  std::shared_ptr<Node> wrap_type(const OutputVector& inputs = {})
64  {
65  return wrap_type<T>(inputs, [](const Output<Node>& output) { return true; });
66  }
67 
68  template <class T>
69  std::shared_ptr<Node> wrap_type(const pattern::op::ValuePredicate& pred)
70  {
71  return wrap_type<T>({}, pred);
72  }
73  }
74 }
ngraph::pattern::op::Pattern
Definition: pattern.hpp:79
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::pattern::op::WrapType
Definition: wrap_type.hpp:29
ngraph::PartialShape::dynamic
static PartialShape dynamic(Rank r=Rank::dynamic())
Construct a PartialShape with the given rank and all dimensions (if any) dynamic.
ngraph::pattern::op::WrapType::get_type_info
const NodeTypeInfo & get_type_info() const override