blob: 42b8b704053987588a0fff9892c3d996d511e862 [file] [log] [blame]
Georgios Pinitasc0b6f762020-11-02 01:37:17 +00001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2020-2021 Arm Limited.
Georgios Pinitasc0b6f762020-11-02 01:37:17 +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/NEGEMMConv2d.h"
Georgios Pinitasec2256b2020-12-03 18:51:58 +000025
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000026#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010027#include "arm_compute/runtime/Tensor.h"
28#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010029#include "src/cpu/operators/CpuGemmDirectConv2d.h"
Georgios Pinitas40f51a62020-11-21 03:04:18 +000030
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000031namespace arm_compute
32{
Michele Di Giorgio8ae3cda2021-06-07 15:30:26 +010033using OperatorType = cpu::CpuGemmDirectConv2d;
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010034using namespace arm_compute::experimental;
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010035
36struct NEGEMMConv2d::Impl
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000037{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010038 const ITensor *weights{ nullptr };
39 std::unique_ptr<OperatorType> op{ nullptr };
40 ITensorPack run_pack{};
41 ITensorPack prep_pack{};
42 WorkspaceData<Tensor> workspace{};
43 MemoryGroup memory_group{};
44 bool is_prepared{ false };
45 experimental::MemoryRequirements aux_mem_req{};
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010046};
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000047
48NEGEMMConv2d::NEGEMMConv2d(const std::shared_ptr<IMemoryManager> &memory_manager)
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010049 : _impl(std::make_unique<Impl>())
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000050{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010051 _impl->memory_group = MemoryGroup(memory_manager);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000052}
Georgios Pinitasec2256b2020-12-03 18:51:58 +000053
54NEGEMMConv2d::~NEGEMMConv2d() = default;
55
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000056void NEGEMMConv2d::configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const Conv2dInfo &info)
57{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010058 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
59
60 _impl->weights = weights;
61 _impl->is_prepared = false;
62 _impl->op = std::make_unique<OperatorType>();
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000063
Pablo Telloa5c428a2021-06-09 11:50:01 +010064 _impl->op->configure(input->info(), weights->info(), biases != nullptr ? biases->info() : nullptr, output->info(), info);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010065
66 _impl->aux_mem_req = _impl->op->workspace();
67 _impl->run_pack = { { TensorType::ACL_SRC_0, input }, { TensorType::ACL_SRC_2, biases }, { TensorType::ACL_DST, output } };
68 _impl->prep_pack = { { TensorType::ACL_SRC_1, weights }, { TensorType::ACL_SRC_2, biases } };
69 _impl->workspace = manage_workspace<Tensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000070}
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010071
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000072Status NEGEMMConv2d::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &info)
73{
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010074 return OperatorType::validate(input, weights, biases, output, info);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000075}
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010076
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000077void NEGEMMConv2d::run()
78{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010079 prepare();
80
81 MemoryGroupResourceScope scope_mg(_impl->memory_group);
82 _impl->op->run(_impl->run_pack);
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000083}
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010084
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000085void NEGEMMConv2d::prepare()
86{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010087 if(!_impl->is_prepared)
88 {
89 _impl->op->prepare(_impl->prep_pack);
90
91 auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
92 _impl->aux_mem_req.end(),
93 [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
94
95 if(has_reshape != std::end(_impl->aux_mem_req))
96 {
97 _impl->weights->mark_as_unused();
98 }
99 else
100 {
101 _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->weights);
102 }
103
104 // Release temporary tensors that are only used in prepare stage
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100105 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100106 _impl->is_prepared = true;
107 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000108}
109} // namespace arm_compute