Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 1 | /* |
Radu Salavat | 62d600f | 2024-04-15 14:42:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, 2024 Arm Limited. |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [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 | #include "DequantizationLayer.h" |
| 25 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 26 | #include "Permute.h" |
| 27 | |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 36 | namespace |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 37 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 38 | template <typename TOut> |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 39 | TOut dequantize(int8_t val, const UniformQuantizationInfo qinfo, DataType dt) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 40 | { |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 41 | if(dt == DataType::QSYMM8 || dt == DataType::QSYMM8_PER_CHANNEL) |
| 42 | { |
| 43 | return static_cast<TOut>(dequantize_qsymm8(val, qinfo)); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | return static_cast<TOut>(dequantize_qasymm8_signed(val, qinfo)); |
| 48 | } |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 49 | } |
| 50 | template <typename TOut> |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 51 | TOut dequantize(uint8_t val, const UniformQuantizationInfo qinfo, DataType dt) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 52 | { |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_UNUSED(dt); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 54 | return static_cast<TOut>(dequantize_qasymm8(val, qinfo)); |
| 55 | } |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 56 | template <typename TOut> |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 57 | TOut dequantize(int16_t val, const UniformQuantizationInfo qinfo, DataType dt) |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 58 | { |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_UNUSED(dt); |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 60 | return static_cast<TOut>(dequantize_qsymm16(val, qinfo)); |
| 61 | } |
Radu Salavat | 62d600f | 2024-04-15 14:42:07 +0000 | [diff] [blame] | 62 | template <typename TOut> |
| 63 | TOut dequantize(int32_t val, const UniformQuantizationInfo qinfo, DataType dt) |
| 64 | { |
| 65 | ARM_COMPUTE_UNUSED(dt); |
| 66 | return static_cast<TOut>(dequantize_s32(val, qinfo)); |
| 67 | } |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 68 | } // namespace |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 69 | template <typename TOut, typename TIn> |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 70 | SimpleTensor<TOut> dequantization_layer(const SimpleTensor<TIn> &src) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 71 | { |
| 72 | const DataType src_data_type = src.data_type(); |
| 73 | const DataType dst_data_type = std::is_same<TOut, float>::value ? DataType::F32 : DataType::F16; |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 74 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 75 | SimpleTensor<TOut> dst{ src.shape(), dst_data_type }; |
| 76 | |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 77 | if(is_data_type_quantized_per_channel(src_data_type)) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 78 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 79 | const int WH = src.shape().x() * src.shape().y(); |
| 80 | const int C = src.shape().z(); |
| 81 | const int N = src.shape().total_size() / (WH * C); |
| 82 | |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 83 | const std::vector<float> qscales = src.quantization_info().scale(); |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 84 | #if defined(_OPENMP) |
| 85 | #pragma omp parallel for collapse(2) |
| 86 | #endif /* _OPENMP */ |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 87 | for(int n = 0; n < N; ++n) |
| 88 | { |
| 89 | for(int c = 0; c < C; ++c) |
| 90 | { |
| 91 | const size_t idx = n * C * WH + c * WH; |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 92 | const UniformQuantizationInfo channel_qinfo = { qscales[c], 0 }; |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 93 | |
| 94 | // Dequantize slice |
| 95 | for(int s = 0; s < WH; ++s) |
| 96 | { |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 97 | dst[idx + s] = dequantize<TOut>(static_cast<TIn>(src[idx + s]), channel_qinfo, src_data_type); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform(); |
| 105 | ARM_COMPUTE_ERROR_ON(quantization_info.offset != 0 && src_data_type == DataType::QSYMM8); |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 106 | #if defined(_OPENMP) |
| 107 | #pragma omp parallel for |
| 108 | #endif /* _OPENMP */ |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 109 | for(int i = 0; i < src.num_elements(); ++i) |
| 110 | { |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 111 | dst[i] = static_cast<TOut>(dequantize<TOut>(static_cast<TIn>(src[i]), quantization_info, src_data_type)); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 112 | } |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return dst; |
| 116 | } |
| 117 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 118 | template SimpleTensor<half> dequantization_layer(const SimpleTensor<uint8_t> &src); |
| 119 | template SimpleTensor<float> dequantization_layer(const SimpleTensor<uint8_t> &src); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 120 | template SimpleTensor<half> dequantization_layer(const SimpleTensor<int8_t> &src); |
| 121 | template SimpleTensor<float> dequantization_layer(const SimpleTensor<int8_t> &src); |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 122 | template SimpleTensor<half> dequantization_layer(const SimpleTensor<int16_t> &src); |
| 123 | template SimpleTensor<float> dequantization_layer(const SimpleTensor<int16_t> &src); |
Radu Salavat | 62d600f | 2024-04-15 14:42:07 +0000 | [diff] [blame] | 124 | template SimpleTensor<float> dequantization_layer(const SimpleTensor<int32_t> &src); |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 125 | } // namespace reference |
| 126 | } // namespace validation |
| 127 | } // namespace test |
| 128 | } // namespace arm_compute |