Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / relu.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.hpp"
22 #include "util/all_close_f.hpp"
23 #include "util/known_element_types.hpp"
24 #include "util/ndarray.hpp"
25 #include "util/test_control.hpp"
26 #include "util/test_tools.hpp"
27
28 NGRAPH_SUPPRESS_DEPRECATED_START
29
30 using namespace std;
31 using namespace ngraph;
32
33 static string s_manifest = "${MANIFEST}";
34
35 NGRAPH_TEST(${BACKEND_NAME}, relu_2Dfprop)
36 {
37     auto shape_a = Shape{2, 5};
38     auto A = make_shared<op::Parameter>(element::f32, shape_a);
39     auto relu = make_shared<op::Relu>(A);
40     auto shape_rt = Shape{2, 5};
41     auto f = make_shared<Function>(relu, ParameterVector{A});
42
43     auto backend = runtime::Backend::create("${BACKEND_NAME}");
44
45     auto a = backend->create_tensor(element::f32, shape_a);
46     copy_data(a, vector<float>{1, 8, -8, 17, -0.5, 1, 8, -8, 17, -0.5});
47     auto result = backend->create_tensor(element::f32, shape_rt);
48     vector<float> expected{1, 8, 0, 17, 0, 1, 8, 0, 17, 0};
49
50     auto handle = backend->compile(f);
51     handle->call_with_validate({result}, {a});
52     EXPECT_TRUE(test::all_close_f(read_vector<float>(result), expected, MIN_FLOAT_TOLERANCE_BITS));
53 }
54
55 NGRAPH_TEST(${BACKEND_NAME}, relu_2Dfprop_i32)
56 {
57     auto shape_a = Shape{2, 5};
58     auto A = make_shared<op::Parameter>(element::i32, shape_a);
59     auto relu = make_shared<op::Relu>(A);
60     auto shape_rt = Shape{2, 5};
61     auto f = make_shared<Function>(relu, ParameterVector{A});
62
63     auto backend = runtime::Backend::create("${BACKEND_NAME}");
64
65     auto a = backend->create_tensor(element::i32, shape_a);
66     copy_data(a, vector<int32_t>{1, 8, -8, 17, -2, 1, 8, -8, 17, -1});
67     auto result = backend->create_tensor(element::i32, shape_rt);
68     vector<int32_t> expected{1, 8, 0, 17, 0, 1, 8, 0, 17, 0};
69
70     auto handle = backend->compile(f);
71     handle->call_with_validate({result}, {a});
72     EXPECT_EQ(expected, read_vector<int32_t>(result));
73 }
74
75 NGRAPH_TEST(${BACKEND_NAME}, relu_4Dfprop)
76 {
77     auto shape_a = Shape{2, 2, 2, 2};
78     auto A = make_shared<op::Parameter>(element::f32, shape_a);
79     auto relu = make_shared<op::Relu>(A);
80     auto shape_rt = Shape{2, 2, 2, 2};
81     auto f = make_shared<Function>(relu, ParameterVector{A});
82
83     auto backend = runtime::Backend::create("${BACKEND_NAME}");
84
85     auto a = backend->create_tensor(element::f32, shape_a);
86     copy_data(a, vector<float>{1, 8, -8, 17, -0.5, 1, 8, -8, 17, -0.5, 1, 8, -8, 17, -0.5, 1});
87     auto result = backend->create_tensor(element::f32, shape_rt);
88     vector<float> expected{1, 8, 0, 17, 0, 1, 8, 0, 17, 0, 1, 8, 0, 17, 0, 1};
89
90     auto handle = backend->compile(f);
91     handle->call_with_validate({result}, {a});
92     EXPECT_TRUE(test::all_close_f(read_vector<float>(result), expected, MIN_FLOAT_TOLERANCE_BITS));
93 }
94
95 NGRAPH_TEST(${BACKEND_NAME}, fuse_max_with_constant_zero_input_as_relu)
96 {
97     auto shape_a = Shape{2, 5};
98     auto A = op::Constant::create(element::f32, shape_a, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
99     auto B = make_shared<op::Parameter>(element::f32, shape_a);
100     auto max = make_shared<op::Maximum>(A, B);
101     auto shape_rt = Shape{2, 5};
102     auto f = make_shared<Function>(max, ParameterVector{B});
103
104     auto backend = runtime::Backend::create("${BACKEND_NAME}");
105
106     auto b = backend->create_tensor(element::f32, shape_a);
107     copy_data(b, vector<float>{1, 8, -8, 17, -0.5, 1, 8, -8, 17, -0.5});
108     auto result = backend->create_tensor(element::f32, shape_rt);
109     vector<float> expected{1, 8, 0, 17, 0, 1, 8, 0, 17, 0};
110
111     auto handle = backend->compile(f);
112     handle->call_with_validate({result}, {b});
113     EXPECT_TRUE(test::all_close_f(read_vector<float>(result), expected, MIN_FLOAT_TOLERANCE_BITS));
114 }