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