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