Compute Library
 23.08
NEDeviceBackend.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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 
36 
46 
47 namespace arm_compute
48 {
49 namespace graph
50 {
51 namespace backends
52 {
53 /** Register CPU backend */
54 static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON);
55 
57  : _allocator()
58 {
59 }
60 
62 {
63  //Nothing to do
64 }
65 
67 {
68  //Nothing to do
69  ARM_COMPUTE_UNUSED(ctx);
70 }
71 
73 {
74  // Set number of threads
75  if(ctx.config().num_threads >= 0)
76  {
78  }
79 
80  // Create function level memory manager
81  if(ctx.memory_management_ctx(Target::NEON) == nullptr)
82  {
83  MemoryManagerContext mm_ctx;
84  mm_ctx.target = Target::NEON;
87  mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
88  mm_ctx.allocator = &_allocator;
89 
90  ctx.insert_memory_management_ctx(std::move(mm_ctx));
91  }
92 
93  // Create function level weights manager
94  if(ctx.weights_management_ctx(Target::NEON) == nullptr)
95  {
96  WeightsManagerContext wm_ctx;
97  wm_ctx.target = Target::NEON;
98  wm_ctx.wm = create_weights_manager();
99 
100  ctx.insert_weights_management_ctx(std::move(wm_ctx));
101  }
102 }
103 
105 {
106  return true;
107 }
108 
110 {
111  return &_allocator;
112 }
113 
114 std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor)
115 {
116  // Get tensor descriptor
117  const TensorDescriptor &tensor_desc = tensor.desc();
118  ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON);
119 
120  // Create backend tensor handle
121  TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
122  info.set_data_layout(tensor_desc.layout);
123 
124  return std::make_unique<NETensorHandle>(info);
125 }
126 
127 std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
128 {
129  if(parent == nullptr)
130  {
131  return nullptr;
132  }
133 
134  return std::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent);
135 }
136 
137 std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx)
138 {
139  ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CPU node with ID : " << node.id() << std::endl);
141 
142  // Configure node
143  return NEFunctionFactory::create(&node, ctx);
144 }
145 
147 {
148  ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CPU node with ID : " << node.id() << std::endl);
150 
151  return NENodeValidator::validate(&node);
152 }
153 
154 std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
155 {
156  std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr;
157  if(affinity == MemoryManagerAffinity::Buffer)
158  {
159  lifetime_mgr = std::make_shared<BlobLifetimeManager>();
160  }
161  else
162  {
163  lifetime_mgr = std::make_shared<OffsetLifetimeManager>();
164  }
165  auto pool_mgr = std::make_shared<PoolManager>();
166  auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
167 
168  return mm;
169 }
170 
171 std::shared_ptr<arm_compute::IWeightsManager> NEDeviceBackend::create_weights_manager()
172 {
173  auto weights_mgr = std::make_shared<IWeightsManager>();
174  return weights_mgr;
175 }
176 
178 {
179  // nop
180 }
181 } // namespace backends
182 } // namespace graph
183 } // namespace arm_compute
arm_compute::graph::GraphContext::memory_management_ctx
MemoryManagerContext * memory_management_ctx(Target target)
Gets a memory manager context for a given target.
Definition: GraphContext.cpp:68
arm_compute::graph::MemoryManagerAffinity::Offset
@ Offset
Affinity at offset level.
arm_compute::graph::WeightsManagerContext
Contains structs required for weights management.
Definition: GraphContext.h:50
arm_compute::graph::GraphConfig::num_threads
int num_threads
Number of threads to use (thread capable backends), if 0 the backend will auto-initialize,...
Definition: Types.h:97
arm_compute::graph::GraphContext::config
const GraphConfig & config() const
Graph configuration accessor.
Definition: GraphContext.cpp:46
NETensorHandle.h
arm_compute::graph::backends::NEDeviceBackend::setup_backend_context
void setup_backend_context(GraphContext &ctx) override
Setups the given graph context.
Definition: NEDeviceBackend.cpp:72
IWeightsManager.h
arm_compute::graph::backends::NEDeviceBackend::release_backend_context
void release_backend_context(GraphContext &ctx) override
Release the backend specific resources associated to a given graph context.
Definition: NEDeviceBackend.cpp:66
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
NESubTensorHandle.h
arm_compute::graph::backends::NEDeviceBackend::initialize_backend
void initialize_backend() override
Initializes the backend.
Definition: NEDeviceBackend.cpp:61
arm_compute::graph::MemoryManagerAffinity::Buffer
@ Buffer
Affinity at buffer level.
arm_compute::graph::Target::NEON
@ NEON
Arm® Neon™ capable target device.
arm_compute::graph::MemoryManagerContext::target
Target target
Target.
Definition: GraphContext.h:42
arm_compute::graph::backends::NEDeviceBackend::create_memory_manager
std::shared_ptr< arm_compute::IMemoryManager > create_memory_manager(MemoryManagerAffinity affinity) override
Create a backend memory manager given its affinity.
Definition: NEDeviceBackend.cpp:154
arm_compute::IScheduler::set_num_threads
virtual void set_num_threads(unsigned int num_threads)=0
Sets the number of threads the scheduler will use to run the kernels.
INode.h
PoolManager.h
GraphContext.h
TensorInfo.h
arm_compute::graph::backends::NEDeviceBackend::sync
void sync() override
Synchronize kernels execution on the backend.
Definition: NEDeviceBackend.cpp:177
arm_compute::graph::MemoryManagerContext::cross_group
std::shared_ptr< arm_compute::IMemoryGroup > cross_group
Cross-function memory group.
Definition: GraphContext.h:45
arm_compute::graph::MemoryManagerContext
Contains structs required for memory management.
Definition: GraphContext.h:40
arm_compute::graph::Tensor
Tensor object.
Definition: Tensor.h:41
arm_compute::graph::GraphContext::weights_management_ctx
WeightsManagerContext * weights_management_ctx(Target target)
Gets a weights manager context for a given target.
Definition: GraphContext.cpp:92
arm_compute::graph::TensorDescriptor::quant_info
QuantizationInfo quant_info
Quantization info.
Definition: TensorDescriptor.h:112
ARM_COMPUTE_LOG_GRAPH_VERBOSE
#define ARM_COMPUTE_LOG_GRAPH_VERBOSE(x)
Definition: Logger.h:50
arm_compute::graph::backends::NEDeviceBackend::create_tensor
std::unique_ptr< ITensorHandle > create_tensor(const Tensor &tensor) override
Create a backend Tensor.
Definition: NEDeviceBackend.cpp:114
arm_compute::graph::MemoryManagerAffinity
MemoryManagerAffinity
Backend Memory Manager affinity.
Definition: Types.h:257
arm_compute::graph::GraphContext::insert_memory_management_ctx
bool insert_memory_management_ctx(MemoryManagerContext &&memory_ctx)
Inserts a memory manager context.
Definition: GraphContext.cpp:56
arm_compute::graph::backends::NEDeviceBackend::create_subtensor
std::unique_ptr< ITensorHandle > create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent) override
Create a backend Sub-Tensor.
Definition: NEDeviceBackend.cpp:127
MemoryGroup.h
arm_compute::test::validation::shape
shape
Definition: DFT.cpp:115
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:467
MemoryManagerOnDemand.h
arm_compute::graph::backends::NEDeviceBackend::validate_node
Status validate_node(INode &node) override
Validate a node.
Definition: NEDeviceBackend.cpp:146
arm_compute::graph::INode
Node interface.
Definition: INode.h:46
arm_compute::graph::TensorDescriptor::layout
DataLayout layout
Data layout.
Definition: TensorDescriptor.h:111
arm_compute::Scheduler::get
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94
arm_compute::IAllocator
Allocator interface.
Definition: IAllocator.h:35
arm_compute::Status
Status class.
Definition: Error.h:52
arm_compute::graph::GraphContext::insert_weights_management_ctx
bool insert_weights_management_ctx(WeightsManagerContext &&weights_ctx)
Inserts a weights manager context.
Definition: GraphContext.cpp:78
arm_compute::graph::TensorDescriptor::shape
TensorShape shape
Tensor shape.
Definition: TensorDescriptor.h:109
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
arm_compute::graph::backends::NEFunctionFactory::create
static std::unique_ptr< arm_compute::IFunction > create(INode *node, GraphContext &ctx)
Create a backend execution function depending on the node type.
Definition: NEFunctionFactory.cpp:121
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:66
Scheduler.h
arm_compute::graph::backends::NENodeValidator::validate
static Status validate(INode *node)
Validate a node.
Definition: NENodeValidator.cpp:57
arm_compute::graph::backends::NEDeviceBackend::configure_node
std::unique_ptr< arm_compute::IFunction > configure_node(INode &node, GraphContext &ctx) override
Configure a backend Node.
Definition: NEDeviceBackend.cpp:137
Logger.h
arm_compute::TensorInfo
Store the tensor's metadata.
Definition: TensorInfo.h:42
arm_compute::graph::MemoryManagerContext::cross_mm
std::shared_ptr< arm_compute::IMemoryManager > cross_mm
Cross-function memory manager.
Definition: GraphContext.h:44
arm_compute::graph::backends::NEDeviceBackend::backend_allocator
IAllocator * backend_allocator() override
Gets a backend memory allocator.
Definition: NEDeviceBackend.cpp:109
NEFunctionFactory.h
arm_compute::graph::backends::NEDeviceBackend::NEDeviceBackend
NEDeviceBackend()
Definition: NEDeviceBackend.cpp:56
arm_compute::graph::TensorDescriptor::target
Target target
Target.
Definition: TensorDescriptor.h:113
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::graph::backends::NEDeviceBackend::create_weights_manager
std::shared_ptr< arm_compute::IWeightsManager > create_weights_manager() override
Create a backend weights manager.
Definition: NEDeviceBackend.cpp:171
arm_compute::graph::TensorDescriptor
Tensor metadata class.
Definition: TensorDescriptor.h:38
Tensor.h
arm_compute::graph::backends::NEDeviceBackend::is_backend_supported
bool is_backend_supported() override
Checks if an instantiated backend is actually supported.
Definition: NEDeviceBackend.cpp:104
BackendRegistrar.h
arm_compute::graph::INode::assigned_target
Target assigned_target() const
Returns assigned target for this node.
Definition: INode.cpp:199
arm_compute::graph::WeightsManagerContext::wm
std::shared_ptr< arm_compute::IWeightsManager > wm
Weights manager.
Definition: GraphContext.h:53
arm_compute::graph::GraphContext
Graph context.
Definition: GraphContext.h:57
arm_compute::graph::MemoryManagerContext::intra_mm
std::shared_ptr< arm_compute::IMemoryManager > intra_mm
Intra-function memory manager.
Definition: GraphContext.h:43
arm_compute::graph::WeightsManagerContext::target
Target target
Target.
Definition: GraphContext.h:52
arm_compute::graph::TensorDescriptor::data_type
DataType data_type
Data type.
Definition: TensorDescriptor.h:110
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
arm_compute::graph::INode::id
NodeID id() const
Returns node's ID.
Definition: INode.cpp:103
NEDeviceBackend.h
arm_compute::graph::ITensorHandle
Tensor handle interface object.
Definition: ITensorHandle.h:38
arm_compute::graph::MemoryManagerContext::allocator
IAllocator * allocator
Backend allocator to use.
Definition: GraphContext.h:46
Allocator.h
BlobLifetimeManager.h
Graph.h
NENodeValidator.h
OffsetLifetimeManager.h