arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "HalPolicy.hpp" |
| 7 | |
Matthew Bentham | f61c270 | 2019-04-23 16:43:27 +0100 | [diff] [blame] | 8 | #include <armnn/Optional.hpp> |
| 9 | |
| 10 | #include "FullyConnected.hpp" |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 11 | #include "Utils.hpp" |
arovir01 | 5602b19 | 2018-10-04 16:15:02 +0100 | [diff] [blame] | 12 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 13 | namespace armnn_driver |
| 14 | { |
| 15 | namespace hal_1_0 |
| 16 | { |
| 17 | |
| 18 | bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data) |
| 19 | { |
| 20 | switch (operation.type) |
| 21 | { |
| 22 | case V1_0::OperationType::ADD: |
| 23 | return ConvertAdd(operation, model, data); |
| 24 | case V1_0::OperationType::AVERAGE_POOL_2D: |
| 25 | return ConvertAveragePool2d(operation, model, data); |
| 26 | case V1_0::OperationType::CONCATENATION: |
| 27 | return ConvertConcatenation(operation, model, data); |
| 28 | case V1_0::OperationType::CONV_2D: |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 29 | return ConvertConv2d(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 30 | case V1_0::OperationType::DEPTHWISE_CONV_2D: |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 31 | return ConvertDepthwiseConv2d(operation, model, data); |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 32 | case V1_0::OperationType::DEQUANTIZE: |
| 33 | return ConvertDequantize(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 34 | case V1_0::OperationType::FLOOR: |
| 35 | return ConvertFloor(operation, model, data); |
| 36 | case V1_0::OperationType::FULLY_CONNECTED: |
| 37 | return ConvertFullyConnected(operation, model, data); |
| 38 | case V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION: |
| 39 | return ConvertLocalResponseNormalization(operation, model, data); |
| 40 | case V1_0::OperationType::LOGISTIC: |
| 41 | return ConvertLogistic(operation, model, data); |
| 42 | case V1_0::OperationType::LSTM: |
| 43 | return ConvertLstm(operation, model, data); |
| 44 | case V1_0::OperationType::L2_NORMALIZATION: |
| 45 | return ConvertL2Normalization(operation, model, data); |
| 46 | case V1_0::OperationType::L2_POOL_2D: |
| 47 | return ConvertL2Pool2d(operation, model, data); |
| 48 | case V1_0::OperationType::MAX_POOL_2D: |
| 49 | return ConvertMaxPool2d(operation, model, data); |
| 50 | case V1_0::OperationType::MUL: |
| 51 | return ConvertMul(operation, model, data); |
| 52 | case V1_0::OperationType::RELU: |
| 53 | return ConvertReLu(operation, model, data); |
| 54 | case V1_0::OperationType::RELU1: |
| 55 | return ConvertReLu1(operation, model, data); |
| 56 | case V1_0::OperationType::RELU6: |
| 57 | return ConvertReLu6(operation, model, data); |
| 58 | case V1_0::OperationType::SOFTMAX: |
| 59 | return ConvertSoftmax(operation, model, data); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 60 | case V1_0::OperationType::SPACE_TO_DEPTH: |
| 61 | return ConvertSpaceToDepth(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 62 | case V1_0::OperationType::TANH: |
| 63 | return ConvertTanH(operation, model, data); |
| 64 | case V1_0::OperationType::RESHAPE: |
| 65 | return ConvertReshape(operation, model, data); |
| 66 | case V1_0::OperationType::RESIZE_BILINEAR: |
| 67 | return ConvertResizeBilinear(operation, model, data); |
| 68 | default: |
| 69 | return Fail("%s: Operation type %s not supported in ArmnnDriver", |
| 70 | __func__, toString(operation.type).c_str()); |
| 71 | } |
| 72 | } |
| 73 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 74 | bool HalPolicy::ValidateConv2dParameters(const Operation &operation) |
| 75 | { |
| 76 | if (operation.inputs.size() != 10 && operation.inputs.size() != 7) |
| 77 | { |
| 78 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | bool HalPolicy::ValidateDepthwiseConv2dParameters(const Operation &operation) |
| 84 | { |
| 85 | if (operation.inputs.size() != 11 && operation.inputs.size() != 8) |
| 86 | { |
| 87 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 92 | bool HalPolicy::ConvertAdd(const Operation& operation, const Model& model, ConversionData& data) |
| 93 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 94 | ALOGV("hal_1_0::HalPolicy::ConvertAdd()"); |
| 95 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 96 | LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
| 97 | LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 1, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 98 | |
| 99 | if (!input0.IsValid() || !input1.IsValid()) |
| 100 | { |
| 101 | return Fail("%s: Operation has invalid inputs", __func__); |
| 102 | } |
| 103 | |
| 104 | // The FuseActivation parameter is always the input index 2 |
| 105 | // and it should be optional |
| 106 | ActivationFn activationFunction; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 107 | if (!GetOptionalInputActivation<hal_1_0::HalPolicy>(operation, 2, activationFunction, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 108 | { |
| 109 | return Fail("%s: Operation has invalid inputs", __func__); |
| 110 | } |
| 111 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 112 | const Operand* outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 113 | if (!outputOperand) |
| 114 | { |
| 115 | return false; |
| 116 | } |
| 117 | |
Aron Virginas-Tar | 4b86213 | 2019-07-24 16:26:57 +0100 | [diff] [blame] | 118 | const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo(); |
| 119 | const armnn::TensorInfo& inputInfo1 = input1.GetTensorInfo(); |
| 120 | |
| 121 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
| 122 | if (IsDynamicTensor(outputInfo)) |
| 123 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 124 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Aron Virginas-Tar | 4b86213 | 2019-07-24 16:26:57 +0100 | [diff] [blame] | 125 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 126 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 127 | bool isSupported = false; |
| 128 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 129 | IsAdditionSupported, |
| 130 | data.m_Backends, |
| 131 | isSupported, |
Aron Virginas-Tar | 4b86213 | 2019-07-24 16:26:57 +0100 | [diff] [blame] | 132 | inputInfo0, |
| 133 | inputInfo1, |
| 134 | outputInfo); |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 135 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddAdditionLayer(); |
Aron Virginas-Tar | 4b86213 | 2019-07-24 16:26:57 +0100 | [diff] [blame] | 141 | armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 142 | |
| 143 | if (endLayer != nullptr) |
| 144 | { |
| 145 | BroadcastTensor(input0, input1, startLayer, *data.m_Network); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 146 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *endLayer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 147 | } |
| 148 | else |
| 149 | { |
| 150 | return Fail("%s: ProcessActivation failed", __func__); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | bool HalPolicy::ConvertAveragePool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 155 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 156 | ALOGV("hal_1_0::HalPolicy::ConvertAveragePool2d()"); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 157 | return ConvertPooling2d<hal_1_0::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Average, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool HalPolicy::ConvertConcatenation(const Operation& operation, const Model& model, ConversionData& data) |
| 161 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 162 | ALOGV("hal_1_0::HalPolicy::ConvertConcatenation()"); |
| 163 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 164 | // The first N (0..N-1) inputs are tensors. The Nth input is the concatenation axis. |
| 165 | if (operation.inputs.size() <= 1) |
| 166 | { |
| 167 | return Fail("%s: Operation has insufficient arguments", __func__); |
| 168 | } |
| 169 | |
| 170 | // Get inputs and outputs |
| 171 | const std::size_t numInputTensors = operation.inputs.size() - 1; |
| 172 | |
| 173 | int32_t concatDim; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 174 | if (!GetInputScalar<hal_1_0::HalPolicy>(operation, numInputTensors, OperandType::INT32, concatDim, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 175 | { |
| 176 | return Fail("%s: Operation has invalid inputs", __func__); |
| 177 | } |
| 178 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 179 | const Operand* const outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 180 | if (!outputOperand) |
| 181 | { |
| 182 | return Fail("%s: Operation has no outputs", __func__); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | armnn::TensorInfo outputInfo = GetTensorInfoForOperand(*outputOperand); |
| 187 | armnn::TensorShape outputShape = outputInfo.GetShape(); |
| 188 | |
| 189 | // |
| 190 | // handle negative concat dims along the lines of tensorflow as described here: |
| 191 | // https://www.tensorflow.org/api_docs/python/tf/concat |
| 192 | // "negative axis refers to axis + rank(values)-th dimension" |
| 193 | // |
| 194 | if (concatDim < 0) |
| 195 | { |
| 196 | concatDim += outputShape.GetNumDimensions(); |
| 197 | } |
| 198 | |
| 199 | if (concatDim >= static_cast<int32_t>(outputShape.GetNumDimensions()) || concatDim < 0) |
| 200 | { |
| 201 | return Fail("%s: Operation has invalid concat axis: %d", __func__, concatDim); |
| 202 | } |
| 203 | |
| 204 | std::vector<LayerInputHandle> inputHandles; |
| 205 | std::vector<armnn::TensorShape> inputShapes; |
| 206 | |
| 207 | inputHandles.reserve(numInputTensors); |
| 208 | inputShapes.reserve(numInputTensors); |
| 209 | |
| 210 | bool inputsHaveBeenReshaped = false; |
| 211 | unsigned int tensorDimensionsAdded = 0; |
| 212 | |
| 213 | for (uint32_t i = 0; i < numInputTensors; ++i) |
| 214 | { |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 215 | const Operand* const operand = GetInputOperand<hal_1_0::HalPolicy>(operation, i, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 216 | if (!operand) |
| 217 | { |
| 218 | return Fail("%s: Operation has invalid inputs", __func__); |
| 219 | } |
| 220 | |
| 221 | armnn::TensorShape operandShape = GetTensorShapeForOperand(*operand); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 222 | LayerInputHandle operandInputHandle = |
| 223 | ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, i, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 224 | |
| 225 | if (operandShape.GetNumDimensions() == 0) |
| 226 | { |
| 227 | return Fail("%s: Operands with rank 0 are not supported", __func__); |
| 228 | } |
| 229 | |
| 230 | if (RequiresReshape(operandShape)) |
| 231 | { |
| 232 | inputsHaveBeenReshaped = true; |
| 233 | |
| 234 | armnn::TensorInfo reshapeInfo = operandInputHandle.GetTensorInfo(); |
| 235 | |
| 236 | // Expand the tensor to three dimensions |
| 237 | if (operandShape.GetNumDimensions() == 2) |
| 238 | { |
| 239 | reshapeInfo.SetShape(armnn::TensorShape({1, operandShape[0], operandShape[1]})); |
| 240 | tensorDimensionsAdded = 1; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | reshapeInfo.SetShape(armnn::TensorShape({1, 1, operandShape[0]})); |
| 245 | tensorDimensionsAdded = 2; |
| 246 | } |
| 247 | |
| 248 | armnn::IConnectableLayer& newReshape = AddReshapeLayer( |
| 249 | *data.m_Network, |
| 250 | operandInputHandle, |
| 251 | reshapeInfo |
| 252 | ); |
| 253 | |
| 254 | // Point to the reshape operation rather then the input operation |
| 255 | operandShape = reshapeInfo.GetShape(); |
| 256 | operandInputHandle = LayerInputHandle(true, &newReshape.GetOutputSlot(0), reshapeInfo); |
| 257 | } |
| 258 | |
| 259 | inputShapes.emplace_back(operandShape); |
| 260 | inputHandles.emplace_back(operandInputHandle); |
| 261 | |
| 262 | if (!inputHandles.back().IsValid()) |
| 263 | { |
| 264 | return Fail("%s: Operation has invalid inputs", __func__); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | BOOST_ASSERT(inputShapes.size() == inputHandles.size()); |
| 269 | |
| 270 | if (inputsHaveBeenReshaped) |
| 271 | { |
| 272 | // Adjust the concatenation dimension by the amount of dimensions added (if any) |
| 273 | concatDim += tensorDimensionsAdded; |
| 274 | |
| 275 | // Add extra dimensions to the output shape to reflect the addition of the reshape layers |
| 276 | if (tensorDimensionsAdded == 1) |
| 277 | { |
| 278 | outputShape = armnn::TensorShape({1, outputShape[0], outputShape[1]}); |
| 279 | } |
| 280 | else if (tensorDimensionsAdded == 2) |
| 281 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 282 | outputShape = armnn::TensorShape({1, 1, outputShape[0]}); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 286 | // Check if permutations is required and get the pair of permutations required for the concatenation. |
| 287 | // Permutation is required when the concat dimension is 2 for a 4D tensor or 1 for a 3D tensor. |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 288 | std::pair<armnn::PermutationVector, armnn::PermutationVector> permutationPair = |
| 289 | std::make_pair(IdentityPermutation4D, IdentityPermutation4D); |
| 290 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 291 | bool needPermute = |
| 292 | CreateConcatPermutationParameters(inputShapes[0].GetNumDimensions(), concatDim, permutationPair); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 293 | |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 294 | if (needPermute) |
| 295 | { |
| 296 | outputShape = armnnUtils::Permuted(outputShape, permutationPair.first); |
| 297 | } |
| 298 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 299 | outputInfo.SetShape(outputShape); |
| 300 | |
| 301 | // this is no-op for identity swizzles, otherwise it replaces both |
| 302 | // the handles and shapes with the swizzled layer output handles and shapes |
| 303 | SwizzleInputs(*data.m_Network, inputHandles, inputShapes, permutationPair.first); |
| 304 | |
Jim Flynn | 7b1e41f | 2019-05-22 18:00:04 +0100 | [diff] [blame] | 305 | // Create an armnn concat layer descriptor - this will also perform validation on the input shapes |
| 306 | armnn::OriginsDescriptor concatDescriptor; |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 307 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 308 | try |
| 309 | { |
Jim Flynn | 7b1e41f | 2019-05-22 18:00:04 +0100 | [diff] [blame] | 310 | // The concat descriptor is always created across the only supported concat dimension |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 311 | // which is 0, 1 or 3 for a 4-D tensor, or 0 or 2 for a 3-D tensor. |
Jim Flynn | 7b1e41f | 2019-05-22 18:00:04 +0100 | [diff] [blame] | 312 | concatDescriptor = |
Jim Flynn | 52aa935 | 2019-05-20 12:52:30 +0100 | [diff] [blame] | 313 | armnn::CreateDescriptorForConcatenation(inputShapes.begin(), inputShapes.end(), concatDim); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 314 | } |
| 315 | catch (const armnn::Exception& error) |
| 316 | { |
Jim Flynn | 7b1e41f | 2019-05-22 18:00:04 +0100 | [diff] [blame] | 317 | return Fail("%s: Error preparing concat descriptor. %s", __func__, error.what()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Validate the output shape is correct given the input shapes based on the |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 321 | // only valid concat dimension which is 0, 1 or 3 for a 4-D tensor, or 0 or 2 for a 3-D tensor. |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 322 | if (!ValidateConcatOutputShape(inputShapes, outputShape, concatDim)) |
| 323 | { |
| 324 | return Fail("%s: Error validating the output shape for concat", __func__); |
| 325 | } |
| 326 | |
| 327 | std::vector<const armnn::TensorInfo*> inputTensorInfos; |
| 328 | std::transform(inputHandles.begin(), inputHandles.end(), std::back_inserter(inputTensorInfos), |
| 329 | [](const LayerInputHandle& h) -> const armnn::TensorInfo*{ return &h.GetTensorInfo(); }); |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 330 | |
| 331 | bool isSupported = false; |
| 332 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 333 | IsConcatSupported, |
| 334 | data.m_Backends, |
| 335 | isSupported, |
| 336 | inputTensorInfos, |
| 337 | outputInfo, |
| 338 | concatDescriptor); |
| 339 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 340 | { |
| 341 | return false; |
| 342 | } |
| 343 | |
Jim Flynn | 7b1e41f | 2019-05-22 18:00:04 +0100 | [diff] [blame] | 344 | armnn::IConnectableLayer* layer = data.m_Network->AddConcatLayer(concatDescriptor); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 345 | assert(layer != nullptr); |
| 346 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 347 | |
| 348 | // Connect inputs to the layer |
| 349 | const int numInputSlots = layer->GetNumInputSlots(); |
| 350 | assert(static_cast<std::size_t>(numInputSlots) == inputHandles.size()); |
| 351 | for (int i = 0; i < numInputSlots; ++i) |
| 352 | { |
| 353 | // connect the input directly to the merge (concat) layer |
| 354 | inputHandles[static_cast<unsigned int>(i)].Connect(layer->GetInputSlot(i)); |
| 355 | } |
| 356 | |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 357 | if (needPermute) |
| 358 | { |
| 359 | // Add permutation layer and connect the output to it, the permutation becomes the output layer |
| 360 | armnn::IConnectableLayer& deswizzleLayer = AddPermuteLayer(*data.m_Network, |
| 361 | layer->GetOutputSlot(0), |
| 362 | permutationPair.second); |
| 363 | layer = &deswizzleLayer; |
| 364 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 365 | |
| 366 | if (inputsHaveBeenReshaped) |
| 367 | { |
| 368 | armnn::TensorInfo afterConcatInfo = layer->GetOutputSlot(0).GetTensorInfo(); |
| 369 | |
| 370 | // Undo the reshape knowing the amount of dimensions added |
| 371 | if (tensorDimensionsAdded == 1) |
| 372 | { |
| 373 | afterConcatInfo.SetShape(armnn::TensorShape({ afterConcatInfo.GetShape()[1], |
| 374 | afterConcatInfo.GetShape()[2] })); |
| 375 | } |
| 376 | else if (tensorDimensionsAdded == 2) |
| 377 | { |
narpra01 | f176d5a | 2018-11-18 20:17:48 +0000 | [diff] [blame] | 378 | afterConcatInfo.SetShape(armnn::TensorShape({ afterConcatInfo.GetShape()[2] })); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | layer = &AddReshapeLayer( |
| 382 | *data.m_Network, |
| 383 | layer->GetOutputSlot(0), |
| 384 | afterConcatInfo |
| 385 | ); |
| 386 | } |
| 387 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 388 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 391 | bool HalPolicy::ConvertConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 392 | { |
| 393 | ALOGV("hal_1_0::HalPolicy::ConvertConv2d()"); |
| 394 | return ValidateConv2dParameters(operation) && ::ConvertConv2d<hal_1_0::HalPolicy>(operation, model, data); |
| 395 | } |
| 396 | |
| 397 | bool HalPolicy::ConvertDepthwiseConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 398 | { |
| 399 | ALOGV("hal_1_0::HalPolicy::ConvertDepthwiseConv2d()"); |
| 400 | return ValidateDepthwiseConv2dParameters(operation) && |
| 401 | ::ConvertDepthwiseConv2d<hal_1_0::HalPolicy>(operation, model, data); |
| 402 | } |
| 403 | |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 404 | bool HalPolicy::ConvertDequantize(const Operation& operation, const Model& model, ConversionData& data) |
| 405 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 406 | ALOGV("hal_1_0::HalPolicy::ConvertDequantize()"); |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 407 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 408 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 409 | if (!input.IsValid()) |
| 410 | { |
| 411 | return Fail("%s: Operation has invalid input", __func__); |
| 412 | } |
| 413 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 414 | const Operand* const outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 415 | if (!outputOperand) |
| 416 | { |
| 417 | return Fail("%s: Operation has invalid outputs", __func__); |
| 418 | } |
| 419 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 420 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 421 | if (IsDynamicTensor(outputInfo)) |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 422 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 423 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 426 | bool isSupported = false; |
| 427 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 428 | IsDequantizeSupported, |
| 429 | data.m_Backends, |
| 430 | isSupported, |
| 431 | input.GetTensorInfo(), |
| 432 | GetTensorInfoForOperand(*outputOperand)); |
| 433 | if (!isSupported) |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 434 | { |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | armnn::IConnectableLayer* const layer = data.m_Network->AddDequantizeLayer(); |
| 439 | assert(layer != nullptr); |
| 440 | input.Connect(layer->GetInputSlot(0)); |
| 441 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 442 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
David Monahan | acf479a | 2019-05-29 14:27:04 +0100 | [diff] [blame] | 443 | } |
| 444 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 445 | bool HalPolicy::ConvertFloor(const Operation& operation, const Model& model, ConversionData& data) |
| 446 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 447 | ALOGV("hal_1_0::HalPolicy::ConvertFloor()"); |
| 448 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 449 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 450 | if (!input.IsValid()) |
| 451 | { |
| 452 | return Fail("%s: Operation has invalid inputs", __func__); |
| 453 | } |
| 454 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 455 | const Operand* const outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 456 | if (!outputOperand) |
| 457 | { |
| 458 | return Fail("%s: Operation has invalid outputs", __func__); |
| 459 | } |
| 460 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 461 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
| 462 | if (IsDynamicTensor(outputInfo)) |
| 463 | { |
| 464 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 465 | } |
| 466 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 467 | bool isSupported = false; |
| 468 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 469 | IsFloorSupported, |
| 470 | data.m_Backends, |
| 471 | isSupported, |
| 472 | input.GetTensorInfo(), |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 473 | outputInfo); |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 474 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 475 | { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | armnn::IConnectableLayer* layer = data.m_Network->AddFloorLayer(); |
| 480 | assert(layer != nullptr); |
| 481 | input.Connect(layer->GetInputSlot(0)); |
| 482 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 483 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | bool HalPolicy::ConvertFullyConnected(const Operation& operation, const Model& model, ConversionData& data) |
| 487 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 488 | ALOGV("hal_1_0::HalPolicy::ConvertFullyConnected()"); |
| 489 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 490 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 491 | if (!input.IsValid()) |
| 492 | { |
| 493 | return Fail("%s: Operation has invalid inputs", __func__); |
| 494 | } |
| 495 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 496 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 497 | if (!output) |
| 498 | { |
| 499 | return Fail("%s: Could not read output 0", __func__); |
| 500 | } |
| 501 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 502 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 503 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
FinnWilliamsArm | 92ec725 | 2019-07-16 12:15:18 +0100 | [diff] [blame] | 504 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 505 | if (IsDynamicTensor(outputInfo)) |
FinnWilliamsArm | 92ec725 | 2019-07-16 12:15:18 +0100 | [diff] [blame] | 506 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 507 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
FinnWilliamsArm | 92ec725 | 2019-07-16 12:15:18 +0100 | [diff] [blame] | 508 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 509 | |
| 510 | // ArmNN does not currently support non-fixed weights or bias |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 511 | ConstTensorPin weightsPin = |
| 512 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 1, model, data); // 2D |
| 513 | ConstTensorPin biasPin = |
| 514 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 2, model, data); // 1D |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 515 | |
| 516 | if (!weightsPin.IsValid() || !biasPin.IsValid()) |
| 517 | { |
| 518 | return Fail("%s: Operation has invalid inputs", __func__); |
| 519 | } |
| 520 | |
| 521 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 522 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 523 | armnn::TensorInfo reshapedInfo = inputInfo; |
Matthew Bentham | f61c270 | 2019-04-23 16:43:27 +0100 | [diff] [blame] | 524 | |
| 525 | try |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 526 | { |
Matthew Bentham | f61c270 | 2019-04-23 16:43:27 +0100 | [diff] [blame] | 527 | reshapedInfo.SetShape(FlattenFullyConnectedInput(inputInfo.GetShape(), weights.GetInfo().GetShape())); |
| 528 | } catch (const std::exception &e) { |
| 529 | return Fail("%s: %s", __func__, e.what()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // ensuring that the bias value is within 1% of the weights input (small float differences can exist) |
| 533 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), reshapedInfo); |
| 534 | |
| 535 | ActivationFn activationFunction; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 536 | if (!GetInputActivationFunction<hal_1_0::HalPolicy>(operation, 3, activationFunction, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 537 | { |
| 538 | return Fail("%s: Operation has invalid inputs", __func__); |
| 539 | } |
| 540 | |
| 541 | armnn::FullyConnectedDescriptor desc; |
| 542 | desc.m_TransposeWeightMatrix = true; |
| 543 | desc.m_BiasEnabled = true; |
| 544 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 545 | bool isSupported = false; |
| 546 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 547 | IsFullyConnectedSupported, |
| 548 | data.m_Backends, |
| 549 | isSupported, |
| 550 | reshapedInfo, |
| 551 | outputInfo, |
| 552 | weights.GetInfo(), |
| 553 | bias.GetInfo(), |
| 554 | desc); |
| 555 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 556 | { |
| 557 | return false; |
| 558 | } |
| 559 | |
Matteo Martincigh | ba01f37 | 2019-05-14 13:28:21 +0100 | [diff] [blame] | 560 | armnn::IConnectableLayer* startLayer = |
| 561 | data.m_Network->AddFullyConnectedLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 562 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data); |
| 563 | |
| 564 | if (endLayer != nullptr) |
| 565 | { |
| 566 | if (inputInfo.GetNumDimensions() > 2U) |
| 567 | { |
| 568 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 569 | reshapeDescriptor.m_TargetShape = reshapedInfo.GetShape(); |
| 570 | |
| 571 | armnn::IConnectableLayer* reshapeLayer = data.m_Network->AddReshapeLayer(reshapeDescriptor); |
| 572 | assert(reshapeLayer != nullptr); |
| 573 | input.Connect(reshapeLayer->GetInputSlot(0)); |
| 574 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedInfo); |
| 575 | reshapeLayer->GetOutputSlot(0).Connect(startLayer->GetInputSlot(0)); |
| 576 | } |
| 577 | else |
| 578 | { |
| 579 | input.Connect(startLayer->GetInputSlot(0)); |
| 580 | } |
| 581 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 582 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *endLayer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 583 | } |
| 584 | else |
| 585 | { |
| 586 | return Fail("%s: ProcessActivation failed", __func__); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | bool HalPolicy::ConvertLocalResponseNormalization(const Operation& operation, |
| 591 | const Model& model, |
| 592 | ConversionData& data) |
| 593 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 594 | ALOGV("hal_1_0::HalPolicy::ConvertLocalResponseNormalization()"); |
| 595 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 596 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 597 | if (!input.IsValid()) |
| 598 | { |
| 599 | return Fail("%s: Operation has invalid inputs", __func__); |
| 600 | } |
| 601 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 602 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 603 | if (!output) |
| 604 | { |
| 605 | return Fail("%s: Could not read output 0", __func__); |
| 606 | } |
| 607 | |
narpra01 | 2fb804a | 2018-10-22 14:52:32 +0100 | [diff] [blame] | 608 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 609 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 610 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 611 | armnn::NormalizationDescriptor descriptor; |
| 612 | |
narpra01 | 2fb804a | 2018-10-22 14:52:32 +0100 | [diff] [blame] | 613 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 614 | descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across; |
narpra01 | 2fb804a | 2018-10-22 14:52:32 +0100 | [diff] [blame] | 615 | descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 616 | |
| 617 | if (!input.IsValid() || |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 618 | !GetInputScalar<hal_1_0::HalPolicy>(operation, 1, OperandType::INT32, descriptor.m_NormSize, model, data) || |
| 619 | !GetInputFloat32<hal_1_0::HalPolicy>(operation, 2, descriptor.m_K, model, data) || |
| 620 | !GetInputFloat32<hal_1_0::HalPolicy>(operation, 3, descriptor.m_Alpha, model, data) || |
| 621 | !GetInputFloat32<hal_1_0::HalPolicy>(operation, 4, descriptor.m_Beta, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 622 | { |
| 623 | return Fail("%s: Operation has invalid inputs", __func__); |
| 624 | } |
| 625 | |
| 626 | // ArmNN expects normSize to be the full size of the normalization |
| 627 | // window rather than the radius as in AndroidNN. |
| 628 | descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize); |
| 629 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 630 | bool isSupported = false; |
| 631 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 632 | IsNormalizationSupported, |
| 633 | data.m_Backends, |
| 634 | isSupported, |
| 635 | inputInfo, |
| 636 | outputInfo, |
| 637 | descriptor); |
| 638 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 639 | { |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | armnn::IConnectableLayer* layer = data.m_Network->AddNormalizationLayer(descriptor); |
| 645 | assert(layer != nullptr); |
narpra01 | 2fb804a | 2018-10-22 14:52:32 +0100 | [diff] [blame] | 646 | input.Connect(layer->GetInputSlot(0)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 647 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 648 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | bool HalPolicy::ConvertLogistic(const Operation& operation, const Model& model, ConversionData& data) |
| 652 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 653 | ALOGV("hal_1_0::HalPolicy::ConvertLogistic()"); |
| 654 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 655 | armnn::ActivationDescriptor desc; |
| 656 | desc.m_Function = armnn::ActivationFunction::Sigmoid; |
| 657 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 658 | return ConvertToActivation<hal_1_0::HalPolicy>(operation, __func__, desc, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | bool HalPolicy::ConvertLstm(const Operation& operation, const Model& model, ConversionData& data) |
| 662 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 663 | ALOGV("hal_1_0::HalPolicy::ConvertLstm()"); |
| 664 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 665 | // Inputs: |
| 666 | // 00: The input: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, input_size], where |
| 667 | // “batch_size” corresponds to the batching dimension, and “input_size” is the size of the input. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 668 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 669 | if (!input.IsValid()) |
| 670 | { |
| 671 | return Fail("%s: Could not read input 0: input", __func__); |
| 672 | } |
| 673 | // 18: The output state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 674 | LayerInputHandle outputStateIn = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 18, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 675 | if (!outputStateIn.IsValid()) |
| 676 | { |
| 677 | return Fail("%s: Could not read input 18: outputStateIn", __func__); |
| 678 | } |
| 679 | // 19: The cell state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 680 | LayerInputHandle cellStateIn = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 19, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 681 | if (!cellStateIn.IsValid()) |
| 682 | { |
| 683 | return Fail("%s: Could not read input 19: cellStateIn", __func__); |
| 684 | } |
| 685 | |
| 686 | // Get the mandatory input tensors: |
| 687 | // 02: The input-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 688 | // [num_units, input_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 689 | const ConstTensorPin inputToForgetWeightsPin = |
| 690 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 2, model, data); |
| 691 | // 03: The input-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 692 | // [num_units, input_size]. |
| 693 | const ConstTensorPin inputToCellWeightsPin = |
| 694 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 3, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 695 | // 04: The input-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 696 | // [num_units, input_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 697 | const ConstTensorPin inputToOutputWeightsPin = |
| 698 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 4, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 699 | // 06: The recurrent-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 700 | // [num_units, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 701 | const ConstTensorPin recurrentToForgetWeightsPin = |
| 702 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 6, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 703 | // 07: The recurrent-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 704 | // [num_units, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 705 | const ConstTensorPin recurrentToCellWeightsPin = |
| 706 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 7, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 707 | // 08: The recurrent-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 708 | // [num_units, output_size]. |
| 709 | const ConstTensorPin recurrentToOutputWeightsPin = |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 710 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 8, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 711 | // 13: The forget gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 712 | const ConstTensorPin forgetGateBiasPin = |
| 713 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 13, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 714 | // 14: The cell bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 715 | const ConstTensorPin cellBiasPin = |
| 716 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 14, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 717 | // 15: The output gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 718 | const ConstTensorPin outputGateBiasPin = |
| 719 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, 15, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 720 | |
| 721 | if (!inputToForgetWeightsPin.IsValid() || |
| 722 | !inputToCellWeightsPin.IsValid() || |
| 723 | !inputToOutputWeightsPin.IsValid() || |
| 724 | !recurrentToForgetWeightsPin.IsValid() || |
| 725 | !recurrentToCellWeightsPin.IsValid() || |
| 726 | !recurrentToOutputWeightsPin.IsValid() || |
| 727 | !forgetGateBiasPin.IsValid() || |
| 728 | !cellBiasPin.IsValid() || |
| 729 | !outputGateBiasPin.IsValid()) |
| 730 | { |
| 731 | return Fail("%s: Operation has invalid tensor inputs", __func__); |
| 732 | } |
| 733 | |
| 734 | // Get the optional input tensors: |
| 735 | // 01: The input-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 736 | // [num_units, input_size], where “num_units” corresponds to the number of cell units. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 737 | const ConstTensorPin inputToInputWeightsPin = |
| 738 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 739 | 1, |
| 740 | model, |
| 741 | data, |
| 742 | g_DontPermute, |
| 743 | nullptr, |
| 744 | true); |
| 745 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 746 | // 05: The recurrent-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 747 | // [num_units, output_size], where “output_size” corresponds to either the number of cell units (i.e., |
| 748 | // “num_units”), or the second dimension of the “projection_weights”, if defined. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 749 | const ConstTensorPin recurrentToInputWeightsPin = |
| 750 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 751 | 5, |
| 752 | model, |
| 753 | data, |
| 754 | g_DontPermute, |
| 755 | nullptr, |
| 756 | true); |
| 757 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 758 | // 09: The cell-to-input weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 759 | const ConstTensorPin cellToInputWeightsPin = |
| 760 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 761 | 9, |
| 762 | model, |
| 763 | data, |
| 764 | g_DontPermute, |
| 765 | nullptr, |
| 766 | true); |
| 767 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 768 | // 10: The cell-to-forget weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 769 | const ConstTensorPin cellToForgetWeightsPin = |
| 770 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 771 | 10, |
| 772 | model, |
| 773 | data, |
| 774 | g_DontPermute, |
| 775 | nullptr, |
| 776 | true); |
| 777 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 778 | // 11: The cell-to-output weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 779 | const ConstTensorPin cellToOutputWeightsPin = |
| 780 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 781 | 11, |
| 782 | model, |
| 783 | data, |
| 784 | g_DontPermute, |
| 785 | nullptr, |
| 786 | true); |
| 787 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 788 | // 12: The input gate bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 789 | const ConstTensorPin inputGateBiasPin = |
| 790 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 791 | 12, |
| 792 | model, |
| 793 | data, |
| 794 | g_DontPermute, |
| 795 | nullptr, |
| 796 | true); |
| 797 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 798 | // 16: The projection weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 799 | // [output_size, num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 800 | const ConstTensorPin projectionWeightsPin = |
| 801 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 802 | 16, |
| 803 | model, |
| 804 | data, |
| 805 | g_DontPermute, |
| 806 | nullptr, |
| 807 | true); |
| 808 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 809 | // 17: The projection bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 810 | const ConstTensorPin projectionBiasPin = |
| 811 | ConvertOperationInputToConstTensorPin<hal_1_0::HalPolicy>(operation, |
| 812 | 17, |
| 813 | model, |
| 814 | data, |
| 815 | g_DontPermute, |
| 816 | nullptr, |
| 817 | true); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 818 | |
| 819 | if ((!inputToInputWeightsPin.IsValid() && !inputToInputWeightsPin.IsOptional()) || |
| 820 | (!recurrentToInputWeightsPin.IsValid() && !recurrentToInputWeightsPin.IsOptional()) || |
| 821 | (!cellToInputWeightsPin.IsValid() && !cellToInputWeightsPin.IsOptional()) || |
| 822 | (!cellToForgetWeightsPin.IsValid() && !cellToForgetWeightsPin.IsOptional()) || |
| 823 | (!cellToOutputWeightsPin.IsValid() && !cellToOutputWeightsPin.IsOptional()) || |
| 824 | (!inputGateBiasPin.IsValid() && !inputGateBiasPin.IsOptional()) || |
| 825 | (!projectionWeightsPin.IsValid() && !projectionWeightsPin.IsOptional()) || |
| 826 | (!projectionBiasPin.IsValid() && !projectionBiasPin.IsOptional())) |
| 827 | { |
| 828 | return Fail("%s: Operation has invalid tensor inputs", __func__); |
| 829 | } |
| 830 | |
| 831 | // Get the mandatory input scalars (actually 1-D tensors of size 1): |
| 832 | // 20: The activation function: A value indicating the activation function: |
| 833 | // 0: None; 1: Relu; 3: Relu6; 4: Tanh; 6: Sigmoid. |
| 834 | // 21: The clipping threshold: for the cell state, such that values are bound within [-cell_clip, cell_clip]. |
| 835 | // If set to 0.0 then clipping is disabled. |
| 836 | // 22: The clipping threshold: for the output from the projection layer, such that values are bound within |
| 837 | // [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. |
| 838 | ActivationFn activation; |
| 839 | float cellClip; |
| 840 | float projClip; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 841 | if (!GetInputActivationFunctionFromTensor<hal_1_0::HalPolicy>(operation, 20, activation, model, data) || |
| 842 | !GetInputScalar<hal_1_0::HalPolicy>(operation, 21, OperandType::FLOAT32, cellClip, model, data) || |
| 843 | !GetInputScalar<hal_1_0::HalPolicy>(operation, 22, OperandType::FLOAT32, projClip, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 844 | { |
| 845 | return Fail("%s: Operation has invalid scalar inputs", __func__); |
| 846 | } |
| 847 | |
| 848 | // Outputs: |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 849 | // 00: The scratch buffer: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units * 4] |
| 850 | // with CIFG, or [batch_size, num_units * 3] without CIFG. |
| 851 | const Operand* scratchBuffer = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 852 | if (!scratchBuffer) |
| 853 | { |
| 854 | return Fail("%s: Could not read output 0: scratchBuffer", __func__); |
| 855 | } |
| 856 | // 01: The output state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 857 | const Operand* outputStateOut = GetOutputOperand<hal_1_0::HalPolicy>(operation, 1, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 858 | if (!outputStateOut) |
| 859 | { |
| 860 | return Fail("%s: Could not read output 1: outputStateOut", __func__); |
| 861 | } |
| 862 | // 02: The cell state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units]. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 863 | const Operand* cellStateOut = GetOutputOperand<hal_1_0::HalPolicy>(operation, 2, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 864 | if (!cellStateOut) |
| 865 | { |
| 866 | return Fail("%s: Could not read output 2: cellStateOut", __func__); |
| 867 | } |
| 868 | // 03: The output: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. This is |
| 869 | // effectively the same as the current “output state (out)” value. |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 870 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 3, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 871 | if (!output) |
| 872 | { |
| 873 | return Fail("%s: Could not read output 3: output", __func__); |
| 874 | } |
| 875 | |
| 876 | // set the params structure for the AddLstmLayer call |
| 877 | armnn::LstmInputParams params; |
| 878 | params.m_InputToInputWeights = inputToInputWeightsPin.GetConstTensorPtr(); |
| 879 | params.m_InputToForgetWeights = inputToForgetWeightsPin.GetConstTensorPtr(); |
| 880 | params.m_InputToCellWeights = inputToCellWeightsPin.GetConstTensorPtr(); |
| 881 | params.m_InputToOutputWeights = inputToOutputWeightsPin.GetConstTensorPtr(); |
| 882 | params.m_RecurrentToInputWeights = recurrentToInputWeightsPin.GetConstTensorPtr(); |
| 883 | params.m_RecurrentToForgetWeights = recurrentToForgetWeightsPin.GetConstTensorPtr(); |
| 884 | params.m_RecurrentToCellWeights = recurrentToCellWeightsPin.GetConstTensorPtr(); |
| 885 | params.m_RecurrentToOutputWeights = recurrentToOutputWeightsPin.GetConstTensorPtr(); |
| 886 | params.m_CellToInputWeights = cellToInputWeightsPin.GetConstTensorPtr(); |
| 887 | params.m_CellToForgetWeights = cellToForgetWeightsPin.GetConstTensorPtr(); |
| 888 | params.m_CellToOutputWeights = cellToOutputWeightsPin.GetConstTensorPtr(); |
| 889 | params.m_InputGateBias = inputGateBiasPin.GetConstTensorPtr(); |
| 890 | params.m_ForgetGateBias = forgetGateBiasPin.GetConstTensorPtr(); |
| 891 | params.m_CellBias = cellBiasPin.GetConstTensorPtr(); |
| 892 | params.m_OutputGateBias = outputGateBiasPin.GetConstTensorPtr(); |
| 893 | params.m_ProjectionWeights = projectionWeightsPin.GetConstTensorPtr(); |
| 894 | params.m_ProjectionBias = projectionBiasPin.GetConstTensorPtr(); |
| 895 | |
| 896 | // set the layer descriptor |
| 897 | armnn::LstmDescriptor desc; |
| 898 | desc.m_ActivationFunc = activation; |
| 899 | desc.m_ClippingThresCell = cellClip; |
| 900 | desc.m_ClippingThresProj = projClip; |
| 901 | desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr || |
| 902 | params.m_RecurrentToInputWeights == nullptr || |
| 903 | params.m_InputGateBias == nullptr); |
| 904 | desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || |
| 905 | params.m_CellToOutputWeights != nullptr); |
| 906 | desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr); |
| 907 | |
| 908 | // validate the optional input groups |
| 909 | if (desc.m_CifgEnabled && |
| 910 | (params.m_InputToInputWeights != nullptr || |
| 911 | params.m_RecurrentToInputWeights != nullptr || |
| 912 | params.m_InputGateBias != nullptr)) |
| 913 | { |
| 914 | return Fail("%s: All, or none, of input-to-input weights, recurrent-to-input weights," |
| 915 | " and input gate bias must be provided", __func__); |
| 916 | } |
| 917 | |
| 918 | if (!desc.m_ProjectionEnabled && params.m_ProjectionBias != nullptr) |
| 919 | { |
| 920 | return Fail("%s: projection bias should not be provided without projection weights", __func__); |
| 921 | } |
| 922 | |
| 923 | if (desc.m_PeepholeEnabled && |
| 924 | (params.m_CellToForgetWeights == nullptr || |
| 925 | params.m_CellToOutputWeights == nullptr || |
| 926 | (!desc.m_CifgEnabled && params.m_CellToInputWeights == nullptr))) |
| 927 | { |
| 928 | return Fail("%s: All, or none, of cell-to-forget weights and cell-to-output weights must be provided" |
| 929 | " and, if CIFG is not enabled, cell-to-input weights must also be provided", __func__); |
| 930 | } |
| 931 | |
| 932 | // Check if the layer is supported |
| 933 | // Inputs |
| 934 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 935 | const armnn::TensorInfo& outputStateInInfo = outputStateIn.GetTensorInfo(); |
| 936 | const armnn::TensorInfo& cellStateInInfo = cellStateIn.GetTensorInfo(); |
| 937 | |
| 938 | // Outputs |
| 939 | const armnn::TensorInfo& scratchBufferInfo = GetTensorInfoForOperand(*scratchBuffer); |
| 940 | const armnn::TensorInfo& outputStateOutInfo = GetTensorInfoForOperand(*outputStateOut); |
| 941 | const armnn::TensorInfo& cellStateOutInfo = GetTensorInfoForOperand(*cellStateOut); |
| 942 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 943 | |
| 944 | // Basic parameters |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 945 | armnn::LstmInputParamsInfo paramsInfo; |
| 946 | paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo()); |
| 947 | paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo()); |
| 948 | paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo()); |
| 949 | paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo()); |
| 950 | paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo()); |
| 951 | paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo()); |
| 952 | paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo()); |
| 953 | paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo()); |
| 954 | paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 955 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 956 | // Optional parameters |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 957 | if(!desc.m_CifgEnabled) |
| 958 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 959 | paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo()); |
| 960 | paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 961 | if (params.m_CellToInputWeights != nullptr) |
| 962 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 963 | paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 964 | } |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 965 | paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if(desc.m_ProjectionEnabled) |
| 969 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 970 | paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 971 | if (params.m_ProjectionBias != nullptr) |
| 972 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 973 | paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | if(desc.m_PeepholeEnabled) |
| 978 | { |
Ferran Balaguer | 177fa0b | 2019-07-02 17:34:46 +0100 | [diff] [blame] | 979 | paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo()); |
| 980 | paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo()); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 981 | } |
| 982 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 983 | bool isSupported = false; |
| 984 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 985 | IsLstmSupported, |
| 986 | data.m_Backends, |
| 987 | isSupported, |
| 988 | inputInfo, |
| 989 | outputStateInInfo, |
| 990 | cellStateInInfo, |
| 991 | scratchBufferInfo, |
| 992 | outputStateOutInfo, |
| 993 | cellStateOutInfo, |
| 994 | outputInfo, |
| 995 | desc, |
| 996 | paramsInfo); |
| 997 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 998 | { |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | // Add the layer |
| 1003 | armnn::IConnectableLayer* layer = data.m_Network->AddLstmLayer(desc, params, "Lstm"); |
| 1004 | |
| 1005 | input.Connect(layer->GetInputSlot(0)); |
| 1006 | outputStateIn.Connect(layer->GetInputSlot(1)); |
| 1007 | cellStateIn.Connect(layer->GetInputSlot(2)); |
| 1008 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1009 | return (SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, 0, model, data) && |
| 1010 | SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 1, *layer, 1, model, data) && |
| 1011 | SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 2, *layer, 2, model, data) && |
| 1012 | SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 3, *layer, 3, model, data)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | bool HalPolicy::ConvertL2Normalization(const Operation& operation, const Model& model, ConversionData& data) |
| 1016 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1017 | ALOGV("hal_1_0::HalPolicy::ConvertL2Normalization()"); |
| 1018 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1019 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1020 | if (!input.IsValid()) |
| 1021 | { |
| 1022 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1023 | } |
| 1024 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1025 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1026 | if (!output) |
| 1027 | { |
| 1028 | return Fail("%s: Could not read output 0", __func__); |
| 1029 | } |
| 1030 | |
| 1031 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1032 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1033 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 1034 | if (IsDynamicTensor(outputInfo)) |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 1035 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1036 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 1037 | } |
| 1038 | |
Matteo Martincigh | 58f7109 | 2018-09-25 15:58:52 +0100 | [diff] [blame] | 1039 | armnn::L2NormalizationDescriptor desc; |
Matteo Martincigh | 5e0ed9f | 2018-10-01 09:26:32 +0100 | [diff] [blame] | 1040 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Matteo Martincigh | 58f7109 | 2018-09-25 15:58:52 +0100 | [diff] [blame] | 1041 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1042 | bool isSupported = false; |
| 1043 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1044 | IsL2NormalizationSupported, |
| 1045 | data.m_Backends, |
| 1046 | isSupported, |
| 1047 | inputInfo, |
| 1048 | outputInfo, |
| 1049 | desc); |
| 1050 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1051 | { |
| 1052 | return false; |
| 1053 | } |
| 1054 | |
Matteo Martincigh | 58f7109 | 2018-09-25 15:58:52 +0100 | [diff] [blame] | 1055 | armnn::IConnectableLayer* layer = data.m_Network->AddL2NormalizationLayer(desc); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1056 | assert(layer != nullptr); |
Matteo Martincigh | 5e0ed9f | 2018-10-01 09:26:32 +0100 | [diff] [blame] | 1057 | input.Connect(layer->GetInputSlot(0)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1058 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1059 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | bool HalPolicy::ConvertL2Pool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 1063 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1064 | ALOGV("hal_1_0::HalPolicy::ConvertL2Pool2d()"); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1065 | return ConvertPooling2d<hal_1_0::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::L2, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | bool HalPolicy::ConvertMaxPool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 1069 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1070 | ALOGV("hal_1_0::HalPolicy::ConvertMaxPool2d()"); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1071 | return ConvertPooling2d<hal_1_0::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Max, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | bool HalPolicy::ConvertMul(const Operation& operation, const Model& model, ConversionData& data) |
| 1075 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1076 | ALOGV("hal_1_0::HalPolicy::ConvertMul()"); |
| 1077 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1078 | LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
| 1079 | LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 1, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1080 | |
| 1081 | if (!input0.IsValid() || !input1.IsValid()) |
| 1082 | { |
| 1083 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1084 | } |
| 1085 | |
| 1086 | // The FuseActivation parameter is always the input index 2 |
| 1087 | // and it should be optional |
| 1088 | ActivationFn activationFunction; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1089 | if (!GetOptionalInputActivation<hal_1_0::HalPolicy>(operation, 2, activationFunction, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1090 | { |
| 1091 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1092 | } |
| 1093 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1094 | const Operand* outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1095 | |
| 1096 | if (outputOperand == nullptr) |
| 1097 | { |
| 1098 | return false; |
| 1099 | } |
| 1100 | |
| 1101 | const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand); |
| 1102 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1103 | bool isSupported = false; |
| 1104 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1105 | IsMultiplicationSupported, |
| 1106 | data.m_Backends, |
| 1107 | isSupported, |
| 1108 | input0.GetTensorInfo(), |
| 1109 | input1.GetTensorInfo(), |
| 1110 | outInfo); |
| 1111 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1112 | { |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | armnn::IConnectableLayer* const startLayer = data.m_Network->AddMultiplicationLayer(); |
| 1117 | armnn::IConnectableLayer* const endLayer = ProcessActivation(outInfo, activationFunction, startLayer, data); |
| 1118 | |
| 1119 | const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo(); |
| 1120 | const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo(); |
| 1121 | |
| 1122 | if (endLayer != nullptr) |
| 1123 | { |
| 1124 | BroadcastTensor(input0, input1, startLayer, *data.m_Network); |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1125 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *endLayer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1126 | } |
| 1127 | else |
| 1128 | { |
| 1129 | return Fail("%s: ProcessActivation failed", __func__); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | bool HalPolicy::ConvertReLu(const Operation& operation, const Model& model, ConversionData& data) |
| 1134 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1135 | ALOGV("hal_1_0::HalPolicy::ConvertReLu()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 1136 | return ::ConvertReLu<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | bool HalPolicy::ConvertReLu1(const Operation& operation, const Model& model, ConversionData& data) |
| 1140 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1141 | ALOGV("hal_1_0::HalPolicy::ConvertReLu1()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 1142 | return ::ConvertReLu1<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | bool HalPolicy::ConvertReLu6(const Operation& operation, const Model& model, ConversionData& data) |
| 1146 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1147 | ALOGV("hal_1_0::HalPolicy::ConvertReLu6()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 1148 | return ::ConvertReLu6<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | bool HalPolicy::ConvertSoftmax(const Operation& operation, const Model& model, ConversionData& data) |
| 1152 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1153 | ALOGV("hal_1_0::HalPolicy::ConvertSoftmax()"); |
| 1154 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1155 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1156 | if (!input.IsValid()) |
| 1157 | { |
| 1158 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1159 | } |
| 1160 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1161 | const Operand* outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1162 | if (!outputOperand) |
| 1163 | { |
| 1164 | return Fail("%s: Operation has no outputs", __func__); |
| 1165 | } |
| 1166 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1167 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 1168 | if (IsDynamicTensor(outputInfo)) |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 1169 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1170 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 1171 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1172 | |
| 1173 | armnn::SoftmaxDescriptor desc; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1174 | if (!GetInputFloat32<hal_1_0::HalPolicy>(operation, 1, desc.m_Beta, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1175 | { |
| 1176 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1177 | } |
| 1178 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1179 | bool isSupported = false; |
| 1180 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1181 | IsSoftmaxSupported, |
| 1182 | data.m_Backends, |
| 1183 | isSupported, |
| 1184 | input.GetTensorInfo(), |
| 1185 | outputInfo, |
| 1186 | desc); |
| 1187 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1188 | { |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | armnn::IConnectableLayer* layer = data.m_Network->AddSoftmaxLayer(desc); |
| 1193 | assert(layer != nullptr); |
| 1194 | input.Connect(layer->GetInputSlot(0)); |
| 1195 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1196 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 1199 | bool HalPolicy::ConvertSpaceToDepth(const Operation& operation, const Model& model, ConversionData& data) |
| 1200 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1201 | ALOGV("hal_1_0::HalPolicy::ConvertSpaceToDepth()"); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 1202 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1203 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 1204 | if (!input.IsValid() ) |
| 1205 | { |
| 1206 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1207 | } |
| 1208 | |
| 1209 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1210 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 1211 | |
| 1212 | if (rank != 4) |
| 1213 | { |
| 1214 | return Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 1215 | } |
| 1216 | |
| 1217 | armnn::SpaceToDepthDescriptor desc; |
| 1218 | bool dataLayoutCheck; |
| 1219 | |
| 1220 | GetInputScalar<hal_1_0::HalPolicy>(operation, 1, OperandType::INT32, desc.m_BlockSize, model, data); |
| 1221 | |
| 1222 | if (desc.m_BlockSize <= 1) |
| 1223 | { |
| 1224 | return Fail("%s: Block size must be at least 1 in all dimensions"); |
| 1225 | } |
| 1226 | |
| 1227 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
| 1228 | if (!output) |
| 1229 | { |
| 1230 | return Fail("%s: Could not read output 0", __func__); |
| 1231 | } |
| 1232 | |
| 1233 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1234 | |
| 1235 | bool isSupported = false; |
| 1236 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1237 | IsSpaceToDepthSupported, |
| 1238 | data.m_Backends, |
| 1239 | isSupported, |
| 1240 | inputInfo, |
| 1241 | outputInfo, |
| 1242 | desc); |
| 1243 | if (!isSupported) |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 1244 | { |
| 1245 | return false; |
| 1246 | } |
| 1247 | |
| 1248 | armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToDepthLayer(desc); |
| 1249 | assert(layer != nullptr); |
| 1250 | input.Connect(layer->GetInputSlot(0)); |
| 1251 | |
| 1252 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
| 1253 | } |
| 1254 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1255 | bool HalPolicy::ConvertTanH(const Operation& operation, const Model& model, ConversionData& data) |
| 1256 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1257 | ALOGV("hal_1_0::HalPolicy::ConvertTanH()"); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 1258 | return ::ConvertTanH<hal_1_0::HalPolicy>(operation, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | bool HalPolicy::ConvertReshape(const Operation& operation, const Model& model, ConversionData& data) |
| 1262 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1263 | ALOGV("hal_1_0::HalPolicy::ConvertReshape()"); |
| 1264 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1265 | const Operand* inputOperand = GetInputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
| 1266 | const Operand* requestedShapeOperand = GetInputOperand<hal_1_0::HalPolicy>(operation, 1, model); |
| 1267 | const Operand* outputOperand = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1268 | |
| 1269 | if (inputOperand == nullptr |
| 1270 | || requestedShapeOperand == nullptr |
| 1271 | || outputOperand == nullptr) |
| 1272 | { |
| 1273 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | if (requestedShapeOperand->dimensions.size() != 1) |
| 1278 | { |
| 1279 | return Fail("%s: Input 1 expected to be one-dimensional (found %i dimensions)", |
| 1280 | __func__, requestedShapeOperand->dimensions.size()); |
| 1281 | } |
| 1282 | |
| 1283 | std::vector<int32_t> targetDimensions; |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1284 | if (!GetTensorInt32Values<hal_1_0::HalPolicy>(*requestedShapeOperand, targetDimensions, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1285 | { |
| 1286 | return Fail("%s: Could not read values of input 1", __func__); |
| 1287 | } |
| 1288 | |
| 1289 | const Shape inputOperandShape = GetOperandShape(*inputOperand); |
| 1290 | |
| 1291 | Shape requestedShape; |
| 1292 | // targetDimensions may contain special values (e.g. -1). reshapePrepare() is an AndroidNN provided utility |
| 1293 | // function that resolves these values into a fully specified tensor shape. |
| 1294 | if (!reshapePrepare(inputOperandShape, targetDimensions.data(), targetDimensions.size(), &requestedShape)) |
| 1295 | { |
| 1296 | return Fail("%s: Failed to resolve the requested shape", __func__); |
| 1297 | } |
| 1298 | |
| 1299 | const Shape outputOperandShape = GetOperandShape(*outputOperand); |
| 1300 | if (!SameShape(requestedShape, outputOperandShape)) |
| 1301 | { |
| 1302 | return Fail("%s: Shape of output operand does not match resolved requested shape", __func__); |
| 1303 | } |
| 1304 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1305 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1306 | if (!input.IsValid()) |
| 1307 | { |
| 1308 | return Fail("%s: Could not read input 0", __func__); |
| 1309 | } |
| 1310 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1311 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 1312 | reshapeDescriptor.m_TargetShape = armnn::TensorShape(requestedShape.dimensions.size(), |
| 1313 | requestedShape.dimensions.data()); |
| 1314 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1315 | bool isSupported = false; |
| 1316 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1317 | IsReshapeSupported, |
| 1318 | data.m_Backends, |
| 1319 | isSupported, |
| 1320 | input.GetTensorInfo(), |
| 1321 | reshapeDescriptor); |
| 1322 | if (!isSupported) |
Matteo Martincigh | 265d1ad | 2019-01-08 18:14:53 +0000 | [diff] [blame] | 1323 | { |
| 1324 | return false; |
| 1325 | } |
| 1326 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1327 | armnn::IConnectableLayer* layer = data.m_Network->AddReshapeLayer(reshapeDescriptor); |
| 1328 | assert(layer != nullptr); |
| 1329 | input.Connect(layer->GetInputSlot(0)); |
| 1330 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1331 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | bool HalPolicy::ConvertResizeBilinear(const Operation& operation, const Model& model, ConversionData& data) |
| 1335 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 1336 | ALOGV("hal_1_0::HalPolicy::ConvertResizeBilinear()"); |
| 1337 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1338 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_0::HalPolicy>(operation, 0, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1339 | if (!input.IsValid()) |
| 1340 | { |
| 1341 | return Fail("%s: Could not read input 0", __func__); |
| 1342 | } |
| 1343 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1344 | const Operand* output = GetOutputOperand<hal_1_0::HalPolicy>(operation, 0, model); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1345 | if (!output) |
| 1346 | { |
| 1347 | return Fail("%s: Could not read output 0", __func__); |
| 1348 | } |
| 1349 | |
| 1350 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1351 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 1352 | |
Aron Virginas-Tar | a5daf86 | 2019-07-01 19:07:20 +0100 | [diff] [blame] | 1353 | armnn::ResizeDescriptor desc; |
| 1354 | desc.m_Method = armnn::ResizeMethod::Bilinear; |
Mohamed Nour Abouelseoud | 81afa30 | 2018-10-29 14:32:55 +0000 | [diff] [blame] | 1355 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1356 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 1357 | bool isSupported = false; |
| 1358 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1359 | IsResizeSupported, |
| 1360 | data.m_Backends, |
| 1361 | isSupported, |
| 1362 | inputInfo, |
| 1363 | outputInfo, |
| 1364 | desc); |
| 1365 | if (!isSupported) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1366 | { |
| 1367 | return false; |
| 1368 | } |
| 1369 | |
Aron Virginas-Tar | 535607d | 2019-07-03 15:46:15 +0100 | [diff] [blame] | 1370 | if (!GetInputScalar<hal_1_0::HalPolicy>(operation, 1, OperandType::INT32, desc.m_TargetWidth, model, data) || |
| 1371 | !GetInputScalar<hal_1_0::HalPolicy>(operation, 2, OperandType::INT32, desc.m_TargetHeight, model, data)) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1372 | { |
| 1373 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1374 | } |
| 1375 | |
Aron Virginas-Tar | a5daf86 | 2019-07-01 19:07:20 +0100 | [diff] [blame] | 1376 | armnn::IConnectableLayer* layer = data.m_Network->AddResizeLayer(desc); |
Mohamed Nour Abouelseoud | 81afa30 | 2018-10-29 14:32:55 +0000 | [diff] [blame] | 1377 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1378 | assert(layer != nullptr); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1379 | |
Mohamed Nour Abouelseoud | 81afa30 | 2018-10-29 14:32:55 +0000 | [diff] [blame] | 1380 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 1381 | input.Connect(layer->GetInputSlot(0)); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1382 | |
Aron Virginas-Tar | cd700e4 | 2019-06-14 14:54:52 +0100 | [diff] [blame] | 1383 | return SetupAndTrackLayerOutputSlot<hal_1_0::HalPolicy>(operation, 0, *layer, model, data); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 1384 | |
| 1385 | } |
| 1386 | |
| 1387 | } // namespace hal_1_0 |
Matteo Martincigh | 58f7109 | 2018-09-25 15:58:52 +0100 | [diff] [blame] | 1388 | } // namespace armnn_driver |