Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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> |
| 39 | TOut dequantize(int8_t val, const UniformQuantizationInfo qinfo) |
| 40 | { |
| 41 | return static_cast<TOut>(dequantize_qsymm8(val, qinfo)); |
| 42 | } |
| 43 | template <typename TOut> |
| 44 | TOut dequantize(uint8_t val, const UniformQuantizationInfo qinfo) |
| 45 | { |
| 46 | return static_cast<TOut>(dequantize_qasymm8(val, qinfo)); |
| 47 | } |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 48 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 49 | template <typename TOut, typename TIn> |
| 50 | SimpleTensor<TOut> dequantization_layer_nchw(const SimpleTensor<TIn> &src) |
| 51 | { |
| 52 | const DataType src_data_type = src.data_type(); |
| 53 | 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] | 54 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 55 | SimpleTensor<TOut> dst{ src.shape(), dst_data_type }; |
| 56 | |
| 57 | if(src_data_type == DataType::QSYMM8_PER_CHANNEL) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 58 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 59 | const int WH = src.shape().x() * src.shape().y(); |
| 60 | const int C = src.shape().z(); |
| 61 | const int N = src.shape().total_size() / (WH * C); |
| 62 | |
| 63 | const std::vector<float> qscales = src.quantization_info().scale(); |
| 64 | |
| 65 | for(int n = 0; n < N; ++n) |
| 66 | { |
| 67 | for(int c = 0; c < C; ++c) |
| 68 | { |
| 69 | const size_t idx = n * C * WH + c * WH; |
| 70 | const UniformQuantizationInfo channel_qinfo = { qscales[c], 0 }; |
| 71 | |
| 72 | // Dequantize slice |
| 73 | for(int s = 0; s < WH; ++s) |
| 74 | { |
| 75 | dst[idx + s] = dequantize<TOut>(src[idx + s], channel_qinfo); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform(); |
| 83 | ARM_COMPUTE_ERROR_ON(quantization_info.offset != 0 && src_data_type == DataType::QSYMM8); |
| 84 | |
| 85 | for(int i = 0; i < src.num_elements(); ++i) |
| 86 | { |
| 87 | dst[i] = static_cast<TOut>(dequantize<TOut>(src[i], quantization_info)); |
| 88 | } |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return dst; |
| 92 | } |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 93 | } // namespace |
| 94 | template <typename TOut, typename TIn> |
| 95 | SimpleTensor<TOut> dequantization_layer(const SimpleTensor<TIn> &src) |
| 96 | { |
| 97 | if(src.data_layout() == DataLayout::NHWC && src.data_type() == DataType::QSYMM8_PER_CHANNEL) |
| 98 | { |
| 99 | SimpleTensor<TIn> src_nchw = reference::permute<TIn>(src, PermutationVector(1U, 2U, 0U)); |
| 100 | return reference::permute<TOut>(dequantization_layer_nchw<TOut>(src_nchw), PermutationVector(2U, 0U, 1U)); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | return dequantization_layer_nchw<TOut>(src); |
| 105 | } |
| 106 | } |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 107 | |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 108 | template SimpleTensor<half> dequantization_layer(const SimpleTensor<uint8_t> &src); |
| 109 | template SimpleTensor<float> dequantization_layer(const SimpleTensor<uint8_t> &src); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 110 | template SimpleTensor<half> dequantization_layer(const SimpleTensor<int8_t> &src); |
| 111 | template SimpleTensor<float> dequantization_layer(const SimpleTensor<int8_t> &src); |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 112 | } // namespace reference |
| 113 | } // namespace validation |
| 114 | } // namespace test |
| 115 | } // namespace arm_compute |