Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 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 | |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 9 | #include <armnn/utility/NumericCast.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 10 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 11 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 12 | #include <arm_compute/core/Coordinates.h> |
| 13 | #include <arm_compute/runtime/CL/CLSubTensor.h> |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 14 | #include <arm_compute/runtime/CL/CLTensor.h> |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 15 | |
| 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | using FactoryId = ITensorHandleFactory::FactoryId; |
| 20 | |
| 21 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateSubTensorHandle(ITensorHandle& parent, |
| 22 | const TensorShape& subTensorShape, |
| 23 | const unsigned int* subTensorOrigin) const |
| 24 | { |
| 25 | arm_compute::Coordinates coords; |
| 26 | arm_compute::TensorShape shape = armcomputetensorutils::BuildArmComputeTensorShape(subTensorShape); |
| 27 | |
| 28 | coords.set_num_dimensions(subTensorShape.GetNumDimensions()); |
| 29 | for (unsigned int i = 0; i < subTensorShape.GetNumDimensions(); ++i) |
| 30 | { |
| 31 | // Arm compute indexes tensor coords in reverse order. |
| 32 | unsigned int revertedIndex = subTensorShape.GetNumDimensions() - i - 1; |
Matthew Sloyan | 171214c | 2020-09-09 09:07:37 +0100 | [diff] [blame] | 33 | coords.set(i, armnn::numeric_cast<int>(subTensorOrigin[revertedIndex])); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 36 | const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape()); |
David Monahan | 49895f4 | 2020-07-21 11:16:51 +0100 | [diff] [blame] | 37 | |
| 38 | // In order for ACL to support subtensors the concat axis cannot be on x or y and the values of x and y |
| 39 | // must match the parent shapes |
| 40 | if (coords.x() != 0 || coords.y() != 0) |
| 41 | { |
| 42 | return nullptr; |
| 43 | } |
| 44 | if ((parentShape.x() != shape.x()) || (parentShape.y() != shape.y())) |
| 45 | { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 49 | if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape)) |
| 50 | { |
| 51 | return nullptr; |
| 52 | } |
| 53 | |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 54 | return std::make_unique<ClSubTensorHandle>(PolymorphicDowncast<IClTensorHandle*>(&parent), shape, coords); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 55 | } |
| 56 | |
David Monahan | c6e5a6e | 2019-10-02 09:33:57 +0100 | [diff] [blame] | 57 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const |
| 58 | { |
| 59 | return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, true); |
| 60 | } |
| 61 | |
| 62 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 63 | DataLayout dataLayout) const |
| 64 | { |
| 65 | return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true); |
| 66 | } |
| 67 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 68 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 69 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 70 | { |
| 71 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo); |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 72 | if (!IsMemoryManaged) |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 73 | { |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 74 | ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed."; |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 75 | } |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 76 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 77 | return tensorHandle; |
| 78 | } |
| 79 | |
| 80 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 81 | DataLayout dataLayout, |
| 82 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 83 | { |
| 84 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout); |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 85 | if (!IsMemoryManaged) |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 86 | { |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 87 | ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed."; |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 88 | } |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 89 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 90 | return tensorHandle; |
| 91 | } |
| 92 | |
| 93 | const FactoryId& ClTensorHandleFactory::GetIdStatic() |
| 94 | { |
| 95 | static const FactoryId s_Id(ClTensorHandleFactoryId()); |
| 96 | return s_Id; |
| 97 | } |
| 98 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 99 | const FactoryId& ClTensorHandleFactory::GetId() const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 100 | { |
| 101 | return GetIdStatic(); |
| 102 | } |
| 103 | |
| 104 | bool ClTensorHandleFactory::SupportsSubTensors() const |
| 105 | { |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | MemorySourceFlags ClTensorHandleFactory::GetExportFlags() const |
| 110 | { |
| 111 | return m_ExportFlags; |
| 112 | } |
| 113 | |
| 114 | MemorySourceFlags ClTensorHandleFactory::GetImportFlags() const |
| 115 | { |
| 116 | return m_ImportFlags; |
| 117 | } |
| 118 | |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 119 | } // namespace armnn |