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}, round)
37 auto A = make_shared<op::Parameter>(element::f32, shape);
38 auto f = make_shared<Function>(make_shared<op::Round>(A), ParameterVector{A});
40 auto backend = runtime::Backend::create("${BACKEND_NAME}");
42 auto a = backend->create_tensor(element::f32, shape);
43 copy_data(a, vector<float>{0.9f, 2.5f, 2.3f, 1.5f, -4.5f});
44 auto result = backend->create_tensor(element::f32, shape);
46 auto handle = backend->compile(f);
47 handle->call_with_validate({result}, {a});
48 EXPECT_TRUE(test::all_close_f((vector<float>{1.0f, 2.0f, 2.0f, 2.0f, -4.0f}),
49 read_vector<float>(result),
50 MIN_FLOAT_TOLERANCE_BITS));
53 NGRAPH_TEST(${BACKEND_NAME}, round_2D)
56 auto A = make_shared<op::Parameter>(element::f32, shape);
57 auto f = make_shared<Function>(make_shared<op::Round>(A), ParameterVector{A});
59 auto backend = runtime::Backend::create("${BACKEND_NAME}");
61 auto a = backend->create_tensor(element::f32, shape);
78 auto result = backend->create_tensor(element::f32, shape);
80 auto handle = backend->compile(f);
81 handle->call_with_validate({result}, {a});
82 EXPECT_TRUE(test::all_close_f(
84 0.f, 0.f, 1.f, 1.f, 2.f, 2.f, 2.f, 2.f, 3.f, -1.f, -2.f, -2.f, -2.f, -2.f, -3.f}),
85 read_vector<float>(result),
86 MIN_FLOAT_TOLERANCE_BITS));
89 NGRAPH_TEST(${BACKEND_NAME}, round_int64)
91 // This tests large numbers that will not fit in a double
93 auto A = make_shared<op::Parameter>(element::i64, shape);
94 auto f = make_shared<Function>(make_shared<op::Round>(A), ParameterVector{A});
96 auto backend = runtime::Backend::create("${BACKEND_NAME}");
98 auto a = backend->create_tensor(element::i64, shape);
99 vector<int64_t> expected{0, 1, 0x4000000000000001};
100 copy_data(a, expected);
101 auto result = backend->create_tensor(element::i64, shape);
103 auto handle = backend->compile(f);
104 handle->call_with_validate({result}, {a});
105 EXPECT_EQ(expected, read_vector<int64_t>(result));