blob: be3ca5e05a1aaa7a79b71e5cf98d03295b432edf [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
Jan Eilerse9f0f0f2019-08-16 10:28:37 +01006#include "ClTensorHandleFactory.hpp"
7#include "ClTensorHandle.hpp"
8
Matthew Sloyan171214c2020-09-09 09:07:37 +01009#include <armnn/utility/NumericCast.hpp>
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/core/Coordinates.h>
13#include <arm_compute/runtime/CL/CLSubTensor.h>
Colm Donelanc74b1752021-03-12 15:58:48 +000014#include <arm_compute/runtime/CL/CLTensor.h>
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010015
16namespace armnn
17{
18
19using FactoryId = ITensorHandleFactory::FactoryId;
20
21std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateSubTensorHandle(ITensorHandle& parent,
22 const TensorShape& subTensorShape,
23 const unsigned int* subTensorOrigin) const
24{
25 arm_compute::Coordinates coords;
26 arm_compute::TensorShape shape = armcomputetensorutils::BuildArmComputeTensorShape(subTensorShape);
27
28 coords.set_num_dimensions(subTensorShape.GetNumDimensions());
29 for (unsigned int i = 0; i < subTensorShape.GetNumDimensions(); ++i)
30 {
31 // Arm compute indexes tensor coords in reverse order.
32 unsigned int revertedIndex = subTensorShape.GetNumDimensions() - i - 1;
Matthew Sloyan171214c2020-09-09 09:07:37 +010033 coords.set(i, armnn::numeric_cast<int>(subTensorOrigin[revertedIndex]));
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010034 }
35
Colm Donelanc74b1752021-03-12 15:58:48 +000036 const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape());
David Monahan49895f42020-07-21 11:16:51 +010037
38 // In order for ACL to support subtensors the concat axis cannot be on x or y and the values of x and y
39 // must match the parent shapes
40 if (coords.x() != 0 || coords.y() != 0)
41 {
42 return nullptr;
43 }
44 if ((parentShape.x() != shape.x()) || (parentShape.y() != shape.y()))
45 {
46 return nullptr;
47 }
48
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010049 if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape))
50 {
51 return nullptr;
52 }
53
Colm Donelanc74b1752021-03-12 15:58:48 +000054 return std::make_unique<ClSubTensorHandle>(PolymorphicDowncast<IClTensorHandle*>(&parent), shape, coords);
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010055}
56
David Monahanc6e5a6e2019-10-02 09:33:57 +010057std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const
58{
59 return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, true);
60}
61
62std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
63 DataLayout dataLayout) const
64{
65 return ClTensorHandleFactory::CreateTensorHandle(tensorInfo, dataLayout, true);
66}
67
David Monahan3fb7e102019-08-20 11:25:29 +010068std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
69 const bool IsMemoryManaged) const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010070{
71 std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo);
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000072 if (!IsMemoryManaged)
David Monahan3fb7e102019-08-20 11:25:29 +010073 {
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000074 ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed.";
David Monahan3fb7e102019-08-20 11:25:29 +010075 }
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000076 tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010077 return tensorHandle;
78}
79
80std::unique_ptr<ITensorHandle> ClTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
David Monahan3fb7e102019-08-20 11:25:29 +010081 DataLayout dataLayout,
82 const bool IsMemoryManaged) const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010083{
84 std::unique_ptr<ClTensorHandle> tensorHandle = std::make_unique<ClTensorHandle>(tensorInfo, dataLayout);
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000085 if (!IsMemoryManaged)
David Monahan3fb7e102019-08-20 11:25:29 +010086 {
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000087 ARMNN_LOG(warning) << "ClTensorHandleFactory only has support for memory managed.";
David Monahan3fb7e102019-08-20 11:25:29 +010088 }
Narumol Prangnawarat265e53e2020-10-30 16:06:55 +000089 tensorHandle->SetMemoryGroup(m_MemoryManager->GetInterLayerMemoryGroup());
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010090 return tensorHandle;
91}
92
93const FactoryId& ClTensorHandleFactory::GetIdStatic()
94{
95 static const FactoryId s_Id(ClTensorHandleFactoryId());
96 return s_Id;
97}
98
Ferran Balaguerbfeb2712019-08-07 15:14:56 +010099const FactoryId& ClTensorHandleFactory::GetId() const
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100100{
101 return GetIdStatic();
102}
103
104bool ClTensorHandleFactory::SupportsSubTensors() const
105{
Mike Kelly363b5722023-10-11 14:25:50 +0100106 return false;
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100107}
108
109MemorySourceFlags ClTensorHandleFactory::GetExportFlags() const
110{
Matthew Benthamb42a2b42022-11-28 13:43:10 +0000111 return MemorySourceFlags(MemorySource::Undefined);
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100112}
113
114MemorySourceFlags ClTensorHandleFactory::GetImportFlags() const
115{
Matthew Benthamb42a2b42022-11-28 13:43:10 +0000116 return MemorySourceFlags(MemorySource::Undefined);
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100117}
118
Matthew Benthamb42a2b42022-11-28 13:43:10 +0000119} // namespace armnn