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