Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Arm Limited. |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [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_GEMMLOWP_ASSEMBLY_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_GEMMLOWP_ASSEMBLY_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" |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [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/GEMMLowp.h" |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T2> |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 46 | class GEMMLowpAssemblyFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(size_t m, size_t n, size_t k) |
| 51 | { |
| 52 | const TensorShape shape_a(k, m); |
| 53 | const TensorShape shape_b(n, k); |
| 54 | const TensorShape shape_c(n, m); |
| 55 | _target = compute_target(shape_a, shape_b, shape_c); |
| 56 | _reference = compute_reference(shape_a, shape_b, shape_c); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | template <typename U> |
| 61 | void fill(U &&tensor, int i, int lo, int hi) |
| 62 | { |
| 63 | std::uniform_int_distribution<> distribution(lo, hi); |
| 64 | library->fill(tensor, distribution, i); |
| 65 | } |
| 66 | |
| 67 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c) |
| 68 | { |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 69 | DataType dt_in = std::is_same<T2, int8_t>::value ? DataType::S8 : DataType::U8; |
| 70 | |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 71 | // Create tensors |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 72 | TensorType a = create_tensor<TensorType>(shape_a, dt_in, 1); |
| 73 | TensorType b = create_tensor<TensorType>(shape_b, dt_in, 1); |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 74 | TensorType c = create_tensor<TensorType>(shape_c, DataType::S32, 1); |
| 75 | |
| 76 | // Create and configure function |
| 77 | FunctionType gemmlowp; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame] | 78 | gemmlowp.configure(&a, &b, nullptr, &c); |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 79 | |
| 80 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 81 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 82 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 83 | |
| 84 | // Allocate tensors |
| 85 | a.allocator()->allocate(); |
| 86 | b.allocator()->allocate(); |
| 87 | c.allocator()->allocate(); |
| 88 | |
| 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 | |
| 93 | // Fill tensors |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 94 | if(dt_in == DataType::S8) |
| 95 | { |
| 96 | fill(AccessorType(a), 0, -128, 127); |
| 97 | fill(AccessorType(b), 1, -128, 127); |
| 98 | } |
| 99 | else |
| 100 | { |
David Mansell | 101de50 | 2018-02-06 17:11:21 +0000 | [diff] [blame] | 101 | fill(AccessorType(a), 0, 0, 255); |
| 102 | fill(AccessorType(b), 1, 0, 255); |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 103 | } |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 104 | fill(AccessorType(c), 2, 0, 0); |
| 105 | |
| 106 | // Compute GEMM function |
| 107 | gemmlowp.run(); |
| 108 | return c; |
| 109 | } |
| 110 | |
| 111 | SimpleTensor<int32_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c) |
| 112 | { |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 113 | DataType dt = std::is_same<T2, int8_t>::value ? DataType::S8 : DataType::U8; |
| 114 | |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 115 | // Create reference |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 116 | SimpleTensor<T2> a{ shape_a, dt, 1 }; |
| 117 | SimpleTensor<T2> b{ shape_b, dt, 1 }; |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 118 | |
| 119 | // Fill reference |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 120 | if(dt == DataType::S8) |
| 121 | { |
| 122 | fill(a, 0, -128, 127); |
| 123 | fill(b, 1, -128, 127); |
| 124 | } |
| 125 | else |
| 126 | { |
David Mansell | 101de50 | 2018-02-06 17:11:21 +0000 | [diff] [blame] | 127 | fill(a, 0, 0, 255); |
| 128 | fill(b, 1, 0, 255); |
Michalis Spyrou | f3dfa27 | 2017-11-21 17:52:12 +0000 | [diff] [blame] | 129 | } |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 130 | |
Georgios Pinitas | ebf6b8a | 2018-09-24 16:31:08 +0100 | [diff] [blame] | 131 | return reference::gemmlowp<int32_t, T2>(a, b, shape_c); |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TensorType _target{}; |
| 135 | SimpleTensor<int32_t> _reference{}; |
| 136 | }; |
| 137 | |
| 138 | } // namespace validation |
| 139 | } // namespace test |
| 140 | } // namespace arm_compute |
| 141 | #endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */ |