Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / api.in.cpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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 //*****************************************************************************
16
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"
26
27 NGRAPH_SUPPRESS_DEPRECATED_START
28
29 using namespace std;
30 using namespace ngraph;
31
32 static string s_manifest = "${MANIFEST}";
33
34 // This tests a backend's implementation of the two parameter version of create_tensor
35 NGRAPH_TEST(${BACKEND_NAME}, create_tensor_1)
36 {
37     Shape shape{2, 2};
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});
41
42     auto backend = runtime::Backend::create("${BACKEND_NAME}");
43
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);
49     copy_data(a, av);
50     copy_data(b, bv);
51
52     shared_ptr<runtime::Tensor> result = backend->create_tensor(element::f32, shape);
53
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));
58 }
59
60 NGRAPH_TEST(${BACKEND_NAME}, get_parameters_and_results)
61 {
62     Shape shape{2, 2};
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});
67
68     auto backend = runtime::Backend::create("${BACKEND_NAME}");
69
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);
75
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());
79
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);
85
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)
90     // {
91     //     EXPECT_NE(parameters[i], func_parameters[i]);
92     // }
93     // for (size_t i = 0; i < 1; ++i)
94     // {
95     //     EXPECT_NE(results[i], func_results[i]);
96     // }
97 }