xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/CL/functions/CLConvolutionLayer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-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 "arm_compute/runtime/CL/functions/CLConvolutionLayer.h"
25 
26 #include "arm_compute/core/CL/CLKernelLibrary.h"
27 #include "arm_compute/core/CL/ICLTensor.h"
28 #include "arm_compute/core/KernelDescriptors.h"
29 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30 #include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h"
31 #include "src/core/CL/ICLKernel.h"
32 #include "src/core/experimental/PostOpUtils.h"
33 #include "src/core/helpers/MemoryHelpers.h"
34 #include "src/gpu/cl/operators/ClConv2d.h"
35 
36 #include "src/common/utils/Log.h"
37 #include "support/Cast.h"
38 
39 namespace arm_compute
40 {
41 using namespace arm_compute::misc::shape_calculator;
42 using namespace arm_compute::experimental;
43 struct CLConvolutionLayer::Impl
44 {
45     MemoryGroup                          memory_group{};
46     std::shared_ptr<IMemoryManager>      memory_manager{};
47     std::unique_ptr<opencl::IClOperator> op{ nullptr };
48     ITensorPack                          run_pack{};
49     ITensorPack                          prep_pack{};
50     WorkspaceData<CLTensor>              workspace{};
51     experimental::MemoryRequirements     aux_mem_req{};
52     std::unique_ptr<IFunction>           func{ nullptr };
53 };
54 
CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)55 CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
56     : _impl(std::make_unique<Impl>())
57 {
58     _impl->memory_manager = std::move(memory_manager);
59 }
60 
61 CLConvolutionLayer::~CLConvolutionLayer() = default;
62 
configure(ICLTensor * input,const ICLTensor * weights,const ICLTensor * biases,ICLTensor * output,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math,unsigned int num_groups,const experimental::PostOpList<ICLTensor * > & post_ops)63 void CLConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
64                                    const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
65 {
66     configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups, post_ops);
67 }
68 
configure(const CLCompileContext & compile_context,ICLTensor * input,const ICLTensor * weights,const ICLTensor * biases,ICLTensor * output,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math,unsigned int num_groups,const experimental::PostOpList<ICLTensor * > & post_ops)69 void CLConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
70                                    const WeightsInfo &weights_info,
71                                    const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
72 {
73     ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
74     ARM_COMPUTE_ERROR_THROW_ON(CLConvolutionLayer::validate(input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv_info, weights_info, dilation, act_info,
75                                                             enable_fast_math, num_groups));
76     ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups, post_ops);
77 
78     // Convert post op arguments to ITensorInfo
79     auto transformed_post_ops = experimental::transform_post_op_list_arguments<ICLTensor *, ITensorInfo *>(post_ops, [](auto tensor)
80     {
81         return tensor->info();
82     });
83     const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups, transformed_post_ops);
84 
85     switch(opencl::ClConv2d::get_convolution_method(input->info(), weights->info(), output->info(), conv2d_info,
86                                                     weights_info, CLScheduler::get().target()))
87     {
88         case ConvolutionMethod::WINOGRAD:
89         case ConvolutionMethod::DIRECT:
90         case ConvolutionMethod::INDIRECT:
91         case ConvolutionMethod::GEMM:
92         {
93             auto f = std::make_unique<opencl::ClConv2d>();
94             f->configure(compile_context, input->info(), weights->info(), ((biases != nullptr) ? biases->info() : nullptr), output->info(), conv2d_info, weights_info);
95             _impl->op = std::move(f);
96             break;
97         }
98         case ConvolutionMethod::FFT:
99         {
100             ARM_COMPUTE_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops");
101             auto f = std::make_unique<CLFFTConvolutionLayer>(_impl->memory_manager);
102             f->configure(compile_context, input, weights, biases, output, conv_info, act_info, enable_fast_math);
103             _impl->func = std::move(f);
104             break;
105         }
106         default:
107             ARM_COMPUTE_ERROR("Not supported.");
108             break;
109     }
110 
111     if(_impl->op)
112     {
113         _impl->memory_group         = MemoryGroup(std::move(_impl->memory_manager));
114         _impl->aux_mem_req          = _impl->op->workspace();
115         _impl->run_pack             = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
116         size_t post_op_tensor_index = 0;
117         for(const auto &op : post_ops.get_list())
118         {
119             for(auto &tensor : op->arguments())
120             {
121                 _impl->run_pack.add_const_tensor(experimental::get_post_op_arg_type(post_op_tensor_index++), *tensor);
122             }
123         }
124         _impl->prep_pack = { { ACL_SRC_1, weights }, { ACL_SRC_2, biases } };
125         _impl->workspace = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
126     }
127 }
128 
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const Size2D & dilation,const ActivationLayerInfo & act_info,bool enable_fast_math,unsigned int num_groups,const experimental::PostOpList<ITensorInfo * > & post_ops)129 Status CLConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
130                                     const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups, const experimental::PostOpList<ITensorInfo *> &post_ops)
131 {
132     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
133     ARM_COMPUTE_RETURN_ERROR_ON_MSG((num_groups != 1) && (input->data_layout() != DataLayout::NCHW), "Grouping (num_groups != 1) with NHWC data layout is not supported");
134 
135     const GPUTarget  gpu_target  = CLScheduler::get().target();
136     const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, num_groups, post_ops);
137 
138     switch(opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target))
139     {
140         case ConvolutionMethod::WINOGRAD:
141         case ConvolutionMethod::DIRECT:
142         case ConvolutionMethod::INDIRECT:
143         case ConvolutionMethod::GEMM:
144         {
145             ARM_COMPUTE_RETURN_ON_ERROR(opencl::ClConv2d::validate(input, weights, biases, output, conv2d_info, weights_info));
146             break;
147         }
148         case ConvolutionMethod::FFT:
149         {
150             // Validate FFT-based convolution layer
151             ARM_COMPUTE_RETURN_ERROR_ON_MSG(post_ops.size() > 0, "CLFFTConvolutionLayer does not support post ops");
152             ARM_COMPUTE_RETURN_ON_ERROR(CLFFTConvolutionLayer::validate(input, weights, nullptr, output, conv_info, act_info, enable_fast_math));
153             break;
154         }
155         default:
156             ARM_COMPUTE_ERROR("Not supported.");
157             break;
158     }
159 
160     return Status{};
161 }
162 
get_convolution_method(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * output,const PadStrideInfo & conv_info,const WeightsInfo & weights_info,const ActivationLayerInfo & act_info,const GPUTarget gpu_target,const Size2D & dilation,bool enable_fast_math)163 ConvolutionMethod CLConvolutionLayer::get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info,
164                                                              const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation, bool enable_fast_math)
165 {
166     const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, enable_fast_math, 1);
167     return opencl::ClConv2d::get_convolution_method(input, weights, output, conv2d_info, weights_info, gpu_target);
168 }
169 
run()170 void CLConvolutionLayer::run()
171 {
172     prepare();
173 
174     MemoryGroupResourceScope scope_mg(_impl->memory_group);
175 
176     if(_impl->func)
177     {
178         _impl->func->run();
179     }
180     else
181     {
182         _impl->op->run(_impl->run_pack);
183     }
184 }
185 
prepare()186 void CLConvolutionLayer::prepare()
187 {
188     if(_impl->func)
189     {
190         _impl->func->prepare();
191     }
192     else
193     {
194         _impl->op->prepare(_impl->prep_pack);
195 
196         // Release temporary tensors that are only used in prepare stage
197         release_temporaries(_impl->aux_mem_req, _impl->workspace);
198     }
199 }
200 } // namespace arm_compute
201