blob: e51f2f9eb609873fbac41edc3fed1757c29d2ef6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +01002 * Copyright (c) 2017-2023 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
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010074 // Make the B matrix dynamic values.
75 auto b_info_to_use = b->info()->clone();
76 if(!gemm_info.reshape_b_only_on_first_run())
77 {
78 b_info_to_use->set_are_values_constant(false);
79 }
80
81 _impl->op->configure(a->info(), b_info_to_use.get(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010082
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010083 _impl->aux_mem_req = _impl->op->workspace();
84 _impl->run_pack = { { ACL_SRC_0, a }, { ACL_SRC_1, b }, { ACL_SRC_2, c }, { ACL_DST, d } };
85 _impl->prep_pack = { { ACL_SRC_1, b }, { ACL_SRC_2, c } };
86 _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 +010087}
88
Giorgio Arenaa855af12018-07-16 17:20:38 +010089Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
90{
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010091 // Make the B matrix dynamic values.
92 auto b_to_use = b->clone();
93 if(!gemm_info.reshape_b_only_on_first_run())
94 {
95 b_to_use->set_are_values_constant(false);
96 }
97
98 return cpu::CpuGemm::validate(a, b_to_use.get(), c, output, alpha, beta, gemm_info);
Giorgio Arenaa855af12018-07-16 17:20:38 +010099}
100
Milos Puzovic13b623e2022-07-27 17:53:21 +0000101Status NEGEMM::has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output,
102 float alpha, float beta, const GEMMInfo &gemm_info)
103{
104 ARM_COMPUTE_UNUSED(alpha, beta);
105 return cpu::CpuGemm::has_opt_impl(expected_weight_format, a, b, c, output, gemm_info);
106}
107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108void NEGEMM::run()
109{
Georgios Pinitas72219332018-06-05 14:56:06 +0100110 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100111
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100112 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100113 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114}
Georgios Pinitas72219332018-06-05 14:56:06 +0100115
116void NEGEMM::prepare()
117{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100118 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100119 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100120 _impl->op->prepare(_impl->prep_pack);
121
122 auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
123 _impl->aux_mem_req.end(),
124 [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
125
126 if(has_reshape != std::end(_impl->aux_mem_req))
Georgios Pinitas72219332018-06-05 14:56:06 +0100127 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100128 _impl->original_b->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100129 }
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100130 else
Georgios Pinitas72219332018-06-05 14:56:06 +0100131 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100132 _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->original_b);
Georgios Pinitas72219332018-06-05 14:56:06 +0100133 }
134
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100135 // Release temporary tensors that are only used in prepare stage
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100136 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100137 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100138 }
139}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100140} // namespace arm_compute