1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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 //*****************************************************************************
17 #include "gtest/gtest.h"
18 #include "ngraph/ngraph.hpp"
19 #include "ngraph/runtime/tensor.hpp"
20 #include "runtime/backend.hpp"
21 #include "util/all_close.hpp"
22 #include "util/all_close_f.hpp"
23 #include "util/known_element_types.hpp"
24 #include "util/ndarray.hpp"
25 #include "util/test_control.hpp"
26 #include "util/test_tools.hpp"
28 NGRAPH_SUPPRESS_DEPRECATED_START
31 using namespace ngraph;
33 static string s_manifest = "${MANIFEST}";
35 NGRAPH_TEST(${BACKEND_NAME}, multiple_backends)
38 auto A1 = make_shared<op::Parameter>(element::f32, shape);
39 auto B1 = make_shared<op::Parameter>(element::f32, shape);
40 auto f = make_shared<Function>(A1 + B1, ParameterVector{A1, B1});
42 auto A2 = make_shared<op::Parameter>(element::f32, shape);
43 auto B2 = make_shared<op::Parameter>(element::f32, shape);
44 auto g = make_shared<Function>(A2 * B2, ParameterVector{A2, B2});
46 auto backend1 = runtime::Backend::create("${BACKEND_NAME}");
48 auto backend2 = runtime::Backend::create("${BACKEND_NAME}");
50 // Create some tensors for input/output
51 shared_ptr<runtime::Tensor> a1 = backend1->create_tensor(element::f32, shape);
52 shared_ptr<runtime::Tensor> b1 = backend1->create_tensor(element::f32, shape);
53 shared_ptr<runtime::Tensor> result1 = backend1->create_tensor(element::f32, shape);
55 shared_ptr<runtime::Tensor> a2 = backend2->create_tensor(element::f32, shape);
56 shared_ptr<runtime::Tensor> b2 = backend2->create_tensor(element::f32, shape);
57 shared_ptr<runtime::Tensor> result2 = backend2->create_tensor(element::f32, shape);
59 copy_data(a1, test::NDArray<float, 2>({{1, 2}, {3, 4}}).get_vector());
60 copy_data(b1, test::NDArray<float, 2>({{5, 6}, {7, 8}}).get_vector());
62 copy_data(a2, test::NDArray<float, 2>({{1, 2}, {3, 4}}).get_vector());
63 copy_data(b2, test::NDArray<float, 2>({{5, 6}, {7, 8}}).get_vector());
65 auto handle1 = backend1->compile(f);
66 handle1->call_with_validate({result1}, {a1, b1});
67 EXPECT_TRUE(test::all_close_f(read_vector<float>(result1),
68 (test::NDArray<float, 2>({{6, 8}, {10, 12}})).get_vector()));
70 auto handle2 = backend2->compile(g);
71 handle2->call_with_validate({result2}, {a2, b2});
72 EXPECT_TRUE(test::all_close_f(read_vector<float>(result2),
73 (test::NDArray<float, 2>({{5, 12}, {21, 32}})).get_vector()));