Moritz Pflanzer | 4dfc235 | 2017-08-02 14:51:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | { |
| 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 46 | class GEMMValidationFixedPointFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type, int fractional_bits) |
| 51 | { |
| 52 | _fractional_bits = fractional_bits; |
| 53 | _data_type = data_type; |
| 54 | |
| 55 | _target = compute_target(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, fractional_bits); |
| 56 | _reference = compute_reference(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, fractional_bits); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | template <typename U> |
| 61 | void fill(U &&tensor, int i) |
| 62 | { |
| 63 | switch(tensor.data_type()) |
| 64 | { |
| 65 | case DataType::F16: |
| 66 | case DataType::F32: |
| 67 | { |
| 68 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 69 | library->fill(tensor, distribution, i); |
| 70 | break; |
| 71 | } |
| 72 | default: |
| 73 | library->fill_tensor_uniform(tensor, i); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta, |
| 78 | DataType data_type, int fixed_point_position) |
| 79 | { |
| 80 | // Create tensors |
| 81 | TensorType a = create_tensor<TensorType>(shape_a, data_type, 1, fixed_point_position); |
| 82 | TensorType b = create_tensor<TensorType>(shape_b, data_type, 1, fixed_point_position); |
| 83 | TensorType c = create_tensor<TensorType>(shape_c, data_type, 1, fixed_point_position); |
| 84 | TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position); |
| 85 | |
| 86 | // Create and configure function |
| 87 | FunctionType gemm; |
| 88 | gemm.configure(&a, &b, &c, &dst, alpha, beta); |
| 89 | |
| 90 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 91 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 93 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 94 | |
| 95 | // Allocate tensors |
| 96 | a.allocator()->allocate(); |
| 97 | b.allocator()->allocate(); |
| 98 | c.allocator()->allocate(); |
| 99 | dst.allocator()->allocate(); |
| 100 | |
| 101 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 102 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 105 | |
| 106 | // Fill tensors |
| 107 | fill(AccessorType(a), 0); |
| 108 | fill(AccessorType(b), 1); |
| 109 | fill(AccessorType(c), 2); |
| 110 | |
| 111 | // Compute GEMM function |
| 112 | gemm.run(); |
| 113 | |
| 114 | return dst; |
| 115 | } |
| 116 | |
| 117 | SimpleTensor<T> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, const TensorShape &output_shape, float alpha, float beta, |
| 118 | DataType data_type, int fixed_point_position) |
| 119 | { |
| 120 | // Create reference |
| 121 | SimpleTensor<T> a{ shape_a, data_type, 1, fixed_point_position }; |
| 122 | SimpleTensor<T> b{ shape_b, data_type, 1, fixed_point_position }; |
| 123 | SimpleTensor<T> c{ shape_c, data_type, 1, fixed_point_position }; |
| 124 | |
| 125 | // Fill reference |
| 126 | fill(a, 0); |
| 127 | fill(b, 1); |
| 128 | fill(c, 2); |
| 129 | |
| 130 | return reference::gemm<T>(a, b, c, alpha, beta); |
| 131 | } |
| 132 | |
| 133 | TensorType _target{}; |
| 134 | SimpleTensor<T> _reference{}; |
| 135 | int _fractional_bits{}; |
| 136 | DataType _data_type{}; |
| 137 | }; |
| 138 | |
| 139 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 140 | class GEMMValidationFixture : public GEMMValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T> |
| 141 | { |
| 142 | public: |
| 143 | template <typename...> |
| 144 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, TensorShape output_shape, float alpha, float beta, DataType data_type) |
| 145 | { |
| 146 | GEMMValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape_a, shape_b, shape_c, output_shape, alpha, beta, data_type, 0); |
| 147 | } |
| 148 | }; |
| 149 | } // namespace validation |
| 150 | } // namespace test |
| 151 | } // namespace arm_compute |
| 152 | #endif /* ARM_COMPUTE_TEST_GEMM_FIXTURE */ |