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/quantized_unary_elementwise_tester.h"
23 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
24
25 namespace tflite {
26 namespace xnnpack {
27
28 TEST(UnsignedQuantizedLogistic, 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 QuantizedUnaryElementwiseTester()
43 .Unsigned(true)
44 .Shape({batch, height, width, channels})
45 .InputZeroPoint(127)
46 .InputScale(15.0f / 127.0f)
47 .OutputZeroPoint(std::numeric_limits<uint8_t>::min())
48 .OutputScale(1.0f / 256.0f)
49 .Test(BuiltinOperator_LOGISTIC, xnnpack_delegate.get());
50 }
51
52 TEST(UnsignedQuantizedLogistic, 3D) {
53 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
54 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
55 TfLiteXNNPackDelegateDelete);
56
57 std::random_device random_device;
58 auto rng = std::mt19937(random_device());
59 auto shape_rng =
60 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
61 const auto batch = shape_rng();
62 const auto width = shape_rng();
63 const auto channels = shape_rng();
64
65 QuantizedUnaryElementwiseTester()
66 .Unsigned(true)
67 .Shape({batch, width, channels})
68 .InputZeroPoint(127)
69 .InputScale(15.0f / 127.0f)
70 .OutputZeroPoint(std::numeric_limits<uint8_t>::min())
71 .OutputScale(1.0f / 256.0f)
72 .Test(BuiltinOperator_LOGISTIC, xnnpack_delegate.get());
73 }
74
75 TEST(UnsignedQuantizedLogistic, 2D) {
76 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
77 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
78 TfLiteXNNPackDelegateDelete);
79
80 std::random_device random_device;
81 auto rng = std::mt19937(random_device());
82 auto shape_rng =
83 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
84 const auto batch = shape_rng();
85 const auto channels = shape_rng();
86
87 QuantizedUnaryElementwiseTester()
88 .Unsigned(true)
89 .Shape({batch, channels})
90 .InputZeroPoint(127)
91 .InputScale(15.0f / 127.0f)
92 .OutputZeroPoint(std::numeric_limits<uint8_t>::min())
93 .OutputScale(1.0f / 256.0f)
94 .Test(BuiltinOperator_LOGISTIC, xnnpack_delegate.get());
95 }
96
97 TEST(UnsignedQuantizedLogistic, 1D) {
98 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
99 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
100 TfLiteXNNPackDelegateDelete);
101
102 std::random_device random_device;
103 auto rng = std::mt19937(random_device());
104 auto shape_rng =
105 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
106 const auto batch = shape_rng();
107
108 QuantizedUnaryElementwiseTester()
109 .Unsigned(true)
110 .Shape({batch})
111 .InputZeroPoint(127)
112 .InputScale(15.0f / 127.0f)
113 .OutputZeroPoint(std::numeric_limits<uint8_t>::min())
114 .OutputScale(1.0f / 256.0f)
115 .Test(BuiltinOperator_LOGISTIC, xnnpack_delegate.get());
116 }
117
TEST(UnsignedQuantizedLogistic,MultiThreading)118 TEST(UnsignedQuantizedLogistic, MultiThreading) {
119 TfLiteXNNPackDelegateOptions delegate_options =
120 TfLiteXNNPackDelegateOptionsDefault();
121 delegate_options.num_threads = 2;
122 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
123 xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options),
124 TfLiteXNNPackDelegateDelete);
125
126 std::random_device random_device;
127 auto rng = std::mt19937(random_device());
128 auto shape_rng =
129 std::bind(std::uniform_int_distribution<int32_t>(2, 5), std::ref(rng));
130 const auto batch = shape_rng();
131 const auto height = shape_rng();
132 const auto width = shape_rng();
133 const auto channels = shape_rng();
134
135 QuantizedUnaryElementwiseTester()
136 .Unsigned(true)
137 .Shape({batch, height, width, channels})
138 .InputZeroPoint(127)
139 .InputScale(15.0f / 127.0f)
140 .OutputZeroPoint(std::numeric_limits<uint8_t>::min())
141 .OutputScale(1.0f / 256.0f)
142 .Test(BuiltinOperator_LOGISTIC, xnnpack_delegate.get());
143 }
144
145 } // namespace xnnpack
146 } // namespace tflite
147