matcher.hpp 2.28 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
16

17
// this is for more nuanced testing
18 19 20 21 22 23 24
class TestMatcher : public ngraph::pattern::Matcher
{
    using ngraph::pattern::Matcher::Matcher;
    bool virtual match_node(const std::shared_ptr<ngraph::Node>& pattern_node,
                            const std::shared_ptr<ngraph::Node>& graph_node,
                            PatternMap& pattern_map) override
    {
25
        if (ngraph::as_type_ptr<::ngraph::op::Parameter>(pattern_node))
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        {
            bool result =
                pattern_node.get() == dynamic_cast<::ngraph::op::Parameter*>(graph_node.get());
            if (result)
            {
                m_matched_list.push_back(graph_node);
            }
            return result;
        }

        return this->ngraph::pattern::Matcher::match_node(pattern_node, graph_node, pattern_map);
    }

public:
    bool match(const std::shared_ptr<ngraph::Node>& pattern_node,
               const std::shared_ptr<ngraph::Node>& graph_node)
    {
43 44
        NGRAPH_CHECK(pattern_node && graph_node); // the same condition throws an exception in the
                                                  // non-test version of `match`
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        NGRAPH_DEBUG << "Starting match pattern = " << pattern_node->get_name()
                     << " , graph_node = " << graph_node->get_name();

        m_pattern_map.clear();
        m_match_root.reset();
        m_matched_list.clear();

        bool is_match = match_node(pattern_node, graph_node, m_pattern_map);
        if (is_match)
        {
            m_match_root = graph_node;
        }
        return is_match;
    }
};