blob: 934a8250cccc81ec743fb58c65b4ec8b60346d26 [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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CPP/Validate.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010033#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/cpu/operators/CpuGemm.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010035
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010036using namespace arm_compute::experimental;
Giorgio Arenaa855af12018-07-16 17:20:38 +010037
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010038namespace arm_compute
39{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010040struct NEGEMM::Impl
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010042 MemoryGroup memory_group{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010043 IWeightsManager *weights_manager{nullptr};
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010044
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045 std::unique_ptr<cpu::CpuGemm> op{nullptr};
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010046
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 const ITensor *original_b{nullptr};
48 bool is_prepared{false};
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010049
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010050 ITensorPack run_pack{};
51 ITensorPack prep_pack{};
52 WorkspaceData<Tensor> workspace{};
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010053 experimental::MemoryRequirements aux_mem_req{};
54};
55
56NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
57 : _impl(std::make_unique<Impl>())
58{
59 _impl->memory_group = MemoryGroup(std::move(memory_manager));
60 _impl->weights_manager = weights_manager;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061}
62
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000063NEGEMM::~NEGEMM() = default;
64
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065void NEGEMM::configure(const ITensor *a,
66 const ITensor *b,
67 const ITensor *c,
68 ITensor *d,
69 float alpha,
70 float beta,
71 const GEMMInfo &gemm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072{
Michele Di Giorgio93b75e02021-06-21 12:00:43 +010073 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 ARM_COMPUTE_ERROR_THROW_ON(cpu::CpuGemm::validate(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr,
75 d->info(), alpha, beta, gemm_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
Gian Marco1d25ed52017-12-16 19:33:50 +000077 // Check if we need to reshape the matrix B only on the first run
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010078 _impl->is_prepared = false;
79 _impl->original_b = b;
80 _impl->op = std::make_unique<cpu::CpuGemm>();
Gian Marco Iodice597a8562018-08-01 15:06:06 +010081
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010082 // Make the B matrix dynamic values.
83 auto b_info_to_use = b->info()->clone();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010084 if (!gemm_info.reshape_b_only_on_first_run())
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010085 {
86 b_info_to_use->set_are_values_constant(false);
87 }
88
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 _impl->op->configure(a->info(), b_info_to_use.get(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta,
90 gemm_info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010091
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +010092 _impl->aux_mem_req = _impl->op->workspace();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093 _impl->run_pack = {{ACL_SRC_0, a}, {ACL_SRC_1, b}, {ACL_SRC_2, c}, {ACL_DST, d}};
94 _impl->prep_pack = {{ACL_SRC_1, b}, {ACL_SRC_2, c}};
95 _impl->workspace =
96 manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097}
98
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099Status NEGEMM::validate(const ITensorInfo *a,
100 const ITensorInfo *b,
101 const ITensorInfo *c,
102 const ITensorInfo *output,
103 float alpha,
104 float beta,
105 const GEMMInfo &gemm_info)
Giorgio Arenaa855af12018-07-16 17:20:38 +0100106{
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100107 // Make the B matrix dynamic values.
108 auto b_to_use = b->clone();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100109 if (!gemm_info.reshape_b_only_on_first_run())
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +0100110 {
111 b_to_use->set_are_values_constant(false);
112 }
113
114 return cpu::CpuGemm::validate(a, b_to_use.get(), c, output, alpha, beta, gemm_info);
Giorgio Arenaa855af12018-07-16 17:20:38 +0100115}
116
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117Status NEGEMM::has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
118 const ITensorInfo *a,
119 const ITensorInfo *b,
120 const ITensorInfo *c,
121 const ITensorInfo *output,
122 float alpha,
123 float beta,
124 const GEMMInfo &gemm_info)
Milos Puzovic13b623e2022-07-27 17:53:21 +0000125{
126 ARM_COMPUTE_UNUSED(alpha, beta);
127 return cpu::CpuGemm::has_opt_impl(expected_weight_format, a, b, c, output, gemm_info);
128}
129
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130void NEGEMM::run()
131{
Georgios Pinitas72219332018-06-05 14:56:06 +0100132 prepare();
Georgios Pinitas658039b2017-09-15 16:30:50 +0100133
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100134 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100135 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136}
Georgios Pinitas72219332018-06-05 14:56:06 +0100137
138void NEGEMM::prepare()
139{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 if (!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100141 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100142 _impl->op->prepare(_impl->prep_pack);
143
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 auto has_reshape =
145 std::find_if(_impl->aux_mem_req.begin(), _impl->aux_mem_req.end(),
146 [](const MemoryInfo &m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100147
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (has_reshape != std::end(_impl->aux_mem_req))
Georgios Pinitas72219332018-06-05 14:56:06 +0100149 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100150 _impl->original_b->mark_as_unused();
Georgios Pinitas72219332018-06-05 14:56:06 +0100151 }
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100152 else
Georgios Pinitas72219332018-06-05 14:56:06 +0100153 {
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100154 _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->original_b);
Georgios Pinitas72219332018-06-05 14:56:06 +0100155 }
156
Michele Di Giorgio4dfc5532021-06-30 12:05:34 +0100157 // Release temporary tensors that are only used in prepare stage
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100158 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
Michele Di Giorgio93b75e02021-06-21 12:00:43 +0100159 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100160 }
161}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100162} // namespace arm_compute