1 /* Copyright 2022 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 <algorithm>
17 #include <cstdint>
18 #include <functional>
19 #include <memory>
20 #include <random>
21
22 #include <gtest/gtest.h>
23 #include "tensorflow/lite/delegates/xnnpack/depth_to_space_tester.h"
24 #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
25
26 namespace tflite {
27 namespace xnnpack {
28
TEST(SignedQuantizedDepthToSpace,SinglePixel)29 TEST(SignedQuantizedDepthToSpace, SinglePixel) {
30 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
31 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
32 TfLiteXNNPackDelegateDelete);
33
34 std::random_device random_device;
35 auto rng = std::mt19937(random_device());
36 auto batch_rng =
37 std::bind(std::uniform_int_distribution<int32_t>(2, 4), std::ref(rng));
38 auto block_rng =
39 std::bind(std::uniform_int_distribution<int32_t>(2, 3), std::ref(rng));
40 auto channel_rng =
41 std::bind(std::uniform_int_distribution<int32_t>(2, 16), std::ref(rng));
42
43 DepthToSpaceTester()
44 .BatchSize(batch_rng())
45 .InputHeight(1)
46 .InputWidth(1)
47 .OutputChannels(channel_rng())
48 .BlockSize(block_rng())
49 .Test(TensorType_INT8, xnnpack_delegate.get());
50 }
51
TEST(SignedQuantizedDepthToSpace,SingleRow)52 TEST(SignedQuantizedDepthToSpace, SingleRow) {
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 batch_rng =
60 std::bind(std::uniform_int_distribution<int32_t>(2, 4), std::ref(rng));
61 auto width_rng =
62 std::bind(std::uniform_int_distribution<int32_t>(5, 25), std::ref(rng));
63 auto block_rng =
64 std::bind(std::uniform_int_distribution<int32_t>(2, 3), std::ref(rng));
65 auto channel_rng =
66 std::bind(std::uniform_int_distribution<int32_t>(2, 16), std::ref(rng));
67
68 DepthToSpaceTester()
69 .BatchSize(batch_rng())
70 .InputHeight(1)
71 .InputWidth(width_rng())
72 .OutputChannels(channel_rng())
73 .BlockSize(block_rng())
74 .Test(TensorType_INT8, xnnpack_delegate.get());
75 }
76
TEST(SignedQuantizedDepthToSpace,SingleColumn)77 TEST(SignedQuantizedDepthToSpace, SingleColumn) {
78 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
79 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
80 TfLiteXNNPackDelegateDelete);
81
82 std::random_device random_device;
83 auto rng = std::mt19937(random_device());
84 auto batch_rng =
85 std::bind(std::uniform_int_distribution<int32_t>(2, 4), std::ref(rng));
86 auto height_rng =
87 std::bind(std::uniform_int_distribution<int32_t>(5, 25), std::ref(rng));
88 auto block_rng =
89 std::bind(std::uniform_int_distribution<int32_t>(2, 3), std::ref(rng));
90 auto channel_rng =
91 std::bind(std::uniform_int_distribution<int32_t>(2, 16), std::ref(rng));
92
93 DepthToSpaceTester()
94 .BatchSize(batch_rng())
95 .InputHeight(height_rng())
96 .InputWidth(1)
97 .OutputChannels(channel_rng())
98 .BlockSize(block_rng())
99 .Test(TensorType_INT8, xnnpack_delegate.get());
100 }
101
TEST(SignedQuantizedDepthToSpace,FullImage)102 TEST(SignedQuantizedDepthToSpace, FullImage) {
103 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
104 xnnpack_delegate(TfLiteXNNPackDelegateCreate(nullptr),
105 TfLiteXNNPackDelegateDelete);
106
107 std::random_device random_device;
108 auto rng = std::mt19937(random_device());
109 auto batch_rng =
110 std::bind(std::uniform_int_distribution<int32_t>(2, 4), std::ref(rng));
111 auto size_rng =
112 std::bind(std::uniform_int_distribution<int32_t>(5, 25), std::ref(rng));
113 auto block_rng =
114 std::bind(std::uniform_int_distribution<int32_t>(2, 3), std::ref(rng));
115 auto channel_rng =
116 std::bind(std::uniform_int_distribution<int32_t>(2, 16), std::ref(rng));
117
118 DepthToSpaceTester()
119 .BatchSize(batch_rng())
120 .InputHeight(size_rng())
121 .InputWidth(size_rng())
122 .OutputChannels(channel_rng())
123 .BlockSize(block_rng())
124 .Test(TensorType_INT8, xnnpack_delegate.get());
125 }
126
TEST(SignedQuantizedDepthToSpace,MultiThreading)127 TEST(SignedQuantizedDepthToSpace, MultiThreading) {
128 TfLiteXNNPackDelegateOptions delegate_options =
129 TfLiteXNNPackDelegateOptionsDefault();
130 delegate_options.num_threads = 2;
131 std::unique_ptr<TfLiteDelegate, decltype(&TfLiteXNNPackDelegateDelete)>
132 xnnpack_delegate(TfLiteXNNPackDelegateCreate(&delegate_options),
133 TfLiteXNNPackDelegateDelete);
134
135 std::random_device random_device;
136 auto rng = std::mt19937(random_device());
137 auto batch_rng =
138 std::bind(std::uniform_int_distribution<int32_t>(2, 4), std::ref(rng));
139 auto size_rng =
140 std::bind(std::uniform_int_distribution<int32_t>(5, 25), std::ref(rng));
141 auto block_rng =
142 std::bind(std::uniform_int_distribution<int32_t>(2, 3), std::ref(rng));
143 auto channel_rng =
144 std::bind(std::uniform_int_distribution<int32_t>(2, 16), std::ref(rng));
145
146 DepthToSpaceTester()
147 .BatchSize(batch_rng())
148 .InputHeight(size_rng())
149 .InputWidth(size_rng())
150 .OutputChannels(channel_rng())
151 .BlockSize(block_rng())
152 .Test(TensorType_INT8, xnnpack_delegate.get());
153 }
154
155 } // namespace xnnpack
156 } // namespace tflite
157