Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "NeonTensorHandleFactory.hpp" |
| 7 | #include "NeonTensorHandle.hpp" |
| 8 | |
| 9 | #include <boost/core/ignore_unused.hpp> |
| 10 | |
| 11 | namespace armnn |
| 12 | { |
| 13 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 14 | using FactoryId = ITensorHandleFactory::FactoryId; |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 15 | |
| 16 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateSubTensorHandle(ITensorHandle& parent, |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 17 | const TensorShape& subTensorShape, |
| 18 | const unsigned int* subTensorOrigin) |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 19 | const |
| 20 | { |
| 21 | const arm_compute::TensorShape shape = armcomputetensorutils::BuildArmComputeTensorShape(subTensorShape); |
| 22 | |
| 23 | arm_compute::Coordinates coords; |
| 24 | coords.set_num_dimensions(subTensorShape.GetNumDimensions()); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 25 | for (unsigned int i = 0; i < subTensorShape.GetNumDimensions(); ++i) |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 26 | { |
| 27 | // Arm compute indexes tensor coords in reverse order. |
| 28 | unsigned int revertedIndex = subTensorShape.GetNumDimensions() - i - 1; |
| 29 | coords.set(i, boost::numeric_cast<int>(subTensorOrigin[revertedIndex])); |
| 30 | } |
| 31 | |
| 32 | const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape()); |
| 33 | if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape)) |
| 34 | { |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | return std::make_unique<NeonSubTensorHandle>( |
| 39 | boost::polymorphic_downcast<IAclTensorHandle*>(&parent), shape, coords); |
| 40 | } |
| 41 | |
David Monahan | c6e5a6e | 2019-10-02 09:33:57 +0100 | [diff] [blame] | 42 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const |
| 43 | { |
| 44 | return NeonTensorHandleFactory::CreateTensorHandle(tensorInfo, true); |
| 45 | } |
| 46 | |
| 47 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 48 | DataLayout dataLayout) const |
| 49 | { |
| 50 | return NeonTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true); |
| 51 | } |
| 52 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 53 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 54 | const bool IsMemoryManaged) const |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 55 | { |
| 56 | auto tensorHandle = std::make_unique<NeonTensorHandle>(tensorInfo); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 57 | if (IsMemoryManaged) |
| 58 | { |
| 59 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
| 60 | } |
| 61 | // If we are not Managing the Memory then we must be importing |
| 62 | tensorHandle->SetImportEnabledFlag(!IsMemoryManaged); |
| 63 | tensorHandle->SetImportFlags(m_ImportFlags); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 64 | |
| 65 | return tensorHandle; |
| 66 | } |
| 67 | |
| 68 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 69 | DataLayout dataLayout, |
| 70 | const bool IsMemoryManaged) const |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 71 | { |
| 72 | auto tensorHandle = std::make_unique<NeonTensorHandle>(tensorInfo, dataLayout); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 73 | if (IsMemoryManaged) |
| 74 | { |
| 75 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
| 76 | } |
| 77 | // If we are not Managing the Memory then we must be importing |
| 78 | tensorHandle->SetImportEnabledFlag(!IsMemoryManaged); |
| 79 | tensorHandle->SetImportFlags(m_ImportFlags); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 80 | |
| 81 | return tensorHandle; |
| 82 | } |
| 83 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 84 | const FactoryId& NeonTensorHandleFactory::GetIdStatic() |
| 85 | { |
| 86 | static const FactoryId s_Id(NeonTensorHandleFactoryId()); |
| 87 | return s_Id; |
| 88 | } |
| 89 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 90 | const FactoryId& NeonTensorHandleFactory::GetId() const |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 91 | { |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 92 | return GetIdStatic(); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool NeonTensorHandleFactory::SupportsSubTensors() const |
| 96 | { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | MemorySourceFlags NeonTensorHandleFactory::GetExportFlags() const |
| 101 | { |
| 102 | return m_ExportFlags; |
| 103 | } |
| 104 | |
| 105 | MemorySourceFlags NeonTensorHandleFactory::GetImportFlags() const |
| 106 | { |
| 107 | return m_ImportFlags; |
| 108 | } |
| 109 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 110 | } // namespace armnn |