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