Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | { |
| 45 | template <typename TensorType, typename AccessorType, typename FunctionType> |
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 | |
| 65 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 66 | int32_t a_offset, int32_t b_offset) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 67 | { |
| 68 | // Create tensors |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 69 | TensorType a = create_tensor<TensorType>(shape_a, DataType::QASYMM8, 1); |
| 70 | TensorType b = create_tensor<TensorType>(shape_b, DataType::QASYMM8, 1); |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 71 | TensorType c = create_tensor<TensorType>(shape_c, DataType::S32, 1); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 72 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 73 | a.info()->set_quantization_info(QuantizationInfo(1.0f / 255, a_offset)); |
| 74 | b.info()->set_quantization_info(QuantizationInfo(1.0f / 255, b_offset)); |
| 75 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 76 | // Create and configure function |
| 77 | FunctionType gemmlowp; |
| 78 | gemmlowp.configure(&a, &b, &c); |
| 79 | |
| 80 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 81 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 82 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 83 | |
| 84 | // Allocate tensors |
| 85 | a.allocator()->allocate(); |
| 86 | b.allocator()->allocate(); |
| 87 | c.allocator()->allocate(); |
| 88 | |
| 89 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 90 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 91 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | |
| 93 | // Fill tensors |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 94 | fill(AccessorType(a), 0); |
| 95 | fill(AccessorType(b), 1); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 96 | |
| 97 | // Compute GEMM function |
| 98 | gemmlowp.run(); |
| 99 | return c; |
| 100 | } |
| 101 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 102 | SimpleTensor<int32_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, |
| 103 | int32_t a_offset, int32_t b_offset) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 104 | { |
| 105 | // Create reference |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 106 | SimpleTensor<uint8_t> a{ shape_a, DataType::QASYMM8, 1 }; |
| 107 | SimpleTensor<uint8_t> b{ shape_b, DataType::QASYMM8, 1 }; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 108 | |
| 109 | // Fill reference |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 110 | fill(a, 0); |
| 111 | fill(b, 1); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 112 | |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 113 | return reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(a, b, a_offset, b_offset); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 116 | TensorType _target{}; |
| 117 | SimpleTensor<int32_t> _reference{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 120 | template <typename TensorType, typename AccessorType, typename FunctionType> |
| 121 | class GEMMLowpQuantizeDownInt32ToUint8ScaleValidationFixture : public framework::Fixture |
| 122 | { |
| 123 | public: |
| 124 | template <typename...> |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 125 | 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] | 126 | { |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 127 | _target = compute_target(shape, result_offset, result_mult_int, result_shift, min, max, add_bias); |
| 128 | _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] | 129 | } |
| 130 | |
| 131 | protected: |
| 132 | template <typename U> |
| 133 | void fill(U &&tensor, int i) |
| 134 | { |
| 135 | std::uniform_int_distribution<> distribution(-6000, 6000); |
| 136 | library->fill(tensor, distribution, i); |
| 137 | } |
| 138 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 139 | 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] | 140 | { |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 141 | TensorShape shape_bias(shape[0]); |
| 142 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 143 | // Create tensors |
| 144 | TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 145 | TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1); |
| 146 | TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 147 | |
| 148 | // Create and configure function |
| 149 | FunctionType output_stage; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 150 | 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] | 151 | |
| 152 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 153 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 154 | |
| 155 | // Allocate tensors |
| 156 | a.allocator()->allocate(); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 157 | c.allocator()->allocate(); |
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 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 162 | // Fill tensor |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 163 | fill(AccessorType(a), 0); |
| 164 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 165 | if(add_bias) |
| 166 | { |
| 167 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 168 | |
| 169 | // Allocate bias tensor |
| 170 | b.allocator()->allocate(); |
| 171 | |
| 172 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 173 | |
| 174 | // Fill tensor |
| 175 | fill(AccessorType(b), 1); |
| 176 | } |
| 177 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 178 | // Compute GEMM function |
| 179 | output_stage.run(); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 180 | return c; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 183 | 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] | 184 | { |
| 185 | // Create reference |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 186 | TensorShape shape_bias(shape[0]); |
| 187 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 188 | SimpleTensor<int32_t> a{ shape, DataType::S32, 1 }; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 189 | SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 }; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 190 | |
| 191 | // Fill reference |
| 192 | fill(a, 0); |
| 193 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 194 | if(add_bias) |
| 195 | { |
| 196 | // Fill bias |
| 197 | fill(b, 1); |
| 198 | |
| 199 | return reference::gemmlowp_quantize_down_int32_to_uint8_scale<int32_t>(a, b, result_offset, result_mult_int, result_shift, min, max); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | return reference::gemmlowp_quantize_down_int32_to_uint8_scale<int32_t>(a, result_offset, result_mult_int, result_shift, min, max); |
| 204 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | TensorType _target{}; |
| 208 | SimpleTensor<uint8_t> _reference{}; |
| 209 | }; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 210 | |
| 211 | template <typename TensorType, typename AccessorType, typename FunctionType> |
| 212 | class GEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointValidationFixture : public framework::Fixture |
| 213 | { |
| 214 | public: |
| 215 | template <typename...> |
| 216 | 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) |
| 217 | { |
| 218 | _target = compute_target(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias); |
| 219 | _reference = compute_reference(shape, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max, add_bias); |
| 220 | } |
| 221 | |
| 222 | protected: |
| 223 | template <typename U> |
| 224 | void fill(U &&tensor, int i) |
| 225 | { |
| 226 | std::uniform_int_distribution<> distribution(-6000, 6000); |
| 227 | library->fill(tensor, distribution, i); |
| 228 | } |
| 229 | |
| 230 | 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) |
| 231 | { |
| 232 | TensorShape shape_bias(shape[0]); |
| 233 | |
| 234 | // Create tensors |
| 235 | TensorType a = create_tensor<TensorType>(shape, DataType::S32, 1); |
| 236 | TensorType b = create_tensor<TensorType>(shape_bias, DataType::S32, 1); |
| 237 | TensorType c = create_tensor<TensorType>(shape, DataType::QASYMM8, 1); |
| 238 | |
| 239 | // Create and configure function |
| 240 | FunctionType output_stage; |
| 241 | output_stage.configure(&a, add_bias ? &b : nullptr, &c, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max); |
| 242 | |
| 243 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 244 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 245 | |
| 246 | // Allocate tensors |
| 247 | a.allocator()->allocate(); |
| 248 | c.allocator()->allocate(); |
| 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 | // Fill tensor |
| 254 | fill(AccessorType(a), 0); |
| 255 | |
| 256 | if(add_bias) |
| 257 | { |
| 258 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 259 | |
| 260 | // Allocate bias tensor |
| 261 | b.allocator()->allocate(); |
| 262 | |
| 263 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 264 | |
| 265 | // Fill tensor |
| 266 | fill(AccessorType(b), 1); |
| 267 | } |
| 268 | |
| 269 | // Compute GEMM function |
| 270 | output_stage.run(); |
| 271 | return c; |
| 272 | } |
| 273 | |
| 274 | 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, |
| 275 | bool add_bias) |
| 276 | { |
| 277 | // Create reference |
| 278 | TensorShape shape_bias(shape[0]); |
| 279 | |
| 280 | SimpleTensor<int32_t> a{ shape, DataType::S32, 1 }; |
| 281 | SimpleTensor<int32_t> b{ shape_bias, DataType::S32, 1 }; |
| 282 | |
| 283 | // Fill reference |
| 284 | fill(a, 0); |
| 285 | |
| 286 | if(add_bias) |
| 287 | { |
| 288 | // Fill bias |
| 289 | fill(b, 1); |
| 290 | |
| 291 | 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); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | 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); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | TensorType _target{}; |
| 300 | SimpleTensor<uint8_t> _reference{}; |
| 301 | }; |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 302 | } // namespace validation |
| 303 | } // namespace test |
| 304 | } // namespace arm_compute |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 305 | #endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */ |