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 | |
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/runtime/CL/CLTensor.h> |
| 13 | #include <arm_compute/core/Coordinates.h> |
| 14 | #include <arm_compute/runtime/CL/CLSubTensor.h> |
| 15 | |
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; |
| 34 | coords.set(i, boost::numeric_cast<int>(subTensorOrigin[revertedIndex])); |
| 35 | } |
| 36 | |
| 37 | const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape( |
| 38 | parent.GetShape()); |
David Monahan | 49895f4 | 2020-07-21 11:16:51 +0100 | [diff] [blame^] | 39 | |
| 40 | // In order for ACL to support subtensors the concat axis cannot be on x or y and the values of x and y |
| 41 | // must match the parent shapes |
| 42 | if (coords.x() != 0 || coords.y() != 0) |
| 43 | { |
| 44 | return nullptr; |
| 45 | } |
| 46 | if ((parentShape.x() != shape.x()) || (parentShape.y() != shape.y())) |
| 47 | { |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 51 | if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape)) |
| 52 | { |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | return std::make_unique<ClSubTensorHandle>( |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 57 | PolymorphicDowncast<IClTensorHandle *>(&parent), shape, coords); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 58 | } |
| 59 | |
David Monahan | c6e5a6e | 2019-10-02 09:33:57 +0100 | [diff] [blame] | 60 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const |
| 61 | { |
| 62 | return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, true); |
| 63 | } |
| 64 | |
| 65 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 66 | DataLayout dataLayout) const |
| 67 | { |
| 68 | return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true); |
| 69 | } |
| 70 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 71 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 72 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 73 | { |
| 74 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 75 | if (IsMemoryManaged) |
| 76 | { |
| 77 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
| 78 | } |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 79 | return tensorHandle; |
| 80 | } |
| 81 | |
| 82 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 83 | DataLayout dataLayout, |
| 84 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 85 | { |
| 86 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 87 | if (IsMemoryManaged) |
| 88 | { |
| 89 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
| 90 | } |
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 | { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | MemorySourceFlags ClTensorHandleFactory::GetExportFlags() const |
| 111 | { |
| 112 | return m_ExportFlags; |
| 113 | } |
| 114 | |
| 115 | MemorySourceFlags ClTensorHandleFactory::GetImportFlags() const |
| 116 | { |
| 117 | return m_ImportFlags; |
| 118 | } |
| 119 | |
| 120 | } // namespace armnn |