Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 1 | /* |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2024 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 | */ |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 24 | #ifndef ACL_TESTS_VALIDATION_FIXTURES_GEMMFIXTURE_H |
| 25 | #define ACL_TESTS_VALIDATION_FIXTURES_GEMMFIXTURE_H |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 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" |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 37 | #include "tests/validation/reference/ElementwiseOperations.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 38 | #include "tests/validation/reference/GEMM.h" |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 39 | |
| 40 | #include <random> |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace test |
| 45 | { |
| 46 | namespace validation |
| 47 | { |
Mohammed Suhail Munshi | 13a2d00 | 2022-09-05 11:57:34 +0100 | [diff] [blame] | 48 | 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, bool pretranspose_a = false, bool pretranspose_b = false, bool run_twice = false> |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 49 | class GEMMGenericValidationFixture : public framework::Fixture |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 50 | { |
| 51 | public: |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +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, bool accumulate=false) |
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); |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 55 | _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, accumulate); |
| 56 | _reference = compute_reference(shape_a, shape_b, output_shape, alpha, beta, data_type, accumulate); |
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, |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 83 | DataType data_type, bool accumulate=false) |
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, |
Georgios Pinitas | 4ee8b15 | 2021-07-16 16:16:43 +0100 | [diff] [blame] | 101 | GEMMInfo(false, false, false, (reinterpret_output_as_3d ? output_shape[2] : 0), reinterpret_input_as_3d, false, GEMMLowpOutputStageInfo(), false, false, (reinterpret_input_as_3d |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 102 | || reinterpret_output_as_3d), arm_compute::ActivationLayerInfo(), false /* fixed_format */, arm_compute::WeightFormat::UNSPECIFIED, false /* pretranspose_B */, accumulate)); |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 103 | ARM_COMPUTE_ASSERT(a.info()->is_resizable()); |
| 104 | ARM_COMPUTE_ASSERT(b.info()->is_resizable()); |
| 105 | ARM_COMPUTE_ASSERT(c.info()->is_resizable()); |
| 106 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 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 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 116 | ARM_COMPUTE_ASSERT(!a.info()->is_resizable()); |
| 117 | ARM_COMPUTE_ASSERT(!b.info()->is_resizable()); |
| 118 | ARM_COMPUTE_ASSERT(!c.info()->is_resizable()); |
| 119 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 120 | |
| 121 | // Fill tensors |
| 122 | fill(AccessorType(a), 0); |
| 123 | fill(AccessorType(b), 1); |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 124 | if (accumulate) |
| 125 | { |
| 126 | fill(AccessorType(dst), 6); |
| 127 | } |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 128 | if(!disable_c) |
| 129 | { |
| 130 | fill(AccessorType(c), 2); |
| 131 | } |
Mohammed Suhail Munshi | 13a2d00 | 2022-09-05 11:57:34 +0100 | [diff] [blame] | 132 | // Run with variable inputs. |
| 133 | if(run_twice) |
| 134 | { |
| 135 | gemm.run(); |
| 136 | fill(AccessorType(a), 3); // Fill tensors with new seed after run |
| 137 | fill(AccessorType(b), 4); |
| 138 | if(!disable_c) |
| 139 | { |
| 140 | fill(AccessorType(c), 5); |
| 141 | } |
| 142 | } |
| 143 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 144 | // Compute GEMM function |
| 145 | gemm.run(); |
| 146 | |
| 147 | return dst; |
| 148 | } |
| 149 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 150 | SimpleTensor<T> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &output_shape, float alpha, float beta, |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 151 | DataType data_type, bool accumulate=false) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 152 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 153 | TensorShape shape_a_to_use = shape_a; |
| 154 | if(reinterpret_input_as_3d) |
| 155 | { |
| 156 | // Collapse the second and third dimension if the input is 3D |
| 157 | shape_a_to_use.collapse(2U, 1U); |
| 158 | } |
| 159 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 160 | // Create reference |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 161 | SimpleTensor<T> a{ shape_a_to_use, data_type, 1 }; |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 162 | SimpleTensor<T> b{ shape_b, data_type, 1 }; |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 163 | SimpleTensor<T> c{ output_shape, data_type, 1 }; |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 164 | SimpleTensor<T> dst{ output_shape, data_type, 1 }; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 165 | |
| 166 | // Fill reference |
| 167 | fill(a, 0); |
| 168 | fill(b, 1); |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 169 | fill(c, 2); |
| 170 | |
| 171 | if(reinterpret_input_as_3d || reinterpret_output_as_3d) |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 172 | { |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 173 | const int n = shape_b[0]; |
| 174 | const int m = reinterpret_output_as_3d ? output_shape[1] * output_shape[2] : output_shape[1]; |
| 175 | const int batch_size = reinterpret_output_as_3d ? output_shape[3] : output_shape[2]; |
| 176 | |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 177 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 178 | for(int i = 1; i < m * batch_size; i++) |
| 179 | { |
| 180 | memcpy(c.data() + i * n, c.data(), n * sizeof(T)); |
| 181 | } |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 182 | } |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 183 | |
Adnan AlSinan | 3bb72b6 | 2022-05-06 12:10:11 +0100 | [diff] [blame] | 184 | /* Note: Assuming the usual batch matmul dimensions A = (B x M x K), B = (B x K x N), if pretranspose_A is set to true, then A is assumed to be (B x K x M), |
| 185 | therefore, A must be pre-transposed before passing it to the fixture. And, we transpose A again in the fixture to make it (B x M x K) |
| 186 | in order to be able to call reference implementation that works with (B x M x K) input. |
| 187 | Similarly, if pretranspose_B is set to true, then B is assumed to be (B x N x K), B must be pre-transposed before passing it to the fixture. */ |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 188 | |
Mohammed Suhail Munshi | bc5c407 | 2022-04-27 13:49:51 +0100 | [diff] [blame] | 189 | // Define transposed shapes |
| 190 | TensorShape a_transposed_shape(a.shape().y(), a.shape().x()); |
| 191 | TensorShape b_transposed_shape(b.shape().y(), b.shape().x()); |
| 192 | |
| 193 | // Define transposed tensors |
| 194 | SimpleTensor<T> a_transposed{ a_transposed_shape, data_type }; |
| 195 | SimpleTensor<T> b_transposed{ b_transposed_shape, data_type }; |
| 196 | |
| 197 | // pretranspose a if necessary |
| 198 | if(pretranspose_a) |
| 199 | { |
| 200 | transpose_matrix<T>(a, a_transposed); |
| 201 | } |
| 202 | |
| 203 | // pretranspose b if necessary |
| 204 | if(pretranspose_b) |
| 205 | { |
| 206 | transpose_matrix<T>(b, b_transposed); |
| 207 | } |
| 208 | |
Mohammed Suhail Munshi | 13a2d00 | 2022-09-05 11:57:34 +0100 | [diff] [blame] | 209 | // Run with variable inputs. |
| 210 | if(run_twice) |
| 211 | { |
| 212 | reference::gemm<T>((pretranspose_a) ? a_transposed : a, (pretranspose_b) ? b_transposed : b, c, alpha, disable_c ? 0.f : beta); |
| 213 | fill((pretranspose_a) ? a_transposed : a, 3); |
| 214 | fill((pretranspose_b) ? b_transposed : b, 4); |
Adnan AlSinan | 26c9d1a | 2022-09-07 13:54:53 +0100 | [diff] [blame] | 215 | fill(c, 5); |
Mohammed Suhail Munshi | 13a2d00 | 2022-09-05 11:57:34 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 218 | // Do in place summation |
| 219 | if (accumulate) |
| 220 | { |
| 221 | fill(dst, 6); |
| 222 | } |
| 223 | |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 224 | // Setting beta to 0 will effectively disable C for the |
| 225 | // computation of the reference: alpha * A * B + 0 * C |
Mohammed Suhail Munshi | bc5c407 | 2022-04-27 13:49:51 +0100 | [diff] [blame] | 226 | // Use transposed tensors if boolean enabled else use original tensors |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 227 | if (accumulate) |
| 228 | { |
| 229 | reference::gemm_accumulate<T>((pretranspose_a) ? a_transposed : a, (pretranspose_b) ? b_transposed : b, c, alpha, disable_c ? 0.f : beta, dst); |
| 230 | return dst; |
| 231 | } |
| 232 | else |
| 233 | { |
| 234 | return reference::gemm<T>((pretranspose_a) ? a_transposed : a, (pretranspose_b) ? b_transposed : b, c, alpha, disable_c ? 0.f : beta); |
| 235 | } |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TensorType _target{}; |
| 239 | SimpleTensor<T> _reference{}; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 240 | }; |
| 241 | |
Radu Salavat | f1f1f87 | 2024-02-27 18:32:26 +0000 | [diff] [blame] | 242 | 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, bool pretranspose_a = false, bool pretranspose_b = false, bool run_twice = false> |
| 243 | class GEMMValidationFixture : protected GEMMGenericValidationFixture<TensorType, AccessorType, FunctionType, T, disable_c, reinterpret_input_as_3d, reinterpret_output_as_3d, pretranspose_a, pretranspose_b, run_twice> |
| 244 | { |
| 245 | public: |
| 246 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, bool pretranspose, DataType data_type) |
| 247 | { |
| 248 | GEMMGenericValidationFixture<TensorType, AccessorType, FunctionType, T, disable_c, reinterpret_input_as_3d, reinterpret_output_as_3d, pretranspose_a, pretranspose_b, run_twice>::setup(shape_a, shape_b, shape_c, output_shape, alpha, beta, pretranspose, data_type, false /*accumulate*/); |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | 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, bool pretranspose_a = false, bool pretranspose_b = false, bool run_twice = false> |
| 253 | class GEMMAccumulateValidationFixture : protected GEMMGenericValidationFixture<TensorType, AccessorType, FunctionType, T, disable_c, reinterpret_input_as_3d, reinterpret_output_as_3d, pretranspose_a, pretranspose_b, run_twice> |
| 254 | { |
| 255 | public: |
| 256 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, bool pretranspose, DataType data_type) |
| 257 | { |
| 258 | bool accumulate = true; |
| 259 | GEMMGenericValidationFixture<TensorType, AccessorType, FunctionType, T, disable_c, reinterpret_input_as_3d, reinterpret_output_as_3d, pretranspose_a, pretranspose_b, run_twice>::setup(shape_a, shape_b, shape_c, output_shape, alpha, beta, pretranspose, data_type, accumulate); |
| 260 | } |
| 261 | }; |
| 262 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 263 | template <typename TensorType, typename AccessorType, typename T, typename GEMMOperatorType> |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 264 | class GEMMMatrixMultiplyValidationFixture : public framework::Fixture |
| 265 | { |
| 266 | public: |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 267 | 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, |
| 268 | DataType data_type, GPUTarget gpu_arch) |
| 269 | { |
| 270 | // Set the tensor shapes for LHS and RHS matrices |
| 271 | const TensorShape lhs_shape(k, m, batch_size); |
| 272 | const TensorShape rhs_shape(n, k, batch_size); |
| 273 | const TensorShape bias_shape(n, |
| 274 | broadcast_bias ? 1 : m, |
| 275 | broadcast_bias ? 1 : batch_size); |
| 276 | |
| 277 | _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] | 278 | _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] | 279 | } |
| 280 | |
| 281 | protected: |
| 282 | template <typename U> |
| 283 | void fill(U &&tensor, int i) |
| 284 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 285 | 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] | 286 | 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] | 287 | |
| 288 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 289 | library->fill(tensor, distribution, i); |
| 290 | |
| 291 | // 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] | 292 | 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] | 293 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 294 | } |
| 295 | |
| 296 | 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, |
| 297 | bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 298 | { |
| 299 | // Create tensors |
| 300 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 301 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 302 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 303 | TensorType dst; |
| 304 | |
| 305 | const unsigned int m = lhs_shape[1]; |
| 306 | const unsigned int n = rhs_shape[0]; |
| 307 | const unsigned int k = lhs_shape[0]; |
| 308 | GEMMReshapeInfo reshape_info(m, n, k, 1, 1, 0, false, broadcast_bias); |
| 309 | |
| 310 | // The output tensor will be auto-initialized within the function |
| 311 | |
| 312 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 313 | GEMMOperatorType gemm; |
| 314 | gemm.configure(gpu_arch, lhs.info(), rhs.info(), bias.info(), dst.info(), alpha, beta, false, reshape_info, fp16_mixed_precision, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 315 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 316 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 317 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 318 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 319 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 320 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 321 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 322 | // Allocate tensors |
| 323 | lhs.allocator()->allocate(); |
| 324 | rhs.allocator()->allocate(); |
| 325 | bias.allocator()->allocate(); |
| 326 | dst.allocator()->allocate(); |
| 327 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 328 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 329 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 330 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 331 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 332 | |
| 333 | // Fill tensors |
| 334 | fill(AccessorType(lhs), 0); |
| 335 | fill(AccessorType(rhs), 1); |
| 336 | fill(AccessorType(bias), 2); |
| 337 | |
| 338 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 339 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs }, |
| 340 | { ACL_SRC_1, &rhs }, |
| 341 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 342 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 343 | gemm.run(gemm_pack); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 344 | |
| 345 | return dst; |
| 346 | } |
| 347 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 348 | 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] | 349 | const ActivationLayerInfo &act_info) |
| 350 | { |
| 351 | TensorShape dst_shape = lhs_shape; |
| 352 | dst_shape[0] = rhs_shape[0]; |
| 353 | dst_shape[1] = lhs_shape[1]; |
| 354 | |
| 355 | // Create reference |
| 356 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 357 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 358 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 359 | |
| 360 | const int n = rhs_shape[0]; |
| 361 | const int m = lhs_shape[1]; |
| 362 | const int batch_size = lhs_shape[2]; |
| 363 | |
| 364 | // Fill reference |
| 365 | fill(lhs, 0); |
| 366 | fill(rhs, 1); |
| 367 | fill(bias, 2); |
| 368 | |
| 369 | if(broadcast_bias) |
| 370 | { |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 371 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 372 | for(int i = 1; i < m * batch_size; i++) |
| 373 | { |
| 374 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 379 | } |
| 380 | |
| 381 | TensorType _target{}; |
| 382 | SimpleTensor<T> _reference{}; |
| 383 | }; |
| 384 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 385 | template <typename TensorType, typename AccessorType, typename T, typename GEMMOperatorType> |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 386 | class GEMMMatrixMultiply3DValidationFixture : public framework::Fixture |
| 387 | { |
| 388 | public: |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 389 | 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, |
| 390 | const ActivationLayerInfo &act_info, DataType data_type, GPUTarget gpu_arch) |
| 391 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 392 | ARM_COMPUTE_UNUSED(broadcast_bias); |
| 393 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 394 | // In case of GEMM3D, m is the product between m_w and m_h |
| 395 | const unsigned int m = m_w * m_h; |
| 396 | |
| 397 | // Set the tensor shapes for LHS and RHS matrices |
| 398 | const TensorShape lhs_shape(k, m, batch_size); |
| 399 | const TensorShape rhs_shape(n, k, batch_size); |
| 400 | const TensorShape bias_shape(n, 1, 1); |
| 401 | |
| 402 | _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] | 403 | _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] | 404 | } |
| 405 | |
| 406 | protected: |
| 407 | template <typename U> |
| 408 | void fill(U &&tensor, int i) |
| 409 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 410 | 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] | 411 | 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] | 412 | |
| 413 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 414 | library->fill(tensor, distribution, i); |
| 415 | } |
| 416 | |
| 417 | 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, |
| 418 | bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 419 | { |
| 420 | // Create tensors |
| 421 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 422 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 423 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 424 | TensorType dst; |
| 425 | |
| 426 | const unsigned int m = lhs_shape[1]; |
| 427 | const unsigned int n = rhs_shape[0]; |
| 428 | const unsigned int k = lhs_shape[0]; |
| 429 | GEMMReshapeInfo reshape_info(m, n, k, 1, 1, m_h, false, true); |
| 430 | |
| 431 | // The output tensor will be auto-initialized within the function |
| 432 | |
| 433 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 434 | GEMMOperatorType gemm; |
| 435 | gemm.configure(gpu_arch, lhs.info(), rhs.info(), bias.info(), dst.info(), alpha, beta, false, reshape_info, fp16_mixed_precision, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 436 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 437 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 438 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 439 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 440 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 441 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 442 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 443 | // Allocate tensors |
| 444 | lhs.allocator()->allocate(); |
| 445 | rhs.allocator()->allocate(); |
| 446 | bias.allocator()->allocate(); |
| 447 | dst.allocator()->allocate(); |
| 448 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 449 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 450 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 451 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 452 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 453 | |
| 454 | // Fill tensors |
| 455 | fill(AccessorType(lhs), 0); |
| 456 | fill(AccessorType(rhs), 1); |
| 457 | fill(AccessorType(bias), 2); |
| 458 | |
| 459 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 460 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs }, |
| 461 | { ACL_SRC_1, &rhs }, |
| 462 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 463 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 464 | gemm.run(gemm_pack); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 465 | |
| 466 | return dst; |
| 467 | } |
| 468 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 469 | 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] | 470 | const ActivationLayerInfo &act_info) |
| 471 | { |
| 472 | TensorShape dst_shape = lhs_shape; |
| 473 | dst_shape.set(0, rhs_shape[0]); |
| 474 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 475 | dst_shape.set(2, m_h); |
| 476 | dst_shape.set(3, lhs_shape[2]); |
| 477 | |
| 478 | // Create reference |
| 479 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 480 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 481 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 482 | |
| 483 | const int n = rhs_shape[0]; |
| 484 | const int m = lhs_shape[1]; |
| 485 | const int batch_size = lhs_shape[2]; |
| 486 | |
| 487 | // Fill reference |
| 488 | fill(lhs, 0); |
| 489 | fill(rhs, 1); |
| 490 | fill(bias, 2); |
| 491 | |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 492 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 493 | for(int i = 1; i < m * batch_size; i++) |
| 494 | { |
| 495 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 496 | } |
| 497 | |
| 498 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 499 | } |
| 500 | |
| 501 | TensorType _target{}; |
| 502 | SimpleTensor<T> _reference{}; |
| 503 | }; |
| 504 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 505 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSOperatorType, typename ReshapeRHSOperatorType, typename GEMMOperatorType> |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 506 | class GEMMMatrixMultiplyInterleavedTransposedValidationFixture : public framework::Fixture |
| 507 | { |
| 508 | public: |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 509 | 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, |
| 510 | const ActivationLayerInfo &act_info, DataType data_type, GPUTarget gpu_arch) |
| 511 | { |
| 512 | GEMMLHSMatrixInfo lhs_info; |
| 513 | lhs_info.m0 = 4; |
| 514 | lhs_info.k0 = 4; |
| 515 | lhs_info.v0 = v0; |
| 516 | lhs_info.interleave = true; |
| 517 | lhs_info.transpose = true; |
| 518 | |
| 519 | GEMMRHSMatrixInfo rhs_info; |
| 520 | rhs_info.n0 = 16 / sizeof(T); |
| 521 | rhs_info.k0 = 1; |
| 522 | rhs_info.h0 = h0; |
| 523 | rhs_info.interleave = false; |
| 524 | rhs_info.transpose = false; |
| 525 | |
| 526 | // Set the tensor shapes for LHS and RHS matrices |
| 527 | const TensorShape lhs_shape(k, m, batch_size); |
| 528 | const TensorShape rhs_shape(n, k, batch_size); |
| 529 | const TensorShape bias_shape(n, |
| 530 | broadcast_bias ? 1 : m, |
| 531 | broadcast_bias ? 1 : batch_size); |
| 532 | |
| 533 | _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] | 534 | _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] | 535 | } |
| 536 | |
| 537 | protected: |
| 538 | template <typename U> |
| 539 | void fill(U &&tensor, int i) |
| 540 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 541 | 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] | 542 | 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] | 543 | |
| 544 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 545 | library->fill(tensor, distribution, i); |
| 546 | |
| 547 | // 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] | 548 | 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] | 549 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 550 | } |
| 551 | |
| 552 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 553 | DataType data_type, float alpha, float beta, bool broadcast_bias, bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 554 | { |
| 555 | // Create tensors |
| 556 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 557 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 558 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 559 | TensorType lhs_reshaped; |
| 560 | TensorType rhs_reshaped; |
| 561 | TensorType dst; |
| 562 | |
| 563 | const unsigned int m = lhs_shape[1]; |
| 564 | const unsigned int n = rhs_shape[0]; |
| 565 | const unsigned int k = lhs_shape[0]; |
| 566 | GEMMReshapeInfo reshape_info(m, n, k, rhs_info.h0, lhs_info.v0, 0, false, broadcast_bias); |
| 567 | |
| 568 | // The output tensor will be auto-initialized within the function |
| 569 | |
| 570 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 571 | ReshapeLHSOperatorType reshape_lhs; |
| 572 | ReshapeRHSOperatorType reshape_rhs; |
| 573 | GEMMOperatorType gemm; |
| 574 | reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info); |
| 575 | reshape_rhs.configure(rhs.info(), rhs_reshaped.info(), rhs_info); |
| 576 | gemm.configure(gpu_arch, lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, true, reshape_info, fp16_mixed_precision, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 577 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 578 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 579 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 580 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 581 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame] | 582 | // 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] | 583 | if(!rhs_info.export_to_cl_image) |
| 584 | { |
| 585 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 586 | } |
| 587 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 588 | // Allocate tensors |
| 589 | lhs.allocator()->allocate(); |
| 590 | rhs.allocator()->allocate(); |
| 591 | lhs_reshaped.allocator()->allocate(); |
| 592 | rhs_reshaped.allocator()->allocate(); |
| 593 | bias.allocator()->allocate(); |
| 594 | dst.allocator()->allocate(); |
| 595 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 596 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 597 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 598 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 599 | ARM_COMPUTE_ASSERT(!lhs_reshaped.info()->is_resizable()); |
| 600 | ARM_COMPUTE_ASSERT(!rhs_reshaped.info()->is_resizable()); |
| 601 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 602 | |
| 603 | // Fill tensors |
| 604 | fill(AccessorType(lhs), 0); |
| 605 | fill(AccessorType(rhs), 1); |
| 606 | fill(AccessorType(bias), 2); |
| 607 | |
| 608 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 609 | ITensorPack reshape_lhs_pack = { { ACL_SRC, &lhs }, { ACL_DST, &lhs_reshaped } }; |
| 610 | reshape_lhs.run(reshape_lhs_pack); |
| 611 | ITensorPack reshape_rhs_pack = { { ACL_SRC, &rhs }, { ACL_DST, &rhs_reshaped } }; |
| 612 | reshape_rhs.run(reshape_rhs_pack); |
| 613 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs_reshaped }, |
| 614 | { ACL_SRC_1, &rhs_reshaped }, |
| 615 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 616 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 617 | gemm.run(gemm_pack); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 618 | |
| 619 | return dst; |
| 620 | } |
| 621 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 622 | 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] | 623 | const ActivationLayerInfo &act_info) |
| 624 | { |
| 625 | TensorShape dst_shape = lhs_shape; |
| 626 | dst_shape[0] = rhs_shape[0]; |
| 627 | dst_shape[1] = lhs_shape[1]; |
| 628 | |
| 629 | // Create reference |
| 630 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 631 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 632 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 633 | |
| 634 | const int n = rhs_shape[0]; |
| 635 | const int m = lhs_shape[1]; |
| 636 | const int batch_size = lhs_shape[2]; |
| 637 | |
| 638 | // Fill reference |
| 639 | fill(lhs, 0); |
| 640 | fill(rhs, 1); |
| 641 | fill(bias, 2); |
| 642 | |
| 643 | if(broadcast_bias) |
| 644 | { |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 645 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 646 | for(int i = 1; i < m * batch_size; i++) |
| 647 | { |
| 648 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 653 | } |
| 654 | |
| 655 | TensorType _target{}; |
| 656 | SimpleTensor<T> _reference{}; |
| 657 | }; |
| 658 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 659 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSOperatorType, typename ReshapeRHSOperatorType, typename GEMMOperatorType> |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 660 | class GEMMMatrixMultiplyInterleavedTransposed3DValidationFixture : public framework::Fixture |
| 661 | { |
| 662 | public: |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 663 | 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, |
| 664 | bool fp16_mixed_precision, const ActivationLayerInfo &act_info, DataType data_type, GPUTarget gpu_arch) |
| 665 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 666 | ARM_COMPUTE_UNUSED(broadcast_bias); |
| 667 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 668 | GEMMLHSMatrixInfo lhs_info; |
| 669 | lhs_info.m0 = 4; |
| 670 | lhs_info.k0 = 4; |
| 671 | lhs_info.v0 = v0; |
| 672 | lhs_info.interleave = true; |
| 673 | lhs_info.transpose = true; |
| 674 | |
| 675 | GEMMRHSMatrixInfo rhs_info; |
| 676 | rhs_info.n0 = 16 / sizeof(T); |
| 677 | rhs_info.k0 = 1; |
| 678 | rhs_info.h0 = h0; |
| 679 | rhs_info.interleave = false; |
| 680 | rhs_info.transpose = false; |
| 681 | |
| 682 | // In case of GEMM3D, m is the product between m_w and m_h |
| 683 | const unsigned int m = m_w * m_h; |
| 684 | |
| 685 | // Set the tensor shapes for LHS and RHS matrices |
| 686 | const TensorShape lhs_shape(k, m, batch_size); |
| 687 | const TensorShape rhs_shape(n, k, batch_size); |
| 688 | const TensorShape bias_shape(n, 1, 1); |
| 689 | |
| 690 | _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] | 691 | _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] | 692 | } |
| 693 | |
| 694 | protected: |
| 695 | template <typename U> |
| 696 | void fill(U &&tensor, int i) |
| 697 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 698 | 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] | 699 | 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] | 700 | |
| 701 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 702 | library->fill(tensor, distribution, i); |
| 703 | } |
| 704 | |
| 705 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 706 | DataType data_type, float alpha, float beta, unsigned int m_h, bool fp16_mixed_precision, const ActivationLayerInfo &act_info, GPUTarget gpu_arch) |
| 707 | { |
| 708 | // Create tensors |
| 709 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 710 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 711 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 712 | TensorType lhs_reshaped; |
| 713 | TensorType rhs_reshaped; |
| 714 | TensorType dst; |
| 715 | |
| 716 | const unsigned int m = lhs_shape[1]; |
| 717 | const unsigned int n = rhs_shape[0]; |
| 718 | const unsigned int k = lhs_shape[0]; |
| 719 | GEMMReshapeInfo reshape_info(m, n, k, rhs_info.h0, lhs_info.v0, m_h, false, true); |
| 720 | |
| 721 | // The output tensor will be auto-initialized within the function |
| 722 | |
| 723 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 724 | ReshapeLHSOperatorType reshape_lhs; |
| 725 | ReshapeRHSOperatorType reshape_rhs; |
| 726 | GEMMOperatorType gemm; |
| 727 | reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info); |
| 728 | reshape_rhs.configure(rhs.info(), rhs_reshaped.info(), rhs_info); |
| 729 | gemm.configure(gpu_arch, lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, true, reshape_info, fp16_mixed_precision, act_info); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 730 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 731 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 732 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 733 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 734 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame] | 735 | // 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] | 736 | if(!rhs_info.export_to_cl_image) |
| 737 | { |
| 738 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 739 | } |
| 740 | |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 741 | // Allocate tensors |
| 742 | lhs.allocator()->allocate(); |
| 743 | rhs.allocator()->allocate(); |
| 744 | lhs_reshaped.allocator()->allocate(); |
| 745 | rhs_reshaped.allocator()->allocate(); |
| 746 | bias.allocator()->allocate(); |
| 747 | dst.allocator()->allocate(); |
| 748 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 749 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 750 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 751 | ARM_COMPUTE_ASSERT(!lhs_reshaped.info()->is_resizable()); |
| 752 | ARM_COMPUTE_ASSERT(!rhs_reshaped.info()->is_resizable()); |
| 753 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 754 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 755 | |
| 756 | // Fill tensors |
| 757 | fill(AccessorType(lhs), 0); |
| 758 | fill(AccessorType(rhs), 1); |
| 759 | fill(AccessorType(bias), 2); |
| 760 | |
| 761 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 762 | ITensorPack reshape_lhs_pack = { { ACL_SRC, &lhs }, { ACL_DST, &lhs_reshaped } }; |
| 763 | reshape_lhs.run(reshape_lhs_pack); |
| 764 | ITensorPack reshape_rhs_pack = { { ACL_SRC, &rhs }, { ACL_DST, &rhs_reshaped } }; |
| 765 | reshape_rhs.run(reshape_rhs_pack); |
| 766 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs_reshaped }, |
| 767 | { ACL_SRC_1, &rhs_reshaped }, |
| 768 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 769 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 770 | gemm.run(gemm_pack); |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 771 | |
| 772 | return dst; |
| 773 | } |
| 774 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 775 | 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] | 776 | const ActivationLayerInfo &act_info) |
| 777 | { |
| 778 | TensorShape dst_shape = lhs_shape; |
| 779 | dst_shape.set(0, rhs_shape[0]); |
| 780 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 781 | dst_shape.set(2, m_h); |
| 782 | dst_shape.set(3, lhs_shape[2]); |
| 783 | |
| 784 | // Create reference |
| 785 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 786 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 787 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 788 | |
| 789 | const int n = rhs_shape[0]; |
| 790 | const int m = lhs_shape[1]; |
| 791 | const int batch_size = lhs_shape[2]; |
| 792 | |
| 793 | // Fill reference |
| 794 | fill(lhs, 0); |
| 795 | fill(rhs, 1); |
| 796 | fill(bias, 2); |
| 797 | |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 798 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | d1f5476 | 2019-07-19 09:54:47 +0100 | [diff] [blame] | 799 | for(int i = 1; i < m * batch_size; i++) |
| 800 | { |
| 801 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 802 | } |
| 803 | |
| 804 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 805 | } |
| 806 | |
| 807 | TensorType _target{}; |
| 808 | SimpleTensor<T> _reference{}; |
| 809 | }; |
| 810 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 811 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSOperatorType, typename ReshapeRHSOperatorType, typename GEMMOperatorType, bool fp_mixed_precision = false> |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 812 | class GEMMMatrixMultiplyReshapedValidationFixture : public framework::Fixture |
| 813 | { |
| 814 | public: |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 815 | 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] | 816 | 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] | 817 | { |
| 818 | GEMMLHSMatrixInfo lhs_info; |
| 819 | lhs_info.m0 = m0; |
| 820 | lhs_info.k0 = k0; |
| 821 | lhs_info.v0 = v0; |
| 822 | lhs_info.interleave = interleave_lhs; |
Giorgio Arena | ae99b6e | 2019-08-01 14:22:12 +0100 | [diff] [blame] | 823 | lhs_info.transpose = lhs_transpose; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 824 | |
| 825 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | e3a849a | 2020-06-10 17:59:30 +0100 | [diff] [blame] | 826 | rhs_info.n0 = n0; |
| 827 | rhs_info.k0 = k0; |
| 828 | rhs_info.h0 = h0; |
| 829 | rhs_info.interleave = interleave_rhs; |
| 830 | rhs_info.transpose = !lhs_transpose; |
| 831 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 832 | |
| 833 | // Set the tensor shapes for LHS and RHS matrices |
| 834 | const TensorShape lhs_shape(k, m, batch_size); |
| 835 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 836 | const TensorShape bias_shape(n, |
| 837 | broadcast_bias ? 1 : m, |
| 838 | broadcast_bias ? 1 : batch_size); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 839 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 840 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias, act_info); |
| 841 | if(validate_result) |
| 842 | { |
| 843 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
| 844 | } |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | protected: |
| 848 | template <typename U> |
| 849 | void fill(U &&tensor, int i) |
| 850 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 851 | 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] | 852 | 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] | 853 | |
| 854 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 855 | library->fill(tensor, distribution, i); |
Gian Marco Iodice | b87b95e | 2019-01-21 17:14:31 +0000 | [diff] [blame] | 856 | |
| 857 | // 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] | 858 | 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] | 859 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 862 | 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] | 863 | 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] | 864 | { |
| 865 | // Create tensors |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 866 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 867 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 868 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 869 | TensorType lhs_reshaped; |
| 870 | TensorType rhs_reshaped; |
| 871 | TensorType dst; |
| 872 | |
| 873 | const unsigned int M = lhs_shape[1]; |
| 874 | const unsigned int N = rhs_shape[0]; |
| 875 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 876 | GEMMKernelInfo kernel_info; |
| 877 | kernel_info.m = M; |
| 878 | kernel_info.n = N; |
| 879 | kernel_info.k = K; |
| 880 | kernel_info.depth_output_gemm3d = 0; |
| 881 | kernel_info.reinterpret_input_as_3d = false; |
| 882 | kernel_info.broadcast_bias = broadcast_bias; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 883 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 884 | kernel_info.fp_mixed_precision = fp_mixed_precision; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 885 | |
| 886 | // The output tensor will be auto-initialized within the function |
| 887 | |
| 888 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 889 | ReshapeLHSOperatorType reshape_lhs; |
| 890 | ReshapeRHSOperatorType reshape_rhs; |
| 891 | GEMMOperatorType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 892 | |
| 893 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 894 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 895 | if(!validate_result) |
| 896 | { |
| 897 | return nullptr; |
| 898 | } |
| 899 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 900 | reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info); |
| 901 | reshape_rhs.configure(rhs.info(), rhs_reshaped.info(), rhs_info); |
| 902 | gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 903 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 904 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 905 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 906 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 907 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame] | 908 | // 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] | 909 | if(!rhs_info.export_to_cl_image) |
| 910 | { |
| 911 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 912 | } |
| 913 | |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 914 | // Allocate tensors |
| 915 | lhs.allocator()->allocate(); |
| 916 | rhs.allocator()->allocate(); |
| 917 | lhs_reshaped.allocator()->allocate(); |
| 918 | rhs_reshaped.allocator()->allocate(); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 919 | bias.allocator()->allocate(); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 920 | dst.allocator()->allocate(); |
| 921 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 922 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 923 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 924 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 925 | ARM_COMPUTE_ASSERT(!lhs_reshaped.info()->is_resizable()); |
| 926 | ARM_COMPUTE_ASSERT(!rhs_reshaped.info()->is_resizable()); |
| 927 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 928 | |
| 929 | // Fill tensors |
| 930 | fill(AccessorType(lhs), 0); |
| 931 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 932 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 933 | |
| 934 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 935 | ITensorPack reshape_lhs_pack = { { ACL_SRC, &lhs }, { ACL_DST, &lhs_reshaped } }; |
| 936 | reshape_lhs.run(reshape_lhs_pack); |
| 937 | ITensorPack reshape_rhs_pack = { { ACL_SRC, &rhs }, { ACL_DST, &rhs_reshaped } }; |
| 938 | reshape_rhs.run(reshape_rhs_pack); |
| 939 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs_reshaped }, |
| 940 | { ACL_SRC_1, &rhs_reshaped }, |
| 941 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 942 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 943 | gemm.run(gemm_pack); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 944 | |
| 945 | return dst; |
| 946 | } |
| 947 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 948 | 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] | 949 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 950 | { |
| 951 | TensorShape dst_shape = lhs_shape; |
| 952 | dst_shape[0] = rhs_shape[0]; |
| 953 | dst_shape[1] = lhs_shape[1]; |
| 954 | |
| 955 | // Create reference |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 956 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 957 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 958 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 959 | |
| 960 | const int n = rhs_shape[0]; |
| 961 | const int m = lhs_shape[1]; |
| 962 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 963 | |
| 964 | // Fill reference |
| 965 | fill(lhs, 0); |
| 966 | fill(rhs, 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 967 | fill(bias, 2); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 968 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 969 | if(broadcast_bias) |
| 970 | { |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 971 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 972 | for(int i = 1; i < m * batch_size; i++) |
| 973 | { |
| 974 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 975 | } |
| 976 | } |
| 977 | |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 978 | if(fp_mixed_precision) |
| 979 | { |
| 980 | return reference::activation_layer(reference::gemm_mixed_precision<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 985 | } |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 988 | bool validate_result = true; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 989 | TensorType _target{}; |
| 990 | SimpleTensor<T> _reference{}; |
| 991 | }; |
| 992 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 993 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSOperatorType, typename ReshapeRHSOperatorType, typename GEMMOperatorType, bool fp_mixed_precision = false> |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 994 | class GEMMMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture |
| 995 | { |
| 996 | public: |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 997 | 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] | 998 | 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] | 999 | { |
| 1000 | GEMMLHSMatrixInfo lhs_info; |
| 1001 | lhs_info.m0 = m0; |
| 1002 | lhs_info.k0 = k0; |
| 1003 | lhs_info.v0 = v0; |
| 1004 | lhs_info.interleave = interleave_lhs; |
Giorgio Arena | ae99b6e | 2019-08-01 14:22:12 +0100 | [diff] [blame] | 1005 | lhs_info.transpose = lhs_transpose; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1006 | |
| 1007 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | e3a849a | 2020-06-10 17:59:30 +0100 | [diff] [blame] | 1008 | rhs_info.n0 = n0; |
| 1009 | rhs_info.k0 = k0; |
| 1010 | rhs_info.h0 = h0; |
| 1011 | rhs_info.interleave = interleave_rhs; |
| 1012 | rhs_info.transpose = !lhs_transpose; |
| 1013 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1014 | |
| 1015 | // In case of GEMM3D, m is the product between m_w and m_h |
| 1016 | const unsigned int m = m_w * m_h; |
| 1017 | |
| 1018 | // Set the tensor shapes for LHS and RHS matrices |
| 1019 | const TensorShape lhs_shape(k, m, batch_size); |
| 1020 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1021 | const TensorShape bias_shape(n, 1, 1); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1022 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1023 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h, act_info); |
| 1024 | if(validate_result) |
| 1025 | { |
| 1026 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, m_h, act_info); |
| 1027 | } |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | protected: |
| 1031 | template <typename U> |
| 1032 | void fill(U &&tensor, int i) |
| 1033 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1034 | 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] | 1035 | 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] | 1036 | |
| 1037 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1038 | library->fill(tensor, distribution, i); |
| 1039 | } |
| 1040 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1041 | 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] | 1042 | 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] | 1043 | { |
| 1044 | // Create tensors |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1045 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1046 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1047 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1048 | TensorType lhs_reshaped; |
| 1049 | TensorType rhs_reshaped; |
| 1050 | TensorType dst; |
| 1051 | |
| 1052 | const unsigned int M = lhs_shape[1]; |
| 1053 | const unsigned int N = rhs_shape[0]; |
| 1054 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1055 | GEMMKernelInfo kernel_info; |
| 1056 | kernel_info.m = M; |
| 1057 | kernel_info.n = N; |
| 1058 | kernel_info.k = K; |
| 1059 | kernel_info.depth_output_gemm3d = m_h; |
| 1060 | kernel_info.reinterpret_input_as_3d = false; |
| 1061 | kernel_info.broadcast_bias = true; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1062 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 1063 | kernel_info.fp_mixed_precision = fp_mixed_precision; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1064 | |
| 1065 | // The output tensor will be auto-initialized within the function |
| 1066 | |
| 1067 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1068 | ReshapeLHSOperatorType reshape_lhs; |
| 1069 | ReshapeRHSOperatorType reshape_rhs; |
| 1070 | GEMMOperatorType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1071 | |
| 1072 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 1073 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 1074 | if(!validate_result) |
| 1075 | { |
| 1076 | return nullptr; |
| 1077 | } |
| 1078 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1079 | reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info); |
| 1080 | reshape_rhs.configure(rhs.info(), rhs_reshaped.info(), rhs_info); |
| 1081 | gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1082 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1083 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 1084 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 1085 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1086 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame] | 1087 | // 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] | 1088 | if(!rhs_info.export_to_cl_image) |
| 1089 | { |
| 1090 | add_padding_x({ &lhs, &rhs, &lhs_reshaped, &rhs_reshaped, &bias, &dst }); |
| 1091 | } |
| 1092 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1093 | // Allocate tensors |
| 1094 | lhs.allocator()->allocate(); |
| 1095 | rhs.allocator()->allocate(); |
| 1096 | lhs_reshaped.allocator()->allocate(); |
| 1097 | rhs_reshaped.allocator()->allocate(); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1098 | bias.allocator()->allocate(); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1099 | dst.allocator()->allocate(); |
| 1100 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1101 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 1102 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 1103 | ARM_COMPUTE_ASSERT(!lhs_reshaped.info()->is_resizable()); |
| 1104 | ARM_COMPUTE_ASSERT(!rhs_reshaped.info()->is_resizable()); |
| 1105 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 1106 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Fill tensors |
| 1109 | fill(AccessorType(lhs), 0); |
| 1110 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1111 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1114 | ITensorPack reshape_lhs_pack = { { ACL_SRC, &lhs }, { ACL_DST, &lhs_reshaped } }; |
| 1115 | reshape_lhs.run(reshape_lhs_pack); |
| 1116 | ITensorPack reshape_rhs_pack = { { ACL_SRC, &rhs }, { ACL_DST, &rhs_reshaped } }; |
| 1117 | reshape_rhs.run(reshape_rhs_pack); |
| 1118 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs_reshaped }, |
| 1119 | { ACL_SRC_1, &rhs_reshaped }, |
| 1120 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 1121 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1122 | gemm.run(gemm_pack); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1123 | |
| 1124 | return dst; |
| 1125 | } |
| 1126 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1127 | 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] | 1128 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1129 | { |
| 1130 | TensorShape dst_shape = lhs_shape; |
| 1131 | dst_shape.set(0, rhs_shape[0]); |
| 1132 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 1133 | dst_shape.set(2, m_h); |
| 1134 | dst_shape.set(3, lhs_shape[2]); |
| 1135 | |
| 1136 | // Create reference |
| 1137 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1138 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1139 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1140 | |
| 1141 | const int n = rhs_shape[0]; |
| 1142 | const int m = lhs_shape[1]; |
| 1143 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1144 | |
| 1145 | // Fill reference |
| 1146 | fill(lhs, 0); |
| 1147 | fill(rhs, 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1148 | fill(bias, 2); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1149 | |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1150 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1151 | for(int i = 1; i < m * batch_size; i++) |
| 1152 | { |
| 1153 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1154 | } |
| 1155 | |
Gian Marco Iodice | 0c17aa2 | 2019-09-27 09:23:15 +0100 | [diff] [blame] | 1156 | if(fp_mixed_precision) |
| 1157 | { |
| 1158 | return reference::activation_layer(reference::gemm_mixed_precision<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 1159 | } |
| 1160 | else |
| 1161 | { |
| 1162 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 1163 | } |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1166 | bool validate_result = true; |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 1167 | TensorType _target{}; |
| 1168 | SimpleTensor<T> _reference{}; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 1169 | }; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1170 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1171 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSOperatorType, typename GEMMOperatorType> |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1172 | class GEMMMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture |
| 1173 | { |
| 1174 | public: |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1175 | 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] | 1176 | 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] | 1177 | { |
| 1178 | GEMMLHSMatrixInfo lhs_info; |
| 1179 | lhs_info.m0 = m0; |
| 1180 | lhs_info.k0 = k0; |
| 1181 | |
| 1182 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1183 | rhs_info.n0 = n0; |
| 1184 | rhs_info.k0 = k0; |
| 1185 | rhs_info.h0 = h0; |
| 1186 | rhs_info.interleave = interleave_rhs; |
| 1187 | rhs_info.transpose = transpose_rhs; |
| 1188 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1189 | |
| 1190 | // Set the tensor shapes for LHS and RHS matrices |
| 1191 | const TensorShape lhs_shape(k, m, batch_size); |
| 1192 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1193 | const TensorShape bias_shape(n, |
| 1194 | broadcast_bias ? 1 : m, |
| 1195 | broadcast_bias ? 1 : batch_size); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1196 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1197 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias, act_info); |
| 1198 | if(validate_result) |
| 1199 | { |
| 1200 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
| 1201 | } |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | protected: |
| 1205 | template <typename U> |
| 1206 | void fill(U &&tensor, int i) |
| 1207 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1208 | 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] | 1209 | 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] | 1210 | |
| 1211 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1212 | library->fill(tensor, distribution, i); |
| 1213 | |
| 1214 | // 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] | 1215 | 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] | 1216 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 1217 | } |
| 1218 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1219 | 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] | 1220 | 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] | 1221 | { |
| 1222 | // Create tensors |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1223 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1224 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1225 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1226 | TensorType rhs_reshaped; |
| 1227 | TensorType dst; |
| 1228 | |
| 1229 | const unsigned int M = lhs_shape[1]; |
| 1230 | const unsigned int N = rhs_shape[0]; |
| 1231 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1232 | GEMMKernelInfo kernel_info; |
| 1233 | kernel_info.m = M; |
| 1234 | kernel_info.n = N; |
| 1235 | kernel_info.k = K; |
| 1236 | kernel_info.depth_output_gemm3d = 0; |
| 1237 | kernel_info.reinterpret_input_as_3d = false; |
| 1238 | kernel_info.broadcast_bias = broadcast_bias; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1239 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1240 | |
| 1241 | // The output tensor will be auto-initialized within the function |
| 1242 | |
| 1243 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1244 | ReshapeRHSOperatorType reshape_rhs; |
| 1245 | GEMMOperatorType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1246 | |
| 1247 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 1248 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 1249 | if(!validate_result) |
| 1250 | { |
| 1251 | return nullptr; |
| 1252 | } |
| 1253 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1254 | reshape_rhs.configure(rhs.info(), rhs_reshaped.info(), rhs_info); |
| 1255 | gemm.configure(lhs.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1256 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1257 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 1258 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 1259 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1260 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame] | 1261 | // 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] | 1262 | if(!rhs_info.export_to_cl_image) |
| 1263 | { |
| 1264 | add_padding_x({ &lhs, &rhs, &rhs_reshaped, &bias, &dst }); |
| 1265 | } |
| 1266 | |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1267 | // Allocate tensors |
| 1268 | lhs.allocator()->allocate(); |
| 1269 | rhs.allocator()->allocate(); |
| 1270 | rhs_reshaped.allocator()->allocate(); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1271 | bias.allocator()->allocate(); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1272 | dst.allocator()->allocate(); |
| 1273 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1274 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 1275 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 1276 | ARM_COMPUTE_ASSERT(!rhs_reshaped.info()->is_resizable()); |
| 1277 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 1278 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1279 | |
| 1280 | // Fill tensors |
| 1281 | fill(AccessorType(lhs), 0); |
| 1282 | fill(AccessorType(rhs), 1); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1283 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1284 | |
| 1285 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1286 | ITensorPack reshape_rhs_pack = { { ACL_SRC, &rhs }, { ACL_DST, &rhs_reshaped } }; |
| 1287 | reshape_rhs.run(reshape_rhs_pack); |
| 1288 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs }, |
| 1289 | { ACL_SRC_1, &rhs_reshaped }, |
| 1290 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 1291 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1292 | gemm.run(gemm_pack); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1293 | |
| 1294 | return dst; |
| 1295 | } |
| 1296 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1297 | 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] | 1298 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1299 | { |
| 1300 | TensorShape dst_shape = lhs_shape; |
| 1301 | dst_shape[0] = rhs_shape[0]; |
| 1302 | dst_shape[1] = lhs_shape[1]; |
| 1303 | |
| 1304 | // Create reference |
| 1305 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1306 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1307 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1308 | |
| 1309 | const int n = rhs_shape[0]; |
| 1310 | const int m = lhs_shape[1]; |
| 1311 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1312 | |
| 1313 | // Fill reference |
| 1314 | fill(lhs, 0); |
| 1315 | fill(rhs, 1); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1316 | fill(bias, 2); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1317 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1318 | if(broadcast_bias) |
| 1319 | { |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1320 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1321 | for(int i = 1; i < m * batch_size; i++) |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1322 | { |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1323 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1324 | } |
| 1325 | } |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1326 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1327 | 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] | 1328 | } |
| 1329 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1330 | bool validate_result = true; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 1331 | TensorType _target{}; |
| 1332 | SimpleTensor<T> _reference{}; |
| 1333 | }; |
| 1334 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1335 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSOperatorType, typename GEMMOperatorType> |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1336 | class GEMMMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture |
| 1337 | { |
| 1338 | public: |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1339 | 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] | 1340 | 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] | 1341 | { |
| 1342 | GEMMLHSMatrixInfo lhs_info; |
| 1343 | lhs_info.m0 = m0; |
| 1344 | lhs_info.k0 = k0; |
| 1345 | |
| 1346 | GEMMRHSMatrixInfo rhs_info; |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1347 | rhs_info.n0 = n0; |
| 1348 | rhs_info.k0 = k0; |
| 1349 | rhs_info.h0 = h0; |
| 1350 | rhs_info.interleave = interleave_rhs; |
| 1351 | rhs_info.transpose = transpose_rhs; |
| 1352 | rhs_info.export_to_cl_image = export_to_cl_image; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1353 | |
| 1354 | // In case of GEMM3D, m is the product between m_w and m_h |
| 1355 | const unsigned int m = m_w * m_h; |
| 1356 | |
| 1357 | // Set the tensor shapes for LHS and RHS matrices |
| 1358 | const TensorShape lhs_shape(k, m, batch_size); |
| 1359 | const TensorShape rhs_shape(n, k, batch_size); |
| 1360 | const TensorShape bias_shape(n, 1, 1); |
| 1361 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1362 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h, act_info, has_pad_y); |
| 1363 | if(validate_result) |
| 1364 | { |
| 1365 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, m_h, act_info); |
| 1366 | } |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | protected: |
| 1370 | template <typename U> |
| 1371 | void fill(U &&tensor, int i) |
| 1372 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1373 | 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] | 1374 | 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] | 1375 | |
| 1376 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1377 | library->fill(tensor, distribution, i); |
| 1378 | } |
| 1379 | |
| 1380 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 1381 | DataType data_type, float alpha, float beta, |
Gian Marco Iodice | 9ae06d4 | 2020-10-22 16:37:12 +0100 | [diff] [blame] | 1382 | 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] | 1383 | { |
| 1384 | // Create tensors |
| 1385 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1386 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1387 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 1388 | TensorType rhs_reshaped; |
| 1389 | TensorType dst; |
| 1390 | |
| 1391 | const unsigned int M = lhs_shape[1]; |
| 1392 | const unsigned int N = rhs_shape[0]; |
| 1393 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1394 | GEMMKernelInfo kernel_info; |
| 1395 | kernel_info.m = M; |
| 1396 | kernel_info.n = N; |
| 1397 | kernel_info.k = K; |
| 1398 | kernel_info.depth_output_gemm3d = m_h; |
| 1399 | kernel_info.reinterpret_input_as_3d = false; |
| 1400 | kernel_info.broadcast_bias = true; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1401 | kernel_info.activation_info = act_info; |
Gian Marco Iodice | 9ae06d4 | 2020-10-22 16:37:12 +0100 | [diff] [blame] | 1402 | kernel_info.has_pad_y = has_pad_y; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1403 | |
| 1404 | // The output tensor will be auto-initialized within the function |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1405 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1406 | ReshapeRHSOperatorType reshape_rhs; |
| 1407 | GEMMOperatorType gemm; |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1408 | |
| 1409 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 1410 | validate_result = validate_result || !rhs_info.export_to_cl_image; |
| 1411 | if(!validate_result) |
| 1412 | { |
| 1413 | return nullptr; |
| 1414 | } |
| 1415 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1416 | reshape_rhs.configure(rhs.info(), rhs_reshaped.info(), rhs_info); |
| 1417 | gemm.configure(lhs.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1418 | |
Gian Marco Iodice | 9ae06d4 | 2020-10-22 16:37:12 +0100 | [diff] [blame] | 1419 | if(has_pad_y) |
| 1420 | { |
| 1421 | // Add dummy padding into lhs to validate has_pad_y path |
| 1422 | lhs.info()->extend_padding(PaddingSize(2, 0, 2, 0)); |
| 1423 | dst.info()->extend_padding(PaddingSize(2, 0, 1, 0)); |
| 1424 | } |
| 1425 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1426 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 1427 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 1428 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1429 | |
Georgios Pinitas | 3dca91b | 2021-04-13 13:35:58 +0100 | [diff] [blame] | 1430 | // 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] | 1431 | if(!rhs_info.export_to_cl_image) |
| 1432 | { |
| 1433 | add_padding_x({ &lhs, &rhs, &rhs_reshaped, &bias, &dst }); |
| 1434 | } |
| 1435 | |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1436 | // Allocate tensors |
| 1437 | lhs.allocator()->allocate(); |
| 1438 | rhs.allocator()->allocate(); |
| 1439 | rhs_reshaped.allocator()->allocate(); |
| 1440 | bias.allocator()->allocate(); |
| 1441 | dst.allocator()->allocate(); |
| 1442 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1443 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 1444 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 1445 | ARM_COMPUTE_ASSERT(!rhs_reshaped.info()->is_resizable()); |
| 1446 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 1447 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1448 | |
| 1449 | // Fill tensors |
| 1450 | fill(AccessorType(lhs), 0); |
| 1451 | fill(AccessorType(rhs), 1); |
| 1452 | fill(AccessorType(bias), 2); |
| 1453 | |
| 1454 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1455 | ITensorPack reshape_rhs_pack = { { ACL_SRC, &rhs }, { ACL_DST, &rhs_reshaped } }; |
| 1456 | reshape_rhs.run(reshape_rhs_pack); |
| 1457 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs }, |
| 1458 | { ACL_SRC_1, &rhs_reshaped }, |
| 1459 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 1460 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1461 | gemm.run(gemm_pack); |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1462 | |
| 1463 | return dst; |
| 1464 | } |
| 1465 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1466 | 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] | 1467 | const ActivationLayerInfo &act_info) |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1468 | { |
| 1469 | TensorShape dst_shape = lhs_shape; |
| 1470 | dst_shape.set(0, rhs_shape[0]); |
| 1471 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 1472 | dst_shape.set(2, m_h); |
| 1473 | dst_shape.set(3, lhs_shape[2]); |
| 1474 | |
| 1475 | // Create reference |
| 1476 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1477 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 1478 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1479 | |
| 1480 | const int n = rhs_shape[0]; |
| 1481 | const int m = lhs_shape[1]; |
| 1482 | const int batch_size = lhs_shape[2]; |
| 1483 | |
| 1484 | // Fill reference |
| 1485 | fill(lhs, 0); |
| 1486 | fill(rhs, 1); |
| 1487 | fill(bias, 2); |
| 1488 | |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1489 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1490 | for(int i = 1; i < m * batch_size; i++) |
| 1491 | { |
| 1492 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1493 | } |
| 1494 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1495 | 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] | 1496 | } |
| 1497 | |
Sheri Zhang | cc3e53c | 2020-11-16 21:17:28 +0000 | [diff] [blame] | 1498 | bool validate_result = true; |
Gian Marco Iodice | e16c890 | 2019-06-14 16:11:10 +0100 | [diff] [blame] | 1499 | TensorType _target{}; |
| 1500 | SimpleTensor<T> _reference{}; |
| 1501 | }; |
| 1502 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1503 | template <typename TensorType, typename AccessorType, typename T, typename GEMMOperatorType> |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1504 | class GEMMMatrixMultiplyNativeValidationFixture : public framework::Fixture |
| 1505 | { |
| 1506 | public: |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1507 | 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, |
| 1508 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1509 | { |
| 1510 | GEMMLHSMatrixInfo lhs_info; |
| 1511 | lhs_info.m0 = m0; |
| 1512 | lhs_info.k0 = k0; |
| 1513 | |
| 1514 | GEMMRHSMatrixInfo rhs_info; |
| 1515 | rhs_info.n0 = n0; |
| 1516 | rhs_info.k0 = k0; |
| 1517 | |
| 1518 | // Set the tensor shapes for LHS and RHS matrices |
| 1519 | const TensorShape lhs_shape(k, m, batch_size); |
| 1520 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1521 | const TensorShape bias_shape(n, |
| 1522 | broadcast_bias ? 1 : m, |
| 1523 | broadcast_bias ? 1 : batch_size); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1524 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1525 | _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] | 1526 | _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] | 1527 | } |
| 1528 | |
| 1529 | protected: |
| 1530 | template <typename U> |
| 1531 | void fill(U &&tensor, int i) |
| 1532 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1533 | 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] | 1534 | 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] | 1535 | |
| 1536 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1537 | library->fill(tensor, distribution, i); |
| 1538 | |
| 1539 | // 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] | 1540 | 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] | 1541 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 1542 | } |
| 1543 | |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1544 | 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] | 1545 | 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] | 1546 | { |
| 1547 | // Create tensors |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1548 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1549 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1550 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1551 | TensorType dst; |
| 1552 | |
| 1553 | const unsigned int M = lhs_shape[1]; |
| 1554 | const unsigned int N = rhs_shape[0]; |
| 1555 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1556 | GEMMKernelInfo kernel_info; |
| 1557 | kernel_info.m = M; |
| 1558 | kernel_info.n = N; |
| 1559 | kernel_info.k = K; |
| 1560 | kernel_info.depth_output_gemm3d = 0; |
| 1561 | kernel_info.reinterpret_input_as_3d = false; |
| 1562 | kernel_info.broadcast_bias = broadcast_bias; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1563 | kernel_info.activation_info = act_info; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1564 | |
| 1565 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1566 | GEMMOperatorType gemm; |
| 1567 | gemm.configure(lhs.info(), rhs.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1568 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1569 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 1570 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 1571 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1572 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 1573 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 1574 | |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1575 | // Allocate tensors |
| 1576 | lhs.allocator()->allocate(); |
| 1577 | rhs.allocator()->allocate(); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1578 | bias.allocator()->allocate(); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1579 | dst.allocator()->allocate(); |
| 1580 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1581 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 1582 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 1583 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 1584 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1585 | |
| 1586 | // Fill tensors |
| 1587 | fill(AccessorType(lhs), 0); |
| 1588 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1589 | fill(AccessorType(bias), 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1590 | |
| 1591 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1592 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs }, |
| 1593 | { ACL_SRC_1, &rhs }, |
| 1594 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 1595 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1596 | gemm.run(gemm_pack); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1597 | |
| 1598 | return dst; |
| 1599 | } |
| 1600 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1601 | 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] | 1602 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1603 | { |
| 1604 | TensorShape dst_shape = lhs_shape; |
| 1605 | dst_shape[0] = rhs_shape[0]; |
| 1606 | dst_shape[1] = lhs_shape[1]; |
| 1607 | |
| 1608 | // Create reference |
| 1609 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1610 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1611 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1612 | |
| 1613 | const int n = rhs_shape[0]; |
| 1614 | const int m = lhs_shape[1]; |
| 1615 | const int batch_size = lhs_shape[2]; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1616 | |
| 1617 | // Fill reference |
| 1618 | fill(lhs, 0); |
| 1619 | fill(rhs, 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1620 | fill(bias, 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1621 | |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1622 | if(broadcast_bias) |
| 1623 | { |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1624 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1625 | for(int i = 1; i < m * batch_size; i++) |
| 1626 | { |
| 1627 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1628 | } |
| 1629 | } |
| 1630 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1631 | 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] | 1632 | } |
| 1633 | |
| 1634 | TensorType _target{}; |
| 1635 | SimpleTensor<T> _reference{}; |
| 1636 | }; |
| 1637 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1638 | template <typename TensorType, typename AccessorType, typename T, typename GEMMOperatorType> |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1639 | class GEMMMatrixMultiplyNative3DValidationFixture : public framework::Fixture |
| 1640 | { |
| 1641 | public: |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1642 | 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, |
| 1643 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1644 | { |
| 1645 | GEMMLHSMatrixInfo lhs_info; |
| 1646 | lhs_info.m0 = m0; |
| 1647 | lhs_info.k0 = k0; |
| 1648 | |
| 1649 | GEMMRHSMatrixInfo rhs_info; |
| 1650 | rhs_info.n0 = n0; |
| 1651 | rhs_info.k0 = k0; |
| 1652 | |
| 1653 | // In case of GEMM3D, m is the product between m_w and m_h |
| 1654 | const unsigned int m = m_w * m_h; |
| 1655 | |
| 1656 | // Set the tensor shapes for LHS and RHS matrices |
| 1657 | const TensorShape lhs_shape(k, m, batch_size); |
| 1658 | const TensorShape rhs_shape(n, k, batch_size); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1659 | const TensorShape bias_shape(n, 1, 1); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1660 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1661 | _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] | 1662 | _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] | 1663 | } |
| 1664 | |
| 1665 | protected: |
| 1666 | template <typename U> |
| 1667 | void fill(U &&tensor, int i) |
| 1668 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 1669 | 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] | 1670 | 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] | 1671 | |
| 1672 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1673 | library->fill(tensor, distribution, i); |
| 1674 | } |
| 1675 | |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1676 | 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] | 1677 | 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] | 1678 | { |
| 1679 | // Create tensors |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1680 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1681 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1682 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1683 | TensorType dst; |
| 1684 | |
| 1685 | const unsigned int M = lhs_shape[1]; |
| 1686 | const unsigned int N = rhs_shape[0]; |
| 1687 | const unsigned int K = lhs_shape[0]; |
Gian Marco Iodice | 7026b30 | 2019-06-26 17:18:11 +0100 | [diff] [blame] | 1688 | GEMMKernelInfo kernel_info; |
| 1689 | kernel_info.m = M; |
| 1690 | kernel_info.n = N; |
| 1691 | kernel_info.k = K; |
| 1692 | kernel_info.depth_output_gemm3d = m_h; |
| 1693 | kernel_info.reinterpret_input_as_3d = false; |
| 1694 | kernel_info.broadcast_bias = true; |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1695 | kernel_info.activation_info = act_info; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1696 | |
| 1697 | // The output tensor will be auto-initialized within the function |
| 1698 | |
| 1699 | // Create and configure function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1700 | GEMMOperatorType gemm; |
| 1701 | gemm.configure(lhs.info(), rhs.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1702 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1703 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 1704 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 1705 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1706 | |
Giorgio Arena | 63825e8 | 2021-03-25 14:54:50 +0000 | [diff] [blame] | 1707 | add_padding_x({ &lhs, &rhs, &bias, &dst }); |
| 1708 | |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1709 | // Allocate tensors |
| 1710 | lhs.allocator()->allocate(); |
| 1711 | rhs.allocator()->allocate(); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1712 | bias.allocator()->allocate(); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1713 | dst.allocator()->allocate(); |
| 1714 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 1715 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 1716 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 1717 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 1718 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1719 | |
| 1720 | // Fill tensors |
| 1721 | fill(AccessorType(lhs), 0); |
| 1722 | fill(AccessorType(rhs), 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1723 | fill(AccessorType(bias), 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1724 | |
| 1725 | // Compute GEMM |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1726 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs }, |
| 1727 | { ACL_SRC_1, &rhs }, |
| 1728 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 1729 | { ACL_DST, &dst } }); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 1730 | gemm.run(gemm_pack); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1731 | |
| 1732 | return dst; |
| 1733 | } |
| 1734 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 1735 | 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] | 1736 | const ActivationLayerInfo &act_info) |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1737 | { |
| 1738 | TensorShape dst_shape = lhs_shape; |
| 1739 | dst_shape.set(0, rhs_shape[0]); |
| 1740 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 1741 | dst_shape.set(2, m_h); |
| 1742 | dst_shape.set(3, lhs_shape[2]); |
| 1743 | |
| 1744 | // Create reference |
| 1745 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1746 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1747 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1748 | |
| 1749 | const int n = rhs_shape[0]; |
| 1750 | const int m = lhs_shape[1]; |
| 1751 | const int batch_size = lhs_shape[2]; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1752 | |
| 1753 | // Fill reference |
| 1754 | fill(lhs, 0); |
| 1755 | fill(rhs, 1); |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1756 | fill(bias, 2); |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 1757 | |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1758 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
Gian Marco Iodice | 944170e | 2019-06-24 14:40:30 +0100 | [diff] [blame] | 1759 | for(int i = 1; i < m * batch_size; i++) |
| 1760 | { |
| 1761 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1762 | } |
| 1763 | |
Gian Marco Iodice | ca1f460 | 2019-07-16 15:46:48 +0100 | [diff] [blame] | 1764 | 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] | 1765 | } |
| 1766 | |
| 1767 | TensorType _target{}; |
| 1768 | SimpleTensor<T> _reference{}; |
| 1769 | }; |
| 1770 | |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1771 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSOperatorType, typename GEMMOperatorType> |
| 1772 | class GEMMMatrixMultiplyReshapedOnlyRhsMMULValidationFixture : public framework::Fixture |
| 1773 | { |
| 1774 | public: |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1775 | void setup(unsigned int m, unsigned int n, unsigned int k, unsigned int batch_size, unsigned int m0, unsigned int n0, unsigned int k0, bool export_to_cl_image, DataType data_type, float alpha, |
| 1776 | float beta, bool broadcast_bias, |
| 1777 | const ActivationLayerInfo &act_info) |
| 1778 | { |
| 1779 | GEMMLHSMatrixInfo lhs_info; |
| 1780 | lhs_info.m0 = m0; |
| 1781 | lhs_info.k0 = k0; |
| 1782 | |
| 1783 | GEMMRHSMatrixInfo rhs_info; |
| 1784 | rhs_info.n0 = n0; |
| 1785 | rhs_info.k0 = k0; |
| 1786 | rhs_info.interleave = true; |
| 1787 | rhs_info.transpose = false; |
| 1788 | rhs_info.h0 = 4; |
| 1789 | rhs_info.export_to_cl_image = export_to_cl_image; |
| 1790 | |
| 1791 | // Set the tensor shapes for LHS and RHS matrices |
| 1792 | const TensorShape lhs_shape(k, m, batch_size); |
| 1793 | const TensorShape rhs_shape(n, k, batch_size); |
| 1794 | const TensorShape bias_shape(n, |
| 1795 | broadcast_bias ? 1 : m, |
| 1796 | broadcast_bias ? 1 : batch_size); |
| 1797 | |
| 1798 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias, act_info); |
| 1799 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, beta, broadcast_bias, act_info); |
| 1800 | } |
| 1801 | |
| 1802 | protected: |
| 1803 | template <typename U> |
| 1804 | void fill(U &&tensor, int i) |
| 1805 | { |
| 1806 | static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); |
| 1807 | 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; |
| 1808 | |
| 1809 | DistributionType distribution{ T(-1.0f), T(1.0f) }; |
| 1810 | library->fill(tensor, distribution, i); |
| 1811 | |
| 1812 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
| 1813 | DistributionType distribution_inf{ T(std::numeric_limits<float>::infinity()), T(std::numeric_limits<float>::infinity()) }; |
| 1814 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 1815 | } |
| 1816 | |
| 1817 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 1818 | DataType data_type, float alpha, float beta, bool broadcast_bias, const ActivationLayerInfo &act_info) |
| 1819 | { |
| 1820 | // Create tensors |
| 1821 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 1822 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 1823 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
| 1824 | TensorType rhs_reshaped; |
| 1825 | TensorType dst; |
| 1826 | |
| 1827 | const unsigned int M = lhs_shape[1]; |
| 1828 | const unsigned int N = rhs_shape[0]; |
| 1829 | const unsigned int K = lhs_shape[0]; |
| 1830 | GEMMKernelInfo kernel_info; |
| 1831 | kernel_info.m = M; |
| 1832 | kernel_info.n = N; |
| 1833 | kernel_info.k = K; |
| 1834 | kernel_info.depth_output_gemm3d = 0; |
| 1835 | kernel_info.reinterpret_input_as_3d = false; |
| 1836 | kernel_info.broadcast_bias = broadcast_bias; |
| 1837 | kernel_info.activation_info = act_info; |
| 1838 | |
| 1839 | // Create and configure function |
| 1840 | ReshapeRHSOperatorType reshape_rhs; |
| 1841 | GEMMOperatorType gemm; |
| 1842 | |
| 1843 | validate_result = bool(reshape_rhs.validate(rhs.info(), rhs_reshaped.info(), rhs_info)); |
| 1844 | if(!validate_result) |
| 1845 | { |
| 1846 | return nullptr; |
| 1847 | } |
| 1848 | |
| 1849 | reshape_rhs.configure(rhs.info(), rhs_reshaped.info(), rhs_info); |
| 1850 | |
| 1851 | validate_result = bool(gemm.validate(lhs.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info)); |
| 1852 | if(!validate_result) |
| 1853 | { |
| 1854 | return nullptr; |
| 1855 | } |
| 1856 | |
| 1857 | gemm.configure(lhs.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
| 1858 | |
| 1859 | ARM_COMPUTE_ASSERT(lhs.info()->is_resizable()); |
| 1860 | ARM_COMPUTE_ASSERT(rhs.info()->is_resizable()); |
| 1861 | ARM_COMPUTE_ASSERT(bias.info()->is_resizable()); |
| 1862 | |
| 1863 | // Allocate tensors |
| 1864 | lhs.allocator()->allocate(); |
| 1865 | rhs.allocator()->allocate(); |
| 1866 | rhs_reshaped.allocator()->allocate(); |
| 1867 | bias.allocator()->allocate(); |
| 1868 | dst.allocator()->allocate(); |
| 1869 | |
| 1870 | ARM_COMPUTE_ASSERT(!lhs.info()->is_resizable()); |
| 1871 | ARM_COMPUTE_ASSERT(!rhs.info()->is_resizable()); |
| 1872 | ARM_COMPUTE_ASSERT(!rhs_reshaped.info()->is_resizable()); |
| 1873 | ARM_COMPUTE_ASSERT(!bias.info()->is_resizable()); |
| 1874 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
| 1875 | |
| 1876 | // Fill tensors |
| 1877 | fill(AccessorType(lhs), 0); |
| 1878 | fill(AccessorType(rhs), 1); |
| 1879 | fill(AccessorType(bias), 2); |
| 1880 | |
| 1881 | // Compute GEMM |
| 1882 | ITensorPack reshape_rhs_pack = { { ACL_SRC, &rhs }, { ACL_DST, &rhs_reshaped } }; |
| 1883 | reshape_rhs.run(reshape_rhs_pack); |
| 1884 | ITensorPack gemm_pack({ { ACL_SRC_0, &lhs }, |
| 1885 | { ACL_SRC_1, &rhs_reshaped }, |
| 1886 | { ACL_SRC_2, &bias }, |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 1887 | { ACL_DST, &dst } }); |
Gunes Bayir | 4bfc70e | 2021-12-10 16:17:56 +0000 | [diff] [blame] | 1888 | gemm.run(gemm_pack); |
| 1889 | |
| 1890 | return dst; |
| 1891 | } |
| 1892 | |
| 1893 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, float beta, bool broadcast_bias, |
| 1894 | const ActivationLayerInfo &act_info) |
| 1895 | { |
| 1896 | if(!validate_result) |
| 1897 | return SimpleTensor<T>(); |
| 1898 | |
| 1899 | TensorShape dst_shape = lhs_shape; |
| 1900 | dst_shape[0] = rhs_shape[0]; |
| 1901 | dst_shape[1] = lhs_shape[1]; |
| 1902 | |
| 1903 | // Create reference |
| 1904 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 1905 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 1906 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 1907 | |
| 1908 | const int n = rhs_shape[0]; |
| 1909 | const int m = lhs_shape[1]; |
| 1910 | const int batch_size = lhs_shape[2]; |
| 1911 | |
| 1912 | // Fill reference |
| 1913 | fill(lhs, 0); |
| 1914 | fill(rhs, 1); |
| 1915 | fill(bias, 2); |
| 1916 | |
| 1917 | if(broadcast_bias) |
| 1918 | { |
| 1919 | // In case of broadcast, we need to simply copy the first into the following "M" ones |
| 1920 | for(int i = 1; i < m * batch_size; i++) |
| 1921 | { |
| 1922 | memcpy(bias.data() + i * n, bias.data(), n * sizeof(T)); |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | return reference::activation_layer(reference::gemm<T>(lhs, rhs, bias, alpha, beta), act_info); |
| 1927 | } |
| 1928 | |
| 1929 | bool validate_result = true; |
| 1930 | TensorType _target{}; |
| 1931 | SimpleTensor<T> _reference{}; |
| 1932 | }; |
| 1933 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 1934 | } // namespace validation |
| 1935 | } // namespace test |
| 1936 | } // namespace arm_compute |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 1937 | #endif // ACL_TESTS_VALIDATION_FIXTURES_GEMMFIXTURE_H |