Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | b309fc2 | 2021-01-05 09:46:16 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +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_GEMM_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_GEMM_FIXTURE |
| 26 | |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/KernelDescriptors.h" |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorShape.h" |
| 29 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 30 | #include "tests/AssetsLibrary.h" |
| 31 | #include "tests/Globals.h" |
| 32 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 33 | #include "tests/framework/Asserts.h" |
| 34 | #include "tests/framework/Fixture.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 35 | #include "tests/validation/Helpers.h" |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 36 | #include "tests/validation/reference/ActivationLayer.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 37 | #include "tests/validation/reference/GEMM.h" |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 38 | |
| 39 | #include <random> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace test |
| 44 | { |
| 45 | namespace validation |
| 46 | { |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 47 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool disable_c = false, bool reinterpret_input_as_3d = false, bool reinterpret_output_as_3d = false> |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 48 | class GEMMValidationFixture : public framework::Fixture |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 49 | { |
| 50 | public: |
| 51 | template <typename...> |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 52 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, bool pretranspose, DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 53 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_UNUSED(pretranspose); |
| 55 | _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type); |
| 56 | _reference = compute_reference(shape_a, shape_b, output_shape, alpha, beta, data_type); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | protected: |
| 60 | template <typename U> |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 61 | void fill(U &&tensor, int i, float lo = -1.f, float hi = 1.f) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 62 | { |
| 63 | switch(tensor.data_type()) |
| 64 | { |
| 65 | case DataType::F16: |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 66 | { |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 67 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ float(lo), float(hi) }; |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 68 | library->fill(tensor, distribution, i); |
| 69 | break; |
| 70 | } |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 71 | case DataType::F32: |
| 72 | { |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 73 | std::uniform_real_distribution<float> distribution(lo, hi); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 74 | library->fill(tensor, distribution, i); |
| 75 | break; |
| 76 | } |
| 77 | default: |
| 78 | library->fill_tensor_uniform(tensor, i); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta, |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 83 | DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 84 | { |
| 85 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 86 | TensorType a = create_tensor<TensorType>(shape_a, data_type, 1); |
| 87 | TensorType b = create_tensor<TensorType>(shape_b, data_type, 1); |
| 88 | TensorType c = create_tensor<TensorType>(shape_c, data_type, 1); |
| 89 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 90 | |
| 91 | // Create and configure function |
| 92 | FunctionType gemm; |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 93 | // The GEMMinfo includes the values of the depth in case of reinterpreted 3d output. |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 94 | // If the output shape has the same number of dimensions of the input the method called is a 2D matrix multiplication (depth_output_reinterpreted_as_3D = 0), |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 95 | // in the other case we have to use the reinterpreted version of GEMM (depth_output_reinterpreted_as_3D = depth of the 3D output). |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 96 | gemm.configure(&a, |
| 97 | &b, |
| 98 | (disable_c) ? nullptr : &c, |
| 99 | &dst, |
| 100 | alpha, beta, |
| 101 | GEMMInfo(false, false, false, (reinterpret_output_as_3d ? output_shape[2] : 0), reinterpret_input_as_3d, false, GEMMLowpOutputStageInfo(), false, (reinterpret_input_as_3d |
| 102 | || reinterpret_output_as_3d))); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 103 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 105 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 106 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 107 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 108 | add_padding_x({ &a, &b, &c, &dst }); |
| 109 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 110 | // Allocate tensors |
| 111 | a.allocator()->allocate(); |
| 112 | b.allocator()->allocate(); |
| 113 | c.allocator()->allocate(); |
| 114 | dst.allocator()->allocate(); |
| 115 | |
| 116 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 117 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 118 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 119 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 120 | |
| 121 | // Fill tensors |
| 122 | fill(AccessorType(a), 0); |
| 123 | fill(AccessorType(b), 1); |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 124 | if(!disable_c) |
| 125 | { |
| 126 | fill(AccessorType(c), 2); |
| 127 | } |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 128 | |
| 129 | // Compute GEMM function |
| 130 | gemm.run(); |
| 131 | |
| 132 | return dst; |
| 133 | } |
| 134 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 135 | SimpleTensor<T> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &output_shape, float alpha, float beta, |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 136 | DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 137 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 138 | TensorShape shape_a_to_use = shape_a; |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 139 | |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 140 | if(reinterpret_input_as_3d) |
| 141 | { |
| 142 | // Collapse the second and third dimension if the input is 3D |
| 143 | shape_a_to_use.collapse(2U, 1U); |
| 144 | } |
| 145 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 146 | // Create reference |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 147 | SimpleTensor<T> a{ shape_a_to_use, data_type, 1 }; |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 148 | SimpleTensor<T> b{ shape_b, data_type, 1 }; |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 149 | SimpleTensor<T> c{ output_shape, data_type, 1 }; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 150 | |
| 151 | // Fill reference |
| 152 | fill(a, 0); |
| 153 | fill(b, 1); |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 154 | fill(c, 2); |
| 155 | |
| 156 | if(reinterpret_input_as_3d || reinterpret_output_as_3d) |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 157 | { |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 158 | const int n = shape_b[0]; |
| 159 | const int m = reinterpret_output_as_3d ? output_shape[1] * output_shape[2] : output_shape[1]; |
| 160 | const int batch_size = reinterpret_output_as_3d ? output_shape[3] : output_shape[2]; |
| 161 | |
| 162 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 163 | for(int i = 1; i < m * batch_size; i++) |
| 164 | { |
| 165 | memcpy(c.data() + i * n, c.data(), n * sizeof(T)); |
| 166 | } |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 167 | } |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 168 | |
| 169 | // Setting beta to 0 will effectively disable C for the |
| 170 | // computation of the reference: alpha * A * B + 0 * C |
| 171 | return reference::gemm<T>(a, b, c, alpha, disable_c ? 0.f : beta); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TensorType _target{}; |
| 175 | SimpleTensor<T> _reference{}; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 178 | template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType> |
| 179 | class GEMMMatrixMultiplyValidationFixture : public framework::Fixture |
| 180 | { |
| 181 | public: |
| 182 | template <typename...> |
| 183 | void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, float alpha, float beta, bool broadcast_bias, bool fp16_mixed_precision, const ActivationLayerInfo &act_info, |
| 184 | DataType data_type, GPUTarget gpu_arch) |
| 185 | { |
| 186 | // Set the tensor shapes for LHS and RHS matrices |
| 187 | const TensorShape lhs_shape(k, m, batch_size); |
| 188 | const TensorShape rhs_shape(n, k, batch_size); |
| 189 | const TensorShape bias_shape(n, |
| 190 | broadcast_bias ? 1 : m, |
| 191 | broadcast_bias ? 1 : batch_size); |
| 192 | |
| 193 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, data_type, alpha, beta, broadcast_bias, fp16_mixed_precision, act_info, gpu_arch); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 194 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | protected: |
| 198 | template <typename U> |
| 199 | void fill(U &&tensor, int i) |
| 200 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 201 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 202 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 203 | |
| 204 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 205 | library->fill(tensor, distribution, i); |
| 206 | |
| 207 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 208 | DistributionType distribution_inf{ T(std::numeric_limits<float>::infinity()), T(std::numeric_limits<float>::infinity()) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 209 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 210 | } |
| 211 | |
| 212 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, DataType data_type, float alpha, float beta, bool broadcast_bias, |
| 213 | bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 214 | { |
| 215 | // Create tensors |
| 216 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 217 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 218 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 219 | TensorType dst; |
| 220 | |
| 221 | const unsigned int m = lhs_shape[1]; |
| 222 | const unsigned int n = rhs_shape[0]; |
| 223 | const unsigned int k = lhs_shape[0]; |
| 224 | GEMMReshapeInfo reshape_info(m, n, k, 1, 1, 0, false, broadcast_bias); |
| 225 | |
| 226 | // The output tensor will be auto-initialized within the function |
| 227 | |
| 228 | // Create and configure function |
| 229 | GEMMFunctionType gemm; |
| 230 | gemm.configure(gpu_arch, &lhs, &rhs, &bias, &dst, alpha, beta, false, reshape_info, fp16_mixed_precision, act_info); |
| 231 | |
| 232 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 233 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 234 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 235 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 236 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 237 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 238 | // Allocate tensors |
| 239 | lhs.allocator()->allocate(); |
| 240 | rhs.allocator()->allocate(); |
| 241 | bias.allocator()->allocate(); |
| 242 | dst.allocator()->allocate(); |
| 243 | |
| 244 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 245 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 246 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 247 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 248 | |
| 249 | // Fill tensors |
| 250 | fill(AccessorType(lhs), 0); |
| 251 | fill(AccessorType(rhs), 1); |
| 252 | fill(AccessorType(bias), 2); |
| 253 | |
| 254 | // Compute GEMM |
| 255 | gemm.run(); |
| 256 | |
| 257 | return dst; |
| 258 | } |
| 259 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 260 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, bool broadcast_bias, |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 261 | const ActivationLayerInfo &act_info) |
| 262 | { |
| 263 | TensorShape dst_shape = lhs_shape; |
| 264 | dst_shape[0] = rhs_shape[0]; |
| 265 | dst_shape[1] = lhs_shape[1]; |
| 266 | |
| 267 | // Create reference |
| 268 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 269 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 270 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 271 | |
| 272 | const int n = rhs_shape[0]; |
| 273 | const int m = lhs_shape[1]; |
| 274 | const int batch_size = lhs_shape[2]; |
| 275 | |
| 276 | // Fill reference |
| 277 | fill(lhs, 0); |
| 278 | fill(rhs, 1); |
| 279 | fill(bias, 2); |
| 280 | |
| 281 | if(broadcast_bias) |
| 282 | { |
| 283 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 284 | for(int i = 1; i < m * batch_size; i++) |
| 285 | { |
| 286 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 291 | } |
| 292 | |
| 293 | TensorType _target{}; |
| 294 | SimpleTensor<T> _reference{}; |
| 295 | }; |
| 296 | |
| 297 | template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType> |
| 298 | class GEMMMatrixMultiply3DValidationFixture : public framework::Fixture |
| 299 | { |
| 300 | public: |
| 301 | template <typename...> |
| 302 | void setup(unsigned int m_w, unsigned int m_h, unsigned int n, unsigned int k, unsigned int batch_size, float alpha, float beta, bool broadcast_bias, bool fp16_mixed_precision, |
| 303 | const ActivationLayerInfo &act_info, DataType data_type, GPUTarget gpu_arch) |
| 304 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 305 | ARM_COMPUTE_UNUSED(broadcast_bias); |
| 306 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 307 | // In case of GEMM3D, m is the product between m_w and m_h |
| 308 | const unsigned int m = m_w * m_h; |
| 309 | |
| 310 | // Set the tensor shapes for LHS and RHS matrices |
| 311 | const TensorShape lhs_shape(k, m, batch_size); |
| 312 | const TensorShape rhs_shape(n, k, batch_size); |
| 313 | const TensorShape bias_shape(n, 1, 1); |
| 314 | |
| 315 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, data_type, alpha, beta, m_h, fp16_mixed_precision, act_info, gpu_arch); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 316 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, m_h, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | protected: |
| 320 | template <typename U> |
| 321 | void fill(U &&tensor, int i) |
| 322 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 323 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 324 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 325 | |
| 326 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 327 | library->fill(tensor, distribution, i); |
| 328 | } |
| 329 | |
| 330 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, DataType data_type, float alpha, float beta, unsigned int m_h, |
| 331 | bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 332 | { |
| 333 | // Create tensors |
| 334 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 335 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 336 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 337 | TensorType dst; |
| 338 | |
| 339 | const unsigned int m = lhs_shape[1]; |
| 340 | const unsigned int n = rhs_shape[0]; |
| 341 | const unsigned int k = lhs_shape[0]; |
| 342 | GEMMReshapeInfo reshape_info(m, n, k, 1, 1, m_h, false, true); |
| 343 | |
| 344 | // The output tensor will be auto-initialized within the function |
| 345 | |
| 346 | // Create and configure function |
| 347 | GEMMFunctionType gemm; |
| 348 | gemm.configure(gpu_arch, &lhs, &rhs, &bias, &dst, alpha, beta, false, reshape_info, fp16_mixed_precision, act_info); |
| 349 | |
| 350 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 351 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 352 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 353 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 354 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 355 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 356 | // Allocate tensors |
| 357 | lhs.allocator()->allocate(); |
| 358 | rhs.allocator()->allocate(); |
| 359 | bias.allocator()->allocate(); |
| 360 | dst.allocator()->allocate(); |
| 361 | |
| 362 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 363 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 364 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 365 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 366 | |
| 367 | // Fill tensors |
| 368 | fill(AccessorType(lhs), 0); |
| 369 | fill(AccessorType(rhs), 1); |
| 370 | fill(AccessorType(bias), 2); |
| 371 | |
| 372 | // Compute GEMM |
| 373 | gemm.run(); |
| 374 | |
| 375 | return dst; |
| 376 | } |
| 377 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 378 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, unsigned int m_h, |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 379 | const ActivationLayerInfo &act_info) |
| 380 | { |
| 381 | TensorShape dst_shape = lhs_shape; |
| 382 | dst_shape.set(0, rhs_shape[0]); |
| 383 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 384 | dst_shape.set(2, m_h); |
| 385 | dst_shape.set(3, lhs_shape[2]); |
| 386 | |
| 387 | // Create reference |
| 388 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 389 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 390 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 391 | |
| 392 | const int n = rhs_shape[0]; |
| 393 | const int m = lhs_shape[1]; |
| 394 | const int batch_size = lhs_shape[2]; |
| 395 | |
| 396 | // Fill reference |
| 397 | fill(lhs, 0); |
| 398 | fill(rhs, 1); |
| 399 | fill(bias, 2); |
| 400 | |
| 401 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 402 | for(int i = 1; i < m * batch_size; i++) |
| 403 | { |
| 404 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 405 | } |
| 406 | |
| 407 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 408 | } |
| 409 | |
| 410 | TensorType _target{}; |
| 411 | SimpleTensor<T> _reference{}; |
| 412 | }; |
| 413 | |
| 414 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 415 | class GEMMMatrixMultiplyInterleavedTransposedValidationFixture : public framework::Fixture |
| 416 | { |
| 417 | public: |
| 418 | template <typename...> |
| 419 | void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, float alpha, float beta, unsigned int v0, unsigned int h0, bool broadcast_bias, bool fp16_mixed_precision, |
| 420 | const ActivationLayerInfo &act_info, DataType data_type, GPUTarget gpu_arch) |
| 421 | { |
| 422 | GEMMLHSMatrixInfo lhs_info; |
| 423 | lhs_info.m0 = 4; |
| 424 | lhs_info.k0 = 4; |
| 425 | lhs_info.v0 = v0; |
| 426 | lhs_info.interleave = true; |
| 427 | lhs_info.transpose = true; |
| 428 | |
| 429 | GEMMRHSMatrixInfo rhs_info; |
| 430 | rhs_info.n0 = 16 / sizeof(T); |
| 431 | rhs_info.k0 = 1; |
| 432 | rhs_info.h0 = h0; |
| 433 | rhs_info.interleave = false; |
| 434 | rhs_info.transpose = false; |
| 435 | |
| 436 | // Set the tensor shapes for LHS and RHS matrices |
| 437 | const TensorShape lhs_shape(k, m, batch_size); |
| 438 | const TensorShape rhs_shape(n, k, batch_size); |
| 439 | const TensorShape bias_shape(n, |
| 440 | broadcast_bias ? 1 : m, |
| 441 | broadcast_bias ? 1 : batch_size); |
| 442 | |
| 443 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias, fp16_mixed_precision, act_info, gpu_arch); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 444 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | protected: |
| 448 | template <typename U> |
| 449 | void fill(U &&tensor, int i) |
| 450 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 451 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 452 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 453 | |
| 454 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 455 | library->fill(tensor, distribution, i); |
| 456 | |
| 457 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 458 | DistributionType distribution_inf{ T(std::numeric_limits<float>::infinity()), T(std::numeric_limits<float>::infinity()) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 459 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 460 | } |
| 461 | |
| 462 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 463 | DataType data_type, float alpha, float beta, bool broadcast_bias, bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 464 | { |
| 465 | // Create tensors |
| 466 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 467 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 468 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 469 | TensorType lhs_reshaped; |
| 470 | TensorType rhs_reshaped; |
| 471 | TensorType dst; |
| 472 | |
| 473 | const unsigned int m = lhs_shape[1]; |
| 474 | const unsigned int n = rhs_shape[0]; |
| 475 | const unsigned int k = lhs_shape[0]; |
| 476 | GEMMReshapeInfo reshape_info(m, n, k, rhs_info.h0, lhs_info.v0, 0, false, broadcast_bias); |
| 477 | |
| 478 | // The output tensor will be auto-initialized within the function |
| 479 | |
| 480 | // Create and configure function |
| 481 | ReshapeLHSFunctionType reshape_lhs; |
| 482 | ReshapeRHSFunctionType reshape_rhs; |
| 483 | GEMMFunctionType gemm; |
| 484 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 485 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
| 486 | gemm.configure(gpu_arch, &lhs_reshaped, &rhs_reshaped, &bias, &dst, alpha, beta, true, reshape_info, fp16_mixed_precision, act_info); |
| 487 | |
| 488 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 489 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 490 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 491 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame^] | 492 | // We do not pad when using image as it needs to comply to strict pitch alignment restrictions |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 493 | if(!rhs_info.export_to_cl_image) |
| 494 | { |
| 495 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 496 | } |
| 497 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 498 | // Allocate tensors |
| 499 | lhs.allocator()->allocate(); |
| 500 | rhs.allocator()->allocate(); |
| 501 | lhs_reshaped.allocator()->allocate(); |
| 502 | rhs_reshaped.allocator()->allocate(); |
| 503 | bias.allocator()->allocate(); |
| 504 | dst.allocator()->allocate(); |
| 505 | |
| 506 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 507 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 508 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 509 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 510 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 511 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 512 | |
| 513 | // Fill tensors |
| 514 | fill(AccessorType(lhs), 0); |
| 515 | fill(AccessorType(rhs), 1); |
| 516 | fill(AccessorType(bias), 2); |
| 517 | |
| 518 | // Compute GEMM |
| 519 | reshape_lhs.run(); |
| 520 | reshape_rhs.run(); |
| 521 | gemm.run(); |
| 522 | |
| 523 | return dst; |
| 524 | } |
| 525 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 526 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, bool broadcast_bias, |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 527 | const ActivationLayerInfo &act_info) |
| 528 | { |
| 529 | TensorShape dst_shape = lhs_shape; |
| 530 | dst_shape[0] = rhs_shape[0]; |
| 531 | dst_shape[1] = lhs_shape[1]; |
| 532 | |
| 533 | // Create reference |
| 534 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 535 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 536 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 537 | |
| 538 | const int n = rhs_shape[0]; |
| 539 | const int m = lhs_shape[1]; |
| 540 | const int batch_size = lhs_shape[2]; |
| 541 | |
| 542 | // Fill reference |
| 543 | fill(lhs, 0); |
| 544 | fill(rhs, 1); |
| 545 | fill(bias, 2); |
| 546 | |
| 547 | if(broadcast_bias) |
| 548 | { |
| 549 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 550 | for(int i = 1; i < m * batch_size; i++) |
| 551 | { |
| 552 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 557 | } |
| 558 | |
| 559 | TensorType _target{}; |
| 560 | SimpleTensor<T> _reference{}; |
| 561 | }; |
| 562 | |
| 563 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 564 | class GEMMMatrixMultiplyInterleavedTransposed3DValidationFixture : public framework::Fixture |
| 565 | { |
| 566 | public: |
| 567 | template <typename...> |
| 568 | void setup(unsigned int m_w, unsigned int m_h, unsigned int n, unsigned int k, unsigned int batch_size, float alpha, float beta, unsigned int v0, unsigned int h0, bool broadcast_bias, |
| 569 | bool fp16_mixed_precision, const ActivationLayerInfo &act_info, DataType data_type, GPUTarget gpu_arch) |
| 570 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 571 | ARM_COMPUTE_UNUSED(broadcast_bias); |
| 572 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 573 | GEMMLHSMatrixInfo lhs_info; |
| 574 | lhs_info.m0 = 4; |
| 575 | lhs_info.k0 = 4; |
| 576 | lhs_info.v0 = v0; |
| 577 | lhs_info.interleave = true; |
| 578 | lhs_info.transpose = true; |
| 579 | |
| 580 | GEMMRHSMatrixInfo rhs_info; |
| 581 | rhs_info.n0 = 16 / sizeof(T); |
| 582 | rhs_info.k0 = 1; |
| 583 | rhs_info.h0 = h0; |
| 584 | rhs_info.interleave = false; |
| 585 | rhs_info.transpose = false; |
| 586 | |
| 587 | // In case of GEMM3D, m is the product between m_w and m_h |
| 588 | const unsigned int m = m_w * m_h; |
| 589 | |
| 590 | // Set the tensor shapes for LHS and RHS matrices |
| 591 | const TensorShape lhs_shape(k, m, batch_size); |
| 592 | const TensorShape rhs_shape(n, k, batch_size); |
| 593 | const TensorShape bias_shape(n, 1, 1); |
| 594 | |
| 595 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h, fp16_mixed_precision, act_info, gpu_arch); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 596 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, m_h, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | protected: |
| 600 | template <typename U> |
| 601 | void fill(U &&tensor, int i) |
| 602 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 603 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 604 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 605 | |
| 606 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 607 | library->fill(tensor, distribution, i); |
| 608 | } |
| 609 | |
| 610 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 611 | DataType data_type, float alpha, float beta, unsigned int m_h, bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 612 | { |
| 613 | // Create tensors |
| 614 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 615 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 616 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 617 | TensorType lhs_reshaped; |
| 618 | TensorType rhs_reshaped; |
| 619 | TensorType dst; |
| 620 | |
| 621 | const unsigned int m = lhs_shape[1]; |
| 622 | const unsigned int n = rhs_shape[0]; |
| 623 | const unsigned int k = lhs_shape[0]; |
| 624 | GEMMReshapeInfo reshape_info(m, n, k, rhs_info.h0, lhs_info.v0, m_h, false, true); |
| 625 | |
| 626 | // The output tensor will be auto-initialized within the function |
| 627 | |
| 628 | // Create and configure function |
| 629 | ReshapeLHSFunctionType reshape_lhs; |
| 630 | ReshapeRHSFunctionType reshape_rhs; |
| 631 | GEMMFunctionType gemm; |
| 632 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 633 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
| 634 | gemm.configure(gpu_arch, &lhs_reshaped, &rhs_reshaped, &bias, &dst, alpha, beta, true, reshape_info, fp16_mixed_precision, act_info); |
| 635 | |
| 636 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 637 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 638 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 639 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame^] | 640 | // We do not pad when using image as it needs to comply to strict pitch alignment restrictions |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 641 | if(!rhs_info.export_to_cl_image) |
| 642 | { |
| 643 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 644 | } |
| 645 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 646 | // Allocate tensors |
| 647 | lhs.allocator()->allocate(); |
| 648 | rhs.allocator()->allocate(); |
| 649 | lhs_reshaped.allocator()->allocate(); |
| 650 | rhs_reshaped.allocator()->allocate(); |
| 651 | bias.allocator()->allocate(); |
| 652 | dst.allocator()->allocate(); |
| 653 | |
| 654 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 655 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 656 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 657 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 658 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 659 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 660 | |
| 661 | // Fill tensors |
| 662 | fill(AccessorType(lhs), 0); |
| 663 | fill(AccessorType(rhs), 1); |
| 664 | fill(AccessorType(bias), 2); |
| 665 | |
| 666 | // Compute GEMM |
| 667 | reshape_lhs.run(); |
| 668 | reshape_rhs.run(); |
| 669 | gemm.run(); |
| 670 | |
| 671 | return dst; |
| 672 | } |
| 673 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 674 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, unsigned int m_h, |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 675 | const ActivationLayerInfo &act_info) |
| 676 | { |
| 677 | TensorShape dst_shape = lhs_shape; |
| 678 | dst_shape.set(0, rhs_shape[0]); |
| 679 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 680 | dst_shape.set(2, m_h); |
| 681 | dst_shape.set(3, lhs_shape[2]); |
| 682 | |
| 683 | // Create reference |
| 684 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 685 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 686 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 687 | |
| 688 | const int n = rhs_shape[0]; |
| 689 | const int m = lhs_shape[1]; |
| 690 | const int batch_size = lhs_shape[2]; |
| 691 | |
| 692 | // Fill reference |
| 693 | fill(lhs, 0); |
| 694 | fill(rhs, 1); |
| 695 | fill(bias, 2); |
| 696 | |
| 697 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 698 | for(int i = 1; i < m * batch_size; i++) |
| 699 | { |
| 700 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 701 | } |
| 702 | |
| 703 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 704 | } |
| 705 | |
| 706 | TensorType _target{}; |
| 707 | SimpleTensor<T> _reference{}; |
| 708 | }; |
| 709 | |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 710 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType, bool fp_mixed_precision = false> |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 711 | class GEMMMatrixMultiplyReshapedValidationFixture : public framework::Fixture |
| 712 | { |
| 713 | public: |
| 714 | template <typename...> |
| 715 | 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, |
Gian Marco Iodice | e3a849a | 2020-06-10 17:59:30 +0100 | [diff] [blame] | 716 | bool interleave_rhs, bool export_to_cl_image, DataType data_type, float alpha, float beta, bool broadcast_bias, bool lhs_transpose, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 717 | { |
| 718 | GEMMLHSMatrixInfo lhs_info; |
| 719 | lhs_info.m0 = m0; |
| 720 | lhs_info.k0 = k0; |
| 721 | lhs_info.v0 = v0; |
| 722 | lhs_info.interleave = interleave_lhs; |
Giorgio Arena | ae99b6e | 2019-08-01 14:22:12 +0100 | [diff] [blame] | 723 | lhs_info.transpose = lhs_transpose; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 724 | |
| 725 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | e3a849a | 2020-06-10 17:59:30 +0100 | [diff] [blame] | 726 | rhs_info.n0 = n0; |
| 727 | rhs_info.k0 = k0; |
| 728 | rhs_info.h0 = h0; |
| 729 | rhs_info.interleave = interleave_rhs; |
| 730 | rhs_info.transpose = !lhs_transpose; |
| 731 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 732 | |
| 733 | // Set the tensor shapes for LHS and RHS matrices |
| 734 | const TensorShape lhs_shape(k, m, batch_size); |
| 735 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 736 | const TensorShape bias_shape(n, |
| 737 | broadcast_bias ? 1 : m, |
| 738 | broadcast_bias ? 1 : batch_size); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 739 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 740 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias, act_info); |
| 741 | if(validate_result) |
| 742 | { |
| 743 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
| 744 | } |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | protected: |
| 748 | template <typename U> |
| 749 | void fill(U &&tensor, int i) |
| 750 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 751 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 752 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 753 | |
| 754 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 755 | library->fill(tensor, distribution, i); |
Gian Marco Iodice | b87b95e | 2019-01-21 17:14:31 +0000 | [diff] [blame] | 756 | |
| 757 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 758 | DistributionType distribution_inf{ T(std::numeric_limits<float>::infinity()), T(std::numeric_limits<float>::infinity()) }; |
Gian Marco Iodice | b87b95e | 2019-01-21 17:14:31 +0000 | [diff] [blame] | 759 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 762 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 763 | DataType data_type, float alpha, float beta, bool broadcast_bias, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 764 | { |
| 765 | // Create tensors |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 766 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 767 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 768 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 769 | TensorType lhs_reshaped; |
| 770 | TensorType rhs_reshaped; |
| 771 | TensorType dst; |
| 772 | |
| 773 | const unsigned int M = lhs_shape[1]; |
| 774 | const unsigned int N = rhs_shape[0]; |
| 775 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 776 | GEMMKernelInfo kernel_info; |
| 777 | kernel_info.m = M; |
| 778 | kernel_info.n = N; |
| 779 | kernel_info.k = K; |
| 780 | kernel_info.depth_output_gemm3d = 0; |
| 781 | kernel_info.reinterpret_input_as_3d = false; |
| 782 | kernel_info.broadcast_bias = broadcast_bias; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 783 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 784 | kernel_info.fp_mixed_precision = fp_mixed_precision; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 785 | |
| 786 | // The output tensor will be auto-initialized within the function |
| 787 | |
| 788 | // Create and configure function |
| 789 | ReshapeLHSFunctionType reshape_lhs; |
| 790 | ReshapeRHSFunctionType reshape_rhs; |
| 791 | GEMMFunctionType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 792 | |
| 793 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 794 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 795 | if(!validate_result) |
| 796 | { |
| 797 | return nullptr; |
| 798 | } |
| 799 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 800 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 801 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 802 | gemm.configure(&lhs_reshaped, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 803 | |
| 804 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 805 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 806 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 807 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame^] | 808 | // We do not pad when using image as it needs to comply to strict pitch alignment restrictions |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 809 | if(!rhs_info.export_to_cl_image) |
| 810 | { |
| 811 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 812 | } |
| 813 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 814 | // Allocate tensors |
| 815 | lhs.allocator()->allocate(); |
| 816 | rhs.allocator()->allocate(); |
| 817 | lhs_reshaped.allocator()->allocate(); |
| 818 | rhs_reshaped.allocator()->allocate(); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 819 | bias.allocator()->allocate(); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 820 | dst.allocator()->allocate(); |
| 821 | |
| 822 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 823 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 824 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 825 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 826 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 827 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 828 | |
| 829 | // Fill tensors |
| 830 | fill(AccessorType(lhs), 0); |
| 831 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 832 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 833 | |
| 834 | // Compute GEMM |
| 835 | reshape_lhs.run(); |
| 836 | reshape_rhs.run(); |
| 837 | gemm.run(); |
| 838 | |
| 839 | return dst; |
| 840 | } |
| 841 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 842 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, bool broadcast_bias, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 843 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 844 | { |
| 845 | TensorShape dst_shape = lhs_shape; |
| 846 | dst_shape[0] = rhs_shape[0]; |
| 847 | dst_shape[1] = lhs_shape[1]; |
| 848 | |
| 849 | // Create reference |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 850 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 851 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 852 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 853 | |
| 854 | const int n = rhs_shape[0]; |
| 855 | const int m = lhs_shape[1]; |
| 856 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 857 | |
| 858 | // Fill reference |
| 859 | fill(lhs, 0); |
| 860 | fill(rhs, 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 861 | fill(bias, 2); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 862 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 863 | if(broadcast_bias) |
| 864 | { |
| 865 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 866 | for(int i = 1; i < m * batch_size; i++) |
| 867 | { |
| 868 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 869 | } |
| 870 | } |
| 871 | |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 872 | if(fp_mixed_precision) |
| 873 | { |
| 874 | return reference::activation_layer(reference::gemm_mixed_precision<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 875 | } |
| 876 | else |
| 877 | { |
| 878 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 879 | } |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 882 | bool validate_result = true; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 883 | TensorType _target{}; |
| 884 | SimpleTensor<T> _reference{}; |
| 885 | }; |
| 886 | |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 887 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType, bool fp_mixed_precision = false> |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 888 | class GEMMMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture |
| 889 | { |
| 890 | public: |
| 891 | template <typename...> |
| 892 | 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, |
Gian Marco Iodice | e3a849a | 2020-06-10 17:59:30 +0100 | [diff] [blame] | 893 | bool interleave_lhs, bool interleave_rhs, bool export_to_cl_image, DataType data_type, float alpha, float beta, bool lhs_transpose, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 894 | { |
| 895 | GEMMLHSMatrixInfo lhs_info; |
| 896 | lhs_info.m0 = m0; |
| 897 | lhs_info.k0 = k0; |
| 898 | lhs_info.v0 = v0; |
| 899 | lhs_info.interleave = interleave_lhs; |
Giorgio Arena | ae99b6e | 2019-08-01 14:22:12 +0100 | [diff] [blame] | 900 | lhs_info.transpose = lhs_transpose; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 901 | |
| 902 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | e3a849a | 2020-06-10 17:59:30 +0100 | [diff] [blame] | 903 | rhs_info.n0 = n0; |
| 904 | rhs_info.k0 = k0; |
| 905 | rhs_info.h0 = h0; |
| 906 | rhs_info.interleave = interleave_rhs; |
| 907 | rhs_info.transpose = !lhs_transpose; |
| 908 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 909 | |
| 910 | // In case of GEMM3D, m is the product between m_w and m_h |
| 911 | const unsigned int m = m_w * m_h; |
| 912 | |
| 913 | // Set the tensor shapes for LHS and RHS matrices |
| 914 | const TensorShape lhs_shape(k, m, batch_size); |
| 915 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 916 | const TensorShape bias_shape(n, 1, 1); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 917 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 918 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h, act_info); |
| 919 | if(validate_result) |
| 920 | { |
| 921 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, m_h, act_info); |
| 922 | } |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | protected: |
| 926 | template <typename U> |
| 927 | void fill(U &&tensor, int i) |
| 928 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 929 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 930 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 931 | |
| 932 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 933 | library->fill(tensor, distribution, i); |
| 934 | } |
| 935 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 936 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 937 | DataType data_type, float alpha, float beta, unsigned int m_h, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 938 | { |
| 939 | // Create tensors |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 940 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 941 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 942 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 943 | TensorType lhs_reshaped; |
| 944 | TensorType rhs_reshaped; |
| 945 | TensorType dst; |
| 946 | |
| 947 | const unsigned int M = lhs_shape[1]; |
| 948 | const unsigned int N = rhs_shape[0]; |
| 949 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 950 | GEMMKernelInfo kernel_info; |
| 951 | kernel_info.m = M; |
| 952 | kernel_info.n = N; |
| 953 | kernel_info.k = K; |
| 954 | kernel_info.depth_output_gemm3d = m_h; |
| 955 | kernel_info.reinterpret_input_as_3d = false; |
| 956 | kernel_info.broadcast_bias = true; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 957 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 958 | kernel_info.fp_mixed_precision = fp_mixed_precision; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 959 | |
| 960 | // The output tensor will be auto-initialized within the function |
| 961 | |
| 962 | // Create and configure function |
| 963 | ReshapeLHSFunctionType reshape_lhs; |
| 964 | ReshapeRHSFunctionType reshape_rhs; |
| 965 | GEMMFunctionType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 966 | |
| 967 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 968 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 969 | if(!validate_result) |
| 970 | { |
| 971 | return nullptr; |
| 972 | } |
| 973 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 974 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 975 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 976 | gemm.configure(&lhs_reshaped, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 977 | |
| 978 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 979 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 980 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 981 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame^] | 982 | // We do not pad when using image as it needs to comply to strict pitch alignment restrictions |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 983 | if(!rhs_info.export_to_cl_image) |
| 984 | { |
| 985 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 986 | } |
| 987 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 988 | // Allocate tensors |
| 989 | lhs.allocator()->allocate(); |
| 990 | rhs.allocator()->allocate(); |
| 991 | lhs_reshaped.allocator()->allocate(); |
| 992 | rhs_reshaped.allocator()->allocate(); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 993 | bias.allocator()->allocate(); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 994 | dst.allocator()->allocate(); |
| 995 | |
| 996 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 997 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 998 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 999 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1000 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1001 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1002 | |
| 1003 | // Fill tensors |
| 1004 | fill(AccessorType(lhs), 0); |
| 1005 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1006 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1007 | |
| 1008 | // Compute GEMM |
| 1009 | reshape_lhs.run(); |
| 1010 | reshape_rhs.run(); |
| 1011 | gemm.run(); |
| 1012 | |
| 1013 | return dst; |
| 1014 | } |
| 1015 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1016 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, unsigned int m_h, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1017 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1018 | { |
| 1019 | TensorShape dst_shape = lhs_shape; |
| 1020 | dst_shape.set(0, rhs_shape[0]); |
| 1021 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 1022 | dst_shape.set(2, m_h); |
| 1023 | dst_shape.set(3, lhs_shape[2]); |
| 1024 | |
| 1025 | // Create reference |
| 1026 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1027 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1028 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1029 | |
| 1030 | const int n = rhs_shape[0]; |
| 1031 | const int m = lhs_shape[1]; |
| 1032 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Fill reference |
| 1035 | fill(lhs, 0); |
| 1036 | fill(rhs, 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1037 | fill(bias, 2); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1038 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1039 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 1040 | for(int i = 1; i < m * batch_size; i++) |
| 1041 | { |
| 1042 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1043 | } |
| 1044 | |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 1045 | if(fp_mixed_precision) |
| 1046 | { |
| 1047 | return reference::activation_layer(reference::gemm_mixed_precision<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 1048 | } |
| 1049 | else |
| 1050 | { |
| 1051 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 1052 | } |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1055 | bool validate_result = true; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1056 | TensorType _target{}; |
| 1057 | SimpleTensor<T> _reference{}; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 1058 | }; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1059 | |
| 1060 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 1061 | class GEMMMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture |
| 1062 | { |
| 1063 | public: |
| 1064 | template <typename...> |
| 1065 | void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int h0, |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1066 | bool interleave_rhs, bool transpose_rhs, bool export_to_cl_image, DataType data_type, float alpha, float beta, bool broadcast_bias, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1067 | { |
| 1068 | GEMMLHSMatrixInfo lhs_info; |
| 1069 | lhs_info.m0 = m0; |
| 1070 | lhs_info.k0 = k0; |
| 1071 | |
| 1072 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1073 | rhs_info.n0 = n0; |
| 1074 | rhs_info.k0 = k0; |
| 1075 | rhs_info.h0 = h0; |
| 1076 | rhs_info.interleave = interleave_rhs; |
| 1077 | rhs_info.transpose = transpose_rhs; |
| 1078 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1079 | |
| 1080 | // Set the tensor shapes for LHS and RHS matrices |
| 1081 | const TensorShape lhs_shape(k, m, batch_size); |
| 1082 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1083 | const TensorShape bias_shape(n, |
| 1084 | broadcast_bias ? 1 : m, |
| 1085 | broadcast_bias ? 1 : batch_size); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1086 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1087 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias, act_info); |
| 1088 | if(validate_result) |
| 1089 | { |
| 1090 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
| 1091 | } |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | protected: |
| 1095 | template <typename U> |
| 1096 | void fill(U &&tensor, int i) |
| 1097 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1098 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 1099 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1100 | |
| 1101 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1102 | library->fill(tensor, distribution, i); |
| 1103 | |
| 1104 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1105 | DistributionType distribution_inf{ T(std::numeric_limits<float>::infinity()), T(std::numeric_limits<float>::infinity()) }; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1106 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 1107 | } |
| 1108 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1109 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1110 | DataType data_type, float alpha, float beta, bool broadcast_bias, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1111 | { |
| 1112 | // Create tensors |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1113 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1114 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1115 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1116 | TensorType rhs_reshaped; |
| 1117 | TensorType dst; |
| 1118 | |
| 1119 | const unsigned int M = lhs_shape[1]; |
| 1120 | const unsigned int N = rhs_shape[0]; |
| 1121 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1122 | GEMMKernelInfo kernel_info; |
| 1123 | kernel_info.m = M; |
| 1124 | kernel_info.n = N; |
| 1125 | kernel_info.k = K; |
| 1126 | kernel_info.depth_output_gemm3d = 0; |
| 1127 | kernel_info.reinterpret_input_as_3d = false; |
| 1128 | kernel_info.broadcast_bias = broadcast_bias; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1129 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1130 | |
| 1131 | // The output tensor will be auto-initialized within the function |
| 1132 | |
| 1133 | // Create and configure function |
| 1134 | ReshapeRHSFunctionType reshape_rhs; |
| 1135 | GEMMFunctionType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1136 | |
| 1137 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 1138 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 1139 | if(!validate_result) |
| 1140 | { |
| 1141 | return nullptr; |
| 1142 | } |
| 1143 | |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1144 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1145 | gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1146 | |
| 1147 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1148 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1149 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1150 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame^] | 1151 | // We do not pad when using image as it needs to comply to strict pitch alignment restrictions |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 1152 | if(!rhs_info.export_to_cl_image) |
| 1153 | { |
| 1154 | add_padding_x({ &lhs, &rhs, &rhs_reshaped, &bias, &dst }); |
| 1155 | } |
| 1156 | |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1157 | // Allocate tensors |
| 1158 | lhs.allocator()->allocate(); |
| 1159 | rhs.allocator()->allocate(); |
| 1160 | rhs_reshaped.allocator()->allocate(); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1161 | bias.allocator()->allocate(); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1162 | dst.allocator()->allocate(); |
| 1163 | |
| 1164 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1165 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1166 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1167 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1168 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1169 | |
| 1170 | // Fill tensors |
| 1171 | fill(AccessorType(lhs), 0); |
| 1172 | fill(AccessorType(rhs), 1); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1173 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Compute GEMM |
| 1176 | reshape_rhs.run(); |
| 1177 | gemm.run(); |
| 1178 | |
| 1179 | return dst; |
| 1180 | } |
| 1181 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1182 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, bool broadcast_bias, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1183 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1184 | { |
| 1185 | TensorShape dst_shape = lhs_shape; |
| 1186 | dst_shape[0] = rhs_shape[0]; |
| 1187 | dst_shape[1] = lhs_shape[1]; |
| 1188 | |
| 1189 | // Create reference |
| 1190 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1191 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1192 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1193 | |
| 1194 | const int n = rhs_shape[0]; |
| 1195 | const int m = lhs_shape[1]; |
| 1196 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1197 | |
| 1198 | // Fill reference |
| 1199 | fill(lhs, 0); |
| 1200 | fill(rhs, 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1201 | fill(bias, 2); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1202 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1203 | if(broadcast_bias) |
| 1204 | { |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1205 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 1206 | for(int i = 1; i < m * batch_size; i++) |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1207 | { |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1208 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1209 | } |
| 1210 | } |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1211 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1212 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1215 | bool validate_result = true; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1216 | TensorType _target{}; |
| 1217 | SimpleTensor<T> _reference{}; |
| 1218 | }; |
| 1219 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1220 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 1221 | class GEMMMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture |
| 1222 | { |
| 1223 | public: |
| 1224 | template <typename...> |
| 1225 | void setup(unsigned int m_w, unsigned int m_h, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, unsigned int h0, |
Gian Marco Iodice | 9ae06d4 | 2020-10-22 16:37:12 +0100 | [diff] [blame] | 1226 | bool interleave_rhs, bool transpose_rhs, bool export_to_cl_image, bool has_pad_y, DataType data_type, float alpha, float beta, const ActivationLayerInfo &act_info) |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1227 | { |
| 1228 | GEMMLHSMatrixInfo lhs_info; |
| 1229 | lhs_info.m0 = m0; |
| 1230 | lhs_info.k0 = k0; |
| 1231 | |
| 1232 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1233 | rhs_info.n0 = n0; |
| 1234 | rhs_info.k0 = k0; |
| 1235 | rhs_info.h0 = h0; |
| 1236 | rhs_info.interleave = interleave_rhs; |
| 1237 | rhs_info.transpose = transpose_rhs; |
| 1238 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1239 | |
| 1240 | // In case of GEMM3D, m is the product between m_w and m_h |
| 1241 | const unsigned int m = m_w * m_h; |
| 1242 | |
| 1243 | // Set the tensor shapes for LHS and RHS matrices |
| 1244 | const TensorShape lhs_shape(k, m, batch_size); |
| 1245 | const TensorShape rhs_shape(n, k, batch_size); |
| 1246 | const TensorShape bias_shape(n, 1, 1); |
| 1247 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1248 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h, act_info, has_pad_y); |
| 1249 | if(validate_result) |
| 1250 | { |
| 1251 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, m_h, act_info); |
| 1252 | } |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | protected: |
| 1256 | template <typename U> |
| 1257 | void fill(U &&tensor, int i) |
| 1258 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1259 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 1260 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1261 | |
| 1262 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1263 | library->fill(tensor, distribution, i); |
| 1264 | } |
| 1265 | |
| 1266 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 1267 | DataType data_type, float alpha, float beta, |
Gian Marco Iodice | 9ae06d4 | 2020-10-22 16:37:12 +0100 | [diff] [blame] | 1268 | unsigned int m_h, const ActivationLayerInfo &act_info, bool has_pad_y) |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1269 | { |
| 1270 | // Create tensors |
| 1271 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1272 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1273 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 1274 | TensorType rhs_reshaped; |
| 1275 | TensorType dst; |
| 1276 | |
| 1277 | const unsigned int M = lhs_shape[1]; |
| 1278 | const unsigned int N = rhs_shape[0]; |
| 1279 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1280 | GEMMKernelInfo kernel_info; |
| 1281 | kernel_info.m = M; |
| 1282 | kernel_info.n = N; |
| 1283 | kernel_info.k = K; |
| 1284 | kernel_info.depth_output_gemm3d = m_h; |
| 1285 | kernel_info.reinterpret_input_as_3d = false; |
| 1286 | kernel_info.broadcast_bias = true; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1287 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | 9ae06d4 | 2020-10-22 16:37:12 +0100 | [diff] [blame] | 1288 | kernel_info.has_pad_y = has_pad_y; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1289 | |
| 1290 | // The output tensor will be auto-initialized within the function |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1291 | // Create and configure function |
| 1292 | ReshapeRHSFunctionType reshape_rhs; |
| 1293 | GEMMFunctionType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1294 | |
| 1295 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 1296 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 1297 | if(!validate_result) |
| 1298 | { |
| 1299 | return nullptr; |
| 1300 | } |
| 1301 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1302 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1303 | gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1304 | |
Gian Marco Iodice | 9ae06d4 | 2020-10-22 16:37:12 +0100 | [diff] [blame] | 1305 | if(has_pad_y) |
| 1306 | { |
| 1307 | // Add dummy padding into lhs to validate has_pad_y path |
| 1308 | lhs.info()->extend_padding(PaddingSize(2, 0, 2, 0)); |
| 1309 | dst.info()->extend_padding(PaddingSize(2, 0, 1, 0)); |
| 1310 | } |
| 1311 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1312 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1313 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1314 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1315 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame^] | 1316 | // We do not pad when using image as it needs to comply to strict pitch alignment restrictions |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 1317 | if(!rhs_info.export_to_cl_image) |
| 1318 | { |
| 1319 | add_padding_x({ &lhs, &rhs, &rhs_reshaped, &bias, &dst }); |
| 1320 | } |
| 1321 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1322 | // Allocate tensors |
| 1323 | lhs.allocator()->allocate(); |
| 1324 | rhs.allocator()->allocate(); |
| 1325 | rhs_reshaped.allocator()->allocate(); |
| 1326 | bias.allocator()->allocate(); |
| 1327 | dst.allocator()->allocate(); |
| 1328 | |
| 1329 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1330 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1331 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1332 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1333 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1334 | |
| 1335 | // Fill tensors |
| 1336 | fill(AccessorType(lhs), 0); |
| 1337 | fill(AccessorType(rhs), 1); |
| 1338 | fill(AccessorType(bias), 2); |
| 1339 | |
| 1340 | // Compute GEMM |
| 1341 | reshape_rhs.run(); |
| 1342 | gemm.run(); |
| 1343 | |
| 1344 | return dst; |
| 1345 | } |
| 1346 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1347 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, unsigned int m_h, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1348 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1349 | { |
| 1350 | TensorShape dst_shape = lhs_shape; |
| 1351 | dst_shape.set(0, rhs_shape[0]); |
| 1352 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 1353 | dst_shape.set(2, m_h); |
| 1354 | dst_shape.set(3, lhs_shape[2]); |
| 1355 | |
| 1356 | // Create reference |
| 1357 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1358 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 1359 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1360 | |
| 1361 | const int n = rhs_shape[0]; |
| 1362 | const int m = lhs_shape[1]; |
| 1363 | const int batch_size = lhs_shape[2]; |
| 1364 | |
| 1365 | // Fill reference |
| 1366 | fill(lhs, 0); |
| 1367 | fill(rhs, 1); |
| 1368 | fill(bias, 2); |
| 1369 | |
| 1370 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 1371 | for(int i = 1; i < m * batch_size; i++) |
| 1372 | { |
| 1373 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1374 | } |
| 1375 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1376 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1377 | } |
| 1378 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1379 | bool validate_result = true; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1380 | TensorType _target{}; |
| 1381 | SimpleTensor<T> _reference{}; |
| 1382 | }; |
| 1383 | |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1384 | template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType> |
| 1385 | class GEMMMatrixMultiplyNativeValidationFixture : public framework::Fixture |
| 1386 | { |
| 1387 | public: |
| 1388 | template <typename...> |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1389 | void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, DataType data_type, float alpha, float beta, bool broadcast_bias, |
| 1390 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1391 | { |
| 1392 | GEMMLHSMatrixInfo lhs_info; |
| 1393 | lhs_info.m0 = m0; |
| 1394 | lhs_info.k0 = k0; |
| 1395 | |
| 1396 | GEMMRHSMatrixInfo rhs_info; |
| 1397 | rhs_info.n0 = n0; |
| 1398 | rhs_info.k0 = k0; |
| 1399 | |
| 1400 | // Set the tensor shapes for LHS and RHS matrices |
| 1401 | const TensorShape lhs_shape(k, m, batch_size); |
| 1402 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1403 | const TensorShape bias_shape(n, |
| 1404 | broadcast_bias ? 1 : m, |
| 1405 | broadcast_bias ? 1 : batch_size); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1406 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1407 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias, act_info); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1408 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | protected: |
| 1412 | template <typename U> |
| 1413 | void fill(U &&tensor, int i) |
| 1414 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1415 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 1416 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1417 | |
| 1418 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1419 | library->fill(tensor, distribution, i); |
| 1420 | |
| 1421 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1422 | DistributionType distribution_inf{ T(std::numeric_limits<float>::infinity()), T(std::numeric_limits<float>::infinity()) }; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1423 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 1424 | } |
| 1425 | |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1426 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1427 | DataType data_type, float alpha, float beta, bool broadcast_bias, const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1428 | { |
| 1429 | // Create tensors |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1430 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1431 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1432 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1433 | TensorType dst; |
| 1434 | |
| 1435 | const unsigned int M = lhs_shape[1]; |
| 1436 | const unsigned int N = rhs_shape[0]; |
| 1437 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1438 | GEMMKernelInfo kernel_info; |
| 1439 | kernel_info.m = M; |
| 1440 | kernel_info.n = N; |
| 1441 | kernel_info.k = K; |
| 1442 | kernel_info.depth_output_gemm3d = 0; |
| 1443 | kernel_info.reinterpret_input_as_3d = false; |
| 1444 | kernel_info.broadcast_bias = broadcast_bias; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1445 | kernel_info.activation_info = act_info; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1446 | |
| 1447 | // Create and configure function |
| 1448 | GEMMFunctionType gemm; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1449 | gemm.configure(&lhs, &rhs, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1450 | |
| 1451 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1452 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1453 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1454 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 1455 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 1456 | |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1457 | // Allocate tensors |
| 1458 | lhs.allocator()->allocate(); |
| 1459 | rhs.allocator()->allocate(); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1460 | bias.allocator()->allocate(); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1461 | dst.allocator()->allocate(); |
| 1462 | |
| 1463 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1464 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1465 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1466 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1467 | |
| 1468 | // Fill tensors |
| 1469 | fill(AccessorType(lhs), 0); |
| 1470 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1471 | fill(AccessorType(bias), 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1472 | |
| 1473 | // Compute GEMM |
| 1474 | gemm.run(); |
| 1475 | |
| 1476 | return dst; |
| 1477 | } |
| 1478 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1479 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, bool broadcast_bias, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1480 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1481 | { |
| 1482 | TensorShape dst_shape = lhs_shape; |
| 1483 | dst_shape[0] = rhs_shape[0]; |
| 1484 | dst_shape[1] = lhs_shape[1]; |
| 1485 | |
| 1486 | // Create reference |
| 1487 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1488 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1489 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1490 | |
| 1491 | const int n = rhs_shape[0]; |
| 1492 | const int m = lhs_shape[1]; |
| 1493 | const int batch_size = lhs_shape[2]; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1494 | |
| 1495 | // Fill reference |
| 1496 | fill(lhs, 0); |
| 1497 | fill(rhs, 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1498 | fill(bias, 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1499 | |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1500 | if(broadcast_bias) |
| 1501 | { |
| 1502 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 1503 | for(int i = 1; i < m * batch_size; i++) |
| 1504 | { |
| 1505 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1506 | } |
| 1507 | } |
| 1508 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1509 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | TensorType _target{}; |
| 1513 | SimpleTensor<T> _reference{}; |
| 1514 | }; |
| 1515 | |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1516 | template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType> |
| 1517 | class GEMMMatrixMultiplyNative3DValidationFixture : public framework::Fixture |
| 1518 | { |
| 1519 | public: |
| 1520 | template <typename...> |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1521 | 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, DataType data_type, float alpha, float beta, |
| 1522 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1523 | { |
| 1524 | GEMMLHSMatrixInfo lhs_info; |
| 1525 | lhs_info.m0 = m0; |
| 1526 | lhs_info.k0 = k0; |
| 1527 | |
| 1528 | GEMMRHSMatrixInfo rhs_info; |
| 1529 | rhs_info.n0 = n0; |
| 1530 | rhs_info.k0 = k0; |
| 1531 | |
| 1532 | // In case of GEMM3D, m is the product between m_w and m_h |
| 1533 | const unsigned int m = m_w * m_h; |
| 1534 | |
| 1535 | // Set the tensor shapes for LHS and RHS matrices |
| 1536 | const TensorShape lhs_shape(k, m, batch_size); |
| 1537 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1538 | const TensorShape bias_shape(n, 1, 1); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1539 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1540 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h, act_info); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1541 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, m_h, act_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | protected: |
| 1545 | template <typename U> |
| 1546 | void fill(U &&tensor, int i) |
| 1547 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1548 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 1549 | using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1550 | |
| 1551 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1552 | library->fill(tensor, distribution, i); |
| 1553 | } |
| 1554 | |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1555 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1556 | DataType data_type, float alpha, float beta, unsigned int m_h, const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1557 | { |
| 1558 | // Create tensors |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1559 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1560 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1561 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1562 | TensorType dst; |
| 1563 | |
| 1564 | const unsigned int M = lhs_shape[1]; |
| 1565 | const unsigned int N = rhs_shape[0]; |
| 1566 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1567 | GEMMKernelInfo kernel_info; |
| 1568 | kernel_info.m = M; |
| 1569 | kernel_info.n = N; |
| 1570 | kernel_info.k = K; |
| 1571 | kernel_info.depth_output_gemm3d = m_h; |
| 1572 | kernel_info.reinterpret_input_as_3d = false; |
| 1573 | kernel_info.broadcast_bias = true; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1574 | kernel_info.activation_info = act_info; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1575 | |
| 1576 | // The output tensor will be auto-initialized within the function |
| 1577 | |
| 1578 | // Create and configure function |
| 1579 | GEMMFunctionType gemm; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1580 | gemm.configure(&lhs, &rhs, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1581 | |
| 1582 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1583 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1584 | ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1585 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 1586 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 1587 | |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1588 | // Allocate tensors |
| 1589 | lhs.allocator()->allocate(); |
| 1590 | rhs.allocator()->allocate(); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1591 | bias.allocator()->allocate(); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1592 | dst.allocator()->allocate(); |
| 1593 | |
| 1594 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1595 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1596 | ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1597 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 1598 | |
| 1599 | // Fill tensors |
| 1600 | fill(AccessorType(lhs), 0); |
| 1601 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1602 | fill(AccessorType(bias), 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1603 | |
| 1604 | // Compute GEMM |
| 1605 | gemm.run(); |
| 1606 | |
| 1607 | return dst; |
| 1608 | } |
| 1609 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1610 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, unsigned int m_h, |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1611 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1612 | { |
| 1613 | TensorShape dst_shape = lhs_shape; |
| 1614 | dst_shape.set(0, rhs_shape[0]); |
| 1615 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 1616 | dst_shape.set(2, m_h); |
| 1617 | dst_shape.set(3, lhs_shape[2]); |
| 1618 | |
| 1619 | // Create reference |
| 1620 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1621 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1622 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1623 | |
| 1624 | const int n = rhs_shape[0]; |
| 1625 | const int m = lhs_shape[1]; |
| 1626 | const int batch_size = lhs_shape[2]; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1627 | |
| 1628 | // Fill reference |
| 1629 | fill(lhs, 0); |
| 1630 | fill(rhs, 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1631 | fill(bias, 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1632 | |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1633 | // In case of broadcast, we need simply copy the first into the following "M" ones |
| 1634 | for(int i = 1; i < m * batch_size; i++) |
| 1635 | { |
| 1636 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1637 | } |
| 1638 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1639 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | TensorType _target{}; |
| 1643 | SimpleTensor<T> _reference{}; |
| 1644 | }; |
| 1645 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 1646 | } // namespace validation |
| 1647 | } // namespace test |
| 1648 | } // namespace arm_compute |
| 1649 | #endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */ |