blob: 8af97f41e2a50a1cf3e72feac54efd69d31f7caf [file] [log] [blame]
Jan Eilerse9f0f0f2019-08-16 10:28:37 +01001//
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 Eilersbb446e52020-04-02 13:56:54 +010010#include <armnn/utility/PolymorphicDowncast.hpp>
11
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010012#include <arm_compute/runtime/CL/CLTensor.h>
13#include <arm_compute/core/Coordinates.h>
14#include <arm_compute/runtime/CL/CLSubTensor.h>
15
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010016
17namespace armnn
18{
19
20using FactoryId = ITensorHandleFactory::FactoryId;
21
22std::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>(
Jan Eilersbb446e52020-04-02 13:56:54 +010045 PolymorphicDowncast<IClTensorHandle *>(&parent), shape, coords);
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010046}
47
David Monahanc6e5a6e2019-10-02 09:33:57 +010048std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const
49{
50 return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, true);
51}
52
53std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
54 DataLayout dataLayout) const
55{
56 return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true);
57}
58
David Monahan3fb7e102019-08-20 11:25:29 +010059std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
60 const bool IsMemoryManaged) const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010061{
62 std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo);
David Monahan3fb7e102019-08-20 11:25:29 +010063 if (IsMemoryManaged)
64 {
65 tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
66 }
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010067 return tensorHandle;
68}
69
70std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
David Monahan3fb7e102019-08-20 11:25:29 +010071 DataLayout dataLayout,
72 const bool IsMemoryManaged) const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010073{
74 std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout);
David Monahan3fb7e102019-08-20 11:25:29 +010075 if (IsMemoryManaged)
76 {
77 tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
78 }
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010079 return tensorHandle;
80}
81
82const FactoryId& ClTensorHandleFactory::GetIdStatic()
83{
84 static const FactoryId s_Id(ClTensorHandleFactoryId());
85 return s_Id;
86}
87
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010088const FactoryId& ClTensorHandleFactory::GetId() const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010089{
90 return GetIdStatic();
91}
92
93bool ClTensorHandleFactory::SupportsSubTensors() const
94{
95 return true;
96}
97
98MemorySourceFlags ClTensorHandleFactory::GetExportFlags() const
99{
100 return m_ExportFlags;
101}
102
103MemorySourceFlags ClTensorHandleFactory::GetImportFlags() const
104{
105 return m_ImportFlags;
106}
107
108} // namespace armnn