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, |
| 393 | bool interleave_rhs, bool transpose_rhs, DataType data_type, float alpha) |
| 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 | |
| 410 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha); |
| 411 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha); |
| 412 | } |
| 413 | |
| 414 | protected: |
| 415 | template <typename U> |
| 416 | void fill(U &&tensor, int i) |
| 417 | { |
| 418 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 419 | library->fill(tensor, distribution, i); |
| 420 | |
| 421 | // Fill border with infinity in order to check the presence of NaN values (i.e. inf * 0) |
| 422 | std::uniform_real_distribution<> distribution_inf(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()); |
| 423 | library->fill_borders_with_garbage(tensor, distribution_inf, i); |
| 424 | } |
| 425 | |
| 426 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha) |
| 427 | { |
| 428 | // Create tensors |
| 429 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 430 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 431 | TensorType rhs_reshaped; |
| 432 | TensorType dst; |
| 433 | |
| 434 | const unsigned int M = lhs_shape[1]; |
| 435 | const unsigned int N = rhs_shape[0]; |
| 436 | const unsigned int K = lhs_shape[0]; |
| 437 | |
| 438 | // The output tensor will be auto-initialized within the function |
| 439 | |
| 440 | // Create and configure function |
| 441 | ReshapeRHSFunctionType reshape_rhs; |
| 442 | GEMMFunctionType gemm; |
| 443 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
| 444 | gemm.configure(&lhs, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K)); |
| 445 | |
| 446 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 447 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 448 | |
| 449 | // Allocate tensors |
| 450 | lhs.allocator()->allocate(); |
| 451 | rhs.allocator()->allocate(); |
| 452 | rhs_reshaped.allocator()->allocate(); |
| 453 | dst.allocator()->allocate(); |
| 454 | |
| 455 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 456 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 457 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 458 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 459 | |
| 460 | // Fill tensors |
| 461 | fill(AccessorType(lhs), 0); |
| 462 | fill(AccessorType(rhs), 1); |
| 463 | |
| 464 | // Compute GEMM |
| 465 | reshape_rhs.run(); |
| 466 | gemm.run(); |
| 467 | |
| 468 | return dst; |
| 469 | } |
| 470 | |
| 471 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha) |
| 472 | { |
| 473 | TensorShape dst_shape = lhs_shape; |
| 474 | dst_shape[0] = rhs_shape[0]; |
| 475 | dst_shape[1] = lhs_shape[1]; |
| 476 | |
| 477 | // Create reference |
| 478 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 479 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 480 | SimpleTensor<T> c{ dst_shape, data_type, 1 }; |
| 481 | |
| 482 | // Fill reference |
| 483 | fill(lhs, 0); |
| 484 | fill(rhs, 1); |
| 485 | |
| 486 | return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f); |
| 487 | } |
| 488 | |
| 489 | TensorType _target{}; |
| 490 | SimpleTensor<T> _reference{}; |
| 491 | }; |
| 492 | |
| 493 | template <typename TensorType, typename AccessorType, typename T, typename ReshapeRHSFunctionType, typename GEMMFunctionType> |
| 494 | class GEMMMatrixMultiplyReshapedOnlyRHS3DValidationFixture : public framework::Fixture |
| 495 | { |
| 496 | public: |
| 497 | template <typename...> |
| 498 | 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, |
| 499 | bool interleave_rhs, bool transpose_rhs, DataType data_type, float alpha) |
| 500 | { |
| 501 | GEMMLHSMatrixInfo lhs_info; |
| 502 | lhs_info.m0 = m0; |
| 503 | lhs_info.k0 = k0; |
| 504 | |
| 505 | GEMMRHSMatrixInfo rhs_info; |
| 506 | rhs_info.n0 = n0; |
| 507 | rhs_info.k0 = k0; |
| 508 | rhs_info.h0 = h0; |
| 509 | rhs_info.interleave = interleave_rhs; |
| 510 | rhs_info.transpose = transpose_rhs; |
| 511 | |
| 512 | // In case of GEMM3D, m is the product between m_w and m_h |
| 513 | const unsigned int m = m_w * m_h; |
| 514 | |
| 515 | // Set the tensor shapes for LHS and RHS matrices |
| 516 | const TensorShape lhs_shape(k, m, batch_size); |
| 517 | const TensorShape rhs_shape(n, k, batch_size); |
| 518 | |
| 519 | _target = compute_target(lhs_shape, rhs_shape, lhs_info, rhs_info, data_type, alpha, m_h); |
| 520 | _reference = compute_reference(lhs_shape, rhs_shape, data_type, alpha, m_h); |
| 521 | } |
| 522 | |
| 523 | protected: |
| 524 | template <typename U> |
| 525 | void fill(U &&tensor, int i) |
| 526 | { |
| 527 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 528 | library->fill(tensor, distribution, i); |
| 529 | } |
| 530 | |
| 531 | TensorType compute_target(const TensorShape &lhs_shape, const TensorShape &rhs_shape, const GEMMLHSMatrixInfo &lhs_info, const GEMMRHSMatrixInfo &rhs_info, DataType data_type, float alpha, |
| 532 | unsigned int m_h) |
| 533 | { |
| 534 | // Create tensors |
| 535 | TensorType lhs = create_tensor<TensorType>(lhs_shape, data_type, 1); |
| 536 | TensorType rhs = create_tensor<TensorType>(rhs_shape, data_type, 1); |
| 537 | TensorType rhs_reshaped; |
| 538 | TensorType dst; |
| 539 | |
| 540 | const unsigned int M = lhs_shape[1]; |
| 541 | const unsigned int N = rhs_shape[0]; |
| 542 | const unsigned int K = lhs_shape[0]; |
| 543 | |
| 544 | // The output tensor will be auto-initialized within the function |
| 545 | |
| 546 | // Create and configure function |
| 547 | ReshapeRHSFunctionType reshape_rhs; |
| 548 | GEMMFunctionType gemm; |
| 549 | reshape_rhs.configure(&rhs, &rhs_reshaped, rhs_info); |
| 550 | gemm.configure(&lhs, &rhs_reshaped, &dst, alpha, lhs_info, rhs_info, GEMMReshapeInfo(M, N, K, 1, 1, m_h)); |
| 551 | |
| 552 | ARM_COMPUTE_EXPECT(lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 553 | ARM_COMPUTE_EXPECT(rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 554 | |
| 555 | // Allocate tensors |
| 556 | lhs.allocator()->allocate(); |
| 557 | rhs.allocator()->allocate(); |
| 558 | rhs_reshaped.allocator()->allocate(); |
| 559 | dst.allocator()->allocate(); |
| 560 | |
| 561 | ARM_COMPUTE_EXPECT(!lhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 562 | ARM_COMPUTE_EXPECT(!rhs.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 563 | ARM_COMPUTE_EXPECT(!rhs_reshaped.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 564 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 565 | |
| 566 | // Fill tensors |
| 567 | fill(AccessorType(lhs), 0); |
| 568 | fill(AccessorType(rhs), 1); |
| 569 | |
| 570 | // Compute GEMM |
| 571 | reshape_rhs.run(); |
| 572 | gemm.run(); |
| 573 | |
| 574 | return dst; |
| 575 | } |
| 576 | |
| 577 | SimpleTensor<T> compute_reference(const TensorShape &lhs_shape, const TensorShape &rhs_shape, DataType data_type, float alpha, unsigned int m_h) |
| 578 | { |
| 579 | TensorShape dst_shape = lhs_shape; |
| 580 | dst_shape.set(0, rhs_shape[0]); |
| 581 | dst_shape.set(1, lhs_shape[1] / m_h); |
| 582 | dst_shape.set(2, m_h); |
| 583 | dst_shape.set(3, lhs_shape[2]); |
| 584 | |
| 585 | // Create reference |
| 586 | SimpleTensor<T> lhs{ lhs_shape, data_type, 1 }; |
| 587 | SimpleTensor<T> rhs{ rhs_shape, data_type, 1 }; |
| 588 | SimpleTensor<T> c{ dst_shape, data_type, 1 }; |
| 589 | |
| 590 | // Fill reference |
| 591 | fill(lhs, 0); |
| 592 | fill(rhs, 1); |
| 593 | |
| 594 | return reference::gemm<T>(lhs, rhs, c, alpha, 0.0f); |
| 595 | } |
| 596 | |
| 597 | TensorType _target{}; |
| 598 | SimpleTensor<T> _reference{}; |
| 599 | }; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 600 | } // namespace validation |
| 601 | } // namespace test |
| 602 | } // namespace arm_compute |
| 603 | #endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */ |