blob: be9ce96dcbbccffa39cecfd8c5195dd87f1e57b5 [file] [log] [blame]
Pablo Tello299025a2017-09-29 11:30:12 +01001/*
Manuel Bottini959c26d2019-12-02 16:22:35 +00002 * Copyright (c) 2017-2020 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
Michele Di Giorgiob54ba282020-01-14 15:31:55 +000027#include "arm_compute/core/KernelDescriptors.h"
Pablo Tello299025a2017-09-29 11:30:12 +010028#include "arm_compute/core/TensorShape.h"
29#include "arm_compute/core/Types.h"
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Pablo Tello299025a2017-09-29 11:30:12 +010031#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/IAccessor.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
Pablo Tello299025a2017-09-29 11:30:12 +010036#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000037#include "tests/validation/reference/GEMMLowp.h"
Pablo Tello299025a2017-09-29 11:30:12 +010038
39#include <random>
40
41namespace arm_compute
42{
43namespace test
44{
45namespace validation
46{
George Wort2d7e6832019-02-22 16:37:41 +000047namespace
48{
49template <typename U>
50void fill(U &&tensor, int i)
51{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000052 switch(tensor.data_type())
53 {
54 case DataType::QSYMM8_PER_CHANNEL:
55 {
56 int min_bound = 128;
57 int max_bound = -127;
58 for(size_t j = 0; j < tensor.quantization_info().scale().size(); j++)
59 {
60 std::pair<int, int> bounds = get_symm_quantized_per_channel_bounds(tensor.quantization_info(), -1.0f, 1.0f, i);
61 if(bounds.first < min_bound)
62 {
63 min_bound = bounds.first;
64 }
65 if(bounds.second > max_bound)
66 {
67 max_bound = bounds.second;
68 }
69 }
70 std::uniform_int_distribution<int8_t> distribution(min_bound, max_bound);
71 library->fill(tensor, distribution, i);
72 break;
73 }
74 case DataType::QASYMM8:
75 {
76 std::uniform_int_distribution<uint8_t> distribution(1, 254);
77 library->fill(tensor, distribution, i);
78 break;
79 }
80 case DataType::F16:
81 case DataType::F32:
82 {
83 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
84 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
85 library->fill(tensor, distribution, i);
86 break;
87 }
88 default:
89 library->fill_tensor_uniform(tensor, i);
90 }
George Wort2d7e6832019-02-22 16:37:41 +000091}
92
93template <typename TensorType, typename AccessorType, typename FunctionType, bool reinterpret_input_as_3d, bool reinterpret_output_as_3d, typename OutputType, bool is_fused = false>
94TensorType compute_gemmlowp_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset,
Manuel Bottini959c26d2019-12-02 16:22:35 +000095 GEMMLowpOutputStageInfo output_stage = GEMMLowpOutputStageInfo(), DataType data_type_a = DataType::QASYMM8, DataType data_type_b = DataType::QASYMM8,
96 QuantizationInfo b_qinfo = QuantizationInfo())
George Wort2d7e6832019-02-22 16:37:41 +000097{
98 // Create tensors
Manuel Bottini959c26d2019-12-02 16:22:35 +000099 DataType data_type_output = output_stage.type == GEMMLowpOutputStageType::NONE ? DataType::S32 : data_type_a;
100
101 TensorType a = create_tensor<TensorType>(shape_a, data_type_a, 1);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000102 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
Manuel Bottini959c26d2019-12-02 16:22:35 +0000103 TensorType output = create_tensor<TensorType>(shape_output, data_type_output, 1);
George Wort2d7e6832019-02-22 16:37:41 +0000104
105 a.info()->set_quantization_info(QuantizationInfo(1.0f / 255, a_offset));
George Wort2d7e6832019-02-22 16:37:41 +0000106
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000107 if(data_type_b == DataType::QSYMM8_PER_CHANNEL)
108 {
109 b.info()->set_quantization_info(b_qinfo);
110 }
111 else
112 {
113 b.info()->set_quantization_info(QuantizationInfo(1.0f / 255, b_offset));
114 }
George Wort2d7e6832019-02-22 16:37:41 +0000115 TensorType bias;
116 if(is_fused)
117 {
118 TensorShape bias_shape(shape_b[0]);
119 bias = create_tensor<TensorType>(bias_shape, DataType::S32, 1);
120 }
121
122 // Create and configure function
123 // The GEMMinfo includes the values of the depth in case of reinterpreted 3d input/output
124 FunctionType gemmlowp;
125 // TODO (COMPMID-1672) - Extending the test to validate add bias in offset contribution
126 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));
127
128 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
129 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
130 ARM_COMPUTE_EXPECT(output.info()->is_resizable(), framework::LogLevel::ERRORS);
131
132 // Allocate tensors
133 a.allocator()->allocate();
134 b.allocator()->allocate();
135 output.allocator()->allocate();
136
137 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
138 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
139 ARM_COMPUTE_EXPECT(!output.info()->is_resizable(), framework::LogLevel::ERRORS);
140
141 // Fill tensors
142 fill(AccessorType(a), 0);
143 fill(AccessorType(b), 1);
144
145 if(is_fused)
146 {
147 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
148 bias.allocator()->allocate();
149 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
150 fill(AccessorType(bias), 2);
151 }
George Wort2d7e6832019-02-22 16:37:41 +0000152 // Compute GEMM function
153 gemmlowp.run();
154 return output;
155}
156
Manuel Bottini959c26d2019-12-02 16:22:35 +0000157template <bool reinterpret_input_as_3d, typename TI = uint8_t, typename TW = uint8_t>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000158SimpleTensor<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,
Manuel Bottini959c26d2019-12-02 16:22:35 +0000159 DataType data_type_a = DataType::QASYMM8, DataType data_type_b = DataType::QASYMM8, QuantizationInfo b_qinfo = QuantizationInfo())
George Wort2d7e6832019-02-22 16:37:41 +0000160{
161 TensorShape shape_a_to_use = shape_a;
162 if(reinterpret_input_as_3d)
163 {
164 // Collapse the second and third dimension if the input is 3D
165 shape_a_to_use.collapse(2U, 1U);
166 }
167
168 // Create reference
Manuel Bottini959c26d2019-12-02 16:22:35 +0000169 SimpleTensor<TI> a{ shape_a_to_use, data_type_a, 1 };
170 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 +0000171
172 // Fill reference
173 fill(a, 0);
174 fill(b, 1);
Manuel Bottini959c26d2019-12-02 16:22:35 +0000175 return reference::gemmlowp_matrix_multiply_core<int32_t, TI, TW>(a, b, shape_output, a_offset, b_offset);
George Wort2d7e6832019-02-22 16:37:41 +0000176}
177}
178
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100179template <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 +0000180class GEMMLowpMatrixMultiplyCoreValidationFixture : public framework::Fixture
Pablo Tello299025a2017-09-29 11:30:12 +0100181{
182public:
183 template <typename...>
George Wort2d7e6832019-02-22 16:37:41 +0000184 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 +0100185 {
George Wort2d7e6832019-02-22 16:37:41 +0000186 _target = compute_target(shape_a, shape_b, shape_output, a_offset, b_offset);
187 _reference = compute_reference(shape_a, shape_b, shape_output, a_offset, b_offset);
Pablo Tello299025a2017-09-29 11:30:12 +0100188 }
189
190protected:
George Wort2d7e6832019-02-22 16:37:41 +0000191 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 +0100192 {
George Wort2d7e6832019-02-22 16:37:41 +0000193 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 +0100194 }
195
George Wort2d7e6832019-02-22 16:37:41 +0000196 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 +0100197 {
George Wort2d7e6832019-02-22 16:37:41 +0000198 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 +0100199 }
200
Pablo Tello6ff12a02017-11-02 16:09:35 +0000201 TensorType _target{};
202 SimpleTensor<int32_t> _reference{};
Pablo Tellobf2fb952017-09-29 16:43:25 +0100203};
204
Manuel Bottini959c26d2019-12-02 16:22:35 +0000205template <typename TensorType, typename AccessorType, typename FunctionType, bool reinterpret_input_as_3d = false, bool reinterpret_output_as_3d = false, typename TI = uint8_t, typename TW = uint8_t>
George Wort2d7e6832019-02-22 16:37:41 +0000206class GEMMLowpMatrixMultiplyCoreFusedOffsetOutputValidationFixture : public framework::Fixture
207{
208public:
209 template <typename...>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000210 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 +0000211 {
212 ARM_COMPUTE_EXPECT(output_stage.type != GEMMLowpOutputStageType::NONE, framework::LogLevel::ERRORS);
Manuel Bottini959c26d2019-12-02 16:22:35 +0000213 DataType data_type_a = data_type_b == DataType::QASYMM8_SIGNED ? DataType::QASYMM8_SIGNED : DataType::QASYMM8;
214
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000215 if(data_type_b == DataType::QSYMM8_PER_CHANNEL)
216 {
217 output_stage.is_quantized_per_channel = true;
218 const size_t num_channels = shape_b[0];
219 std::vector<float> scales(num_channels);
220 std::uniform_real_distribution<> distribution(0, 1);
221 library->fill(scales, distribution, 0);
222 output_stage.gemmlowp_multipliers.resize(num_channels);
223 output_stage.gemmlowp_shifts.resize(num_channels);
224 for(size_t i = 0; i < num_channels; ++i)
225 {
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000226 quantization::calculate_quantized_multiplier(scales[i], &output_stage.gemmlowp_multipliers[i], &output_stage.gemmlowp_shifts[i]);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000227 }
228
Manuel Bottini959c26d2019-12-02 16:22:35 +0000229 _reference = compute_reference(shape_a, shape_b, shape_output, a_offset, 0, output_stage, data_type_a, data_type_b, QuantizationInfo(scales));
230 _target = compute_target(shape_a, shape_b, shape_output, a_offset, 0, output_stage, data_type_a, data_type_b, QuantizationInfo(scales));
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000231 }
232 else
233 {
Manuel Bottini959c26d2019-12-02 16:22:35 +0000234 _reference = compute_reference(shape_a, shape_b, shape_output, a_offset, b_offset, output_stage, data_type_a, data_type_b, QuantizationInfo());
235 _target = compute_target(shape_a, shape_b, shape_output, a_offset, b_offset, output_stage, data_type_a, data_type_b, QuantizationInfo());
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000236 }
George Wort2d7e6832019-02-22 16:37:41 +0000237 }
238
239protected:
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000240 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,
Manuel Bottini959c26d2019-12-02 16:22:35 +0000241 DataType data_type_a, DataType data_type_b, QuantizationInfo b_qinfo)
George Wort2d7e6832019-02-22 16:37:41 +0000242 {
243 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,
Manuel Bottini959c26d2019-12-02 16:22:35 +0000244 output_stage, data_type_a, data_type_b, b_qinfo);
George Wort2d7e6832019-02-22 16:37:41 +0000245 }
246
Manuel Bottini959c26d2019-12-02 16:22:35 +0000247 SimpleTensor<TI> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset,
248 GEMMLowpOutputStageInfo output_stage, DataType data_type_a, DataType data_type_b, QuantizationInfo b_qinfo)
George Wort2d7e6832019-02-22 16:37:41 +0000249 {
Manuel Bottini959c26d2019-12-02 16:22:35 +0000250 SimpleTensor<int32_t> output = compute_gemmlowp_reference<reinterpret_input_as_3d, TI, TW>(shape_a, shape_b, shape_output, a_offset, b_offset, data_type_a, data_type_b, b_qinfo);
George Wort2d7e6832019-02-22 16:37:41 +0000251
252 TensorShape bias_shape(shape_b[0]);
253 SimpleTensor<int32_t> bias{ bias_shape, DataType::S32, 1 };
254 fill(bias, 2);
255
256 switch(output_stage.type)
257 {
258 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Manuel Bottini959c26d2019-12-02 16:22:35 +0000259 return reference::gemmlowp_quantize_down_scale<int32_t, TW>(output, bias,
260 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 +0000261 break;
262 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Manuel Bottini959c26d2019-12-02 16:22:35 +0000263 return reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, TW>(output, bias,
264 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 +0000265 break;
266 default:
267 ARM_COMPUTE_ERROR("Not Supported!");
268 }
269 }
270
Manuel Bottini959c26d2019-12-02 16:22:35 +0000271 TensorType _target{};
272 SimpleTensor<TI> _reference{};
George Wort2d7e6832019-02-22 16:37:41 +0000273};
274
Gian Marcoe75a02b2017-11-08 12:24:09 +0000275template <typename TensorType, typename AccessorType, typename FunctionType>
276class GEMMLowpQuantizeDownInt32ToUint8ScaleValidationFixture : public framework::Fixture
277{
278public:
279 template <typename...>
Gian Marco6b77e912017-11-17 09:27:57 +0000280 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 +0000281 {
Gian Marco6b77e912017-11-17 09:27:57 +0000282 _target = compute_target(shape, result_offset, result_mult_int, result_shift, min, max, add_bias);
283 _reference = compute_reference(shape, result_offset, result_mult_int, result_shift, min, max, add_bias);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000284 }
285
286protected:
287 template <typename U>
288 void fill(U &&tensor, int i)
289 {
290 std::uniform_int_distribution<> distribution(-6000, 6000);
291 library->fill(tensor, distribution, i);
292 }
293
Gian Marco6b77e912017-11-17 09:27:57 +0000294 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 +0000295 {
Gian Marco6b77e912017-11-17 09:27:57 +0000296 TensorShape shape_bias(shape[0]);
297
Gian Marcoe75a02b2017-11-08 12:24:09 +0000298 // Create tensors
299 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
Gian Marco6b77e912017-11-17 09:27:57 +0000300 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
301 TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000302
303 // Create and configure function
304 FunctionType output_stage;
Gian Marco6b77e912017-11-17 09:27:57 +0000305 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 +0000306
307 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marco6b77e912017-11-17 09:27:57 +0000308 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000309
310 // Allocate tensors
311 a.allocator()->allocate();
Gian Marco6b77e912017-11-17 09:27:57 +0000312 c.allocator()->allocate();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000313
314 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marco6b77e912017-11-17 09:27:57 +0000315 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000316
Gian Marco6b77e912017-11-17 09:27:57 +0000317 // Fill tensor
Gian Marcoe75a02b2017-11-08 12:24:09 +0000318 fill(AccessorType(a), 0);
319
Gian Marco6b77e912017-11-17 09:27:57 +0000320 if(add_bias)
321 {
322 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
323
324 // Allocate bias tensor
325 b.allocator()->allocate();
326
327 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
328
329 // Fill tensor
330 fill(AccessorType(b), 1);
331 }
332
Gian Marcoe75a02b2017-11-08 12:24:09 +0000333 // Compute GEMM function
334 output_stage.run();
Gian Marco6b77e912017-11-17 09:27:57 +0000335 return c;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000336 }
337
Gian Marco6b77e912017-11-17 09:27:57 +0000338 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 +0000339 {
340 // Create reference
Gian Marco6b77e912017-11-17 09:27:57 +0000341 TensorShape shape_bias(shape[0]);
342
Gian Marcoe75a02b2017-11-08 12:24:09 +0000343 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
Gian Marco6b77e912017-11-17 09:27:57 +0000344 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
Gian Marcoe75a02b2017-11-08 12:24:09 +0000345
346 // Fill reference
347 fill(a, 0);
348
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000349 const std::vector<int32_t> result_mult_int_vec = { result_mult_int };
350 const std::vector<int32_t> result_shift_vec = { result_shift };
351
Gian Marco6b77e912017-11-17 09:27:57 +0000352 if(add_bias)
353 {
354 // Fill bias
355 fill(b, 1);
356
Manuel Bottini959c26d2019-12-02 16:22:35 +0000357 return reference::gemmlowp_quantize_down_scale<int32_t, uint8_t>(a, b, result_offset, result_mult_int_vec, result_shift_vec, min, max);
Gian Marco6b77e912017-11-17 09:27:57 +0000358 }
359 else
360 {
Manuel Bottini959c26d2019-12-02 16:22:35 +0000361 return reference::gemmlowp_quantize_down_scale<int32_t, uint8_t>(a, result_offset, result_mult_int_vec, result_shift_vec, min, max);
Gian Marco6b77e912017-11-17 09:27:57 +0000362 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000363 }
364
365 TensorType _target{};
366 SimpleTensor<uint8_t> _reference{};
367};
Gian Marco58c57942017-11-28 09:10:03 +0000368
369template <typename TensorType, typename AccessorType, typename FunctionType>
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000370class GEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointValidationFixture : public framework::Fixture
371{
372public:
373 template <typename...>
374 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)
375 {
376 _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
377 _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
378 }
379
380protected:
381 template <typename U>
382 void fill(U &&tensor, int i)
383 {
384 std::uniform_int_distribution<> distribution(-6000, 6000);
385 library->fill(tensor, distribution, i);
386 }
387
388 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)
389 {
390 TensorShape shape_bias(shape[0]);
391
392 // Create tensors
393 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
394 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
395 TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8_SIGNED, 1);
396
397 // Create and configure function
398 FunctionType output_stage;
399 output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
400
401 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
402 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
403
404 // Allocate tensors
405 a.allocator()->allocate();
406 c.allocator()->allocate();
407
408 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
409 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
410
411 // Fill tensor
412 fill(AccessorType(a), 0);
413
414 if(add_bias)
415 {
416 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
417
418 // Allocate bias tensor
419 b.allocator()->allocate();
420
421 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
422
423 // Fill tensor
424 fill(AccessorType(b), 1);
425 }
426
427 // Compute GEMM function
428 output_stage.run();
429 return c;
430 }
431
432 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,
433 bool add_bias)
434 {
435 // Create reference
436 TensorShape shape_bias(shape[0]);
437
438 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
439 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
440
441 // Fill reference
442 fill(a, 0);
443
444 const std::vector<int32_t> result_fixed_point_multiplier_vec = { result_fixed_point_multiplier };
445 const std::vector<int32_t> result_shift_vec = { result_shift };
446
447 if(add_bias)
448 {
449 // Fill bias
450 fill(b, 1);
451
452 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);
453 }
454 else
455 {
456 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);
457 }
458 }
459
460 TensorType _target{};
461 SimpleTensor<int8_t> _reference{};
462};
463
464template <typename TensorType, typename AccessorType, typename FunctionType>
Gian Marco58c57942017-11-28 09:10:03 +0000465class GEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointValidationFixture : public framework::Fixture
466{
467public:
468 template <typename...>
469 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)
470 {
471 _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
472 _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias);
473 }
474
475protected:
476 template <typename U>
477 void fill(U &&tensor, int i)
478 {
479 std::uniform_int_distribution<> distribution(-6000, 6000);
480 library->fill(tensor, distribution, i);
481 }
482
483 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)
484 {
485 TensorShape shape_bias(shape[0]);
486
487 // Create tensors
488 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
489 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
490 TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1);
491
492 // Create and configure function
493 FunctionType output_stage;
494 output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
495
496 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
497 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
498
499 // Allocate tensors
500 a.allocator()->allocate();
501 c.allocator()->allocate();
502
503 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
504 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
505
506 // Fill tensor
507 fill(AccessorType(a), 0);
508
509 if(add_bias)
510 {
511 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
512
513 // Allocate bias tensor
514 b.allocator()->allocate();
515
516 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
517
518 // Fill tensor
519 fill(AccessorType(b), 1);
520 }
521
522 // Compute GEMM function
523 output_stage.run();
524 return c;
525 }
526
527 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,
528 bool add_bias)
529 {
530 // Create reference
531 TensorShape shape_bias(shape[0]);
532
533 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
534 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
535
536 // Fill reference
537 fill(a, 0);
538
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000539 const std::vector<int32_t> result_fixed_point_multiplier_vec = { result_fixed_point_multiplier };
540 const std::vector<int32_t> result_shift_vec = { result_shift };
541
Gian Marco58c57942017-11-28 09:10:03 +0000542 if(add_bias)
543 {
544 // Fill bias
545 fill(b, 1);
546
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000547 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 +0000548 }
549 else
550 {
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000551 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 +0000552 }
553 }
554
555 TensorType _target{};
556 SimpleTensor<uint8_t> _reference{};
557};
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000558
Sheri Zhang1b14c752020-03-09 14:29:52 +0000559template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
560class GEMMLowpQuantizeDownInt32ScaleByFloatValidationFixture : public framework::Fixture
561{
562public:
563 template <typename...>
564 void setup(DataType data_type, TensorShape shape, float result_real_multiplier, int32_t result_offset, int32_t min, int32_t max, bool add_bias)
565 {
566 _target = compute_target(data_type, shape, result_real_multiplier, result_offset, min, max, add_bias);
567 _reference = compute_reference(shape, result_real_multiplier, result_offset, min, max, add_bias);
568 }
569
570protected:
571 template <typename U>
572 void fill(U &&tensor, int i)
573 {
574 // To avoid data all being clampped
575 std::uniform_int_distribution<> distribution(-500, 500);
576 library->fill(tensor, distribution, i);
577 }
578
579 TensorType compute_target(DataType data_type, const TensorShape &shape, float result_multiplier, int32_t result_offset, int32_t min, int32_t max, bool add_bias)
580 {
581 TensorShape shape_bias(shape[0]);
582
583 // Create tensors
584 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
585 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
586 TensorType c = create_tensor<TensorType>(shape, data_type, 1);
587
588 // create output stage info
589 GEMMLowpOutputStageInfo info;
590 info.gemmlowp_max_bound = max;
591 info.gemmlowp_min_bound = min;
592 info.gemmlowp_real_multiplier = result_multiplier;
593 info.gemmlowp_offset = result_offset;
594 info.type = GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT;
595 info.output_data_type = data_type;
596
597 // Create and configure function
598 FunctionType output_stage;
599 output_stage.configure(&a, add_bias ? &b : nullptr, &c, info);
600
601 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
602 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
603
604 // Allocate tensors
605 a.allocator()->allocate();
606 c.allocator()->allocate();
607
608 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
609 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
610
611 // Fill tensor
612 fill(AccessorType(a), 0);
613
614 if(add_bias)
615 {
616 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
617
618 // Allocate bias tensor
619 b.allocator()->allocate();
620
621 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
622
623 // Fill tensor
624 fill(AccessorType(b), 1);
625 }
626
627 // Compute GEMM function
628 output_stage.run();
629 return c;
630 }
631
632 SimpleTensor<T> compute_reference(const TensorShape &shape, float_t result_real_multiplier, int32_t result_offset, int32_t min, int32_t max, bool add_bias)
633 {
634 // Create reference
635 TensorShape shape_bias(shape[0]);
636
637 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
638 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
639
640 // Fill reference
641 fill(a, 0);
642
643 const std::vector<float_t> result_float_multiplier_vec = { result_real_multiplier };
644
645 if(add_bias)
646 {
647 // Fill bias
648 fill(b, 1);
649
650 return reference::gemmlowp_quantize_down_scale_by_float<int32_t, T>(a, b, result_float_multiplier_vec, result_offset, min, max);
651 }
652 else
653 {
654 return reference::gemmlowp_quantize_down_scale_by_float<int32_t, T>(a, result_float_multiplier_vec, result_offset, min, max);
655 }
656 }
657
658 TensorType _target{};
659 SimpleTensor<T> _reference{};
660};
661
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100662template <typename TensorType, typename AccessorType, typename FunctionType>
663class GEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointValidationFixture : public framework::Fixture
664{
665public:
666 template <typename...>
667 void setup(TensorShape shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
668 {
669 _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, min, max, add_bias);
670 _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, min, max, add_bias);
671 }
672
673protected:
674 template <typename U>
675 void fill(U &&tensor, int i)
676 {
677 std::uniform_int_distribution<> distribution(-6000, 6000);
678 library->fill(tensor, distribution, i);
679 }
680
681 TensorType compute_target(const TensorShape &shape, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t min, int32_t max, bool add_bias)
682 {
683 TensorShape shape_bias(shape[0]);
684
685 // Create tensors
686 TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1);
687 TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1);
688 TensorType c = create_tensor<TensorType>(shape, DataType::QSYMM16, 1);
689
690 // Create and configure function
691 FunctionType output_stage;
692 output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, min, max);
693
694 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
695 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
696
697 // Allocate tensors
698 a.allocator()->allocate();
699 c.allocator()->allocate();
700
701 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
702 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
703
704 // Fill tensor
705 fill(AccessorType(a), 0);
706
707 if(add_bias)
708 {
709 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
710
711 // Allocate bias tensor
712 b.allocator()->allocate();
713
714 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
715
716 // Fill tensor
717 fill(AccessorType(b), 1);
718 }
719
720 // Compute GEMM function
721 output_stage.run();
722 return c;
723 }
724
725 SimpleTensor<int16_t> compute_reference(const TensorShape &shape, int32_t result_fixed_point_multiplier, int32_t result_shift, int32_t min, int32_t max,
726 bool add_bias)
727 {
728 // Create reference
729 TensorShape shape_bias(shape[0]);
730
731 SimpleTensor<int32_t> a{ shape, DataType::S32, 1 };
732 SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 };
733
734 // Fill reference
735 fill(a, 0);
736
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000737 const std::vector<int32_t> result_fixed_point_multiplier_vec = { result_fixed_point_multiplier };
738 const std::vector<int32_t> result_shift_vec = { result_shift };
739
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100740 if(add_bias)
741 {
742 // Fill bias
743 fill(b, 1);
744
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000745 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 +0100746 }
747 else
748 {
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000749 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 +0100750 }
751 }
752
753 TensorType _target{};
754 SimpleTensor<int16_t> _reference{};
755};
756
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000757template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
758class GEMMLowpMatrixMultiplyReshapedValidationFixture : public framework::Fixture
759{
760public:
761 template <typename...>
762 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,
Sheri Zhang28287af2020-02-25 14:13:54 +0000763 bool interleave_rhs, DataType data_type)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000764 {
765 GEMMLHSMatrixInfo lhs_info;
766 lhs_info.m0 = m0;
767 lhs_info.k0 = k0;
768 lhs_info.v0 = v0;
769 lhs_info.interleave = interleave_lhs;
770 lhs_info.transpose = false;
771
772 GEMMRHSMatrixInfo rhs_info;
773 rhs_info.n0 = n0;
774 rhs_info.k0 = k0;
775 rhs_info.h0 = h0;
776 rhs_info.interleave = interleave_rhs;
777 rhs_info.transpose = true;
778
779 // Set the tensor shapes for LHS and RHS matrices
780 const TensorShape lhs_shape(k, m, batch_size);
781 const TensorShape rhs_shape(n, k, batch_size);
782
Sheri Zhang28287af2020-02-25 14:13:54 +0000783 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type);
784 _reference = compute_reference(lhs_shape, rhs_shape, data_type);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000785 }
786
787protected:
788 template <typename U>
789 void fill(U &&tensor, int i)
790 {
Sheri Zhang28287af2020-02-25 14:13:54 +0000791 switch(tensor.data_type())
792 {
793 case DataType::QASYMM8:
794 {
795 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
796 std::uniform_int_distribution<> distribution(1, 254);
797 library->fill(tensor, distribution, i);
798 }
799 break;
800 case DataType::QASYMM8_SIGNED:
801 {
802 std::uniform_int_distribution<> distribution(-127, 126);
803 library->fill(tensor, distribution, i);
804 }
805 break;
806 default:
807 ARM_COMPUTE_ERROR("Unsupported data type");
808 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000809 }
810
Sheri Zhang28287af2020-02-25 14:13:54 +0000811 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000812 {
813 // Create tensors
Sheri Zhang28287af2020-02-25 14:13:54 +0000814 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
815 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000816 TensorType lhs_reshaped;
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 ReshapeLHSFunctionType reshape_lhs;
828 ReshapeRHSFunctionType reshape_rhs;
829 GEMMFunctionType gemm;
830 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
831 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
832 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
833
834 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
835 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
836
837 // Allocate tensors
838 lhs.allocator()->allocate();
839 rhs.allocator()->allocate();
840 lhs_reshaped.allocator()->allocate();
841 rhs_reshaped.allocator()->allocate();
842 dst.allocator()->allocate();
843
844 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
845 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
846 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
847 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
848 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
849
850 // Fill tensors
851 fill(AccessorType(lhs), 0);
852 fill(AccessorType(rhs), 1);
853
854 // Compute GEMM
855 reshape_lhs.run();
856 reshape_rhs.run();
857 gemm.run();
858
859 return dst;
860 }
861
Sheri Zhang28287af2020-02-25 14:13:54 +0000862 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000863 {
864 TensorShape dst_shape = lhs_shape;
865 dst_shape[0] = rhs_shape[0];
866 dst_shape[1] = lhs_shape[1];
867
Sheri Zhang28287af2020-02-25 14:13:54 +0000868 switch(data_type)
869 {
870 case DataType::QASYMM8:
871 {
872 // Create reference
873 SimpleTensor<uint8_t> lhs{ lhs_shape, data_type, 1 };
874 SimpleTensor<uint8_t> rhs{ rhs_shape, data_type, 1 };
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000875
Sheri Zhang28287af2020-02-25 14:13:54 +0000876 // Fill reference
877 fill(lhs, 0);
878 fill(rhs, 1);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000879
Sheri Zhang28287af2020-02-25 14:13:54 +0000880 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
881 }
882 case DataType::QASYMM8_SIGNED:
883 {
884 // Create reference
885 SimpleTensor<int8_t> lhs{ lhs_shape, data_type, 1 };
886 SimpleTensor<int8_t> rhs{ rhs_shape, data_type, 1 };
887
888 // Fill reference
889 fill(lhs, 0);
890 fill(rhs, 1);
891
892 return reference::gemmlowp_matrix_multiply_core<int32_t, int8_t>(lhs, rhs, dst_shape, 0, 0);
893 }
894 default:
895 ARM_COMPUTE_ERROR("Unsupported data type");
896 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000897 }
898
899 TensorType _target{};
900 SimpleTensor<int32_t> _reference{};
901};
902
903template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
904class GEMMLowpMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture
905{
906public:
907 template <typename...>
908 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,
Sheri Zhang28287af2020-02-25 14:13:54 +0000909 bool interleave_lhs, bool interleave_rhs, DataType data_type)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000910 {
911 GEMMLHSMatrixInfo lhs_info;
912 lhs_info.m0 = m0;
913 lhs_info.k0 = k0;
914 lhs_info.v0 = v0;
915 lhs_info.interleave = interleave_lhs;
916 lhs_info.transpose = false;
917
918 GEMMRHSMatrixInfo rhs_info;
919 rhs_info.n0 = n0;
920 rhs_info.k0 = k0;
921 rhs_info.h0 = h0;
922 rhs_info.interleave = interleave_rhs;
923 rhs_info.transpose = true;
924
925 // In case of GEMM3D, m is the product between m_w and m_h
926 const unsigned int m = m_w * m_h;
927
928 // Set the tensor shapes for LHS and RHS matrices
929 const TensorShape lhs_shape(k, m, batch_size);
930 const TensorShape rhs_shape(n, k, batch_size);
931
Sheri Zhang28287af2020-02-25 14:13:54 +0000932 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h, data_type);
933 _reference = compute_reference(lhs_shape, rhs_shape, m_h, data_type);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000934 }
935
936protected:
937 template <typename U>
938 void fill(U &&tensor, int i)
939 {
Sheri Zhang28287af2020-02-25 14:13:54 +0000940 switch(tensor.data_type())
941 {
942 case DataType::QASYMM8:
943 {
944 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
945 std::uniform_int_distribution<> distribution(1, 254);
946 library->fill(tensor, distribution, i);
947 }
948 break;
949 case DataType::QASYMM8_SIGNED:
950 {
951 std::uniform_int_distribution<> distribution(-127, 126);
952 library->fill(tensor, distribution, i);
953 }
954 break;
955 default:
956 ARM_COMPUTE_ERROR("Unsupported data type");
957 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000958 }
959
Sheri Zhang28287af2020-02-25 14:13:54 +0000960 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h,
961 DataType data_type)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000962 {
963 // Create tensors
Sheri Zhang28287af2020-02-25 14:13:54 +0000964 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
965 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000966 TensorType lhs_reshaped;
967 TensorType rhs_reshaped;
968 TensorType dst;
969
970 const unsigned int M = lhs_shape[1];
971 const unsigned int N = rhs_shape[0];
972 const unsigned int K = lhs_shape[0];
973
974 // The output tensor will be auto-initialized within the function
975
976 // Create and configure function
977 ReshapeLHSFunctionType reshape_lhs;
978 ReshapeRHSFunctionType reshape_rhs;
979 GEMMFunctionType gemm;
980 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
981 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
982 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
983
984 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
985 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
986
987 // Allocate tensors
988 lhs.allocator()->allocate();
989 rhs.allocator()->allocate();
990 lhs_reshaped.allocator()->allocate();
991 rhs_reshaped.allocator()->allocate();
992 dst.allocator()->allocate();
993
994 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
995 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
996 ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
997 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
998 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
999
1000 // Fill tensors
1001 fill(AccessorType(lhs), 0);
1002 fill(AccessorType(rhs), 1);
1003
1004 // Compute GEMM
1005 reshape_lhs.run();
1006 reshape_rhs.run();
1007 gemm.run();
1008
1009 return dst;
1010 }
1011
Sheri Zhang28287af2020-02-25 14:13:54 +00001012 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h, DataType data_type)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +00001013 {
1014 TensorShape dst_shape = lhs_shape;
1015 dst_shape.set(0, rhs_shape[0]);
1016 dst_shape.set(1, lhs_shape[1] / m_h);
1017 dst_shape.set(2, m_h);
1018 dst_shape.set(3, lhs_shape[2]);
1019
Sheri Zhang28287af2020-02-25 14:13:54 +00001020 switch(data_type)
1021 {
1022 case DataType::QASYMM8:
1023 {
1024 // Create reference
1025 SimpleTensor<uint8_t> lhs{ lhs_shape, data_type, 1 };
1026 SimpleTensor<uint8_t> rhs{ rhs_shape, data_type, 1 };
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +00001027
Sheri Zhang28287af2020-02-25 14:13:54 +00001028 // Fill reference
1029 fill(lhs, 0);
1030 fill(rhs, 1);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +00001031
Sheri Zhang28287af2020-02-25 14:13:54 +00001032 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1033 }
1034 case DataType::QASYMM8_SIGNED:
1035 {
1036 // Create reference
1037 SimpleTensor<int8_t> lhs{ lhs_shape, data_type, 1 };
1038 SimpleTensor<int8_t> rhs{ rhs_shape, data_type, 1 };
1039
1040 // Fill reference
1041 fill(lhs, 0);
1042 fill(rhs, 1);
1043
1044 return reference::gemmlowp_matrix_multiply_core<int32_t, int8_t>(lhs, rhs, dst_shape, 0, 0);
1045 }
1046 default:
1047 ARM_COMPUTE_ERROR("Unsupported data type");
1048 }
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +00001049 }
1050
1051 TensorType _target{};
1052 SimpleTensor<int32_t> _reference{};
1053};
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001054
1055template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
1056class GEMMLowpMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture
1057{
1058public:
1059 template <typename...>
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001060 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0,
1061 unsigned int k0, unsigned int h0, bool interleave_rhs, bool transpose_rhs, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001062 {
1063 GEMMLHSMatrixInfo lhs_info;
1064 lhs_info.m0 = m0;
1065 lhs_info.k0 = k0;
1066
1067 GEMMRHSMatrixInfo rhs_info;
1068 rhs_info.n0 = n0;
1069 rhs_info.k0 = k0;
1070 rhs_info.h0 = h0;
1071 rhs_info.interleave = interleave_rhs;
1072 rhs_info.transpose = transpose_rhs;
1073
1074 // Set the tensor shapes for LHS and RHS matrices
1075 const TensorShape lhs_shape(k, m, batch_size);
1076 const TensorShape rhs_shape(n, k, batch_size);
1077
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001078 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type);
1079 _reference = compute_reference(lhs_shape, rhs_shape, data_type);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001080 }
1081
1082protected:
1083 template <typename U>
1084 void fill(U &&tensor, int i)
1085 {
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001086 switch(tensor.data_type())
1087 {
1088 case DataType::QASYMM8:
1089 {
1090 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1091 std::uniform_int_distribution<> distribution(1, 254);
1092 library->fill(tensor, distribution, i);
1093 }
1094 break;
1095 case DataType::QASYMM8_SIGNED:
1096 {
1097 std::uniform_int_distribution<> distribution(-127, 126);
1098 library->fill(tensor, distribution, i);
1099 }
1100 break;
1101 default:
1102 ARM_COMPUTE_ERROR("Unsupported data type");
1103 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001104 }
1105
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001106 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info,
1107 const GEMMRHSMatrixInfo &rhs_info, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001108 {
1109 // Create tensors
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001110 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
1111 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001112 TensorType rhs_reshaped;
1113 TensorType dst;
1114
1115 const unsigned int M = lhs_shape[1];
1116 const unsigned int N = rhs_shape[0];
1117 const unsigned int K = lhs_shape[0];
1118
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001119 GEMMKernelInfo gemm_info;
1120 gemm_info.m = M;
1121 gemm_info.n = N;
1122 gemm_info.k = K;
1123 gemm_info.lhs_info = lhs_info;
1124 gemm_info.rhs_info = rhs_info;
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001125 // The output tensor will be auto-initialized within the function
1126
1127 // Create and configure function
1128 ReshapeRHSFunctionType reshape_rhs;
1129 GEMMFunctionType gemm;
1130 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001131 gemm.configure(&lhs, &rhs_reshaped, &dst, gemm_info);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001132
1133 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1134 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1135
1136 // Allocate tensors
1137 lhs.allocator()->allocate();
1138 rhs.allocator()->allocate();
1139 rhs_reshaped.allocator()->allocate();
1140 dst.allocator()->allocate();
1141
1142 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1143 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1144 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
1145 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1146
1147 // Fill tensors
1148 fill(AccessorType(lhs), 0);
1149 fill(AccessorType(rhs), 1);
1150
1151 // Compute GEMM
1152 reshape_rhs.run();
1153 gemm.run();
1154
1155 return dst;
1156 }
1157
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001158 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001159 {
1160 TensorShape dst_shape = lhs_shape;
1161 dst_shape[0] = rhs_shape[0];
1162 dst_shape[1] = lhs_shape[1];
1163
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001164 if(data_type == DataType::QASYMM8)
1165 {
1166 // Create reference
1167 SimpleTensor<uint8_t> lhs{ lhs_shape, data_type, 1 };
1168 SimpleTensor<uint8_t> rhs{ rhs_shape, data_type, 1 };
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001169
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001170 // Fill reference
1171 fill(lhs, 0);
1172 fill(rhs, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001173
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001174 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1175 }
1176 else
1177 {
1178 // Create reference
1179 SimpleTensor<int8_t> lhs{ lhs_shape, data_type, 1 };
1180 SimpleTensor<int8_t> rhs{ rhs_shape, data_type, 1 };
1181
1182 // Fill reference
1183 fill(lhs, 0);
1184 fill(rhs, 1);
1185
1186 return reference::gemmlowp_matrix_multiply_core<int32_t, int8_t>(lhs, rhs, dst_shape, 0, 0);
1187 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001188 }
1189
1190 TensorType _target{};
1191 SimpleTensor<int32_t> _reference{};
1192};
1193
1194template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType>
1195class GEMMLowpMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture
1196{
1197public:
1198 template <typename...>
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001199 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,
1200 unsigned int k0, unsigned int h0, bool interleave_rhs, bool transpose_rhs, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001201 {
1202 GEMMLHSMatrixInfo lhs_info;
1203 lhs_info.m0 = m0;
1204 lhs_info.k0 = k0;
1205
1206 GEMMRHSMatrixInfo rhs_info;
1207 rhs_info.n0 = n0;
1208 rhs_info.k0 = k0;
1209 rhs_info.h0 = h0;
1210 rhs_info.interleave = interleave_rhs;
1211 rhs_info.transpose = transpose_rhs;
1212
1213 // In case of GEMM3D, m is the product between m_w and m_h
1214 const unsigned int m = m_w * m_h;
1215
1216 // Set the tensor shapes for LHS and RHS matrices
1217 const TensorShape lhs_shape(k, m, batch_size);
1218 const TensorShape rhs_shape(n, k, batch_size);
1219
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001220 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h, data_type);
1221 _reference = compute_reference(lhs_shape, rhs_shape, m_h, data_type);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001222 }
1223
1224protected:
1225 template <typename U>
1226 void fill(U &&tensor, int i)
1227 {
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001228 switch(tensor.data_type())
1229 {
1230 case DataType::QASYMM8:
1231 {
1232 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1233 std::uniform_int_distribution<> distribution(1, 254);
1234 library->fill(tensor, distribution, i);
1235 }
1236 break;
1237 case DataType::QASYMM8_SIGNED:
1238 {
1239 std::uniform_int_distribution<> distribution(-127, 126);
1240 library->fill(tensor, distribution, i);
1241 }
1242 break;
1243 default:
1244 ARM_COMPUTE_ERROR("Unsupported data type");
1245 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001246 }
1247
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001248 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info,
1249 const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h, DataType data_type)
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001250 {
1251 // Create tensors
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001252 TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1);
1253 TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001254 TensorType rhs_reshaped;
1255 TensorType dst;
1256
1257 const unsigned int M = lhs_shape[1];
1258 const unsigned int N = rhs_shape[0];
1259 const unsigned int K = lhs_shape[0];
1260
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001261 GEMMKernelInfo gemm_info;
1262 gemm_info.m = M;
1263 gemm_info.n = N;
1264 gemm_info.k = K;
1265 gemm_info.depth_output_gemm3d = m_h;
1266 gemm_info.lhs_info = lhs_info;
1267 gemm_info.rhs_info = rhs_info;
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001268 // The output tensor will be auto-initialized within the function
1269
1270 // Create and configure function
1271 ReshapeRHSFunctionType reshape_rhs;
1272 GEMMFunctionType gemm;
1273 reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001274 gemm.configure(&lhs, &rhs_reshaped, &dst, gemm_info);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001275
1276 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1277 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1278
1279 // Allocate tensors
1280 lhs.allocator()->allocate();
1281 rhs.allocator()->allocate();
1282 rhs_reshaped.allocator()->allocate();
1283 dst.allocator()->allocate();
1284
1285 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1286 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1287 ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS);
1288 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1289
1290 // Fill tensors
1291 fill(AccessorType(lhs), 0);
1292 fill(AccessorType(rhs), 1);
1293
1294 // Compute GEMM
1295 reshape_rhs.run();
1296 gemm.run();
1297
1298 return dst;
1299 }
1300
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001301 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 +00001302 {
1303 TensorShape dst_shape = lhs_shape;
1304 dst_shape.set(0, rhs_shape[0]);
1305 dst_shape.set(1, lhs_shape[1] / m_h);
1306 dst_shape.set(2, m_h);
1307 dst_shape.set(3, lhs_shape[2]);
1308
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001309 if(data_type == DataType::QASYMM8)
1310 {
1311 // Create reference
1312 SimpleTensor<uint8_t> lhs{ lhs_shape, data_type, 1 };
1313 SimpleTensor<uint8_t> rhs{ rhs_shape, data_type, 1 };
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001314
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001315 // Fill reference
1316 fill(lhs, 0);
1317 fill(rhs, 1);
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001318
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001319 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1320 }
1321 else
1322 {
1323 // Create reference
1324 SimpleTensor<int8_t> lhs{ lhs_shape, data_type, 1 };
1325 SimpleTensor<int8_t> rhs{ rhs_shape, data_type, 1 };
1326
1327 // Fill reference
1328 fill(lhs, 0);
1329 fill(rhs, 1);
1330
1331 return reference::gemmlowp_matrix_multiply_core<int32_t, int8_t>(lhs, rhs, dst_shape, 0, 0);
1332 }
Gian Marco Iodice62251f72019-03-11 16:07:12 +00001333 }
1334
1335 TensorType _target{};
1336 SimpleTensor<int32_t> _reference{};
1337};
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001338
1339template <typename TensorType, typename AccessorType, typename GEMMFunctionType>
1340class GEMMLowpMatrixMultiplyNativeValidationFixture : public framework::Fixture
1341{
1342public:
1343 template <typename...>
1344 void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0)
1345 {
1346 GEMMLHSMatrixInfo lhs_info;
1347 lhs_info.m0 = m0;
1348 lhs_info.k0 = k0;
1349
1350 GEMMRHSMatrixInfo rhs_info;
1351 rhs_info.n0 = n0;
1352 rhs_info.k0 = k0;
1353
1354 // Set the tensor shapes for LHS and RHS matrices
1355 const TensorShape lhs_shape(k, m, batch_size);
1356 const TensorShape rhs_shape(n, k, batch_size);
1357
1358 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info);
1359 _reference = compute_reference(lhs_shape, rhs_shape);
1360 }
1361
1362protected:
1363 template <typename U>
1364 void fill(U &&tensor, int i)
1365 {
1366 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1367 std::uniform_int_distribution<> distribution(1, 254);
1368 library->fill(tensor, distribution, i);
1369 }
1370
1371 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info)
1372 {
1373 // Create tensors
1374 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
1375 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
1376 TensorType dst;
1377
1378 const unsigned int M = lhs_shape[1];
1379 const unsigned int N = rhs_shape[0];
1380 const unsigned int K = lhs_shape[0];
1381
1382 // The output tensor will be auto-initialized within the function
1383
1384 // Create and configure function
1385 GEMMFunctionType gemm;
1386 gemm.configure(&lhs, &rhs, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K));
1387
1388 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1389 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1390
1391 // Allocate tensors
1392 lhs.allocator()->allocate();
1393 rhs.allocator()->allocate();
1394 dst.allocator()->allocate();
1395
1396 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1397 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1398 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1399
1400 // Fill tensors
1401 fill(AccessorType(lhs), 0);
1402 fill(AccessorType(rhs), 1);
1403
1404 // Compute GEMM
1405 gemm.run();
1406
1407 return dst;
1408 }
1409
1410 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape)
1411 {
1412 TensorShape dst_shape = lhs_shape;
1413 dst_shape[0] = rhs_shape[0];
1414 dst_shape[1] = lhs_shape[1];
1415
1416 // Create reference
1417 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
1418 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
1419
1420 // Fill reference
1421 fill(lhs, 0);
1422 fill(rhs, 1);
1423
1424 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1425 }
1426
1427 TensorType _target{};
1428 SimpleTensor<int32_t> _reference{};
1429};
1430
1431template <typename TensorType, typename AccessorType, typename GEMMFunctionType>
1432class GEMMLowpMatrixMultiplyNative3DValidationFixture : public framework::Fixture
1433{
1434public:
1435 template <typename...>
1436 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)
1437 {
1438 GEMMLHSMatrixInfo lhs_info;
1439 lhs_info.m0 = m0;
1440 lhs_info.k0 = k0;
1441
1442 GEMMRHSMatrixInfo rhs_info;
1443 rhs_info.n0 = n0;
1444 rhs_info.k0 = k0;
1445
1446 // In case of GEMM3D, m is the product between m_w and m_h
1447 const unsigned int m = m_w * m_h;
1448
1449 // Set the tensor shapes for LHS and RHS matrices
1450 const TensorShape lhs_shape(k, m, batch_size);
1451 const TensorShape rhs_shape(n, k, batch_size);
1452
1453 _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h);
1454 _reference = compute_reference(lhs_shape, rhs_shape, m_h);
1455 }
1456
1457protected:
1458 template <typename U>
1459 void fill(U &&tensor, int i)
1460 {
1461 // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path
1462 std::uniform_int_distribution<> distribution(1, 254);
1463 library->fill(tensor, distribution, i);
1464 }
1465
1466 TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h)
1467 {
1468 // Create tensors
1469 TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1);
1470 TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1);
1471 TensorType dst;
1472
1473 const unsigned int M = lhs_shape[1];
1474 const unsigned int N = rhs_shape[0];
1475 const unsigned int K = lhs_shape[0];
1476
1477 // The output tensor will be auto-initialized within the function
1478
1479 // Create and configure function
1480 GEMMFunctionType gemm;
1481 gemm.configure(&lhs, &rhs, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h));
1482
1483 ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1484 ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1485
1486 // Allocate tensors
1487 lhs.allocator()->allocate();
1488 rhs.allocator()->allocate();
1489 dst.allocator()->allocate();
1490
1491 ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1492 ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS);
1493 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
1494
1495 // Fill tensors
1496 fill(AccessorType(lhs), 0);
1497 fill(AccessorType(rhs), 1);
1498
1499 // Compute GEMM
1500 gemm.run();
1501
1502 return dst;
1503 }
1504
1505 SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h)
1506 {
1507 TensorShape dst_shape = lhs_shape;
1508 dst_shape.set(0, rhs_shape[0]);
1509 dst_shape.set(1, lhs_shape[1] / m_h);
1510 dst_shape.set(2, m_h);
1511 dst_shape.set(3, lhs_shape[2]);
1512
1513 // Create reference
1514 SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 };
1515 SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 };
1516
1517 // Fill reference
1518 fill(lhs, 0);
1519 fill(rhs, 1);
1520
1521 return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0);
1522 }
1523
1524 TensorType _target{};
1525 SimpleTensor<int32_t> _reference{};
1526};
Pablo Tello299025a2017-09-29 11:30:12 +01001527} // namespace validation
1528} // namespace test
1529} // namespace arm_compute
George Wort2d7e6832019-02-22 16:37:41 +00001530#endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */