blob: 7c6114640cd78995832ff2380bbecaa0ab1c46b4 [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"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Giorgio Arena657bdb32018-04-26 18:52:01 +010029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/Types.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000032#include "support/StringSupport.h"
Giorgio Arena657bdb32018-04-26 18:52:01 +010033
34namespace arm_compute
35{
36CLConvertFullyConnectedWeightsKernel::CLConvertFullyConnectedWeightsKernel()
37 : _input(nullptr), _output(nullptr)
38{
39}
40
41void CLConvertFullyConnectedWeightsKernel::configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape,
42 DataLayout data_layout)
43{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010044 configure(CLKernelLibrary::get().get_compile_context(), input, output, original_input_shape, data_layout);
45}
46
Manuel Bottini256c0b92020-04-21 13:29:30 +010047void CLConvertFullyConnectedWeightsKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape,
Manuel Bottini4c6bd512020-04-08 10:15:51 +010048 DataLayout data_layout)
49{
Giorgio Arena657bdb32018-04-26 18:52:01 +010050 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010051
52 // Output tensor auto initialisation if not yet initialized
53 auto_init_if_empty(*output->info(), *input->info()->clone());
54
Giorgio Arena657bdb32018-04-26 18:52:01 +010055 ARM_COMPUTE_ERROR_THROW_ON(CLConvertFullyConnectedWeightsKernel::validate(input->info(), output->info(), original_input_shape, data_layout));
56
57 _input = input;
58 _output = output;
59
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010060 const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
61
62 const int width_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
63 const int height_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
64 const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
65
66 const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
67 const unsigned int num_channels = original_input_shape[channel_idx];
68
69 const unsigned int factor_1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
70 const unsigned int factor_2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
Giorgio Arena657bdb32018-04-26 18:52:01 +010071
72 // Set build options
73 CLBuildOptions build_opts;
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000074 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010075 build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(factor_1));
76 build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(factor_2));
Giorgio Arena657bdb32018-04-26 18:52:01 +010077
78 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +010079 _kernel = create_kernel(compile_context, "convert_fc_weights", build_opts.options());
Giorgio Arena657bdb32018-04-26 18:52:01 +010080
81 // Configure kernel window
82 Window win = calculate_max_window(*input->info(), Steps());
Anthony Barbierb6eb3532018-08-08 13:20:04 +010083 ICLKernel::configure_internal(win);
Giorgio Arena657bdb32018-04-26 18:52:01 +010084}
85
86Status CLConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape,
87 DataLayout data_layout)
88{
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000089 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010090 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000091 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giorgio Arena657bdb32018-04-26 18:52:01 +010092 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() != 2);
93 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != original_input_shape.total_size_lower(3));
94 ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
95
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010096 // Checks performed when output is configured
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000097 if(output->total_size() != 0)
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010098 {
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
101 }
102
Giorgio Arena657bdb32018-04-26 18:52:01 +0100103 return Status{};
104}
105
106void CLConvertFullyConnectedWeightsKernel::run(const Window &window, cl::CommandQueue &queue)
107{
108 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
109 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
110
111 unsigned int idx = 0;
112 add_2D_tensor_argument(idx, _input, window);
113 add_2D_tensor_argument(idx, _output, window);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100114 enqueue(queue, *this, window, lws_hint());
Giorgio Arena657bdb32018-04-26 18:52:01 +0100115}
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100116} // namespace arm_compute