Deprecate nGraph v0 ops and builders (#1856)
[platform/upstream/dldt.git] / ngraph / test / backend / quantized_dot.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}, quantized_dot_u8u8)
36 {
37     Shape shape_a{1, 2}; // input shape
38     vector<uint8_t> a_data = {2, 3};
39     Shape shape_b{2, 3}; // filter shape
40     vector<uint8_t> b_data = {0, 2, 4, 1, 3, 5};
41     auto A = make_shared<op::Parameter>(element::u8, shape_a);
42     auto B = make_shared<op::Parameter>(element::u8, shape_b);
43     auto input_scale = op::Constant::create(element::f32, Shape{}, {2});
44     auto input_zero_point = op::Constant::create(element::u8, Shape{}, {0});
45     auto filter_scale = op::Constant::create(element::f32, Shape{}, {1});
46     auto filter_zero_point = op::Constant::create(element::u8, Shape{}, {0});
47     auto output_scale = op::Constant::create(element::f32, Shape{}, {2});
48     auto output_zero_point = op::Constant::create(element::u8, Shape{}, {0});
49     AxisSet axes{};
50
51     Shape shape_r{1, 3}; // output shape
52     auto QD = make_shared<op::QuantizedDot>(A,
53                                             B,
54                                             1,
55                                             input_scale,
56                                             input_zero_point,
57                                             filter_scale,
58                                             filter_zero_point,
59                                             output_scale,
60                                             output_zero_point,
61                                             element::u8,
62                                             axes,
63                                             axes,
64                                             axes);
65     auto f = make_shared<Function>(NodeVector{QD}, ParameterVector{A, B});
66     auto backend = runtime::Backend::create("${BACKEND_NAME}");
67     // Create some tensors for input/output
68     auto a = backend->create_tensor(element::u8, shape_a);
69     copy_data(a, a_data);
70     auto b = backend->create_tensor(element::u8, shape_b);
71     copy_data(b, b_data);
72     auto result = backend->create_tensor(element::u8, shape_r);
73     auto handle = backend->compile(f);
74     handle->call_with_validate({result}, {a, b});
75     EXPECT_EQ((vector<uint8_t>{3, 13, 23}), read_vector<uint8_t>(result));
76 }
77
78 NGRAPH_TEST(${BACKEND_NAME}, quantized_dot_int32_output)
79 {
80     Shape shape_a{1, 2}; // input shape
81     vector<uint8_t> a_data = {2, 3};
82     Shape shape_b{2, 3}; // filter shape
83     vector<int8_t> b_data = {0, 1, 2, 3, 4, 5};
84     auto A = make_shared<op::Parameter>(element::u8, shape_a);
85     auto B = make_shared<op::Parameter>(element::i8, shape_b);
86     auto input_scale = op::Constant::create(element::f32, Shape{}, {1});
87     auto input_zero_point = op::Constant::create(element::u8, Shape{}, {0});
88     auto filter_scale = op::Constant::create(element::f32, Shape{}, {1});
89     auto filter_zero_point = op::Constant::create(element::i8, Shape{}, {0});
90     auto output_scale = op::Constant::create(element::f32, Shape{}, {1});
91     auto output_zero_point = op::Constant::create(element::i32, Shape{}, {0});
92     AxisSet axes{};
93
94     Shape shape_r{1, 3}; // output shape
95     auto QD = make_shared<op::QuantizedDot>(A,
96                                             B,
97                                             1,
98                                             input_scale,
99                                             input_zero_point,
100                                             filter_scale,
101                                             filter_zero_point,
102                                             output_scale,
103                                             output_zero_point,
104                                             element::i32,
105                                             axes,
106                                             axes,
107                                             axes);
108     auto f = make_shared<Function>(NodeVector{QD}, ParameterVector{A, B});
109     auto backend = runtime::Backend::create("${BACKEND_NAME}");
110     // Create some tensors for input/output
111     auto a = backend->create_tensor(element::u8, shape_a);
112     copy_data(a, a_data);
113     auto b = backend->create_tensor(element::i8, shape_b);
114     copy_data(b, b_data);
115     auto result = backend->create_tensor(element::i32, shape_r);
116     auto handle = backend->compile(f);
117     handle->call_with_validate({result}, {a, b});
118     EXPECT_EQ((vector<int32_t>{9, 14, 19}), read_vector<int32_t>(result));
119 }