xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/kernels/ClConvertFullyConnectedWeightsKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
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/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 
37 namespace arm_compute
38 {
39 namespace opencl
40 {
41 namespace kernels
42 {
ClConvertFullyConnectedWeightsKernel()43 ClConvertFullyConnectedWeightsKernel::ClConvertFullyConnectedWeightsKernel()
44 {
45     _type = CLKernelType::ELEMENTWISE;
46 }
47 
configure(const CLCompileContext & compile_context,const ITensorInfo * src,ITensorInfo * dst,const TensorShape & original_src_shape,DataLayout data_layout)48 void ClConvertFullyConnectedWeightsKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *dst, const TensorShape &original_src_shape,
49                                                      DataLayout data_layout)
50 {
51     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
52 
53     // Output tensor auto initialisation if not yet initialized
54     auto_init_if_empty(*dst, *src->clone());
55 
56     auto padding_info = get_padding_info({ src, dst });
57 
58     ARM_COMPUTE_ERROR_THROW_ON(ClConvertFullyConnectedWeightsKernel::validate(src, dst, original_src_shape, data_layout));
59 
60     const DataLayout src_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
61 
62     const int width_idx   = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::WIDTH);
63     const int height_idx  = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::HEIGHT);
64     const int channel_idx = get_data_layout_dimension_index(src_data_layout, DataLayoutDimension::CHANNEL);
65 
66     const unsigned int num_elems_per_src_plane = original_src_shape[width_idx] * original_src_shape[height_idx];
67     const unsigned int num_channels            = original_src_shape[channel_idx];
68 
69     const unsigned int factor_1 = (data_layout == DataLayout::NCHW) ? num_elems_per_src_plane : num_channels;
70     const unsigned int factor_2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_src_plane;
71 
72     // Set build options
73     CLBuildOptions build_opts;
74     build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(src->element_size()));
75     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));
77 
78     // Create kernel
79     _kernel = create_kernel(compile_context, "convert_fc_weights", build_opts.options());
80 
81     // Configure kernel window
82     Window win = calculate_max_window(*src, Steps());
83     ICLKernel::configure_internal(win);
84 
85     ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
86 }
87 
validate(const ITensorInfo * src,const ITensorInfo * dst,const TensorShape & original_src_shape,DataLayout data_layout)88 Status ClConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const TensorShape &original_src_shape,
89                                                       DataLayout data_layout)
90 {
91     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
92     ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
93     ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
94     ARM_COMPUTE_RETURN_ERROR_ON(src->num_dimensions() != 2);
95     ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(1) != original_src_shape.total_size_lower(3));
96     ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
97 
98     // Checks performed when dst is configured
99     if(dst->total_size() != 0)
100     {
101         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
102         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
103     }
104 
105     return Status{};
106 }
107 
run_op(ITensorPack & tensors,const Window & window,::cl::CommandQueue & queue)108 void ClConvertFullyConnectedWeightsKernel::run_op(ITensorPack &tensors, const Window &window, ::cl::CommandQueue &queue)
109 {
110     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
111     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
112 
113     const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
114     auto       dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
115     ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
116 
117     unsigned int idx = 0;
118     add_2D_tensor_argument(idx, src, window);
119     add_2D_tensor_argument(idx, dst, window);
120     enqueue(queue, *this, window, lws_hint());
121 }
122 } // namespace kernels
123 } // namespace opencl
124 } // namespace arm_compute
125