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