aboutsummaryrefslogtreecommitdiff
path: root/src/backends/cl/ClImportTensorHandleFactory.cpp
blob: 26d5f9c47a8ae6f61b97fee6bf610a0e8116af65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Copyright © 2021 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ClImportTensorHandleFactory.hpp"
#include "ClImportTensorHandle.hpp"

#include <armnn/utility/NumericCast.hpp>
#include <armnn/utility/PolymorphicDowncast.hpp>

#include <arm_compute/core/Coordinates.h>
#include <arm_compute/runtime/CL/CLTensor.h>

namespace armnn
{

using FactoryId = ITensorHandleFactory::FactoryId;

std::unique_ptr<ITensorHandle> ClImportTensorHandleFactory::CreateSubTensorHandle(
    ITensorHandle& parent, const TensorShape& subTensorShape, const unsigned int* subTensorOrigin) const
{
    arm_compute::Coordinates coords;
    arm_compute::TensorShape shape = armcomputetensorutils::BuildArmComputeTensorShape(subTensorShape);

    coords.set_num_dimensions(subTensorShape.GetNumDimensions());
    for (unsigned int i = 0; i < subTensorShape.GetNumDimensions(); ++i)
    {
        // Arm compute indexes tensor coords in reverse order.
        unsigned int revertedIndex = subTensorShape.GetNumDimensions() - i - 1;
        coords.set(i, armnn::numeric_cast<int>(subTensorOrigin[revertedIndex]));
    }

    const arm_compute::TensorShape parentShape = armcomputetensorutils::BuildArmComputeTensorShape(parent.GetShape());

    // In order for ACL to support subtensors the concat axis cannot be on x or y and the values of x and y
    // must match the parent shapes
    if (coords.x() != 0 || coords.y() != 0)
    {
        return nullptr;
    }
    if ((parentShape.x() != shape.x()) || (parentShape.y() != shape.y()))
    {
        return nullptr;
    }

    if (!::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, parentShape, coords, shape))
    {
        return nullptr;
    }

    return std::make_unique<ClImportSubTensorHandle>(
        PolymorphicDowncast<IClImportTensorHandle*>(&parent), shape, coords);
}

std::unique_ptr<ITensorHandle> ClImportTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo) const
{
    std::unique_ptr<ClImportTensorHandle> tensorHandle = std::make_unique<ClImportTensorHandle>(tensorInfo,
                                                                                                GetImportFlags());
    return tensorHandle;
}

std::unique_ptr<ITensorHandle> ClImportTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
                                                                               DataLayout dataLayout) const
{
    std::unique_ptr<ClImportTensorHandle> tensorHandle = std::make_unique<ClImportTensorHandle>(tensorInfo,
                                                                                                dataLayout,
                                                                                                GetImportFlags());
    return tensorHandle;
}

std::unique_ptr<ITensorHandle> ClImportTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
                                                                               const bool IsMemoryManaged) const
{
    if (IsMemoryManaged)
    {
        throw InvalidArgumentException("ClImportTensorHandleFactory does not support memory managed tensors.");
    }
    return CreateTensorHandle(tensorInfo);
}

std::unique_ptr<ITensorHandle> ClImportTensorHandleFactory::CreateTensorHandle(const TensorInfo& tensorInfo,
                                                                               DataLayout dataLayout,
                                                                               const bool IsMemoryManaged) const
{
    if (IsMemoryManaged)
    {
        throw InvalidArgumentException("ClImportTensorHandleFactory does not support memory managed tensors.");
    }
    return CreateTensorHandle(tensorInfo, dataLayout);
}

const FactoryId& ClImportTensorHandleFactory::GetIdStatic()
{
    static const FactoryId s_Id(ClImportTensorHandleFactoryId());
    return s_Id;
}

const FactoryId& ClImportTensorHandleFactory::GetId() const
{
    return GetIdStatic();
}

bool ClImportTensorHandleFactory::SupportsSubTensors() const
{
    return true;
}

bool ClImportTensorHandleFactory::SupportsMapUnmap() const
{
    return false;
}

MemorySourceFlags ClImportTensorHandleFactory::GetExportFlags() const
{
    return m_ExportFlags;
}

MemorySourceFlags ClImportTensorHandleFactory::GetImportFlags() const
{
    return m_ImportFlags;
}

std::vector<Capability> ClImportTensorHandleFactory::GetCapabilities(const IConnectableLayer* layer,
                                                                     const IConnectableLayer* connectedLayer,
                                                                     CapabilityClass capabilityClass)
{
    IgnoreUnused(layer);
    IgnoreUnused(connectedLayer);
    std::vector<Capability> capabilities;
    if (capabilityClass == CapabilityClass::FallbackImportDisabled)
    {
        Capability paddingCapability(CapabilityClass::FallbackImportDisabled, true);
        capabilities.push_back(paddingCapability);
    }
    return capabilities;
}

}    // namespace armnn