Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 2ee9801 | 2021-02-15 20:42:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +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 | #include "arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h" |
| 25 | |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Size2D.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 29 | #include "arm_compute/runtime/Tensor.h" |
| 30 | #include "src/core/helpers/MemoryHelpers.h" |
| 31 | #include "src/runtime/cpu/operators/CpuGemmConvolution.h" |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 32 | |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 33 | using namespace arm_compute::experimental; |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 34 | |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 37 | struct NEGEMMConvolutionLayer::Impl |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 38 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 39 | const ITensor *weights{ nullptr }; |
| 40 | std::unique_ptr<cpu::CpuGemmConvolution> op{ nullptr }; |
| 41 | ITensorPack run_pack{}; |
| 42 | ITensorPack prep_pack{}; |
| 43 | MemoryGroup memory_group{}; |
| 44 | IWeightsManager *weights_manager{ nullptr }; |
| 45 | MemoryRequirements aux_mem_req{}; |
| 46 | WorkspaceData<Tensor> workspace_tensors{}; |
| 47 | bool is_prepared{ false }; |
| 48 | }; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 49 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 50 | NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager, IWeightsManager *weights_manager) |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 51 | : _impl(std::make_unique<Impl>()) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 52 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 53 | _impl->weights_manager = weights_manager; |
| 54 | _impl->memory_group = MemoryGroup(memory_manager); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 55 | } |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 56 | NEGEMMConvolutionLayer::~NEGEMMConvolutionLayer() = default; |
Gian Marco Iodice | 597a856 | 2018-08-01 15:06:06 +0100 | [diff] [blame] | 57 | |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 58 | void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 59 | const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 60 | { |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 62 | _impl->weights = weights; |
| 63 | _impl->op = std::make_unique<cpu::CpuGemmConvolution>(); |
| 64 | _impl->op->configure(input->info(), weights->info(), (biases != nullptr ? biases->info() : nullptr), output->info(), conv_info, weights_info, dilation, act_info, num_groups); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 65 | |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 66 | _impl->run_pack = |
Gian Marco Iodice | db9d46d | 2018-08-08 12:29:38 +0100 | [diff] [blame] | 67 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 68 | { TensorType::ACL_SRC_0, input }, |
| 69 | { TensorType::ACL_SRC_1, weights }, |
| 70 | { TensorType::ACL_SRC_2, biases }, |
| 71 | { TensorType::ACL_DST, output } |
| 72 | }; |
| 73 | _impl->prep_pack = |
Georgios Pinitas | e413d25 | 2018-11-14 18:29:58 +0000 | [diff] [blame] | 74 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 75 | { TensorType::ACL_SRC_1, weights }, |
| 76 | { TensorType::ACL_SRC_2, biases }, |
| 77 | }; |
| 78 | _impl->aux_mem_req = _impl->op->workspace(); |
| 79 | _impl->workspace_tensors = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 83 | const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups) |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 84 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 85 | return cpu::CpuGemmConvolution::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info, num_groups); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void NEGEMMConvolutionLayer::run() |
| 89 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 90 | prepare(); |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 91 | MemoryGroupResourceScope scope_mg(_impl->memory_group); |
| 92 | _impl->op->run(_impl->run_pack); |
Isabella Gottardi | 6acc6ad | 2018-02-02 17:19:18 +0000 | [diff] [blame] | 93 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 94 | |
| 95 | void NEGEMMConvolutionLayer::prepare() |
| 96 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 97 | if(!_impl->is_prepared) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 98 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 99 | _impl->op->prepare(_impl->prep_pack); |
| 100 | auto has_reshape = std::find_if(_impl->aux_mem_req.begin(), |
| 101 | _impl->aux_mem_req.end(), |
| 102 | [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; }); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 103 | |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 104 | if(has_reshape != std::end(_impl->aux_mem_req)) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 105 | { |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 106 | _impl->weights->mark_as_unused(); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 107 | } |
Manuel Bottini | 29599d0 | 2021-07-06 15:01:35 +0100 | [diff] [blame^] | 108 | for(auto &ws : _impl->workspace_tensors) |
| 109 | { |
| 110 | const int slot = ws.first; |
| 111 | for(auto &m : _impl->aux_mem_req) |
| 112 | { |
| 113 | if(m.slot == slot && m.lifetime == MemoryLifetime::Prepare) |
| 114 | { |
| 115 | auto tensor = ws.second.get(); |
| 116 | tensor->allocator()->free(); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | _impl->is_prepared = true; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
Michalis Spyrou | e7be8a0 | 2019-12-12 16:16:09 +0000 | [diff] [blame] | 124 | } // namespace arm_compute |