blob: e560f1de4a6409b984864ae19714d346f604ab0f [file] [log] [blame]
Gian Marco Iodice4d81d752020-07-14 15:05:31 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2020-2021, 2023 Arm Limited.
Gian Marco Iodice4d81d752020-07-14 15:05:31 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLMaxUnpoolingLayerKernel.h"
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010025
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000033#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010038#include "support/StringSupport.h"
39
40namespace arm_compute
41{
42using namespace misc::shape_calculator;
43
44namespace
45{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046Status validate_arguments(const ITensorInfo *input,
47 const ITensorInfo *output,
48 const PoolingLayerInfo &pool_info,
49 const ITensorInfo *indices)
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010050{
51 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, indices);
52 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
54 DataType::F16, DataType::F32);
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010055 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, indices);
57
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 int pool_stride_x = 0;
59 int pool_stride_y = 0;
60 PoolingType pool_type = pool_info.pool_type;
61 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010062 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063 const int pool_size_x = pool_info.pool_size.width;
64 const int pool_size_y = pool_info.pool_size.height;
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010065 const Size2D pool_size(pool_size_x, pool_size_y);
66
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX,
68 "Pooling indices only supported for MAX pooling method");
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070 if (output->total_size() != 0)
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010071 {
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
74 }
75
76 return Status{};
77}
78} // namespace
79
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080CLMaxUnpoolingLayerKernel::CLMaxUnpoolingLayerKernel() : _input(nullptr), _output(nullptr), _indices(nullptr)
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010081{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010082 _type = CLKernelType::POOL;
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010083}
84
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085void CLMaxUnpoolingLayerKernel::configure(const CLCompileContext &compile_context,
86 const ICLTensor *input,
87 const ICLTensor *indices,
88 ICLTensor *output,
89 const PoolingLayerInfo &pool_info)
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010090{
91 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
92 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, indices->info()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093 auto padding_info = get_padding_info({input, indices, output});
Gian Marco Iodice4d81d752020-07-14 15:05:31 +010094
95 _input = input;
96 _output = output;
97 _indices = indices;
98
99 // Create build options
100 CLBuildOptions build_opts;
101 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
102 build_opts.add_option("-DWIDTH_DST=" + support::cpp11::to_string(output->info()->dimension(0)));
103 build_opts.add_option("-DHEIGHT_DST=" + support::cpp11::to_string(output->info()->dimension(1)));
104 build_opts.add_option("-DDEPTH_DST=" + support::cpp11::to_string(output->info()->dimension(2)));
105
106 const std::string kernel_name("max_unpooling_layer_2");
107
108 // Create kernel
109 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
110
111 const TensorShape output_shape = compute_unpool_shape(*input->info(), pool_info);
112 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
113
114 auto window = calculate_max_window(*input->info(), Steps());
115 ICLKernel::configure_internal(window);
116
117 // Set config_id for enabling LWS tuning
118 _config_id = kernel_name;
119 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
120 _config_id += "_";
121 _config_id += support::cpp11::to_string(output->info()->dimension(0));
122 _config_id += "_";
123 _config_id += support::cpp11::to_string(output->info()->dimension(1));
124 _config_id += "_";
125 _config_id += support::cpp11::to_string(output->info()->dimension(2));
126 _config_id += "_";
127 _config_id += support::cpp11::to_string(output->info()->dimension(3));
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000128 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Gian Marco Iodice4d81d752020-07-14 15:05:31 +0100129}
130
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100131Status CLMaxUnpoolingLayerKernel::validate(const ITensorInfo *input,
132 const ITensorInfo *indices,
133 const ITensorInfo *output,
134 const PoolingLayerInfo &pool_info)
Gian Marco Iodice4d81d752020-07-14 15:05:31 +0100135{
136 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
137 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, indices));
138 return Status{};
139}
140
141void CLMaxUnpoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
142{
143 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
144 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
145
146 Window slice = window.first_slice_window_3D();
147
148 do
149 {
150 unsigned int idx = 0;
151 add_3D_tensor_argument(idx, _input, slice);
152 add_3D_tensor_argument(idx, _output, slice);
153 add_3D_tensor_argument(idx, _indices, slice);
154 enqueue(queue, *this, slice, lws_hint());
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 } while (window.slide_window_slice_3D(slice));
Gian Marco Iodice4d81d752020-07-14 15:05:31 +0100156}
157} // namespace arm_compute