blob: b4e42bf3bcf3d9e32bf99398a8e72cf257ac5356 [file] [log] [blame]
Giorgio Arena657bdb32018-04-26 18:52:01 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Giorgio Arena657bdb32018-04-26 18:52:01 +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/CLConvertFullyConnectedWeightsKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/Helpers.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/CL/CLValidate.h"
31#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000033#include "support/StringSupport.h"
Giorgio Arena657bdb32018-04-26 18:52:01 +010034
35namespace arm_compute
36{
37CLConvertFullyConnectedWeightsKernel::CLConvertFullyConnectedWeightsKernel()
38 : _input(nullptr), _output(nullptr)
39{
40}
41
42void CLConvertFullyConnectedWeightsKernel::configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape,
43 DataLayout data_layout)
44{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010045 configure(CLKernelLibrary::get().get_compile_context(), input, output, original_input_shape, data_layout);
46}
47
Manuel Bottini256c0b92020-04-21 13:29:30 +010048void CLConvertFullyConnectedWeightsKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010049 DataLayout data_layout)
50{
Giorgio Arena657bdb32018-04-26 18:52:01 +010051 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010052
53 // Output tensor auto initialisation if not yet initialized
54 auto_init_if_empty(*output->info(), *input->info()->clone());
55
Giorgio Arena657bdb32018-04-26 18:52:01 +010056 ARM_COMPUTE_ERROR_THROW_ON(CLConvertFullyConnectedWeightsKernel::validate(input->info(), output->info(), original_input_shape, data_layout));
57
58 _input = input;
59 _output = output;
60
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010061 const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
62
63 const int width_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
64 const int height_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
65 const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
66
67 const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
68 const unsigned int num_channels = original_input_shape[channel_idx];
69
70 const unsigned int factor_1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
71 const unsigned int factor_2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
Giorgio Arena657bdb32018-04-26 18:52:01 +010072
73 // Set build options
74 CLBuildOptions build_opts;
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000075 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010076 build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(factor_1));
77 build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(factor_2));
Giorgio Arena657bdb32018-04-26 18:52:01 +010078
79 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +010080 _kernel = create_kernel(compile_context, "convert_fc_weights", build_opts.options());
Giorgio Arena657bdb32018-04-26 18:52:01 +010081
82 // Configure kernel window
83 Window win = calculate_max_window(*input->info(), Steps());
Anthony Barbierb6eb3532018-08-08 13:20:04 +010084 ICLKernel::configure_internal(win);
Giorgio Arena657bdb32018-04-26 18:52:01 +010085}
86
87Status CLConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape,
88 DataLayout data_layout)
89{
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000090 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010091 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000092 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giorgio Arena657bdb32018-04-26 18:52:01 +010093 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() != 2);
94 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != original_input_shape.total_size_lower(3));
95 ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
96
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010097 // Checks performed when output is configured
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000098 if(output->total_size() != 0)
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010099 {
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
101 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
102 }
103
Giorgio Arena657bdb32018-04-26 18:52:01 +0100104 return Status{};
105}
106
107void CLConvertFullyConnectedWeightsKernel::run(const Window &window, cl::CommandQueue &queue)
108{
109 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
110 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
111
112 unsigned int idx = 0;
113 add_2D_tensor_argument(idx, _input, window);
114 add_2D_tensor_argument(idx, _output, window);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100115 enqueue(queue, *this, window, lws_hint());
Giorgio Arena657bdb32018-04-26 18:52:01 +0100116}
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100117} // namespace arm_compute