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 | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 8 | #include "OutputShapeUtils.hpp" |
| 9 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 10 | #include "../1.0/HalPolicy.hpp" |
| 11 | #include "../1.1/HalPolicy.hpp" |
| 12 | |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 13 | #include <DataLayoutIndexed.hpp> |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 14 | #include <Half.hpp> |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 15 | |
| 16 | #include <cmath> |
| 17 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 18 | namespace armnn_driver |
| 19 | { |
| 20 | namespace hal_1_2 |
| 21 | { |
| 22 | |
| 23 | bool HandledByV1_0(V1_2::OperationType operationType) |
| 24 | { |
| 25 | switch (static_cast<V1_0::OperationType>(operationType)) |
| 26 | { |
| 27 | case V1_0::OperationType::ADD: |
| 28 | case V1_0::OperationType::AVERAGE_POOL_2D: |
| 29 | case V1_0::OperationType::CONCATENATION: |
| 30 | case V1_0::OperationType::DEPTH_TO_SPACE: |
| 31 | case V1_0::OperationType::DEQUANTIZE: |
| 32 | case V1_0::OperationType::EMBEDDING_LOOKUP: |
| 33 | case V1_0::OperationType::FLOOR: |
| 34 | case V1_0::OperationType::FULLY_CONNECTED: |
| 35 | case V1_0::OperationType::HASHTABLE_LOOKUP: |
| 36 | case V1_0::OperationType::L2_NORMALIZATION: |
| 37 | case V1_0::OperationType::L2_POOL_2D: |
| 38 | case V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION: |
| 39 | case V1_0::OperationType::LOGISTIC: |
| 40 | case V1_0::OperationType::LSH_PROJECTION: |
| 41 | case V1_0::OperationType::LSTM: |
| 42 | case V1_0::OperationType::MAX_POOL_2D: |
| 43 | case V1_0::OperationType::MUL: |
| 44 | case V1_0::OperationType::RELU: |
| 45 | case V1_0::OperationType::RELU1: |
| 46 | case V1_0::OperationType::RELU6: |
| 47 | case V1_0::OperationType::RESHAPE: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 48 | case V1_0::OperationType::RNN: |
| 49 | case V1_0::OperationType::SOFTMAX: |
| 50 | case V1_0::OperationType::SPACE_TO_DEPTH: |
| 51 | case V1_0::OperationType::SVDF: |
| 52 | case V1_0::OperationType::TANH: |
| 53 | case V1_0::OperationType::OEM_OPERATION: |
| 54 | return true; |
| 55 | default: |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | bool HandledByV1_1(V1_2::OperationType operationType) |
| 61 | { |
| 62 | if (HandledByV1_0(operationType)) |
| 63 | { |
| 64 | return true; |
| 65 | } |
| 66 | switch (static_cast<V1_1::OperationType>(operationType)) |
| 67 | { |
| 68 | case V1_1::OperationType::BATCH_TO_SPACE_ND: |
| 69 | case V1_1::OperationType::DIV: |
| 70 | case V1_1::OperationType::MEAN: |
| 71 | case V1_1::OperationType::PAD: |
| 72 | case V1_1::OperationType::SPACE_TO_BATCH_ND: |
| 73 | case V1_1::OperationType::SQUEEZE: |
| 74 | case V1_1::OperationType::STRIDED_SLICE: |
| 75 | case V1_1::OperationType::SUB: |
| 76 | case V1_1::OperationType::TRANSPOSE: |
| 77 | return true; |
| 78 | default: |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | bool HandledByV1_0(const V1_2::Operation& operation) |
| 84 | { |
| 85 | return HandledByV1_0(operation.type); |
| 86 | } |
| 87 | |
| 88 | bool HandledByV1_1(const V1_2::Operation& operation) |
| 89 | { |
| 90 | return HandledByV1_1(operation.type); |
| 91 | } |
| 92 | |
| 93 | V1_0::OperationType CastToV1_0(V1_2::OperationType type) |
| 94 | { |
| 95 | return static_cast<V1_0::OperationType>(type); |
| 96 | } |
| 97 | |
| 98 | V1_1::OperationType CastToV1_1(V1_2::OperationType type) |
| 99 | { |
| 100 | return static_cast<V1_1::OperationType>(type); |
| 101 | } |
| 102 | |
| 103 | V1_0::Operation ConvertToV1_0(const V1_2::Operation& operation) |
| 104 | { |
| 105 | V1_0::Operation op; |
| 106 | op.type = CastToV1_0(operation.type); |
| 107 | op.inputs = operation.inputs; |
| 108 | op.outputs = operation.outputs; |
| 109 | return op; |
| 110 | } |
| 111 | |
| 112 | V1_1::Operation ConvertToV1_1(const V1_2::Operation& operation) |
| 113 | { |
| 114 | V1_1::Operation op; |
| 115 | op.type = CastToV1_1(operation.type); |
| 116 | op.inputs = operation.inputs; |
| 117 | op.outputs = operation.outputs; |
| 118 | return op; |
| 119 | } |
| 120 | |
| 121 | bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data) |
| 122 | { |
| 123 | if (HandledByV1_0(operation) && compliantWithV1_0(model)) |
| 124 | { |
| 125 | hal_1_0::HalPolicy::Operation v10Operation = ConvertToV1_0(operation); |
| 126 | hal_1_0::HalPolicy::Model v10Model = convertToV1_0(model); |
| 127 | |
| 128 | return hal_1_0::HalPolicy::ConvertOperation(v10Operation, v10Model, data); |
| 129 | } |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 130 | |
| 131 | if (HandledByV1_1(operation) && compliantWithV1_1(model)) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 132 | { |
| 133 | hal_1_1::HalPolicy::Operation v11Operation = ConvertToV1_1(operation); |
| 134 | hal_1_1::HalPolicy::Model v11Model = convertToV1_1(model); |
| 135 | |
| 136 | return hal_1_1::HalPolicy::ConvertOperation(v11Operation, v11Model, data); |
| 137 | } |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 138 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 139 | switch (operation.type) |
| 140 | { |
| 141 | case V1_2::OperationType::CONV_2D: |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 142 | return ConvertConv2d(operation, model, data); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 143 | case V1_2::OperationType::DEPTHWISE_CONV_2D: |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 144 | return ConvertDepthwiseConv2d(operation, model, data); |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 145 | case V1_2::OperationType::MAXIMUM: |
| 146 | return ConvertMaximum(operation, model, data); |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 147 | case V1_2::OperationType::PAD_V2: |
| 148 | return ConvertPadV2(operation, model, data); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 149 | case V1_2::OperationType::PRELU: |
| 150 | return ConvertPrelu(operation, model, data); |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 151 | case V1_2::OperationType::RESIZE_BILINEAR: |
| 152 | return ConvertResize(operation, model, data, armnn::ResizeMethod::Bilinear); |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 153 | case V1_2::OperationType::RESIZE_NEAREST_NEIGHBOR: |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 154 | return ConvertResize(operation, model, data, armnn::ResizeMethod::NearestNeighbor); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 155 | default: |
| 156 | return Fail("%s: Operation type %s not supported in ArmnnDriver", |
| 157 | __func__, toString(operation.type).c_str()); |
| 158 | } |
| 159 | } |
| 160 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 161 | bool HalPolicy::ConvertConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 162 | { |
| 163 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 164 | if (!input.IsValid()) |
| 165 | { |
| 166 | return Fail("%s: Operation has invalid inputs", __func__); |
| 167 | } |
| 168 | |
| 169 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 170 | if (!output) |
| 171 | { |
| 172 | return Fail("%s: Could not read output 0", __func__); |
| 173 | } |
| 174 | |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame^] | 175 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 176 | armnn::TensorInfo outputInfo = GetTensorInfoForOperand(*output); |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 177 | |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 178 | armnn::Convolution2dDescriptor desc; |
| 179 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 180 | |
| 181 | // Determine whether padding is implicit or explicit |
| 182 | bool implicitPadding = operation.inputs.size() == 7 || |
| 183 | (operation.inputs.size() >= 8 && |
| 184 | GetInputOperand<hal_1_2::HalPolicy>(operation, 7, model)->type == OperandType::BOOL); |
| 185 | |
| 186 | if (implicitPadding) |
| 187 | { |
| 188 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 7, model, data); |
| 189 | } |
| 190 | else if (operation.inputs.size() >= 10) |
| 191 | { |
| 192 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 10, model, data); |
| 193 | } |
| 194 | |
| 195 | const armnn::PermutationVector OHWIToOIHW = {0, 2, 3, 1}; |
| 196 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 197 | // ArmNN does not currently support non-fixed weights or bias |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 198 | // The NNAPI filter is always OHWI [depth_out, filter_height, filter_width, depth_in] but ArmNN expects the |
| 199 | // filter's height and width indices to match the input's height and width indices so we permute it to OIHW if |
| 200 | // the DataLayout is NCHW |
| 201 | const ConstTensorPin weightsPin = (desc.m_DataLayout == armnn::DataLayout::NCHW) ? |
| 202 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data, OHWIToOIHW) : |
| 203 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 204 | const ConstTensorPin biasPin = |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 205 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 206 | |
| 207 | if (!weightsPin.IsValid()) |
| 208 | { |
| 209 | return Fail("%s: Operation has invalid weights", __func__); |
| 210 | } |
| 211 | |
| 212 | if (!biasPin.IsValid()) |
| 213 | { |
| 214 | return Fail("%s: Operation has invalid biases", __func__); |
| 215 | } |
| 216 | |
| 217 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 218 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
| 219 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 220 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 221 | ActivationFn activation; |
| 222 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 223 | if (implicitPadding) |
| 224 | { |
| 225 | android::nn::PaddingScheme paddingScheme; |
| 226 | if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 227 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_StrideX, model, data) || |
| 228 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideY, model, data) || |
| 229 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 6, activation, model, data) || |
| 230 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 8, desc, model, data)) |
| 231 | { |
| 232 | return Fail("%s: Operation has invalid inputs (implicit padding)", __func__); |
| 233 | } |
| 234 | |
Mike Kelly | e1d60bb | 2019-07-11 11:44:52 +0100 | [diff] [blame] | 235 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout); |
| 236 | unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex(); |
| 237 | unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex(); |
| 238 | const uint32_t kernelX = weights.GetShape()[widthIndex]; |
| 239 | const uint32_t kernelY = weights.GetShape()[heightIndex]; |
| 240 | const uint32_t inputX = inputInfo.GetShape()[widthIndex]; |
| 241 | const uint32_t inputY = inputInfo.GetShape()[heightIndex]; |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 242 | |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 243 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 244 | 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] | 245 | |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 246 | } |
| 247 | else if (operation.inputs.size() >= 10) |
| 248 | { |
| 249 | // explicit padding |
| 250 | if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) || |
| 251 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) || |
| 252 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) || |
| 253 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) || |
| 254 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) || |
| 255 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) || |
| 256 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 9, activation, model, data) || |
| 257 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 11, desc, model, data)) |
| 258 | { |
| 259 | return Fail("%s: Operation has invalid inputs (explicit padding)", __func__); |
| 260 | } |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 261 | } |
| 262 | else |
| 263 | { |
| 264 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 265 | } |
| 266 | |
| 267 | desc.m_BiasEnabled = true; |
| 268 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 269 | |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame^] | 270 | if (IsDynamicOutput(outputInfo)) |
| 271 | { |
| 272 | try |
| 273 | { |
| 274 | ALOGD("Output shape not set, will infer from inputs"); |
| 275 | outputInfo.SetShape(InferConvolution2dOutputShape(inputInfo.GetShape(), |
| 276 | weights.GetInfo().GetShape(), |
| 277 | desc)); |
| 278 | } |
| 279 | catch (armnn::Exception& e) |
| 280 | { |
| 281 | return Fail("%s: Could not infer dynamic output shape: %s", __func__, e.what()); |
| 282 | } |
| 283 | } |
| 284 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 285 | bool isSupported = false; |
| 286 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 287 | IsConvolution2dSupported, |
| 288 | data.m_Backends, |
| 289 | isSupported, |
| 290 | inputInfo, |
| 291 | outputInfo, |
| 292 | desc, |
| 293 | weights.GetInfo(), |
| 294 | biases); |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame^] | 295 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 296 | if (!isSupported) |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 297 | { |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | armnn::IConnectableLayer* startLayer = |
| 302 | data.m_Network->AddConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 303 | |
| 304 | if (!startLayer) |
| 305 | { |
| 306 | return Fail("%s: AddConvolution2dLayer failed", __func__); |
| 307 | } |
| 308 | |
| 309 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data); |
| 310 | |
| 311 | if (!endLayer) |
| 312 | { |
| 313 | return Fail("%s: ProcessActivation failed", __func__); |
| 314 | } |
| 315 | |
| 316 | input.Connect(startLayer->GetInputSlot(0)); |
| 317 | |
Aron Virginas-Tar | 2b17312 | 2019-07-15 14:29:09 +0100 | [diff] [blame^] | 318 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, |
| 319 | 0, |
| 320 | *endLayer, |
| 321 | model, |
| 322 | data, |
| 323 | armnn::Optional<armnn::TensorInfo>(outputInfo)); |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | bool HalPolicy::ConvertDepthwiseConv2d(const Operation& operation, const Model& model, ConversionData& data) |
| 327 | { |
| 328 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 329 | |
| 330 | if (!input.IsValid()) |
| 331 | { |
| 332 | return Fail("%s: Operation has invalid inputs", __func__); |
| 333 | } |
| 334 | |
| 335 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 336 | |
| 337 | if (!output) |
| 338 | { |
| 339 | return Fail("%s: Could not read output 0", __func__); |
| 340 | } |
| 341 | |
| 342 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 343 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 344 | |
| 345 | // ArmNN does not currently support non-fixed weights or bias |
| 346 | // Find the shape of the weights tensor. In AndroidNN this will be [ 1, H, W, I * M ] |
| 347 | const Operand* weightsOperand = GetInputOperand<hal_1_2::HalPolicy>(operation, 1, model); |
| 348 | |
| 349 | if (weightsOperand == nullptr) |
| 350 | { |
| 351 | return Fail("%s: Operand is invalid", __func__); |
| 352 | } |
| 353 | armnn::DepthwiseConvolution2dDescriptor desc; |
| 354 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 355 | |
| 356 | // Determine whether padding is implicit or explicit |
| 357 | bool implicitPadding = operation.inputs.size() == 8 || |
| 358 | (operation.inputs.size() >= 9 && |
| 359 | GetInputOperand<hal_1_2::HalPolicy>(operation, 8, model)->type == OperandType::BOOL); |
| 360 | |
| 361 | // Look ahead to find the optional DataLayout, if present |
| 362 | const uint32_t dataLayoutFlagIndex = implicitPadding ? 8 : 11; |
| 363 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, dataLayoutFlagIndex, model, data); |
| 364 | |
| 365 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout); |
| 366 | unsigned int channelsIndex = dataLayoutIndexed.GetChannelsIndex(); |
| 367 | unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex(); |
| 368 | unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex(); |
| 369 | |
| 370 | // Reinterpret weight data as [ H, W, I, M ] |
| 371 | armnn::TensorShape weightsShape({ weightsOperand->dimensions[1], |
| 372 | weightsOperand->dimensions[2], |
| 373 | inputInfo.GetShape()[channelsIndex], |
| 374 | weightsOperand->dimensions[3] / inputInfo.GetShape()[channelsIndex] }); |
| 375 | |
| 376 | // Swizzle weight data [ H, W, I, M ] -> [ M, I, H, W ] |
| 377 | const armnn::PermutationVector HWIMToMIHW = { 2U, 3U, 1U, 0U }; |
| 378 | |
| 379 | const ConstTensorPin weightsPin = |
| 380 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, |
| 381 | 1, |
| 382 | model, |
| 383 | data, |
| 384 | HWIMToMIHW, |
| 385 | &weightsShape); |
| 386 | |
| 387 | // Bias is a 1D tensor |
| 388 | const ConstTensorPin biasPin = |
| 389 | ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data); |
| 390 | |
| 391 | if (!weightsPin.IsValid()) |
| 392 | { |
| 393 | return Fail("%s: Operation has invalid weights", __func__); |
| 394 | } |
| 395 | |
| 396 | if (!biasPin.IsValid()) |
| 397 | { |
| 398 | return Fail("%s: Operation has invalid biases", __func__); |
| 399 | } |
| 400 | |
| 401 | armnn::ConstTensor weights = weightsPin.GetConstTensor(); |
| 402 | armnn::ConstTensor bias = biasPin.GetConstTensor(); |
| 403 | SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo); |
| 404 | |
| 405 | ActivationFn activation; |
| 406 | |
| 407 | if (implicitPadding) |
| 408 | { |
| 409 | android::nn::PaddingScheme paddingScheme; |
| 410 | if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 3, paddingScheme, model, data) || |
| 411 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_StrideX, model, data) || |
| 412 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideY, model, data) || |
| 413 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 7, activation, model, data) || |
| 414 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 9, desc, model, data)) |
| 415 | { |
| 416 | return Fail("%s: Operation has invalid inputs (implicit padding)", __func__); |
| 417 | } |
| 418 | |
| 419 | const uint32_t kernelX = weights.GetShape()[3]; |
| 420 | const uint32_t kernelY = weights.GetShape()[2]; |
| 421 | const uint32_t inputX = inputInfo.GetShape()[widthIndex]; |
| 422 | const uint32_t inputY = inputInfo.GetShape()[heightIndex]; |
| 423 | |
Mike Kelly | 86b36d4 | 2019-07-12 16:39:33 +0100 | [diff] [blame] | 424 | CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, paddingScheme); |
| 425 | 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] | 426 | } |
| 427 | else if (operation.inputs.size() >= 11) |
| 428 | { |
| 429 | // explicit padding |
| 430 | if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) || |
| 431 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) || |
| 432 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) || |
| 433 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) || |
| 434 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) || |
| 435 | !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) || |
| 436 | !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 10, activation, model, data) || |
| 437 | !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 12, desc, model, data)) |
| 438 | { |
| 439 | return Fail("%s: Operation has invalid inputs (explicit padding)", __func__); |
| 440 | } |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | return Fail("%s: Unsupported number of operation inputs", __func__); |
| 445 | } |
| 446 | |
| 447 | desc.m_BiasEnabled = true; |
| 448 | armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo()); |
| 449 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 450 | bool isSupported = false; |
| 451 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 452 | IsDepthwiseConvolutionSupported, |
| 453 | data.m_Backends, |
| 454 | isSupported, |
| 455 | inputInfo, |
| 456 | outputInfo, |
| 457 | desc, |
| 458 | weights.GetInfo(), |
| 459 | biases); |
| 460 | if (!isSupported) |
Aron Virginas-Tar | 24e699d | 2019-06-17 14:47:46 +0100 | [diff] [blame] | 461 | { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | armnn::IConnectableLayer* startLayer = |
| 466 | data.m_Network->AddDepthwiseConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias)); |
| 467 | if (!startLayer) |
| 468 | { |
| 469 | return Fail("%s: AddDepthwiseConvolution2dLayer failed", __func__); |
| 470 | } |
| 471 | |
| 472 | armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data); |
| 473 | if (!endLayer) |
| 474 | { |
| 475 | return Fail("%s: ProcessActivation failed", __func__); |
| 476 | } |
| 477 | |
| 478 | input.Connect(startLayer->GetInputSlot(0)); |
| 479 | |
| 480 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *endLayer, model, data); |
| 481 | } |
| 482 | |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 483 | bool HalPolicy::ConvertMaximum(const Operation& operation, const Model& model, ConversionData& data) |
| 484 | { |
| 485 | LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 486 | LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data); |
| 487 | |
| 488 | if (!input0.IsValid() || !input1.IsValid()) |
| 489 | { |
| 490 | return Fail("%s: Operation has invalid inputs", __func__); |
| 491 | } |
| 492 | |
| 493 | const Operand* outputOperand = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 494 | if (!outputOperand) |
| 495 | { |
| 496 | return Fail("%s: Could not read output", __func__); |
| 497 | } |
| 498 | |
Aron Virginas-Tar | d759323 | 2019-07-16 13:17:06 +0100 | [diff] [blame] | 499 | armnn::TensorInfo outInfo = GetTensorInfoForOperand(*outputOperand); |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 500 | if (IsDynamicOutput(outInfo)) |
| 501 | { |
| 502 | ALOGD("Output shape not set, will infer from inputs"); |
| 503 | outInfo.SetShape(InferMaximumOutputShape(input0.GetTensorInfo().GetShape(), input1.GetTensorInfo().GetShape())); |
| 504 | } |
| 505 | |
Aron Virginas-Tar | d759323 | 2019-07-16 13:17:06 +0100 | [diff] [blame] | 506 | bool isSupported = false; |
| 507 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 508 | IsMaximumSupported, |
| 509 | data.m_Backends, |
| 510 | isSupported, |
| 511 | input0.GetTensorInfo(), |
| 512 | input1.GetTensorInfo(), |
| 513 | outInfo); |
| 514 | |
| 515 | if (!isSupported) |
Narumol Prangnawarat | 95b1ef6 | 2019-07-15 12:02:20 +0100 | [diff] [blame] | 516 | { |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | armnn::IConnectableLayer* layer = data.m_Network->AddMaximumLayer(); |
| 521 | assert(layer != nullptr); |
| 522 | BroadcastTensor(input0, input1, layer, *data.m_Network); |
| 523 | |
| 524 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, |
| 525 | 0, |
| 526 | *layer, |
| 527 | model, |
| 528 | data, |
| 529 | armnn::Optional<armnn::TensorInfo>(outInfo)); |
| 530 | } |
| 531 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 532 | bool HalPolicy::ConvertPadV2(const Operation& operation, const Model& model, ConversionData& data) |
| 533 | { |
| 534 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 535 | if (!input.IsValid()) |
| 536 | { |
| 537 | return Fail("%s: Could not read input 0", __func__); |
| 538 | } |
| 539 | |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 540 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 541 | if (!output) |
| 542 | { |
| 543 | return Fail("%s: Could not read output", __func__); |
| 544 | } |
| 545 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 546 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 547 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 548 | |
| 549 | armnn::PadDescriptor descriptor; |
| 550 | if (!ConvertPaddings<hal_1_2::HalPolicy>(operation, model, data, rank, descriptor)) |
| 551 | { |
| 552 | return Fail("%s: Could not convert paddings", __func__); |
| 553 | } |
| 554 | |
Sadik Armagan | 310d8ff | 2019-07-11 10:53:38 +0100 | [diff] [blame] | 555 | armnn::TensorInfo outputInfo = GetTensorInfoForOperand(*output); |
| 556 | if (IsDynamicOutput(outputInfo)) |
| 557 | { |
| 558 | ALOGD("Output shape not set, will infer from inputs"); |
| 559 | outputInfo.SetShape(InferPadOutputShape(inputInfo.GetShape(), descriptor.m_PadList)); |
| 560 | } |
| 561 | |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 562 | // Determine type of padding value |
| 563 | OperandType operandType0; |
| 564 | OperandType operandType2; |
| 565 | |
| 566 | if (!GetOperandType<hal_1_2::HalPolicy>(operation, 0, model, operandType0) || |
| 567 | !GetOperandType<hal_1_2::HalPolicy>(operation, 2, model, operandType2)) |
| 568 | { |
| 569 | return Fail("%s: Operation has invalid inputs", __func__); |
| 570 | } |
| 571 | |
| 572 | // Read value to use for padding |
| 573 | if (operandType0 == OperandType::TENSOR_FLOAT16 && operandType2 == OperandType::FLOAT16) |
| 574 | { |
| 575 | armnn::Half f16PadValue; |
| 576 | if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 2, operandType2, f16PadValue, model, data)) |
| 577 | { |
| 578 | return Fail("%s: Could not read input 2 (FLOAT16)", __func__); |
| 579 | } |
| 580 | |
| 581 | descriptor.m_PadValue = f16PadValue; |
| 582 | } |
| 583 | else if (operandType0 == OperandType::TENSOR_FLOAT32 && operandType2 == OperandType::FLOAT32) |
| 584 | { |
| 585 | if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 2, descriptor.m_PadValue, model, data)) |
| 586 | { |
| 587 | return Fail("%s: Could not read input 2 (FLOAT32)", __func__); |
| 588 | } |
| 589 | } |
| 590 | else if (operandType0 == OperandType::TENSOR_QUANT8_ASYMM && operandType2 == OperandType::INT32) |
| 591 | { |
| 592 | int32_t quantizedPadValue = 0; |
| 593 | if (!GetInputInt32<hal_1_2::HalPolicy>(operation, 2, quantizedPadValue, model, data)) |
| 594 | { |
| 595 | return Fail("%s: Could not read input 2 (INT32)", __func__); |
| 596 | } |
| 597 | |
| 598 | descriptor.m_PadValue = armnn::Dequantize(quantizedPadValue, |
| 599 | inputInfo.GetQuantizationScale(), |
| 600 | inputInfo.GetQuantizationOffset()); |
| 601 | } |
| 602 | else |
| 603 | { |
| 604 | return Fail("%s: Operation has invalid inputs: type mismatch", __func__); |
| 605 | } |
| 606 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 607 | bool isSupported = false; |
| 608 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 609 | IsPadSupported, |
| 610 | data.m_Backends, |
| 611 | isSupported, |
| 612 | inputInfo, |
| 613 | outputInfo, |
| 614 | descriptor); |
| 615 | if (!isSupported) |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 616 | { |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor); |
| 621 | assert(layer != nullptr); |
| 622 | input.Connect(layer->GetInputSlot(0)); |
| 623 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 624 | |
Sadik Armagan | 310d8ff | 2019-07-11 10:53:38 +0100 | [diff] [blame] | 625 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, |
| 626 | 0, |
| 627 | *layer, |
| 628 | model, |
| 629 | data, |
| 630 | armnn::Optional<armnn::TensorInfo>(outputInfo)); |
Aron Virginas-Tar | cb8ac84 | 2019-07-05 15:47:07 +0100 | [diff] [blame] | 631 | } |
| 632 | |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 633 | bool HalPolicy::ConvertPrelu(const Operation& operation, const Model& model, ConversionData& data) |
| 634 | { |
| 635 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 636 | LayerInputHandle alpha = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data); |
| 637 | |
| 638 | if (!input.IsValid() || !alpha.IsValid()) |
| 639 | { |
| 640 | return Fail("%s: Operation has invalid inputs", __func__); |
| 641 | } |
| 642 | |
| 643 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 644 | |
| 645 | if (!output) |
| 646 | { |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 647 | return Fail("%s: Could not read output", __func__); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 651 | const armnn::TensorInfo& alphaInfo = alpha.GetTensorInfo(); |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 652 | |
| 653 | armnn::TensorInfo outputInfo = GetTensorInfoForOperand(*output); |
Aron Virginas-Tar | 366e0a6 | 2019-07-10 13:01:41 +0100 | [diff] [blame] | 654 | if (IsDynamicOutput(outputInfo)) |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 655 | { |
| 656 | ALOGD("Output shape not set, will infer from inputs"); |
| 657 | outputInfo.SetShape(InferPreluOutputShape(inputInfo.GetShape(), alphaInfo.GetShape())); |
| 658 | } |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 659 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 660 | bool isSupported = false; |
| 661 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 662 | IsPreluSupported, |
| 663 | data.m_Backends, |
| 664 | isSupported, |
| 665 | inputInfo, |
| 666 | alphaInfo, |
| 667 | outputInfo); |
| 668 | if (!isSupported) |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 669 | { |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | armnn::IConnectableLayer* const layer = data.m_Network->AddPreluLayer(); |
| 674 | |
| 675 | if (!layer) |
| 676 | { |
| 677 | return Fail("%s: AddPreluLayer failed", __func__); |
| 678 | } |
| 679 | |
Matteo Martincigh | 0bd89a8 | 2019-07-02 16:53:10 +0100 | [diff] [blame] | 680 | BroadcastTensor(input, alpha, layer, *data.m_Network); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 681 | |
Aron Virginas-Tar | f03fcf0 | 2019-07-09 17:44:24 +0100 | [diff] [blame] | 682 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, |
| 683 | 0, |
| 684 | *layer, |
| 685 | model, |
| 686 | data, |
| 687 | armnn::Optional<armnn::TensorInfo>(outputInfo)); |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 690 | bool HalPolicy::ConvertResize(const Operation& operation, |
| 691 | const Model& model, |
| 692 | ConversionData& data, |
| 693 | armnn::ResizeMethod resizeMethod) |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 694 | { |
| 695 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 696 | if (!input.IsValid()) |
| 697 | { |
| 698 | return Fail("%s: Could not read input 0", __func__); |
| 699 | } |
| 700 | |
| 701 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 702 | if (!output) |
| 703 | { |
| 704 | return Fail("%s: Could not read output 0", __func__); |
| 705 | } |
| 706 | |
| 707 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 708 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
| 709 | |
| 710 | armnn::ResizeDescriptor descriptor; |
Aron Virginas-Tar | fb2fa29 | 2019-07-04 11:59:48 +0100 | [diff] [blame] | 711 | descriptor.m_Method = resizeMethod; |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 712 | descriptor.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 3, model, data); |
| 713 | |
| 714 | OperandType operandType1; |
| 715 | OperandType operandType2; |
| 716 | |
| 717 | if (!GetOperandType<hal_1_2::HalPolicy>(operation, 1, model, operandType1) || |
| 718 | !GetOperandType<hal_1_2::HalPolicy>(operation, 2, model, operandType2)) |
| 719 | { |
| 720 | return Fail("%s: Operation has invalid inputs", __func__); |
| 721 | } |
| 722 | |
| 723 | if (operandType1 != operandType2) |
| 724 | { |
| 725 | return Fail("%s: Operation has invalid inputs. Type of input 1 and 2 should be the same", __func__); |
| 726 | } |
| 727 | |
| 728 | if (operandType1 == OperandType::INT32) |
| 729 | { |
| 730 | // Case 1: resizing by shape |
| 731 | int32_t targetWidth = 0; |
| 732 | int32_t targetHeight = 0; |
| 733 | |
| 734 | if (!GetInputInt32<hal_1_2::HalPolicy>(operation, 1, targetWidth, model, data) || |
| 735 | !GetInputInt32<hal_1_2::HalPolicy>(operation, 2, targetHeight, model, data)) |
| 736 | { |
| 737 | return Fail("%s: Operation has invalid inputs for resizing by shape", __func__); |
| 738 | } |
| 739 | |
| 740 | if (targetWidth < 0 || targetHeight < 0) |
| 741 | { |
| 742 | return Fail("%s: Operation has invalid inputs for resizing by shape. " |
| 743 | "Target width/height cannot be < 0", __func__); |
| 744 | } |
| 745 | |
| 746 | descriptor.m_TargetWidth = static_cast<uint32_t>(targetWidth); |
| 747 | descriptor.m_TargetWidth = static_cast<uint32_t>(targetHeight); |
| 748 | } |
| 749 | else if (operandType1 == OperandType::FLOAT32) |
| 750 | { |
| 751 | // Case 2: resizing by scale |
| 752 | float widthScale = 1.0f; |
| 753 | float heightScale = 1.0f; |
| 754 | |
| 755 | if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 1, widthScale, model, data) || |
| 756 | !GetInputFloat32<hal_1_2::HalPolicy>(operation, 2, heightScale, model, data)) |
| 757 | { |
| 758 | return Fail("%s: Operation has invalid inputs for resizing by scale", __func__); |
| 759 | } |
| 760 | |
| 761 | const armnn::TensorShape& inputShape = inputInfo.GetShape(); |
| 762 | armnnUtils::DataLayoutIndexed dataLayoutIndexed(descriptor.m_DataLayout); |
| 763 | |
| 764 | float width = inputShape[dataLayoutIndexed.GetWidthIndex()]; |
| 765 | float height = inputShape[dataLayoutIndexed.GetHeightIndex()]; |
| 766 | |
| 767 | descriptor.m_TargetWidth = std::floor(width * widthScale); |
| 768 | descriptor.m_TargetHeight = std::floor(height * heightScale); |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | // NOTE: FLOAT16 scales are not supported |
| 773 | return false; |
| 774 | } |
| 775 | |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 776 | bool isSupported = false; |
| 777 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 778 | IsResizeSupported, |
| 779 | data.m_Backends, |
| 780 | isSupported, |
| 781 | inputInfo, |
| 782 | outputInfo, |
| 783 | descriptor); |
| 784 | if (!isSupported) |
Aron Virginas-Tar | 7a6d11b | 2019-07-03 15:27:08 +0100 | [diff] [blame] | 785 | { |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | armnn::IConnectableLayer* layer = data.m_Network->AddResizeLayer(descriptor); |
| 790 | |
| 791 | assert(layer != nullptr); |
| 792 | |
| 793 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo); |
| 794 | input.Connect(layer->GetInputSlot(0)); |
| 795 | |
| 796 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
| 797 | } |
| 798 | |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 799 | bool HalPolicy::ConvertSpaceToDepth(const Operation& operation, const Model& model, ConversionData& data) |
| 800 | { |
| 801 | LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data); |
| 802 | |
| 803 | if (!input.IsValid() ) |
| 804 | { |
| 805 | return Fail("%s: Operation has invalid inputs", __func__); |
| 806 | } |
| 807 | |
| 808 | const armnn::TensorInfo& inputInfo = input.GetTensorInfo(); |
| 809 | unsigned int rank = inputInfo.GetNumDimensions(); |
| 810 | |
| 811 | if (rank != 4) |
| 812 | { |
| 813 | return Fail("%s: Only inputs with rank 4 are supported", __func__); |
| 814 | } |
| 815 | |
| 816 | armnn::SpaceToDepthDescriptor desc; |
| 817 | |
| 818 | GetInputScalar<hal_1_2::HalPolicy>(operation, 1, OperandType::INT32, desc.m_BlockSize, model, data); |
| 819 | |
| 820 | if (desc.m_BlockSize <= 1) |
| 821 | { |
| 822 | return Fail("%s: Block size must be at least 1 in all dimensions"); |
| 823 | } |
| 824 | |
| 825 | desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 2, model, data); |
| 826 | |
| 827 | const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model); |
| 828 | if (!output) |
| 829 | { |
| 830 | return Fail("%s: Could not read output 0", __func__); |
| 831 | } |
| 832 | |
| 833 | const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output); |
Ferran Balaguer | d30093c | 2019-07-09 17:04:47 +0100 | [diff] [blame] | 834 | |
| 835 | bool isSupported = false; |
| 836 | FORWARD_LAYER_SUPPORT_FUNC(__func__, |
| 837 | IsSpaceToDepthSupported, |
| 838 | data.m_Backends, |
| 839 | isSupported, |
| 840 | inputInfo, |
| 841 | outputInfo, |
| 842 | desc); |
| 843 | if (!isSupported) |
Keith Davis | a6bc52f | 2019-06-26 09:39:49 +0100 | [diff] [blame] | 844 | { |
| 845 | return false; |
| 846 | } |
| 847 | |
| 848 | armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToDepthLayer(desc); |
| 849 | assert(layer != nullptr); |
| 850 | input.Connect(layer->GetInputSlot(0)); |
| 851 | |
| 852 | return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data); |
| 853 | } |
| 854 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 855 | } // namespace hal_1_2 |
Matteo Martincigh | 17ffff3 | 2019-06-27 14:12:55 +0100 | [diff] [blame] | 856 | } // namespace armnn_driver |