Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 1 | // |
Colm Donelan | b4ef163 | 2024-02-01 15:00:43 +0000 | [diff] [blame^] | 2 | // Copyright © 2017, 2024 Arm Ltd. All rights reserved. |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 6 | #include "ClTensorHandleFactory.hpp" |
| 7 | #include "ClTensorHandle.hpp" |
| 8 | |
Colm Donelan | b4ef163 | 2024-02-01 15:00:43 +0000 | [diff] [blame^] | 9 | #include <armnn/backends/IMemoryManager.hpp> |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 10 | #include <armnn/utility/NumericCast.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 11 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 12 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 13 | #include <arm_compute/core/Coordinates.h> |
| 14 | #include <arm_compute/runtime/CL/CLSubTensor.h> |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 15 | #include <arm_compute/runtime/CL/CLTensor.h> |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | using FactoryId = ITensorHandleFactory::FactoryId; |
| 21 | |
| 22 | std::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 Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 34 | coords.set(i, armnn::numeric_cast<int>(subTensorOrigin[revertedIndex])); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 37 | const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape()); |
David Monahan | 49895f4 | 2020-07-21 11:16:51 +0100 | [diff] [blame] | 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 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 50 | if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape)) |
| 51 | { |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 55 | return std::make_unique<ClSubTensorHandle>(PolymorphicDowncast<IClTensorHandle*>(&parent), shape, coords); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 56 | } |
| 57 | |
David Monahan | c6e5a6e | 2019-10-02 09:33:57 +0100 | [diff] [blame] | 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 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 69 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 70 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 71 | { |
| 72 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo); |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 73 | if (!IsMemoryManaged) |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 74 | { |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 75 | ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed."; |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 76 | } |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 77 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 78 | return tensorHandle; |
| 79 | } |
| 80 | |
| 81 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 82 | DataLayout dataLayout, |
| 83 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 84 | { |
| 85 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout); |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 86 | if (!IsMemoryManaged) |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 87 | { |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 88 | ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed."; |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 89 | } |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 90 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 91 | return tensorHandle; |
| 92 | } |
| 93 | |
| 94 | const FactoryId& ClTensorHandleFactory::GetIdStatic() |
| 95 | { |
| 96 | static const FactoryId s_Id(ClTensorHandleFactoryId()); |
| 97 | return s_Id; |
| 98 | } |
| 99 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 100 | const FactoryId& ClTensorHandleFactory::GetId() const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 101 | { |
| 102 | return GetIdStatic(); |
| 103 | } |
| 104 | |
| 105 | bool ClTensorHandleFactory::SupportsSubTensors() const |
| 106 | { |
Mike Kelly | 363b572 | 2023-10-11 14:25:50 +0100 | [diff] [blame] | 107 | return false; |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | MemorySourceFlags ClTensorHandleFactory::GetExportFlags() const |
| 111 | { |
Matthew Bentham | b42a2b4 | 2022-11-28 13:43:10 +0000 | [diff] [blame] | 112 | return MemorySourceFlags(MemorySource::Undefined); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | MemorySourceFlags ClTensorHandleFactory::GetImportFlags() const |
| 116 | { |
Matthew Bentham | b42a2b4 | 2022-11-28 13:43:10 +0000 | [diff] [blame] | 117 | return MemorySourceFlags(MemorySource::Undefined); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Matthew Bentham | b42a2b4 | 2022-11-28 13:43:10 +0000 | [diff] [blame] | 120 | } // namespace armnn |