Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 3 | * |
| 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 Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/KernelDescriptors.h" |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorShape.h" |
| 29 | #include "arm_compute/core/Types.h" |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 31 | #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 Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 36 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 37 | #include "tests/validation/reference/GEMMLowp.h" |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 38 | |
| 39 | #include <random> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace test |
| 44 | { |
| 45 | namespace validation |
| 46 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 47 | namespace |
| 48 | { |
| 49 | template <typename U> |
| 50 | void fill(U &&tensor, int i) |
| 51 | { |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 52 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | template <typename TensorType, typename AccessorType, typename FunctionType, bool reinterpret_input_as_3d, bool reinterpret_output_as_3d, typename OutputType, bool is_fused = false> |
| 94 | TensorType compute_gemmlowp_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset, |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 95 | GEMMLowpOutputStageInfo output_stage = GEMMLowpOutputStageInfo(), DataType data_type_a = DataType::QASYMM8, DataType data_type_b = DataType::QASYMM8, |
| 96 | QuantizationInfo b_qinfo = QuantizationInfo()) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 97 | { |
| 98 | // Create tensors |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 99 | 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 Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 102 | 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 Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 103 | TensorType output = create_tensor<TensorType>(shape_output, data_type_output, 1); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 104 | |
| 105 | a.info()->set_quantization_info(QuantizationInfo(1.0f / 255, a_offset)); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 106 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 107 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 115 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 152 | // Compute GEMM function |
| 153 | gemmlowp.run(); |
| 154 | return output; |
| 155 | } |
| 156 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 157 | template <bool reinterpret_input_as_3d, typename TI = uint8_t, typename TW = uint8_t> |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 158 | SimpleTensor<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 Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 159 | DataType data_type_a = DataType::QASYMM8, DataType data_type_b = DataType::QASYMM8, QuantizationInfo b_qinfo = QuantizationInfo()) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 160 | { |
| 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 Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 169 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 171 | |
| 172 | // Fill reference |
| 173 | fill(a, 0); |
| 174 | fill(b, 1); |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 175 | return reference::gemmlowp_matrix_multiply_core<int32_t, TI, TW>(a, b, shape_output, a_offset, b_offset); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 179 | template <typename TensorType, typename AccessorType, typename FunctionType, bool reinterpret_input_as_3d = false, bool reinterpret_output_as_3d = false> |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 180 | class GEMMLowpMatrixMultiplyCoreValidationFixture : public framework::Fixture |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 181 | { |
| 182 | public: |
| 183 | template <typename...> |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 184 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_output, int32_t a_offset, int32_t b_offset) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 185 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 186 | _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 Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | protected: |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 191 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_output, int32_t a_offset, int32_t b_offset) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 192 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 193 | 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 Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 194 | } |
| 195 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 196 | 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 Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 197 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 198 | return compute_gemmlowp_reference<reinterpret_input_as_3d>(shape_a, shape_b, shape_output, a_offset, b_offset); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 201 | TensorType _target{}; |
| 202 | SimpleTensor<int32_t> _reference{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 203 | }; |
| 204 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 205 | template <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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 206 | class GEMMLowpMatrixMultiplyCoreFusedOffsetOutputValidationFixture : public framework::Fixture |
| 207 | { |
| 208 | public: |
| 209 | template <typename...> |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 210 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 211 | { |
| 212 | ARM_COMPUTE_EXPECT(output_stage.type != GEMMLowpOutputStageType::NONE, framework::LogLevel::ERRORS); |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 213 | DataType data_type_a = data_type_b == DataType::QASYMM8_SIGNED ? DataType::QASYMM8_SIGNED : DataType::QASYMM8; |
| 214 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 215 | 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 Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 226 | quantization::calculate_quantized_multiplier(scales[i], &output_stage.gemmlowp_multipliers[i], &output_stage.gemmlowp_shifts[i]); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 229 | _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 Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 234 | _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 Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 236 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | protected: |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 240 | 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 Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 241 | DataType data_type_a, DataType data_type_b, QuantizationInfo b_qinfo) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 242 | { |
| 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 Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 244 | output_stage, data_type_a, data_type_b, b_qinfo); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 247 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 249 | { |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 250 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 251 | |
| 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 Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 259 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 261 | break; |
| 262 | case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT: |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 263 | 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 Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 265 | break; |
| 266 | default: |
| 267 | ARM_COMPUTE_ERROR("Not Supported!"); |
| 268 | } |
| 269 | } |
| 270 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 271 | TensorType _target{}; |
| 272 | SimpleTensor<TI> _reference{}; |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 275 | template <typename TensorType, typename AccessorType, typename FunctionType> |
| 276 | class GEMMLowpQuantizeDownInt32ToUint8ScaleValidationFixture : public framework::Fixture |
| 277 | { |
| 278 | public: |
| 279 | template <typename...> |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 280 | 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 Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 281 | { |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 282 | _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 Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | protected: |
| 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 Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 294 | 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 Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 295 | { |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 296 | TensorShape shape_bias(shape[0]); |
| 297 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 298 | // Create tensors |
| 299 | TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 300 | TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1); |
| 301 | TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 302 | |
| 303 | // Create and configure function |
| 304 | FunctionType output_stage; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 305 | output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_offset, result_mult_int, result_shift, min, max); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 306 | |
| 307 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 308 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 309 | |
| 310 | // Allocate tensors |
| 311 | a.allocator()->allocate(); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 312 | c.allocator()->allocate(); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 313 | |
| 314 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 315 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 316 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 317 | // Fill tensor |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 318 | fill(AccessorType(a), 0); |
| 319 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 320 | 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 Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 333 | // Compute GEMM function |
| 334 | output_stage.run(); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 335 | return c; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 338 | 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 Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 339 | { |
| 340 | // Create reference |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 341 | TensorShape shape_bias(shape[0]); |
| 342 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 343 | SimpleTensor<int32_t> a{ shape, DataType::S32, 1 }; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 344 | SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 }; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 345 | |
| 346 | // Fill reference |
| 347 | fill(a, 0); |
| 348 | |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 349 | 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 Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 352 | if(add_bias) |
| 353 | { |
| 354 | // Fill bias |
| 355 | fill(b, 1); |
| 356 | |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 357 | return reference::gemmlowp_quantize_down_scale<int32_t, uint8_t>(a, b, result_offset, result_mult_int_vec, result_shift_vec, min, max); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 358 | } |
| 359 | else |
| 360 | { |
Manuel Bottini | 959c26d | 2019-12-02 16:22:35 +0000 | [diff] [blame] | 361 | return reference::gemmlowp_quantize_down_scale<int32_t, uint8_t>(a, result_offset, result_mult_int_vec, result_shift_vec, min, max); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 362 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | TensorType _target{}; |
| 366 | SimpleTensor<uint8_t> _reference{}; |
| 367 | }; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 368 | |
| 369 | template <typename TensorType, typename AccessorType, typename FunctionType> |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 370 | class GEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointValidationFixture : public framework::Fixture |
| 371 | { |
| 372 | public: |
| 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 | |
| 380 | protected: |
| 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 | |
| 464 | template <typename TensorType, typename AccessorType, typename FunctionType> |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 465 | class GEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointValidationFixture : public framework::Fixture |
| 466 | { |
| 467 | public: |
| 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 | |
| 475 | protected: |
| 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 Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 539 | 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 Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 542 | if(add_bias) |
| 543 | { |
| 544 | // Fill bias |
| 545 | fill(b, 1); |
| 546 | |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 547 | 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 Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 548 | } |
| 549 | else |
| 550 | { |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 551 | 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 Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | TensorType _target{}; |
| 556 | SimpleTensor<uint8_t> _reference{}; |
| 557 | }; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 558 | |
Sheri Zhang | 1b14c75 | 2020-03-09 14:29:52 +0000 | [diff] [blame^] | 559 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 560 | class GEMMLowpQuantizeDownInt32ScaleByFloatValidationFixture : public framework::Fixture |
| 561 | { |
| 562 | public: |
| 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 | |
| 570 | protected: |
| 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 Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 662 | template <typename TensorType, typename AccessorType, typename FunctionType> |
| 663 | class GEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointValidationFixture : public framework::Fixture |
| 664 | { |
| 665 | public: |
| 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 | |
| 673 | protected: |
| 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 Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 737 | 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 Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 740 | if(add_bias) |
| 741 | { |
| 742 | // Fill bias |
| 743 | fill(b, 1); |
| 744 | |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 745 | 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 Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 746 | } |
| 747 | else |
| 748 | { |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 749 | 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 Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | TensorType _target{}; |
| 754 | SimpleTensor<int16_t> _reference{}; |
| 755 | }; |
| 756 | |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 757 | template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 758 | class GEMMLowpMatrixMultiplyReshapedValidationFixture : public framework::Fixture |
| 759 | { |
| 760 | public: |
| 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 Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 763 | bool interleave_rhs, DataType data_type) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 764 | { |
| 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 Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 783 | _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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | protected: |
| 788 | template <typename U> |
| 789 | void fill(U &&tensor, int i) |
| 790 | { |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 791 | 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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 811 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 812 | { |
| 813 | // Create tensors |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 814 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 815 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 816 | 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 Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 862 | SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 863 | { |
| 864 | TensorShape dst_shape = lhs_shape; |
| 865 | dst_shape[0] = rhs_shape[0]; |
| 866 | dst_shape[1] = lhs_shape[1]; |
| 867 | |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 868 | 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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 875 | |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 876 | // Fill reference |
| 877 | fill(lhs, 0); |
| 878 | fill(rhs, 1); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 879 | |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 880 | 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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | TensorType _target{}; |
| 900 | SimpleTensor<int32_t> _reference{}; |
| 901 | }; |
| 902 | |
| 903 | template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 904 | class GEMMLowpMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture |
| 905 | { |
| 906 | public: |
| 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 Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 909 | bool interleave_lhs, bool interleave_rhs, DataType data_type) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 910 | { |
| 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 Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 932 | _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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | protected: |
| 937 | template <typename U> |
| 938 | void fill(U &&tensor, int i) |
| 939 | { |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 940 | 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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 960 | 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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 962 | { |
| 963 | // Create tensors |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 964 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 965 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 966 | 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 Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 1012 | SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h, DataType data_type) |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 1013 | { |
| 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 Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 1020 | 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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 1027 | |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 1028 | // Fill reference |
| 1029 | fill(lhs, 0); |
| 1030 | fill(rhs, 1); |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 1031 | |
Sheri Zhang | 28287af | 2020-02-25 14:13:54 +0000 | [diff] [blame] | 1032 | 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 Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | TensorType _target{}; |
| 1052 | SimpleTensor<int32_t> _reference{}; |
| 1053 | }; |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1054 | |
| 1055 | template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 1056 | class GEMMLowpMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture |
| 1057 | { |
| 1058 | public: |
| 1059 | template <typename...> |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1060 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1062 | { |
| 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 Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1078 | _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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | protected: |
| 1083 | template <typename U> |
| 1084 | void fill(U &&tensor, int i) |
| 1085 | { |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1086 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1106 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1108 | { |
| 1109 | // Create tensors |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1110 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1111 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1112 | 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 Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 1119 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1125 | // 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 Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 1131 | gemm.configure(&lhs, &rhs_reshaped, &dst, gemm_info); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1132 | |
| 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 Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1158 | SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type) |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1159 | { |
| 1160 | TensorShape dst_shape = lhs_shape; |
| 1161 | dst_shape[0] = rhs_shape[0]; |
| 1162 | dst_shape[1] = lhs_shape[1]; |
| 1163 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1164 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1169 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1170 | // Fill reference |
| 1171 | fill(lhs, 0); |
| 1172 | fill(rhs, 1); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1173 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1174 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | TensorType _target{}; |
| 1191 | SimpleTensor<int32_t> _reference{}; |
| 1192 | }; |
| 1193 | |
| 1194 | template <typename TensorType, typename AccessorType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 1195 | class GEMMLowpMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture |
| 1196 | { |
| 1197 | public: |
| 1198 | template <typename...> |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1199 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1201 | { |
| 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 Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1220 | _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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | protected: |
| 1225 | template <typename U> |
| 1226 | void fill(U &&tensor, int i) |
| 1227 | { |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1228 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1248 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1250 | { |
| 1251 | // Create tensors |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1252 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1253 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1254 | 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 Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 1261 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1268 | // 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 Giorgio | b54ba28 | 2020-01-14 15:31:55 +0000 | [diff] [blame] | 1274 | gemm.configure(&lhs, &rhs_reshaped, &dst, gemm_info); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1275 | |
| 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 Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1301 | SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h, DataType data_type) |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1302 | { |
| 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 Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1309 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1314 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1315 | // Fill reference |
| 1316 | fill(lhs, 0); |
| 1317 | fill(rhs, 1); |
Gian Marco Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1318 | |
Michele Di Giorgio | f9179d3 | 2019-11-27 16:17:30 +0000 | [diff] [blame] | 1319 | 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 Iodice | 62251f7 | 2019-03-11 16:07:12 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | TensorType _target{}; |
| 1336 | SimpleTensor<int32_t> _reference{}; |
| 1337 | }; |
Gian Marco Iodice | e751062 | 2019-06-03 17:28:17 +0100 | [diff] [blame] | 1338 | |
| 1339 | template <typename TensorType, typename AccessorType, typename GEMMFunctionType> |
| 1340 | class GEMMLowpMatrixMultiplyNativeValidationFixture : public framework::Fixture |
| 1341 | { |
| 1342 | public: |
| 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 | |
| 1362 | protected: |
| 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 | |
| 1431 | template <typename TensorType, typename AccessorType, typename GEMMFunctionType> |
| 1432 | class GEMMLowpMatrixMultiplyNative3DValidationFixture : public framework::Fixture |
| 1433 | { |
| 1434 | public: |
| 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 | |
| 1457 | protected: |
| 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 Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 1527 | } // namespace validation |
| 1528 | } // namespace test |
| 1529 | } // namespace arm_compute |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 1530 | #endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */ |