blob: 5d092ecac27f3b61cbcb2bf530b0a928c750ba4d [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:
257 return reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(output, bias,
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000258 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>
364class GEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointValidationFixture : 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, 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<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,
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
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000438 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
Gian Marco58c57942017-11-28 09:10:03 +0000441 if(add_bias)
442 {
443 // Fill bias
444 fill(b, 1);
445
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000446 return reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(a, b, result_fixed_point_multiplier_vec, result_shift_vec, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +0000447 }
448 else
449 {
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000450 return reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(a, result_fixed_point_multiplier_vec, result_shift_vec, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +0000451 }
452 }
453
454 TensorType _target{};
455 SimpleTensor<uint8_t> _reference{};
456};
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000457
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100458template <typename TensorType, typename AccessorType, typename FunctionType>
459class GEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointValidationFixture : public framework::Fixture
460{
461public:
462 template <typename...>
463 void setup(TensorShape shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
464 {
465 _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, min, max, add_bias);
466 _reference = compute_reference(shape, result_fixedpoint_multiplier, result_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 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::QSYMM16, 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, 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<int16_t> compute_reference(const TensorShape &shape, int32_t result_fixed_point_multiplier, int32_t result_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
533 if(add_bias)
534 {
535 // Fill bias
536 fill(b, 1);
537
538 return reference::gemmlowp_quantize_down_int32_to_int16_scale_by_fixedpoint<int32_t>(a, b, result_fixed_point_multiplier, result_shift, min, max);
539 }
540 else
541 {
542 return reference::gemmlowp_quantize_down_int32_to_int16_scale_by_fixedpoint<int32_t>(a, result_fixed_point_multiplier, result_shift, min, max);
543 }
544 }
545
546 TensorType _target{};
547 SimpleTensor<int16_t> _reference{};
548};
549
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000550template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
551class GEMMLowpMatrixMultiplyReshapedValidationFixture : public framework::Fixture
552{
553public:
554 template <typename...>
555 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,
556 bool interleave_rhs)
557 {
558 GEMMLHSMatrixInfo lhs_info;
559 lhs_info.m0 = m0;
560 lhs_info.k0 = k0;
561 lhs_info.v0 = v0;
562 lhs_info.interleave = interleave_lhs;
563 lhs_info.transpose = false;
564
565 GEMMRHSMatrixInfo rhs_info;
566 rhs_info.n0 = n0;
567 rhs_info.k0 = k0;
568 rhs_info.h0 = h0;
569 rhs_info.interleave = interleave_rhs;
570 rhs_info.transpose = true;
571
572 // Set the tensor shapes for LHS and RHS matrices
573 const TensorShape lhs_shape(k, m, batch_size);
574 const TensorShape rhs_shape(n, k, batch_size);
575
576 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info);
577 _reference = compute_reference(lhs_shape, rhs_shape);
578 }
579
580protected:
581 template <typename U>
582 void fill(U &&tensor, int i)
583 {
584 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
585 std::uniform_int_distribution<> distribution(1, 254);
586 library->fill(tensor, distribution, i);
587 }
588
589 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info)
590 {
591 // Create tensors
592 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
593 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
594 TensorType lhs_reshaped;
595 TensorType rhs_reshaped;
596 TensorType dst;
597
598 const unsigned int M = lhs_shape[1];
599 const unsigned int N = rhs_shape[0];
600 const unsigned int K = lhs_shape[0];
601
602 // The output tensor will be auto-initialized within the function
603
604 // Create and configure function
605 ReshapeLHSFunctionType reshape_lhs;
606 ReshapeRHSFunctionType reshape_rhs;
607 GEMMFunctionType gemm;
608 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
609 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
610 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
611
612 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
613 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
614
615 // Allocate tensors
616 lhs.allocator()->allocate();
617 rhs.allocator()->allocate();
618 lhs_reshaped.allocator()->allocate();
619 rhs_reshaped.allocator()->allocate();
620 dst.allocator()->allocate();
621
622 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
623 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
624 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
625 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
626 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
627
628 // Fill tensors
629 fill(AccessorType(lhs), 0);
630 fill(AccessorType(rhs), 1);
631
632 // Compute GEMM
633 reshape_lhs.run();
634 reshape_rhs.run();
635 gemm.run();
636
637 return dst;
638 }
639
640 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape)
641 {
642 TensorShape dst_shape = lhs_shape;
643 dst_shape[0] = rhs_shape[0];
644 dst_shape[1] = lhs_shape[1];
645
646 // Create reference
647 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
648 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
649
650 // Fill reference
651 fill(lhs, 0);
652 fill(rhs, 1);
653
654 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
655 }
656
657 TensorType _target{};
658 SimpleTensor<int32_t> _reference{};
659};
660
661template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
662class GEMMLowpMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture
663{
664public:
665 template <typename...>
666 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,
667 bool interleave_lhs, bool interleave_rhs)
668 {
669 GEMMLHSMatrixInfo lhs_info;
670 lhs_info.m0 = m0;
671 lhs_info.k0 = k0;
672 lhs_info.v0 = v0;
673 lhs_info.interleave = interleave_lhs;
674 lhs_info.transpose = false;
675
676 GEMMRHSMatrixInfo rhs_info;
677 rhs_info.n0 = n0;
678 rhs_info.k0 = k0;
679 rhs_info.h0 = h0;
680 rhs_info.interleave = interleave_rhs;
681 rhs_info.transpose = true;
682
683 // In case of GEMM3D, m is the product between m_w and m_h
684 const unsigned int m = m_w * m_h;
685
686 // Set the tensor shapes for LHS and RHS matrices
687 const TensorShape lhs_shape(k, m, batch_size);
688 const TensorShape rhs_shape(n, k, batch_size);
689
690 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h);
691 _reference = compute_reference(lhs_shape, rhs_shape, m_h);
692 }
693
694protected:
695 template <typename U>
696 void fill(U &&tensor, int i)
697 {
698 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
699 std::uniform_int_distribution<> distribution(1, 254);
700 library->fill(tensor, distribution, i);
701 }
702
703 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h)
704 {
705 // Create tensors
706 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
707 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
708 TensorType lhs_reshaped;
709 TensorType rhs_reshaped;
710 TensorType dst;
711
712 const unsigned int M = lhs_shape[1];
713 const unsigned int N = rhs_shape[0];
714 const unsigned int K = lhs_shape[0];
715
716 // The output tensor will be auto-initialized within the function
717
718 // Create and configure function
719 ReshapeLHSFunctionType reshape_lhs;
720 ReshapeRHSFunctionType reshape_rhs;
721 GEMMFunctionType gemm;
722 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
723 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
724 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
725
726 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
727 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
728
729 // Allocate tensors
730 lhs.allocator()->allocate();
731 rhs.allocator()->allocate();
732 lhs_reshaped.allocator()->allocate();
733 rhs_reshaped.allocator()->allocate();
734 dst.allocator()->allocate();
735
736 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
737 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
738 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
739 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
740 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
741
742 // Fill tensors
743 fill(AccessorType(lhs), 0);
744 fill(AccessorType(rhs), 1);
745
746 // Compute GEMM
747 reshape_lhs.run();
748 reshape_rhs.run();
749 gemm.run();
750
751 return dst;
752 }
753
754 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h)
755 {
756 TensorShape dst_shape = lhs_shape;
757 dst_shape.set(0, rhs_shape[0]);
758 dst_shape.set(1, lhs_shape[1] / m_h);
759 dst_shape.set(2, m_h);
760 dst_shape.set(3, lhs_shape[2]);
761
762 // Create reference
763 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
764 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
765
766 // Fill reference
767 fill(lhs, 0);
768 fill(rhs, 1);
769
770 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
771 }
772
773 TensorType _target{};
774 SimpleTensor<int32_t> _reference{};
775};
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000776
777template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
778class GEMMLowpMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture
779{
780public:
781 template <typename...>
782 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 h0, bool interleave_rhs, bool transpose_rhs)
783 {
784 GEMMLHSMatrixInfo lhs_info;
785 lhs_info.m0 = m0;
786 lhs_info.k0 = k0;
787
788 GEMMRHSMatrixInfo rhs_info;
789 rhs_info.n0 = n0;
790 rhs_info.k0 = k0;
791 rhs_info.h0 = h0;
792 rhs_info.interleave = interleave_rhs;
793 rhs_info.transpose = transpose_rhs;
794
795 // Set the tensor shapes for LHS and RHS matrices
796 const TensorShape lhs_shape(k, m, batch_size);
797 const TensorShape rhs_shape(n, k, batch_size);
798
799 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info);
800 _reference = compute_reference(lhs_shape, rhs_shape);
801 }
802
803protected:
804 template <typename U>
805 void fill(U &&tensor, int i)
806 {
807 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
808 std::uniform_int_distribution<> distribution(1, 254);
809 library->fill(tensor, distribution, i);
810 }
811
812 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info)
813 {
814 // Create tensors
815 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
816 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
817 TensorType rhs_reshaped;
818 TensorType dst;
819
820 const unsigned int M = lhs_shape[1];
821 const unsigned int N = rhs_shape[0];
822 const unsigned int K = lhs_shape[0];
823
824 // The output tensor will be auto-initialized within the function
825
826 // Create and configure function
827 ReshapeRHSFunctionType reshape_rhs;
828 GEMMFunctionType gemm;
829 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
830 gemm.configure(&lhs, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
831
832 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
833 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
834
835 // Allocate tensors
836 lhs.allocator()->allocate();
837 rhs.allocator()->allocate();
838 rhs_reshaped.allocator()->allocate();
839 dst.allocator()->allocate();
840
841 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
842 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
843 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
844 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
845
846 // Fill tensors
847 fill(AccessorType(lhs), 0);
848 fill(AccessorType(rhs), 1);
849
850 // Compute GEMM
851 reshape_rhs.run();
852 gemm.run();
853
854 return dst;
855 }
856
857 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape)
858 {
859 TensorShape dst_shape = lhs_shape;
860 dst_shape[0] = rhs_shape[0];
861 dst_shape[1] = lhs_shape[1];
862
863 // Create reference
864 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
865 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
866
867 // Fill reference
868 fill(lhs, 0);
869 fill(rhs, 1);
870
871 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
872 }
873
874 TensorType _target{};
875 SimpleTensor<int32_t> _reference{};
876};
877
878template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
879class GEMMLowpMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture
880{
881public:
882 template <typename...>
883 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 h0,
884 bool interleave_rhs, bool transpose_rhs)
885 {
886 GEMMLHSMatrixInfo lhs_info;
887 lhs_info.m0 = m0;
888 lhs_info.k0 = k0;
889
890 GEMMRHSMatrixInfo rhs_info;
891 rhs_info.n0 = n0;
892 rhs_info.k0 = k0;
893 rhs_info.h0 = h0;
894 rhs_info.interleave = interleave_rhs;
895 rhs_info.transpose = transpose_rhs;
896
897 // In case of GEMM3D, m is the product between m_w and m_h
898 const unsigned int m = m_w * m_h;
899
900 // Set the tensor shapes for LHS and RHS matrices
901 const TensorShape lhs_shape(k, m, batch_size);
902 const TensorShape rhs_shape(n, k, batch_size);
903
904 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h);
905 _reference = compute_reference(lhs_shape, rhs_shape, m_h);
906 }
907
908protected:
909 template <typename U>
910 void fill(U &&tensor, int i)
911 {
912 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
913 std::uniform_int_distribution<> distribution(1, 254);
914 library->fill(tensor, distribution, i);
915 }
916
917 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h)
918 {
919 // Create tensors
920 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
921 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
922 TensorType rhs_reshaped;
923 TensorType dst;
924
925 const unsigned int M = lhs_shape[1];
926 const unsigned int N = rhs_shape[0];
927 const unsigned int K = lhs_shape[0];
928
929 // The output tensor will be auto-initialized within the function
930
931 // Create and configure function
932 ReshapeRHSFunctionType reshape_rhs;
933 GEMMFunctionType gemm;
934 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
935 gemm.configure(&lhs, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
936
937 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
938 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
939
940 // Allocate tensors
941 lhs.allocator()->allocate();
942 rhs.allocator()->allocate();
943 rhs_reshaped.allocator()->allocate();
944 dst.allocator()->allocate();
945
946 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
947 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
948 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
949 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
950
951 // Fill tensors
952 fill(AccessorType(lhs), 0);
953 fill(AccessorType(rhs), 1);
954
955 // Compute GEMM
956 reshape_rhs.run();
957 gemm.run();
958
959 return dst;
960 }
961
962 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h)
963 {
964 TensorShape dst_shape = lhs_shape;
965 dst_shape.set(0, rhs_shape[0]);
966 dst_shape.set(1, lhs_shape[1] / m_h);
967 dst_shape.set(2, m_h);
968 dst_shape.set(3, lhs_shape[2]);
969
970 // Create reference
971 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
972 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
973
974 // Fill reference
975 fill(lhs, 0);
976 fill(rhs, 1);
977
978 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
979 }
980
981 TensorType _target{};
982 SimpleTensor<int32_t> _reference{};
983};
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100984
985template <typename TensorType, typename AccessorType, typename GEMMFunctionType>
986class GEMMLowpMatrixMultiplyNativeValidationFixture : public framework::Fixture
987{
988public:
989 template <typename...>
990 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0)
991 {
992 GEMMLHSMatrixInfo lhs_info;
993 lhs_info.m0 = m0;
994 lhs_info.k0 = k0;
995
996 GEMMRHSMatrixInfo rhs_info;
997 rhs_info.n0 = n0;
998 rhs_info.k0 = k0;
999
1000 // Set the tensor shapes for LHS and RHS matrices
1001 const TensorShape lhs_shape(k, m, batch_size);
1002 const TensorShape rhs_shape(n, k, batch_size);
1003
1004 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info);
1005 _reference = compute_reference(lhs_shape, rhs_shape);
1006 }
1007
1008protected:
1009 template <typename U>
1010 void fill(U &&tensor, int i)
1011 {
1012 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1013 std::uniform_int_distribution<> distribution(1, 254);
1014 library->fill(tensor, distribution, i);
1015 }
1016
1017 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info)
1018 {
1019 // Create tensors
1020 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
1021 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
1022 TensorType dst;
1023
1024 const unsigned int M = lhs_shape[1];
1025 const unsigned int N = rhs_shape[0];
1026 const unsigned int K = lhs_shape[0];
1027
1028 // The output tensor will be auto-initialized within the function
1029
1030 // Create and configure function
1031 GEMMFunctionType gemm;
1032 gemm.configure(&lhs, &rhs, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
1033
1034 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1035 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1036
1037 // Allocate tensors
1038 lhs.allocator()->allocate();
1039 rhs.allocator()->allocate();
1040 dst.allocator()->allocate();
1041
1042 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1043 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1044 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1045
1046 // Fill tensors
1047 fill(AccessorType(lhs), 0);
1048 fill(AccessorType(rhs), 1);
1049
1050 // Compute GEMM
1051 gemm.run();
1052
1053 return dst;
1054 }
1055
1056 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape)
1057 {
1058 TensorShape dst_shape = lhs_shape;
1059 dst_shape[0] = rhs_shape[0];
1060 dst_shape[1] = lhs_shape[1];
1061
1062 // Create reference
1063 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
1064 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
1065
1066 // Fill reference
1067 fill(lhs, 0);
1068 fill(rhs, 1);
1069
1070 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1071 }
1072
1073 TensorType _target{};
1074 SimpleTensor<int32_t> _reference{};
1075};
1076
1077template <typename TensorType, typename AccessorType, typename GEMMFunctionType>
1078class GEMMLowpMatrixMultiplyNative3DValidationFixture : public framework::Fixture
1079{
1080public:
1081 template <typename...>
1082 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)
1083 {
1084 GEMMLHSMatrixInfo lhs_info;
1085 lhs_info.m0 = m0;
1086 lhs_info.k0 = k0;
1087
1088 GEMMRHSMatrixInfo rhs_info;
1089 rhs_info.n0 = n0;
1090 rhs_info.k0 = k0;
1091
1092 // In case of GEMM3D, m is the product between m_w and m_h
1093 const unsigned int m = m_w * m_h;
1094
1095 // Set the tensor shapes for LHS and RHS matrices
1096 const TensorShape lhs_shape(k, m, batch_size);
1097 const TensorShape rhs_shape(n, k, batch_size);
1098
1099 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h);
1100 _reference = compute_reference(lhs_shape, rhs_shape, m_h);
1101 }
1102
1103protected:
1104 template <typename U>
1105 void fill(U &&tensor, int i)
1106 {
1107 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1108 std::uniform_int_distribution<> distribution(1, 254);
1109 library->fill(tensor, distribution, i);
1110 }
1111
1112 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h)
1113 {
1114 // Create tensors
1115 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
1116 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
1117 TensorType dst;
1118
1119 const unsigned int M = lhs_shape[1];
1120 const unsigned int N = rhs_shape[0];
1121 const unsigned int K = lhs_shape[0];
1122
1123 // The output tensor will be auto-initialized within the function
1124
1125 // Create and configure function
1126 GEMMFunctionType gemm;
1127 gemm.configure(&lhs, &rhs, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
1128
1129 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1130 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1131
1132 // Allocate tensors
1133 lhs.allocator()->allocate();
1134 rhs.allocator()->allocate();
1135 dst.allocator()->allocate();
1136
1137 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1138 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1139 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1140
1141 // Fill tensors
1142 fill(AccessorType(lhs), 0);
1143 fill(AccessorType(rhs), 1);
1144
1145 // Compute GEMM
1146 gemm.run();
1147
1148 return dst;
1149 }
1150
1151 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h)
1152 {
1153 TensorShape dst_shape = lhs_shape;
1154 dst_shape.set(0, rhs_shape[0]);
1155 dst_shape.set(1, lhs_shape[1] / m_h);
1156 dst_shape.set(2, m_h);
1157 dst_shape.set(3, lhs_shape[2]);
1158
1159 // Create reference
1160 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
1161 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
1162
1163 // Fill reference
1164 fill(lhs, 0);
1165 fill(rhs, 1);
1166
1167 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1168 }
1169
1170 TensorType _target{};
1171 SimpleTensor<int32_t> _reference{};
1172};
Pablo Tello299025a2017-09-29 11:30:12 +01001173} // namespace validation
1174} // namespace test
1175} // namespace arm_compute
George Wort2d7e6832019-02-22 16:37:41 +00001176#endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */