Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | b87b95e | 2019-01-21 17:14:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_GEMM_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_GEMM_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 34 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/GEMM.h" |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool disable_c = false, bool reinterpret_input_as_3d = false, bool reinterpret_ouput_as_3d = false> |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 46 | class GEMMValidationFixture : public framework::Fixture |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | template <typename...> |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 50 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, bool pretranspose, DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 51 | { |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 52 | _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, pretranspose, data_type); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 53 | _reference = compute_reference(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | protected: |
| 57 | template <typename U> |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 58 | 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] | 59 | { |
| 60 | switch(tensor.data_type()) |
| 61 | { |
| 62 | case DataType::F16: |
| 63 | case DataType::F32: |
| 64 | { |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 65 | std::uniform_real_distribution<> distribution(lo, hi); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 66 | library->fill(tensor, distribution, i); |
| 67 | break; |
| 68 | } |
| 69 | default: |
| 70 | library->fill_tensor_uniform(tensor, i); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta, |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 75 | bool pretranspose, DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 76 | { |
| 77 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 78 | TensorType a = create_tensor<TensorType>(shape_a, data_type, 1); |
| 79 | TensorType b = create_tensor<TensorType>(shape_b, data_type, 1); |
| 80 | TensorType c = create_tensor<TensorType>(shape_c, data_type, 1); |
| 81 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 82 | |
| 83 | // Create and configure function |
| 84 | FunctionType gemm; |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 85 | // 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] | 86 | // 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] | 87 | // in the other case we have to use the reinterpreted version of GEMM (depth_output_reinterpreted_as_3D = depth of the 3D output). |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 88 | gemm.configure(&a, &b, (disable_c) ? nullptr : &c, &dst, alpha, beta, GEMMInfo(false, false, false, (reinterpret_ouput_as_3d ? output_shape[2] : 0), reinterpret_input_as_3d)); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 89 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 90 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 91 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 93 | |
| 94 | // Allocate tensors |
| 95 | a.allocator()->allocate(); |
| 96 | b.allocator()->allocate(); |
| 97 | c.allocator()->allocate(); |
| 98 | dst.allocator()->allocate(); |
| 99 | |
| 100 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 102 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | |
| 105 | // Fill tensors |
| 106 | fill(AccessorType(a), 0); |
| 107 | fill(AccessorType(b), 1); |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 108 | if(!disable_c) |
| 109 | { |
| 110 | fill(AccessorType(c), 2); |
| 111 | } |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 112 | |
| 113 | // Compute GEMM function |
| 114 | gemm.run(); |
| 115 | |
| 116 | return dst; |
| 117 | } |
| 118 | |
| 119 | SimpleTensor<T> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta, |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 120 | DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 121 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 122 | TensorShape shape_a_to_use = shape_a; |
| 123 | if(reinterpret_input_as_3d) |
| 124 | { |
| 125 | // Collapse the second and third dimension if the input is 3D |
| 126 | shape_a_to_use.collapse(2U, 1U); |
| 127 | } |
| 128 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 129 | // Create reference |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 130 | SimpleTensor<T> a{ shape_a_to_use, data_type, 1 }; |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 131 | SimpleTensor<T> b{ shape_b, data_type, 1 }; |
| 132 | SimpleTensor<T> c{ shape_c, data_type, 1 }; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 133 | |
| 134 | // Fill reference |
| 135 | fill(a, 0); |
| 136 | fill(b, 1); |
Pablo Tello | 0e37b5c | 2018-10-30 11:18:37 +0000 | [diff] [blame] | 137 | if(!disable_c) |
| 138 | { |
| 139 | fill(c, 2); |
| 140 | return reference::gemm<T>(a, b, c, alpha, beta); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | // Setting beta to 0 will effectively disable C for the |
| 145 | // computation of the reference: alpha * A * B + 0 * C |
| 146 | return reference::gemm<T>(a, b, c, alpha, 0.f); |
| 147 | } |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TensorType _target{}; |
| 151 | SimpleTensor<T> _reference{}; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 154 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 155 | class GEMMMatrixMultiplyReshapedValidationFixture : public framework::Fixture |
| 156 | { |
| 157 | public: |
| 158 | template <typename...> |
| 159 | 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 | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 160 | bool interleave_rhs, DataType data_type, float alpha) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 161 | { |
| 162 | GEMMLHSMatrixInfo lhs_info; |
| 163 | lhs_info.m0 = m0; |
| 164 | lhs_info.k0 = k0; |
| 165 | lhs_info.v0 = v0; |
| 166 | lhs_info.interleave = interleave_lhs; |
| 167 | lhs_info.transpose = false; |
| 168 | |
| 169 | GEMMRHSMatrixInfo rhs_info; |
| 170 | rhs_info.n0 = n0; |
| 171 | rhs_info.k0 = k0; |
| 172 | rhs_info.h0 = h0; |
| 173 | rhs_info.interleave = interleave_rhs; |
| 174 | rhs_info.transpose = true; |
| 175 | |
| 176 | // Set the tensor shapes for LHS and RHS matrices |
| 177 | const TensorShape lhs_shape(k, m, batch_size); |
| 178 | const TensorShape rhs_shape(n, k, batch_size); |
| 179 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 180 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha); |
| 181 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | protected: |
| 185 | template <typename U> |
| 186 | void fill(U &&tensor, int i) |
| 187 | { |
| 188 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 189 | library->fill(tensor, distribution, i); |
Gian Marco Iodice | b87b95e | 2019-01-21 17:14:31 +0000 | [diff] [blame] | 190 | |
| 191 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
| 192 | std::uniform_real_distribution<> distribution_inf(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()); |
| 193 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 196 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 197 | { |
| 198 | // Create tensors |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 199 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 200 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 201 | TensorType lhs_reshaped; |
| 202 | TensorType rhs_reshaped; |
| 203 | TensorType dst; |
| 204 | |
| 205 | const unsigned int M = lhs_shape[1]; |
| 206 | const unsigned int N = rhs_shape[0]; |
| 207 | const unsigned int K = lhs_shape[0]; |
| 208 | |
| 209 | // The output tensor will be auto-initialized within the function |
| 210 | |
| 211 | // Create and configure function |
| 212 | ReshapeLHSFunctionType reshape_lhs; |
| 213 | ReshapeRHSFunctionType reshape_rhs; |
| 214 | GEMMFunctionType gemm; |
| 215 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 216 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 217 | gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K)); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 218 | |
| 219 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 220 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 221 | |
| 222 | // Allocate tensors |
| 223 | lhs.allocator()->allocate(); |
| 224 | rhs.allocator()->allocate(); |
| 225 | lhs_reshaped.allocator()->allocate(); |
| 226 | rhs_reshaped.allocator()->allocate(); |
| 227 | dst.allocator()->allocate(); |
| 228 | |
| 229 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 230 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 231 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 232 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 233 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 234 | |
| 235 | // Fill tensors |
| 236 | fill(AccessorType(lhs), 0); |
| 237 | fill(AccessorType(rhs), 1); |
| 238 | |
| 239 | // Compute GEMM |
| 240 | reshape_lhs.run(); |
| 241 | reshape_rhs.run(); |
| 242 | gemm.run(); |
| 243 | |
| 244 | return dst; |
| 245 | } |
| 246 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 247 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha) |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 248 | { |
| 249 | TensorShape dst_shape = lhs_shape; |
| 250 | dst_shape[0] = rhs_shape[0]; |
| 251 | dst_shape[1] = lhs_shape[1]; |
| 252 | |
| 253 | // Create reference |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 254 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 255 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 256 | SimpleTensor<T> c{ dst_shape, data_type, 1 }; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 257 | |
| 258 | // Fill reference |
| 259 | fill(lhs, 0); |
| 260 | fill(rhs, 1); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 261 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 262 | return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f); |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Gian Marco Iodice | 9382ab3 | 2018-12-17 15:12:07 +0000 | [diff] [blame] | 265 | TensorType _target{}; |
| 266 | SimpleTensor<T> _reference{}; |
| 267 | }; |
| 268 | |
| 269 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeLHSFunctionType, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 270 | class GEMMMatrixMultiplyReshaped3DValidationFixture : public framework::Fixture |
| 271 | { |
| 272 | public: |
| 273 | template <typename...> |
| 274 | 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, |
| 275 | bool interleave_lhs, |
| 276 | bool interleave_rhs, DataType data_type, float alpha) |
| 277 | { |
| 278 | GEMMLHSMatrixInfo lhs_info; |
| 279 | lhs_info.m0 = m0; |
| 280 | lhs_info.k0 = k0; |
| 281 | lhs_info.v0 = v0; |
| 282 | lhs_info.interleave = interleave_lhs; |
| 283 | lhs_info.transpose = false; |
| 284 | |
| 285 | GEMMRHSMatrixInfo rhs_info; |
| 286 | rhs_info.n0 = n0; |
| 287 | rhs_info.k0 = k0; |
| 288 | rhs_info.h0 = h0; |
| 289 | rhs_info.interleave = interleave_rhs; |
| 290 | rhs_info.transpose = true; |
| 291 | |
| 292 | // In case of GEMM3D, m is the product between m_w and m_h |
| 293 | const unsigned int m = m_w * m_h; |
| 294 | |
| 295 | // Set the tensor shapes for LHS and RHS matrices |
| 296 | const TensorShape lhs_shape(k, m, batch_size); |
| 297 | const TensorShape rhs_shape(n, k, batch_size); |
| 298 | |
| 299 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha, m_h); |
| 300 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, m_h); |
| 301 | } |
| 302 | |
| 303 | protected: |
| 304 | template <typename U> |
| 305 | void fill(U &&tensor, int i) |
| 306 | { |
| 307 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 308 | library->fill(tensor, distribution, i); |
| 309 | } |
| 310 | |
| 311 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha, |
| 312 | unsigned int m_h) |
| 313 | { |
| 314 | // Create tensors |
| 315 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 316 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 317 | TensorType lhs_reshaped; |
| 318 | TensorType rhs_reshaped; |
| 319 | TensorType dst; |
| 320 | |
| 321 | const unsigned int M = lhs_shape[1]; |
| 322 | const unsigned int N = rhs_shape[0]; |
| 323 | const unsigned int K = lhs_shape[0]; |
| 324 | |
| 325 | // The output tensor will be auto-initialized within the function |
| 326 | |
| 327 | // Create and configure function |
| 328 | ReshapeLHSFunctionType reshape_lhs; |
| 329 | ReshapeRHSFunctionType reshape_rhs; |
| 330 | GEMMFunctionType gemm; |
| 331 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 332 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
| 333 | gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h)); |
| 334 | |
| 335 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 336 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 337 | |
| 338 | // Allocate tensors |
| 339 | lhs.allocator()->allocate(); |
| 340 | rhs.allocator()->allocate(); |
| 341 | lhs_reshaped.allocator()->allocate(); |
| 342 | rhs_reshaped.allocator()->allocate(); |
| 343 | dst.allocator()->allocate(); |
| 344 | |
| 345 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 346 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 347 | ARM_COMPUTE_EXPECT(!lhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 348 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 349 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 350 | |
| 351 | // Fill tensors |
| 352 | fill(AccessorType(lhs), 0); |
| 353 | fill(AccessorType(rhs), 1); |
| 354 | |
| 355 | // Compute GEMM |
| 356 | reshape_lhs.run(); |
| 357 | reshape_rhs.run(); |
| 358 | gemm.run(); |
| 359 | |
| 360 | return dst; |
| 361 | } |
| 362 | |
| 363 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, unsigned int m_h) |
| 364 | { |
| 365 | TensorShape dst_shape = lhs_shape; |
| 366 | dst_shape.set(0, rhs_shape[0]); |
| 367 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 368 | dst_shape.set(2, m_h); |
| 369 | dst_shape.set(3, lhs_shape[2]); |
| 370 | |
| 371 | // Create reference |
| 372 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 373 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 374 | SimpleTensor<T> c{ dst_shape, data_type, 1 }; |
| 375 | |
| 376 | // Fill reference |
| 377 | fill(lhs, 0); |
| 378 | fill(rhs, 1); |
| 379 | |
| 380 | return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f); |
| 381 | } |
| 382 | |
| 383 | TensorType _target{}; |
| 384 | SimpleTensor<T> _reference{}; |
Gian Marco Iodice | bf9731e | 2018-12-12 10:18:04 +0000 | [diff] [blame] | 385 | }; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 386 | |
| 387 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 388 | class GEMMMatrixMultiplyReshapedOnlyRHSValidationFixture : public framework::Fixture |
| 389 | { |
| 390 | public: |
| 391 | template <typename...> |
| 392 | 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, |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 393 | bool interleave_rhs, bool transpose_rhs, DataType data_type, float alpha, float beta, bool broadcast_bias) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 394 | { |
| 395 | GEMMLHSMatrixInfo lhs_info; |
| 396 | lhs_info.m0 = m0; |
| 397 | lhs_info.k0 = k0; |
| 398 | |
| 399 | GEMMRHSMatrixInfo rhs_info; |
| 400 | rhs_info.n0 = n0; |
| 401 | rhs_info.k0 = k0; |
| 402 | rhs_info.h0 = h0; |
| 403 | rhs_info.interleave = interleave_rhs; |
| 404 | rhs_info.transpose = transpose_rhs; |
| 405 | |
| 406 | // Set the tensor shapes for LHS and RHS matrices |
| 407 | const TensorShape lhs_shape(k, m, batch_size); |
| 408 | const TensorShape rhs_shape(n, k, batch_size); |
| 409 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 410 | TensorShape bias_shape; |
| 411 | if(broadcast_bias) |
| 412 | { |
| 413 | bias_shape = TensorShape(n, 1, 1); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | bias_shape = TensorShape(n, m, batch_size); |
| 418 | } |
| 419 | |
| 420 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, broadcast_bias); |
| 421 | _reference = compute_reference(lhs_shape, rhs_shape, bias_shape, data_type, alpha, beta, broadcast_bias); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | protected: |
| 425 | template <typename U> |
| 426 | void fill(U &&tensor, int i) |
| 427 | { |
| 428 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 429 | library->fill(tensor, distribution, i); |
| 430 | |
| 431 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
| 432 | std::uniform_real_distribution<> distribution_inf(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()); |
| 433 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 434 | } |
| 435 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 436 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 437 | DataType data_type, float alpha, float beta, bool broadcast_bias) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 438 | { |
| 439 | // Create tensors |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 440 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 441 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 442 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 443 | TensorType rhs_reshaped; |
| 444 | TensorType dst; |
| 445 | |
| 446 | const unsigned int M = lhs_shape[1]; |
| 447 | const unsigned int N = rhs_shape[0]; |
| 448 | const unsigned int K = lhs_shape[0]; |
| 449 | |
| 450 | // The output tensor will be auto-initialized within the function |
| 451 | |
| 452 | // Create and configure function |
| 453 | ReshapeRHSFunctionType reshape_rhs; |
| 454 | GEMMFunctionType gemm; |
| 455 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 456 | gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, 0, false, broadcast_bias)); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 457 | |
| 458 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 459 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 460 | |
| 461 | // Allocate tensors |
| 462 | lhs.allocator()->allocate(); |
| 463 | rhs.allocator()->allocate(); |
| 464 | rhs_reshaped.allocator()->allocate(); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 465 | bias.allocator()->allocate(); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 466 | dst.allocator()->allocate(); |
| 467 | |
| 468 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 469 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 470 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 471 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 472 | |
| 473 | // Fill tensors |
| 474 | fill(AccessorType(lhs), 0); |
| 475 | fill(AccessorType(rhs), 1); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 476 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 477 | |
| 478 | // Compute GEMM |
| 479 | reshape_rhs.run(); |
| 480 | gemm.run(); |
| 481 | |
| 482 | return dst; |
| 483 | } |
| 484 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 485 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, DataType data_type, float alpha, float beta, bool broadcast_bias) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 486 | { |
| 487 | TensorShape dst_shape = lhs_shape; |
| 488 | dst_shape[0] = rhs_shape[0]; |
| 489 | dst_shape[1] = lhs_shape[1]; |
| 490 | |
| 491 | // Create reference |
| 492 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 493 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 494 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 495 | |
| 496 | const int n = rhs_shape[0]; |
| 497 | const int m = lhs_shape[1]; |
| 498 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 499 | |
| 500 | // Fill reference |
| 501 | fill(lhs, 0); |
| 502 | fill(rhs, 1); |
| 503 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 504 | if(broadcast_bias) |
| 505 | { |
| 506 | SimpleTensor<T> tmp{ bias_shape, data_type, 1 }; |
| 507 | fill(tmp, 2); |
| 508 | for(int i = 0; i < m * batch_size; i++) |
| 509 | { |
| 510 | memcpy(bias.data() + i * n, tmp.data(), n * sizeof(T)); |
| 511 | } |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | fill(bias, 2); |
| 516 | } |
| 517 | |
| 518 | return (reference::gemm<T>(lhs, rhs, bias, alpha, beta)); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | TensorType _target{}; |
| 522 | SimpleTensor<T> _reference{}; |
| 523 | }; |
| 524 | |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 525 | template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType> |
| 526 | class GEMMMatrixMultiplyNativeValidationFixture : public framework::Fixture |
| 527 | { |
| 528 | public: |
| 529 | template <typename...> |
| 530 | 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) |
| 531 | { |
| 532 | GEMMLHSMatrixInfo lhs_info; |
| 533 | lhs_info.m0 = m0; |
| 534 | lhs_info.k0 = k0; |
| 535 | |
| 536 | GEMMRHSMatrixInfo rhs_info; |
| 537 | rhs_info.n0 = n0; |
| 538 | rhs_info.k0 = k0; |
| 539 | |
| 540 | // Set the tensor shapes for LHS and RHS matrices |
| 541 | const TensorShape lhs_shape(k, m, batch_size); |
| 542 | const TensorShape rhs_shape(n, k, batch_size); |
| 543 | |
| 544 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha); |
| 545 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha); |
| 546 | } |
| 547 | |
| 548 | protected: |
| 549 | template <typename U> |
| 550 | void fill(U &&tensor, int i) |
| 551 | { |
| 552 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 553 | library->fill(tensor, distribution, i); |
| 554 | |
| 555 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
| 556 | std::uniform_real_distribution<> distribution_inf(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()); |
| 557 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 558 | } |
| 559 | |
| 560 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha) |
| 561 | { |
| 562 | // Create tensors |
| 563 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 564 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 565 | TensorType dst; |
| 566 | |
| 567 | const unsigned int M = lhs_shape[1]; |
| 568 | const unsigned int N = rhs_shape[0]; |
| 569 | const unsigned int K = lhs_shape[0]; |
| 570 | |
| 571 | // Create and configure function |
| 572 | GEMMFunctionType gemm; |
| 573 | gemm.configure(&lhs, &rhs, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K)); |
| 574 | |
| 575 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 576 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 577 | |
| 578 | // Allocate tensors |
| 579 | lhs.allocator()->allocate(); |
| 580 | rhs.allocator()->allocate(); |
| 581 | dst.allocator()->allocate(); |
| 582 | |
| 583 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 584 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 585 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 586 | |
| 587 | // Fill tensors |
| 588 | fill(AccessorType(lhs), 0); |
| 589 | fill(AccessorType(rhs), 1); |
| 590 | |
| 591 | // Compute GEMM |
| 592 | gemm.run(); |
| 593 | |
| 594 | return dst; |
| 595 | } |
| 596 | |
| 597 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha) |
| 598 | { |
| 599 | TensorShape dst_shape = lhs_shape; |
| 600 | dst_shape[0] = rhs_shape[0]; |
| 601 | dst_shape[1] = lhs_shape[1]; |
| 602 | |
| 603 | // Create reference |
| 604 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 605 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 606 | SimpleTensor<T> c{ dst_shape, data_type, 1 }; |
| 607 | |
| 608 | // Fill reference |
| 609 | fill(lhs, 0); |
| 610 | fill(rhs, 1); |
| 611 | |
| 612 | return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f); |
| 613 | } |
| 614 | |
| 615 | TensorType _target{}; |
| 616 | SimpleTensor<T> _reference{}; |
| 617 | }; |
| 618 | |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 619 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 620 | class GEMMMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture |
| 621 | { |
| 622 | public: |
| 623 | template <typename...> |
| 624 | 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, |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 625 | bool interleave_rhs, bool transpose_rhs, DataType data_type, float alpha, float beta) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 626 | { |
| 627 | GEMMLHSMatrixInfo lhs_info; |
| 628 | lhs_info.m0 = m0; |
| 629 | lhs_info.k0 = k0; |
| 630 | |
| 631 | GEMMRHSMatrixInfo rhs_info; |
| 632 | rhs_info.n0 = n0; |
| 633 | rhs_info.k0 = k0; |
| 634 | rhs_info.h0 = h0; |
| 635 | rhs_info.interleave = interleave_rhs; |
| 636 | rhs_info.transpose = transpose_rhs; |
| 637 | |
| 638 | // In case of GEMM3D, m is the product between m_w and m_h |
| 639 | const unsigned int m = m_w * m_h; |
| 640 | |
| 641 | // Set the tensor shapes for LHS and RHS matrices |
| 642 | const TensorShape lhs_shape(k, m, batch_size); |
| 643 | const TensorShape rhs_shape(n, k, batch_size); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 644 | const TensorShape bias_shape(n, 1, 1); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 645 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 646 | _target = compute_target(lhs_shape, rhs_shape, bias_shape, lhs_info, rhs_info, data_type, alpha, beta, m_h); |
| 647 | _reference = compute_reference(lhs_shape, rhs_shape, bias_shape, data_type, alpha, beta, m_h); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | protected: |
| 651 | template <typename U> |
| 652 | void fill(U &&tensor, int i) |
| 653 | { |
| 654 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 655 | library->fill(tensor, distribution, i); |
| 656 | } |
| 657 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 658 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, |
| 659 | DataType data_type, float alpha, float beta, |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 660 | unsigned int m_h) |
| 661 | { |
| 662 | // Create tensors |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 663 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 664 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 665 | TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 666 | TensorType rhs_reshaped; |
| 667 | TensorType dst; |
| 668 | |
| 669 | const unsigned int M = lhs_shape[1]; |
| 670 | const unsigned int N = rhs_shape[0]; |
| 671 | const unsigned int K = lhs_shape[0]; |
| 672 | |
| 673 | // The output tensor will be auto-initialized within the function |
| 674 | |
| 675 | // Create and configure function |
| 676 | ReshapeRHSFunctionType reshape_rhs; |
| 677 | GEMMFunctionType gemm; |
| 678 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 679 | gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h, false, true)); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 680 | |
| 681 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 682 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 683 | |
| 684 | // Allocate tensors |
| 685 | lhs.allocator()->allocate(); |
| 686 | rhs.allocator()->allocate(); |
| 687 | rhs_reshaped.allocator()->allocate(); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 688 | bias.allocator()->allocate(); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 689 | dst.allocator()->allocate(); |
| 690 | |
| 691 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 692 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 693 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 694 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 695 | |
| 696 | // Fill tensors |
| 697 | fill(AccessorType(lhs), 0); |
| 698 | fill(AccessorType(rhs), 1); |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 699 | fill(AccessorType(bias), 2); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 700 | |
| 701 | // Compute GEMM |
| 702 | reshape_rhs.run(); |
| 703 | gemm.run(); |
| 704 | |
| 705 | return dst; |
| 706 | } |
| 707 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 708 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const TensorShape &bias_shape, DataType data_type, float alpha, float beta, unsigned int m_h) |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 709 | { |
| 710 | TensorShape dst_shape = lhs_shape; |
| 711 | dst_shape.set(0, rhs_shape[0]); |
| 712 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 713 | dst_shape.set(2, m_h); |
| 714 | dst_shape.set(3, lhs_shape[2]); |
| 715 | |
| 716 | // Create reference |
| 717 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 718 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 719 | SimpleTensor<T> bias{ dst_shape, data_type, 1 }; |
| 720 | |
| 721 | const int n = rhs_shape[0]; |
| 722 | const int m = lhs_shape[1]; |
| 723 | const int batch_size = lhs_shape[2]; |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 724 | |
| 725 | // Fill reference |
| 726 | fill(lhs, 0); |
| 727 | fill(rhs, 1); |
| 728 | |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 729 | SimpleTensor<T> tmp{ bias_shape, data_type, 1 }; |
| 730 | fill(tmp, 2); |
| 731 | for(int i = 0; i < m * batch_size; i++) |
| 732 | { |
| 733 | memcpy(bias.data() + i * n, tmp.data(), n * sizeof(T)); |
| 734 | } |
| 735 | |
| 736 | return reference::gemm<T>(lhs, rhs, bias, alpha, beta); |
Gian Marco Iodice | adc5395 | 2019-02-15 11:10:31 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | TensorType _target{}; |
| 740 | SimpleTensor<T> _reference{}; |
| 741 | }; |
giuros01 | b3204e7 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 742 | |
| 743 | template <typename TensorType, typename AccessorType, typename T, typename GEMMFunctionType> |
| 744 | class GEMMMatrixMultiplyNative3DValidationFixture : public framework::Fixture |
| 745 | { |
| 746 | public: |
| 747 | template <typename...> |
| 748 | 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) |
| 749 | { |
| 750 | GEMMLHSMatrixInfo lhs_info; |
| 751 | lhs_info.m0 = m0; |
| 752 | lhs_info.k0 = k0; |
| 753 | |
| 754 | GEMMRHSMatrixInfo rhs_info; |
| 755 | rhs_info.n0 = n0; |
| 756 | rhs_info.k0 = k0; |
| 757 | |
| 758 | // In case of GEMM3D, m is the product between m_w and m_h |
| 759 | const unsigned int m = m_w * m_h; |
| 760 | |
| 761 | // Set the tensor shapes for LHS and RHS matrices |
| 762 | const TensorShape lhs_shape(k, m, batch_size); |
| 763 | const TensorShape rhs_shape(n, k, batch_size); |
| 764 | |
| 765 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha, m_h); |
| 766 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, m_h); |
| 767 | } |
| 768 | |
| 769 | protected: |
| 770 | template <typename U> |
| 771 | void fill(U &&tensor, int i) |
| 772 | { |
| 773 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 774 | library->fill(tensor, distribution, i); |
| 775 | } |
| 776 | |
| 777 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha, |
| 778 | unsigned int m_h) |
| 779 | { |
| 780 | // Create tensors |
| 781 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 782 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 783 | TensorType rhs_reshaped; |
| 784 | TensorType dst; |
| 785 | |
| 786 | const unsigned int M = lhs_shape[1]; |
| 787 | const unsigned int N = rhs_shape[0]; |
| 788 | const unsigned int K = lhs_shape[0]; |
| 789 | |
| 790 | // The output tensor will be auto-initialized within the function |
| 791 | |
| 792 | // Create and configure function |
| 793 | GEMMFunctionType gemm; |
| 794 | gemm.configure(&lhs, &rhs, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h)); |
| 795 | |
| 796 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 797 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 798 | |
| 799 | // Allocate tensors |
| 800 | lhs.allocator()->allocate(); |
| 801 | rhs.allocator()->allocate(); |
| 802 | rhs_reshaped.allocator()->allocate(); |
| 803 | dst.allocator()->allocate(); |
| 804 | |
| 805 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 806 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 807 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 808 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 809 | |
| 810 | // Fill tensors |
| 811 | fill(AccessorType(lhs), 0); |
| 812 | fill(AccessorType(rhs), 1); |
| 813 | |
| 814 | // Compute GEMM |
| 815 | gemm.run(); |
| 816 | |
| 817 | return dst; |
| 818 | } |
| 819 | |
| 820 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, unsigned int m_h) |
| 821 | { |
| 822 | TensorShape dst_shape = lhs_shape; |
| 823 | dst_shape.set(0, rhs_shape[0]); |
| 824 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 825 | dst_shape.set(2, m_h); |
| 826 | dst_shape.set(3, lhs_shape[2]); |
| 827 | |
| 828 | // Create reference |
| 829 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 830 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 831 | SimpleTensor<T> c{ dst_shape, data_type, 1 }; |
| 832 | |
| 833 | // Fill reference |
| 834 | fill(lhs, 0); |
| 835 | fill(rhs, 1); |
| 836 | |
| 837 | return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f); |
| 838 | } |
| 839 | |
| 840 | TensorType _target{}; |
| 841 | SimpleTensor<T> _reference{}; |
| 842 | }; |
| 843 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 844 | } // namespace validation |
| 845 | } // namespace test |
| 846 | } // namespace arm_compute |
| 847 | #endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */ |