blob: 91c1ae4e92ab4bb169ec93d6ed186ee9f67af9b1 [file] [log] [blame]
arovir01b0717b52018-09-05 17:03:25 +01001//
Mike Kellye2d611e2021-10-14 12:35:58 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
arovir01b0717b52018-09-05 17:03:25 +01003// 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{
Mike Kellye2d611e2021-10-14 12:35:58 +010034 if (!IsValid())
35 {
36 throw armnn::RuntimeException("LayerInputHandle is invalid");
37 }
38
arovir01b0717b52018-09-05 17:03:25 +010039 if (m_OutputSlot)
40 {
41 m_OutputSlot->Connect(inputSlot);
42 }
43}
44
Finn Williamsa4983ce2020-07-23 12:55:12 +010045void LayerInputHandle::Disconnect(armnn::IInputSlot& inputSlot)
46{
Mike Kellye2d611e2021-10-14 12:35:58 +010047 if (!IsValid())
48 {
49 throw armnn::RuntimeException("LayerInputHandle is invalid");
50 }
Finn Williamsa4983ce2020-07-23 12:55:12 +010051 if (m_OutputSlot)
52 {
53 m_OutputSlot->Disconnect(inputSlot);
54 }
55}
56
arovir01b0717b52018-09-05 17:03:25 +010057const armnn::TensorInfo& LayerInputHandle::GetTensorInfo() const
58{
59 return m_TensorInfo;
60}
61
Cathal Corbett915f2a72022-04-15 14:12:08 +010062void LayerInputHandle::SanitizeQuantizationScale(LayerInputHandle& weight,
63 LayerInputHandle& input)
64{
65 armnn::TensorInfo weightInfo = weight.GetTensorInfo();
66 armnn::TensorInfo inputInfo = input.GetTensorInfo();
67 armnn::TensorInfo biasInfo = GetTensorInfo();
68
69 SanitizeBiasQuantizationScale(biasInfo, weightInfo, inputInfo);
70
71 m_TensorInfo = biasInfo;
72 m_OutputSlot->SetTensorInfo(biasInfo);
73}
74
arovir01b0717b52018-09-05 17:03:25 +010075ConstTensorPin::ConstTensorPin(bool optional)
76 : m_Optional(optional)
77{}
78
Jan Eilersa71c0632021-04-12 13:12:19 +010079ConstTensorPin::ConstTensorPin(armnn::TensorInfo& tensorInfo,
arovir01b0717b52018-09-05 17:03:25 +010080 const void* valueStart,
81 uint32_t numBytes,
82 const armnn::PermutationVector& mappings)
Jim Flynn8920cae2021-05-13 15:48:45 +010083 : m_Optional(false)
arovir01b0717b52018-09-05 17:03:25 +010084{
Jan Eilers0b7a4192020-03-09 18:20:42 +000085 armnn::IgnoreUnused(numBytes);
Colm Donelanccfeb5e2021-03-30 15:30:13 +010086 if (tensorInfo.GetNumBytes() != numBytes)
87 {
88 ALOGW("The size of ConstTensor does not match its TensorInfo.");
89 }
arovir01b0717b52018-09-05 17:03:25 +010090
91 const bool needsSwizzling = (mappings.GetSize() > 0);
92 if (needsSwizzling)
93 {
94 m_SwizzledTensorData.resize(tensorInfo.GetNumBytes());
95 SwizzleAndroidNn4dTensorToArmNn(tensorInfo, valueStart, m_SwizzledTensorData.data(), mappings);
96
Jan Eilersa71c0632021-04-12 13:12:19 +010097 m_ConstTensor = armnn::ConstTensor(tensorInfo, m_SwizzledTensorData.data());
arovir01b0717b52018-09-05 17:03:25 +010098 }
99 else
100 {
101 m_ConstTensor = armnn::ConstTensor(tensorInfo, valueStart);
102 }
103}
104
105bool ConstTensorPin::IsValid() const
106{
107 return m_ConstTensor.GetMemoryArea() != nullptr;
108}
109
110bool ConstTensorPin::IsOptional() const
111{
112 return m_Optional;
113}
114
115const armnn::ConstTensor& ConstTensorPin::GetConstTensor() const
116{
117 return m_ConstTensor;
118}
119
120const armnn::ConstTensor* ConstTensorPin::GetConstTensorPtr() const
121{
122 if (IsValid() && m_ConstTensor.GetNumElements() > 0)
123 {
124 return &m_ConstTensor;
125 }
126 // tensor is either invalid, or has no elements (indicating an optional tensor that was not provided)
127 return nullptr;
128}
129
130///
131/// Utility functions
132///
133
134armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo,
135 ActivationFn activation,
136 armnn::IConnectableLayer* prevLayer,
137 ConversionData& data)
138{
Mike Kellye2d611e2021-10-14 12:35:58 +0100139 if (prevLayer->GetNumOutputSlots() != 1)
140 {
141 Fail("%s: Incorrect Number of OutputSlots expected 1 was %i", __func__, prevLayer->GetNumOutputSlots());
142 return nullptr;
143 }
arovir01b0717b52018-09-05 17:03:25 +0100144 prevLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
145
146 armnn::IConnectableLayer* activationLayer = prevLayer;
147
148 if (activation != ActivationFn::kActivationNone)
149 {
150 armnn::ActivationDescriptor activationDesc;
151 switch (activation)
152 {
153 case ActivationFn::kActivationRelu:
154 {
155 activationDesc.m_Function = armnn::ActivationFunction::ReLu;
156 break;
157 }
158 case ActivationFn::kActivationRelu1:
159 {
160 activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu;
161 activationDesc.m_A = 1.0f;
162 activationDesc.m_B = -1.0f;
163 break;
164 }
165 case ActivationFn::kActivationRelu6:
166 {
167 activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu;
168 activationDesc.m_A = 6.0f;
169 break;
170 }
171 case ActivationFn::kActivationSigmoid:
172 {
173 activationDesc.m_Function = armnn::ActivationFunction::Sigmoid;
174 break;
175 }
176 case ActivationFn::kActivationTanh:
177 {
178 activationDesc.m_Function = armnn::ActivationFunction::TanH;
179 activationDesc.m_A = 1.0f;
180 activationDesc.m_B = 1.0f;
181 break;
182 }
183 default:
184 {
185 Fail("%s: Invalid activation enum value %i", __func__, activation);
186 return nullptr;
187 }
188 }
189
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100190 bool isSupported = false;
191 FORWARD_LAYER_SUPPORT_FUNC(__func__,
192 IsActivationSupported,
193 data.m_Backends,
194 isSupported,
195 prevLayer->GetOutputSlot(0).GetTensorInfo(),
196 tensorInfo,
197 activationDesc);
198 if (!isSupported)
arovir01b0717b52018-09-05 17:03:25 +0100199 {
200 return nullptr;
201 }
202
203 activationLayer = data.m_Network->AddActivationLayer(activationDesc);
204
205 prevLayer->GetOutputSlot(0).Connect(activationLayer->GetInputSlot(0));
206 activationLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
207 }
208
209 return activationLayer;
210}
211
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100212} // namespace armnn_driver