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/known_element_types.hpp"
24 #include "util/ndarray.hpp"
25 #include "util/test_control.hpp"
26 #include "util/test_tools.hpp"
28 NGRAPH_SUPPRESS_DEPRECATED_START
31 using namespace ngraph;
33 static string s_manifest = "${MANIFEST}";
35 NGRAPH_TEST(${BACKEND_NAME}, quantized_dot_u8u8)
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});
51 Shape shape_r{1, 3}; // output shape
52 auto QD = make_shared<op::QuantizedDot>(A,
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);
70 auto b = backend->create_tensor(element::u8, shape_b);
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));
78 NGRAPH_TEST(${BACKEND_NAME}, quantized_dot_int32_output)
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});
94 Shape shape_r{1, 3}; // output shape
95 auto QD = make_shared<op::QuantizedDot>(A,
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));