Compute Library
 23.11
NEGEMMLowpMatrixMultiplyCore.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021, 2023 Arm Limited.
3  *
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  */
25 
32 
35 
36 using namespace arm_compute::experimental;
37 
38 namespace arm_compute
39 {
40 struct NEGEMMLowpMatrixMultiplyCore::Impl
41 {
42  const ITensor *b{nullptr};
43  std::unique_ptr<cpu::CpuGemmLowpMatrixMultiplyCore> op{nullptr};
44  ITensorPack run_pack{};
45  ITensorPack prep_pack{};
46  MemoryGroup memory_group{};
47  IWeightsManager *weights_manager{nullptr};
48  MemoryRequirements aux_mem_req{};
49  WorkspaceData<Tensor> workspace_tensors{};
50  bool is_prepared{false};
51 };
52 
53 NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager,
54  IWeightsManager *weights_manager)
55  : _impl(std::make_unique<Impl>())
56 {
57  _impl->weights_manager = weights_manager;
58  _impl->memory_group = MemoryGroup(memory_manager);
59 }
61 
63  const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output, const GEMMInfo &gemm_info)
64 {
65  ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
66 
67  // Make the B matrix dynamic values.
68  auto b_info_to_use = b->info()->clone();
69  if (!gemm_info.reshape_b_only_on_first_run())
70  {
71  b_info_to_use->set_are_values_constant(false);
72  }
73 
74  _impl->b = b;
75  _impl->op = std::make_unique<cpu::CpuGemmLowpMatrixMultiplyCore>();
76  _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},
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);
86 }
87 
89  const ITensorInfo *b,
90  const ITensorInfo *c,
91  const ITensorInfo *output,
92  const GEMMInfo &gemm_info)
93 {
94  // Make the B matrix dynamic values.
95  auto b_info_to_use = b->clone();
96  if (!gemm_info.reshape_b_only_on_first_run())
97  {
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);
102 }
103 
105 {
106  prepare();
107  MemoryGroupResourceScope scope_mg(_impl->memory_group);
108  _impl->op->run(_impl->run_pack);
109 }
110 
112 {
113  if (!_impl->is_prepared)
114  {
115  _impl->op->prepare(_impl->prep_pack);
116 
117  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; });
120 
121  if (has_reshape != std::end(_impl->aux_mem_req))
122  {
123  _impl->b->mark_as_unused();
124  }
125 
126  // Release temporary tensors that are only used in prepare stage
127  release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace_tensors);
128  _impl->is_prepared = true;
129  }
130 }
131 } // namespace arm_compute
arm_compute::experimental::MemoryRequirements
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:123
arm_compute::NEGEMMLowpMatrixMultiplyCore::run
void run() override
Run the kernels contained in the function.
Definition: NEGEMMLowpMatrixMultiplyCore.cpp:104
IWeightsManager.h
arm_compute::NEGEMMLowpMatrixMultiplyCore::validate
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration of NEGEMMLowpMatrixMultiply...
Definition: NEGEMMLowpMatrixMultiplyCore.cpp:88
arm_compute::NEGEMMLowpMatrixMultiplyCore::configure
void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *output, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel's inputs, output.
Definition: NEGEMMLowpMatrixMultiplyCore.cpp:62
arm_compute::cpu::CpuGemmLowpMatrixMultiplyCore::validate
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *dst, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration.
Definition: CpuGemmLowpMatrixMultiplyCore.cpp:326
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::GEMMInfo
GEMM information class.
Definition: GEMMInfo.h:69
NEGEMMLowpMatrixMultiplyCore.h
arm_compute::ACL_SRC_0
@ ACL_SRC_0
Definition: Types.h:45
arm_compute::ACL_SRC_1
@ ACL_SRC_1
Definition: Types.h:46
arm_compute::NEGEMMLowpMatrixMultiplyCore::NEGEMMLowpMatrixMultiplyCore
NEGEMMLowpMatrixMultiplyCore(std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
Constructor.
Definition: NEGEMMLowpMatrixMultiplyCore.cpp:53
CpuGemmLowpMatrixMultiplyCore.h
arm_compute::ACL_SRC_2
@ ACL_SRC_2
Definition: Types.h:47
MemoryGroup.h
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::experimental::MemoryInfo
Definition: Types.h:91
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
arm_compute::ACL_DST
@ ACL_DST
Definition: Types.h:55
arm_compute::GEMMInfo::reshape_b_only_on_first_run
bool reshape_b_only_on_first_run() const
Flag which specifies if the reshape of matrix B should executed only for the first.
Definition: GEMMInfo.h:163
arm_compute::Status
Status class.
Definition: Error.h:52
Tensor.h
MemoryHelpers.h
arm_compute::experimental
Definition: Types.h:83
NEScheduler.h
arm_compute::test::validation::b
SimpleTensor< float > b
Definition: DFT.cpp:157
arm_compute::MemoryGroupResourceScope
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::mlgo::parser::end
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:283
arm_compute::NEGEMMLowpMatrixMultiplyCore::~NEGEMMLowpMatrixMultiplyCore
~NEGEMMLowpMatrixMultiplyCore()
Default destructor.
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
ITensor.h
arm_compute::IWeightsManager
Weights manager interface to handle weights transformations.
Definition: IWeightsManager.h:36
Validate.h
arm_compute::MemoryGroup
Memory group.
Definition: MemoryGroup.h:42
arm_compute::NEGEMMLowpMatrixMultiplyCore::prepare
void prepare() override
Prepare the function for executing.
Definition: NEGEMMLowpMatrixMultiplyCore.cpp:111