xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/operators/CpuDepthwiseConv2dAssemblyDispatch.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2019-2022 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust 
25*c217d954SCole Faust #include "src/cpu/operators/CpuDepthwiseConv2dAssemblyDispatch.h"
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/ITensorInfo.h"
28*c217d954SCole Faust #include "arm_compute/runtime/NEON/NEScheduler.h"
29*c217d954SCole Faust #include "src/common/utils/Log.h"
30*c217d954SCole Faust #include "src/core/CPP/Validate.h"
31*c217d954SCole Faust #include "src/core/helpers/AutoConfiguration.h"
32*c217d954SCole Faust #include "src/core/utils/AssemblyUtils.h"
33*c217d954SCole Faust #include "src/cpu/kernels/internal/CpuDepthwiseConv2dAssemblyWrapperKernel.h"
34*c217d954SCole Faust 
35*c217d954SCole Faust namespace arm_compute
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace cpu
38*c217d954SCole Faust {
39*c217d954SCole Faust struct CpuDepthwiseConv2dAssemblyDispatch::LocalImpl
40*c217d954SCole Faust {
41*c217d954SCole Faust     std::unique_ptr<kernels::CpuDepthwiseConv2dAssemblyWrapperKernel> asm_kernel{ nullptr };
42*c217d954SCole Faust     bool                                                              is_prepared{ false };
43*c217d954SCole Faust     bool                                                              are_weights_const{ true };
44*c217d954SCole Faust     experimental::MemoryRequirements                                  mem_req{};
45*c217d954SCole Faust };
46*c217d954SCole Faust 
47*c217d954SCole Faust #ifndef DOXYGEN_SKIP_THIS
CpuDepthwiseConv2dAssemblyDispatch()48*c217d954SCole Faust CpuDepthwiseConv2dAssemblyDispatch::CpuDepthwiseConv2dAssemblyDispatch()
49*c217d954SCole Faust     : _pImpl(std::make_unique<LocalImpl>())
50*c217d954SCole Faust {
51*c217d954SCole Faust }
52*c217d954SCole Faust #endif /* DOXYGEN_SKIP_THIS */
53*c217d954SCole Faust 
54*c217d954SCole Faust CpuDepthwiseConv2dAssemblyDispatch::~CpuDepthwiseConv2dAssemblyDispatch() = default;
55*c217d954SCole Faust 
configure(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * bias,ITensorInfo * dst,const ConvolutionInfo & info)56*c217d954SCole Faust void CpuDepthwiseConv2dAssemblyDispatch::configure(const ITensorInfo     *src,
57*c217d954SCole Faust                                                    const ITensorInfo     *weights,
58*c217d954SCole Faust                                                    const ITensorInfo     *bias,
59*c217d954SCole Faust                                                    ITensorInfo           *dst,
60*c217d954SCole Faust                                                    const ConvolutionInfo &info)
61*c217d954SCole Faust {
62*c217d954SCole Faust     ARM_COMPUTE_LOG_PARAMS(src, weights, bias, dst, info);
63*c217d954SCole Faust     const CPUInfo     &ci          = NEScheduler::get().cpu_info();
64*c217d954SCole Faust     const unsigned int num_threads = NEScheduler::get().num_threads();
65*c217d954SCole Faust     _pImpl->is_prepared            = false;
66*c217d954SCole Faust     _pImpl->are_weights_const      = weights->are_values_constant();
67*c217d954SCole Faust 
68*c217d954SCole Faust     // If we don't support a combination of data types, silently return: it is the caller's responsibility to check if configure() was successful via is_configured()
69*c217d954SCole Faust     if(!CpuDepthwiseConv2dAssemblyDispatch::validate(src, weights, bias, dst, info))
70*c217d954SCole Faust     {
71*c217d954SCole Faust         return;
72*c217d954SCole Faust     }
73*c217d954SCole Faust 
74*c217d954SCole Faust     auto dwc_wrapper = std::make_unique<kernels::CpuDepthwiseConv2dAssemblyWrapperKernel>();
75*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(dwc_wrapper == nullptr);
76*c217d954SCole Faust     dwc_wrapper->configure(src, weights, bias, dst, info, ci);
77*c217d954SCole Faust 
78*c217d954SCole Faust     // Compute memory requirements for assembly kernels
79*c217d954SCole Faust     constexpr size_t alignment = 4096;
80*c217d954SCole Faust     _pImpl->mem_req.push_back({ TensorType::ACL_INT_0, dwc_wrapper->get_working_size(num_threads, src->dimension(0)), alignment });
81*c217d954SCole Faust     _pImpl->mem_req.push_back({ TensorType::ACL_INT_1, dwc_wrapper->get_storage_size(), alignment });
82*c217d954SCole Faust     _pImpl->asm_kernel = std::move(dwc_wrapper);
83*c217d954SCole Faust }
84*c217d954SCole Faust 
validate(const ITensorInfo * src,const ITensorInfo * weights,const ITensorInfo * bias,const ITensorInfo * dst,const ConvolutionInfo & info)85*c217d954SCole Faust Status CpuDepthwiseConv2dAssemblyDispatch::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *dst, const ConvolutionInfo &info)
86*c217d954SCole Faust {
87*c217d954SCole Faust     return kernels::CpuDepthwiseConv2dAssemblyWrapperKernel::validate(src, weights, bias, dst, info);
88*c217d954SCole Faust }
89*c217d954SCole Faust 
workspace() const90*c217d954SCole Faust experimental::MemoryRequirements CpuDepthwiseConv2dAssemblyDispatch::workspace() const
91*c217d954SCole Faust {
92*c217d954SCole Faust     return _pImpl->mem_req;
93*c217d954SCole Faust }
94*c217d954SCole Faust 
is_activation_supported(const ActivationLayerInfo & activation)95*c217d954SCole Faust bool CpuDepthwiseConv2dAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
96*c217d954SCole Faust {
97*c217d954SCole Faust     arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation);
98*c217d954SCole Faust     return act.type != arm_gemm::Activation::Type::None;
99*c217d954SCole Faust }
100*c217d954SCole Faust 
run(ITensorPack & tensors)101*c217d954SCole Faust void CpuDepthwiseConv2dAssemblyDispatch::run(ITensorPack &tensors)
102*c217d954SCole Faust {
103*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
104*c217d954SCole Faust 
105*c217d954SCole Faust     prepare(tensors);
106*c217d954SCole Faust 
107*c217d954SCole Faust     NEScheduler::get().schedule_op(_pImpl->asm_kernel.get(), Window::DimY, _pImpl->asm_kernel->window(), tensors);
108*c217d954SCole Faust }
109*c217d954SCole Faust 
prepare(ITensorPack & tensors)110*c217d954SCole Faust void CpuDepthwiseConv2dAssemblyDispatch::prepare(ITensorPack &tensors)
111*c217d954SCole Faust {
112*c217d954SCole Faust     const ITensor *weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
113*c217d954SCole Faust 
114*c217d954SCole Faust     if((!_pImpl->are_weights_const && weights != nullptr) || !_pImpl->is_prepared)
115*c217d954SCole Faust     {
116*c217d954SCole Faust         // Pack weights and bias
117*c217d954SCole Faust         const ITensor *bias    = tensors.get_const_tensor(TensorType::ACL_SRC_2);
118*c217d954SCole Faust         ITensor       *storage = tensors.get_tensor(TensorType::ACL_INT_1);
119*c217d954SCole Faust 
120*c217d954SCole Faust         const auto weights_ptr    = weights->buffer() + weights->info()->offset_first_element_in_bytes();
121*c217d954SCole Faust         const auto bias_ptr       = (bias) ? bias->buffer() + bias->info()->offset_first_element_in_bytes() : nullptr;
122*c217d954SCole Faust         auto       parameters_ptr = storage->buffer() + storage->info()->offset_first_element_in_bytes();
123*c217d954SCole Faust 
124*c217d954SCole Faust         const auto weights_shape   = weights->info()->tensor_shape();
125*c217d954SCole Faust         const auto weights_padding = weights->info()->padding();
126*c217d954SCole Faust 
127*c217d954SCole Faust         const size_t ld_weights_col = weights_shape[0] + weights_padding.left + weights_padding.right;
128*c217d954SCole Faust         const size_t ld_weights_row = ld_weights_col * (weights_shape[1] + weights_padding.top + weights_padding.bottom);
129*c217d954SCole Faust         _pImpl->asm_kernel->pack_parameters(parameters_ptr, bias_ptr, weights_ptr, ld_weights_col, ld_weights_row);
130*c217d954SCole Faust 
131*c217d954SCole Faust         weights->mark_as_unused();
132*c217d954SCole Faust         if(bias != nullptr)
133*c217d954SCole Faust         {
134*c217d954SCole Faust             bias->mark_as_unused();
135*c217d954SCole Faust         }
136*c217d954SCole Faust         _pImpl->is_prepared = true;
137*c217d954SCole Faust     }
138*c217d954SCole Faust }
139*c217d954SCole Faust } // namespace cpu
140*c217d954SCole Faust } // namespace arm_compute
141