blob: 8e12f23fa6e9310128c7b5cf16c1aa6dc3a9be15 [file] [log] [blame]
Gian Marco Iodice5d016812022-11-17 11:03:39 +00001/*
2 * Copyright (c) 2022 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/ClIndirectConv2dAddressPrecalculationKernel.h"
25
26#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/KernelDescriptors.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.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{
43namespace
44{
45Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst,
46 const PadStrideInfo &conv_info, const DirectConvComputeKernelInfo &desc)
47{
48 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, weights);
51 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(src, DataLayout::NHWC);
52 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(0) != src->dimension(0), "Weights feature map dimension should match the respective src's one");
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->num_dimensions() > 4, "Weights can be at most 4 dimensional");
54 ARM_COMPUTE_RETURN_ERROR_ON_MSG(desc.m0 <= 0 || desc.m0 > 8, "M0 can only be greater than 0 and less than or equal to 8");
55
56 // Checks performed when dst is configured
57 if(dst->total_size() != 0)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(),
60 misc::shape_calculator::compute_indirect_buffer_shape(src->tensor_shape(),
61 src->data_layout(),
62 weights->tensor_shape(),
63 conv_info,
64 desc));
65 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::S32);
66 }
67
68 return Status{};
69}
70} // namespace
71
72ClIndirectConv2dAddressPrecalculationKernel::ClIndirectConv2dAddressPrecalculationKernel()
73{
74 _type = CLKernelType::ELEMENTWISE;
75}
76
77void ClIndirectConv2dAddressPrecalculationKernel::configure(const CLCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *dst,
78 const PadStrideInfo &conv_info, const DirectConvComputeKernelInfo &desc)
79{
80 ARM_COMPUTE_ERROR_ON_NULLPTR(src, weights, dst);
81 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, dst, conv_info, desc));
82
Gian Marco Iodice76335eb2022-11-17 11:03:39 +000083 constexpr unsigned int width_idx = 1;
84 constexpr unsigned int height_idx = 2;
Gian Marco Iodice5d016812022-11-17 11:03:39 +000085
86 // Get dst shape
87 TensorShape output_shape = misc::shape_calculator::compute_indirect_buffer_shape(src->tensor_shape(),
88 src->data_layout(),
89 weights->tensor_shape(),
90 conv_info,
91 desc);
92
93 TensorShape output_conv_shape = misc::shape_calculator::compute_deep_convolution_shape(*src, *weights, conv_info);
94
95 // Output auto inizialitation if not yet initialized
96 auto_init_if_empty(*dst, output_shape, 1, DataType::S32);
97
98 // Configure kernel window
99 Window win;
100
101 // Create window and update padding
102 win = calculate_max_window(output_shape, Steps(1));
103
104 ICLKernel::configure_internal(win);
105
106 std::stringstream kernel_name;
107 CLBuildOptions build_options;
108
109 kernel_name << "indirect_convolution_address_precalculation";
110
111 const unsigned int pad_left = conv_info.pad_left();
112 const unsigned int pad_top = conv_info.pad_top();
113 const unsigned int conv_stride_x = std::get<0>(conv_info.stride());
114 const unsigned int conv_stride_y = std::get<1>(conv_info.stride());
115 const auto dst_data_type = dst->data_type();
116
117 build_options.add_option("-DSRC_CONV_WIDTH=" + support::cpp11::to_string(src->dimension(width_idx)));
118 build_options.add_option("-DSRC_CONV_HEIGHT=" + support::cpp11::to_string(src->dimension(height_idx)));
119 build_options.add_option("-DDST_CONV_WIDTH=" + support::cpp11::to_string(output_conv_shape[width_idx]));
120 build_options.add_option("-DDST_CONV_HEIGHT=" + support::cpp11::to_string(output_conv_shape[height_idx]));
121 build_options.add_option("-DDST_TENSOR_TYPE=BUFFER");
122 build_options.add_option("-DDST_DATA_TYPE=" + get_cl_type_from_data_type(dst_data_type));
123 build_options.add_option("-DWEI_CONV_WIDTH=" + support::cpp11::to_string(weights->dimension(width_idx)));
124 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
125 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
126 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
127 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
128 build_options.add_option("-DM0=" + support::cpp11::to_string(desc.m0));
129
Gian Marco Iodice76335eb2022-11-17 11:03:39 +0000130 // A macro guard to compile ONLY the kernel of interest
131 build_options.add_option("-D" + upper_string(kernel_name.str()));
132
Gian Marco Iodice5d016812022-11-17 11:03:39 +0000133 _kernel = create_kernel(compile_context, kernel_name.str(), build_options.options());
134
135 // Since this kernel should be called only once, we do not need to set the config_id for tuning
136}
137
138Status ClIndirectConv2dAddressPrecalculationKernel::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst,
139 const PadStrideInfo &conv_info, const DirectConvComputeKernelInfo &desc)
140{
141 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, dst, conv_info, desc));
142 return Status{};
143}
144
145void ClIndirectConv2dAddressPrecalculationKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
146{
147 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
148 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
149
150 // Get initial windows
151 const Window slice = window.first_slice_window_3D();
152
153 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
154
155 unsigned int idx = 0;
156 add_4d_tensor_nhwc_argument(idx, dst);
157 enqueue(queue, *this, slice);
158}
159} // namespace kernels
160} // namespace opencl
161} // namespace arm_compute