Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / add.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 "ngraph/ngraph.hpp"
36 #include "util/engine/test_engines.hpp"
37 #include "util/test_case.hpp"
38 #include "util/test_control.hpp"
39
40 NGRAPH_SUPPRESS_DEPRECATED_START
41
42 using namespace std;
43 using namespace ngraph;
44
45 static string s_manifest = "${MANIFEST}";
46 using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME});
47
48 NGRAPH_TEST(${BACKEND_NAME}, add)
49 {
50     Shape shape{2, 2};
51     auto A = make_shared<op::Parameter>(element::f32, shape);
52     auto B = make_shared<op::Parameter>(element::f32, shape);
53     auto f = make_shared<Function>(make_shared<op::Add>(A, B), ParameterVector{A, B});
54
55     vector<float> a{1, 2, 3, 4};
56     vector<float> b{5, 6, 7, 8};
57
58     auto test_case = test::TestCase<TestEngine>(f);
59     test_case.add_multiple_inputs<float>({a, b});
60     test_case.add_expected_output<float>(shape, {6, 8, 10, 12});
61     test_case.run();
62 }
63
64 NGRAPH_TEST(${BACKEND_NAME}, add_overload)
65 {
66     Shape shape{2, 2};
67     auto A = make_shared<op::Parameter>(element::f32, shape);
68     auto B = make_shared<op::Parameter>(element::f32, shape);
69     auto f = make_shared<Function>(A + B, ParameterVector{A, B});
70
71     vector<float> a{1, 2, 3, 4};
72     vector<float> b{5, 6, 7, 8};
73
74     auto test_case = test::TestCase<TestEngine>(f);
75     test_case.add_multiple_inputs<float>({a, b});
76     test_case.add_expected_output<float>(shape, {6, 8, 10, 12});
77     test_case.run();
78 }
79
80 NGRAPH_TEST(${BACKEND_NAME}, add_in_place)
81 {
82     Shape shape{2, 2};
83     auto A = make_shared<op::Parameter>(element::f32, shape);
84     auto B = make_shared<op::Parameter>(element::f32, shape);
85     auto T = A + B;
86     auto T2 = T + T;
87     auto T3 = T2 + T2;
88     auto T4 = T3 + T3;
89
90     auto f = make_shared<Function>(T4, ParameterVector{A, B});
91
92     vector<float> a{1, 2, 3, 4};
93     vector<float> b{5, 6, 7, 8};
94
95     auto test_case = test::TestCase<TestEngine>(f);
96     test_case.add_multiple_inputs<float>({a, b});
97     test_case.add_expected_output<float>(shape, {48, 64, 80, 96});
98     test_case.run();
99 }