Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / divide.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 <algorithm>
18 #include <cinttypes>
19 #include <cmath>
20 #include <cstdlib>
21 #include <random>
22 #include <string>
23
24 // clang-format off
25 #ifdef ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
26 #define DEFAULT_FLOAT_TOLERANCE_BITS ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS
27 #endif
28
29 #ifdef ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
30 #define DEFAULT_DOUBLE_TOLERANCE_BITS ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS
31 #endif
32 // clang-format on
33
34 #include "gtest/gtest.h"
35 #include "runtime/backend.hpp"
36 #include "ngraph/runtime/tensor.hpp"
37 #include "ngraph/ngraph.hpp"
38 #include "util/all_close.hpp"
39 #include "util/all_close_f.hpp"
40 #include "util/ndarray.hpp"
41 #include "util/test_control.hpp"
42 #include "util/test_tools.hpp"
43
44 NGRAPH_SUPPRESS_DEPRECATED_START
45
46 using namespace std;
47 using namespace ngraph;
48
49 static string s_manifest = "${MANIFEST}";
50
51 NGRAPH_TEST(${BACKEND_NAME}, divide)
52 {
53     Shape shape{2, 2};
54
55     auto A = make_shared<op::Parameter>(element::f32, shape);
56     auto B = make_shared<op::Parameter>(element::f32, shape);
57     auto f = make_shared<Function>(make_shared<op::Divide>(A, B), ParameterVector{A, B});
58
59     auto backend = runtime::Backend::create("${BACKEND_NAME}");
60
61     // Create some tensors for input/output
62     auto a = backend->create_tensor(element::f32, shape);
63     copy_data(a, vector<float>{2, 4, 8, 16});
64     auto b = backend->create_tensor(element::f32, shape);
65     copy_data(b, vector<float>{1, 2, 4, 8});
66     auto result = backend->create_tensor(element::f32, shape);
67
68     auto handle = backend->compile(f);
69     handle->call_with_validate({result}, {a, b});
70     EXPECT_TRUE(test::all_close_f((vector<float>{2, 2, 2, 2}), read_vector<float>(result)));
71 }
72
73 NGRAPH_TEST(${BACKEND_NAME}, divide_int32)
74 {
75     Shape shape{2, 2};
76
77     auto A = make_shared<op::Parameter>(element::i32, shape);
78     auto B = make_shared<op::Parameter>(element::i32, shape);
79     auto f = make_shared<Function>(make_shared<op::Divide>(A, B), ParameterVector{A, B});
80
81     auto backend = runtime::Backend::create("${BACKEND_NAME}");
82
83     // Create some tensors for input/output
84     auto a = backend->create_tensor(element::i32, shape);
85     copy_data(a, vector<int32_t>{0x40000140, 0x40000001, 8, 16});
86     auto b = backend->create_tensor(element::i32, shape);
87     copy_data(b, vector<int32_t>{2, 5, 4, 8});
88     auto result = backend->create_tensor(element::i32, shape);
89
90     auto handle = backend->compile(f);
91     handle->call_with_validate({result}, {a, b});
92     EXPECT_EQ((vector<int32_t>{536871072, 214748365, 2, 2}), read_vector<int32_t>(result));
93 }
94
95 NGRAPH_TEST(${BACKEND_NAME}, divide_cpp_rounding_int32)
96 {
97     Shape shape{2, 2};
98
99     auto A = make_shared<op::Parameter>(element::i32, shape);
100     auto B = make_shared<op::Parameter>(element::i32, shape);
101     auto f = make_shared<Function>(make_shared<op::Divide>(A, B, false), ParameterVector{A, B});
102
103     auto backend = runtime::Backend::create("${BACKEND_NAME}");
104
105     // Create some tensors for input/output
106     auto a = backend->create_tensor(element::i32, shape);
107     copy_data(a, vector<int32_t>{-10, -10, 10, 10});
108     auto b = backend->create_tensor(element::i32, shape);
109     copy_data(b, vector<int32_t>{-3, 3, -3, 3});
110     auto result = backend->create_tensor(element::i32, shape);
111
112     auto handle = backend->compile(f);
113     handle->call_with_validate({result}, {a, b});
114     EXPECT_EQ((vector<int32_t>{3, -3, -3, 3}), read_vector<int32_t>(result));
115 }
116
117 NGRAPH_TEST(${BACKEND_NAME}, divide_python_rounding_int32)
118 {
119     Shape shape{2, 2};
120
121     auto A = make_shared<op::Parameter>(element::i32, shape);
122     auto B = make_shared<op::Parameter>(element::i32, shape);
123     auto f = make_shared<Function>(make_shared<op::Divide>(A, B), ParameterVector{A, B});
124
125     auto backend = runtime::Backend::create("${BACKEND_NAME}");
126
127     // Create some tensors for input/output
128     auto a = backend->create_tensor(element::i32, shape);
129     copy_data(a, vector<int32_t>{-10, -10, 10, 10});
130     auto b = backend->create_tensor(element::i32, shape);
131     copy_data(b, vector<int32_t>{-3, 3, -3, 3});
132     auto result = backend->create_tensor(element::i32, shape);
133
134     auto handle = backend->compile(f);
135     handle->call_with_validate({result}, {a, b});
136     EXPECT_EQ((vector<int32_t>{3, -4, -4, 3}), read_vector<int32_t>(result));
137 }
138
139 NGRAPH_TEST(${BACKEND_NAME}, divide_overload)
140 {
141     Shape shape{2, 2};
142
143     auto A = make_shared<op::Parameter>(element::f32, shape);
144     auto B = make_shared<op::Parameter>(element::f32, shape);
145     auto f = make_shared<Function>(A / B, ParameterVector{A, B});
146
147     auto backend = runtime::Backend::create("${BACKEND_NAME}");
148
149     // Create some tensors for input/output
150     auto a = backend->create_tensor(element::f32, shape);
151     copy_data(a, vector<float>{2, 4, 8, 16});
152     auto b = backend->create_tensor(element::f32, shape);
153     copy_data(b, vector<float>{1, 2, 4, 8});
154     auto result = backend->create_tensor(element::f32, shape);
155
156     auto handle = backend->compile(f);
157     handle->call_with_validate({result}, {a, b});
158     EXPECT_TRUE(test::all_close_f((vector<float>{2, 2, 2, 2}), read_vector<float>(result)));
159 }
160
161 NGRAPH_TEST(${BACKEND_NAME}, divide_by_zero_float32)
162 {
163     Shape shape{2, 2};
164
165     auto A = make_shared<op::Parameter>(element::f32, shape);
166     auto B = make_shared<op::Parameter>(element::f32, shape);
167     auto f = make_shared<Function>(make_shared<op::Divide>(A, B), ParameterVector{A, B});
168
169     auto backend = runtime::Backend::create("${BACKEND_NAME}");
170
171     // Create some tensors for input/output
172     auto a = backend->create_tensor(element::f32, shape);
173     copy_data(a, vector<float>{2, 4, 8, 16});
174     auto b = backend->create_tensor(element::f32, shape);
175     copy_data(b, vector<float>{0, 0, 0, 0});
176     auto result = backend->create_tensor(element::f32, shape);
177
178     auto handle = backend->compile(f);
179     handle->call_with_validate({result}, {a, b});
180     EXPECT_EQ((vector<float>{std::numeric_limits<float>::infinity(),
181                              std::numeric_limits<float>::infinity(),
182                              std::numeric_limits<float>::infinity(),
183                              std::numeric_limits<float>::infinity()}),
184               read_vector<float>(result));
185 }