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 | |
Narumol Prangnawarat | 1a26896 | 2020-07-27 15:52:13 +0100 | [diff] [blame] | 9 | #include "Layer.hpp" |
| 10 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 11 | #include <armnn/utility/IgnoreUnused.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 12 | #include <armnn/utility/PolymorphicDowncast.hpp> |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 13 | |
| 14 | namespace armnn |
| 15 | { |
| 16 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 17 | using FactoryId = ITensorHandleFactory::FactoryId; |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 18 | |
| 19 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateSubTensorHandle(ITensorHandle& parent, |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 20 | const TensorShape& subTensorShape, |
| 21 | const unsigned int* subTensorOrigin) |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 22 | const |
| 23 | { |
| 24 | const arm_compute::TensorShape shape = armcomputetensorutils::BuildArmComputeTensorShape(subTensorShape); |
| 25 | |
| 26 | arm_compute::Coordinates coords; |
| 27 | coords.set_num_dimensions(subTensorShape.GetNumDimensions()); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 28 | for (unsigned int i = 0; i < subTensorShape.GetNumDimensions(); ++i) |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 29 | { |
| 30 | // Arm compute indexes tensor coords in reverse order. |
| 31 | unsigned int revertedIndex = subTensorShape.GetNumDimensions() - i - 1; |
| 32 | coords.set(i, boost::numeric_cast<int>(subTensorOrigin[revertedIndex])); |
| 33 | } |
| 34 | |
| 35 | const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape()); |
David Monahan | 49895f4 | 2020-07-21 11:16:51 +0100 | [diff] [blame] | 36 | |
| 37 | // In order for ACL to support subtensors the concat axis cannot be on x or y and the values of x and y |
| 38 | // must match the parent shapes |
| 39 | if (coords.x() != 0 || coords.y() != 0) |
| 40 | { |
| 41 | return nullptr; |
| 42 | } |
| 43 | if ((parentShape.x() != shape.x()) || (parentShape.y() != shape.y())) |
| 44 | { |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 48 | if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape)) |
| 49 | { |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
| 53 | return std::make_unique<NeonSubTensorHandle>( |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 54 | PolymorphicDowncast<IAclTensorHandle*>(&parent), shape, coords); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 55 | } |
| 56 | |
David Monahan | c6e5a6e | 2019-10-02 09:33:57 +0100 | [diff] [blame] | 57 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const |
| 58 | { |
| 59 | return NeonTensorHandleFactory::CreateTensorHandle(tensorInfo, true); |
| 60 | } |
| 61 | |
| 62 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 63 | DataLayout dataLayout) const |
| 64 | { |
| 65 | return NeonTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true); |
| 66 | } |
| 67 | |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 68 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
| 69 | const bool IsMemoryManaged) const |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 70 | { |
| 71 | auto tensorHandle = std::make_unique<NeonTensorHandle>(tensorInfo); |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 72 | if (IsMemoryManaged) |
| 73 | { |
| 74 | tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup()); |
| 75 | } |
| 76 | // If we are not Managing the Memory then we must be importing |
| 77 | tensorHandle->SetImportEnabledFlag(!IsMemoryManaged); |
James Conroy | 57d10b7 | 2019-10-25 09:44:14 +0100 | [diff] [blame] | 78 | tensorHandle->SetImportFlags(GetImportFlags()); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 79 | |
| 80 | return tensorHandle; |
| 81 | } |
| 82 | |
| 83 | std::unique_ptr<ITensorHandle> NeonTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo, |
David Monahan | 3fb7e10 | 2019-08-20 11:25:29 +0100 | [diff] [blame] | 84 | DataLayout dataLayout, |
| 85 | const bool IsMemoryManaged) const |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 86 | { |
| 87 | auto tensorHandle = std::make_unique<NeonTensorHandle>(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 | } |
| 92 | // If we are not Managing the Memory then we must be importing |
| 93 | tensorHandle->SetImportEnabledFlag(!IsMemoryManaged); |
James Conroy | 57d10b7 | 2019-10-25 09:44:14 +0100 | [diff] [blame] | 94 | tensorHandle->SetImportFlags(GetImportFlags()); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 95 | |
| 96 | return tensorHandle; |
| 97 | } |
| 98 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 99 | const FactoryId& NeonTensorHandleFactory::GetIdStatic() |
| 100 | { |
| 101 | static const FactoryId s_Id(NeonTensorHandleFactoryId()); |
| 102 | return s_Id; |
| 103 | } |
| 104 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 105 | const FactoryId& NeonTensorHandleFactory::GetId() const |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 106 | { |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 107 | return GetIdStatic(); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool NeonTensorHandleFactory::SupportsSubTensors() const |
| 111 | { |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | MemorySourceFlags NeonTensorHandleFactory::GetExportFlags() const |
| 116 | { |
James Conroy | 57d10b7 | 2019-10-25 09:44:14 +0100 | [diff] [blame] | 117 | return 0; |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | MemorySourceFlags NeonTensorHandleFactory::GetImportFlags() const |
| 121 | { |
James Conroy | ffab16f | 2019-11-07 14:37:09 +0000 | [diff] [blame] | 122 | return 0; |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Narumol Prangnawarat | 1a26896 | 2020-07-27 15:52:13 +0100 | [diff] [blame] | 125 | std::vector<Capability> NeonTensorHandleFactory::GetCapabilities(const IConnectableLayer* layer, |
| 126 | const IConnectableLayer* connectedLayer, |
| 127 | CapabilityClass capabilityClass) |
| 128 | |
| 129 | { |
| 130 | IgnoreUnused(connectedLayer); |
| 131 | std::vector<Capability> capabilities; |
| 132 | if (capabilityClass == CapabilityClass::PaddingRequired) |
| 133 | { |
| 134 | auto search = paddingRequiredLayers.find((PolymorphicDowncast<const Layer*>(layer))->GetType()); |
| 135 | if ( search != paddingRequiredLayers.end()) |
| 136 | { |
| 137 | Capability paddingCapability(CapabilityClass::PaddingRequired, true); |
| 138 | capabilities.push_back(paddingCapability); |
| 139 | } |
| 140 | } |
| 141 | return capabilities; |
| 142 | } |
| 143 | |
Ferran Balaguer | bfeb271 | 2019-08-07 15:14:56 +0100 | [diff] [blame] | 144 | } // namespace armnn |