1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include <cstdint>
17 #include <functional>
18 #include <memory>
19 #include <random>
20
21 #include <gtest/gtest.h>
22 #include "tensorflow/lite/delegates/xnnpack/quantize_tester.h"
23 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
24
25 namespace tflite {
26 namespace xnnpack {
27
28 TEST(QuantizeUInt8ToUInt8, 4D) {
29 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
30 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
31 TfLiteXNNPackDelegateDelete);
32
33 std::random_device random_device;
34 auto rng = std::mt19937(random_device());
35 auto shape_rng =
36 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
37 const auto batch = shape_rng();
38 const auto height = shape_rng();
39 const auto width = shape_rng();
40 const auto channels = shape_rng();
41
42 QuantizeTester()
43 .Shape({batch, height, width, channels})
44 .InputZeroPoint(113)
45 .InputScale(0.75f)
46 .OutputZeroPoint(131)
47 .OutputScale(1.25f)
48 .Test(TensorType_UINT8, TensorType_UINT8, xnnpack_delegate.get());
49 }
50
51 TEST(QuantizeUInt8ToUInt8, 3D) {
52 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
53 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
54 TfLiteXNNPackDelegateDelete);
55
56 std::random_device random_device;
57 auto rng = std::mt19937(random_device());
58 auto shape_rng =
59 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
60 const auto batch = shape_rng();
61 const auto width = shape_rng();
62 const auto channels = shape_rng();
63
64 QuantizeTester()
65 .Shape({batch, width, channels})
66 .InputZeroPoint(113)
67 .InputScale(0.75f)
68 .OutputZeroPoint(131)
69 .OutputScale(1.25f)
70 .Test(TensorType_UINT8, TensorType_UINT8, xnnpack_delegate.get());
71 }
72
73 TEST(QuantizeUInt8ToUInt8, 2D) {
74 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
75 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
76 TfLiteXNNPackDelegateDelete);
77
78 std::random_device random_device;
79 auto rng = std::mt19937(random_device());
80 auto shape_rng =
81 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
82 const auto batch = shape_rng();
83 const auto channels = shape_rng();
84
85 QuantizeTester()
86 .Shape({batch, channels})
87 .InputZeroPoint(113)
88 .InputScale(0.75f)
89 .OutputZeroPoint(131)
90 .OutputScale(1.25f)
91 .Test(TensorType_UINT8, TensorType_UINT8, xnnpack_delegate.get());
92 }
93
94 TEST(QuantizeUInt8ToUInt8, 1D) {
95 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
96 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
97 TfLiteXNNPackDelegateDelete);
98
99 std::random_device random_device;
100 auto rng = std::mt19937(random_device());
101 auto shape_rng =
102 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
103 const auto batch = shape_rng();
104
105 QuantizeTester()
106 .Shape({batch})
107 .InputZeroPoint(113)
108 .InputScale(0.75f)
109 .OutputZeroPoint(131)
110 .OutputScale(1.25f)
111 .Test(TensorType_UINT8, TensorType_UINT8, xnnpack_delegate.get());
112 }
113
TEST(QuantizeUInt8ToUInt8,MultiThreading)114 TEST(QuantizeUInt8ToUInt8, MultiThreading) {
115 TfLiteXNNPackDelegateOptions delegate_options =
116 TfLiteXNNPackDelegateOptionsDefault();
117 delegate_options.num_threads = 2;
118 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
119 xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options),
120 TfLiteXNNPackDelegateDelete);
121
122 std::random_device random_device;
123 auto rng = std::mt19937(random_device());
124 auto shape_rng =
125 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
126 const auto batch = shape_rng();
127 const auto height = shape_rng();
128 const auto width = shape_rng();
129 const auto channels = shape_rng();
130
131 QuantizeTester()
132 .Shape({batch, height, width, channels})
133 .InputZeroPoint(113)
134 .InputScale(0.75f)
135 .OutputZeroPoint(131)
136 .OutputScale(1.25f)
137 .Test(TensorType_UINT8, TensorType_UINT8, xnnpack_delegate.get());
138 }
139
140 } // namespace xnnpack
141 } // namespace tflite
142