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_f.hpp"
22 #include "util/ndarray.hpp"
23 #include "util/random.hpp"
24 #include "util/test_control.hpp"
25 #include "util/test_tools.hpp"
27 NGRAPH_SUPPRESS_DEPRECATED_START
30 using namespace ngraph;
32 static string s_manifest = "${MANIFEST}";
34 // This tests a backend's implementation of the two parameter version of create_tensor
35 NGRAPH_TEST(${BACKEND_NAME}, create_tensor_1)
38 auto A = make_shared<op::Parameter>(element::f32, shape);
39 auto B = make_shared<op::Parameter>(element::f32, shape);
40 auto f = make_shared<Function>(make_shared<op::Add>(A, B), ParameterVector{A, B});
42 auto backend = runtime::Backend::create("${BACKEND_NAME}");
44 // Create some tensors for input/output
45 vector<float> av = {1, 2, 3, 4};
46 vector<float> bv = {5, 6, 7, 8};
47 shared_ptr<runtime::Tensor> a = backend->create_tensor(element::f32, shape);
48 shared_ptr<runtime::Tensor> b = backend->create_tensor(element::f32, shape);
52 shared_ptr<runtime::Tensor> result = backend->create_tensor(element::f32, shape);
54 auto handle = backend->compile(f);
55 handle->call_with_validate({result}, {a, b});
56 vector<float> expected = {6, 8, 10, 12};
57 EXPECT_TRUE(test::all_close_f(read_vector<float>(result), expected, MIN_FLOAT_TOLERANCE_BITS));
60 NGRAPH_TEST(${BACKEND_NAME}, get_parameters_and_results)
63 auto A = make_shared<op::Parameter>(element::f32, shape);
64 auto B = make_shared<op::Parameter>(element::f32, shape);
65 auto C = make_shared<op::Parameter>(element::f32, shape);
66 auto f = make_shared<Function>((A + B) * C, ParameterVector{A, B, C});
68 auto backend = runtime::Backend::create("${BACKEND_NAME}");
70 // Create some tensors for input/output
71 shared_ptr<runtime::Tensor> a = backend->create_tensor(element::f32, shape);
72 shared_ptr<runtime::Tensor> b = backend->create_tensor(element::f32, shape);
73 shared_ptr<runtime::Tensor> c = backend->create_tensor(element::f32, shape);
74 shared_ptr<runtime::Tensor> result = backend->create_tensor(element::f32, shape);
76 copy_data(a, test::NDArray<float, 2>({{1, 2}, {3, 4}}).get_vector());
77 copy_data(b, test::NDArray<float, 2>({{5, 6}, {7, 8}}).get_vector());
78 copy_data(c, test::NDArray<float, 2>({{9, 10}, {11, 12}}).get_vector());
80 auto handle = backend->compile(f);
81 auto parameters = handle->get_parameters();
82 auto results = handle->get_results();
83 ASSERT_EQ(parameters.size(), 3);
84 ASSERT_EQ(results.size(), 1);
86 // This part can't be enabled until we force backends to make a copy of the source graph
87 // auto func_parameters = f->get_parameters();
88 // auto func_results = f->get_results();
89 // for (size_t i = 0; i < 3; ++i)
91 // EXPECT_NE(parameters[i], func_parameters[i]);
93 // for (size_t i = 0; i < 1; ++i)
95 // EXPECT_NE(results[i], func_results[i]);