blob: 9cc6e28680b353911733c4db113696fdc8a0d045 [file] [log] [blame]
arovir01b0717b52018-09-05 17:03:25 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ConversionUtils.hpp"
Mike Kelly4a956582020-02-28 10:32:09 +00007#include <armnnUtils/Permute.hpp>
arovir01b0717b52018-09-05 17:03:25 +01008
9///
10/// Helper classes
11///
12
13namespace armnn_driver
14{
15
16LayerInputHandle::LayerInputHandle()
17 : m_OutputSlot(nullptr)
18 , m_Valid(false)
19{}
20
21LayerInputHandle::LayerInputHandle(bool valid, armnn::IOutputSlot* outputSlot, armnn::TensorInfo tensorInfo)
22 : m_OutputSlot(outputSlot)
23 , m_Valid(valid)
24 , m_TensorInfo(tensorInfo)
25{}
26
27bool LayerInputHandle::IsValid() const
28{
29 return m_Valid;
30}
31
32void LayerInputHandle::Connect(armnn::IInputSlot& inputSlot)
33{
Narumol Prangnawarat4d07e5e2020-04-06 16:46:21 +010034 ARMNN_ASSERT(IsValid());
arovir01b0717b52018-09-05 17:03:25 +010035 if (m_OutputSlot)
36 {
37 m_OutputSlot->Connect(inputSlot);
38 }
39}
40
Finn Williamsa4983ce2020-07-23 12:55:12 +010041void LayerInputHandle::Disconnect(armnn::IInputSlot& inputSlot)
42{
43 ARMNN_ASSERT(IsValid());
44 if (m_OutputSlot)
45 {
46 m_OutputSlot->Disconnect(inputSlot);
47 }
48}
49
arovir01b0717b52018-09-05 17:03:25 +010050const armnn::TensorInfo& LayerInputHandle::GetTensorInfo() const
51{
52 return m_TensorInfo;
53}
54
55ConstTensorPin::ConstTensorPin(bool optional)
56 : m_Optional(optional)
57{}
58
59ConstTensorPin::ConstTensorPin(const armnn::TensorInfo& tensorInfo,
60 const void* valueStart,
61 uint32_t numBytes,
62 const armnn::PermutationVector& mappings)
63{
Jan Eilers0b7a4192020-03-09 18:20:42 +000064 armnn::IgnoreUnused(numBytes);
Colm Donelanccfeb5e2021-03-30 15:30:13 +010065 if (tensorInfo.GetNumBytes() != numBytes)
66 {
67 ALOGW("The size of ConstTensor does not match its TensorInfo.");
68 }
arovir01b0717b52018-09-05 17:03:25 +010069
70 const bool needsSwizzling = (mappings.GetSize() > 0);
71 if (needsSwizzling)
72 {
73 m_SwizzledTensorData.resize(tensorInfo.GetNumBytes());
74 SwizzleAndroidNn4dTensorToArmNn(tensorInfo, valueStart, m_SwizzledTensorData.data(), mappings);
75
76 m_ConstTensor = armnn::ConstTensor(armnnUtils::Permuted(tensorInfo, mappings), m_SwizzledTensorData.data());
77 }
78 else
79 {
80 m_ConstTensor = armnn::ConstTensor(tensorInfo, valueStart);
81 }
82}
83
84bool ConstTensorPin::IsValid() const
85{
86 return m_ConstTensor.GetMemoryArea() != nullptr;
87}
88
89bool ConstTensorPin::IsOptional() const
90{
91 return m_Optional;
92}
93
94const armnn::ConstTensor& ConstTensorPin::GetConstTensor() const
95{
96 return m_ConstTensor;
97}
98
99const armnn::ConstTensor* ConstTensorPin::GetConstTensorPtr() const
100{
101 if (IsValid() && m_ConstTensor.GetNumElements() > 0)
102 {
103 return &m_ConstTensor;
104 }
105 // tensor is either invalid, or has no elements (indicating an optional tensor that was not provided)
106 return nullptr;
107}
108
109///
110/// Utility functions
111///
112
113armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo,
114 ActivationFn activation,
115 armnn::IConnectableLayer* prevLayer,
116 ConversionData& data)
117{
Narumol Prangnawarat4d07e5e2020-04-06 16:46:21 +0100118 ARMNN_ASSERT(prevLayer->GetNumOutputSlots() == 1);
arovir01b0717b52018-09-05 17:03:25 +0100119
120 prevLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
121
122 armnn::IConnectableLayer* activationLayer = prevLayer;
123
124 if (activation != ActivationFn::kActivationNone)
125 {
126 armnn::ActivationDescriptor activationDesc;
127 switch (activation)
128 {
129 case ActivationFn::kActivationRelu:
130 {
131 activationDesc.m_Function = armnn::ActivationFunction::ReLu;
132 break;
133 }
134 case ActivationFn::kActivationRelu1:
135 {
136 activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu;
137 activationDesc.m_A = 1.0f;
138 activationDesc.m_B = -1.0f;
139 break;
140 }
141 case ActivationFn::kActivationRelu6:
142 {
143 activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu;
144 activationDesc.m_A = 6.0f;
145 break;
146 }
147 case ActivationFn::kActivationSigmoid:
148 {
149 activationDesc.m_Function = armnn::ActivationFunction::Sigmoid;
150 break;
151 }
152 case ActivationFn::kActivationTanh:
153 {
154 activationDesc.m_Function = armnn::ActivationFunction::TanH;
155 activationDesc.m_A = 1.0f;
156 activationDesc.m_B = 1.0f;
157 break;
158 }
159 default:
160 {
161 Fail("%s: Invalid activation enum value %i", __func__, activation);
162 return nullptr;
163 }
164 }
165
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100166 bool isSupported = false;
167 FORWARD_LAYER_SUPPORT_FUNC(__func__,
168 IsActivationSupported,
169 data.m_Backends,
170 isSupported,
171 prevLayer->GetOutputSlot(0).GetTensorInfo(),
172 tensorInfo,
173 activationDesc);
174 if (!isSupported)
arovir01b0717b52018-09-05 17:03:25 +0100175 {
176 return nullptr;
177 }
178
179 activationLayer = data.m_Network->AddActivationLayer(activationDesc);
180
181 prevLayer->GetOutputSlot(0).Connect(activationLayer->GetInputSlot(0));
182 activationLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
183 }
184
185 return activationLayer;
186}
187
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100188} // namespace armnn_driver