Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool reinterpret_input_as_3d = false, bool reinterpret_ouput_as_3d = false> |
| 46 | class GEMMValidationFixture : public framework::Fixture |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | template <typename...> |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 50 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 51 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 52 | _data_type = data_type; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 53 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 54 | _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type); |
| 55 | _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] | 56 | } |
| 57 | |
| 58 | protected: |
| 59 | template <typename U> |
| 60 | void fill(U &&tensor, int i) |
| 61 | { |
| 62 | switch(tensor.data_type()) |
| 63 | { |
| 64 | case DataType::F16: |
| 65 | case DataType::F32: |
| 66 | { |
| 67 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 68 | library->fill(tensor, distribution, i); |
| 69 | break; |
| 70 | } |
| 71 | default: |
| 72 | library->fill_tensor_uniform(tensor, i); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | TensorType compute_target(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] | 77 | DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 78 | { |
| 79 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 80 | TensorType a = create_tensor<TensorType>(shape_a, data_type, 1); |
| 81 | TensorType b = create_tensor<TensorType>(shape_b, data_type, 1); |
| 82 | TensorType c = create_tensor<TensorType>(shape_c, data_type, 1); |
| 83 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 84 | |
| 85 | // Create and configure function |
| 86 | FunctionType gemm; |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 87 | // The GEMMinfo includes the values of the depth in case of reinterpreted 3d output. |
| 88 | // 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 = 1), |
| 89 | // 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 | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 90 | gemm.configure(&a, &b, &c, &dst, alpha, beta, GEMMInfo(false, false, false, (reinterpret_ouput_as_3d ? output_shape[2] : 1), reinterpret_input_as_3d)); |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 91 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 93 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 94 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 95 | |
| 96 | // Allocate tensors |
| 97 | a.allocator()->allocate(); |
| 98 | b.allocator()->allocate(); |
| 99 | c.allocator()->allocate(); |
| 100 | dst.allocator()->allocate(); |
| 101 | |
| 102 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 105 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 106 | |
| 107 | // Fill tensors |
| 108 | fill(AccessorType(a), 0); |
| 109 | fill(AccessorType(b), 1); |
| 110 | fill(AccessorType(c), 2); |
| 111 | |
| 112 | // Compute GEMM function |
| 113 | gemm.run(); |
| 114 | |
| 115 | return dst; |
| 116 | } |
| 117 | |
| 118 | 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] | 119 | DataType data_type) |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 120 | { |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 121 | TensorShape shape_a_to_use = shape_a; |
| 122 | if(reinterpret_input_as_3d) |
| 123 | { |
| 124 | // Collapse the second and third dimension if the input is 3D |
| 125 | shape_a_to_use.collapse(2U, 1U); |
| 126 | } |
| 127 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 128 | // Create reference |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 129 | SimpleTensor<T> a{ shape_a_to_use, data_type, 1 }; |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 130 | SimpleTensor<T> b{ shape_b, data_type, 1 }; |
| 131 | SimpleTensor<T> c{ shape_c, data_type, 1 }; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 132 | |
| 133 | // Fill reference |
| 134 | fill(a, 0); |
| 135 | fill(b, 1); |
| 136 | fill(c, 2); |
| 137 | |
| 138 | return reference::gemm<T>(a, b, c, alpha, beta); |
| 139 | } |
| 140 | |
| 141 | TensorType _target{}; |
| 142 | SimpleTensor<T> _reference{}; |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 143 | DataType _data_type{}; |
| 144 | }; |
| 145 | |
Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 146 | } // namespace validation |
| 147 | } // namespace test |
| 148 | } // namespace arm_compute |
| 149 | #endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */ |