blob: ebbe7fcfc2b021afeba6c6d5b80834e256c5b41a [file] [log] [blame]
Sadik Armagan32ca1442020-11-13 17:51:56 +00001//
Teresa Charlinad1b3d72023-03-14 12:10:28 +00002// Copyright © 2020, 2023 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan32ca1442020-11-13 17:51:56 +00003// SPDX-License-Identifier: MIT
4//
5
6#include "ConvolutionTestHelper.hpp"
7
8#include <armnn_delegate.hpp>
9
10#include <flatbuffers/flatbuffers.h>
11#include <tensorflow/lite/interpreter.h>
12#include <tensorflow/lite/kernels/register.h>
13#include <tensorflow/lite/model.h>
Sadik Armagan32ca1442020-11-13 17:51:56 +000014#include <tensorflow/lite/version.h>
15
16#include <doctest/doctest.h>
17
18namespace armnnDelegate
19{
20
Colm Donelaneff204a2023-11-28 15:46:09 +000021void DepthwiseConv2dValidReluFp32Test()
Sadik Armagan32ca1442020-11-13 17:51:56 +000022{
23 // Set input data
24 std::vector<int32_t> inputShape { 1, 3, 2, 2 };
25 std::vector<int32_t> filterShape { 1, 2, 2, 4 };
26 std::vector<int32_t> biasShape { 4 };
Matthew Sloyanebe392d2023-03-30 10:12:08 +010027 std::vector<int32_t> outputShape { 1, 2, 1, 4 };
Sadik Armagan32ca1442020-11-13 17:51:56 +000028
29 static std::vector<float> inputValues =
30 {
31 1, 2, 7, 8,
32 3, 4, 9, 10,
33 5, 6, 11, 12
34 };
35
36 std::vector<float> filterValues =
37 {
38 1, 2, 3, 4,
39 -9, 10, -11, 12,
40 5, 6, 7, 8,
41 13, -14, 15, -16
42 };
43
44 std::vector<float> biasValues = { 1, 2, 3, 4 };
45
46 std::vector<float> expectedOutputValues =
47 {
48 71, 0, 99, 0,
49 91, 0, 127, 0
50 };
51
52 tflite::Padding padding = tflite::Padding_VALID;
53 int32_t depth_multiplier = 2;
54
55 ConvolutionTest<float>(tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
56 ::tflite::TensorType_FLOAT32,
57 1, // strideX
58 1, // strideY
59 1, // dilationX
60 1, // dilationY
61 padding,
62 tflite::ActivationFunctionType_RELU,
Sadik Armagan32ca1442020-11-13 17:51:56 +000063 inputShape,
64 filterShape,
65 outputShape,
66 inputValues,
67 filterValues,
68 expectedOutputValues,
69 biasShape,
70 biasValues,
Jan Eilers7612bd62021-04-06 17:29:03 +010071 {1.0f}, // biasScale
72 {0}, // biasOffset
73 {1.0f}, // filterScale
74 {0}, // filterOffsets
75 2.0f, // outputQuantScale
76 0, // outputQuantOffset
77 1.0f, // quantScale
78 0, // quantOffset
Sadik Armagan32ca1442020-11-13 17:51:56 +000079 depth_multiplier);
80}
81
Colm Donelaneff204a2023-11-28 15:46:09 +000082void DepthwiseConv2dSameUint8Test()
Sadik Armagan32ca1442020-11-13 17:51:56 +000083{
84 // Set input data
85 std::vector<int32_t> inputShape { 1, 3, 3, 1 };
86 std::vector<int32_t> filterShape { 1, 3, 3, 1 };
87 std::vector<int32_t> biasShape { 1 } ;
88 std::vector<int32_t> outputShape { 1, 3, 3, 1 };
89
90 static std::vector<uint8_t> inputValues =
91 {
92 0, 1, 2,
93 3, 4, 5,
94 6, 7, 8
95 };
96
97 std::vector<uint8_t> filterValues = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
98
99 std::vector<int32_t> biasValues = { 10 };
100
101 std::vector<uint8_t> expectedOutputValues =
102 {
103 12, 23, 24, // ( 14+10)/2, ( 35+10)/2, ( 38+10)/2,
104 34, 65, 61, // ( 57+10)/2, (120+10)/2, (111+10)/2,
105 60, 104, 84 // (110+10)/2, (197+10)/2, (158+10)/2
106 };
107
108 tflite::Padding padding = tflite::Padding_SAME;
109
110 ConvolutionTest<uint8_t, int32_t>(tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
111 ::tflite::TensorType_UINT8,
112 1, // strideX
113 1, // strideY
114 1, // dilationX
115 1, // dilationY
116 padding,
117 tflite::ActivationFunctionType_NONE,
Sadik Armagan32ca1442020-11-13 17:51:56 +0000118 inputShape,
119 filterShape,
120 outputShape,
121 inputValues,
122 filterValues,
123 expectedOutputValues,
124 biasShape,
125 biasValues);
126}
127
Jan Eilers7612bd62021-04-06 17:29:03 +0100128void DepthwiseConv2dSameInt8PerChannelTest(std::vector<armnn::BackendId>& backends)
129{
130 // Set input data
131 std::vector<int32_t> inputShape { 1, 4, 4, 4 };
132 std::vector<int32_t> filterShape { 1, 2, 2, 16 };
133 std::vector<int32_t> biasShape {16} ;
134 std::vector<int32_t> outputShape { 1, 4, 4, 16 };
135
136 static std::vector<int8_t> inputValues =
137 {
138 3,3,3,4, 4,4,0,0, 0,3,4,3, 0,2,2,3,
139 3,0,3,0, 0,3,2,1, 4,1,2,2, 0,0,0,4,
140 3,2,2,2, 2,1,0,4, 4,3,2,4, 3,2,0,0,
141 4,1,4,4, 1,0,4,3, 3,2,0,3, 1,1,0,2
142 };
143
144 std::vector<int8_t> filterValues = { 12,20,10, 3, 2,24, 9,10, 5,16,30,12, 3,10, 4,32,
145 8, 0,30, 3, 0,16,12,15,20,12, 0, 3, 9,20, 8, 8,
146 12,15,20, 0, 0, 0, 3,15,15, 8,40,12, 9, 5, 2,24,
147 4, 0, 0, 6, 6, 0, 3, 5,20, 8,20, 3, 6,15, 4, 0 };
148 std::vector<float> filterScales = { 0.25, 0.2, 0.1, 0.3333333333,
149 0.5, 0.125, 0.33333333, 0.2,
150 0.2, 0.25, 0.1, 0.333333333,
151 0.3333333333, 0.2, 0.5, 0.125 };
152
153 int32_t filterQuantizationDim = 3;
154
155 int32_t depth_multiplier = 4;
156
157 std::vector<int32_t> biasValues = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
158
159 float inputScale = 1.0f;
160 std::vector<float> biasScales {};
161 std::vector<int64_t> biasOffsets {};
162 std::vector<int64_t> filterOffsets {};
163 for (const auto& filterScale: filterScales)
164 {
165 biasScales.push_back(inputScale * filterScale);
166 // filter and bias offset always needs to be zero for per channel. We don't support anything else
167 biasOffsets.push_back(0);
168 filterOffsets.push_back(0);
169 }
170
171 std::vector<int8_t> expectedOutputValues =
172 {
173 26,21,21, 7,12,17,28,21,20,22,25,26, 6,11,10,16,
174 16,16, 4,12, 7,18,28,27,30,20,12,14,16,19,17, 6,
175 12,12, 8, 0, 3,13,18,15,18,26,20,26,26,32,28,21,
176 0, 0, 0, 0, 2, 6, 6, 4, 2, 8, 6, 8,15,10,10,24,
177 20,21, 9, 7, 3, 6,15,16,17,22,17,22,17,18,14, 7,
178 18, 6,16,12,12,11,17,15,18,18,10,12,27,26,22,18,
179 27,28,12,10, 7, 3, 8,13, 8,12,14,16,26,24,24,24,
180 9, 9, 6, 0, 0, 0, 2, 6, 0, 0, 0, 0, 4, 8, 8,16,
181 26,24,17, 7, 2, 8,11,10,30,24,30,28,32,33,30,24,
182 20,11,16,12, 7, 9,17,13,20,14,16,18,31,36,33,29,
183 28,25,19, 9, 6,13,20,19, 2, 8, 6, 8,17,17,15,25,
184 12,15, 5, 3, 2, 6, 7, 7, 0, 0, 0, 0, 6, 2, 2, 6,
185 14,16, 7, 5, 1, 3, 3, 2,20,28,12,20,13,20,20,19,
186 9, 4,10, 4, 0, 4, 8, 6, 4,16,12,16,12,18,18,15,
187 11,12, 6, 4, 2, 8,10, 7, 0, 0, 0, 0, 9,14,14,14,
188 3, 4, 1, 1, 1, 3, 3, 2, 0, 0, 0, 0, 2, 4, 4, 8
189 };
190
191 tflite::Padding padding = tflite::Padding_SAME;
192
193 ConvolutionTest<int8_t, int32_t>(tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
194 ::tflite::TensorType_INT8,
195 1, // strideX
196 1, // strideY
197 1, // dilationX
198 1, // dilationY
199 padding,
200 tflite::ActivationFunctionType_NONE,
Jan Eilers7612bd62021-04-06 17:29:03 +0100201 inputShape,
202 filterShape,
203 outputShape,
204 inputValues,
205 filterValues,
206 expectedOutputValues,
207 biasShape,
208 biasValues,
209 biasScales,
210 biasOffsets,
211 filterScales,
212 filterOffsets,
213 1.0f,
214 0,
215 inputScale,
216 0,
217 depth_multiplier,
Colm Donelaneff204a2023-11-28 15:46:09 +0000218 filterQuantizationDim,
219 backends);
Jan Eilers7612bd62021-04-06 17:29:03 +0100220}
221
Colm Donelaneff204a2023-11-28 15:46:09 +0000222TEST_SUITE("DepthwiseConv2d_Tests")
Sadik Armagan32ca1442020-11-13 17:51:56 +0000223{
224
Colm Donelaneff204a2023-11-28 15:46:09 +0000225TEST_CASE ("DepthwiseConv2d_Valid_Relu_Fp32_Test")
Sadik Armagan32ca1442020-11-13 17:51:56 +0000226{
Colm Donelaneff204a2023-11-28 15:46:09 +0000227 DepthwiseConv2dValidReluFp32Test();
Sadik Armagan32ca1442020-11-13 17:51:56 +0000228}
229
Colm Donelaneff204a2023-11-28 15:46:09 +0000230TEST_CASE ("DepthwiseConv2d_Same_Uint8_Test")
Sadik Armagan32ca1442020-11-13 17:51:56 +0000231{
Colm Donelaneff204a2023-11-28 15:46:09 +0000232 DepthwiseConv2dSameUint8Test();
Sadik Armagan32ca1442020-11-13 17:51:56 +0000233}
234
Jan Eilers7612bd62021-04-06 17:29:03 +0100235TEST_CASE ("DepthwiseConv2d_Same_Int8_PerChannelQuantization_CpuRef_Test")
236{
Colm Donelaneff204a2023-11-28 15:46:09 +0000237 // Only works on CpuRef.
Jan Eilers7612bd62021-04-06 17:29:03 +0100238 std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef};
239 DepthwiseConv2dSameInt8PerChannelTest(backends);
240}
241
Colm Donelaneff204a2023-11-28 15:46:09 +0000242}//End of TEST_SUITE("DepthwiseConv2d_Tests")
Sadik Armagan32ca1442020-11-13 17:51:56 +0000243
244} // namespace armnnDelegate