argon_fusion.cpp 2.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*******************************************************************************
* Copyright 2018 Intel Corporation
*
* 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 18 19 20 21 22 23

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <memory>

#include "gtest/gtest.h"
Yixing Lao's avatar
Yixing Lao committed
24 25

#include "ngraph/file_util.hpp"
26 27 28
#include "ngraph/graph_util.hpp"
#include "ngraph/log.hpp"
#include "ngraph/ngraph.hpp"
29
#include "ngraph/ops/relu.hpp"
30 31 32 33 34 35 36 37 38
#include "ngraph/ops/sum.hpp"
#include "ngraph/pass/graph_rewrite.hpp"
#include "ngraph/pass/manager.hpp"
#include "ngraph/pattern/matcher.hpp"
#include "ngraph/pattern/op/any.hpp"
#include "ngraph/pattern/op/label.hpp"
#include "ngraph/runtime/argon/pass/argon_fusion.hpp"
#include "ngraph/serializer.hpp"
#include "ngraph/util.hpp"
39
#include "nlohmann/json.hpp"
40 41 42 43 44 45 46 47
#include "util/matcher.hpp"
#include "util/test_tools.hpp"

using namespace ngraph;
using namespace std;

TEST(Argon_fusion, fuse_max_with_constant_zero_input_as_relu)
{
48
    Shape shape_a{1, 5};
49 50 51
    auto A = op::Constant::create(element::f32, shape_a, {0, 0, 0, 0, 0});
    auto B = make_shared<op::Parameter>(element::f32, shape_a);
    auto max = make_shared<op::Maximum>(A, B);
52
    Shape shape_rt{1, 5};
53
    auto f = make_shared<Function>(max, op::ParameterVector{B});
54 55 56 57 58 59 60 61 62 63 64 65 66 67

    auto manager = runtime::Manager::get("ARGON");
    auto external = manager->compile(f);
    auto backend = manager->allocate_backend();
    auto cf = backend->make_call_frame(external);

    auto b = backend->make_primary_tensor_view(element::f32, shape_a);
    copy_data(b, vector<float>{1, 8, -8, 17, -0.5});
    auto result = backend->make_primary_tensor_view(element::f32, shape_rt);
    vector<float> expected{1, 8, 0, 17, 0};

    cf->call({b}, {result});
    EXPECT_EQ(read_vector<float>(result), expected);
}