Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 34 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/GEMMLowp.h" |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 45 | 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] | 46 | class GEMMLowpMatrixMultiplyCoreValidationFixture : public framework::Fixture |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | template <typename...> |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 50 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, int32_t a_offset, int32_t b_offset) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 51 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 52 | _target = compute_target(shape_a, shape_b, shape_c, a_offset, b_offset); |
| 53 | _reference = compute_reference(shape_a, shape_b, shape_c, a_offset, b_offset); |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | protected: |
| 57 | template <typename U> |
| 58 | void fill(U &&tensor, int i) |
| 59 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 60 | // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path |
| 61 | std::uniform_int_distribution<> distribution(1, 254); |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 62 | library->fill(tensor, distribution, i); |
| 63 | } |
| 64 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 65 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, int32_t a_offset, int32_t b_offset) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 66 | { |
| 67 | // Create tensors |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 68 | TensorType a = create_tensor<TensorType>(shape_a, DataType::QASYMM8, 1); |
| 69 | TensorType b = create_tensor<TensorType>(shape_b, DataType::QASYMM8, 1); |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 70 | TensorType c = create_tensor<TensorType>(shape_c, DataType::S32, 1); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 71 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 72 | a.info()->set_quantization_info(QuantizationInfo(1.0f / 255, a_offset)); |
| 73 | b.info()->set_quantization_info(QuantizationInfo(1.0f / 255, b_offset)); |
| 74 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 75 | // Create and configure function |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 76 | // The GEMMinfo includes the values of the depth in case of reinterpreted 3d input/output |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 77 | FunctionType gemmlowp; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 78 | // TODO (COMPMID-1672) - Extending the test to validate add bias in offset contribution |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 79 | gemmlowp.configure(&a, &b, nullptr, &c, GEMMInfo(false, false, false, (reinterpret_output_as_3d ? shape_c[2] : 0), reinterpret_input_as_3d)); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 80 | |
| 81 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 82 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 83 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 84 | |
| 85 | // Allocate tensors |
| 86 | a.allocator()->allocate(); |
| 87 | b.allocator()->allocate(); |
| 88 | c.allocator()->allocate(); |
| 89 | |
| 90 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 91 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 93 | |
| 94 | // Fill tensors |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 95 | fill(AccessorType(a), 0); |
| 96 | fill(AccessorType(b), 1); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 97 | |
| 98 | // Compute GEMM function |
| 99 | gemmlowp.run(); |
| 100 | return c; |
| 101 | } |
| 102 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 103 | SimpleTensor<int32_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, int32_t a_offset, int32_t b_offset) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 104 | { |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 105 | TensorShape shape_a_to_use = shape_a; |
| 106 | if(reinterpret_input_as_3d) |
| 107 | { |
| 108 | // Collapse the second and third dimension if the input is 3D |
| 109 | shape_a_to_use.collapse(2U, 1U); |
| 110 | } |
| 111 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 112 | // Create reference |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 113 | SimpleTensor<uint8_t> a{ shape_a_to_use, DataType::QASYMM8, 1 }; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 114 | SimpleTensor<uint8_t> b{ shape_b, DataType::QASYMM8, 1 }; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 115 | |
| 116 | // Fill reference |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 117 | fill(a, 0); |
| 118 | fill(b, 1); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 119 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 120 | return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(a, b, shape_c, a_offset, b_offset); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 123 | TensorType _target{}; |
| 124 | SimpleTensor<int32_t> _reference{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 125 | }; |
| 126 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 127 | template <typename TensorType, typename AccessorType, typename FunctionType> |
| 128 | class GEMMLowpQuantizeDownInt32ToUint8ScaleValidationFixture : public framework::Fixture |
| 129 | { |
| 130 | public: |
| 131 | template <typename...> |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 132 | 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] | 133 | { |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 134 | _target = compute_target(shape, result_offset, result_mult_int, result_shift, min, max, add_bias); |
| 135 | _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] | 136 | } |
| 137 | |
| 138 | protected: |
| 139 | template <typename U> |
| 140 | void fill(U &&tensor, int i) |
| 141 | { |
| 142 | std::uniform_int_distribution<> distribution(-6000, 6000); |
| 143 | library->fill(tensor, distribution, i); |
| 144 | } |
| 145 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 146 | 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] | 147 | { |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 148 | TensorShape shape_bias(shape[0]); |
| 149 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 150 | // Create tensors |
| 151 | TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 152 | TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1); |
| 153 | TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 154 | |
| 155 | // Create and configure function |
| 156 | FunctionType output_stage; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 157 | 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] | 158 | |
| 159 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 160 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 161 | |
| 162 | // Allocate tensors |
| 163 | a.allocator()->allocate(); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 164 | c.allocator()->allocate(); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 165 | |
| 166 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 167 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 168 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 169 | // Fill tensor |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 170 | fill(AccessorType(a), 0); |
| 171 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 172 | if(add_bias) |
| 173 | { |
| 174 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 175 | |
| 176 | // Allocate bias tensor |
| 177 | b.allocator()->allocate(); |
| 178 | |
| 179 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 180 | |
| 181 | // Fill tensor |
| 182 | fill(AccessorType(b), 1); |
| 183 | } |
| 184 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 185 | // Compute GEMM function |
| 186 | output_stage.run(); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 187 | return c; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 190 | 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] | 191 | { |
| 192 | // Create reference |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 193 | TensorShape shape_bias(shape[0]); |
| 194 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 195 | SimpleTensor<int32_t> a{ shape, DataType::S32, 1 }; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 196 | SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 }; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 197 | |
| 198 | // Fill reference |
| 199 | fill(a, 0); |
| 200 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 201 | if(add_bias) |
| 202 | { |
| 203 | // Fill bias |
| 204 | fill(b, 1); |
| 205 | |
| 206 | return reference::gemmlowp_quantize_down_int32_to_uint8_scale<int32_t>(a, b, result_offset, result_mult_int, result_shift, min, max); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | return reference::gemmlowp_quantize_down_int32_to_uint8_scale<int32_t>(a, result_offset, result_mult_int, result_shift, min, max); |
| 211 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TensorType _target{}; |
| 215 | SimpleTensor<uint8_t> _reference{}; |
| 216 | }; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 217 | |
| 218 | template <typename TensorType, typename AccessorType, typename FunctionType> |
| 219 | class GEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointValidationFixture : public framework::Fixture |
| 220 | { |
| 221 | public: |
| 222 | template <typename...> |
| 223 | 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) |
| 224 | { |
| 225 | _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias); |
| 226 | _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias); |
| 227 | } |
| 228 | |
| 229 | protected: |
| 230 | template <typename U> |
| 231 | void fill(U &&tensor, int i) |
| 232 | { |
| 233 | std::uniform_int_distribution<> distribution(-6000, 6000); |
| 234 | library->fill(tensor, distribution, i); |
| 235 | } |
| 236 | |
| 237 | 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) |
| 238 | { |
| 239 | TensorShape shape_bias(shape[0]); |
| 240 | |
| 241 | // Create tensors |
| 242 | TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1); |
| 243 | TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1); |
| 244 | TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1); |
| 245 | |
| 246 | // Create and configure function |
| 247 | FunctionType output_stage; |
| 248 | output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max); |
| 249 | |
| 250 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 251 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 252 | |
| 253 | // Allocate tensors |
| 254 | a.allocator()->allocate(); |
| 255 | c.allocator()->allocate(); |
| 256 | |
| 257 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 258 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 259 | |
| 260 | // Fill tensor |
| 261 | fill(AccessorType(a), 0); |
| 262 | |
| 263 | if(add_bias) |
| 264 | { |
| 265 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 266 | |
| 267 | // Allocate bias tensor |
| 268 | b.allocator()->allocate(); |
| 269 | |
| 270 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 271 | |
| 272 | // Fill tensor |
| 273 | fill(AccessorType(b), 1); |
| 274 | } |
| 275 | |
| 276 | // Compute GEMM function |
| 277 | output_stage.run(); |
| 278 | return c; |
| 279 | } |
| 280 | |
| 281 | 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, |
| 282 | bool add_bias) |
| 283 | { |
| 284 | // Create reference |
| 285 | TensorShape shape_bias(shape[0]); |
| 286 | |
| 287 | SimpleTensor<int32_t> a{ shape, DataType::S32, 1 }; |
| 288 | SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 }; |
| 289 | |
| 290 | // Fill reference |
| 291 | fill(a, 0); |
| 292 | |
| 293 | if(add_bias) |
| 294 | { |
| 295 | // Fill bias |
| 296 | fill(b, 1); |
| 297 | |
| 298 | return reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(a, b, result_fixed_point_multiplier, result_shift, result_offset_after_shift, min, max); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | return reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(a, result_fixed_point_multiplier, result_shift, result_offset_after_shift, min, max); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | TensorType _target{}; |
| 307 | SimpleTensor<uint8_t> _reference{}; |
| 308 | }; |
Gian Marco Iodice | db63b9c | 2019-01-17 09:47:04 +0000 | [diff] [blame] | 309 | |
| 310 | template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 311 | class GEMMLowpMatrixMultiplyReshapedValidationFixture : public framework::Fixture |
| 312 | { |
| 313 | public: |
| 314 | template <typename...> |
| 315 | 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, |
| 316 | bool interleave_rhs) |
| 317 | { |
| 318 | GEMMLHSMatrixInfo lhs_info; |
| 319 | lhs_info.m0 = m0; |
| 320 | lhs_info.k0 = k0; |
| 321 | lhs_info.v0 = v0; |
| 322 | lhs_info.interleave = interleave_lhs; |
| 323 | lhs_info.transpose = false; |
| 324 | |
| 325 | GEMMRHSMatrixInfo rhs_info; |
| 326 | rhs_info.n0 = n0; |
| 327 | rhs_info.k0 = k0; |
| 328 | rhs_info.h0 = h0; |
| 329 | rhs_info.interleave = interleave_rhs; |
| 330 | rhs_info.transpose = true; |
| 331 | |
| 332 | // Set the tensor shapes for LHS and RHS matrices |
| 333 | const TensorShape lhs_shape(k, m, batch_size); |
| 334 | const TensorShape rhs_shape(n, k, batch_size); |
| 335 | |
| 336 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info); |
| 337 | _reference = compute_reference(lhs_shape, rhs_shape); |
| 338 | } |
| 339 | |
| 340 | protected: |
| 341 | template <typename U> |
| 342 | void fill(U &&tensor, int i) |
| 343 | { |
| 344 | // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path |
| 345 | std::uniform_int_distribution<> distribution(1, 254); |
| 346 | library->fill(tensor, distribution, i); |
| 347 | } |
| 348 | |
| 349 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info) |
| 350 | { |
| 351 | // Create tensors |
| 352 | TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1); |
| 353 | TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1); |
| 354 | TensorType lhs_reshaped; |
| 355 | TensorType rhs_reshaped; |
| 356 | TensorType dst; |
| 357 | |
| 358 | const unsigned int M = lhs_shape[1]; |
| 359 | const unsigned int N = rhs_shape[0]; |
| 360 | const unsigned int K = lhs_shape[0]; |
| 361 | |
| 362 | // The output tensor will be auto-initialized within the function |
| 363 | |
| 364 | // Create and configure function |
| 365 | ReshapeLHSFunctionType reshape_lhs; |
| 366 | ReshapeRHSFunctionType reshape_rhs; |
| 367 | GEMMFunctionType gemm; |
| 368 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 369 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
| 370 | gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K)); |
| 371 | |
| 372 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 373 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 374 | |
| 375 | // Allocate tensors |
| 376 | lhs.allocator()->allocate(); |
| 377 | rhs.allocator()->allocate(); |
| 378 | lhs_reshaped.allocator()->allocate(); |
| 379 | rhs_reshaped.allocator()->allocate(); |
| 380 | dst.allocator()->allocate(); |
| 381 | |
| 382 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 383 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 384 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 385 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 386 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 387 | |
| 388 | // Fill tensors |
| 389 | fill(AccessorType(lhs), 0); |
| 390 | fill(AccessorType(rhs), 1); |
| 391 | |
| 392 | // Compute GEMM |
| 393 | reshape_lhs.run(); |
| 394 | reshape_rhs.run(); |
| 395 | gemm.run(); |
| 396 | |
| 397 | return dst; |
| 398 | } |
| 399 | |
| 400 | SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape) |
| 401 | { |
| 402 | TensorShape dst_shape = lhs_shape; |
| 403 | dst_shape[0] = rhs_shape[0]; |
| 404 | dst_shape[1] = lhs_shape[1]; |
| 405 | |
| 406 | // Create reference |
| 407 | SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 }; |
| 408 | SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 }; |
| 409 | |
| 410 | // Fill reference |
| 411 | fill(lhs, 0); |
| 412 | fill(rhs, 1); |
| 413 | |
| 414 | return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0); |
| 415 | } |
| 416 | |
| 417 | TensorType _target{}; |
| 418 | SimpleTensor<int32_t> _reference{}; |
| 419 | }; |
| 420 | |
| 421 | template <typename TensorType, typename AccessorType, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 422 | class GEMMLowpMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture |
| 423 | { |
| 424 | public: |
| 425 | template <typename...> |
| 426 | 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, |
| 427 | bool interleave_lhs, bool interleave_rhs) |
| 428 | { |
| 429 | GEMMLHSMatrixInfo lhs_info; |
| 430 | lhs_info.m0 = m0; |
| 431 | lhs_info.k0 = k0; |
| 432 | lhs_info.v0 = v0; |
| 433 | lhs_info.interleave = interleave_lhs; |
| 434 | lhs_info.transpose = false; |
| 435 | |
| 436 | GEMMRHSMatrixInfo rhs_info; |
| 437 | rhs_info.n0 = n0; |
| 438 | rhs_info.k0 = k0; |
| 439 | rhs_info.h0 = h0; |
| 440 | rhs_info.interleave = interleave_rhs; |
| 441 | rhs_info.transpose = true; |
| 442 | |
| 443 | // In case of GEMM3D, m is the product between m_w and m_h |
| 444 | const unsigned int m = m_w * m_h; |
| 445 | |
| 446 | // Set the tensor shapes for LHS and RHS matrices |
| 447 | const TensorShape lhs_shape(k, m, batch_size); |
| 448 | const TensorShape rhs_shape(n, k, batch_size); |
| 449 | |
| 450 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, m_h); |
| 451 | _reference = compute_reference(lhs_shape, rhs_shape, m_h); |
| 452 | } |
| 453 | |
| 454 | protected: |
| 455 | template <typename U> |
| 456 | void fill(U &&tensor, int i) |
| 457 | { |
| 458 | // Between 1 and 254 in order to avoid having -128 and 128 for the DOT product path |
| 459 | std::uniform_int_distribution<> distribution(1, 254); |
| 460 | library->fill(tensor, distribution, i); |
| 461 | } |
| 462 | |
| 463 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, unsigned int m_h) |
| 464 | { |
| 465 | // Create tensors |
| 466 | TensorType lhs = create_tensor<TensorType>(lhs_shape, DataType::QASYMM8, 1); |
| 467 | TensorType rhs = create_tensor<TensorType>(rhs_shape, DataType::QASYMM8, 1); |
| 468 | TensorType lhs_reshaped; |
| 469 | TensorType rhs_reshaped; |
| 470 | TensorType dst; |
| 471 | |
| 472 | const unsigned int M = lhs_shape[1]; |
| 473 | const unsigned int N = rhs_shape[0]; |
| 474 | const unsigned int K = lhs_shape[0]; |
| 475 | |
| 476 | // The output tensor will be auto-initialized within the function |
| 477 | |
| 478 | // Create and configure function |
| 479 | ReshapeLHSFunctionType reshape_lhs; |
| 480 | ReshapeRHSFunctionType reshape_rhs; |
| 481 | GEMMFunctionType gemm; |
| 482 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 483 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
| 484 | gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h)); |
| 485 | |
| 486 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 487 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 488 | |
| 489 | // Allocate tensors |
| 490 | lhs.allocator()->allocate(); |
| 491 | rhs.allocator()->allocate(); |
| 492 | lhs_reshaped.allocator()->allocate(); |
| 493 | rhs_reshaped.allocator()->allocate(); |
| 494 | dst.allocator()->allocate(); |
| 495 | |
| 496 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 497 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 498 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 499 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 500 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 501 | |
| 502 | // Fill tensors |
| 503 | fill(AccessorType(lhs), 0); |
| 504 | fill(AccessorType(rhs), 1); |
| 505 | |
| 506 | // Compute GEMM |
| 507 | reshape_lhs.run(); |
| 508 | reshape_rhs.run(); |
| 509 | gemm.run(); |
| 510 | |
| 511 | return dst; |
| 512 | } |
| 513 | |
| 514 | SimpleTensor<int32_t> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, unsigned int m_h) |
| 515 | { |
| 516 | TensorShape dst_shape = lhs_shape; |
| 517 | dst_shape.set(0, rhs_shape[0]); |
| 518 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 519 | dst_shape.set(2, m_h); |
| 520 | dst_shape.set(3, lhs_shape[2]); |
| 521 | |
| 522 | // Create reference |
| 523 | SimpleTensor<uint8_t> lhs{ lhs_shape, DataType::QASYMM8, 1 }; |
| 524 | SimpleTensor<uint8_t> rhs{ rhs_shape, DataType::QASYMM8, 1 }; |
| 525 | |
| 526 | // Fill reference |
| 527 | fill(lhs, 0); |
| 528 | fill(rhs, 1); |
| 529 | |
| 530 | return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(lhs, rhs, dst_shape, 0, 0); |
| 531 | } |
| 532 | |
| 533 | TensorType _target{}; |
| 534 | SimpleTensor<int32_t> _reference{}; |
| 535 | }; |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 536 | } // namespace validation |
| 537 | } // namespace test |
| 538 | } // namespace arm_compute |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 539 | #endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */ |