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 | |
| 10 | #include <arm_compute/runtime/CL/CLTensor.h> |
| 11 | #include <arm_compute/core/Coordinates.h> |
| 12 | #include <arm_compute/runtime/CL/CLSubTensor.h> |
| 13 | |
| 14 | #include <boost/polymorphic_cast.hpp> |
| 15 | |
| 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()); |
| 39 | if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape)) |
| 40 | { |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
| 44 | return std::make_unique<ClSubTensorHandle>( |
| 45 | boost::polymorphic_downcast<IClTensorHandle *>(&parent), shape, coords); |
| 46 | } |
| 47 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame^] | 48 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 49 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 50 | { |
| 51 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame^] | 52 | if (IsMemoryManaged) |
| 53 | { |
| 54 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
| 55 | } |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 56 | return tensorHandle; |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame^] | 60 | DataLayout dataLayout, |
| 61 | const bool IsMemoryManaged) const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 62 | { |
| 63 | std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame^] | 64 | if (IsMemoryManaged) |
| 65 | { |
| 66 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
| 67 | } |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 68 | return tensorHandle; |
| 69 | } |
| 70 | |
| 71 | const FactoryId& ClTensorHandleFactory::GetIdStatic() |
| 72 | { |
| 73 | static const FactoryId s_Id(ClTensorHandleFactoryId()); |
| 74 | return s_Id; |
| 75 | } |
| 76 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 77 | const FactoryId& ClTensorHandleFactory::GetId() const |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 78 | { |
| 79 | return GetIdStatic(); |
| 80 | } |
| 81 | |
| 82 | bool ClTensorHandleFactory::SupportsSubTensors() const |
| 83 | { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | MemorySourceFlags ClTensorHandleFactory::GetExportFlags() const |
| 88 | { |
| 89 | return m_ExportFlags; |
| 90 | } |
| 91 | |
| 92 | MemorySourceFlags ClTensorHandleFactory::GetImportFlags() const |
| 93 | { |
| 94 | return m_ImportFlags; |
| 95 | } |
| 96 | |
| 97 | } // namespace armnn |