arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1 | // |
Cathal Corbett | 8de96f7 | 2022-09-01 13:34:59 +0100 | [diff] [blame] | 2 | // Copyright © 2017,2022 Arm Ltd and Contributors. All rights reserved. |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ConversionUtils.hpp" |
Mike Kelly | 4a95658 | 2020-02-28 10:32:09 +0000 | [diff] [blame] | 7 | #include <armnnUtils/Permute.hpp> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 8 | |
| 9 | /// |
| 10 | /// Helper classes |
| 11 | /// |
| 12 | |
| 13 | namespace armnn_driver |
| 14 | { |
| 15 | |
| 16 | LayerInputHandle::LayerInputHandle() |
| 17 | : m_OutputSlot(nullptr) |
| 18 | , m_Valid(false) |
| 19 | {} |
| 20 | |
| 21 | LayerInputHandle::LayerInputHandle(bool valid, armnn::IOutputSlot* outputSlot, armnn::TensorInfo tensorInfo) |
| 22 | : m_OutputSlot(outputSlot) |
| 23 | , m_Valid(valid) |
| 24 | , m_TensorInfo(tensorInfo) |
| 25 | {} |
| 26 | |
| 27 | bool LayerInputHandle::IsValid() const |
| 28 | { |
| 29 | return m_Valid; |
| 30 | } |
| 31 | |
| 32 | void LayerInputHandle::Connect(armnn::IInputSlot& inputSlot) |
| 33 | { |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 34 | if (!IsValid()) |
| 35 | { |
| 36 | throw armnn::RuntimeException("LayerInputHandle is invalid"); |
| 37 | } |
| 38 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 39 | if (m_OutputSlot) |
| 40 | { |
| 41 | m_OutputSlot->Connect(inputSlot); |
| 42 | } |
| 43 | } |
| 44 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 45 | void LayerInputHandle::Disconnect(armnn::IInputSlot& inputSlot) |
| 46 | { |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 47 | if (!IsValid()) |
| 48 | { |
| 49 | throw armnn::RuntimeException("LayerInputHandle is invalid"); |
| 50 | } |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 51 | if (m_OutputSlot) |
| 52 | { |
| 53 | m_OutputSlot->Disconnect(inputSlot); |
| 54 | } |
| 55 | } |
| 56 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 57 | const armnn::TensorInfo& LayerInputHandle::GetTensorInfo() const |
| 58 | { |
| 59 | return m_TensorInfo; |
| 60 | } |
| 61 | |
Cathal Corbett | 915f2a7 | 2022-04-15 14:12:08 +0100 | [diff] [blame] | 62 | void LayerInputHandle::SanitizeQuantizationScale(LayerInputHandle& weight, |
| 63 | LayerInputHandle& input) |
| 64 | { |
Colm Donelan | bb7b281 | 2022-05-09 17:52:23 +0100 | [diff] [blame] | 65 | if (m_OutputSlot) |
| 66 | { |
| 67 | armnn::TensorInfo weightInfo = weight.GetTensorInfo(); |
| 68 | armnn::TensorInfo inputInfo = input.GetTensorInfo(); |
| 69 | armnn::TensorInfo biasInfo = GetTensorInfo(); |
Cathal Corbett | 915f2a7 | 2022-04-15 14:12:08 +0100 | [diff] [blame] | 70 | |
Colm Donelan | bb7b281 | 2022-05-09 17:52:23 +0100 | [diff] [blame] | 71 | SanitizeBiasQuantizationScale(biasInfo, weightInfo, inputInfo); |
Cathal Corbett | 915f2a7 | 2022-04-15 14:12:08 +0100 | [diff] [blame] | 72 | |
Colm Donelan | bb7b281 | 2022-05-09 17:52:23 +0100 | [diff] [blame] | 73 | m_TensorInfo = biasInfo; |
| 74 | m_OutputSlot->SetTensorInfo(biasInfo); |
| 75 | } |
Cathal Corbett | 915f2a7 | 2022-04-15 14:12:08 +0100 | [diff] [blame] | 76 | } |
| 77 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 78 | ConstTensorPin::ConstTensorPin(bool optional) |
| 79 | : m_Optional(optional) |
| 80 | {} |
| 81 | |
Jan Eilers | a71c063 | 2021-04-12 13:12:19 +0100 | [diff] [blame] | 82 | ConstTensorPin::ConstTensorPin(armnn::TensorInfo& tensorInfo, |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 83 | const void* valueStart, |
| 84 | uint32_t numBytes, |
| 85 | const armnn::PermutationVector& mappings) |
Jim Flynn | 8920cae | 2021-05-13 15:48:45 +0100 | [diff] [blame] | 86 | : m_Optional(false) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 87 | { |
Jan Eilers | 0b7a419 | 2020-03-09 18:20:42 +0000 | [diff] [blame] | 88 | armnn::IgnoreUnused(numBytes); |
Colm Donelan | ccfeb5e | 2021-03-30 15:30:13 +0100 | [diff] [blame] | 89 | if (tensorInfo.GetNumBytes() != numBytes) |
| 90 | { |
| 91 | ALOGW("The size of ConstTensor does not match its TensorInfo."); |
| 92 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 93 | |
| 94 | const bool needsSwizzling = (mappings.GetSize() > 0); |
| 95 | if (needsSwizzling) |
| 96 | { |
| 97 | m_SwizzledTensorData.resize(tensorInfo.GetNumBytes()); |
| 98 | SwizzleAndroidNn4dTensorToArmNn(tensorInfo, valueStart, m_SwizzledTensorData.data(), mappings); |
| 99 | |
Jan Eilers | a71c063 | 2021-04-12 13:12:19 +0100 | [diff] [blame] | 100 | m_ConstTensor = armnn::ConstTensor(tensorInfo, m_SwizzledTensorData.data()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
| 104 | m_ConstTensor = armnn::ConstTensor(tensorInfo, valueStart); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | bool ConstTensorPin::IsValid() const |
| 109 | { |
| 110 | return m_ConstTensor.GetMemoryArea() != nullptr; |
| 111 | } |
| 112 | |
| 113 | bool ConstTensorPin::IsOptional() const |
| 114 | { |
| 115 | return m_Optional; |
| 116 | } |
| 117 | |
| 118 | const armnn::ConstTensor& ConstTensorPin::GetConstTensor() const |
| 119 | { |
| 120 | return m_ConstTensor; |
| 121 | } |
| 122 | |
| 123 | const armnn::ConstTensor* ConstTensorPin::GetConstTensorPtr() const |
| 124 | { |
| 125 | if (IsValid() && m_ConstTensor.GetNumElements() > 0) |
| 126 | { |
| 127 | return &m_ConstTensor; |
| 128 | } |
| 129 | // tensor is either invalid, or has no elements (indicating an optional tensor that was not provided) |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
| 133 | /// |
| 134 | /// Utility functions |
| 135 | /// |
| 136 | |
| 137 | armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo, |
| 138 | ActivationFn activation, |
| 139 | armnn::IConnectableLayer* prevLayer, |
| 140 | ConversionData& data) |
| 141 | { |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 142 | if (prevLayer->GetNumOutputSlots() != 1) |
| 143 | { |
| 144 | Fail("%s: Incorrect Number of OutputSlots expected 1 was %i", __func__, prevLayer->GetNumOutputSlots()); |
| 145 | return nullptr; |
| 146 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 147 | prevLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 148 | |
| 149 | armnn::IConnectableLayer* activationLayer = prevLayer; |
| 150 | |
| 151 | if (activation != ActivationFn::kActivationNone) |
| 152 | { |
| 153 | armnn::ActivationDescriptor activationDesc; |
| 154 | switch (activation) |
| 155 | { |
| 156 | case ActivationFn::kActivationRelu: |
| 157 | { |
| 158 | activationDesc.m_Function = armnn::ActivationFunction::ReLu; |
| 159 | break; |
| 160 | } |
| 161 | case ActivationFn::kActivationRelu1: |
| 162 | { |
| 163 | activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 164 | activationDesc.m_A = 1.0f; |
| 165 | activationDesc.m_B = -1.0f; |
| 166 | break; |
| 167 | } |
| 168 | case ActivationFn::kActivationRelu6: |
| 169 | { |
| 170 | activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 171 | activationDesc.m_A = 6.0f; |
| 172 | break; |
| 173 | } |
| 174 | case ActivationFn::kActivationSigmoid: |
| 175 | { |
| 176 | activationDesc.m_Function = armnn::ActivationFunction::Sigmoid; |
| 177 | break; |
| 178 | } |
| 179 | case ActivationFn::kActivationTanh: |
| 180 | { |
| 181 | activationDesc.m_Function = armnn::ActivationFunction::TanH; |
| 182 | activationDesc.m_A = 1.0f; |
| 183 | activationDesc.m_B = 1.0f; |
| 184 | break; |
| 185 | } |
| 186 | default: |
| 187 | { |
| 188 | Fail("%s: Invalid activation enum value %i", __func__, activation); |
| 189 | return nullptr; |
| 190 | } |
| 191 | } |
| 192 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 193 | bool isSupported = false; |
Cathal Corbett | 8de96f7 | 2022-09-01 13:34:59 +0100 | [diff] [blame] | 194 | armnn::BackendId setBackend; |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 195 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 196 | IsActivationSupported, |
| 197 | data.m_Backends, |
| 198 | isSupported, |
Cathal Corbett | 8de96f7 | 2022-09-01 13:34:59 +0100 | [diff] [blame] | 199 | setBackend, |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 200 | prevLayer->GetOutputSlot(0).GetTensorInfo(), |
| 201 | tensorInfo, |
| 202 | activationDesc); |
| 203 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 204 | { |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
| 208 | activationLayer = data.m_Network->AddActivationLayer(activationDesc); |
Cathal Corbett | 8de96f7 | 2022-09-01 13:34:59 +0100 | [diff] [blame] | 209 | activationLayer->SetBackendId(setBackend); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 210 | |
| 211 | prevLayer->GetOutputSlot(0).Connect(activationLayer->GetInputSlot(0)); |
| 212 | activationLayer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 213 | } |
| 214 | |
| 215 | return activationLayer; |
| 216 | } |
| 217 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 218 | } // namespace armnn_driver |