blob: 49f2f68a76908e7cc0f71754a82d5bae025bf882 [file] [log] [blame]
Teresa Charlin91b7f742021-04-12 13:57:00 +01001/*
2 * Copyright (c) 2018-2021 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 "src/core/gpu/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"
30#include "arm_compute/core/Utils.h"
31#include "src/core/CL/CLValidate.h"
32#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
34#include "support/Cast.h"
35#include "support/StringSupport.h"
36
37namespace arm_compute
38{
39namespace opencl
40{
41namespace kernels
42{
43void ClConvertFullyConnectedWeightsKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *dst, const TensorShape &original_src_shape,
44 DataLayout data_layout)
45{
46 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
47
48 // Output tensor auto initialisation if not yet initialized
49 auto_init_if_empty(*dst, *src->clone());
50
51 auto padding_info = get_padding_info({ src, dst });
52
53 ARM_COMPUTE_ERROR_THROW_ON(ClConvertFullyConnectedWeightsKernel::validate(src, dst, original_src_shape, data_layout));
54
55 const DataLayout src_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
56
57 const int width_idx = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::WIDTH);
58 const int height_idx = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::HEIGHT);
59 const int channel_idx = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::CHANNEL);
60
61 const unsigned int num_elems_per_src_plane = original_src_shape[width_idx] * original_src_shape[height_idx];
62 const unsigned int num_channels = original_src_shape[channel_idx];
63
64 const unsigned int factor_1 = (data_layout == DataLayout::NCHW) ? num_elems_per_src_plane : num_channels;
65 const unsigned int factor_2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_src_plane;
66
67 // Set build options
68 CLBuildOptions build_opts;
69 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(src->element_size()));
70 build_opts.add_option("-DFACTOR_1=" + support::cpp11::to_string(factor_1));
71 build_opts.add_option("-DFACTOR_2=" + support::cpp11::to_string(factor_2));
72
73 // Create kernel
74 _kernel = create_kernel(compile_context, "convert_fc_weights", build_opts.options());
75
76 // Configure kernel window
77 Window win = calculate_max_window(*src, Steps());
78 ICLKernel::configure_internal(win);
79
80 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
81}
82
83Status ClConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const TensorShape &original_src_shape,
84 DataLayout data_layout)
85{
86 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
87 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
88 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
89 ARM_COMPUTE_RETURN_ERROR_ON(src->num_dimensions() != 2);
90 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(1) != original_src_shape.total_size_lower(3));
91 ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
92
93 // Checks performed when dst is configured
94 if(dst->total_size() != 0)
95 {
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
97 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
98 }
99
100 return Status{};
101}
102
103void ClConvertFullyConnectedWeightsKernel::run_op(ITensorPack &tensors, 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 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
109 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
110 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
111
112 unsigned int idx = 0;
113 add_2D_tensor_argument(idx, src, window);
114 add_2D_tensor_argument(idx, dst, window);
115 enqueue(queue, *this, window, lws_hint());
116}
117} // namespace kernels
118} // namespace opencl
119} // namespace arm_compute