Pablo Tello | 088cc7f | 2017-12-07 15:20:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, 2018 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_TRANSPOSE_1XW_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_GEMM_TRANSPOSE_1XW_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
| 34 | #include "tests/validation/Helpers.h" |
| 35 | #include "tests/validation/reference/GEMMTranspose1xW.h" |
| 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 GEMMTranspose1xWValidationFixedPointFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(size_t x, size_t y, DataType data_type, int fractional_bits) |
| 51 | { |
| 52 | _fractional_bits = fractional_bits; |
| 53 | _data_type = data_type; |
| 54 | const TensorShape shape_a(x, y); |
| 55 | const unsigned int transpose_w = 16 / data_size_from_type(data_type); |
| 56 | const TensorShape shape_b(static_cast<size_t>(y * transpose_w), static_cast<size_t>(std::ceil(x / static_cast<float>(transpose_w)))); |
| 57 | _target = compute_target(shape_a, shape_b, data_type, fractional_bits); |
| 58 | _reference = compute_reference(shape_a, shape_b, data_type, fractional_bits); |
| 59 | } |
| 60 | |
| 61 | protected: |
| 62 | template <typename U> |
| 63 | void fill(U &&tensor, int i) |
| 64 | { |
| 65 | switch(tensor.data_type()) |
| 66 | { |
| 67 | case DataType::F16: |
| 68 | case DataType::F32: |
| 69 | { |
| 70 | std::uniform_real_distribution<> distribution(-1.f, 1.f); |
| 71 | library->fill(tensor, distribution, i); |
| 72 | break; |
| 73 | } |
| 74 | default: |
| 75 | library->fill_tensor_uniform(tensor, i); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, DataType data_type, int fixed_point_position) |
| 81 | { |
| 82 | // Create tensors |
| 83 | TensorType a = create_tensor<TensorType>(shape_a, data_type, 1, fixed_point_position); |
| 84 | TensorType b = create_tensor<TensorType>(shape_b, data_type, 1, fixed_point_position); |
| 85 | |
| 86 | // Create and configure function |
| 87 | FunctionType f; |
| 88 | f.configure(&a, &b); |
| 89 | |
| 90 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 91 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | |
| 93 | // Allocate tensors |
| 94 | a.allocator()->allocate(); |
| 95 | b.allocator()->allocate(); |
| 96 | |
| 97 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 98 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 99 | |
| 100 | // Fill tensors |
| 101 | fill(AccessorType(a), 0); |
| 102 | fill(AccessorType(b), 1); |
| 103 | |
| 104 | // Compute GEMM function |
| 105 | f.run(); |
| 106 | |
| 107 | return b; |
| 108 | } |
| 109 | |
| 110 | SimpleTensor<T> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, DataType data_type, int fixed_point_position) |
| 111 | { |
| 112 | // Create reference |
| 113 | SimpleTensor<T> a{ shape_a, data_type, 1, fixed_point_position }; |
| 114 | |
| 115 | // Fill reference |
| 116 | fill(a, 0); |
| 117 | |
| 118 | return reference::gemm_transpose_1xW<T>(a); |
| 119 | } |
| 120 | |
| 121 | TensorType _target{}; |
| 122 | SimpleTensor<T> _reference{}; |
| 123 | int _fractional_bits{}; |
| 124 | DataType _data_type{}; |
| 125 | }; |
| 126 | |
| 127 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 128 | class GEMMTranspose1xWValidationFixture : public GEMMTranspose1xWValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T> |
| 129 | { |
| 130 | public: |
| 131 | template <typename...> |
| 132 | void setup(size_t x, size_t y, DataType data_type) |
| 133 | { |
| 134 | GEMMTranspose1xWValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(x, y, data_type, 0); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | } // namespace validation |
| 139 | } // namespace test |
| 140 | } // namespace arm_compute |
| 141 | #endif /* ARM_COMPUTE_TEST_GEMM_TRANSPOSE_1XW_FIXTURE */ |