blob: df99677fbbf411f7b08350b5295020c0a5ab9ab0 [file] [log] [blame]
Jan Eilerse9f0f0f2019-08-16 10:28:37 +01001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
Jan Eilerse9f0f0f2019-08-16 10:28:37 +01003// SPDX-License-Identifier: MIT
4//
5
Jan Eilerse9f0f0f2019-08-16 10:28:37 +01006#include "ClTensorHandleFactory.hpp"
7#include "ClTensorHandle.hpp"
8
Colm Donelanb4ef1632024-02-01 15:00:43 +00009#include <armnn/backends/IMemoryManager.hpp>
Matthew Sloyan171214c2020-09-09 09:07:37 +010010#include <armnn/utility/NumericCast.hpp>
Jan Eilersbb446e52020-04-02 13:56:54 +010011#include <armnn/utility/PolymorphicDowncast.hpp>
12
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010013#include <arm_compute/core/Coordinates.h>
14#include <arm_compute/runtime/CL/CLSubTensor.h>
Colm Donelanc74b1752021-03-12 15:58:48 +000015#include <arm_compute/runtime/CL/CLTensor.h>
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010016
17namespace armnn
18{
19
20using FactoryId = ITensorHandleFactory::FactoryId;
21
22std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateSubTensorHandle(ITensorHandle& parent,
23 const TensorShape& subTensorShape,
24 const unsigned int* subTensorOrigin) const
25{
26 arm_compute::Coordinates coords;
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;
Matthew Sloyan171214c2020-09-09 09:07:37 +010034 coords.set(i, armnn::numeric_cast<int>(subTensorOrigin[revertedIndex]));
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010035 }
36
Colm Donelanc74b1752021-03-12 15:58:48 +000037 const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape());
David Monahan49895f42020-07-21 11:16:51 +010038
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
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010050 if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape))
51 {
52 return nullptr;
53 }
54
Colm Donelanc74b1752021-03-12 15:58:48 +000055 return std::make_unique<ClSubTensorHandle>(PolymorphicDowncast<IClTensorHandle*>(&parent), shape, coords);
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010056}
57
David Monahanc6e5a6e2019-10-02 09:33:57 +010058std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const
59{
60 return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, true);
61}
62
63std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
64 DataLayout dataLayout) const
65{
66 return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true);
67}
68
David Monahan3fb7e102019-08-20 11:25:29 +010069std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
70 const bool IsMemoryManaged) const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010071{
72 std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo);
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000073 if (!IsMemoryManaged)
David Monahan3fb7e102019-08-20 11:25:29 +010074 {
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000075 ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed.";
David Monahan3fb7e102019-08-20 11:25:29 +010076 }
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000077 tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010078 return tensorHandle;
79}
80
81std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
David Monahan3fb7e102019-08-20 11:25:29 +010082 DataLayout dataLayout,
83 const bool IsMemoryManaged) const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010084{
85 std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout);
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000086 if (!IsMemoryManaged)
David Monahan3fb7e102019-08-20 11:25:29 +010087 {
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000088 ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed.";
David Monahan3fb7e102019-08-20 11:25:29 +010089 }
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000090 tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010091 return tensorHandle;
92}
93
94const FactoryId& ClTensorHandleFactory::GetIdStatic()
95{
96 static const FactoryId s_Id(ClTensorHandleFactoryId());
97 return s_Id;
98}
99
Ferran Balaguerbfeb2712019-08-07 15:14:56 +0100100const FactoryId& ClTensorHandleFactory::GetId() const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100101{
102 return GetIdStatic();
103}
104
105bool ClTensorHandleFactory::SupportsSubTensors() const
106{
Mike Kelly363b5722023-10-11 14:25:50 +0100107 return false;
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100108}
109
110MemorySourceFlags ClTensorHandleFactory::GetExportFlags() const
111{
Matthew Benthamb42a2b42022-11-28 13:43:10 +0000112 return MemorySourceFlags(MemorySource::Undefined);
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100113}
114
115MemorySourceFlags ClTensorHandleFactory::GetImportFlags() const
116{
Matthew Benthamb42a2b42022-11-28 13:43:10 +0000117 return MemorySourceFlags(MemorySource::Undefined);
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100118}
119
Matthew Benthamb42a2b42022-11-28 13:43:10 +0000120} // namespace armnn