Mike Kelly | b5fdf38 | 2019-06-11 16:35: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 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 8 | #include "Utils.hpp" |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 9 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 10 | #include "../1.0/HalPolicy.hpp" |
| 11 | #include "../1.1/HalPolicy.hpp" |
| 12 | |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 13 | #include <DataLayoutIndexed.hpp> |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 14 | #include <Half.hpp> |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 15 | |
| 16 | #include <cmath> |
| 17 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 18 | namespace armnn_driver |
| 19 | { |
| 20 | namespace hal_1_2 |
| 21 | { |
| 22 | |
| 23 | bool HandledByV1_0(V1_2::OperationType operationType) |
| 24 | { |
| 25 | switch (static_cast<V1_0::OperationType>(operationType)) |
| 26 | { |
| 27 | case V1_0::OperationType::ADD: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 28 | case V1_0::OperationType::CONCATENATION: |
| 29 | case V1_0::OperationType::DEPTH_TO_SPACE: |
| 30 | case V1_0::OperationType::DEQUANTIZE: |
| 31 | case V1_0::OperationType::EMBEDDING_LOOKUP: |
| 32 | case V1_0::OperationType::FLOOR: |
| 33 | case V1_0::OperationType::FULLY_CONNECTED: |
| 34 | case V1_0::OperationType::HASHTABLE_LOOKUP: |
| 35 | case V1_0::OperationType::L2_NORMALIZATION: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 36 | case V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION: |
| 37 | case V1_0::OperationType::LOGISTIC: |
| 38 | case V1_0::OperationType::LSH_PROJECTION: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 39 | case V1_0::OperationType::MUL: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 40 | case V1_0::OperationType::RESHAPE: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 41 | case V1_0::OperationType::RNN: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 42 | case V1_0::OperationType::SVDF: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 43 | case V1_0::OperationType::OEM_OPERATION: |
| 44 | return true; |
| 45 | default: |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | bool HandledByV1_1(V1_2::OperationType operationType) |
| 51 | { |
| 52 | if (HandledByV1_0(operationType)) |
| 53 | { |
| 54 | return true; |
| 55 | } |
| 56 | switch (static_cast<V1_1::OperationType>(operationType)) |
| 57 | { |
| 58 | case V1_1::OperationType::BATCH_TO_SPACE_ND: |
| 59 | case V1_1::OperationType::DIV: |
| 60 | case V1_1::OperationType::MEAN: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 61 | case V1_1::OperationType::SPACE_TO_BATCH_ND: |
| 62 | case V1_1::OperationType::SQUEEZE: |
| 63 | case V1_1::OperationType::STRIDED_SLICE: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 64 | case V1_1::OperationType::TRANSPOSE: |
| 65 | return true; |
| 66 | default: |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | bool HandledByV1_0(const V1_2::Operation& operation) |
| 72 | { |
| 73 | return HandledByV1_0(operation.type); |
| 74 | } |
| 75 | |
| 76 | bool HandledByV1_1(const V1_2::Operation& operation) |
| 77 | { |
| 78 | return HandledByV1_1(operation.type); |
| 79 | } |
| 80 | |
| 81 | V1_0::OperationType CastToV1_0(V1_2::OperationType type) |
| 82 | { |
| 83 | return static_cast<V1_0::OperationType>(type); |
| 84 | } |
| 85 | |
| 86 | V1_1::OperationType CastToV1_1(V1_2::OperationType type) |
| 87 | { |
| 88 | return static_cast<V1_1::OperationType>(type); |
| 89 | } |
| 90 | |
| 91 | V1_0::Operation ConvertToV1_0(const V1_2::Operation& operation) |
| 92 | { |
| 93 | V1_0::Operation op; |
| 94 | op.type = CastToV1_0(operation.type); |
| 95 | op.inputs = operation.inputs; |
| 96 | op.outputs = operation.outputs; |
| 97 | return op; |
| 98 | } |
| 99 | |
| 100 | V1_1::Operation ConvertToV1_1(const V1_2::Operation& operation) |
| 101 | { |
| 102 | V1_1::Operation op; |
| 103 | op.type = CastToV1_1(operation.type); |
| 104 | op.inputs = operation.inputs; |
| 105 | op.outputs = operation.outputs; |
| 106 | return op; |
| 107 | } |
| 108 | |
| 109 | bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data) |
| 110 | { |
| 111 | if (HandledByV1_0(operation) && compliantWithV1_0(model)) |
| 112 | { |
| 113 | hal_1_0::HalPolicy::Operation v10Operation = ConvertToV1_0(operation); |
| 114 | hal_1_0::HalPolicy::Model v10Model = convertToV1_0(model); |
| 115 | |
| 116 | return hal_1_0::HalPolicy::ConvertOperation(v10Operation, v10Model, data); |
| 117 | } |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 118 | |
| 119 | if (HandledByV1_1(operation) && compliantWithV1_1(model)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 120 | { |
| 121 | hal_1_1::HalPolicy::Operation v11Operation = ConvertToV1_1(operation); |
| 122 | hal_1_1::HalPolicy::Model v11Model = convertToV1_1(model); |
| 123 | |
| 124 | return hal_1_1::HalPolicy::ConvertOperation(v11Operation, v11Model, data); |
| 125 | } |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 126 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 127 | switch (operation.type) |
| 128 | { |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 129 | case V1_2::OperationType::AVERAGE_POOL_2D: |
| 130 | return ConvertAveragePool2d(operation, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 131 | case V1_2::OperationType::CONV_2D: |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 132 | return ConvertConv2d(operation, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 133 | case V1_2::OperationType::DEPTHWISE_CONV_2D: |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 134 | return ConvertDepthwiseConv2d(operation, model, data); |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 135 | case V1_2::OperationType::L2_POOL_2D: |
| 136 | return ConvertL2Pool2d(operation, model, data); |
| 137 | case V1_2::OperationType::MAX_POOL_2D: |
| 138 | return ConvertMaxPool2d(operation, model, data); |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 139 | case V1_2::OperationType::MAXIMUM: |
| 140 | return ConvertMaximum(operation, model, data); |
Ellen Norris-Thompson | 1cb29aa | 2019-07-11 17:27:37 +0100 | [diff] [blame] | 141 | case V1_2::OperationType::MINIMUM: |
| 142 | return ConvertMinimum(operation, model, data); |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 143 | case V1_2::OperationType::PAD: |
Aron Virginas-Tar | c921f6b | 2019-07-25 10:14:33 +0100 | [diff] [blame] | 144 | return ConvertPad(operation, model, data); |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 145 | case V1_2::OperationType::PAD_V2: |
| 146 | return ConvertPadV2(operation, model, data); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 147 | case V1_2::OperationType::PRELU: |
| 148 | return ConvertPrelu(operation, model, data); |
Sadik Armagan | 5a476a8 | 2019-07-30 09:43:18 +0100 | [diff] [blame] | 149 | case V1_2::OperationType::QUANTIZE: |
| 150 | return ConvertQuantize(operation, model, data); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 151 | case V1_2::OperationType::RELU: |
| 152 | return ConvertReLu(operation, model, data); |
| 153 | case V1_2::OperationType::RELU1: |
| 154 | return ConvertReLu1(operation, model, data); |
| 155 | case V1_2::OperationType::RELU6: |
| 156 | return ConvertReLu6(operation, model, data); |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 157 | case V1_2::OperationType::RESIZE_BILINEAR: |
| 158 | return ConvertResize(operation, model, data, armnn::ResizeMethod::Bilinear); |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 159 | case V1_2::OperationType::RESIZE_NEAREST_NEIGHBOR: |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 160 | return ConvertResize(operation, model, data, armnn::ResizeMethod::NearestNeighbor); |
David Monahan | 613b49c | 2019-06-27 11:37:47 +0100 | [diff] [blame] | 161 | case V1_2::OperationType::TRANSPOSE_CONV_2D: |
| 162 | return ConvertTransposeConvolution2d(operation, model, data); |
Francis Murtagh | 074c25a | 2019-07-22 16:40:57 +0100 | [diff] [blame] | 163 | case V1_2::OperationType::SOFTMAX: |
| 164 | return ConvertSoftmax(operation, model, data); |
Aron Virginas-Tar | ad1ab53 | 2019-07-25 11:24:42 +0100 | [diff] [blame] | 165 | case V1_2::OperationType::SPACE_TO_DEPTH: |
| 166 | return ConvertSpaceToDepth(operation, model, data); |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 167 | case V1_2::OperationType::SUB: |
| 168 | return ConvertSub(operation, model, data); |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 169 | case V1_2::OperationType::TANH: |
| 170 | return ConvertTanH(operation, model, data); |
Ferran Balaguer | b2397fd | 2019-07-25 12:12:39 +0100 | [diff] [blame] | 171 | case V1_2::OperationType::LSTM: |
| 172 | return ConvertLstm(operation, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 173 | default: |
| 174 | return Fail("%s: Operation type %s not supported in ArmnnDriver", |
| 175 | __func__, toString(operation.type).c_str()); |
| 176 | } |
| 177 | } |
| 178 | |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 179 | bool HalPolicy::ConvertAveragePool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 180 | { |
| 181 | ALOGV("hal_1_2::HalPolicy::ConvertAveragePool2d()"); |
| 182 | return ConvertPooling2d<hal_1_2::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Average, model, data); |
| 183 | } |
| 184 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 185 | bool HalPolicy::ConvertConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 186 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 187 | ALOGV("hal_1_2::HalPolicy::ConvertConv2d()"); |
| 188 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 189 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 190 | if (!input.IsValid()) |
| 191 | { |
| 192 | return Fail("%s: Operation has invalid inputs", __func__); |
| 193 | } |
| 194 | |
| 195 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 196 | if (!output) |
| 197 | { |
| 198 | return Fail("%s: Could not read output 0", __func__); |
| 199 | } |
| 200 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 201 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 202 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 203 | |
| 204 | if (IsDynamicTensor(outputInfo)) |
| 205 | { |
| 206 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 207 | } |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 208 | |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 209 | armnn::Convolution2dDescriptor desc; |
| 210 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 211 | |
| 212 | // Determine whether padding is implicit or explicit |
| 213 | bool implicitPadding = operation.inputs.size() == 7 || |
| 214 | (operation.inputs.size() >= 8 && |
| 215 | GetInputOperand<hal_1_2::HalPolicy>(operation, 7, model)->type == OperandType::BOOL); |
| 216 | |
| 217 | if (implicitPadding) |
| 218 | { |
| 219 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 7, model, data); |
| 220 | } |
| 221 | else if (operation.inputs.size() >= 10) |
| 222 | { |
| 223 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 10, model, data); |
| 224 | } |
| 225 | |
| 226 | const armnn::PermutationVector OHWIToOIHW = {0, 2, 3, 1}; |
| 227 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 228 | // ArmNN does not currently support non-fixed weights or bias |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 229 | // The NNAPI filter is always OHWI [depth_out, filter_height, filter_width, depth_in] but ArmNN expects the |
| 230 | // filter's height and width indices to match the input's height and width indices so we permute it to OIHW if |
| 231 | // the DataLayout is NCHW |
| 232 | const ConstTensorPin weightsPin = (desc.m_DataLayout == armnn::DataLayout::NCHW) ? |
| 233 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data, OHWIToOIHW) : |
| 234 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 235 | const ConstTensorPin biasPin = |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 236 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 237 | |
| 238 | if (!weightsPin.IsValid()) |
| 239 | { |
| 240 | return Fail("%s: Operation has invalid weights", __func__); |
| 241 | } |
| 242 | |
| 243 | if (!biasPin.IsValid()) |
| 244 | { |
| 245 | return Fail("%s: Operation has invalid biases", __func__); |
| 246 | } |
| 247 | |
| 248 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 249 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
| 250 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 251 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 252 | ActivationFn activation; |
| 253 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 254 | if (implicitPadding) |
| 255 | { |
| 256 | android::nn::PaddingScheme paddingScheme; |
| 257 | if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 258 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_StrideX, model, data) || |
| 259 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideY, model, data) || |
| 260 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 6, activation, model, data) || |
| 261 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 8, desc, model, data)) |
| 262 | { |
| 263 | return Fail("%s: Operation has invalid inputs (implicit padding)", __func__); |
| 264 | } |
| 265 | |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 266 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout); |
| 267 | unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex(); |
| 268 | unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex(); |
| 269 | const uint32_t kernelX = weights.GetShape()[widthIndex]; |
| 270 | const uint32_t kernelY = weights.GetShape()[heightIndex]; |
| 271 | const uint32_t inputX = inputInfo.GetShape()[widthIndex]; |
| 272 | const uint32_t inputY = inputInfo.GetShape()[heightIndex]; |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 273 | |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 274 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 275 | CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, paddingScheme); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 276 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 277 | } |
| 278 | else if (operation.inputs.size() >= 10) |
| 279 | { |
| 280 | // explicit padding |
| 281 | if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) || |
| 282 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) || |
| 283 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) || |
| 284 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) || |
| 285 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) || |
| 286 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) || |
| 287 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 9, activation, model, data) || |
| 288 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 11, desc, model, data)) |
| 289 | { |
| 290 | return Fail("%s: Operation has invalid inputs (explicit padding)", __func__); |
| 291 | } |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 292 | } |
| 293 | else |
| 294 | { |
| 295 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 296 | } |
| 297 | |
| 298 | desc.m_BiasEnabled = true; |
| 299 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 300 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 301 | bool isSupported = false; |
| 302 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 303 | IsConvolution2dSupported, |
| 304 | data.m_Backends, |
| 305 | isSupported, |
| 306 | inputInfo, |
| 307 | outputInfo, |
| 308 | desc, |
| 309 | weights.GetInfo(), |
| 310 | biases); |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame] | 311 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 312 | if (!isSupported) |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 313 | { |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | armnn::IConnectableLayer* startLayer = |
| 318 | data.m_Network->AddConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 319 | |
| 320 | if (!startLayer) |
| 321 | { |
| 322 | return Fail("%s: AddConvolution2dLayer failed", __func__); |
| 323 | } |
| 324 | |
| 325 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data); |
| 326 | |
| 327 | if (!endLayer) |
| 328 | { |
| 329 | return Fail("%s: ProcessActivation failed", __func__); |
| 330 | } |
| 331 | |
| 332 | input.Connect(startLayer->GetInputSlot(0)); |
| 333 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 334 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *endLayer, model, data); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | bool HalPolicy::ConvertDepthwiseConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 338 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 339 | ALOGV("hal_1_2::HalPolicy::ConvertDepthwiseConv2d()"); |
| 340 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 341 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 342 | |
| 343 | if (!input.IsValid()) |
| 344 | { |
| 345 | return Fail("%s: Operation has invalid inputs", __func__); |
| 346 | } |
| 347 | |
| 348 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 349 | |
| 350 | if (!output) |
| 351 | { |
| 352 | return Fail("%s: Could not read output 0", __func__); |
| 353 | } |
| 354 | |
| 355 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 356 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 357 | |
| 358 | if (IsDynamicTensor(outputInfo)) |
| 359 | { |
| 360 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 361 | } |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 362 | |
| 363 | // ArmNN does not currently support non-fixed weights or bias |
| 364 | // Find the shape of the weights tensor. In AndroidNN this will be [ 1, H, W, I * M ] |
| 365 | const Operand* weightsOperand = GetInputOperand<hal_1_2::HalPolicy>(operation, 1, model); |
| 366 | |
| 367 | if (weightsOperand == nullptr) |
| 368 | { |
| 369 | return Fail("%s: Operand is invalid", __func__); |
| 370 | } |
| 371 | armnn::DepthwiseConvolution2dDescriptor desc; |
| 372 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 373 | |
| 374 | // Determine whether padding is implicit or explicit |
| 375 | bool implicitPadding = operation.inputs.size() == 8 || |
| 376 | (operation.inputs.size() >= 9 && |
| 377 | GetInputOperand<hal_1_2::HalPolicy>(operation, 8, model)->type == OperandType::BOOL); |
| 378 | |
| 379 | // Look ahead to find the optional DataLayout, if present |
| 380 | const uint32_t dataLayoutFlagIndex = implicitPadding ? 8 : 11; |
| 381 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, dataLayoutFlagIndex, model, data); |
| 382 | |
| 383 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout); |
| 384 | unsigned int channelsIndex = dataLayoutIndexed.GetChannelsIndex(); |
| 385 | unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex(); |
| 386 | unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex(); |
| 387 | |
| 388 | // Reinterpret weight data as [ H, W, I, M ] |
| 389 | armnn::TensorShape weightsShape({ weightsOperand->dimensions[1], |
| 390 | weightsOperand->dimensions[2], |
| 391 | inputInfo.GetShape()[channelsIndex], |
| 392 | weightsOperand->dimensions[3] / inputInfo.GetShape()[channelsIndex] }); |
| 393 | |
| 394 | // Swizzle weight data [ H, W, I, M ] -> [ M, I, H, W ] |
| 395 | const armnn::PermutationVector HWIMToMIHW = { 2U, 3U, 1U, 0U }; |
| 396 | |
| 397 | const ConstTensorPin weightsPin = |
| 398 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 399 | 1, |
| 400 | model, |
| 401 | data, |
| 402 | HWIMToMIHW, |
| 403 | &weightsShape); |
| 404 | |
| 405 | // Bias is a 1D tensor |
| 406 | const ConstTensorPin biasPin = |
| 407 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data); |
| 408 | |
| 409 | if (!weightsPin.IsValid()) |
| 410 | { |
| 411 | return Fail("%s: Operation has invalid weights", __func__); |
| 412 | } |
| 413 | |
| 414 | if (!biasPin.IsValid()) |
| 415 | { |
| 416 | return Fail("%s: Operation has invalid biases", __func__); |
| 417 | } |
| 418 | |
| 419 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 420 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
| 421 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 422 | |
| 423 | ActivationFn activation; |
| 424 | |
| 425 | if (implicitPadding) |
| 426 | { |
| 427 | android::nn::PaddingScheme paddingScheme; |
| 428 | if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 429 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_StrideX, model, data) || |
| 430 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideY, model, data) || |
| 431 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 7, activation, model, data) || |
| 432 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 9, desc, model, data)) |
| 433 | { |
| 434 | return Fail("%s: Operation has invalid inputs (implicit padding)", __func__); |
| 435 | } |
| 436 | |
| 437 | const uint32_t kernelX = weights.GetShape()[3]; |
| 438 | const uint32_t kernelY = weights.GetShape()[2]; |
| 439 | const uint32_t inputX = inputInfo.GetShape()[widthIndex]; |
| 440 | const uint32_t inputY = inputInfo.GetShape()[heightIndex]; |
| 441 | |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 442 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 443 | CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, paddingScheme); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 444 | } |
| 445 | else if (operation.inputs.size() >= 11) |
| 446 | { |
| 447 | // explicit padding |
| 448 | if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) || |
| 449 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) || |
| 450 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) || |
| 451 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) || |
| 452 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) || |
| 453 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) || |
| 454 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 10, activation, model, data) || |
| 455 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 12, desc, model, data)) |
| 456 | { |
| 457 | return Fail("%s: Operation has invalid inputs (explicit padding)", __func__); |
| 458 | } |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 463 | } |
| 464 | |
| 465 | desc.m_BiasEnabled = true; |
| 466 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 467 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 468 | bool isSupported = false; |
| 469 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 470 | IsDepthwiseConvolutionSupported, |
| 471 | data.m_Backends, |
| 472 | isSupported, |
| 473 | inputInfo, |
| 474 | outputInfo, |
| 475 | desc, |
| 476 | weights.GetInfo(), |
| 477 | biases); |
Aron Virginas-Tar | 9fd3739 | 2019-07-15 18:04:32 +0100 | [diff] [blame] | 478 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 479 | if (!isSupported) |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 480 | { |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | armnn::IConnectableLayer* startLayer = |
| 485 | data.m_Network->AddDepthwiseConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
Aron Virginas-Tar | 9fd3739 | 2019-07-15 18:04:32 +0100 | [diff] [blame] | 486 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 487 | if (!startLayer) |
| 488 | { |
| 489 | return Fail("%s: AddDepthwiseConvolution2dLayer failed", __func__); |
| 490 | } |
| 491 | |
| 492 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data); |
| 493 | if (!endLayer) |
| 494 | { |
| 495 | return Fail("%s: ProcessActivation failed", __func__); |
| 496 | } |
| 497 | |
| 498 | input.Connect(startLayer->GetInputSlot(0)); |
| 499 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 500 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *endLayer, model, data); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Sadik Armagan | 15d63e2 | 2019-07-26 16:59:35 +0100 | [diff] [blame] | 503 | bool HalPolicy::ConvertL2Pool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 504 | { |
| 505 | ALOGV("hal_1_2::HalPolicy::ConvertL2Pool2d()"); |
| 506 | return ConvertPooling2d<hal_1_2::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::L2, model, data); |
| 507 | } |
| 508 | |
| 509 | bool HalPolicy::ConvertMaxPool2d(const Operation& operation, const Model& model, ConversionData& data) |
| 510 | { |
| 511 | ALOGV("hal_1_2::HalPolicy::ConvertMaxPool2d()"); |
| 512 | return ConvertPooling2d<hal_1_2::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Max, model, data); |
| 513 | } |
| 514 | |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 515 | bool HalPolicy::ConvertMaximum(const Operation& operation, const Model& model, ConversionData& data) |
| 516 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 517 | ALOGV("hal_1_2::HalPolicy::ConvertMaximum()"); |
| 518 | |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 519 | LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 520 | LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data); |
| 521 | |
| 522 | if (!input0.IsValid() || !input1.IsValid()) |
| 523 | { |
| 524 | return Fail("%s: Operation has invalid inputs", __func__); |
| 525 | } |
| 526 | |
| 527 | const Operand* outputOperand = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 528 | if (!outputOperand) |
| 529 | { |
| 530 | return Fail("%s: Could not read output", __func__); |
| 531 | } |
| 532 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 533 | const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 534 | if (IsDynamicTensor(outInfo)) |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 535 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 536 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Aron Virginas-Tar | d759323 | 2019-07-16 13:17:06 +0100 | [diff] [blame] | 539 | bool isSupported = false; |
| 540 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 541 | IsMaximumSupported, |
| 542 | data.m_Backends, |
| 543 | isSupported, |
| 544 | input0.GetTensorInfo(), |
| 545 | input1.GetTensorInfo(), |
| 546 | outInfo); |
| 547 | |
| 548 | if (!isSupported) |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 549 | { |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | armnn::IConnectableLayer* layer = data.m_Network->AddMaximumLayer(); |
| 554 | assert(layer != nullptr); |
| 555 | BroadcastTensor(input0, input1, layer, *data.m_Network); |
| 556 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 557 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Ellen Norris-Thompson | 1cb29aa | 2019-07-11 17:27:37 +0100 | [diff] [blame] | 560 | bool HalPolicy::ConvertMinimum(const Operation& operation, const Model& model, ConversionData& data) |
| 561 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 562 | ALOGV("hal_1_2::HalPolicy::ConvertMinimum()"); |
| 563 | |
Ellen Norris-Thompson | 1cb29aa | 2019-07-11 17:27:37 +0100 | [diff] [blame] | 564 | LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 565 | LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data); |
| 566 | |
| 567 | if (!input0.IsValid() || !input1.IsValid()) |
| 568 | { |
| 569 | return Fail("%s: Operation has invalid inputs", __func__); |
| 570 | } |
| 571 | |
| 572 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 573 | if (!output) |
| 574 | { |
| 575 | return Fail("%s: Could not read output 0", __func__); |
| 576 | } |
| 577 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 578 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 579 | if (IsDynamicTensor(outputInfo)) |
Ellen Norris-Thompson | 1cb29aa | 2019-07-11 17:27:37 +0100 | [diff] [blame] | 580 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 581 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Ellen Norris-Thompson | 1cb29aa | 2019-07-11 17:27:37 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | bool isSupported = false; |
| 585 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 586 | IsMinimumSupported, |
| 587 | data.m_Backends, |
| 588 | isSupported, |
| 589 | input0.GetTensorInfo(), |
| 590 | input1.GetTensorInfo(), |
| 591 | outputInfo); |
| 592 | |
| 593 | if (!isSupported) |
| 594 | { |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | armnn::IConnectableLayer* const layer = data.m_Network->AddMinimumLayer(); |
| 599 | assert(layer != nullptr); |
| 600 | BroadcastTensor(input0, input1, layer, *data.m_Network); |
| 601 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 602 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
Ellen Norris-Thompson | 1cb29aa | 2019-07-11 17:27:37 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Aron Virginas-Tar | c921f6b | 2019-07-25 10:14:33 +0100 | [diff] [blame] | 605 | bool HalPolicy::ConvertPad(const Operation& operation, const Model& model, ConversionData& data) |
| 606 | { |
| 607 | ALOGV("hal_1_2::HalPolicy::ConvertPad()"); |
| 608 | return ::ConvertPad<hal_1_2::HalPolicy>(operation, model, data); |
| 609 | } |
| 610 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 611 | bool HalPolicy::ConvertPadV2(const Operation& operation, const Model& model, ConversionData& data) |
| 612 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 613 | ALOGV("hal_1_2::HalPolicy::ConvertPadV2()"); |
| 614 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 615 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 616 | if (!input.IsValid()) |
| 617 | { |
| 618 | return Fail("%s: Could not read input 0", __func__); |
| 619 | } |
| 620 | |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 621 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 622 | if (!output) |
| 623 | { |
| 624 | return Fail("%s: Could not read output", __func__); |
| 625 | } |
| 626 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 627 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 628 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 629 | |
| 630 | armnn::PadDescriptor descriptor; |
| 631 | if (!ConvertPaddings<hal_1_2::HalPolicy>(operation, model, data, rank, descriptor)) |
| 632 | { |
| 633 | return Fail("%s: Could not convert paddings", __func__); |
| 634 | } |
| 635 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 636 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 637 | if (IsDynamicTensor(outputInfo)) |
Sadik Armagan | 310d8ff | 2019-07-11 10:53:38 +0100 | [diff] [blame] | 638 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 639 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Sadik Armagan | 310d8ff | 2019-07-11 10:53:38 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 642 | // Determine type of padding value |
| 643 | OperandType operandType0; |
| 644 | OperandType operandType2; |
| 645 | |
| 646 | if (!GetOperandType<hal_1_2::HalPolicy>(operation, 0, model, operandType0) || |
| 647 | !GetOperandType<hal_1_2::HalPolicy>(operation, 2, model, operandType2)) |
| 648 | { |
| 649 | return Fail("%s: Operation has invalid inputs", __func__); |
| 650 | } |
| 651 | |
| 652 | // Read value to use for padding |
| 653 | if (operandType0 == OperandType::TENSOR_FLOAT16 && operandType2 == OperandType::FLOAT16) |
| 654 | { |
| 655 | armnn::Half f16PadValue; |
| 656 | if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 2, operandType2, f16PadValue, model, data)) |
| 657 | { |
| 658 | return Fail("%s: Could not read input 2 (FLOAT16)", __func__); |
| 659 | } |
| 660 | |
| 661 | descriptor.m_PadValue = f16PadValue; |
| 662 | } |
| 663 | else if (operandType0 == OperandType::TENSOR_FLOAT32 && operandType2 == OperandType::FLOAT32) |
| 664 | { |
| 665 | if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 2, descriptor.m_PadValue, model, data)) |
| 666 | { |
| 667 | return Fail("%s: Could not read input 2 (FLOAT32)", __func__); |
| 668 | } |
| 669 | } |
| 670 | else if (operandType0 == OperandType::TENSOR_QUANT8_ASYMM && operandType2 == OperandType::INT32) |
| 671 | { |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 672 | int32_t intPadValue = 0; |
| 673 | if (!GetInputInt32<hal_1_2::HalPolicy>(operation, 2, intPadValue, model, data)) |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 674 | { |
| 675 | return Fail("%s: Could not read input 2 (INT32)", __func__); |
| 676 | } |
Mike Kelly | 3c67394 | 2019-07-25 09:26:06 +0100 | [diff] [blame] | 677 | descriptor.m_PadValue = intPadValue; |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 678 | } |
| 679 | else |
| 680 | { |
| 681 | return Fail("%s: Operation has invalid inputs: type mismatch", __func__); |
| 682 | } |
| 683 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 684 | bool isSupported = false; |
| 685 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 686 | IsPadSupported, |
| 687 | data.m_Backends, |
| 688 | isSupported, |
| 689 | inputInfo, |
| 690 | outputInfo, |
| 691 | descriptor); |
| 692 | if (!isSupported) |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 693 | { |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor); |
| 698 | assert(layer != nullptr); |
| 699 | input.Connect(layer->GetInputSlot(0)); |
| 700 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 701 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 702 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 705 | bool HalPolicy::ConvertPrelu(const Operation& operation, const Model& model, ConversionData& data) |
| 706 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 707 | ALOGV("hal_1_2::HalPolicy::ConvertPrelu()"); |
| 708 | |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 709 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 710 | LayerInputHandle alpha = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data); |
| 711 | |
| 712 | if (!input.IsValid() || !alpha.IsValid()) |
| 713 | { |
| 714 | return Fail("%s: Operation has invalid inputs", __func__); |
| 715 | } |
| 716 | |
| 717 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 718 | |
| 719 | if (!output) |
| 720 | { |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 721 | return Fail("%s: Could not read output", __func__); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 725 | const armnn::TensorInfo& alphaInfo = alpha.GetTensorInfo(); |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 726 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 727 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 728 | if (IsDynamicTensor(outputInfo)) |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 729 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 730 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 731 | } |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 732 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 733 | bool isSupported = false; |
| 734 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 735 | IsPreluSupported, |
| 736 | data.m_Backends, |
| 737 | isSupported, |
| 738 | inputInfo, |
| 739 | alphaInfo, |
| 740 | outputInfo); |
| 741 | if (!isSupported) |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 742 | { |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | armnn::IConnectableLayer* const layer = data.m_Network->AddPreluLayer(); |
| 747 | |
| 748 | if (!layer) |
| 749 | { |
| 750 | return Fail("%s: AddPreluLayer failed", __func__); |
| 751 | } |
| 752 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 753 | BroadcastTensor(input, alpha, layer, *data.m_Network); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 754 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 755 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Sadik Armagan | 5a476a8 | 2019-07-30 09:43:18 +0100 | [diff] [blame] | 758 | bool HalPolicy::ConvertQuantize(const Operation& operation, const Model& model, ConversionData& data) |
| 759 | { |
| 760 | ALOGV("hal_1_2::HalPolicy::ConvertQuantize()"); |
| 761 | |
| 762 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 763 | if (!input.IsValid()) |
| 764 | { |
| 765 | return Fail("%s: Operation has invalid input", __func__); |
| 766 | } |
| 767 | |
| 768 | const Operand* const outputOperand = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 769 | if (!outputOperand) |
| 770 | { |
| 771 | return Fail("%s: Operation has invalid outputs", __func__); |
| 772 | } |
| 773 | |
| 774 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
| 775 | if (IsDynamicTensor(outputInfo)) |
| 776 | { |
| 777 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 778 | } |
| 779 | |
| 780 | bool isSupported = false; |
| 781 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 782 | IsQuantizeSupported, |
| 783 | data.m_Backends, |
| 784 | isSupported, |
| 785 | input.GetTensorInfo(), |
| 786 | outputInfo); |
| 787 | if (!isSupported) |
| 788 | { |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | armnn::IConnectableLayer* const layer = data.m_Network->AddQuantizeLayer(); |
| 793 | assert(layer != nullptr); |
| 794 | input.Connect(layer->GetInputSlot(0)); |
| 795 | |
| 796 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
| 797 | } |
| 798 | |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 799 | bool HalPolicy::ConvertReLu(const Operation& operation, const Model& model, ConversionData& data) |
| 800 | { |
| 801 | ALOGV("hal_1_2::HalPolicy::ConvertReLu()"); |
| 802 | return ::ConvertReLu<hal_1_2::HalPolicy>(operation, model, data); |
| 803 | } |
| 804 | |
| 805 | bool HalPolicy::ConvertReLu1(const Operation& operation, const Model& model, ConversionData& data) |
| 806 | { |
| 807 | ALOGV("hal_1_2::HalPolicy::ConvertReLu1()"); |
| 808 | return ::ConvertReLu1<hal_1_2::HalPolicy>(operation, model, data); |
| 809 | } |
| 810 | |
| 811 | bool HalPolicy::ConvertReLu6(const Operation& operation, const Model& model, ConversionData& data) |
| 812 | { |
| 813 | ALOGV("hal_1_2::HalPolicy::ConvertReLu6()"); |
| 814 | return ::ConvertReLu6<hal_1_2::HalPolicy>(operation, model, data); |
| 815 | } |
| 816 | |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 817 | bool HalPolicy::ConvertResize(const Operation& operation, |
| 818 | const Model& model, |
| 819 | ConversionData& data, |
| 820 | armnn::ResizeMethod resizeMethod) |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 821 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 822 | ALOGV("hal_1_2::HalPolicy::ConvertResize()"); |
| 823 | |
| 824 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 825 | if (!input.IsValid()) |
| 826 | { |
| 827 | return Fail("%s: Could not read input 0", __func__); |
| 828 | } |
| 829 | |
| 830 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 831 | if (!output) |
| 832 | { |
| 833 | return Fail("%s: Could not read output 0", __func__); |
| 834 | } |
| 835 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 836 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 837 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 838 | |
| 839 | if (IsDynamicTensor(outputInfo)) |
| 840 | { |
| 841 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 842 | } |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 843 | |
| 844 | armnn::ResizeDescriptor descriptor; |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 845 | descriptor.m_Method = resizeMethod; |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 846 | descriptor.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 3, model, data); |
| 847 | |
| 848 | OperandType operandType1; |
| 849 | OperandType operandType2; |
| 850 | |
| 851 | if (!GetOperandType<hal_1_2::HalPolicy>(operation, 1, model, operandType1) || |
| 852 | !GetOperandType<hal_1_2::HalPolicy>(operation, 2, model, operandType2)) |
| 853 | { |
| 854 | return Fail("%s: Operation has invalid inputs", __func__); |
| 855 | } |
| 856 | |
| 857 | if (operandType1 != operandType2) |
| 858 | { |
| 859 | return Fail("%s: Operation has invalid inputs. Type of input 1 and 2 should be the same", __func__); |
| 860 | } |
| 861 | |
| 862 | if (operandType1 == OperandType::INT32) |
| 863 | { |
| 864 | // Case 1: resizing by shape |
| 865 | int32_t targetWidth = 0; |
| 866 | int32_t targetHeight = 0; |
| 867 | |
| 868 | if (!GetInputInt32<hal_1_2::HalPolicy>(operation, 1, targetWidth, model, data) || |
| 869 | !GetInputInt32<hal_1_2::HalPolicy>(operation, 2, targetHeight, model, data)) |
| 870 | { |
| 871 | return Fail("%s: Operation has invalid inputs for resizing by shape", __func__); |
| 872 | } |
| 873 | |
| 874 | if (targetWidth < 0 || targetHeight < 0) |
| 875 | { |
| 876 | return Fail("%s: Operation has invalid inputs for resizing by shape. " |
| 877 | "Target width/height cannot be < 0", __func__); |
| 878 | } |
| 879 | |
| 880 | descriptor.m_TargetWidth = static_cast<uint32_t>(targetWidth); |
Teresa Charlin | 9843c01 | 2019-07-19 12:18:35 +0100 | [diff] [blame] | 881 | descriptor.m_TargetHeight = static_cast<uint32_t>(targetHeight); |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 882 | } |
| 883 | else if (operandType1 == OperandType::FLOAT32) |
| 884 | { |
| 885 | // Case 2: resizing by scale |
| 886 | float widthScale = 1.0f; |
| 887 | float heightScale = 1.0f; |
| 888 | |
| 889 | if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 1, widthScale, model, data) || |
| 890 | !GetInputFloat32<hal_1_2::HalPolicy>(operation, 2, heightScale, model, data)) |
| 891 | { |
| 892 | return Fail("%s: Operation has invalid inputs for resizing by scale", __func__); |
| 893 | } |
| 894 | |
| 895 | const armnn::TensorShape& inputShape = inputInfo.GetShape(); |
| 896 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(descriptor.m_DataLayout); |
| 897 | |
| 898 | float width = inputShape[dataLayoutIndexed.GetWidthIndex()]; |
| 899 | float height = inputShape[dataLayoutIndexed.GetHeightIndex()]; |
| 900 | |
| 901 | descriptor.m_TargetWidth = std::floor(width * widthScale); |
| 902 | descriptor.m_TargetHeight = std::floor(height * heightScale); |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | // NOTE: FLOAT16 scales are not supported |
| 907 | return false; |
| 908 | } |
| 909 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 910 | bool isSupported = false; |
| 911 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 912 | IsResizeSupported, |
| 913 | data.m_Backends, |
| 914 | isSupported, |
| 915 | inputInfo, |
| 916 | outputInfo, |
| 917 | descriptor); |
Aron Virginas-Tar | be5d356 | 2019-07-16 11:32:29 +0100 | [diff] [blame] | 918 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 919 | if (!isSupported) |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 920 | { |
| 921 | return false; |
| 922 | } |
| 923 | |
| 924 | armnn::IConnectableLayer* layer = data.m_Network->AddResizeLayer(descriptor); |
| 925 | |
| 926 | assert(layer != nullptr); |
| 927 | |
| 928 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 929 | input.Connect(layer->GetInputSlot(0)); |
| 930 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 931 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 932 | } |
| 933 | |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 934 | bool HalPolicy::ConvertSpaceToDepth(const Operation& operation, const Model& model, ConversionData& data) |
| 935 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 936 | ALOGV("hal_1_2::HalPolicy::ConvertSpaceToDepth()"); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 937 | |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 938 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 939 | if (!input.IsValid() ) |
| 940 | { |
| 941 | return Fail("%s: Operation has invalid inputs", __func__); |
| 942 | } |
| 943 | |
| 944 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 945 | unsigned int rank = inputInfo.GetNumDimensions(); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 946 | if (rank != 4) |
| 947 | { |
| 948 | return Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 949 | } |
| 950 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 951 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 952 | if (!output) |
| 953 | { |
| 954 | return Fail("%s: Could not read output 0", __func__); |
| 955 | } |
| 956 | |
| 957 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 958 | if (IsDynamicTensor(outputInfo)) |
| 959 | { |
| 960 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 961 | } |
| 962 | |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 963 | armnn::SpaceToDepthDescriptor desc; |
| 964 | |
| 965 | GetInputScalar<hal_1_2::HalPolicy>(operation, 1, OperandType::INT32, desc.m_BlockSize, model, data); |
| 966 | |
| 967 | if (desc.m_BlockSize <= 1) |
| 968 | { |
| 969 | return Fail("%s: Block size must be at least 1 in all dimensions"); |
| 970 | } |
| 971 | |
| 972 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 2, model, data); |
| 973 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 974 | bool isSupported = false; |
| 975 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 976 | IsSpaceToDepthSupported, |
| 977 | data.m_Backends, |
| 978 | isSupported, |
| 979 | inputInfo, |
| 980 | outputInfo, |
| 981 | desc); |
| 982 | if (!isSupported) |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 983 | { |
| 984 | return false; |
| 985 | } |
| 986 | |
| 987 | armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToDepthLayer(desc); |
| 988 | assert(layer != nullptr); |
| 989 | input.Connect(layer->GetInputSlot(0)); |
| 990 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 991 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Francis Murtagh | 074c25a | 2019-07-22 16:40:57 +0100 | [diff] [blame] | 994 | bool HalPolicy::ConvertSoftmax(const Operation& operation, const Model& model, ConversionData& data) |
| 995 | { |
Aron Virginas-Tar | 29404fb | 2019-07-24 13:55:31 +0100 | [diff] [blame] | 996 | ALOGV("hal_1_2::HalPolicy::ConvertSoftmax()"); |
| 997 | |
Francis Murtagh | 074c25a | 2019-07-22 16:40:57 +0100 | [diff] [blame] | 998 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 999 | if (!input.IsValid()) |
| 1000 | { |
| 1001 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1002 | } |
| 1003 | |
| 1004 | const Operand* outputOperand = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 1005 | if (!outputOperand) |
| 1006 | { |
| 1007 | return Fail("%s: Operation has no outputs", __func__); |
| 1008 | } |
| 1009 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1010 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand); |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 1011 | if (IsDynamicTensor(outputInfo)) |
Francis Murtagh | 074c25a | 2019-07-22 16:40:57 +0100 | [diff] [blame] | 1012 | { |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1013 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
Francis Murtagh | 074c25a | 2019-07-22 16:40:57 +0100 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | armnn::SoftmaxDescriptor desc; |
| 1017 | if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 1, desc.m_Beta, model, data)) |
| 1018 | { |
| 1019 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1020 | } |
| 1021 | |
| 1022 | if (operation.inputs.size() > 2 && !GetInputScalar<hal_1_2::HalPolicy>(operation, |
| 1023 | 2, |
| 1024 | HalPolicy::OperandType::INT32, |
| 1025 | desc.m_Axis, |
| 1026 | model, |
| 1027 | data)) |
| 1028 | { |
| 1029 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1030 | } |
| 1031 | |
| 1032 | bool isSupported = false; |
| 1033 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1034 | IsSoftmaxSupported, |
| 1035 | data.m_Backends, |
| 1036 | isSupported, |
| 1037 | input.GetTensorInfo(), |
| 1038 | outputInfo, |
| 1039 | desc); |
| 1040 | if (!isSupported) |
| 1041 | { |
| 1042 | return false; |
| 1043 | } |
| 1044 | |
| 1045 | armnn::IConnectableLayer* layer = data.m_Network->AddSoftmaxLayer(desc); |
| 1046 | assert(layer != nullptr); |
| 1047 | input.Connect(layer->GetInputSlot(0)); |
| 1048 | |
Aron Virginas-Tar | b7421e5 | 2019-07-26 13:14:39 +0100 | [diff] [blame] | 1049 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
Francis Murtagh | 074c25a | 2019-07-22 16:40:57 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
Mike Kelly | 0a87936 | 2019-07-29 16:56:31 +0100 | [diff] [blame] | 1052 | bool HalPolicy::ConvertSub(const Operation& operation, const Model& model, ConversionData& data) |
| 1053 | { |
| 1054 | ALOGV("hal_1_2::HalPolicy::ConvertSub()"); |
| 1055 | return ::ConvertSub<hal_1_2::HalPolicy>(operation, model, data); |
| 1056 | } |
| 1057 | |
Sadik Armagan | 6111316 | 2019-07-25 09:09:40 +0100 | [diff] [blame] | 1058 | bool HalPolicy::ConvertTanH(const Operation& operation, const Model& model, ConversionData& data) |
| 1059 | { |
| 1060 | ALOGV("hal_1_2::HalPolicy::ConvertTanH()"); |
| 1061 | return ::ConvertTanH<hal_1_2::HalPolicy>(operation, model, data); |
| 1062 | } |
| 1063 | |
Ferran Balaguer | b2397fd | 2019-07-25 12:12:39 +0100 | [diff] [blame] | 1064 | bool HalPolicy::ConvertLstm(const Operation& operation, const Model& model, ConversionData& data) |
| 1065 | { |
| 1066 | // Inputs: |
| 1067 | // 00: The input: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, input_size], where |
| 1068 | // “batch_size” corresponds to the batching dimension, and “input_size” is the size of the input. |
| 1069 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 1070 | if (!input.IsValid()) |
| 1071 | { |
| 1072 | return Fail("%s: Could not read input 0: input", __func__); |
| 1073 | } |
| 1074 | // 18: The output state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. |
| 1075 | LayerInputHandle outputStateIn = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 18, model, data); |
| 1076 | if (!outputStateIn.IsValid()) |
| 1077 | { |
| 1078 | return Fail("%s: Could not read input 18: outputStateIn", __func__); |
| 1079 | } |
| 1080 | // 19: The cell state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units]. |
| 1081 | LayerInputHandle cellStateIn = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 19, model, data); |
| 1082 | if (!cellStateIn.IsValid()) |
| 1083 | { |
| 1084 | return Fail("%s: Could not read input 19: cellStateIn", __func__); |
| 1085 | } |
| 1086 | |
| 1087 | // Get the mandatory input tensors: |
| 1088 | // 02: The input-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1089 | // [num_units, input_size]. |
| 1090 | const ConstTensorPin inputToForgetWeightsPin = |
| 1091 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data); |
| 1092 | // 03: The input-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1093 | // [num_units, input_size]. |
| 1094 | const ConstTensorPin inputToCellWeightsPin = |
| 1095 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 3, model, data); |
| 1096 | // 04: The input-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1097 | // [num_units, input_size]. |
| 1098 | const ConstTensorPin inputToOutputWeightsPin = |
| 1099 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 4, model, data); |
| 1100 | // 06: The recurrent-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1101 | // [num_units, output_size]. |
| 1102 | const ConstTensorPin recurrentToForgetWeightsPin = |
| 1103 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 6, model, data); |
| 1104 | // 07: The recurrent-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1105 | // [num_units, output_size]. |
| 1106 | const ConstTensorPin recurrentToCellWeightsPin = |
| 1107 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 7, model, data); |
| 1108 | // 08: The recurrent-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1109 | // [num_units, output_size]. |
| 1110 | const ConstTensorPin recurrentToOutputWeightsPin = |
| 1111 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 8, model, data); |
| 1112 | // 13: The forget gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
| 1113 | const ConstTensorPin forgetGateBiasPin = |
| 1114 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 13, model, data); |
| 1115 | // 14: The cell bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
| 1116 | const ConstTensorPin cellBiasPin = |
| 1117 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 14, model, data); |
| 1118 | // 15: The output gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
| 1119 | const ConstTensorPin outputGateBiasPin = |
| 1120 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 15, model, data); |
| 1121 | |
| 1122 | if (!inputToForgetWeightsPin.IsValid() || |
| 1123 | !inputToCellWeightsPin.IsValid() || |
| 1124 | !inputToOutputWeightsPin.IsValid() || |
| 1125 | !recurrentToForgetWeightsPin.IsValid() || |
| 1126 | !recurrentToCellWeightsPin.IsValid() || |
| 1127 | !recurrentToOutputWeightsPin.IsValid() || |
| 1128 | !forgetGateBiasPin.IsValid() || |
| 1129 | !cellBiasPin.IsValid() || |
| 1130 | !outputGateBiasPin.IsValid()) |
| 1131 | { |
| 1132 | return Fail("%s: Operation has invalid tensor inputs", __func__); |
| 1133 | } |
| 1134 | |
| 1135 | // Get the optional input tensors: |
| 1136 | // 01: The input-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1137 | // [num_units, input_size], where “num_units” corresponds to the number of cell units. |
| 1138 | const ConstTensorPin inputToInputWeightsPin = |
| 1139 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1140 | 1, |
| 1141 | model, |
| 1142 | data, |
| 1143 | g_DontPermute, |
| 1144 | nullptr, |
| 1145 | true); |
| 1146 | |
| 1147 | // 05: The recurrent-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1148 | // [num_units, output_size], where “output_size” corresponds to either the number of cell units (i.e., |
| 1149 | // “num_units”), or the second dimension of the “projection_weights”, if defined. |
| 1150 | const ConstTensorPin recurrentToInputWeightsPin = |
| 1151 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1152 | 5, |
| 1153 | model, |
| 1154 | data, |
| 1155 | g_DontPermute, |
| 1156 | nullptr, |
| 1157 | true); |
| 1158 | |
| 1159 | // 09: The cell-to-input weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
| 1160 | const ConstTensorPin cellToInputWeightsPin = |
| 1161 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1162 | 9, |
| 1163 | model, |
| 1164 | data, |
| 1165 | g_DontPermute, |
| 1166 | nullptr, |
| 1167 | true); |
| 1168 | |
| 1169 | // 10: The cell-to-forget weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
| 1170 | const ConstTensorPin cellToForgetWeightsPin = |
| 1171 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1172 | 10, |
| 1173 | model, |
| 1174 | data, |
| 1175 | g_DontPermute, |
| 1176 | nullptr, |
| 1177 | true); |
| 1178 | |
| 1179 | // 11: The cell-to-output weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
| 1180 | const ConstTensorPin cellToOutputWeightsPin = |
| 1181 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1182 | 11, |
| 1183 | model, |
| 1184 | data, |
| 1185 | g_DontPermute, |
| 1186 | nullptr, |
| 1187 | true); |
| 1188 | |
| 1189 | // 12: The input gate bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units]. |
| 1190 | const ConstTensorPin inputGateBiasPin = |
| 1191 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1192 | 12, |
| 1193 | model, |
| 1194 | data, |
| 1195 | g_DontPermute, |
| 1196 | nullptr, |
| 1197 | true); |
| 1198 | |
| 1199 | // 16: The projection weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape |
| 1200 | // [output_size, num_units]. |
| 1201 | const ConstTensorPin projectionWeightsPin = |
| 1202 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1203 | 16, |
| 1204 | model, |
| 1205 | data, |
| 1206 | g_DontPermute, |
| 1207 | nullptr, |
| 1208 | true); |
| 1209 | |
| 1210 | // 17: The projection bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [output_size]. |
| 1211 | const ConstTensorPin projectionBiasPin = |
| 1212 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1213 | 17, |
| 1214 | model, |
| 1215 | data, |
| 1216 | g_DontPermute, |
| 1217 | nullptr, |
| 1218 | true); |
| 1219 | |
| 1220 | if ((!inputToInputWeightsPin.IsValid() && !inputToInputWeightsPin.IsOptional()) || |
| 1221 | (!recurrentToInputWeightsPin.IsValid() && !recurrentToInputWeightsPin.IsOptional()) || |
| 1222 | (!cellToInputWeightsPin.IsValid() && !cellToInputWeightsPin.IsOptional()) || |
| 1223 | (!cellToForgetWeightsPin.IsValid() && !cellToForgetWeightsPin.IsOptional()) || |
| 1224 | (!cellToOutputWeightsPin.IsValid() && !cellToOutputWeightsPin.IsOptional()) || |
| 1225 | (!inputGateBiasPin.IsValid() && !inputGateBiasPin.IsOptional()) || |
| 1226 | (!projectionWeightsPin.IsValid() && !projectionWeightsPin.IsOptional()) || |
| 1227 | (!projectionBiasPin.IsValid() && !projectionBiasPin.IsOptional())) |
| 1228 | { |
| 1229 | return Fail("%s: Operation has invalid tensor inputs", __func__); |
| 1230 | } |
| 1231 | |
| 1232 | // Get the mandatory input scalars (actually 1-D tensors of size 1): |
| 1233 | // 20: The activation function: A value indicating the activation function: |
| 1234 | // 0: None; 1: Relu; 3: Relu6; 4: Tanh; 6: Sigmoid. |
| 1235 | // 21: The clipping threshold: for the cell state, such that values are bound within [-cell_clip, cell_clip]. |
| 1236 | // If set to 0.0 then clipping is disabled. |
| 1237 | // 22: The clipping threshold: for the output from the projection layer, such that values are bound within |
| 1238 | // [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled. |
| 1239 | ActivationFn activation; |
| 1240 | float cellClip; |
| 1241 | float projClip; |
| 1242 | if (!GetInputActivationFunctionFromTensor<hal_1_2::HalPolicy>(operation, 20, activation, model, data) || |
| 1243 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 21, OperandType::FLOAT32, cellClip, model, data) || |
| 1244 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 22, OperandType::FLOAT32, projClip, model, data)) |
| 1245 | { |
| 1246 | return Fail("%s: Operation has invalid scalar inputs", __func__); |
| 1247 | } |
| 1248 | |
| 1249 | // Get the normalization tensors |
| 1250 | // 23: The input layer normalization weights. A 1-D tensor of shape [num_units]. |
| 1251 | // Used to rescale normalized inputs to activation at input gate. |
| 1252 | const ConstTensorPin inputLayerNormWeightsPin = |
| 1253 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1254 | 23, |
| 1255 | model, |
| 1256 | data, |
| 1257 | g_DontPermute, |
| 1258 | nullptr, |
| 1259 | true); |
| 1260 | |
| 1261 | // 24: The forget layer normalization weights. A 1-D tensor of shape [num_units]. |
| 1262 | // Used to rescale normalized inputs to activation at forget gate. |
| 1263 | const ConstTensorPin forgetLayerNormWeightsPin = |
| 1264 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1265 | 24, |
| 1266 | model, |
| 1267 | data, |
| 1268 | g_DontPermute, |
| 1269 | nullptr, |
| 1270 | true); |
| 1271 | |
| 1272 | // 25: The cell layer normalization weights. A 1-D tensor of shape [num_units]. |
| 1273 | // Used to rescale normalized inputs to activation at cell gate. |
| 1274 | const ConstTensorPin cellLayerNormWeightsPin = |
| 1275 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1276 | 25, |
| 1277 | model, |
| 1278 | data, |
| 1279 | g_DontPermute, |
| 1280 | nullptr, |
| 1281 | true); |
| 1282 | |
| 1283 | // 26: The output layer normalization weights. A 1-D tensor of shape [num_units]. |
| 1284 | // Used to rescale normalized inputs to activation at output gate. |
| 1285 | const ConstTensorPin outputLayerNormWeightsPin = |
| 1286 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 1287 | 26, |
| 1288 | model, |
| 1289 | data, |
| 1290 | g_DontPermute, |
| 1291 | nullptr, |
| 1292 | true); |
| 1293 | |
| 1294 | // Outputs: |
| 1295 | // 00: The scratch buffer: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units * 4] |
| 1296 | // with CIFG, or [batch_size, num_units * 3] without CIFG. |
| 1297 | const Operand* scratchBuffer = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 1298 | if (!scratchBuffer) |
| 1299 | { |
| 1300 | return Fail("%s: Could not read output 0: scratchBuffer", __func__); |
| 1301 | } |
| 1302 | // 01: The output state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. |
| 1303 | const Operand* outputStateOut = GetOutputOperand<hal_1_2::HalPolicy>(operation, 1, model); |
| 1304 | if (!outputStateOut) |
| 1305 | { |
| 1306 | return Fail("%s: Could not read output 1: outputStateOut", __func__); |
| 1307 | } |
| 1308 | // 02: The cell state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units]. |
| 1309 | const Operand* cellStateOut = GetOutputOperand<hal_1_2::HalPolicy>(operation, 2, model); |
| 1310 | if (!cellStateOut) |
| 1311 | { |
| 1312 | return Fail("%s: Could not read output 2: cellStateOut", __func__); |
| 1313 | } |
| 1314 | // 03: The output: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. This is |
| 1315 | // effectively the same as the current “output state (out)” value. |
| 1316 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 3, model); |
| 1317 | if (!output) |
| 1318 | { |
| 1319 | return Fail("%s: Could not read output 3: output", __func__); |
| 1320 | } |
| 1321 | |
| 1322 | // set the params structure for the AddLstmLayer call |
| 1323 | armnn::LstmInputParams params; |
| 1324 | params.m_InputToInputWeights = inputToInputWeightsPin.GetConstTensorPtr(); |
| 1325 | params.m_InputToForgetWeights = inputToForgetWeightsPin.GetConstTensorPtr(); |
| 1326 | params.m_InputToCellWeights = inputToCellWeightsPin.GetConstTensorPtr(); |
| 1327 | params.m_InputToOutputWeights = inputToOutputWeightsPin.GetConstTensorPtr(); |
| 1328 | params.m_RecurrentToInputWeights = recurrentToInputWeightsPin.GetConstTensorPtr(); |
| 1329 | params.m_RecurrentToForgetWeights = recurrentToForgetWeightsPin.GetConstTensorPtr(); |
| 1330 | params.m_RecurrentToCellWeights = recurrentToCellWeightsPin.GetConstTensorPtr(); |
| 1331 | params.m_RecurrentToOutputWeights = recurrentToOutputWeightsPin.GetConstTensorPtr(); |
| 1332 | params.m_CellToInputWeights = cellToInputWeightsPin.GetConstTensorPtr(); |
| 1333 | params.m_CellToForgetWeights = cellToForgetWeightsPin.GetConstTensorPtr(); |
| 1334 | params.m_CellToOutputWeights = cellToOutputWeightsPin.GetConstTensorPtr(); |
| 1335 | params.m_InputGateBias = inputGateBiasPin.GetConstTensorPtr(); |
| 1336 | params.m_ForgetGateBias = forgetGateBiasPin.GetConstTensorPtr(); |
| 1337 | params.m_CellBias = cellBiasPin.GetConstTensorPtr(); |
| 1338 | params.m_OutputGateBias = outputGateBiasPin.GetConstTensorPtr(); |
| 1339 | params.m_ProjectionWeights = projectionWeightsPin.GetConstTensorPtr(); |
| 1340 | params.m_ProjectionBias = projectionBiasPin.GetConstTensorPtr(); |
| 1341 | params.m_InputLayerNormWeights = inputLayerNormWeightsPin.GetConstTensorPtr(); |
| 1342 | params.m_ForgetLayerNormWeights = forgetLayerNormWeightsPin.GetConstTensorPtr(); |
| 1343 | params.m_CellLayerNormWeights = cellLayerNormWeightsPin.GetConstTensorPtr(); |
| 1344 | params.m_OutputLayerNormWeights = outputLayerNormWeightsPin.GetConstTensorPtr(); |
| 1345 | |
| 1346 | // set the layer descriptor |
| 1347 | armnn::LstmDescriptor desc; |
| 1348 | desc.m_ActivationFunc = activation; |
| 1349 | desc.m_ClippingThresCell = cellClip; |
| 1350 | desc.m_ClippingThresProj = projClip; |
| 1351 | desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr || |
| 1352 | params.m_RecurrentToInputWeights == nullptr || |
| 1353 | params.m_InputGateBias == nullptr); |
| 1354 | desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || |
| 1355 | params.m_CellToOutputWeights != nullptr); |
| 1356 | desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr); |
| 1357 | desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr || |
| 1358 | params.m_ForgetLayerNormWeights != nullptr || |
| 1359 | params.m_CellLayerNormWeights != nullptr || |
| 1360 | params.m_OutputLayerNormWeights != nullptr); |
| 1361 | |
| 1362 | // validate the optional input groups |
| 1363 | if (desc.m_CifgEnabled && |
| 1364 | (params.m_InputToInputWeights != nullptr || |
| 1365 | params.m_RecurrentToInputWeights != nullptr || |
| 1366 | params.m_InputGateBias != nullptr)) |
| 1367 | { |
| 1368 | return Fail("%s: All, or none, of input-to-input weights, recurrent-to-input weights," |
| 1369 | " and input gate bias must be provided", __func__); |
| 1370 | } |
| 1371 | |
| 1372 | if (!desc.m_ProjectionEnabled && params.m_ProjectionBias != nullptr) |
| 1373 | { |
| 1374 | return Fail("%s: projection bias should not be provided without projection weights", __func__); |
| 1375 | } |
| 1376 | |
| 1377 | if (desc.m_PeepholeEnabled && |
| 1378 | (params.m_CellToForgetWeights == nullptr || |
| 1379 | params.m_CellToOutputWeights == nullptr || |
| 1380 | (!desc.m_CifgEnabled && params.m_CellToInputWeights == nullptr))) |
| 1381 | { |
| 1382 | return Fail("%s: All, or none, of cell-to-forget weights and cell-to-output weights must be provided" |
| 1383 | " and, if CIFG is not enabled, cell-to-input weights must also be provided", __func__); |
| 1384 | } |
| 1385 | |
| 1386 | if (desc.m_LayerNormEnabled && |
| 1387 | (params.m_ForgetLayerNormWeights == nullptr || |
| 1388 | params.m_CellLayerNormWeights == nullptr || |
| 1389 | params.m_OutputLayerNormWeights == nullptr || |
| 1390 | (!desc.m_CifgEnabled && params.m_InputLayerNormWeights == nullptr))) |
| 1391 | { |
| 1392 | return Fail("%s: All, or none, of forget-norm weights, cell-norm weights and output-norm weights must be" |
| 1393 | " provided and, if CIFG is not enabled, input-norm weights must also be provided", __func__); |
| 1394 | } |
| 1395 | |
| 1396 | // Check if the layer is supported |
| 1397 | // Inputs |
| 1398 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1399 | const armnn::TensorInfo& outputStateInInfo = outputStateIn.GetTensorInfo(); |
| 1400 | const armnn::TensorInfo& cellStateInInfo = cellStateIn.GetTensorInfo(); |
| 1401 | |
| 1402 | // Outputs |
| 1403 | const armnn::TensorInfo& scratchBufferInfo = GetTensorInfoForOperand(*scratchBuffer); |
| 1404 | const armnn::TensorInfo& outputStateOutInfo = GetTensorInfoForOperand(*outputStateOut); |
| 1405 | const armnn::TensorInfo& cellStateOutInfo = GetTensorInfoForOperand(*cellStateOut); |
| 1406 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 1407 | |
Ferran Balaguer | a4a629a | 2019-07-30 10:16:13 +0100 | [diff] [blame^] | 1408 | if (IsDynamicTensor(scratchBufferInfo) || |
| 1409 | IsDynamicTensor(outputStateOutInfo) || |
| 1410 | IsDynamicTensor(cellStateOutInfo) || |
| 1411 | IsDynamicTensor(outputInfo)) |
| 1412 | { |
| 1413 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 1414 | } |
| 1415 | |
Ferran Balaguer | b2397fd | 2019-07-25 12:12:39 +0100 | [diff] [blame] | 1416 | // Basic parameters |
| 1417 | armnn::LstmInputParamsInfo paramsInfo; |
| 1418 | paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo()); |
| 1419 | paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo()); |
| 1420 | paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo()); |
| 1421 | paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo()); |
| 1422 | paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo()); |
| 1423 | paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo()); |
| 1424 | paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo()); |
| 1425 | paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo()); |
| 1426 | paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo()); |
| 1427 | |
| 1428 | // Optional parameters |
| 1429 | if(!desc.m_CifgEnabled) |
| 1430 | { |
| 1431 | paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo()); |
| 1432 | paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo()); |
| 1433 | if (params.m_CellToInputWeights != nullptr) |
| 1434 | { |
| 1435 | paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo()); |
| 1436 | } |
| 1437 | paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo()); |
| 1438 | } |
| 1439 | |
| 1440 | if(desc.m_ProjectionEnabled) |
| 1441 | { |
| 1442 | paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo()); |
| 1443 | if (params.m_ProjectionBias != nullptr) |
| 1444 | { |
| 1445 | paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo()); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | if(desc.m_PeepholeEnabled) |
| 1450 | { |
| 1451 | paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo()); |
| 1452 | paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo()); |
| 1453 | } |
| 1454 | |
| 1455 | if (desc.m_LayerNormEnabled) |
| 1456 | { |
| 1457 | if(!desc.m_CifgEnabled) |
| 1458 | { |
| 1459 | paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo()); |
| 1460 | } |
| 1461 | paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo()); |
| 1462 | paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo()); |
| 1463 | paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo()); |
| 1464 | } |
| 1465 | |
| 1466 | bool isSupported = false; |
| 1467 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1468 | IsLstmSupported, |
| 1469 | data.m_Backends, |
| 1470 | isSupported, |
| 1471 | inputInfo, |
| 1472 | outputStateInInfo, |
| 1473 | cellStateInInfo, |
| 1474 | scratchBufferInfo, |
| 1475 | outputStateOutInfo, |
| 1476 | cellStateOutInfo, |
| 1477 | outputInfo, |
| 1478 | desc, |
| 1479 | paramsInfo); |
| 1480 | if (!isSupported) |
| 1481 | { |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
| 1485 | // Add the layer |
| 1486 | armnn::IConnectableLayer* layer = data.m_Network->AddLstmLayer(desc, params, "Lstm"); |
| 1487 | |
| 1488 | input.Connect(layer->GetInputSlot(0)); |
| 1489 | outputStateIn.Connect(layer->GetInputSlot(1)); |
| 1490 | cellStateIn.Connect(layer->GetInputSlot(2)); |
| 1491 | |
| 1492 | return (SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, 0, model, data) && |
| 1493 | SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 1, *layer, 1, model, data) && |
| 1494 | SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 2, *layer, 2, model, data) && |
| 1495 | SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 3, *layer, 3, model, data)); |
| 1496 | } |
| 1497 | |
David Monahan | 613b49c | 2019-06-27 11:37:47 +0100 | [diff] [blame] | 1498 | bool HalPolicy::ConvertTransposeConvolution2d(const Operation& operation, const Model& model, ConversionData& data) |
| 1499 | { |
| 1500 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 1501 | |
| 1502 | if (!input.IsValid()) |
| 1503 | { |
| 1504 | return Fail("%s: Operation has invalid inputs", __func__); |
| 1505 | } |
| 1506 | |
| 1507 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 1508 | |
| 1509 | if (!output) |
| 1510 | { |
| 1511 | return Fail("%s: Could not read output 0", __func__); |
| 1512 | } |
| 1513 | |
| 1514 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 1515 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 1516 | if (IsDynamicTensor(outputInfo)) |
| 1517 | { |
| 1518 | return Fail("%s: Dynamic output tensors are not supported", __func__); |
| 1519 | } |
| 1520 | |
| 1521 | // ArmNN does not currently support non-fixed weights or bias |
| 1522 | // Find the shape of the weights tensor. In AndroidNN this will be [ 1, H, W, I * M ] |
| 1523 | const Operand* weightsOperand = GetInputOperand<hal_1_2::HalPolicy>(operation, 1, model); |
| 1524 | |
| 1525 | if (weightsOperand == nullptr) |
| 1526 | { |
| 1527 | return Fail("%s: Operand is invalid", __func__); |
| 1528 | } |
| 1529 | armnn::TransposeConvolution2dDescriptor desc; |
| 1530 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1531 | |
| 1532 | // Determine whether padding is implicit or explicit |
| 1533 | bool implicitPadding = operation.inputs.size() == 9; |
| 1534 | |
| 1535 | if (implicitPadding ) |
| 1536 | { |
| 1537 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 8, model, data); |
| 1538 | } |
| 1539 | else |
| 1540 | { |
| 1541 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 10, model, data); |
| 1542 | } |
| 1543 | |
| 1544 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout); |
| 1545 | unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex(); |
| 1546 | unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex(); |
| 1547 | |
| 1548 | const armnn::PermutationVector OHWIToOIHW = {0, 2, 3, 1}; |
| 1549 | |
| 1550 | // The shape of the weight is [depth_out, filter_height, filter_width, depth_in]. |
| 1551 | // We have to permute it to OIHW if the data layout is NCHW. |
| 1552 | const ConstTensorPin weightsPin = (desc.m_DataLayout == armnn::DataLayout::NCHW) ? |
| 1553 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data, OHWIToOIHW) : |
| 1554 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data); |
| 1555 | |
| 1556 | // Bias is a 1D tensor |
| 1557 | const ConstTensorPin biasPin = |
| 1558 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data); |
| 1559 | |
| 1560 | if (!weightsPin.IsValid()) |
| 1561 | { |
| 1562 | return Fail("%s: Operation has invalid weights", __func__); |
| 1563 | } |
| 1564 | |
| 1565 | if (!biasPin.IsValid()) |
| 1566 | { |
| 1567 | return Fail("%s: Operation has invalid biases", __func__); |
| 1568 | } |
| 1569 | |
| 1570 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 1571 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
| 1572 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 1573 | |
| 1574 | ActivationFn activation; |
| 1575 | |
| 1576 | if (implicitPadding) |
| 1577 | { |
| 1578 | android::nn::PaddingScheme paddingScheme; |
| 1579 | if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 4, paddingScheme, model, data) || |
| 1580 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideX, model, data) || |
| 1581 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_StrideY, model, data) || |
| 1582 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 7, activation, model, data)) |
| 1583 | { |
| 1584 | return Fail("%s: Operation has invalid inputs (implicit padding)", __func__); |
| 1585 | } |
| 1586 | |
| 1587 | const uint32_t kernelX = weights.GetShape()[widthIndex]; |
| 1588 | const uint32_t kernelY = weights.GetShape()[heightIndex]; |
| 1589 | const uint32_t inputX = inputInfo.GetShape()[widthIndex]; |
| 1590 | const uint32_t inputY = inputInfo.GetShape()[heightIndex]; |
| 1591 | |
| 1592 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 1593 | CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, paddingScheme); |
| 1594 | } |
| 1595 | else if (operation.inputs.size() == 11) |
| 1596 | { |
| 1597 | // explicit padding |
| 1598 | if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) || |
| 1599 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) || |
| 1600 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) || |
| 1601 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) || |
| 1602 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) || |
| 1603 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) || |
| 1604 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 9, activation, model, data)) |
| 1605 | { |
| 1606 | return Fail("%s: Operation has invalid inputs (explicit padding)", __func__); |
| 1607 | } |
| 1608 | } |
| 1609 | else |
| 1610 | { |
| 1611 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 1612 | } |
| 1613 | |
| 1614 | desc.m_BiasEnabled = true; |
| 1615 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 1616 | |
| 1617 | bool isSupported = false; |
| 1618 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 1619 | IsTransposeConvolution2dSupported, |
| 1620 | data.m_Backends, |
| 1621 | isSupported, |
| 1622 | inputInfo, |
| 1623 | outputInfo, |
| 1624 | desc, |
| 1625 | weights.GetInfo(), |
| 1626 | biases); |
| 1627 | if (!isSupported) |
| 1628 | { |
| 1629 | return false; |
| 1630 | } |
| 1631 | |
| 1632 | armnn::IConnectableLayer* startLayer = |
| 1633 | data.m_Network->AddTransposeConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 1634 | if (!startLayer) |
| 1635 | { |
| 1636 | return Fail("%s: AddTransposeConvolution2dLayer failed", __func__); |
| 1637 | } |
| 1638 | |
| 1639 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data); |
| 1640 | if (!endLayer) |
| 1641 | { |
| 1642 | return Fail("%s: ProcessActivation failed", __func__); |
| 1643 | } |
| 1644 | |
| 1645 | input.Connect(startLayer->GetInputSlot(0)); |
| 1646 | |
| 1647 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *endLayer, model, data); |
| 1648 | } |
| 1649 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 1650 | } // namespace hal_1_2 |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 1651 | } // namespace armnn_driver |