hybrid_backend.cpp 3.32 KB
Newer Older
Sandeep's avatar
Sandeep committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//*****************************************************************************
// Copyright 2017-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 24 25

#include <memory>

#include "gtest/gtest.h"

#include "hybrid_utils.hpp"
#include "ngraph/log.hpp"
#include "ngraph/ngraph.hpp"
#include "ngraph/runtime/backend.hpp"
#include "ngraph/runtime/backend_manager.hpp"
26
#include "ngraph/runtime/hybrid/hybrid_backend.hpp"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#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;

static runtime::Backend* hybrid1_creator(const char* config)
{
    vector<shared_ptr<runtime::Backend>> backend_list;
    set<string> s0 = {"Add"};
    auto b0 = make_shared<BackendWrapper>("INTERPRETER", s0, "AddOnly");
    backend_list.push_back(b0);

#define NGRAPH_OP(a, b) #a,
    set<string> s1 = {
#include "ngraph/op/op_tbl.hpp"
    };
    auto b1 = make_shared<BackendWrapper>("INTERPRETER", s1, "AllOps");
    backend_list.push_back(b1);

    return new TestBackend(backend_list);
}

53
TEST(HYBRID, abc)
54 55 56 57 58 59 60 61
{
    const string backend_name = "HYBRID1";
    runtime::BackendManager::register_backend(backend_name, hybrid1_creator);

    Shape shape{2, 2};
    auto A = make_shared<op::Parameter>(element::f32, shape);
    auto B = make_shared<op::Parameter>(element::f32, shape);
    auto C = make_shared<op::Parameter>(element::f32, shape);
62
    auto f = make_shared<Function>((A + B) * C, ParameterVector{A, B, C});
63 64 65 66 67 68 69 70 71 72 73 74 75

    auto backend = runtime::Backend::create(backend_name);

    // 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);
    shared_ptr<runtime::Tensor> result = backend->create_tensor(element::f32, shape);

    copy_data(a, test::NDArray<float, 2>({{1, 2}, {3, 4}}).get_vector());
    copy_data(b, test::NDArray<float, 2>({{5, 6}, {7, 8}}).get_vector());
    copy_data(c, test::NDArray<float, 2>({{9, 10}, {11, 12}}).get_vector());

76
    backend->call_with_validate(backend->compile(f), {result}, {a, b, c});
77 78 79
    EXPECT_EQ(read_vector<float>(result),
              (test::NDArray<float, 2>({{54, 80}, {110, 144}})).get_vector());

80
    backend->call_with_validate(backend->compile(f), {result}, {b, a, c});
81 82 83
    EXPECT_EQ(read_vector<float>(result),
              (test::NDArray<float, 2>({{54, 80}, {110, 144}})).get_vector());

84
    backend->call_with_validate(backend->compile(f), {result}, {a, c, b});
85 86 87
    EXPECT_EQ(read_vector<float>(result),
              (test::NDArray<float, 2>({{50, 72}, {98, 128}})).get_vector());
}