blob: 13635c6e34156fc9fc2a1b99da4159a8f6bd4535 [file] [log] [blame]
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00001/*
Francesco Petrogalli553f6952022-06-30 10:22:01 +00002 * Copyright (c) 2017-2022 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"
Georgios Pinitas7891a732021-08-20 21:39:25 +010031#include "src/cpu/operators/CpuGemmConv2d.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{
Georgios Pinitas19884632021-08-16 12:38:54 +010039 const ITensor *weights{ nullptr };
40 std::unique_ptr<cpu::CpuGemmConv2d> op{ nullptr };
41 ITensorPack run_pack{};
42 MemoryGroup memory_group{};
43 IWeightsManager *weights_manager{ nullptr };
44 MemoryRequirements aux_mem_req{};
45 WorkspaceData<Tensor> workspace_tensors{};
46 bool is_prepared{ false };
Manuel Bottini29599d02021-07-06 15:01:35 +010047};
Michalis Spyrouebcebf12020-10-21 00:04:14 +010048
Michalis Spyrou1a569a32019-09-10 17:20:34 +010049NEGEMMConvolutionLayer::NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager, IWeightsManager *weights_manager)
Manuel Bottini29599d02021-07-06 15:01:35 +010050 : _impl(std::make_unique<Impl>())
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000051{
Manuel Bottini29599d02021-07-06 15:01:35 +010052 _impl->weights_manager = weights_manager;
53 _impl->memory_group = MemoryGroup(memory_manager);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000054}
Manuel Bottini29599d02021-07-06 15:01:35 +010055NEGEMMConvolutionLayer::~NEGEMMConvolutionLayer() = default;
Gian Marco Iodice597a8562018-08-01 15:06:06 +010056
Alex Gilday7da29b62018-03-23 14:16:00 +000057void 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 +010058 const Size2D &dilation, const ActivationLayerInfo &act_info, bool enable_fast_math, unsigned int num_groups)
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000059{
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000060 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Francesco Petrogalli553f6952022-06-30 10:22:01 +000061
Manuel Bottini29599d02021-07-06 15:01:35 +010062 _impl->weights = weights;
Georgios Pinitas19884632021-08-16 12:38:54 +010063 _impl->op = std::make_unique<cpu::CpuGemmConv2d>();
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 };
Manuel Bottini29599d02021-07-06 15:01:35 +010073 _impl->aux_mem_req = _impl->op->workspace();
Georgios Pinitasd4a5bc52021-08-12 07:42:51 +010074 _impl->workspace_tensors = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->run_pack);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000075}
76
77Status 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 +010078 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 +000079{
Georgios Pinitas19884632021-08-16 12:38:54 +010080 return cpu::CpuGemmConv2d::validate(input, weights, biases, output, conv_info, weights_info, dilation, act_info, enable_fast_math, num_groups);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000081}
82
Francesco Petrogalli553f6952022-06-30 10:22:01 +000083Status NEGEMMConvolutionLayer::has_opt_impl(arm_gemm::WeightFormat &expected_weight_format, const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
84 const PadStrideInfo &conv_info,
85 const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, const bool enable_fast_math)
86{
87 return cpu::CpuGemmConv2d::has_opt_impl(expected_weight_format, src, weights, biases, dst, conv_info, weights_info, dilation, act_info, enable_fast_math);
88}
89
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000090void NEGEMMConvolutionLayer::run()
91{
Georgios Pinitas72219332018-06-05 14:56:06 +010092 prepare();
Manuel Bottini29599d02021-07-06 15:01:35 +010093 MemoryGroupResourceScope scope_mg(_impl->memory_group);
94 _impl->op->run(_impl->run_pack);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000095}
Georgios Pinitas72219332018-06-05 14:56:06 +010096
97void NEGEMMConvolutionLayer::prepare()
98{
Manuel Bottini29599d02021-07-06 15:01:35 +010099 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100100 {
Georgios Pinitasd4a5bc52021-08-12 07:42:51 +0100101 _impl->op->prepare(_impl->run_pack);
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100102
103 // Release temporary tensors that are only used in prepare stage
104 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace_tensors);
Manuel Bottini29599d02021-07-06 15:01:35 +0100105 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100106 }
107}
Michalis Spyroue7be8a02019-12-12 16:16:09 +0000108} // namespace arm_compute