blob: d078155155ce84c4becba6b16209c63e38e3152f [file] [log] [blame]
Georgios Pinitas47d39dc2019-03-11 14:03:23 +00001/*
Michael Tyler8deee9b2023-06-30 11:26:05 +01002 * Copyright (c) 2019-2023 Arm Limited.
Georgios Pinitas47d39dc2019-03-11 14:03:23 +00003 *
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
Georgios Pinitas7891a732021-08-20 21:39:25 +010025#include "src/cpu/operators/CpuDepthwiseConv2dAssemblyDispatch.h"
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000026
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000027#include "arm_compute/core/ITensorInfo.h"
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000028#include "arm_compute/runtime/NEON/NEScheduler.h"
ramelg013ae3d882021-09-12 23:07:47 +010029#include "src/common/utils/Log.h"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000030#include "src/core/CPP/Validate.h"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/utils/AssemblyUtils.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010033#include "src/cpu/kernels/internal/CpuDepthwiseConv2dAssemblyWrapperKernel.h"
Georgios Pinitas4c758512019-07-10 19:49:11 +010034
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000035namespace arm_compute
36{
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010037namespace cpu
38{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010039struct CpuDepthwiseConv2dAssemblyDispatch::LocalImpl
Georgios Pinitas30271c72019-06-24 14:56:34 +010040{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000041 std::unique_ptr<kernels::CpuDepthwiseConv2dAssemblyWrapperKernel> asm_kernel{ nullptr };
42 bool is_prepared{ false };
Milos Puzovica7077e92022-10-28 16:49:15 +010043 bool are_weights_const{ true };
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000044 experimental::MemoryRequirements mem_req{};
Georgios Pinitas30271c72019-06-24 14:56:34 +010045};
46
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000047#ifndef DOXYGEN_SKIP_THIS
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010048CpuDepthwiseConv2dAssemblyDispatch::CpuDepthwiseConv2dAssemblyDispatch()
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010049 : _pImpl(std::make_unique<LocalImpl>())
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000050{
51}
52#endif /* DOXYGEN_SKIP_THIS */
53
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010054CpuDepthwiseConv2dAssemblyDispatch::~CpuDepthwiseConv2dAssemblyDispatch() = default;
Georgios Pinitas30271c72019-06-24 14:56:34 +010055
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010056void CpuDepthwiseConv2dAssemblyDispatch::configure(const ITensorInfo *src,
57 const ITensorInfo *weights,
58 const ITensorInfo *bias,
59 ITensorInfo *dst,
60 const ConvolutionInfo &info)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000061{
ramelg013ae3d882021-09-12 23:07:47 +010062 ARM_COMPUTE_LOG_PARAMS(src, weights, bias, dst, info);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000063 const CPUInfo &ci = NEScheduler::get().cpu_info();
64 const unsigned int num_threads = NEScheduler::get().num_threads();
65 _pImpl->is_prepared = false;
Milos Puzovica7077e92022-10-28 16:49:15 +010066 _pImpl->are_weights_const = weights->are_values_constant();
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000067
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000068 // 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 if(!CpuDepthwiseConv2dAssemblyDispatch::validate(src, weights, bias, dst, info))
70 {
71 return;
72 }
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000073
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000074 auto dwc_wrapper = std::make_unique<kernels::CpuDepthwiseConv2dAssemblyWrapperKernel>();
75 ARM_COMPUTE_ERROR_ON(dwc_wrapper == nullptr);
76 dwc_wrapper->configure(src, weights, bias, dst, info, ci);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000077
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000078 // Compute memory requirements for assembly kernels
79 constexpr size_t alignment = 4096;
Michael Tyler8deee9b2023-06-30 11:26:05 +010080 _pImpl->mem_req.push_back({ TensorType::ACL_INT_0, dwc_wrapper->get_working_size(num_threads), alignment });
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000081 _pImpl->mem_req.push_back({ TensorType::ACL_INT_1, dwc_wrapper->get_storage_size(), alignment });
82 _pImpl->asm_kernel = std::move(dwc_wrapper);
83}
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000084
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000085Status CpuDepthwiseConv2dAssemblyDispatch::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *bias, const ITensorInfo *dst, const ConvolutionInfo &info)
86{
87 return kernels::CpuDepthwiseConv2dAssemblyWrapperKernel::validate(src, weights, bias, dst, info);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000088}
89
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010090experimental::MemoryRequirements CpuDepthwiseConv2dAssemblyDispatch::workspace() const
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +010091{
92 return _pImpl->mem_req;
93}
94
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000095bool CpuDepthwiseConv2dAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000096{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000097 arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation);
98 return act.type != arm_gemm::Activation::Type::None;
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000099}
100
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100101void CpuDepthwiseConv2dAssemblyDispatch::run(ITensorPack &tensors)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000102{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000103 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
104
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100105 prepare(tensors);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000106
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000107 NEScheduler::get().schedule_op(_pImpl->asm_kernel.get(), Window::DimY, _pImpl->asm_kernel->window(), tensors);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000108}
109
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100110void CpuDepthwiseConv2dAssemblyDispatch::prepare(ITensorPack &tensors)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000111{
Milos Puzovica7077e92022-10-28 16:49:15 +0100112 const ITensor *weights = tensors.get_const_tensor(TensorType::ACL_SRC_1);
113
114 if((!_pImpl->are_weights_const && weights != nullptr) || !_pImpl->is_prepared)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000115 {
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000116 // Pack weights and bias
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000117 const ITensor *bias = tensors.get_const_tensor(TensorType::ACL_SRC_2);
118 ITensor *storage = tensors.get_tensor(TensorType::ACL_INT_1);
119
120 const auto weights_ptr = weights->buffer() + weights->info()->offset_first_element_in_bytes();
121 const auto bias_ptr = (bias) ? bias->buffer() + bias->info()->offset_first_element_in_bytes() : nullptr;
122 auto parameters_ptr = storage->buffer() + storage->info()->offset_first_element_in_bytes();
123
124 const auto weights_shape = weights->info()->tensor_shape();
125 const auto weights_padding = weights->info()->padding();
126
127 const size_t ld_weights_col = weights_shape[0] + weights_padding.left + weights_padding.right;
128 const size_t ld_weights_row = ld_weights_col * (weights_shape[1] + weights_padding.top + weights_padding.bottom);
129 _pImpl->asm_kernel->pack_parameters(parameters_ptr, bias_ptr, weights_ptr, ld_weights_col, ld_weights_row);
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000130
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100131 weights->mark_as_unused();
132 if(bias != nullptr)
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000133 {
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100134 bias->mark_as_unused();
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000135 }
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100136 _pImpl->is_prepared = true;
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000137 }
138}
Michalis Spyrou60c3b0e2021-04-08 12:02:58 +0100139} // namespace cpu
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000140} // namespace arm_compute