1 /*
2 * Copyright (c) 2018-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/ClWinogradFilterTransformKernel.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/TensorInfo.h"
31 #include "arm_compute/core/Types.h"
32 #include "arm_compute/core/Utils.h"
33 #include "arm_compute/core/Validate.h"
34 #include "arm_compute/core/Window.h"
35 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
36 #include "src/core/CL/CLValidate.h"
37 #include "src/core/helpers/AutoConfiguration.h"
38 #include "src/core/helpers/WindowHelpers.h"
39 #include "support/Cast.h"
40 #include "support/StringSupport.h"
41
42 using namespace arm_compute::misc::shape_calculator;
43
44 namespace arm_compute
45 {
46 namespace opencl
47 {
48 namespace kernels
49 {
50 namespace
51 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const WinogradInfo & winograd_info)52 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
53 {
54 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
55 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
56
57 const Size2D kernel_size = winograd_info.kernel_size;
58 const Size2D output_tile_size = winograd_info.output_tile_size;
59
60 const size_t idx_w = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
61 const size_t idx_h = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
62
63 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!cl_winograd_convolution_layer_supported(output_tile_size, kernel_size, input->data_layout()), "Winograd filter transform not supported");
64 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_w) != kernel_size.width || input->dimension(idx_h) != kernel_size.height);
65 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
66
67 // Checks performed when output is configured
68 if(output->total_size() != 0)
69 {
70 const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_winograd_filter_transform_shape(*input, winograd_info));
71
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
73 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
74 }
75
76 return Status{};
77 }
78
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output)79 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
80 {
81 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
82 ARM_COMPUTE_UNUSED(output);
83
84 const unsigned int num_elems_processed_per_iteration_x = input->data_layout() == DataLayout::NCHW ? input->dimension(0) : 1;
85 const unsigned int num_elems_processed_per_iteration_y = input->dimension(1);
86 const unsigned int num_elems_read_per_iteration_z = input->data_layout() == DataLayout::NCHW ? 1 : input->dimension(2);
87
88 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y, num_elems_read_per_iteration_z));
89 Window win_collapsed = win.collapse(win, Window::DimZ);
90 return std::make_pair(Status{}, win_collapsed);
91 }
92 } // namespace
93
ClWinogradFilterTransformKernel()94 ClWinogradFilterTransformKernel::ClWinogradFilterTransformKernel()
95 {
96 _type = CLKernelType::WINOGRAD;
97 }
98
configure(const ClCompileContext & compile_context,ITensorInfo * src,ITensorInfo * dst,const WinogradInfo & winograd_info)99 void ClWinogradFilterTransformKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const WinogradInfo &winograd_info)
100 {
101 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
102
103 // Output auto initialization if not yet initialized
104 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_winograd_filter_transform_shape(*src, winograd_info)));
105
106 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, winograd_info));
107 auto padding_info = get_padding_info({ src, dst });
108
109 // Set build options
110 CLBuildOptions build_opts;
111
112 // For NHWC layouts pass tensor dimesions at runtime
113 if(src->data_layout() == DataLayout::NHWC)
114 {
115 _src_dim_z = src->dimension(2);
116 }
117 else
118 {
119 build_opts.add_option("-DSRC_DIM_Z=" + support::cpp11::to_string(src->dimension(2)));
120 }
121 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(src->data_type()));
122 build_opts.add_option_if(winograd_info.kernel_size.height == 1, "-DWINOGRAD_FILTER_TRANSFORM_HORIZONTAL");
123 build_opts.add_option_if(winograd_info.kernel_size.width == 1, "-DWINOGRAD_FILTER_TRANSFORM_VERTICAL");
124 const Size2D kernel_size = winograd_info.kernel_size;
125 const Size2D output_tile_size = winograd_info.output_tile_size;
126
127 // Create kernel
128 std::string kernel_name = "winograd_filter_transform_" + output_tile_size.to_string() + "_" + kernel_size.to_string() + "_" + lower_string(string_from_data_layout(src->data_layout()));
129
130 // A macro guard to compile ONLY the kernel of interest
131 build_opts.add_option("-D" + upper_string(kernel_name));
132 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
133
134 // Configure kernel window
135 auto win_config = validate_and_configure_window(src, dst);
136 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
137 IClKernel::configure_internal(win_config.second);
138 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
139 }
140
validate(const ITensorInfo * src,const ITensorInfo * dst,const WinogradInfo & winograd_info)141 Status ClWinogradFilterTransformKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const WinogradInfo &winograd_info)
142 {
143 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, winograd_info));
144 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), dst->clone().get()).first);
145
146 return Status{};
147 }
148
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)149 void ClWinogradFilterTransformKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
150 {
151 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
152 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IClKernel::window(), window);
153
154 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
155 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
156
157 // Setup output window
158 Window window_out;
159 window_out.use_tensor_dimensions(dst->info()->tensor_shape(), 0);
160
161 unsigned int idx = 0;
162 add_4D_tensor_argument(idx, src, window);
163 add_3D_tensor_argument(idx, dst, window_out);
164 if(src->info()->data_layout() == DataLayout::NHWC)
165 {
166 _kernel.setArg<cl_uint>(idx++, _src_dim_z);
167 }
168 enqueue(queue, *this, window, lws_hint());
169 }
170 } // namespace kernels
171 } // namespace opencl
172 } // namespace arm_compute
173