Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 1 | /* |
Jonathan Deakin | 2db938c | 2024-02-05 15:32:31 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2019-2024 Arm Limited. |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 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 | |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 25 | #include "src/cpu/operators/CpuDepthwiseConv2dAssemblyDispatch.h" |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 26 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/ITensorInfo.h" |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 30 | #include "src/common/utils/Log.h" |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 31 | #include "src/core/CPP/Validate.h" |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/utils/AssemblyUtils.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 34 | #include "src/cpu/kernels/internal/CpuDepthwiseConv2dAssemblyWrapperKernel.h" |
Georgios Pinitas | 4c75851 | 2019-07-10 19:49:11 +0100 | [diff] [blame] | 35 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 38 | namespace cpu |
| 39 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 40 | struct CpuDepthwiseConv2dAssemblyDispatch::LocalImpl |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 41 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | std::unique_ptr<kernels::CpuDepthwiseConv2dAssemblyWrapperKernel> asm_kernel{nullptr}; |
| 43 | bool is_prepared{false}; |
| 44 | bool are_weights_const{true}; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 45 | experimental::MemoryRequirements mem_req{}; |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 48 | #ifndef DOXYGEN_SKIP_THIS |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | CpuDepthwiseConv2dAssemblyDispatch::CpuDepthwiseConv2dAssemblyDispatch() : _pImpl(std::make_unique<LocalImpl>()) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | #endif /* DOXYGEN_SKIP_THIS */ |
| 53 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 54 | CpuDepthwiseConv2dAssemblyDispatch::~CpuDepthwiseConv2dAssemblyDispatch() = default; |
Georgios Pinitas | 30271c7 | 2019-06-24 14:56:34 +0100 | [diff] [blame] | 55 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 56 | void CpuDepthwiseConv2dAssemblyDispatch::configure(const ITensorInfo *src, |
| 57 | const ITensorInfo *weights, |
| 58 | const ITensorInfo *bias, |
| 59 | ITensorInfo *dst, |
| 60 | const ConvolutionInfo &info) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 61 | { |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_LOG_PARAMS(src, weights, bias, dst, info); |
Pablo Marquez Tello | 17e116e | 2023-12-05 15:44:50 +0000 | [diff] [blame] | 63 | const CPUInfo &ci = NEScheduler::get().cpu_info(); |
| 64 | const unsigned int num_threads = NEScheduler::get().num_threads(); |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 65 | _pImpl->is_prepared = false; |
Milos Puzovic | a7077e9 | 2022-10-28 16:49:15 +0100 | [diff] [blame] | 66 | _pImpl->are_weights_const = weights->are_values_constant(); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 67 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 68 | // 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() |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | if (!CpuDepthwiseConv2dAssemblyDispatch::validate(src, weights, bias, dst, info)) |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 70 | { |
| 71 | return; |
| 72 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 73 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 74 | 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 Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 77 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 78 | // Compute memory requirements for assembly kernels |
| 79 | constexpr size_t alignment = 4096; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | _pImpl->mem_req.push_back({TensorType::ACL_INT_0, dwc_wrapper->get_working_size(num_threads), alignment}); |
| 81 | _pImpl->mem_req.push_back({TensorType::ACL_INT_1, dwc_wrapper->get_storage_size(), alignment}); |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 82 | _pImpl->asm_kernel = std::move(dwc_wrapper); |
| 83 | } |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 84 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | Status CpuDepthwiseConv2dAssemblyDispatch::validate(const ITensorInfo *src, |
| 86 | const ITensorInfo *weights, |
| 87 | const ITensorInfo *bias, |
| 88 | const ITensorInfo *dst, |
| 89 | const ConvolutionInfo &info) |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 90 | { |
| 91 | return kernels::CpuDepthwiseConv2dAssemblyWrapperKernel::validate(src, weights, bias, dst, info); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 94 | experimental::MemoryRequirements CpuDepthwiseConv2dAssemblyDispatch::workspace() const |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 95 | { |
| 96 | return _pImpl->mem_req; |
| 97 | } |
| 98 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 99 | bool CpuDepthwiseConv2dAssemblyDispatch::is_activation_supported(const ActivationLayerInfo &activation) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 100 | { |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 101 | arm_gemm::Activation act = assembly_utils::map_to_arm_gemm_activation(activation); |
| 102 | return act.type != arm_gemm::Activation::Type::None; |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 105 | void CpuDepthwiseConv2dAssemblyDispatch::run(ITensorPack &tensors) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 106 | { |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided"); |
| 108 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 109 | prepare(tensors); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 110 | |
Jonathan Deakin | 2db938c | 2024-02-05 15:32:31 +0000 | [diff] [blame^] | 111 | // Split over rows (z) if there's more than 1, otherwise batches (w). This logic |
| 112 | // corresponds to the threading strategy in DepthFirstDriver::execute_internal |
| 113 | auto split_dimension = _pImpl->asm_kernel->window().num_iterations(Window::DimZ) == 1 ? Window::DimZ : Window::DimW; |
| 114 | |
| 115 | NEScheduler::get().schedule_op(_pImpl->asm_kernel.get(), split_dimension, _pImpl->asm_kernel->window(), tensors); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 118 | void CpuDepthwiseConv2dAssemblyDispatch::prepare(ITensorPack &tensors) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 119 | { |
Milos Puzovic | a7077e9 | 2022-10-28 16:49:15 +0100 | [diff] [blame] | 120 | const ITensor *weights = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 121 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | if ((!_pImpl->are_weights_const && weights != nullptr) || !_pImpl->is_prepared) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 123 | { |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 124 | // Pack weights and bias |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 125 | const ITensor *bias = tensors.get_const_tensor(TensorType::ACL_SRC_2); |
| 126 | ITensor *storage = tensors.get_tensor(TensorType::ACL_INT_1); |
| 127 | |
| 128 | const auto weights_ptr = weights->buffer() + weights->info()->offset_first_element_in_bytes(); |
| 129 | const auto bias_ptr = (bias) ? bias->buffer() + bias->info()->offset_first_element_in_bytes() : nullptr; |
| 130 | auto parameters_ptr = storage->buffer() + storage->info()->offset_first_element_in_bytes(); |
| 131 | |
| 132 | const auto weights_shape = weights->info()->tensor_shape(); |
| 133 | const auto weights_padding = weights->info()->padding(); |
| 134 | |
| 135 | const size_t ld_weights_col = weights_shape[0] + weights_padding.left + weights_padding.right; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | const size_t ld_weights_row = |
| 137 | ld_weights_col * (weights_shape[1] + weights_padding.top + weights_padding.bottom); |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 138 | _pImpl->asm_kernel->pack_parameters(parameters_ptr, bias_ptr, weights_ptr, ld_weights_col, ld_weights_row); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 139 | |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 140 | weights->mark_as_unused(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 141 | if (bias != nullptr) |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 142 | { |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 143 | bias->mark_as_unused(); |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 144 | } |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 145 | _pImpl->is_prepared = true; |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
Michalis Spyrou | 60c3b0e | 2021-04-08 12:02:58 +0100 | [diff] [blame] | 148 | } // namespace cpu |
Georgios Pinitas | 47d39dc | 2019-03-11 14:03:23 +0000 | [diff] [blame] | 149 | } // namespace arm_compute |