ArmNN
 24.05
ClTensorHandleFactory.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 #include "ClTensorHandle.hpp"
8 
12 
13 #include <arm_compute/core/Coordinates.h>
14 #include <arm_compute/runtime/CL/CLSubTensor.h>
15 #include <arm_compute/runtime/CL/CLTensor.h>
16 
17 namespace armnn
18 {
19 
21 
22 std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateSubTensorHandle(ITensorHandle& parent,
23  const TensorShape& subTensorShape,
24  const unsigned int* subTensorOrigin) const
25 {
27  arm_compute::TensorShape shape = armcomputetensorutils::BuildArmComputeTensorShape(subTensorShape);
28 
29  coords.set_num_dimensions(subTensorShape.GetNumDimensions());
30  for (unsigned int i = 0; i < subTensorShape.GetNumDimensions(); ++i)
31  {
32  // Arm compute indexes tensor coords in reverse order.
33  unsigned int revertedIndex = subTensorShape.GetNumDimensions() - i - 1;
34  coords.set(i, armnn::numeric_cast<int>(subTensorOrigin[revertedIndex]));
35  }
36 
37  const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape());
38 
39  // In order for ACL to support subtensors the concat axis cannot be on x or y and the values of x and y
40  // must match the parent shapes
41  if (coords.x() != 0 || coords.y() != 0)
42  {
43  return nullptr;
44  }
45  if ((parentShape.x() != shape.x()) || (parentShape.y() != shape.y()))
46  {
47  return nullptr;
48  }
49 
50  if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape))
51  {
52  return nullptr;
53  }
54 
55  return std::make_unique<ClSubTensorHandle>(PolymorphicDowncast<IClTensorHandle*>(&parent), shape, coords);
56 }
57 
58 std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const
59 {
60  return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, true);
61 }
62 
63 std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
64  DataLayout dataLayout) const
65 {
66  return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true);
67 }
68 
69 std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
70  const bool IsMemoryManaged) const
71 {
72  std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo);
73  if (!IsMemoryManaged)
74  {
75  ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed.";
76  }
77  tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
78  return tensorHandle;
79 }
80 
81 std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
82  DataLayout dataLayout,
83  const bool IsMemoryManaged) const
84 {
85  std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout);
86  if (!IsMemoryManaged)
87  {
88  ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed.";
89  }
90  tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
91  return tensorHandle;
92 }
93 
95 {
96  static const FactoryId s_Id(ClTensorHandleFactoryId());
97  return s_Id;
98 }
99 
101 {
102  return GetIdStatic();
103 }
104 
106 {
107  return false;
108 }
109 
111 {
113 }
114 
116 {
118 }
119 
120 } // namespace armnn
armnn::ClTensorHandleFactoryId
constexpr const char * ClTensorHandleFactoryId()
Definition: ClTensorHandleFactory.hpp:15
armnn::ClTensorHandleFactory::SupportsSubTensors
bool SupportsSubTensors() const override
Definition: ClTensorHandleFactory.cpp:105
armnn::ClTensorHandleFactory::GetImportFlags
MemorySourceFlags GetImportFlags() const override
Definition: ClTensorHandleFactory.cpp:115
armnn::DataLayout
DataLayout
Definition: Types.hpp:62
armnn::ClTensorHandleFactory::GetIdStatic
static const FactoryId & GetIdStatic()
Definition: ClTensorHandleFactory.cpp:94
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::ClTensorHandleFactory::GetExportFlags
MemorySourceFlags GetExportFlags() const override
Definition: ClTensorHandleFactory.cpp:110
armnn::MemorySourceFlags
unsigned int MemorySourceFlags
Definition: MemorySources.hpp:15
ClTensorHandleFactory.hpp
armnn::ITensorHandle
Definition: ITensorHandle.hpp:16
armnn::ClTensorHandleFactory::CreateTensorHandle
std::unique_ptr< ITensorHandle > CreateTensorHandle(const TensorInfo &tensorInfo) const override
Definition: ClTensorHandleFactory.cpp:58
armnn::ITensorHandle::GetShape
virtual TensorShape GetShape() const =0
Get the number of elements for each dimension ordered from slowest iterating dimension to fastest ite...
armnn::Coordinates
std::array< unsigned int, MaxNumOfTensorDimensions > Coordinates
Definition: InternalTypes.hpp:15
armnn::ClTensorHandleFactory::CreateSubTensorHandle
std::unique_ptr< ITensorHandle > CreateSubTensorHandle(ITensorHandle &parent, const TensorShape &subTensorShape, const unsigned int *subTensorOrigin) const override
Definition: ClTensorHandleFactory.cpp:22
NumericCast.hpp
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
armnn::ClTensorHandleFactory::GetId
const FactoryId & GetId() const override
Definition: ClTensorHandleFactory.cpp:100
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::FactoryId
ITensorHandleFactory::FactoryId FactoryId
Definition: MockTensorHandleFactory.cpp:12
armnn::MemorySource::Undefined
@ Undefined
PolymorphicDowncast.hpp
ClTensorHandle.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ITensorHandleFactory::FactoryId
std::string FactoryId
Definition: ITensorHandleFactory.hpp:49
armnn::BoostLogSeverityMapping::warning
@ warning
IMemoryManager.hpp