blob: fa6a48e77c89957e11e3a558ef9031af5fe59279 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
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 */
24#include "arm_compute/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ToUint8ScaleKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
33#include "support/ToolchainSupport.h"
34
35using namespace arm_compute;
36
37namespace arm_compute
38{
39class Coordinates;
40} // namespace arm_compute
41
42CLGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::CLGEMMLowpQuantizeDownInt32ToUint8ScaleKernel()
43 : _input(nullptr), _bias(nullptr), _output(nullptr)
44{
45}
46
47void CLGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, int result_offset, int result_mult_int, int result_shift, int min,
48 int max)
49{
50 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
52 ARM_COMPUTE_ERROR_ON(max > 255);
53 ARM_COMPUTE_ERROR_ON(min < 0 || min > max);
54
55 if(bias != nullptr)
56 {
57 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
58 ARM_COMPUTE_ERROR_ON(bias->info()->num_dimensions() > 1);
59 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != bias->info()->dimension(0));
60 }
61
62 _input = input;
63 _bias = bias;
64 _output = output;
65
66 // Set the arguments to pass at compile time
67 CLBuildOptions build_opts;
68 build_opts.add_option("-DRESULT_OFFSET=" + support::cpp11::to_string(result_offset));
69 build_opts.add_option("-DRESULT_MULT_INT=" + support::cpp11::to_string(result_mult_int));
70 build_opts.add_option("-DRESULT_SHIFT=" + support::cpp11::to_string(result_shift));
71 build_opts.add_option_if((min != 0) && (min != max), "-DMIN_BOUND=" + support::cpp11::to_string(min));
72 build_opts.add_option_if((max != 255) && (min != max), "-DMAX_BOUND=" + support::cpp11::to_string(max));
73 build_opts.add_option_if(bias != nullptr, "-DADD_BIAS");
74
75 // Create kernel
76 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_output_stage_quantize_down", build_opts.options()));
77
78 constexpr unsigned int num_elems_processed_per_iteration = 16;
79
80 // Configure kernel window
81 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
82
83 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
84 AccessWindowHorizontal output_result_access(output->info(), 0, num_elems_processed_per_iteration);
85
86 update_window_and_padding(win,
87 input_access,
88 output_result_access);
89
90 if(bias != nullptr)
91 {
92 AccessWindowStatic bias_access(bias->info(), 0, 0, ceil_to_multiple(bias->info()->dimension(0), num_elems_processed_per_iteration), bias->info()->tensor_shape()[1]);
93
94 update_window_and_padding(win,
95 bias_access);
96 }
97
98 output_result_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
99
100 ICLKernel::configure(win);
101}
102
103void CLGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window, cl::CommandQueue &queue)
104{
105 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
106 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
107
108 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
109 Window slice = collapsed.first_slice_window_3D();
110
111 unsigned int idx1 = num_arguments_per_3D_tensor();
112 if(_bias != nullptr)
113 {
114 Window biases_slice(slice);
115 biases_slice.set(Window::DimY, Window::Dimension(0, 1, 1));
116 biases_slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
117 add_1D_tensor_argument(idx1, _bias, biases_slice);
118 }
119
120 do
121 {
122 unsigned int idx = 0;
123 add_3D_tensor_argument(idx, _input, slice);
124 add_3D_tensor_argument(idx1, _output, slice);
125 enqueue(queue, *this, slice);
126 }
127 while(collapsed.slide_window_slice_3D(slice));
128}