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/ndarray.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 NGRAPH_TEST(${BACKEND_NAME}, select)
37 auto A = make_shared<op::Parameter>(element::boolean, shape);
38 auto B = make_shared<op::Parameter>(element::f32, shape);
39 auto C = make_shared<op::Parameter>(element::f32, shape);
40 auto f = make_shared<Function>(make_shared<op::Select>(A, B, C), ParameterVector{A, B, C});
42 auto backend = runtime::Backend::create("${BACKEND_NAME}");
44 // Create some tensors for input/output
45 auto a = backend->create_tensor(element::boolean, shape);
46 copy_data(a, vector<char>{0, 1, 1, 0, 0, 1, 0, 1});
47 auto b = backend->create_tensor(element::f32, shape);
48 copy_data(b, vector<float>{1, 2, 3, 4, 5, 6, 7, 8});
49 auto c = backend->create_tensor(element::f32, shape);
50 copy_data(c, vector<float>{11, 12, 13, 14, 15, 16, 17, 18});
51 auto result = backend->create_tensor(element::f32, shape);
53 auto handle = backend->compile(f);
54 handle->call_with_validate({result}, {a, b, c});
55 EXPECT_TRUE(test::all_close_f((vector<float>{11, 2, 3, 14, 15, 6, 17, 8}),
56 read_vector<float>(result),
57 MIN_FLOAT_TOLERANCE_BITS));
60 NGRAPH_TEST(${BACKEND_NAME}, select_v1)
62 auto A = make_shared<op::Parameter>(element::boolean, Shape{4});
63 auto B = make_shared<op::Parameter>(element::f32, Shape{4});
64 auto C = make_shared<op::Parameter>(element::f32, Shape{2, 4});
65 auto f = make_shared<Function>(make_shared<op::v1::Select>(A, B, C), ParameterVector{A, B, C});
67 auto backend = runtime::Backend::create("${BACKEND_NAME}");
69 // Create some tensors for input/output
70 auto a = backend->create_tensor(element::boolean, Shape{4});
71 copy_data(a, vector<char>{0, 1, 1, 0});
72 auto b = backend->create_tensor(element::f32, Shape{4});
73 copy_data(b, vector<float>{1, 2, 3, 4});
74 auto c = backend->create_tensor(element::f32, Shape{2, 4});
75 copy_data(c, vector<float>{11, 12, 13, 14, 15, 16, 17, 18});
76 auto result = backend->create_tensor(element::f32, Shape{2, 4});
78 auto handle = backend->compile(f);
79 handle->call_with_validate({result}, {a, b, c});
81 test::all_close_f((vector<float>{11, 2, 3, 14, 15, 2, 3, 18}), read_vector<float>(result)));
84 NGRAPH_TEST(${BACKEND_NAME}, select_double)
87 auto A = make_shared<op::Parameter>(element::boolean, shape);
88 auto B = make_shared<op::Parameter>(element::f64, shape);
89 auto C = make_shared<op::Parameter>(element::f64, shape);
90 auto f = make_shared<Function>(make_shared<op::Select>(A, B, C), ParameterVector{A, B, C});
92 auto backend = runtime::Backend::create("${BACKEND_NAME}");
94 // Create some tensors for input/output
95 auto a = backend->create_tensor(element::boolean, shape);
96 copy_data(a, vector<char>{0, 1, 1, 0, 0, 1, 0, 1});
97 auto b = backend->create_tensor(element::f64, shape);
98 copy_data(b, vector<double>{1, 2, 3, 4, 5, 6, 7, 8});
99 auto c = backend->create_tensor(element::f64, shape);
100 copy_data(c, vector<double>{11, 12, 13, 14, 15, 16, 17, 18});
101 auto result = backend->create_tensor(element::f64, shape);
103 auto handle = backend->compile(f);
104 handle->call_with_validate({result}, {a, b, c});
105 EXPECT_TRUE(test::all_close_f((vector<double>{11, 2, 3, 14, 15, 6, 17, 8}),
106 read_vector<double>(result)));