blob: d4c1bec5f4f978b60d9867469a4a5dd570ebde52 [file] [log] [blame]
Michele Di Giorgio56dd7262017-07-27 09:53:49 +01001/*
Michalis Spyrou995f5522018-01-29 13:43:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Michele Di Giorgio56dd7262017-07-27 09:53:49 +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/CL/kernels/CLDequantizationLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35using namespace arm_compute;
36
Alex Gilday60954c62018-03-05 16:22:48 +000037namespace
38{
39Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max)
40{
41 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, min_max);
42 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
43 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3);
44
45 if(output->tensor_shape().total_size() > 0)
46 {
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
49 }
50
51 return Status{};
52}
53
54std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *min_max)
55{
56 // Output tensor auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010057 auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::F32);
Alex Gilday60954c62018-03-05 16:22:48 +000058
59 constexpr unsigned int num_elems_processed_per_iteration = 4;
60
61 // Configure window
62 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
63 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
64 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
65 AccessWindowStatic min_max_access(min_max, 0, 0, 2, min_max->dimension(1));
66
67 // Update window and padding
68 bool window_changed = update_window_and_padding(win, input_access, output_access, min_max_access);
69
70 output_access.set_valid_region(win, input->valid_region());
71
72 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
73 return std::make_tuple(err, win);
74}
75} // namespace
76
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010077CLDequantizationLayerKernel::CLDequantizationLayerKernel()
78 : _input(nullptr), _output(nullptr), _min_max(nullptr)
79{
80}
81
82void CLDequantizationLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *min_max)
83{
Alex Gilday60954c62018-03-05 16:22:48 +000084 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, min_max);
85 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), min_max->info()));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010086
87 _input = input;
88 _output = output;
89 _min_max = min_max;
90
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010091 // Create kernel
92 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("dequantization_layer"));
93
Alex Gilday60954c62018-03-05 16:22:48 +000094 // Configure kernel window
95 auto win_config = validate_and_configure_window(input->info(), output->info(), min_max->info());
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010096
Alex Gilday60954c62018-03-05 16:22:48 +000097 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010098
Anthony Barbierb6eb3532018-08-08 13:20:04 +010099 ICLKernel::configure_internal(std::get<1>(win_config));
Alex Gilday60954c62018-03-05 16:22:48 +0000100}
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100101
Alex Gilday60954c62018-03-05 16:22:48 +0000102Status CLDequantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max)
103{
104 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, min_max));
105 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), min_max->clone().get())));
106
107 return Status{};
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100108}
109
110void CLDequantizationLayerKernel::run(const Window &window, cl::CommandQueue &queue)
111{
112 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
113 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
114
115 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3);
116 Window slice = window_collapsed.first_slice_window_3D();
117
118 Window min_max_window = window;
119 min_max_window.set(Window::DimX, Window::Dimension(0, 0, 0));
120 min_max_window.set(Window::DimY, Window::Dimension(0, _min_max->info()->dimension(1), 1));
121 min_max_window.set(Window::DimZ, Window::Dimension(0, 0, 0));
122
123 Window min_max_slice = min_max_window.first_slice_window_1D();
124
125 do
126 {
127 unsigned int idx = 0;
128 add_3D_tensor_argument(idx, _input, slice);
129 add_3D_tensor_argument(idx, _output, slice);
130 add_1D_tensor_argument(idx, _min_max, min_max_slice);
131 enqueue(queue, *this, slice);
132 }
Michalis Spyrou995f5522018-01-29 13:43:35 +0000133 while(window_collapsed.slide_window_slice_3D(slice) && min_max_window.slide_window_slice_1D(min_max_slice));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +0100134}