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 | */ |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 24 | #include "GEMMLowp.h" |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Types.h" |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 36 | namespace |
| 37 | { |
| 38 | template <typename T> |
| 39 | void quantize_down_int32_to_uint8_scale(const SimpleTensor<T> *in, const SimpleTensor<T> *bias, SimpleTensor<uint8_t> *dst, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, |
| 40 | int32_t min, int32_t max) |
| 41 | { |
| 42 | const int cols_in = in->shape().x(); |
| 43 | |
| 44 | for(int i = 0; i < in->num_elements(); ++i) |
| 45 | { |
| 46 | int32_t result = ((*in)[i] + result_offset) * result_mult_int; |
| 47 | |
| 48 | if(bias != nullptr) |
| 49 | { |
| 50 | result += (*bias)[i % cols_in]; |
| 51 | } |
| 52 | |
| 53 | result >>= result_shift; |
| 54 | |
| 55 | // Bounded ReLu |
| 56 | if(min != max) |
| 57 | { |
| 58 | result = std::max(min, std::min(max, result)); |
| 59 | } |
| 60 | |
| 61 | (*dst)[i] = static_cast<uint8_t>(std::max(0, std::min(255, result))); |
| 62 | } |
| 63 | } |
| 64 | } // namespace |
| 65 | |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 66 | template <typename T> |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 67 | SimpleTensor<int32_t> gemmlowp_matrix_multiply_core(const SimpleTensor<T> &a, const SimpleTensor<T> &b, int32_t a_offset, int32_t b_offset) |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 68 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 69 | TensorShape shape(b.shape()[0], a.shape()[1]); |
| 70 | |
| 71 | SimpleTensor<int32_t> c(shape, DataType::S32); |
| 72 | |
| 73 | const int K = a.shape().x(); |
| 74 | const int b_width = b.shape().x(); |
| 75 | const int rows = c.shape().y(); //M |
| 76 | const int cols = c.shape().x(); //N |
| 77 | |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 78 | std::vector<int32_t> acc; |
| 79 | acc.resize(cols); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 80 | |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 81 | for(int i = 0; i < rows; ++i) |
| 82 | { |
| 83 | for(int j = 0; j < cols; ++j) |
| 84 | { |
| 85 | acc[j] = 0; |
| 86 | } |
| 87 | for(int k = 0; k < K; ++k) |
| 88 | { |
| 89 | const int32_t tmp_a = a_offset + static_cast<int32_t>(a[k + i * K]); |
| 90 | for(int j = 0; j < b_width; ++j) |
| 91 | { |
| 92 | const int32_t tmp_b = b_offset + static_cast<int32_t>(b[j + k * b_width]); |
| 93 | const int32_t mult_as_int = tmp_a * tmp_b; |
| 94 | acc[j] += mult_as_int; |
| 95 | } |
| 96 | } |
| 97 | for(int j = 0; j < cols; ++j) |
| 98 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 99 | c[j + i * cols] = acc[j]; |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | return c; |
| 104 | } |
| 105 | |
Pablo Tello | 181e651 | 2017-11-15 13:28:27 +0000 | [diff] [blame] | 106 | // used to validate assembly kernels which don't know anything about offsets |
| 107 | SimpleTensor<int32_t> gemmlowp(const SimpleTensor<int8_t> &a, const SimpleTensor<int8_t> &b) |
| 108 | { |
| 109 | return gemmlowp_matrix_multiply_core(a, b, 0, 0); |
| 110 | } |
| 111 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 112 | template <typename T> |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 113 | SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<T> &in, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, int32_t min, int32_t max) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 114 | { |
| 115 | SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8); |
| 116 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 117 | quantize_down_int32_to_uint8_scale<T>(&in, nullptr, &dst, result_offset, result_mult_int, result_shift, min, max); |
| 118 | |
| 119 | return dst; |
| 120 | } |
| 121 | |
| 122 | template <typename T> |
| 123 | SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<T> &in, const SimpleTensor<T> &bias, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, |
| 124 | int32_t min, int32_t max) |
| 125 | { |
| 126 | SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8); |
| 127 | |
| 128 | quantize_down_int32_to_uint8_scale<T>(&in, &bias, &dst, result_offset, result_mult_int, result_shift, min, max); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 129 | |
| 130 | return dst; |
| 131 | } |
| 132 | |
| 133 | template SimpleTensor<int32_t> gemmlowp_matrix_multiply_core(const SimpleTensor<uint8_t> &a, const SimpleTensor<uint8_t> &b, int32_t a_offset, int32_t b_offset); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 134 | template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<int32_t> &a, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, int32_t min, |
| 135 | int32_t max); |
| 136 | template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b, int32_t result_offset, int32_t result_mult_int, |
| 137 | int32_t result_shift, int32_t min, int32_t max); |
Pablo Tello | 299025a | 2017-09-29 11:30:12 +0100 | [diff] [blame] | 138 | } // namespace reference |
| 139 | } // namespace validation |
| 140 | } // namespace test |
| 141 | } // namespace arm_compute |