Pablo Tello | 299025a | 2017-09-29 11:30:12 +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_GEMMLOWP_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_GEMMLOWP_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/CPP/GEMMLowp.h" |
| 35 | #include "tests/validation/Helpers.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> |
| 46 | class GEMMLowpOffsetValidationFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
Gian Marco | fa4cacd | 2017-10-18 17:05:02 +0100 | [diff] [blame] | 50 | void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, int32_t a_offset, int32_t b_offset, int32_t c_offset, int32_t c_mult_int, int32_t out_shift, DataType data_type) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 51 | { |
Gian Marco | fa4cacd | 2017-10-18 17:05:02 +0100 | [diff] [blame] | 52 | _target = compute_target(shape_a, shape_b, shape_c, a_offset, b_offset, c_offset, c_mult_int, out_shift, data_type); |
| 53 | _reference = compute_reference(shape_a, shape_b, shape_c, a_offset, b_offset, c_offset, c_mult_int, out_shift, data_type); |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | protected: |
| 57 | template <typename U> |
| 58 | void fill(U &&tensor, int i) |
| 59 | { |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 60 | ARM_COMPUTE_ERROR_ON(tensor.data_type() != DataType::S8); |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 61 | std::uniform_int_distribution<> distribution(0, 3); |
| 62 | library->fill(tensor, distribution, i); |
| 63 | } |
| 64 | |
| 65 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, |
Gian Marco | fa4cacd | 2017-10-18 17:05:02 +0100 | [diff] [blame] | 66 | int32_t a_offset, int32_t b_offset, int32_t c_offset, int32_t c_mult_int, int32_t out_shift, DataType data_type) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 67 | { |
| 68 | // Create tensors |
Gian Marco | fa4cacd | 2017-10-18 17:05:02 +0100 | [diff] [blame] | 69 | TensorType a = create_tensor<TensorType>(shape_a, data_type, 1); |
| 70 | TensorType b = create_tensor<TensorType>(shape_b, data_type, 1); |
| 71 | TensorType c = create_tensor<TensorType>(shape_c, data_type, 1); |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 72 | |
| 73 | // Create and configure function |
| 74 | FunctionType gemmlowp; |
| 75 | gemmlowp.configure(&a, &b, &c, a_offset, b_offset, c_offset, c_mult_int, out_shift); |
| 76 | |
| 77 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 78 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 79 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 80 | |
| 81 | // Allocate tensors |
| 82 | a.allocator()->allocate(); |
| 83 | b.allocator()->allocate(); |
| 84 | c.allocator()->allocate(); |
| 85 | |
| 86 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 87 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 88 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 89 | |
| 90 | // Fill tensors |
| 91 | fill(AccessorType(a), 0); |
| 92 | fill(AccessorType(b), 1); |
| 93 | fill(AccessorType(c), 2); |
| 94 | |
| 95 | // Compute GEMM function |
| 96 | gemmlowp.run(); |
| 97 | return c; |
| 98 | } |
| 99 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 100 | SimpleTensor<int8_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c, |
| 101 | int32_t a_offset, int32_t b_offset, int32_t c_offset, int32_t c_mult_int, int32_t out_shift, DataType data_type) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 102 | { |
| 103 | // Create reference |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 104 | SimpleTensor<int8_t> a{ shape_a, data_type, 1 }; |
| 105 | SimpleTensor<int8_t> b{ shape_b, data_type, 1 }; |
| 106 | SimpleTensor<int8_t> c{ shape_c, data_type, 1 }; |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 107 | |
| 108 | // Fill reference |
| 109 | fill(a, 0); |
| 110 | fill(b, 1); |
| 111 | fill(c, 2); |
| 112 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 113 | return reference::gemmlowp<int8_t>(a, b, c, a_offset, b_offset, c_offset, c_mult_int, out_shift); |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 116 | TensorType _target{}; |
| 117 | SimpleTensor<int8_t> _reference{}; |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 120 | template <typename TensorType, typename AccessorType, typename FunctionType> |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 121 | class GEMMLowpMatrixMultiplyValidationFixture : public framework::Fixture |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 122 | { |
| 123 | public: |
| 124 | template <typename...> |
| 125 | void setup(size_t m, size_t n, size_t k) |
| 126 | { |
| 127 | const TensorShape shape_a(k, m); |
| 128 | const TensorShape shape_b(n, k); |
| 129 | const TensorShape shape_c(n, m); |
| 130 | _target = compute_target(shape_a, shape_b, shape_c); |
| 131 | _reference = compute_reference(shape_a, shape_b, shape_c); |
| 132 | } |
| 133 | |
| 134 | protected: |
| 135 | template <typename U> |
| 136 | void fill(U &&tensor, int i, int lo, int hi) |
| 137 | { |
| 138 | std::uniform_int_distribution<> distribution(lo, hi); |
| 139 | library->fill(tensor, distribution, i); |
| 140 | } |
| 141 | |
| 142 | TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c) |
| 143 | { |
| 144 | // Create tensors |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 145 | TensorType a = create_tensor<TensorType>(shape_a, DataType::S8, 1); |
| 146 | TensorType b = create_tensor<TensorType>(shape_b, DataType::S8, 1); |
| 147 | TensorType c = create_tensor<TensorType>(shape_c, DataType::S32, 1); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 148 | |
| 149 | // Create and configure function |
| 150 | FunctionType gemmlowp; |
| 151 | gemmlowp.configure(&a, &b, &c); |
| 152 | |
| 153 | ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 154 | ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 155 | ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 156 | |
| 157 | // Allocate tensors |
| 158 | a.allocator()->allocate(); |
| 159 | b.allocator()->allocate(); |
| 160 | c.allocator()->allocate(); |
| 161 | |
| 162 | ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 163 | ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 164 | ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 165 | |
| 166 | // Fill tensors |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 167 | fill(AccessorType(a), 0, -128, 127); |
| 168 | fill(AccessorType(b), 1, -128, 127); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 169 | fill(AccessorType(c), 2, 0, 0); |
| 170 | |
| 171 | // Compute GEMM function |
| 172 | gemmlowp.run(); |
| 173 | return c; |
| 174 | } |
| 175 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 176 | SimpleTensor<int32_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 177 | { |
| 178 | // Create reference |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 179 | SimpleTensor<int8_t> a{ shape_a, DataType::S8, 1 }; |
| 180 | SimpleTensor<int8_t> b{ shape_b, DataType::S8, 1 }; |
| 181 | SimpleTensor<int32_t> c{ shape_c, DataType::S32, 1 }; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 182 | |
| 183 | // Fill reference |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 184 | fill(a, 0, -128, 127); |
| 185 | fill(b, 1, -128, 127); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 186 | fill(c, 2, 0, 0); |
| 187 | |
| 188 | return reference::gemmlowp(a, b, c); |
| 189 | } |
| 190 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame^] | 191 | TensorType _target{}; |
| 192 | SimpleTensor<int32_t> _reference{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 193 | }; |
| 194 | |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 195 | } // namespace validation |
| 196 | } // namespace test |
| 197 | } // namespace arm_compute |
| 198 | #endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */ |