blob: ace3fd5840e25abf17634b4b577e5f9a69b50841 [file] [log] [blame]
Giorgio Arena657bdb32018-04-26 18:52:01 +01001/*
2 * Copyright (c) 2018 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/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"
32
33namespace arm_compute
34{
35CLConvertFullyConnectedWeightsKernel::CLConvertFullyConnectedWeightsKernel()
36 : _input(nullptr), _output(nullptr)
37{
38}
39
40void CLConvertFullyConnectedWeightsKernel::configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape,
41 DataLayout data_layout)
42{
43 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010044
45 // Output tensor auto initialisation if not yet initialized
46 auto_init_if_empty(*output->info(), *input->info()->clone());
47
Giorgio Arena657bdb32018-04-26 18:52:01 +010048 ARM_COMPUTE_ERROR_THROW_ON(CLConvertFullyConnectedWeightsKernel::validate(input->info(), output->info(), original_input_shape, data_layout));
49
50 _input = input;
51 _output = output;
52
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010053 const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
54
55 const int width_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
56 const int height_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
57 const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
58
59 const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
60 const unsigned int num_channels = original_input_shape[channel_idx];
61
62 const unsigned int factor_1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
63 const unsigned int factor_2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
Giorgio Arena657bdb32018-04-26 18:52:01 +010064
65 // Set build options
66 CLBuildOptions build_opts;
67 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010068 build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(factor_1));
69 build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(factor_2));
Giorgio Arena657bdb32018-04-26 18:52:01 +010070
71 // Create kernel
72 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("convert_fc_weights", build_opts.options()));
73
74 // Configure kernel window
75 Window win = calculate_max_window(*input->info(), Steps());
Anthony Barbierb6eb3532018-08-08 13:20:04 +010076 ICLKernel::configure_internal(win);
Giorgio Arena657bdb32018-04-26 18:52:01 +010077}
78
79Status CLConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape,
80 DataLayout data_layout)
81{
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010082 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010083 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1,
84 DataType::U8, DataType::S8, DataType::QASYMM8,
85 DataType::U16, DataType::S16,
86 DataType::U32, DataType::S32,
Vidhya Sudhan Loganathanf4cb81b2018-07-04 15:13:14 +010087 DataType::F16, DataType::F32);
Giorgio Arena657bdb32018-04-26 18:52:01 +010088 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() != 2);
89 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != original_input_shape.total_size_lower(3));
90 ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
91
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010092 // Checks performed when output is configured
93 if((output != nullptr) && (output->total_size() != 0))
94 {
95 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
97 }
98
Giorgio Arena657bdb32018-04-26 18:52:01 +010099 return Status{};
100}
101
102void CLConvertFullyConnectedWeightsKernel::run(const Window &window, cl::CommandQueue &queue)
103{
104 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
105 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
106
107 unsigned int idx = 0;
108 add_2D_tensor_argument(idx, _input, window);
109 add_2D_tensor_argument(idx, _output, window);
110 enqueue(queue, *this, window);
111}
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100112} // namespace arm_compute