blob: f63fcb02fddc6bc301c820e86206bdf6b2c3bee6 [file] [log] [blame]
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00001/*
Georgios Pinitas2ee98012021-02-15 20:42:39 +00002 * Copyright (c) 2017-2021 Arm Limited.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +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#include "arm_compute/runtime/NEON/functions/NEGEMMConvolutionLayer.h"
25
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000026#include "arm_compute/core/Size2D.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
Manuel Bottini29599d02021-07-06 15:01:35 +010029#include "arm_compute/runtime/Tensor.h"
30#include "src/core/helpers/MemoryHelpers.h"
31#include "src/runtime/cpu/operators/CpuGemmConvolution.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000032
Manuel Bottini29599d02021-07-06 15:01:35 +010033using namespace arm_compute::experimental;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000034
Michalis Spyroue7be8a02019-12-12 16:16:09 +000035namespace arm_compute
36{
Manuel Bottini29599d02021-07-06 15:01:35 +010037struct NEGEMMConvolutionLayer::Impl
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000038{
Manuel Bottini29599d02021-07-06 15:01:35 +010039 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 Spyrouebcebf12020-10-21 00:04:14 +010049
Michalis Spyrou1a569a32019-09-10 17:20:34 +010050NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager, IWeightsManager *weights_manager)
Manuel Bottini29599d02021-07-06 15:01:35 +010051 : _impl(std::make_unique<Impl>())
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000052{
Manuel Bottini29599d02021-07-06 15:01:35 +010053 _impl->weights_manager = weights_manager;
54 _impl->memory_group = MemoryGroup(memory_manager);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000055}
Manuel Bottini29599d02021-07-06 15:01:35 +010056NEGEMMConvolutionLayer::~NEGEMMConvolutionLayer() = default;
Gian Marco Iodice597a8562018-08-01 15:06:06 +010057
Alex Gilday7da29b62018-03-23 14:16:00 +000058void NEGEMMConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010059 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000060{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000061 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Manuel Bottini29599d02021-07-06 15:01:35 +010062 _impl->weights = weights;
63 _impl->op = std::make_unique<cpu::CpuGemmConvolution>();
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010064 _impl->op->configure(input->info(), weights->info(), (biases != nullptr ? biases->info() : nullptr), output->info(), conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000065
Manuel Bottini29599d02021-07-06 15:01:35 +010066 _impl->run_pack =
Gian Marco Iodicedb9d46d2018-08-08 12:29:38 +010067 {
Manuel Bottini29599d02021-07-06 15:01:35 +010068 { 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 Pinitase413d252018-11-14 18:29:58 +000074 {
Manuel Bottini29599d02021-07-06 15:01:35 +010075 { 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 Gottardi6acc6ad2018-02-02 17:19:18 +000080}
81
82Status NEGEMMConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010083 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000084{
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010085 return cpu::CpuGemmConvolution::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000086}
87
88void NEGEMMConvolutionLayer::run()
89{
Georgios Pinitas72219332018-06-05 14:56:06 +010090 prepare();
Manuel Bottini29599d02021-07-06 15:01:35 +010091 MemoryGroupResourceScope scope_mg(_impl->memory_group);
92 _impl->op->run(_impl->run_pack);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000093}
Georgios Pinitas72219332018-06-05 14:56:06 +010094
95void NEGEMMConvolutionLayer::prepare()
96{
Manuel Bottini29599d02021-07-06 15:01:35 +010097 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +010098 {
Manuel Bottini29599d02021-07-06 15:01:35 +010099 _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 Pinitas72219332018-06-05 14:56:06 +0100103
Manuel Bottini29599d02021-07-06 15:01:35 +0100104 if(has_reshape != std::end(_impl->aux_mem_req))
Georgios Pinitas72219332018-06-05 14:56:06 +0100105 {
Manuel Bottini29599d02021-07-06 15:01:35 +0100106 _impl->weights->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100107 }
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100108
109 // Release temporary tensors that are only used in prepare stage
110 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace_tensors);
Manuel Bottini29599d02021-07-06 15:01:35 +0100111 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100112 }
113}
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000114} // namespace arm_compute