blob: 58ade9fb3ae3e2a05b71fb791ad7aa87f0e1ef0a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +01002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/NEGEMM.h"
25
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010026#include "arm_compute/core/ITensorPack.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Types.h"
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010029#include "arm_compute/runtime/MemoryGroup.h"
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010030#include "arm_compute/runtime/Tensor.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CPP/Validate.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010032#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010033#include "src/cpu/operators/CpuGemm.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010034
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010035using namespace arm_compute::experimental;
Giorgio Arenaa855af12018-07-16 17:20:38 +010036
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010037namespace arm_compute
38{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010039struct NEGEMM::Impl
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010041 MemoryGroup memory_group{};
42 IWeightsManager *weights_manager{ nullptr };
43
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010044 std::unique_ptr<cpu::CpuGemm> op{ nullptr };
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010045
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010046 const ITensor *original_b{ nullptr };
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010047 bool is_prepared{ false };
48
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010049 ITensorPack run_pack{};
50 ITensorPack prep_pack{};
51 WorkspaceData<Tensor> workspace{};
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010052 experimental::MemoryRequirements aux_mem_req{};
53};
54
55NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
56 : _impl(std::make_unique<Impl>())
57{
58 _impl->memory_group = MemoryGroup(std::move(memory_manager));
59 _impl->weights_manager = weights_manager;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060}
61
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000062NEGEMM::~NEGEMM() = default;
63
Gian Marco1d25ed52017-12-16 19:33:50 +000064void NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010066 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010067 ARM_COMPUTE_ERROR_THROW_ON(cpu::CpuGemm::validate(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068
Gian Marco1d25ed52017-12-16 19:33:50 +000069 // Check if we need to reshape the matrix B only on the first run
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010070 _impl->is_prepared = false;
71 _impl->original_b = b;
72 _impl->op = std::make_unique<cpu::CpuGemm>();
Gian Marco Iodice597a8562018-08-01 15:06:06 +010073
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010074 _impl->op->configure(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010075
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010076 _impl->aux_mem_req = _impl->op->workspace();
77 _impl->run_pack = { { ACL_SRC_0, a }, { ACL_SRC_1, b }, { ACL_SRC_2, c }, { ACL_DST, d } };
78 _impl->prep_pack = { { ACL_SRC_1, b }, { ACL_SRC_2, c } };
79 _impl->workspace = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080}
81
Giorgio Arenaa855af12018-07-16 17:20:38 +010082Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
83{
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010084 return cpu::CpuGemm::validate(a, b, c, output, alpha, beta, gemm_info);
Giorgio Arenaa855af12018-07-16 17:20:38 +010085}
86
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087void NEGEMM::run()
88{
Georgios Pinitas72219332018-06-05 14:56:06 +010089 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +010090
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010091 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010092 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093}
Georgios Pinitas72219332018-06-05 14:56:06 +010094
95void NEGEMM::prepare()
96{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010097 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +010098 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010099 _impl->op->prepare(_impl->prep_pack);
100
101 auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
102 _impl->aux_mem_req.end(),
103 [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
104
105 if(has_reshape != std::end(_impl->aux_mem_req))
Georgios Pinitas72219332018-06-05 14:56:06 +0100106 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100107 _impl->original_b->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100108 }
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100109 else
Georgios Pinitas72219332018-06-05 14:56:06 +0100110 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100111 _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->original_b);
Georgios Pinitas72219332018-06-05 14:56:06 +0100112 }
113
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100114 // Release temporary tensors that are only used in prepare stage
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100115 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100116 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100117 }
118}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100119} // namespace arm_compute