blob: db52be5062a1f4db953e4b1b012d384a53f415d4 [file] [log] [blame]
Pablo Tello299025a2017-09-29 11:30:12 +01001/*
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +00002 * Copyright (c) 2017-2019 ARM Limited.
Pablo Tello299025a2017-09-29 11:30:12 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE
25#define ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Pablo Tello299025a2017-09-29 11:30:12 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Pablo Tello299025a2017-09-29 11:30:12 +010035#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/GEMMLowp.h"
Pablo Tello299025a2017-09-29 11:30:12 +010037
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
George Wort2d7e6832019-02-22 16:37:41 +000046namespace
47{
48template <typename U>
49void fill(U &&tensor, int i)
50{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000051 switch(tensor.data_type())
52 {
53 case DataType::QSYMM8_PER_CHANNEL:
54 {
55 int min_bound = 128;
56 int max_bound = -127;
57 for(size_t j = 0; j < tensor.quantization_info().scale().size(); j++)
58 {
59 std::pair<int, int> bounds = get_symm_quantized_per_channel_bounds(tensor.quantization_info(), -1.0f, 1.0f, i);
60 if(bounds.first < min_bound)
61 {
62 min_bound = bounds.first;
63 }
64 if(bounds.second > max_bound)
65 {
66 max_bound = bounds.second;
67 }
68 }
69 std::uniform_int_distribution<int8_t> distribution(min_bound, max_bound);
70 library->fill(tensor, distribution, i);
71 break;
72 }
73 case DataType::QASYMM8:
74 {
75 std::uniform_int_distribution<uint8_t> distribution(1, 254);
76 library->fill(tensor, distribution, i);
77 break;
78 }
79 case DataType::F16:
80 case DataType::F32:
81 {
82 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
83 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
84 library->fill(tensor, distribution, i);
85 break;
86 }
87 default:
88 library->fill_tensor_uniform(tensor, i);
89 }
George Wort2d7e6832019-02-22 16:37:41 +000090}
91
92template <typename TensorType, typename AccessorType, typename FunctionType, bool reinterpret_input_as_3d, bool reinterpret_output_as_3d, typename OutputType, bool is_fused = false>
93TensorType compute_gemmlowp_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000094 GEMMLowpOutputStageInfo output_stage = GEMMLowpOutputStageInfo(), DataType data_type_b = DataType::QASYMM8, QuantizationInfo b_qinfo = QuantizationInfo())
George Wort2d7e6832019-02-22 16:37:41 +000095{
96 // Create tensors
97 TensorType a = create_tensor<TensorType>(shape_a, DataType::QASYMM8, 1);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000098 TensorType b = create_tensor<TensorType>(shape_b, data_type_b, 1); // gemm output before output stage mismatch if i pass data_layout_output here. to be investigated
George Wort2d7e6832019-02-22 16:37:41 +000099 TensorType output = create_tensor<TensorType>(shape_output, output_stage.type == GEMMLowpOutputStageType::NONE ? DataType::S32 : DataType::QASYMM8, 1);
100
101 a.info()->set_quantization_info(QuantizationInfo(1.0f / 255, a_offset));
George Wort2d7e6832019-02-22 16:37:41 +0000102
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000103 if(data_type_b == DataType::QSYMM8_PER_CHANNEL)
104 {
105 b.info()->set_quantization_info(b_qinfo);
106 }
107 else
108 {
109 b.info()->set_quantization_info(QuantizationInfo(1.0f / 255, b_offset));
110 }
George Wort2d7e6832019-02-22 16:37:41 +0000111 TensorType bias;
112 if(is_fused)
113 {
114 TensorShape bias_shape(shape_b[0]);
115 bias = create_tensor<TensorType>(bias_shape, DataType::S32, 1);
116 }
117
118 // Create and configure function
119 // The GEMMinfo includes the values of the depth in case of reinterpreted 3d input/output
120 FunctionType gemmlowp;
121 // TODO (COMPMID-1672) - Extending the test to validate add bias in offset contribution
122 gemmlowp.configure(&a, &b, is_fused ? &bias : nullptr, &output, GEMMInfo(false, false, false, (reinterpret_output_as_3d ? shape_output[2] : 0), reinterpret_input_as_3d, false, output_stage));
123
124 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
125 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
126 ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS);
127
128 // Allocate tensors
129 a.allocator()->allocate();
130 b.allocator()->allocate();
131 output.allocator()->allocate();
132
133 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
134 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
135 ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS);
136
137 // Fill tensors
138 fill(AccessorType(a), 0);
139 fill(AccessorType(b), 1);
140
141 if(is_fused)
142 {
143 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
144 bias.allocator()->allocate();
145 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
146 fill(AccessorType(bias), 2);
147 }
George Wort2d7e6832019-02-22 16:37:41 +0000148 // Compute GEMM function
149 gemmlowp.run();
150 return output;
151}
152
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000153template <bool reinterpret_input_as_3d, typename TW = uint8_t>
154SimpleTensor<int32_t> compute_gemmlowp_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset,
155 DataType data_type_b = DataType::QASYMM8, QuantizationInfo b_qinfo = QuantizationInfo())
George Wort2d7e6832019-02-22 16:37:41 +0000156{
157 TensorShape shape_a_to_use = shape_a;
158 if(reinterpret_input_as_3d)
159 {
160 // Collapse the second and third dimension if the input is 3D
161 shape_a_to_use.collapse(2U, 1U);
162 }
163
164 // Create reference
165 SimpleTensor<uint8_t> a{ shape_a_to_use, DataType::QASYMM8, 1 };
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000166 SimpleTensor<TW> b{ shape_b, data_type_b, 1, data_type_b == DataType::QSYMM8_PER_CHANNEL ? b_qinfo : QuantizationInfo(1.0f / 255, b_offset) };
George Wort2d7e6832019-02-22 16:37:41 +0000167
168 // Fill reference
169 fill(a, 0);
170 fill(b, 1);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000171 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t, TW>(a, b, shape_output, a_offset, b_offset);
George Wort2d7e6832019-02-22 16:37:41 +0000172}
173}
174
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100175template <typename TensorType, typename AccessorType, typename FunctionType, bool reinterpret_input_as_3d = false, bool reinterpret_output_as_3d = false>
Gian Marcoe75a02b2017-11-08 12:24:09 +0000176class GEMMLowpMatrixMultiplyCoreValidationFixture : public framework::Fixture
Pablo Tello299025a2017-09-29 11:30:12 +0100177{
178public:
179 template <typename...>
George Wort2d7e6832019-02-22 16:37:41 +0000180 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_output, int32_t a_offset, int32_t b_offset)
Pablo Tello299025a2017-09-29 11:30:12 +0100181 {
George Wort2d7e6832019-02-22 16:37:41 +0000182 _target = compute_target(shape_a, shape_b, shape_output, a_offset, b_offset);
183 _reference = compute_reference(shape_a, shape_b, shape_output, a_offset, b_offset);
Pablo Tello299025a2017-09-29 11:30:12 +0100184 }
185
186protected:
George Wort2d7e6832019-02-22 16:37:41 +0000187 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset)
Pablo Tello299025a2017-09-29 11:30:12 +0100188 {
George Wort2d7e6832019-02-22 16:37:41 +0000189 return compute_gemmlowp_target<TensorType, AccessorType, FunctionType, reinterpret_input_as_3d, reinterpret_output_as_3d, int32_t>(shape_a, shape_b, shape_output, a_offset, b_offset);
Pablo Tello299025a2017-09-29 11:30:12 +0100190 }
191
George Wort2d7e6832019-02-22 16:37:41 +0000192 SimpleTensor<int32_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset)
Pablo Tello299025a2017-09-29 11:30:12 +0100193 {
George Wort2d7e6832019-02-22 16:37:41 +0000194 return compute_gemmlowp_reference<reinterpret_input_as_3d>(shape_a, shape_b, shape_output, a_offset, b_offset);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100195 }
196
Pablo Tello6ff12a02017-11-02 16:09:35 +0000197 TensorType _target{};
198 SimpleTensor<int32_t> _reference{};
Pablo Tellobf2fb952017-09-29 16:43:25 +0100199};
200
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000201template <typename TensorType, typename AccessorType, typename FunctionType, bool reinterpret_input_as_3d = false, bool reinterpret_output_as_3d = false, typename TW = uint8_t>
George Wort2d7e6832019-02-22 16:37:41 +0000202class GEMMLowpMatrixMultiplyCoreFusedOffsetOutputValidationFixture : public framework::Fixture
203{
204public:
205 template <typename...>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000206 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_output, int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage, DataType data_type_b)
George Wort2d7e6832019-02-22 16:37:41 +0000207 {
208 ARM_COMPUTE_EXPECT(output_stage.type != GEMMLowpOutputStageType::NONE, framework::LogLevel::ERRORS);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000209 if(data_type_b == DataType::QSYMM8_PER_CHANNEL)
210 {
211 output_stage.is_quantized_per_channel = true;
212 const size_t num_channels = shape_b[0];
213 std::vector<float> scales(num_channels);
214 std::uniform_real_distribution<> distribution(0, 1);
215 library->fill(scales, distribution, 0);
216 output_stage.gemmlowp_multipliers.resize(num_channels);
217 output_stage.gemmlowp_shifts.resize(num_channels);
218 for(size_t i = 0; i < num_channels; ++i)
219 {
220 quantization::calculate_quantized_multiplier_less_than_one(scales[i], &output_stage.gemmlowp_multipliers[i], &output_stage.gemmlowp_shifts[i]);
221 }
222
223 _reference = compute_reference(shape_a, shape_b, shape_output, a_offset, 0, output_stage, data_type_b, QuantizationInfo(scales));
224 _target = compute_target(shape_a, shape_b, shape_output, a_offset, 0, output_stage, data_type_b, QuantizationInfo(scales));
225 }
226 else
227 {
228 _reference = compute_reference(shape_a, shape_b, shape_output, a_offset, b_offset, output_stage, data_type_b, QuantizationInfo());
229 _target = compute_target(shape_a, shape_b, shape_output, a_offset, b_offset, output_stage, data_type_b, QuantizationInfo());
230 }
George Wort2d7e6832019-02-22 16:37:41 +0000231 }
232
233protected:
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000234 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage,
235 DataType data_type_b, QuantizationInfo b_qinfo)
George Wort2d7e6832019-02-22 16:37:41 +0000236 {
237 return compute_gemmlowp_target<TensorType, AccessorType, FunctionType, reinterpret_input_as_3d, reinterpret_output_as_3d, qasymm8_t, true>(shape_a, shape_b, shape_output, a_offset, b_offset,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000238 output_stage, data_type_b, b_qinfo);
George Wort2d7e6832019-02-22 16:37:41 +0000239 }
240
241 SimpleTensor<qasymm8_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000242 GEMMLowpOutputStageInfo output_stage, DataType data_type_b, QuantizationInfo b_qinfo)
George Wort2d7e6832019-02-22 16:37:41 +0000243 {
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000244 SimpleTensor<int32_t> output = compute_gemmlowp_reference<reinterpret_input_as_3d, TW>(shape_a, shape_b, shape_output, a_offset, b_offset, data_type_b, b_qinfo);
George Wort2d7e6832019-02-22 16:37:41 +0000245
246 TensorShape bias_shape(shape_b[0]);
247 SimpleTensor<int32_t> bias{ bias_shape, DataType::S32, 1 };
248 fill(bias, 2);
249
250 switch(output_stage.type)
251 {
252 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
253 return reference::gemmlowp_quantize_down_int32_to_uint8_scale<int32_t>(output, bias,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000254 output_stage.gemmlowp_offset, output_stage.gemmlowp_multipliers, output_stage.gemmlowp_shifts, output_stage.gemmlowp_min_bound, output_stage.gemmlowp_max_bound);
George Wort2d7e6832019-02-22 16:37:41 +0000255 break;
256 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000257 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, uint8_t>(output, bias,
258 output_stage.gemmlowp_multipliers, output_stage.gemmlowp_shifts, output_stage.gemmlowp_offset, output_stage.gemmlowp_min_bound, output_stage.gemmlowp_max_bound);
George Wort2d7e6832019-02-22 16:37:41 +0000259 break;
260 default:
261 ARM_COMPUTE_ERROR("Not Supported!");
262 }
263 }
264
265 TensorType _target{};
266 SimpleTensor<qasymm8_t> _reference{};
267};
268
Gian Marcoe75a02b2017-11-08 12:24:09 +0000269template <typename TensorType, typename AccessorType, typename FunctionType>
270class GEMMLowpQuantizeDownInt32ToUint8ScaleValidationFixture : public framework::Fixture
271{
272public:
273 template <typename...>
Gian Marco6b77e912017-11-17 09:27:57 +0000274 void setup(TensorShape shape, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000275 {
Gian Marco6b77e912017-11-17 09:27:57 +0000276 _target = compute_target(shape, result_offset, result_mult_int, result_shift, min, max, add_bias);
277 _reference = compute_reference(shape, result_offset, result_mult_int, result_shift, min, max, add_bias);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000278 }
279
280protected:
281 template <typename U>
282 void fill(U &&tensor, int i)
283 {
284 std::uniform_int_distribution<> distribution(-6000, 6000);
285 library->fill(tensor, distribution, i);
286 }
287
Gian Marco6b77e912017-11-17 09:27:57 +0000288 TensorType compute_target(const TensorShape &shape, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000289 {
Gian Marco6b77e912017-11-17 09:27:57 +0000290 TensorShape shape_bias(shape[0]);
291
Gian Marcoe75a02b2017-11-08 12:24:09 +0000292 // Create tensors
293 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
Gian Marco6b77e912017-11-17 09:27:57 +0000294 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
295 TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000296
297 // Create and configure function
298 FunctionType output_stage;
Gian Marco6b77e912017-11-17 09:27:57 +0000299 output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_offset, result_mult_int, result_shift, min, max);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000300
301 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marco6b77e912017-11-17 09:27:57 +0000302 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000303
304 // Allocate tensors
305 a.allocator()->allocate();
Gian Marco6b77e912017-11-17 09:27:57 +0000306 c.allocator()->allocate();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000307
308 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marco6b77e912017-11-17 09:27:57 +0000309 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000310
Gian Marco6b77e912017-11-17 09:27:57 +0000311 // Fill tensor
Gian Marcoe75a02b2017-11-08 12:24:09 +0000312 fill(AccessorType(a), 0);
313
Gian Marco6b77e912017-11-17 09:27:57 +0000314 if(add_bias)
315 {
316 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
317
318 // Allocate bias tensor
319 b.allocator()->allocate();
320
321 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
322
323 // Fill tensor
324 fill(AccessorType(b), 1);
325 }
326
Gian Marcoe75a02b2017-11-08 12:24:09 +0000327 // Compute GEMM function
328 output_stage.run();
Gian Marco6b77e912017-11-17 09:27:57 +0000329 return c;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000330 }
331
Gian Marco6b77e912017-11-17 09:27:57 +0000332 SimpleTensor<uint8_t> compute_reference(const TensorShape &shape, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000333 {
334 // Create reference
Gian Marco6b77e912017-11-17 09:27:57 +0000335 TensorShape shape_bias(shape[0]);
336
Gian Marcoe75a02b2017-11-08 12:24:09 +0000337 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
Gian Marco6b77e912017-11-17 09:27:57 +0000338 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
Gian Marcoe75a02b2017-11-08 12:24:09 +0000339
340 // Fill reference
341 fill(a, 0);
342
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000343 const std::vector<int32_t> result_mult_int_vec = { result_mult_int };
344 const std::vector<int32_t> result_shift_vec = { result_shift };
345
Gian Marco6b77e912017-11-17 09:27:57 +0000346 if(add_bias)
347 {
348 // Fill bias
349 fill(b, 1);
350
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000351 return reference::gemmlowp_quantize_down_int32_to_uint8_scale<int32_t>(a, b, result_offset, result_mult_int_vec, result_shift_vec, min, max);
Gian Marco6b77e912017-11-17 09:27:57 +0000352 }
353 else
354 {
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000355 return reference::gemmlowp_quantize_down_int32_to_uint8_scale<int32_t>(a, result_offset, result_mult_int_vec, result_shift_vec, min, max);
Gian Marco6b77e912017-11-17 09:27:57 +0000356 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000357 }
358
359 TensorType _target{};
360 SimpleTensor<uint8_t> _reference{};
361};
Gian Marco58c57942017-11-28 09:10:03 +0000362
363template <typename TensorType, typename AccessorType, typename FunctionType>
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000364class GEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointValidationFixture : public framework::Fixture
365{
366public:
367 template <typename...>
368 void setup(TensorShape shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max, bool add_bias)
369 {
370 _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
371 _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
372 }
373
374protected:
375 template <typename U>
376 void fill(U &&tensor, int i)
377 {
378 std::uniform_int_distribution<> distribution(-6000, 6000);
379 library->fill(tensor, distribution, i);
380 }
381
382 TensorType compute_target(const TensorShape &shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max, bool add_bias)
383 {
384 TensorShape shape_bias(shape[0]);
385
386 // Create tensors
387 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
388 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
389 TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8_SIGNED, 1);
390
391 // Create and configure function
392 FunctionType output_stage;
393 output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
394
395 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
396 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
397
398 // Allocate tensors
399 a.allocator()->allocate();
400 c.allocator()->allocate();
401
402 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
403 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
404
405 // Fill tensor
406 fill(AccessorType(a), 0);
407
408 if(add_bias)
409 {
410 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
411
412 // Allocate bias tensor
413 b.allocator()->allocate();
414
415 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
416
417 // Fill tensor
418 fill(AccessorType(b), 1);
419 }
420
421 // Compute GEMM function
422 output_stage.run();
423 return c;
424 }
425
426 SimpleTensor<int8_t> compute_reference(const TensorShape &shape, int32_t result_fixed_point_multiplier, int32_t result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max,
427 bool add_bias)
428 {
429 // Create reference
430 TensorShape shape_bias(shape[0]);
431
432 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
433 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
434
435 // Fill reference
436 fill(a, 0);
437
438 const std::vector<int32_t> result_fixed_point_multiplier_vec = { result_fixed_point_multiplier };
439 const std::vector<int32_t> result_shift_vec = { result_shift };
440
441 if(add_bias)
442 {
443 // Fill bias
444 fill(b, 1);
445
446 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, int8_t>(a, b, result_fixed_point_multiplier_vec, result_shift_vec, result_offset_after_shift, min, max);
447 }
448 else
449 {
450 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, int8_t>(a, result_fixed_point_multiplier_vec, result_shift_vec, result_offset_after_shift, min, max);
451 }
452 }
453
454 TensorType _target{};
455 SimpleTensor<int8_t> _reference{};
456};
457
458template <typename TensorType, typename AccessorType, typename FunctionType>
Gian Marco58c57942017-11-28 09:10:03 +0000459class GEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointValidationFixture : public framework::Fixture
460{
461public:
462 template <typename...>
463 void setup(TensorShape shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max, bool add_bias)
464 {
465 _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
466 _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
467 }
468
469protected:
470 template <typename U>
471 void fill(U &&tensor, int i)
472 {
473 std::uniform_int_distribution<> distribution(-6000, 6000);
474 library->fill(tensor, distribution, i);
475 }
476
477 TensorType compute_target(const TensorShape &shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max, bool add_bias)
478 {
479 TensorShape shape_bias(shape[0]);
480
481 // Create tensors
482 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
483 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
484 TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1);
485
486 // Create and configure function
487 FunctionType output_stage;
488 output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
489
490 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
491 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
492
493 // Allocate tensors
494 a.allocator()->allocate();
495 c.allocator()->allocate();
496
497 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
498 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
499
500 // Fill tensor
501 fill(AccessorType(a), 0);
502
503 if(add_bias)
504 {
505 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
506
507 // Allocate bias tensor
508 b.allocator()->allocate();
509
510 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
511
512 // Fill tensor
513 fill(AccessorType(b), 1);
514 }
515
516 // Compute GEMM function
517 output_stage.run();
518 return c;
519 }
520
521 SimpleTensor<uint8_t> compute_reference(const TensorShape &shape, int32_t result_fixed_point_multiplier, int32_t result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max,
522 bool add_bias)
523 {
524 // Create reference
525 TensorShape shape_bias(shape[0]);
526
527 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
528 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
529
530 // Fill reference
531 fill(a, 0);
532
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000533 const std::vector<int32_t> result_fixed_point_multiplier_vec = { result_fixed_point_multiplier };
534 const std::vector<int32_t> result_shift_vec = { result_shift };
535
Gian Marco58c57942017-11-28 09:10:03 +0000536 if(add_bias)
537 {
538 // Fill bias
539 fill(b, 1);
540
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000541 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, uint8_t>(a, b, result_fixed_point_multiplier_vec, result_shift_vec, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +0000542 }
543 else
544 {
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000545 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, uint8_t>(a, result_fixed_point_multiplier_vec, result_shift_vec, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +0000546 }
547 }
548
549 TensorType _target{};
550 SimpleTensor<uint8_t> _reference{};
551};
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000552
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100553template <typename TensorType, typename AccessorType, typename FunctionType>
554class GEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointValidationFixture : public framework::Fixture
555{
556public:
557 template <typename...>
558 void setup(TensorShape shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
559 {
560 _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, min, max, add_bias);
561 _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, min, max, add_bias);
562 }
563
564protected:
565 template <typename U>
566 void fill(U &&tensor, int i)
567 {
568 std::uniform_int_distribution<> distribution(-6000, 6000);
569 library->fill(tensor, distribution, i);
570 }
571
572 TensorType compute_target(const TensorShape &shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
573 {
574 TensorShape shape_bias(shape[0]);
575
576 // Create tensors
577 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
578 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
579 TensorType c = create_tensor<TensorType>(shape, DataType::QSYMM16, 1);
580
581 // Create and configure function
582 FunctionType output_stage;
583 output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, min, max);
584
585 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
586 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
587
588 // Allocate tensors
589 a.allocator()->allocate();
590 c.allocator()->allocate();
591
592 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
593 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
594
595 // Fill tensor
596 fill(AccessorType(a), 0);
597
598 if(add_bias)
599 {
600 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
601
602 // Allocate bias tensor
603 b.allocator()->allocate();
604
605 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
606
607 // Fill tensor
608 fill(AccessorType(b), 1);
609 }
610
611 // Compute GEMM function
612 output_stage.run();
613 return c;
614 }
615
616 SimpleTensor<int16_t> compute_reference(const TensorShape &shape, int32_t result_fixed_point_multiplier, int32_t result_shift, int32_t min, int32_t max,
617 bool add_bias)
618 {
619 // Create reference
620 TensorShape shape_bias(shape[0]);
621
622 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
623 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
624
625 // Fill reference
626 fill(a, 0);
627
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000628 const std::vector<int32_t> result_fixed_point_multiplier_vec = { result_fixed_point_multiplier };
629 const std::vector<int32_t> result_shift_vec = { result_shift };
630
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100631 if(add_bias)
632 {
633 // Fill bias
634 fill(b, 1);
635
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000636 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, int16_t>(a, b, result_fixed_point_multiplier_vec, result_shift_vec, 0, min, max);
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100637 }
638 else
639 {
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000640 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, int16_t>(a, result_fixed_point_multiplier_vec, result_shift_vec, 0, min, max);
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100641 }
642 }
643
644 TensorType _target{};
645 SimpleTensor<int16_t> _reference{};
646};
647
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000648template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
649class GEMMLowpMatrixMultiplyReshapedValidationFixture : public framework::Fixture
650{
651public:
652 template <typename...>
653 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int v0, unsigned int h0, bool interleave_lhs,
654 bool interleave_rhs)
655 {
656 GEMMLHSMatrixInfo lhs_info;
657 lhs_info.m0 = m0;
658 lhs_info.k0 = k0;
659 lhs_info.v0 = v0;
660 lhs_info.interleave = interleave_lhs;
661 lhs_info.transpose = false;
662
663 GEMMRHSMatrixInfo rhs_info;
664 rhs_info.n0 = n0;
665 rhs_info.k0 = k0;
666 rhs_info.h0 = h0;
667 rhs_info.interleave = interleave_rhs;
668 rhs_info.transpose = true;
669
670 // Set the tensor shapes for LHS and RHS matrices
671 const TensorShape lhs_shape(k, m, batch_size);
672 const TensorShape rhs_shape(n, k, batch_size);
673
674 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info);
675 _reference = compute_reference(lhs_shape, rhs_shape);
676 }
677
678protected:
679 template <typename U>
680 void fill(U &&tensor, int i)
681 {
682 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
683 std::uniform_int_distribution<> distribution(1, 254);
684 library->fill(tensor, distribution, i);
685 }
686
687 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info)
688 {
689 // Create tensors
690 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
691 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
692 TensorType lhs_reshaped;
693 TensorType rhs_reshaped;
694 TensorType dst;
695
696 const unsigned int M = lhs_shape[1];
697 const unsigned int N = rhs_shape[0];
698 const unsigned int K = lhs_shape[0];
699
700 // The output tensor will be auto-initialized within the function
701
702 // Create and configure function
703 ReshapeLHSFunctionType reshape_lhs;
704 ReshapeRHSFunctionType reshape_rhs;
705 GEMMFunctionType gemm;
706 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
707 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
708 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
709
710 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
711 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
712
713 // Allocate tensors
714 lhs.allocator()->allocate();
715 rhs.allocator()->allocate();
716 lhs_reshaped.allocator()->allocate();
717 rhs_reshaped.allocator()->allocate();
718 dst.allocator()->allocate();
719
720 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
721 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
722 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
723 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
724 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
725
726 // Fill tensors
727 fill(AccessorType(lhs), 0);
728 fill(AccessorType(rhs), 1);
729
730 // Compute GEMM
731 reshape_lhs.run();
732 reshape_rhs.run();
733 gemm.run();
734
735 return dst;
736 }
737
738 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape)
739 {
740 TensorShape dst_shape = lhs_shape;
741 dst_shape[0] = rhs_shape[0];
742 dst_shape[1] = lhs_shape[1];
743
744 // Create reference
745 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
746 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
747
748 // Fill reference
749 fill(lhs, 0);
750 fill(rhs, 1);
751
752 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
753 }
754
755 TensorType _target{};
756 SimpleTensor<int32_t> _reference{};
757};
758
759template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
760class GEMMLowpMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture
761{
762public:
763 template <typename...>
764 void setup(unsigned int m_w, unsigned int m_h, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int v0, unsigned int h0,
765 bool interleave_lhs, bool interleave_rhs)
766 {
767 GEMMLHSMatrixInfo lhs_info;
768 lhs_info.m0 = m0;
769 lhs_info.k0 = k0;
770 lhs_info.v0 = v0;
771 lhs_info.interleave = interleave_lhs;
772 lhs_info.transpose = false;
773
774 GEMMRHSMatrixInfo rhs_info;
775 rhs_info.n0 = n0;
776 rhs_info.k0 = k0;
777 rhs_info.h0 = h0;
778 rhs_info.interleave = interleave_rhs;
779 rhs_info.transpose = true;
780
781 // In case of GEMM3D, m is the product between m_w and m_h
782 const unsigned int m = m_w * m_h;
783
784 // Set the tensor shapes for LHS and RHS matrices
785 const TensorShape lhs_shape(k, m, batch_size);
786 const TensorShape rhs_shape(n, k, batch_size);
787
788 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h);
789 _reference = compute_reference(lhs_shape, rhs_shape, m_h);
790 }
791
792protected:
793 template <typename U>
794 void fill(U &&tensor, int i)
795 {
796 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
797 std::uniform_int_distribution<> distribution(1, 254);
798 library->fill(tensor, distribution, i);
799 }
800
801 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h)
802 {
803 // Create tensors
804 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
805 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
806 TensorType lhs_reshaped;
807 TensorType rhs_reshaped;
808 TensorType dst;
809
810 const unsigned int M = lhs_shape[1];
811 const unsigned int N = rhs_shape[0];
812 const unsigned int K = lhs_shape[0];
813
814 // The output tensor will be auto-initialized within the function
815
816 // Create and configure function
817 ReshapeLHSFunctionType reshape_lhs;
818 ReshapeRHSFunctionType reshape_rhs;
819 GEMMFunctionType gemm;
820 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
821 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
822 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
823
824 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
825 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
826
827 // Allocate tensors
828 lhs.allocator()->allocate();
829 rhs.allocator()->allocate();
830 lhs_reshaped.allocator()->allocate();
831 rhs_reshaped.allocator()->allocate();
832 dst.allocator()->allocate();
833
834 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
835 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
836 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
837 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
838 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
839
840 // Fill tensors
841 fill(AccessorType(lhs), 0);
842 fill(AccessorType(rhs), 1);
843
844 // Compute GEMM
845 reshape_lhs.run();
846 reshape_rhs.run();
847 gemm.run();
848
849 return dst;
850 }
851
852 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h)
853 {
854 TensorShape dst_shape = lhs_shape;
855 dst_shape.set(0, rhs_shape[0]);
856 dst_shape.set(1, lhs_shape[1] / m_h);
857 dst_shape.set(2, m_h);
858 dst_shape.set(3, lhs_shape[2]);
859
860 // Create reference
861 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
862 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
863
864 // Fill reference
865 fill(lhs, 0);
866 fill(rhs, 1);
867
868 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
869 }
870
871 TensorType _target{};
872 SimpleTensor<int32_t> _reference{};
873};
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000874
875template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
876class GEMMLowpMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture
877{
878public:
879 template <typename...>
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000880 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0,
881 unsigned int k0, unsigned int h0, bool interleave_rhs, bool transpose_rhs, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000882 {
883 GEMMLHSMatrixInfo lhs_info;
884 lhs_info.m0 = m0;
885 lhs_info.k0 = k0;
886
887 GEMMRHSMatrixInfo rhs_info;
888 rhs_info.n0 = n0;
889 rhs_info.k0 = k0;
890 rhs_info.h0 = h0;
891 rhs_info.interleave = interleave_rhs;
892 rhs_info.transpose = transpose_rhs;
893
894 // Set the tensor shapes for LHS and RHS matrices
895 const TensorShape lhs_shape(k, m, batch_size);
896 const TensorShape rhs_shape(n, k, batch_size);
897
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000898 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type);
899 _reference = compute_reference(lhs_shape, rhs_shape, data_type);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000900 }
901
902protected:
903 template <typename U>
904 void fill(U &&tensor, int i)
905 {
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000906 switch(tensor.data_type())
907 {
908 case DataType::QASYMM8:
909 {
910 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
911 std::uniform_int_distribution<> distribution(1, 254);
912 library->fill(tensor, distribution, i);
913 }
914 break;
915 case DataType::QASYMM8_SIGNED:
916 {
917 std::uniform_int_distribution<> distribution(-127, 126);
918 library->fill(tensor, distribution, i);
919 }
920 break;
921 default:
922 ARM_COMPUTE_ERROR("Unsupported data type");
923 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000924 }
925
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000926 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info,
927 const GEMMRHSMatrixInfo &rhs_info, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000928 {
929 // Create tensors
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000930 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
931 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000932 TensorType rhs_reshaped;
933 TensorType dst;
934
935 const unsigned int M = lhs_shape[1];
936 const unsigned int N = rhs_shape[0];
937 const unsigned int K = lhs_shape[0];
938
939 // The output tensor will be auto-initialized within the function
940
941 // Create and configure function
942 ReshapeRHSFunctionType reshape_rhs;
943 GEMMFunctionType gemm;
944 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
945 gemm.configure(&lhs, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
946
947 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
948 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
949
950 // Allocate tensors
951 lhs.allocator()->allocate();
952 rhs.allocator()->allocate();
953 rhs_reshaped.allocator()->allocate();
954 dst.allocator()->allocate();
955
956 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
957 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
958 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
959 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
960
961 // Fill tensors
962 fill(AccessorType(lhs), 0);
963 fill(AccessorType(rhs), 1);
964
965 // Compute GEMM
966 reshape_rhs.run();
967 gemm.run();
968
969 return dst;
970 }
971
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000972 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000973 {
974 TensorShape dst_shape = lhs_shape;
975 dst_shape[0] = rhs_shape[0];
976 dst_shape[1] = lhs_shape[1];
977
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000978 if(data_type == DataType::QASYMM8)
979 {
980 // Create reference
981 SimpleTensor<uint8_t> lhs{ lhs_shape, data_type, 1 };
982 SimpleTensor<uint8_t> rhs{ rhs_shape, data_type, 1 };
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000983
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000984 // Fill reference
985 fill(lhs, 0);
986 fill(rhs, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000987
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000988 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
989 }
990 else
991 {
992 // Create reference
993 SimpleTensor<int8_t> lhs{ lhs_shape, data_type, 1 };
994 SimpleTensor<int8_t> rhs{ rhs_shape, data_type, 1 };
995
996 // Fill reference
997 fill(lhs, 0);
998 fill(rhs, 1);
999
1000 return reference::gemmlowp_matrix_multiply_core<int32_t, int8_t>(lhs, rhs, dst_shape, 0, 0);
1001 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001002 }
1003
1004 TensorType _target{};
1005 SimpleTensor<int32_t> _reference{};
1006};
1007
1008template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
1009class GEMMLowpMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture
1010{
1011public:
1012 template <typename...>
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001013 void setup(unsigned int m_w, unsigned int m_h, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0,
1014 unsigned int k0, unsigned int h0, bool interleave_rhs, bool transpose_rhs, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001015 {
1016 GEMMLHSMatrixInfo lhs_info;
1017 lhs_info.m0 = m0;
1018 lhs_info.k0 = k0;
1019
1020 GEMMRHSMatrixInfo rhs_info;
1021 rhs_info.n0 = n0;
1022 rhs_info.k0 = k0;
1023 rhs_info.h0 = h0;
1024 rhs_info.interleave = interleave_rhs;
1025 rhs_info.transpose = transpose_rhs;
1026
1027 // In case of GEMM3D, m is the product between m_w and m_h
1028 const unsigned int m = m_w * m_h;
1029
1030 // Set the tensor shapes for LHS and RHS matrices
1031 const TensorShape lhs_shape(k, m, batch_size);
1032 const TensorShape rhs_shape(n, k, batch_size);
1033
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001034 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h, data_type);
1035 _reference = compute_reference(lhs_shape, rhs_shape, m_h, data_type);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001036 }
1037
1038protected:
1039 template <typename U>
1040 void fill(U &&tensor, int i)
1041 {
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001042 switch(tensor.data_type())
1043 {
1044 case DataType::QASYMM8:
1045 {
1046 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1047 std::uniform_int_distribution<> distribution(1, 254);
1048 library->fill(tensor, distribution, i);
1049 }
1050 break;
1051 case DataType::QASYMM8_SIGNED:
1052 {
1053 std::uniform_int_distribution<> distribution(-127, 126);
1054 library->fill(tensor, distribution, i);
1055 }
1056 break;
1057 default:
1058 ARM_COMPUTE_ERROR("Unsupported data type");
1059 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001060 }
1061
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001062 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info,
1063 const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001064 {
1065 // Create tensors
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001066 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
1067 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001068 TensorType rhs_reshaped;
1069 TensorType dst;
1070
1071 const unsigned int M = lhs_shape[1];
1072 const unsigned int N = rhs_shape[0];
1073 const unsigned int K = lhs_shape[0];
1074
1075 // The output tensor will be auto-initialized within the function
1076
1077 // Create and configure function
1078 ReshapeRHSFunctionType reshape_rhs;
1079 GEMMFunctionType gemm;
1080 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
1081 gemm.configure(&lhs, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
1082
1083 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1084 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1085
1086 // Allocate tensors
1087 lhs.allocator()->allocate();
1088 rhs.allocator()->allocate();
1089 rhs_reshaped.allocator()->allocate();
1090 dst.allocator()->allocate();
1091
1092 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1093 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1094 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
1095 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1096
1097 // Fill tensors
1098 fill(AccessorType(lhs), 0);
1099 fill(AccessorType(rhs), 1);
1100
1101 // Compute GEMM
1102 reshape_rhs.run();
1103 gemm.run();
1104
1105 return dst;
1106 }
1107
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001108 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001109 {
1110 TensorShape dst_shape = lhs_shape;
1111 dst_shape.set(0, rhs_shape[0]);
1112 dst_shape.set(1, lhs_shape[1] / m_h);
1113 dst_shape.set(2, m_h);
1114 dst_shape.set(3, lhs_shape[2]);
1115
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001116 if(data_type == DataType::QASYMM8)
1117 {
1118 // Create reference
1119 SimpleTensor<uint8_t> lhs{ lhs_shape, data_type, 1 };
1120 SimpleTensor<uint8_t> rhs{ rhs_shape, data_type, 1 };
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001121
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001122 // Fill reference
1123 fill(lhs, 0);
1124 fill(rhs, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001125
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001126 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1127 }
1128 else
1129 {
1130 // Create reference
1131 SimpleTensor<int8_t> lhs{ lhs_shape, data_type, 1 };
1132 SimpleTensor<int8_t> rhs{ rhs_shape, data_type, 1 };
1133
1134 // Fill reference
1135 fill(lhs, 0);
1136 fill(rhs, 1);
1137
1138 return reference::gemmlowp_matrix_multiply_core<int32_t, int8_t>(lhs, rhs, dst_shape, 0, 0);
1139 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001140 }
1141
1142 TensorType _target{};
1143 SimpleTensor<int32_t> _reference{};
1144};
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001145
1146template <typename TensorType, typename AccessorType, typename GEMMFunctionType>
1147class GEMMLowpMatrixMultiplyNativeValidationFixture : public framework::Fixture
1148{
1149public:
1150 template <typename...>
1151 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0)
1152 {
1153 GEMMLHSMatrixInfo lhs_info;
1154 lhs_info.m0 = m0;
1155 lhs_info.k0 = k0;
1156
1157 GEMMRHSMatrixInfo rhs_info;
1158 rhs_info.n0 = n0;
1159 rhs_info.k0 = k0;
1160
1161 // Set the tensor shapes for LHS and RHS matrices
1162 const TensorShape lhs_shape(k, m, batch_size);
1163 const TensorShape rhs_shape(n, k, batch_size);
1164
1165 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info);
1166 _reference = compute_reference(lhs_shape, rhs_shape);
1167 }
1168
1169protected:
1170 template <typename U>
1171 void fill(U &&tensor, int i)
1172 {
1173 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1174 std::uniform_int_distribution<> distribution(1, 254);
1175 library->fill(tensor, distribution, i);
1176 }
1177
1178 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info)
1179 {
1180 // Create tensors
1181 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
1182 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
1183 TensorType dst;
1184
1185 const unsigned int M = lhs_shape[1];
1186 const unsigned int N = rhs_shape[0];
1187 const unsigned int K = lhs_shape[0];
1188
1189 // The output tensor will be auto-initialized within the function
1190
1191 // Create and configure function
1192 GEMMFunctionType gemm;
1193 gemm.configure(&lhs, &rhs, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
1194
1195 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1196 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1197
1198 // Allocate tensors
1199 lhs.allocator()->allocate();
1200 rhs.allocator()->allocate();
1201 dst.allocator()->allocate();
1202
1203 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1204 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1205 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1206
1207 // Fill tensors
1208 fill(AccessorType(lhs), 0);
1209 fill(AccessorType(rhs), 1);
1210
1211 // Compute GEMM
1212 gemm.run();
1213
1214 return dst;
1215 }
1216
1217 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape)
1218 {
1219 TensorShape dst_shape = lhs_shape;
1220 dst_shape[0] = rhs_shape[0];
1221 dst_shape[1] = lhs_shape[1];
1222
1223 // Create reference
1224 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
1225 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
1226
1227 // Fill reference
1228 fill(lhs, 0);
1229 fill(rhs, 1);
1230
1231 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1232 }
1233
1234 TensorType _target{};
1235 SimpleTensor<int32_t> _reference{};
1236};
1237
1238template <typename TensorType, typename AccessorType, typename GEMMFunctionType>
1239class GEMMLowpMatrixMultiplyNative3DValidationFixture : public framework::Fixture
1240{
1241public:
1242 template <typename...>
1243 void setup(unsigned int m_w, unsigned int m_h, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0)
1244 {
1245 GEMMLHSMatrixInfo lhs_info;
1246 lhs_info.m0 = m0;
1247 lhs_info.k0 = k0;
1248
1249 GEMMRHSMatrixInfo rhs_info;
1250 rhs_info.n0 = n0;
1251 rhs_info.k0 = k0;
1252
1253 // In case of GEMM3D, m is the product between m_w and m_h
1254 const unsigned int m = m_w * m_h;
1255
1256 // Set the tensor shapes for LHS and RHS matrices
1257 const TensorShape lhs_shape(k, m, batch_size);
1258 const TensorShape rhs_shape(n, k, batch_size);
1259
1260 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h);
1261 _reference = compute_reference(lhs_shape, rhs_shape, m_h);
1262 }
1263
1264protected:
1265 template <typename U>
1266 void fill(U &&tensor, int i)
1267 {
1268 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1269 std::uniform_int_distribution<> distribution(1, 254);
1270 library->fill(tensor, distribution, i);
1271 }
1272
1273 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h)
1274 {
1275 // Create tensors
1276 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
1277 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
1278 TensorType dst;
1279
1280 const unsigned int M = lhs_shape[1];
1281 const unsigned int N = rhs_shape[0];
1282 const unsigned int K = lhs_shape[0];
1283
1284 // The output tensor will be auto-initialized within the function
1285
1286 // Create and configure function
1287 GEMMFunctionType gemm;
1288 gemm.configure(&lhs, &rhs, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
1289
1290 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1291 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1292
1293 // Allocate tensors
1294 lhs.allocator()->allocate();
1295 rhs.allocator()->allocate();
1296 dst.allocator()->allocate();
1297
1298 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1299 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1300 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1301
1302 // Fill tensors
1303 fill(AccessorType(lhs), 0);
1304 fill(AccessorType(rhs), 1);
1305
1306 // Compute GEMM
1307 gemm.run();
1308
1309 return dst;
1310 }
1311
1312 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h)
1313 {
1314 TensorShape dst_shape = lhs_shape;
1315 dst_shape.set(0, rhs_shape[0]);
1316 dst_shape.set(1, lhs_shape[1] / m_h);
1317 dst_shape.set(2, m_h);
1318 dst_shape.set(3, lhs_shape[2]);
1319
1320 // Create reference
1321 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
1322 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
1323
1324 // Fill reference
1325 fill(lhs, 0);
1326 fill(rhs, 1);
1327
1328 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1329 }
1330
1331 TensorType _target{};
1332 SimpleTensor<int32_t> _reference{};
1333};
Pablo Tello299025a2017-09-29 11:30:12 +01001334} // namespace validation
1335} // namespace test
1336} // namespace arm_compute
George Wort2d7e6832019-02-22 16:37:41 +00001337#endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */