blob: 44bfc6a51e114aec01b82cdab9871b54f2ab24ca [file] [log] [blame]
Gian Marco Iodiceab182122017-10-09 15:05:40 +01001/*
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +01002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Gian Marco Iodiceab182122017-10-09 15:05:40 +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/NEGEMMLowpMatrixMultiplyCore.h"
25
Gian Marco Iodiceab182122017-10-09 15:05:40 +010026#include "arm_compute/core/ITensor.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010027#include "arm_compute/core/Validate.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010028#include "arm_compute/runtime/IWeightsManager.h"
29#include "arm_compute/runtime/MemoryGroup.h"
Gian Marco Iodiceab182122017-10-09 15:05:40 +010030#include "arm_compute/runtime/NEON/NEScheduler.h"
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010031#include "arm_compute/runtime/Tensor.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010032
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010034#include "src/cpu/operators/CpuGemmLowpMatrixMultiplyCore.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010035
36using namespace arm_compute::experimental;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010037
Michele Di Giorgioa602f032020-03-12 19:34:33 +000038namespace arm_compute
39{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010040struct NEGEMMLowpMatrixMultiplyCore::Impl
41{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042 const ITensor *b{nullptr};
43 std::unique_ptr<cpu::CpuGemmLowpMatrixMultiplyCore> op{nullptr};
Manuel Bottinicfac51c2021-06-18 15:47:28 +010044 ITensorPack run_pack{};
45 ITensorPack prep_pack{};
46 MemoryGroup memory_group{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 IWeightsManager *weights_manager{nullptr};
Manuel Bottinicfac51c2021-06-18 15:47:28 +010048 MemoryRequirements aux_mem_req{};
49 WorkspaceData<Tensor> workspace_tensors{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 bool is_prepared{false};
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010051};
52
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager,
54 IWeightsManager *weights_manager)
Manuel Bottinicfac51c2021-06-18 15:47:28 +010055 : _impl(std::make_unique<Impl>())
Gian Marco Iodiceab182122017-10-09 15:05:40 +010056{
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010057 _impl->weights_manager = weights_manager;
Manuel Bottinicfac51c2021-06-18 15:47:28 +010058 _impl->memory_group = MemoryGroup(memory_manager);
Gian Marco Iodiceab182122017-10-09 15:05:40 +010059}
Manuel Bottinicfac51c2021-06-18 15:47:28 +010060NEGEMMLowpMatrixMultiplyCore::~NEGEMMLowpMatrixMultiplyCore() = default;
Gian Marco Iodiceab182122017-10-09 15:05:40 +010061
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062void NEGEMMLowpMatrixMultiplyCore::configure(
63 const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output, const GEMMInfo &gemm_info)
Gian Marco Iodiceab182122017-10-09 15:05:40 +010064{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000065 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010066
67 // Make the B matrix dynamic values.
68 auto b_info_to_use = b->info()->clone();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069 if (!gemm_info.reshape_b_only_on_first_run())
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010070 {
71 b_info_to_use->set_are_values_constant(false);
72 }
73
Manuel Bottinicfac51c2021-06-18 15:47:28 +010074 _impl->b = b;
75 _impl->op = std::make_unique<cpu::CpuGemmLowpMatrixMultiplyCore>();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 _impl->op->configure(a->info(), b_info_to_use.get(), (c != nullptr ? c->info() : nullptr), output->info(),
77 gemm_info);
78 _impl->run_pack = {{TensorType::ACL_SRC_0, a},
79 {TensorType::ACL_SRC_1, b},
80 {TensorType::ACL_SRC_2, c},
81 {TensorType::ACL_DST, output}};
82 _impl->prep_pack = {{TensorType::ACL_SRC_1, b}, {TensorType::ACL_SRC_2, c}};
83 _impl->aux_mem_req = _impl->op->workspace();
84 _impl->workspace_tensors =
85 manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Gian Marco Iodiceab182122017-10-09 15:05:40 +010086}
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088Status NEGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a,
89 const ITensorInfo *b,
90 const ITensorInfo *c,
91 const ITensorInfo *output,
92 const GEMMInfo &gemm_info)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000093{
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010094 // Make the B matrix dynamic values.
95 auto b_info_to_use = b->clone();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 if (!gemm_info.reshape_b_only_on_first_run())
Viet-Hoa Do9b0a6b42023-04-03 16:27:25 +010097 {
98 b_info_to_use->set_are_values_constant(false);
99 }
100
101 return cpu::CpuGemmLowpMatrixMultiplyCore::validate(a, b_info_to_use.get(), c, output, gemm_info);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000102}
103
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100104void NEGEMMLowpMatrixMultiplyCore::run()
105{
Georgios Pinitas72219332018-06-05 14:56:06 +0100106 prepare();
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100107 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100108 _impl->op->run(_impl->run_pack);
Georgios Pinitas72219332018-06-05 14:56:06 +0100109}
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100110
Georgios Pinitas72219332018-06-05 14:56:06 +0100111void NEGEMMLowpMatrixMultiplyCore::prepare()
112{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100113 if (!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100114 {
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100115 _impl->op->prepare(_impl->prep_pack);
116
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 auto has_reshape =
118 std::find_if(_impl->aux_mem_req.begin(), _impl->aux_mem_req.end(),
119 [](const MemoryInfo &m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100120
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 if (has_reshape != std::end(_impl->aux_mem_req))
Georgios Pinitas72219332018-06-05 14:56:06 +0100122 {
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100123 _impl->b->mark_as_unused();
124 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100125
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100126 // Release temporary tensors that are only used in prepare stage
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100127 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace_tensors);
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100128 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100129 }
Pablo Tello6ff12a02017-11-02 16:09:35 +0000130}
Michele Di Giorgioa602f032020-03-12 19:34:33 +0000131} // namespace arm_compute