blob: 119aa4ad9a60127816b009255a5cd4dcf2dd8742 [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
Georgios Pinitas574775c2019-02-18 20:08:02 +00002 * Copyright (c) 2017-2019 ARM Limited.
Michele Di Giorgio32982d82017-07-07 14:44:43 +01003 *
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 "arm_compute/core/NEON/kernels/NEDequantizationLayerKernel.h"
25
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010026#include "arm_compute/core/AccessWindowStatic.h"
Georgios Pinitas574775c2019-02-18 20:08:02 +000027#include "arm_compute/core/CPP/Validate.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010028#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
Georgios Pinitas574775c2019-02-18 20:08:02 +000030#include "arm_compute/core/NEON/NEAsymm.h"
31#include "arm_compute/core/NEON/wrapper/wrapper.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010032#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include <arm_neon.h>
37
Georgios Pinitas574775c2019-02-18 20:08:02 +000038namespace arm_compute
39{
Alex Gilday60954c62018-03-05 16:22:48 +000040namespace
41{
Georgios Pinitas574775c2019-02-18 20:08:02 +000042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Alex Gilday60954c62018-03-05 16:22:48 +000043{
Georgios Pinitas574775c2019-02-18 20:08:02 +000044 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8);
Alex Gilday60954c62018-03-05 16:22:48 +000046
47 if(output->tensor_shape().total_size() > 0)
48 {
Georgios Pinitas574775c2019-02-18 20:08:02 +000049 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(output);
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32);
Alex Gilday60954c62018-03-05 16:22:48 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
52 }
53
54 return Status{};
55}
56
Georgios Pinitas574775c2019-02-18 20:08:02 +000057std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Alex Gilday60954c62018-03-05 16:22:48 +000058{
Georgios Pinitas574775c2019-02-18 20:08:02 +000059 // Configure kernel window
60 Window win = calculate_max_window(*input, Steps());
61
Alex Gilday60954c62018-03-05 16:22:48 +000062 // Output tensor auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010063 auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::F32);
Alex Gilday60954c62018-03-05 16:22:48 +000064
Georgios Pinitas574775c2019-02-18 20:08:02 +000065 // NEDequantizationLayerKernel doesn't need padding so update_window_and_padding() can be skipped
66 Coordinates coord;
67 coord.set_num_dimensions(output->num_dimensions());
68 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Alex Gilday60954c62018-03-05 16:22:48 +000069
Georgios Pinitas574775c2019-02-18 20:08:02 +000070 return std::make_tuple(Status{}, win);
71}
Alex Gilday60954c62018-03-05 16:22:48 +000072
Georgios Pinitas574775c2019-02-18 20:08:02 +000073template <typename T>
74inline void store_result(T *ptr, const float32x4x4_t &v)
75{
76 ARM_COMPUTE_UNUSED(ptr, v);
77}
Alex Gilday60954c62018-03-05 16:22:48 +000078
Georgios Pinitas574775c2019-02-18 20:08:02 +000079template <>
80inline void store_result<float>(float *ptr, const float32x4x4_t &v)
81{
82 wrapper::vstore(ptr, v.val[0]);
83 wrapper::vstore(ptr + 4, v.val[1]);
84 wrapper::vstore(ptr + 8, v.val[2]);
85 wrapper::vstore(ptr + 12, v.val[3]);
86}
Alex Gilday60954c62018-03-05 16:22:48 +000087
Georgios Pinitas574775c2019-02-18 20:08:02 +000088#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
89template <>
90inline void store_result<float16_t>(float16_t *ptr, const float32x4x4_t &v)
91{
92 wrapper::vstore(ptr, vcombine_f16(vcvt_f16_f32(v.val[0]), vcvt_f16_f32(v.val[1])));
93 wrapper::vstore(ptr + 8, vcombine_f16(vcvt_f16_f32(v.val[2]), vcvt_f16_f32(v.val[3])));
94}
95#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
96
97template <typename T>
98void run_dequantization(const ITensor *input, ITensor *output, const Window &window)
99{
100 const QuantizationInfo &qinfo = input->info()->quantization_info();
101
102 const int window_step_x = 16;
103 const auto window_start_x = static_cast<int>(window.x().start());
104 const auto window_end_x = static_cast<int>(window.x().end());
105
106 // Collapse window and reset first dimension to handle tail calculations manually
107 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
108 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
109
110 // Create iterators
111 Iterator in(input, win_collapsed);
112 Iterator out(output, win_collapsed);
113
114 execute_window_loop(win_collapsed, [&](const Coordinates & id)
115 {
116 const auto in_ptr = reinterpret_cast<const uint8_t *>(in.ptr());
117 const auto out_ptr = reinterpret_cast<T *>(out.ptr());
118
119 int x = window_start_x;
120 for(; x <= (window_end_x - window_step_x); x += window_step_x)
121 {
122 const auto vin = wrapper::vloadq(in_ptr + x);
123 const auto vdeq = vdequantize(vin, qinfo);
124
125 store_result<T>(reinterpret_cast<T *>(out_ptr + x), vdeq);
126 }
127
128 // Compute left-over elements
129 for(; x < window_end_x; ++x)
130 {
131 uint8_t val = *(in_ptr + x);
132 *(out_ptr + x) = static_cast<T>(qinfo.dequantize(val));
133 }
134 },
135 in, out);
Alex Gilday60954c62018-03-05 16:22:48 +0000136}
137} // namespace
138
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100139NEDequantizationLayerKernel::NEDequantizationLayerKernel()
Georgios Pinitas574775c2019-02-18 20:08:02 +0000140 : _input(nullptr), _output(nullptr)
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100141{
142}
143
Georgios Pinitas574775c2019-02-18 20:08:02 +0000144void NEDequantizationLayerKernel::configure(const ITensor *input, ITensor *output)
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100145{
Georgios Pinitas574775c2019-02-18 20:08:02 +0000146 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
147 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100148
Georgios Pinitas574775c2019-02-18 20:08:02 +0000149 _input = input;
150 _output = output;
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100151
Alex Gilday60954c62018-03-05 16:22:48 +0000152 // Configure kernel window
Georgios Pinitas574775c2019-02-18 20:08:02 +0000153 auto win_config = validate_and_configure_window(input->info(), output->info());
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100154
Alex Gilday60954c62018-03-05 16:22:48 +0000155 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100156
Alex Gilday60954c62018-03-05 16:22:48 +0000157 INEKernel::configure(std::get<1>(win_config));
158}
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100159
Georgios Pinitas574775c2019-02-18 20:08:02 +0000160Status NEDequantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Alex Gilday60954c62018-03-05 16:22:48 +0000161{
Georgios Pinitas574775c2019-02-18 20:08:02 +0000162 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
163 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get())));
Alex Gilday60954c62018-03-05 16:22:48 +0000164 return Status{};
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100165}
166
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100167void NEDequantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100168{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100169 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100170 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
171 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
172
Georgios Pinitas574775c2019-02-18 20:08:02 +0000173 switch(_output->info()->data_type())
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100174 {
Georgios Pinitas574775c2019-02-18 20:08:02 +0000175 case DataType::F32:
176 run_dequantization<float>(_input, _output, window);
177 break;
178#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
179 case DataType::F16:
180 run_dequantization<float16_t>(_input, _output, window);
181 break;
182#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
183 default:
184 ARM_COMPUTE_ERROR("Unsupported data type.");
185 }
186}
187} // namespace arm_compute