hybrid_backend.cpp 3.12 KB
Newer Older
Sandeep's avatar
Sandeep committed
1
//*****************************************************************************
2
// Copyright 2017-2019 Intel Corporation
Sandeep's avatar
Sandeep committed
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 18 19 20 21 22 23 24

#include <memory>

#include "gtest/gtest.h"

#include "ngraph/log.hpp"
#include "ngraph/ngraph.hpp"
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/backend_manager.hpp"
25
#include "ngraph/runtime/hybrid/hybrid_backend.hpp"
26
#include "ngraph/runtime/interpreter/int_backend.hpp"
27 28 29 30 31 32 33 34 35
#include "util/all_close.hpp"
#include "util/all_close_f.hpp"
#include "util/ndarray.hpp"
#include "util/test_control.hpp"
#include "util/test_tools.hpp"

using namespace std;
using namespace ngraph;

36
static runtime::Backend* hybrid_creator(const char* config)
37
{
38 39 40 41 42
    vector<string> unsupported_0 = {"Add"};
    vector<string> unsupported_1 = {"Multiply"};
    vector<shared_ptr<runtime::Backend>> backend_list = {
        make_shared<runtime::interpreter::INTBackend>(unsupported_0),
        make_shared<runtime::interpreter::INTBackend>(unsupported_1)};
43

44
    return new runtime::hybrid::HybridBackend(backend_list);
45 46
}

47
TEST(HYBRID, abc)
48
{
49 50
    const string backend_name = "H1";
    runtime::BackendManager::register_backend(backend_name, hybrid_creator);
51 52 53 54

    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
55 56 57
    auto D = make_shared<op::Parameter>(element::f32, shape);
    auto t1 = A * B;
    auto t2 = t1 * D;
58
    auto C = make_shared<op::Parameter>(element::f32, shape);
59
    auto f = make_shared<Function>((t2 + C) * t1, ParameterVector{A, B, C, D});
60

61 62
    shared_ptr<runtime::Backend> backend = runtime::Backend::create("H1");
    static_pointer_cast<runtime::hybrid::HybridBackend>(backend)->set_debug_enabled(true);
63 64 65 66 67

    // Create some tensors for input/output
    shared_ptr<runtime::Tensor> a = backend->create_tensor(element::f32, shape);
    shared_ptr<runtime::Tensor> b = backend->create_tensor(element::f32, shape);
    shared_ptr<runtime::Tensor> c = backend->create_tensor(element::f32, shape);
68
    shared_ptr<runtime::Tensor> d = backend->create_tensor(element::f32, shape);
69 70
    shared_ptr<runtime::Tensor> result = backend->create_tensor(element::f32, shape);

71 72 73 74
    copy_data(a, vector<float>{1, 2, 3, 4});
    copy_data(b, vector<float>{5, 6, 7, 8});
    copy_data(c, vector<float>{9, 10, 11, 12});
    copy_data(d, vector<float>{4, 3, 2, 1});
75

76
    auto handle = backend->compile(f);
77 78
    backend->call_with_validate(handle, {result}, {a, b, c, d});
    EXPECT_EQ(read_vector<float>(result), (vector<float>{145, 552, 1113, 1408}));
79
}