blob: e084947e52b6a5005340d3530ca9cbd1fb8e6a32 [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::DEPTH_TO_SPACE:
29 case V1_0::OperationType::DEQUANTIZE:
30 case V1_0::OperationType::EMBEDDING_LOOKUP:
31 case V1_0::OperationType::FLOOR:
32 case V1_0::OperationType::FULLY_CONNECTED:
33 case V1_0::OperationType::HASHTABLE_LOOKUP:
34 case V1_0::OperationType::L2_NORMALIZATION:
Mike Kellyb5fdf382019-06-11 16:35:25 +010035 case V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION:
36 case V1_0::OperationType::LOGISTIC:
37 case V1_0::OperationType::LSH_PROJECTION:
Mike Kellyb5fdf382019-06-11 16:35:25 +010038 case V1_0::OperationType::MUL:
Mike Kellyb5fdf382019-06-11 16:35:25 +010039 case V1_0::OperationType::RESHAPE:
Mike Kellyb5fdf382019-06-11 16:35:25 +010040 case V1_0::OperationType::RNN:
Mike Kellyb5fdf382019-06-11 16:35:25 +010041 case V1_0::OperationType::SVDF:
Mike Kellyb5fdf382019-06-11 16:35:25 +010042 case V1_0::OperationType::OEM_OPERATION:
43 return true;
44 default:
45 return false;
46 }
47}
48
49bool HandledByV1_1(V1_2::OperationType operationType)
50{
51 if (HandledByV1_0(operationType))
52 {
53 return true;
54 }
55 switch (static_cast<V1_1::OperationType>(operationType))
56 {
Mike Kellyb5fdf382019-06-11 16:35:25 +010057 case V1_1::OperationType::DIV:
58 case V1_1::OperationType::MEAN:
Mike Kellyb5fdf382019-06-11 16:35:25 +010059 case V1_1::OperationType::SQUEEZE:
60 case V1_1::OperationType::STRIDED_SLICE:
Mike Kellyb5fdf382019-06-11 16:35:25 +010061 case V1_1::OperationType::TRANSPOSE:
62 return true;
63 default:
64 return false;
65 }
66}
67
68bool HandledByV1_0(const V1_2::Operation& operation)
69{
70 return HandledByV1_0(operation.type);
71}
72
73bool HandledByV1_1(const V1_2::Operation& operation)
74{
75 return HandledByV1_1(operation.type);
76}
77
78V1_0::OperationType CastToV1_0(V1_2::OperationType type)
79{
80 return static_cast<V1_0::OperationType>(type);
81}
82
83V1_1::OperationType CastToV1_1(V1_2::OperationType type)
84{
85 return static_cast<V1_1::OperationType>(type);
86}
87
88V1_0::Operation ConvertToV1_0(const V1_2::Operation& operation)
89{
90 V1_0::Operation op;
91 op.type = CastToV1_0(operation.type);
92 op.inputs = operation.inputs;
93 op.outputs = operation.outputs;
94 return op;
95}
96
97V1_1::Operation ConvertToV1_1(const V1_2::Operation& operation)
98{
99 V1_1::Operation op;
100 op.type = CastToV1_1(operation.type);
101 op.inputs = operation.inputs;
102 op.outputs = operation.outputs;
103 return op;
104}
105
106bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data)
107{
108 if (HandledByV1_0(operation) && compliantWithV1_0(model))
109 {
110 hal_1_0::HalPolicy::Operation v10Operation = ConvertToV1_0(operation);
111 hal_1_0::HalPolicy::Model v10Model = convertToV1_0(model);
112
113 return hal_1_0::HalPolicy::ConvertOperation(v10Operation, v10Model, data);
114 }
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100115
116 if (HandledByV1_1(operation) && compliantWithV1_1(model))
Mike Kellyb5fdf382019-06-11 16:35:25 +0100117 {
118 hal_1_1::HalPolicy::Operation v11Operation = ConvertToV1_1(operation);
119 hal_1_1::HalPolicy::Model v11Model = convertToV1_1(model);
120
121 return hal_1_1::HalPolicy::ConvertOperation(v11Operation, v11Model, data);
122 }
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100123
Mike Kellyb5fdf382019-06-11 16:35:25 +0100124 switch (operation.type)
125 {
Sadik Armagan15d63e22019-07-26 16:59:35 +0100126 case V1_2::OperationType::AVERAGE_POOL_2D:
127 return ConvertAveragePool2d(operation, model, data);
Finn Williams23b87b32019-07-30 11:44:05 +0100128 case V1_2::OperationType::BATCH_TO_SPACE_ND:
129 return ConvertBatchToSpaceNd(operation, model, data);
Mike Kellyb8805202019-07-31 17:25:43 +0100130 case V1_2::OperationType::CONCATENATION:
131 return ConvertConcatenation(operation, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100132 case V1_2::OperationType::CONV_2D:
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100133 return ConvertConv2d(operation, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100134 case V1_2::OperationType::DEPTHWISE_CONV_2D:
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100135 return ConvertDepthwiseConv2d(operation, model, data);
Sadik Armagan15d63e22019-07-26 16:59:35 +0100136 case V1_2::OperationType::L2_POOL_2D:
137 return ConvertL2Pool2d(operation, model, data);
138 case V1_2::OperationType::MAX_POOL_2D:
139 return ConvertMaxPool2d(operation, model, data);
Narumol Prangnawarat95b1ef62019-07-15 12:02:20 +0100140 case V1_2::OperationType::MAXIMUM:
141 return ConvertMaximum(operation, model, data);
Ellen Norris-Thompson1cb29aa2019-07-11 17:27:37 +0100142 case V1_2::OperationType::MINIMUM:
143 return ConvertMinimum(operation, model, data);
Mike Kelly3c673942019-07-25 09:26:06 +0100144 case V1_2::OperationType::PAD:
Aron Virginas-Tarc921f6b2019-07-25 10:14:33 +0100145 return ConvertPad(operation, model, data);
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100146 case V1_2::OperationType::PAD_V2:
147 return ConvertPadV2(operation, model, data);
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100148 case V1_2::OperationType::PRELU:
149 return ConvertPrelu(operation, model, data);
Sadik Armagan5a476a82019-07-30 09:43:18 +0100150 case V1_2::OperationType::QUANTIZE:
151 return ConvertQuantize(operation, model, data);
Ellen Norris-Thompson7efb46d2019-07-24 17:39:19 +0100152 case V1_2::OperationType::QUANTIZED_16BIT_LSTM:
153 return ConvertQuantizedLstm(operation, model, data);
Sadik Armagan61113162019-07-25 09:09:40 +0100154 case V1_2::OperationType::RELU:
155 return ConvertReLu(operation, model, data);
156 case V1_2::OperationType::RELU1:
157 return ConvertReLu1(operation, model, data);
158 case V1_2::OperationType::RELU6:
159 return ConvertReLu6(operation, model, data);
Aron Virginas-Tarfb2fa292019-07-04 11:59:48 +0100160 case V1_2::OperationType::RESIZE_BILINEAR:
161 return ConvertResize(operation, model, data, armnn::ResizeMethod::Bilinear);
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +0100162 case V1_2::OperationType::RESIZE_NEAREST_NEIGHBOR:
Aron Virginas-Tarfb2fa292019-07-04 11:59:48 +0100163 return ConvertResize(operation, model, data, armnn::ResizeMethod::NearestNeighbor);
David Monahan613b49c2019-06-27 11:37:47 +0100164 case V1_2::OperationType::TRANSPOSE_CONV_2D:
Aron Virginas-Tar8b991682019-07-31 12:54:59 +0100165 return ConvertTransposeConv2d(operation, model, data);
Francis Murtagh074c25a2019-07-22 16:40:57 +0100166 case V1_2::OperationType::SOFTMAX:
167 return ConvertSoftmax(operation, model, data);
Finn Williamsd74c5052019-07-30 17:06:00 +0100168 case V1_2::OperationType::SPACE_TO_BATCH_ND :
169 return ConvertSpaceToBatchNd(operation, model, data);
Aron Virginas-Tarad1ab532019-07-25 11:24:42 +0100170 case V1_2::OperationType::SPACE_TO_DEPTH:
171 return ConvertSpaceToDepth(operation, model, data);
Mike Kelly0a879362019-07-29 16:56:31 +0100172 case V1_2::OperationType::SUB:
173 return ConvertSub(operation, model, data);
Sadik Armagan61113162019-07-25 09:09:40 +0100174 case V1_2::OperationType::TANH:
175 return ConvertTanH(operation, model, data);
Ferran Balaguerb2397fd2019-07-25 12:12:39 +0100176 case V1_2::OperationType::LSTM:
177 return ConvertLstm(operation, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100178 default:
179 return Fail("%s: Operation type %s not supported in ArmnnDriver",
180 __func__, toString(operation.type).c_str());
181 }
182}
183
Sadik Armagan15d63e22019-07-26 16:59:35 +0100184bool HalPolicy::ConvertAveragePool2d(const Operation& operation, const Model& model, ConversionData& data)
185{
186 ALOGV("hal_1_2::HalPolicy::ConvertAveragePool2d()");
187 return ConvertPooling2d<hal_1_2::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Average, model, data);
188}
189
Finn Williams23b87b32019-07-30 11:44:05 +0100190bool HalPolicy::ConvertBatchToSpaceNd(const Operation& operation, const Model& model, ConversionData& data)
191{
192 ALOGV("hal_1_2::HalPolicy::ConvertBatchToSpaceNd()");
193 return ::ConvertBatchToSpaceNd<hal_1_2::HalPolicy>(operation, model, data);
194}
195
Mike Kellyb8805202019-07-31 17:25:43 +0100196bool HalPolicy::ConvertConcatenation(const Operation& operation, const Model& model, ConversionData& data)
197{
198 ALOGV("hal_1_2::HalPolicy::ConvertConcatenation()");
199 return ::ConvertConcatenation<hal_1_2::HalPolicy>(operation, model, data);
200}
201
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100202bool HalPolicy::ConvertConv2d(const Operation& operation, const Model& model, ConversionData& data)
203{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100204 ALOGV("hal_1_2::HalPolicy::ConvertConv2d()");
205
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100206 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
207 if (!input.IsValid())
208 {
209 return Fail("%s: Operation has invalid inputs", __func__);
210 }
211
212 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
213 if (!output)
214 {
215 return Fail("%s: Could not read output 0", __func__);
216 }
217
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100218 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
219 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
220
221 if (IsDynamicTensor(outputInfo))
222 {
223 return Fail("%s: Dynamic output tensors are not supported", __func__);
224 }
Aron Virginas-Tar366e0a62019-07-10 13:01:41 +0100225
Mike Kellye1d60bb2019-07-11 11:44:52 +0100226 armnn::Convolution2dDescriptor desc;
227 desc.m_DataLayout = armnn::DataLayout::NHWC;
228
229 // Determine whether padding is implicit or explicit
230 bool implicitPadding = operation.inputs.size() == 7 ||
231 (operation.inputs.size() >= 8 &&
232 GetInputOperand<hal_1_2::HalPolicy>(operation, 7, model)->type == OperandType::BOOL);
233
234 if (implicitPadding)
235 {
236 desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 7, model, data);
237 }
238 else if (operation.inputs.size() >= 10)
239 {
240 desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 10, model, data);
241 }
242
243 const armnn::PermutationVector OHWIToOIHW = {0, 2, 3, 1};
244
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100245 // ArmNN does not currently support non-fixed weights or bias
Mike Kellye1d60bb2019-07-11 11:44:52 +0100246 // The NNAPI filter is always OHWI [depth_out, filter_height, filter_width, depth_in] but ArmNN expects the
247 // filter's height and width indices to match the input's height and width indices so we permute it to OIHW if
248 // the DataLayout is NCHW
249 const ConstTensorPin weightsPin = (desc.m_DataLayout == armnn::DataLayout::NCHW) ?
250 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data, OHWIToOIHW) :
251 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data);
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100252 const ConstTensorPin biasPin =
Mike Kellye1d60bb2019-07-11 11:44:52 +0100253 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data);
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100254
255 if (!weightsPin.IsValid())
256 {
257 return Fail("%s: Operation has invalid weights", __func__);
258 }
259
260 if (!biasPin.IsValid())
261 {
262 return Fail("%s: Operation has invalid biases", __func__);
263 }
264
265 armnn::ConstTensor weights = weightsPin.GetConstTensor();
266 armnn::ConstTensor bias = biasPin.GetConstTensor();
267 SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo);
268
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100269 ActivationFn activation;
270
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100271 if (implicitPadding)
272 {
273 android::nn::PaddingScheme paddingScheme;
274 if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 3, paddingScheme, model, data) ||
275 !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_StrideX, model, data) ||
276 !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideY, model, data) ||
277 !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 6, activation, model, data) ||
278 !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 8, desc, model, data))
279 {
280 return Fail("%s: Operation has invalid inputs (implicit padding)", __func__);
281 }
282
Mike Kellye1d60bb2019-07-11 11:44:52 +0100283 armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout);
284 unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex();
285 unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex();
286 const uint32_t kernelX = weights.GetShape()[widthIndex];
287 const uint32_t kernelY = weights.GetShape()[heightIndex];
288 const uint32_t inputX = inputInfo.GetShape()[widthIndex];
289 const uint32_t inputY = inputInfo.GetShape()[heightIndex];
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100290
Mike Kelly86b36d42019-07-12 16:39:33 +0100291 CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, paddingScheme);
292 CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, paddingScheme);
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100293
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100294 }
295 else if (operation.inputs.size() >= 10)
296 {
297 // explicit padding
298 if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) ||
299 !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) ||
300 !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) ||
301 !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) ||
302 !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) ||
303 !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) ||
304 !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 9, activation, model, data) ||
305 !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 11, desc, model, data))
306 {
307 return Fail("%s: Operation has invalid inputs (explicit padding)", __func__);
308 }
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100309 }
310 else
311 {
312 return Fail("%s: Unsupported number of operation inputs", __func__);
313 }
314
315 desc.m_BiasEnabled = true;
316 armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo());
317
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100318 bool isSupported = false;
319 FORWARD_LAYER_SUPPORT_FUNC(__func__,
320 IsConvolution2dSupported,
321 data.m_Backends,
322 isSupported,
323 inputInfo,
324 outputInfo,
325 desc,
326 weights.GetInfo(),
327 biases);
Aron Virginas-Tar2b173122019-07-15 14:29:09 +0100328
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100329 if (!isSupported)
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100330 {
331 return false;
332 }
333
334 armnn::IConnectableLayer* startLayer =
335 data.m_Network->AddConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias));
336
337 if (!startLayer)
338 {
339 return Fail("%s: AddConvolution2dLayer failed", __func__);
340 }
341
342 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data);
343
344 if (!endLayer)
345 {
346 return Fail("%s: ProcessActivation failed", __func__);
347 }
348
349 input.Connect(startLayer->GetInputSlot(0));
350
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100351 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *endLayer, model, data);
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100352}
353
354bool HalPolicy::ConvertDepthwiseConv2d(const Operation& operation, const Model& model, ConversionData& data)
355{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100356 ALOGV("hal_1_2::HalPolicy::ConvertDepthwiseConv2d()");
357
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100358 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
359
360 if (!input.IsValid())
361 {
362 return Fail("%s: Operation has invalid inputs", __func__);
363 }
364
365 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
366
367 if (!output)
368 {
369 return Fail("%s: Could not read output 0", __func__);
370 }
371
372 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100373 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
374
375 if (IsDynamicTensor(outputInfo))
376 {
377 return Fail("%s: Dynamic output tensors are not supported", __func__);
378 }
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100379
380 // ArmNN does not currently support non-fixed weights or bias
381 // Find the shape of the weights tensor. In AndroidNN this will be [ 1, H, W, I * M ]
382 const Operand* weightsOperand = GetInputOperand<hal_1_2::HalPolicy>(operation, 1, model);
383
384 if (weightsOperand == nullptr)
385 {
386 return Fail("%s: Operand is invalid", __func__);
387 }
388 armnn::DepthwiseConvolution2dDescriptor desc;
389 desc.m_DataLayout = armnn::DataLayout::NHWC;
390
391 // Determine whether padding is implicit or explicit
392 bool implicitPadding = operation.inputs.size() == 8 ||
393 (operation.inputs.size() >= 9 &&
394 GetInputOperand<hal_1_2::HalPolicy>(operation, 8, model)->type == OperandType::BOOL);
395
396 // Look ahead to find the optional DataLayout, if present
397 const uint32_t dataLayoutFlagIndex = implicitPadding ? 8 : 11;
398 desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, dataLayoutFlagIndex, model, data);
399
400 armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout);
401 unsigned int channelsIndex = dataLayoutIndexed.GetChannelsIndex();
402 unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex();
403 unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex();
404
405 // Reinterpret weight data as [ H, W, I, M ]
406 armnn::TensorShape weightsShape({ weightsOperand->dimensions[1],
407 weightsOperand->dimensions[2],
408 inputInfo.GetShape()[channelsIndex],
409 weightsOperand->dimensions[3] / inputInfo.GetShape()[channelsIndex] });
410
411 // Swizzle weight data [ H, W, I, M ] -> [ M, I, H, W ]
412 const armnn::PermutationVector HWIMToMIHW = { 2U, 3U, 1U, 0U };
413
414 const ConstTensorPin weightsPin =
415 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
416 1,
417 model,
418 data,
419 HWIMToMIHW,
420 &weightsShape);
421
422 // Bias is a 1D tensor
423 const ConstTensorPin biasPin =
424 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data);
425
426 if (!weightsPin.IsValid())
427 {
428 return Fail("%s: Operation has invalid weights", __func__);
429 }
430
431 if (!biasPin.IsValid())
432 {
433 return Fail("%s: Operation has invalid biases", __func__);
434 }
435
436 armnn::ConstTensor weights = weightsPin.GetConstTensor();
437 armnn::ConstTensor bias = biasPin.GetConstTensor();
438 SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo);
439
440 ActivationFn activation;
441
442 if (implicitPadding)
443 {
444 android::nn::PaddingScheme paddingScheme;
445 if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 3, paddingScheme, model, data) ||
446 !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_StrideX, model, data) ||
447 !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideY, model, data) ||
448 !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 7, activation, model, data) ||
449 !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 9, desc, model, data))
450 {
451 return Fail("%s: Operation has invalid inputs (implicit padding)", __func__);
452 }
453
454 const uint32_t kernelX = weights.GetShape()[3];
455 const uint32_t kernelY = weights.GetShape()[2];
456 const uint32_t inputX = inputInfo.GetShape()[widthIndex];
457 const uint32_t inputY = inputInfo.GetShape()[heightIndex];
458
Mike Kelly86b36d42019-07-12 16:39:33 +0100459 CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, paddingScheme);
460 CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, paddingScheme);
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100461 }
462 else if (operation.inputs.size() >= 11)
463 {
464 // explicit padding
465 if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) ||
466 !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) ||
467 !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) ||
468 !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) ||
469 !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) ||
470 !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) ||
471 !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 10, activation, model, data) ||
472 !GetOptionalConvolutionDilationParams<hal_1_2::HalPolicy>(operation, 12, desc, model, data))
473 {
474 return Fail("%s: Operation has invalid inputs (explicit padding)", __func__);
475 }
476 }
477 else
478 {
479 return Fail("%s: Unsupported number of operation inputs", __func__);
480 }
481
482 desc.m_BiasEnabled = true;
483 armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo());
484
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100485 bool isSupported = false;
486 FORWARD_LAYER_SUPPORT_FUNC(__func__,
487 IsDepthwiseConvolutionSupported,
488 data.m_Backends,
489 isSupported,
490 inputInfo,
491 outputInfo,
492 desc,
493 weights.GetInfo(),
494 biases);
Aron Virginas-Tar9fd37392019-07-15 18:04:32 +0100495
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100496 if (!isSupported)
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100497 {
498 return false;
499 }
500
501 armnn::IConnectableLayer* startLayer =
502 data.m_Network->AddDepthwiseConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias));
Aron Virginas-Tar9fd37392019-07-15 18:04:32 +0100503
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100504 if (!startLayer)
505 {
506 return Fail("%s: AddDepthwiseConvolution2dLayer failed", __func__);
507 }
508
509 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data);
510 if (!endLayer)
511 {
512 return Fail("%s: ProcessActivation failed", __func__);
513 }
514
515 input.Connect(startLayer->GetInputSlot(0));
516
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100517 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *endLayer, model, data);
Aron Virginas-Tar24e699d2019-06-17 14:47:46 +0100518}
519
Sadik Armagan15d63e22019-07-26 16:59:35 +0100520bool HalPolicy::ConvertL2Pool2d(const Operation& operation, const Model& model, ConversionData& data)
521{
522 ALOGV("hal_1_2::HalPolicy::ConvertL2Pool2d()");
523 return ConvertPooling2d<hal_1_2::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::L2, model, data);
524}
525
526bool HalPolicy::ConvertMaxPool2d(const Operation& operation, const Model& model, ConversionData& data)
527{
528 ALOGV("hal_1_2::HalPolicy::ConvertMaxPool2d()");
529 return ConvertPooling2d<hal_1_2::HalPolicy>(operation, __func__, armnn::PoolingAlgorithm::Max, model, data);
530}
531
Narumol Prangnawarat95b1ef62019-07-15 12:02:20 +0100532bool HalPolicy::ConvertMaximum(const Operation& operation, const Model& model, ConversionData& data)
533{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100534 ALOGV("hal_1_2::HalPolicy::ConvertMaximum()");
535
Narumol Prangnawarat95b1ef62019-07-15 12:02:20 +0100536 LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
537 LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data);
538
539 if (!input0.IsValid() || !input1.IsValid())
540 {
541 return Fail("%s: Operation has invalid inputs", __func__);
542 }
543
544 const Operand* outputOperand = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
545 if (!outputOperand)
546 {
547 return Fail("%s: Could not read output", __func__);
548 }
549
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100550 const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand);
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100551 if (IsDynamicTensor(outInfo))
Narumol Prangnawarat95b1ef62019-07-15 12:02:20 +0100552 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100553 return Fail("%s: Dynamic output tensors are not supported", __func__);
Narumol Prangnawarat95b1ef62019-07-15 12:02:20 +0100554 }
555
Aron Virginas-Tard7593232019-07-16 13:17:06 +0100556 bool isSupported = false;
557 FORWARD_LAYER_SUPPORT_FUNC(__func__,
558 IsMaximumSupported,
559 data.m_Backends,
560 isSupported,
561 input0.GetTensorInfo(),
562 input1.GetTensorInfo(),
563 outInfo);
564
565 if (!isSupported)
Narumol Prangnawarat95b1ef62019-07-15 12:02:20 +0100566 {
567 return false;
568 }
569
570 armnn::IConnectableLayer* layer = data.m_Network->AddMaximumLayer();
571 assert(layer != nullptr);
572 BroadcastTensor(input0, input1, layer, *data.m_Network);
573
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100574 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
Narumol Prangnawarat95b1ef62019-07-15 12:02:20 +0100575}
576
Ellen Norris-Thompson1cb29aa2019-07-11 17:27:37 +0100577bool HalPolicy::ConvertMinimum(const Operation& operation, const Model& model, ConversionData& data)
578{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100579 ALOGV("hal_1_2::HalPolicy::ConvertMinimum()");
580
Ellen Norris-Thompson1cb29aa2019-07-11 17:27:37 +0100581 LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
582 LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data);
583
584 if (!input0.IsValid() || !input1.IsValid())
585 {
586 return Fail("%s: Operation has invalid inputs", __func__);
587 }
588
589 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
590 if (!output)
591 {
592 return Fail("%s: Could not read output 0", __func__);
593 }
594
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100595 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100596 if (IsDynamicTensor(outputInfo))
Ellen Norris-Thompson1cb29aa2019-07-11 17:27:37 +0100597 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100598 return Fail("%s: Dynamic output tensors are not supported", __func__);
Ellen Norris-Thompson1cb29aa2019-07-11 17:27:37 +0100599 }
600
601 bool isSupported = false;
602 FORWARD_LAYER_SUPPORT_FUNC(__func__,
603 IsMinimumSupported,
604 data.m_Backends,
605 isSupported,
606 input0.GetTensorInfo(),
607 input1.GetTensorInfo(),
608 outputInfo);
609
610 if (!isSupported)
611 {
612 return false;
613 }
614
615 armnn::IConnectableLayer* const layer = data.m_Network->AddMinimumLayer();
616 assert(layer != nullptr);
617 BroadcastTensor(input0, input1, layer, *data.m_Network);
618
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100619 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
Ellen Norris-Thompson1cb29aa2019-07-11 17:27:37 +0100620}
621
Aron Virginas-Tarc921f6b2019-07-25 10:14:33 +0100622bool HalPolicy::ConvertPad(const Operation& operation, const Model& model, ConversionData& data)
623{
624 ALOGV("hal_1_2::HalPolicy::ConvertPad()");
625 return ::ConvertPad<hal_1_2::HalPolicy>(operation, model, data);
626}
627
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100628bool HalPolicy::ConvertPadV2(const Operation& operation, const Model& model, ConversionData& data)
629{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100630 ALOGV("hal_1_2::HalPolicy::ConvertPadV2()");
631
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100632 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
633 if (!input.IsValid())
634 {
635 return Fail("%s: Could not read input 0", __func__);
636 }
637
Aron Virginas-Tar366e0a62019-07-10 13:01:41 +0100638 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
639 if (!output)
640 {
641 return Fail("%s: Could not read output", __func__);
642 }
643
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100644 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
645 unsigned int rank = inputInfo.GetNumDimensions();
646
647 armnn::PadDescriptor descriptor;
648 if (!ConvertPaddings<hal_1_2::HalPolicy>(operation, model, data, rank, descriptor))
649 {
650 return Fail("%s: Could not convert paddings", __func__);
651 }
652
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100653 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100654 if (IsDynamicTensor(outputInfo))
Sadik Armagan310d8ff2019-07-11 10:53:38 +0100655 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100656 return Fail("%s: Dynamic output tensors are not supported", __func__);
Sadik Armagan310d8ff2019-07-11 10:53:38 +0100657 }
658
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100659 // Determine type of padding value
660 OperandType operandType0;
661 OperandType operandType2;
662
663 if (!GetOperandType<hal_1_2::HalPolicy>(operation, 0, model, operandType0) ||
664 !GetOperandType<hal_1_2::HalPolicy>(operation, 2, model, operandType2))
665 {
666 return Fail("%s: Operation has invalid inputs", __func__);
667 }
668
669 // Read value to use for padding
670 if (operandType0 == OperandType::TENSOR_FLOAT16 && operandType2 == OperandType::FLOAT16)
671 {
672 armnn::Half f16PadValue;
673 if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 2, operandType2, f16PadValue, model, data))
674 {
675 return Fail("%s: Could not read input 2 (FLOAT16)", __func__);
676 }
677
678 descriptor.m_PadValue = f16PadValue;
679 }
680 else if (operandType0 == OperandType::TENSOR_FLOAT32 && operandType2 == OperandType::FLOAT32)
681 {
682 if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 2, descriptor.m_PadValue, model, data))
683 {
684 return Fail("%s: Could not read input 2 (FLOAT32)", __func__);
685 }
686 }
687 else if (operandType0 == OperandType::TENSOR_QUANT8_ASYMM && operandType2 == OperandType::INT32)
688 {
Mike Kelly3c673942019-07-25 09:26:06 +0100689 int32_t intPadValue = 0;
690 if (!GetInputInt32<hal_1_2::HalPolicy>(operation, 2, intPadValue, model, data))
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100691 {
692 return Fail("%s: Could not read input 2 (INT32)", __func__);
693 }
Mike Kelly3c673942019-07-25 09:26:06 +0100694 descriptor.m_PadValue = intPadValue;
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100695 }
696 else
697 {
698 return Fail("%s: Operation has invalid inputs: type mismatch", __func__);
699 }
700
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100701 bool isSupported = false;
702 FORWARD_LAYER_SUPPORT_FUNC(__func__,
703 IsPadSupported,
704 data.m_Backends,
705 isSupported,
706 inputInfo,
707 outputInfo,
708 descriptor);
709 if (!isSupported)
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100710 {
711 return false;
712 }
713
714 armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor);
715 assert(layer != nullptr);
716 input.Connect(layer->GetInputSlot(0));
717 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
718
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100719 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +0100720}
721
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100722bool HalPolicy::ConvertPrelu(const Operation& operation, const Model& model, ConversionData& data)
723{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100724 ALOGV("hal_1_2::HalPolicy::ConvertPrelu()");
725
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100726 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
727 LayerInputHandle alpha = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 1, model, data);
728
729 if (!input.IsValid() || !alpha.IsValid())
730 {
731 return Fail("%s: Operation has invalid inputs", __func__);
732 }
733
734 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
735
736 if (!output)
737 {
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100738 return Fail("%s: Could not read output", __func__);
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100739 }
740
741 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
742 const armnn::TensorInfo& alphaInfo = alpha.GetTensorInfo();
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100743 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Aron Virginas-Tarf03fcf02019-07-09 17:44:24 +0100744
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100745 if (IsDynamicTensor(outputInfo))
Aron Virginas-Tarf03fcf02019-07-09 17:44:24 +0100746 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100747 return Fail("%s: Dynamic output tensors are not supported", __func__);
Aron Virginas-Tarf03fcf02019-07-09 17:44:24 +0100748 }
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100749
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100750 bool isSupported = false;
751 FORWARD_LAYER_SUPPORT_FUNC(__func__,
752 IsPreluSupported,
753 data.m_Backends,
754 isSupported,
755 inputInfo,
756 alphaInfo,
757 outputInfo);
758 if (!isSupported)
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100759 {
760 return false;
761 }
762
763 armnn::IConnectableLayer* const layer = data.m_Network->AddPreluLayer();
764
765 if (!layer)
766 {
767 return Fail("%s: AddPreluLayer failed", __func__);
768 }
769
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100770 BroadcastTensor(input, alpha, layer, *data.m_Network);
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100771
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100772 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
Matteo Martincigh17ffff32019-06-27 14:12:55 +0100773}
774
Sadik Armagan5a476a82019-07-30 09:43:18 +0100775bool HalPolicy::ConvertQuantize(const Operation& operation, const Model& model, ConversionData& data)
776{
777 ALOGV("hal_1_2::HalPolicy::ConvertQuantize()");
778
779 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
780 if (!input.IsValid())
781 {
782 return Fail("%s: Operation has invalid input", __func__);
783 }
784
785 const Operand* const outputOperand = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
786 if (!outputOperand)
787 {
788 return Fail("%s: Operation has invalid outputs", __func__);
789 }
790
791 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
792 if (IsDynamicTensor(outputInfo))
793 {
794 return Fail("%s: Dynamic output tensors are not supported", __func__);
795 }
796
797 bool isSupported = false;
798 FORWARD_LAYER_SUPPORT_FUNC(__func__,
799 IsQuantizeSupported,
800 data.m_Backends,
801 isSupported,
802 input.GetTensorInfo(),
803 outputInfo);
804 if (!isSupported)
805 {
806 return false;
807 }
808
809 armnn::IConnectableLayer* const layer = data.m_Network->AddQuantizeLayer();
810 assert(layer != nullptr);
811 input.Connect(layer->GetInputSlot(0));
812
813 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
814}
815
Ellen Norris-Thompson7efb46d2019-07-24 17:39:19 +0100816bool HalPolicy::ConvertQuantizedLstm(const Operation& operation, const Model& model, ConversionData& data)
817{
818 ALOGV("hal_1_2::HalPolicy::ConvertQuantizedLstm()");
819
820 //Inputs:
821 // 0: The input: A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape [numBatches, inputSize]
822 // specifying the input to the LSTM cell. Tensor is quantized with a fixed quantization range of -1, 127/128.
823 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
824 if (!input.IsValid())
825 {
826 return Fail("%s: Could not read input 0: input", __func__);
827 }
828
829 //13: The previous cell state: A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT16_SYMM and shape
830 // [numBatches, outputSize] specifying the cell state from the previous time step of the LSTM cell.
831 // It is quantized using a quantization range of -2^4, 2^4 * 32767/32768.
832 LayerInputHandle previousCellStateIn = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 13, model, data);
833 if (!previousCellStateIn.IsValid())
834 {
835 return Fail("%s: Could not read input 13: previousCellStateIn", __func__);
836 }
837
838 // 14: The previous output state: A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
839 // [numBathes, outputSize] specifying the output of the LSTM cell from previous time-step. Tensor
840 // is quantized with a fixed quantization range of -1, 127/128.
841 LayerInputHandle previousOutputIn = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 14, model, data);
842 if (!previousOutputIn.IsValid())
843 {
844 return Fail("%s: Could not read input 14: previousOutputIn", __func__);
845 }
846
847 // Get the input tensors:
848 // 1: The input-to-input weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
849 // [outputSize, inputSize] specifying input-to-input part of weights for fully-connected layer inside the
850 // LSTM cell. Quantization zero point and scale must be the same across all the weights.
851 const ConstTensorPin inputToInputWeightsPin =
852 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 0, model, data);
853
854 // 2: The input-to-forget weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
855 // [outputSize, inputSize] specifying input-to-forget part of weights for fully-connected layer inside the
856 // LSTM cell. Quantization zero point and scale must be the same across all the weights.
857 const ConstTensorPin inputToForgetWeightsPin =
858 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data);
859
860 // 3: The input-to-cell weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
861 // [outputSize, inputSize] specifying input-to-cell part of weights for fully-connected layer inside the
862 // LSTM cell. Quantization zero point and scale must be the same across all the weights.
863 const ConstTensorPin inputToCellWeightsPin =
864 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data);
865
866 // 4: The input-to-output weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
867 // [outputSize, inputSize] specifying input-to-output part of weights for fully-connected layer inside the
868 // LSTM cell. Quantization zero point and scale must be the same across all the weights.
869 const ConstTensorPin inputToOutputWeightsPin =
870 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 3, model, data);
871
872 // 5: The recurrent-to-input weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
873 // [outputSize, outputSize] specifying recurrent-to-input part of weights for fully-connected layer inside
874 // the LSTM cell. Quantization zero point and scale must be the same across all the weights.
875 const ConstTensorPin recurrentToInputWeightsPin =
876 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 4, model, data);
877
878 // 6: The recurrent-to-forget weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
879 // [outputSize, outputSize] specifying recurrent-to-forget part of weights for fully-connected layer inside
880 // the LSTM cell. Quantization zero point and scale must be the same across all the weights.
881 const ConstTensorPin recurrentToForgetWeightsPin =
882 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 5, model, data);
883
884 // 7: The recurrent-to-cell weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
885 // [outputSize, outputSize] specifying recurrent-to-cell part of weights for fully-connected layer inside
886 // the LSTM cell. Quantization zero point and scale must be the same across all the weights.
887 const ConstTensorPin recurrentToCellWeightsPin =
888 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 6, model, data);
889
890 // 8: The recurrent-to-output weights. A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape
891 // [outputSize, outputSize] specifying recurrent-to-output part of weights for fully-connected layer inside
892 // the LSTM cell. Quantization zero point and scale must be the same across all the weights.
893 const ConstTensorPin recurrentToOutputWeightsPin =
894 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 7, model, data);
895
896 // 9: The input gate bias. A 1-D tensor of type ANEURALNETWORKS_TENSOR_INT32 and shape [outputSize] specifying the
897 // bias for the fully-connected layer inside the LSTM cell. Bias is quantized with scale being a product
898 // of input and weights scales and zeroPoint equal to 0.
899 const ConstTensorPin inputGateBiasPin =
900 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 8, model, data);
901
902 // 10: The forget gate bias. A 1-D tensor of type ANEURALNETWORKS_TENSOR_INT32 and shape [outputSize] specifying
903 // the bias for the fully-connected layer inside the LSTM cell. Bias is quantized with scale being a product
904 // of input and weights scales and zeroPoint equal to 0.
905 const ConstTensorPin forgetGateBiasPin =
906 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 9, model, data);
907
908 // 11:The cell bias. A 1-D tensor of type ANEURALNETWORKS_TENSOR_INT32 and shape [outputSize] specifying the bias
909 // for the fully-connected layer inside the LSTM cell. Bias is quantized with scale being a product of input
910 // and weights scales and zeroPoint equal to 0.
911 const ConstTensorPin cellBiasPin =
912 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 10, model, data);
913
914 // 12:The output gate bias. A 1-D tensor of type ANEURALNETWORKS_TENSOR_INT32 and shape [outputSize] specifying
915 // the bias for the fully-connected layer inside the LSTM cell. Bias is quantized with scale being a product
916 // of input and weights scales and zeroPoint equal to 0.
917 const ConstTensorPin outputGateBiasPin =
918 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 11, model, data);
919
920 if (!inputToInputWeightsPin.IsValid() ||
921 !inputToForgetWeightsPin.IsValid() ||
922 !inputToCellWeightsPin.IsValid() ||
923 !inputToOutputWeightsPin.IsValid() ||
924 !recurrentToInputWeightsPin.IsValid() ||
925 !recurrentToForgetWeightsPin.IsValid() ||
926 !recurrentToCellWeightsPin.IsValid() ||
927 !recurrentToOutputWeightsPin.IsValid() ||
928 !inputGateBiasPin.IsValid() ||
929 !forgetGateBiasPin.IsValid() ||
930 !cellBiasPin.IsValid() ||
931 !outputGateBiasPin.IsValid())
932 {
933 return Fail("%s: Operation has invalid tensor inputs", __func__);
934 }
935
936 // Outputs:
937 // 0: The cell state: A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT16_SYMM and shape [numBatches, outputSize]
938 // which contains a cell state from the current time step. Tensor is quantized using a quantization range
939 // of -2^4, 2^4 * 32767/32768.
940 const Operand* cellStateOut = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
941 if (!cellStateOut)
942 {
943 return Fail("%s: Could not read output 0: cellStateOut", __func__);
944 }
945
946 // 1: The output: A 2-D tensor of type ANEURALNETWORKS_TENSOR_QUANT8_ASYMM and shape [numBathes, outputSize] which
947 // contains the output value. Tensor is quantized with a fixed quantization range of -1, 127/128.
948 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 1, model);
949 if (!output)
950 {
951 return Fail("%s: Could not read output 1: output", __func__);
952 }
953
954 // Inputs
955 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
956 const armnn::TensorInfo& previousCellStateInInfo = previousCellStateIn.GetTensorInfo();
957 const armnn::TensorInfo& previousOutputInInfo = previousOutputIn.GetTensorInfo();
958
959 // Outputs
960 const armnn::TensorInfo& cellStateOutInfo = GetTensorInfoForOperand(*cellStateOut);
961 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
962
963 // Dynamic tensors currently not supported
964 if (IsDynamicTensor(cellStateOutInfo) || IsDynamicTensor(outputInfo))
965 {
966 return Fail("%s: Dynamic output tensors are not supported", __func__);
967 }
968
969 armnn::QuantizedLstmInputParams params;
970
971 params.m_InputToInputWeights = inputToInputWeightsPin.GetConstTensorPtr();
972 params.m_InputToForgetWeights = inputToForgetWeightsPin.GetConstTensorPtr();
973 params.m_InputToCellWeights = inputToCellWeightsPin.GetConstTensorPtr();
974 params.m_InputToOutputWeights = inputToOutputWeightsPin.GetConstTensorPtr();
975 params.m_RecurrentToInputWeights = recurrentToInputWeightsPin.GetConstTensorPtr();
976 params.m_RecurrentToForgetWeights = recurrentToForgetWeightsPin.GetConstTensorPtr();
977 params.m_RecurrentToCellWeights = recurrentToCellWeightsPin.GetConstTensorPtr();
978 params.m_RecurrentToOutputWeights = recurrentToOutputWeightsPin.GetConstTensorPtr();
979 params.m_InputGateBias = inputGateBiasPin.GetConstTensorPtr();
980 params.m_ForgetGateBias = forgetGateBiasPin.GetConstTensorPtr();
981 params.m_CellBias = cellBiasPin.GetConstTensorPtr();
982 params.m_OutputGateBias = outputGateBiasPin.GetConstTensorPtr();
983
984 armnn::QuantizedLstmInputParamsInfo paramsInfo;
985 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
986 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
987 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
988 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
989 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
990 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
991 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
992 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
993 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
994 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
995 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
996 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
997
998 bool isSupported = false;
999 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1000 IsQuantizedLstmSupported,
1001 data.m_Backends,
1002 isSupported,
1003 inputInfo,
1004 previousCellStateInInfo,
1005 previousOutputInInfo,
1006 cellStateOutInfo,
1007 outputInfo,
1008 paramsInfo);
1009
1010 if (!isSupported)
1011 {
1012 return false;
1013 }
1014
1015 armnn::IConnectableLayer* const layer = data.m_Network->AddQuantizedLstmLayer(params, "QuantizedLstm");
1016 input.Connect(layer->GetInputSlot(0));
1017 previousOutputIn.Connect(layer->GetInputSlot(1));
1018 previousCellStateIn.Connect(layer->GetInputSlot(2));
1019
1020 return (SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, 0, model, data) &&
1021 SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 1, *layer, 1, model, data));
1022}
1023
Sadik Armagan61113162019-07-25 09:09:40 +01001024bool HalPolicy::ConvertReLu(const Operation& operation, const Model& model, ConversionData& data)
1025{
1026 ALOGV("hal_1_2::HalPolicy::ConvertReLu()");
1027 return ::ConvertReLu<hal_1_2::HalPolicy>(operation, model, data);
1028}
1029
1030bool HalPolicy::ConvertReLu1(const Operation& operation, const Model& model, ConversionData& data)
1031{
1032 ALOGV("hal_1_2::HalPolicy::ConvertReLu1()");
1033 return ::ConvertReLu1<hal_1_2::HalPolicy>(operation, model, data);
1034}
1035
1036bool HalPolicy::ConvertReLu6(const Operation& operation, const Model& model, ConversionData& data)
1037{
1038 ALOGV("hal_1_2::HalPolicy::ConvertReLu6()");
1039 return ::ConvertReLu6<hal_1_2::HalPolicy>(operation, model, data);
1040}
1041
Aron Virginas-Tarfb2fa292019-07-04 11:59:48 +01001042bool HalPolicy::ConvertResize(const Operation& operation,
1043 const Model& model,
1044 ConversionData& data,
1045 armnn::ResizeMethod resizeMethod)
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +01001046{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +01001047 ALOGV("hal_1_2::HalPolicy::ConvertResize()");
1048
1049 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +01001050 if (!input.IsValid())
1051 {
1052 return Fail("%s: Could not read input 0", __func__);
1053 }
1054
1055 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
1056 if (!output)
1057 {
1058 return Fail("%s: Could not read output 0", __func__);
1059 }
1060
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001061 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
1062 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
1063
1064 if (IsDynamicTensor(outputInfo))
1065 {
1066 return Fail("%s: Dynamic output tensors are not supported", __func__);
1067 }
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +01001068
1069 armnn::ResizeDescriptor descriptor;
Aron Virginas-Tarfb2fa292019-07-04 11:59:48 +01001070 descriptor.m_Method = resizeMethod;
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +01001071 descriptor.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 3, model, data);
1072
1073 OperandType operandType1;
1074 OperandType operandType2;
1075
1076 if (!GetOperandType<hal_1_2::HalPolicy>(operation, 1, model, operandType1) ||
1077 !GetOperandType<hal_1_2::HalPolicy>(operation, 2, model, operandType2))
1078 {
1079 return Fail("%s: Operation has invalid inputs", __func__);
1080 }
1081
1082 if (operandType1 != operandType2)
1083 {
1084 return Fail("%s: Operation has invalid inputs. Type of input 1 and 2 should be the same", __func__);
1085 }
1086
1087 if (operandType1 == OperandType::INT32)
1088 {
1089 // Case 1: resizing by shape
1090 int32_t targetWidth = 0;
1091 int32_t targetHeight = 0;
1092
1093 if (!GetInputInt32<hal_1_2::HalPolicy>(operation, 1, targetWidth, model, data) ||
1094 !GetInputInt32<hal_1_2::HalPolicy>(operation, 2, targetHeight, model, data))
1095 {
1096 return Fail("%s: Operation has invalid inputs for resizing by shape", __func__);
1097 }
1098
1099 if (targetWidth < 0 || targetHeight < 0)
1100 {
1101 return Fail("%s: Operation has invalid inputs for resizing by shape. "
1102 "Target width/height cannot be < 0", __func__);
1103 }
1104
1105 descriptor.m_TargetWidth = static_cast<uint32_t>(targetWidth);
Teresa Charlin9843c012019-07-19 12:18:35 +01001106 descriptor.m_TargetHeight = static_cast<uint32_t>(targetHeight);
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +01001107 }
1108 else if (operandType1 == OperandType::FLOAT32)
1109 {
1110 // Case 2: resizing by scale
1111 float widthScale = 1.0f;
1112 float heightScale = 1.0f;
1113
1114 if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 1, widthScale, model, data) ||
1115 !GetInputFloat32<hal_1_2::HalPolicy>(operation, 2, heightScale, model, data))
1116 {
1117 return Fail("%s: Operation has invalid inputs for resizing by scale", __func__);
1118 }
1119
1120 const armnn::TensorShape& inputShape = inputInfo.GetShape();
1121 armnnUtils::DataLayoutIndexed dataLayoutIndexed(descriptor.m_DataLayout);
1122
1123 float width = inputShape[dataLayoutIndexed.GetWidthIndex()];
1124 float height = inputShape[dataLayoutIndexed.GetHeightIndex()];
1125
1126 descriptor.m_TargetWidth = std::floor(width * widthScale);
1127 descriptor.m_TargetHeight = std::floor(height * heightScale);
1128 }
1129 else
1130 {
1131 // NOTE: FLOAT16 scales are not supported
1132 return false;
1133 }
1134
Ferran Balaguerd30093c2019-07-09 17:04:47 +01001135 bool isSupported = false;
1136 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1137 IsResizeSupported,
1138 data.m_Backends,
1139 isSupported,
1140 inputInfo,
1141 outputInfo,
1142 descriptor);
Aron Virginas-Tarbe5d3562019-07-16 11:32:29 +01001143
Ferran Balaguerd30093c2019-07-09 17:04:47 +01001144 if (!isSupported)
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +01001145 {
1146 return false;
1147 }
1148
1149 armnn::IConnectableLayer* layer = data.m_Network->AddResizeLayer(descriptor);
1150
1151 assert(layer != nullptr);
1152
1153 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1154 input.Connect(layer->GetInputSlot(0));
1155
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001156 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +01001157}
1158
Finn Williamsd74c5052019-07-30 17:06:00 +01001159bool HalPolicy::ConvertSpaceToBatchNd(const Operation& operation, const Model& model, ConversionData& data)
1160{
1161 ALOGV("hal_1_2::HalPolicy::ConvertSpaceToBatchNd()");
1162 return ::ConvertSpaceToBatchNd<hal_1_2::HalPolicy>(operation, model, data);
1163}
1164
Keith Davisa6bc52f2019-06-26 09:39:49 +01001165bool HalPolicy::ConvertSpaceToDepth(const Operation& operation, const Model& model, ConversionData& data)
1166{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +01001167 ALOGV("hal_1_2::HalPolicy::ConvertSpaceToDepth()");
Keith Davisa6bc52f2019-06-26 09:39:49 +01001168
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +01001169 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
Keith Davisa6bc52f2019-06-26 09:39:49 +01001170 if (!input.IsValid() )
1171 {
1172 return Fail("%s: Operation has invalid inputs", __func__);
1173 }
1174
1175 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
1176 unsigned int rank = inputInfo.GetNumDimensions();
Keith Davisa6bc52f2019-06-26 09:39:49 +01001177 if (rank != 4)
1178 {
1179 return Fail("%s: Only inputs with rank 4 are supported", __func__);
1180 }
1181
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001182 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
1183 if (!output)
1184 {
1185 return Fail("%s: Could not read output 0", __func__);
1186 }
1187
1188 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
1189 if (IsDynamicTensor(outputInfo))
1190 {
1191 return Fail("%s: Dynamic output tensors are not supported", __func__);
1192 }
1193
Keith Davisa6bc52f2019-06-26 09:39:49 +01001194 armnn::SpaceToDepthDescriptor desc;
1195
1196 GetInputScalar<hal_1_2::HalPolicy>(operation, 1, OperandType::INT32, desc.m_BlockSize, model, data);
1197
1198 if (desc.m_BlockSize <= 1)
1199 {
1200 return Fail("%s: Block size must be at least 1 in all dimensions");
1201 }
1202
1203 desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 2, model, data);
1204
Ferran Balaguerd30093c2019-07-09 17:04:47 +01001205 bool isSupported = false;
1206 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1207 IsSpaceToDepthSupported,
1208 data.m_Backends,
1209 isSupported,
1210 inputInfo,
1211 outputInfo,
1212 desc);
1213 if (!isSupported)
Keith Davisa6bc52f2019-06-26 09:39:49 +01001214 {
1215 return false;
1216 }
1217
1218 armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToDepthLayer(desc);
1219 assert(layer != nullptr);
1220 input.Connect(layer->GetInputSlot(0));
1221
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001222 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
Keith Davisa6bc52f2019-06-26 09:39:49 +01001223}
1224
Francis Murtagh074c25a2019-07-22 16:40:57 +01001225bool HalPolicy::ConvertSoftmax(const Operation& operation, const Model& model, ConversionData& data)
1226{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +01001227 ALOGV("hal_1_2::HalPolicy::ConvertSoftmax()");
1228
Francis Murtagh074c25a2019-07-22 16:40:57 +01001229 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
1230 if (!input.IsValid())
1231 {
1232 return Fail("%s: Operation has invalid inputs", __func__);
1233 }
1234
1235 const Operand* outputOperand = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
1236 if (!outputOperand)
1237 {
1238 return Fail("%s: Operation has no outputs", __func__);
1239 }
1240
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001241 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +01001242 if (IsDynamicTensor(outputInfo))
Francis Murtagh074c25a2019-07-22 16:40:57 +01001243 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001244 return Fail("%s: Dynamic output tensors are not supported", __func__);
Francis Murtagh074c25a2019-07-22 16:40:57 +01001245 }
1246
1247 armnn::SoftmaxDescriptor desc;
1248 if (!GetInputFloat32<hal_1_2::HalPolicy>(operation, 1, desc.m_Beta, model, data))
1249 {
1250 return Fail("%s: Operation has invalid inputs", __func__);
1251 }
1252
1253 if (operation.inputs.size() > 2 && !GetInputScalar<hal_1_2::HalPolicy>(operation,
1254 2,
1255 HalPolicy::OperandType::INT32,
1256 desc.m_Axis,
1257 model,
1258 data))
1259 {
1260 return Fail("%s: Operation has invalid inputs", __func__);
1261 }
1262
1263 bool isSupported = false;
1264 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1265 IsSoftmaxSupported,
1266 data.m_Backends,
1267 isSupported,
1268 input.GetTensorInfo(),
1269 outputInfo,
1270 desc);
1271 if (!isSupported)
1272 {
1273 return false;
1274 }
1275
1276 armnn::IConnectableLayer* layer = data.m_Network->AddSoftmaxLayer(desc);
1277 assert(layer != nullptr);
1278 input.Connect(layer->GetInputSlot(0));
1279
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001280 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, model, data);
Francis Murtagh074c25a2019-07-22 16:40:57 +01001281}
1282
Mike Kelly0a879362019-07-29 16:56:31 +01001283bool HalPolicy::ConvertSub(const Operation& operation, const Model& model, ConversionData& data)
1284{
1285 ALOGV("hal_1_2::HalPolicy::ConvertSub()");
1286 return ::ConvertSub<hal_1_2::HalPolicy>(operation, model, data);
1287}
1288
Sadik Armagan61113162019-07-25 09:09:40 +01001289bool HalPolicy::ConvertTanH(const Operation& operation, const Model& model, ConversionData& data)
1290{
1291 ALOGV("hal_1_2::HalPolicy::ConvertTanH()");
1292 return ::ConvertTanH<hal_1_2::HalPolicy>(operation, model, data);
1293}
1294
Ferran Balaguerb2397fd2019-07-25 12:12:39 +01001295bool HalPolicy::ConvertLstm(const Operation& operation, const Model& model, ConversionData& data)
1296{
1297 // Inputs:
1298 // 00: The input: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, input_size], where
1299 // “batch_size” corresponds to the batching dimension, and “input_size” is the size of the input.
1300 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
1301 if (!input.IsValid())
1302 {
1303 return Fail("%s: Could not read input 0: input", __func__);
1304 }
1305 // 18: The output state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size].
1306 LayerInputHandle outputStateIn = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 18, model, data);
1307 if (!outputStateIn.IsValid())
1308 {
1309 return Fail("%s: Could not read input 18: outputStateIn", __func__);
1310 }
1311 // 19: The cell state: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units].
1312 LayerInputHandle cellStateIn = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 19, model, data);
1313 if (!cellStateIn.IsValid())
1314 {
1315 return Fail("%s: Could not read input 19: cellStateIn", __func__);
1316 }
1317
1318 // Get the mandatory input tensors:
1319 // 02: The input-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1320 // [num_units, input_size].
1321 const ConstTensorPin inputToForgetWeightsPin =
1322 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data);
1323 // 03: The input-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1324 // [num_units, input_size].
1325 const ConstTensorPin inputToCellWeightsPin =
1326 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 3, model, data);
1327 // 04: The input-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1328 // [num_units, input_size].
1329 const ConstTensorPin inputToOutputWeightsPin =
1330 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 4, model, data);
1331 // 06: The recurrent-to-forget weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1332 // [num_units, output_size].
1333 const ConstTensorPin recurrentToForgetWeightsPin =
1334 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 6, model, data);
1335 // 07: The recurrent-to-cell weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1336 // [num_units, output_size].
1337 const ConstTensorPin recurrentToCellWeightsPin =
1338 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 7, model, data);
1339 // 08: The recurrent-to-output weights: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1340 // [num_units, output_size].
1341 const ConstTensorPin recurrentToOutputWeightsPin =
1342 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 8, model, data);
1343 // 13: The forget gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units].
1344 const ConstTensorPin forgetGateBiasPin =
1345 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 13, model, data);
1346 // 14: The cell bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units].
1347 const ConstTensorPin cellBiasPin =
1348 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 14, model, data);
1349 // 15: The output gate bias: A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units].
1350 const ConstTensorPin outputGateBiasPin =
1351 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 15, model, data);
1352
1353 if (!inputToForgetWeightsPin.IsValid() ||
1354 !inputToCellWeightsPin.IsValid() ||
1355 !inputToOutputWeightsPin.IsValid() ||
1356 !recurrentToForgetWeightsPin.IsValid() ||
1357 !recurrentToCellWeightsPin.IsValid() ||
1358 !recurrentToOutputWeightsPin.IsValid() ||
1359 !forgetGateBiasPin.IsValid() ||
1360 !cellBiasPin.IsValid() ||
1361 !outputGateBiasPin.IsValid())
1362 {
1363 return Fail("%s: Operation has invalid tensor inputs", __func__);
1364 }
1365
1366 // Get the optional input tensors:
1367 // 01: The input-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1368 // [num_units, input_size], where “num_units” corresponds to the number of cell units.
1369 const ConstTensorPin inputToInputWeightsPin =
1370 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1371 1,
1372 model,
1373 data,
1374 g_DontPermute,
1375 nullptr,
1376 true);
1377
1378 // 05: The recurrent-to-input weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1379 // [num_units, output_size], where “output_size” corresponds to either the number of cell units (i.e.,
1380 // “num_units”), or the second dimension of the “projection_weights”, if defined.
1381 const ConstTensorPin recurrentToInputWeightsPin =
1382 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1383 5,
1384 model,
1385 data,
1386 g_DontPermute,
1387 nullptr,
1388 true);
1389
1390 // 09: The cell-to-input weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units].
1391 const ConstTensorPin cellToInputWeightsPin =
1392 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1393 9,
1394 model,
1395 data,
1396 g_DontPermute,
1397 nullptr,
1398 true);
1399
1400 // 10: The cell-to-forget weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units].
1401 const ConstTensorPin cellToForgetWeightsPin =
1402 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1403 10,
1404 model,
1405 data,
1406 g_DontPermute,
1407 nullptr,
1408 true);
1409
1410 // 11: The cell-to-output weights: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units].
1411 const ConstTensorPin cellToOutputWeightsPin =
1412 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1413 11,
1414 model,
1415 data,
1416 g_DontPermute,
1417 nullptr,
1418 true);
1419
1420 // 12: The input gate bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [num_units].
1421 const ConstTensorPin inputGateBiasPin =
1422 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1423 12,
1424 model,
1425 data,
1426 g_DontPermute,
1427 nullptr,
1428 true);
1429
1430 // 16: The projection weights: Optional. A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape
1431 // [output_size, num_units].
1432 const ConstTensorPin projectionWeightsPin =
1433 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1434 16,
1435 model,
1436 data,
1437 g_DontPermute,
1438 nullptr,
1439 true);
1440
1441 // 17: The projection bias: Optional. A 1-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [output_size].
1442 const ConstTensorPin projectionBiasPin =
1443 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1444 17,
1445 model,
1446 data,
1447 g_DontPermute,
1448 nullptr,
1449 true);
1450
1451 if ((!inputToInputWeightsPin.IsValid() && !inputToInputWeightsPin.IsOptional()) ||
1452 (!recurrentToInputWeightsPin.IsValid() && !recurrentToInputWeightsPin.IsOptional()) ||
1453 (!cellToInputWeightsPin.IsValid() && !cellToInputWeightsPin.IsOptional()) ||
1454 (!cellToForgetWeightsPin.IsValid() && !cellToForgetWeightsPin.IsOptional()) ||
1455 (!cellToOutputWeightsPin.IsValid() && !cellToOutputWeightsPin.IsOptional()) ||
1456 (!inputGateBiasPin.IsValid() && !inputGateBiasPin.IsOptional()) ||
1457 (!projectionWeightsPin.IsValid() && !projectionWeightsPin.IsOptional()) ||
1458 (!projectionBiasPin.IsValid() && !projectionBiasPin.IsOptional()))
1459 {
1460 return Fail("%s: Operation has invalid tensor inputs", __func__);
1461 }
1462
1463 // Get the mandatory input scalars (actually 1-D tensors of size 1):
1464 // 20: The activation function: A value indicating the activation function:
1465 // 0: None; 1: Relu; 3: Relu6; 4: Tanh; 6: Sigmoid.
1466 // 21: The clipping threshold: for the cell state, such that values are bound within [-cell_clip, cell_clip].
1467 // If set to 0.0 then clipping is disabled.
1468 // 22: The clipping threshold: for the output from the projection layer, such that values are bound within
1469 // [-proj_clip, proj_clip]. If set to 0.0 then clipping is disabled.
1470 ActivationFn activation;
1471 float cellClip;
1472 float projClip;
1473 if (!GetInputActivationFunctionFromTensor<hal_1_2::HalPolicy>(operation, 20, activation, model, data) ||
1474 !GetInputScalar<hal_1_2::HalPolicy>(operation, 21, OperandType::FLOAT32, cellClip, model, data) ||
1475 !GetInputScalar<hal_1_2::HalPolicy>(operation, 22, OperandType::FLOAT32, projClip, model, data))
1476 {
1477 return Fail("%s: Operation has invalid scalar inputs", __func__);
1478 }
1479
1480 // Get the normalization tensors
1481 // 23: The input layer normalization weights. A 1-D tensor of shape [num_units].
1482 // Used to rescale normalized inputs to activation at input gate.
1483 const ConstTensorPin inputLayerNormWeightsPin =
1484 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1485 23,
1486 model,
1487 data,
1488 g_DontPermute,
1489 nullptr,
1490 true);
1491
1492 // 24: The forget layer normalization weights. A 1-D tensor of shape [num_units].
1493 // Used to rescale normalized inputs to activation at forget gate.
1494 const ConstTensorPin forgetLayerNormWeightsPin =
1495 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1496 24,
1497 model,
1498 data,
1499 g_DontPermute,
1500 nullptr,
1501 true);
1502
1503 // 25: The cell layer normalization weights. A 1-D tensor of shape [num_units].
1504 // Used to rescale normalized inputs to activation at cell gate.
1505 const ConstTensorPin cellLayerNormWeightsPin =
1506 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1507 25,
1508 model,
1509 data,
1510 g_DontPermute,
1511 nullptr,
1512 true);
1513
1514 // 26: The output layer normalization weights. A 1-D tensor of shape [num_units].
1515 // Used to rescale normalized inputs to activation at output gate.
1516 const ConstTensorPin outputLayerNormWeightsPin =
1517 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation,
1518 26,
1519 model,
1520 data,
1521 g_DontPermute,
1522 nullptr,
1523 true);
1524
1525 // Outputs:
1526 // 00: The scratch buffer: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units * 4]
1527 // with CIFG, or [batch_size, num_units * 3] without CIFG.
1528 const Operand* scratchBuffer = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
1529 if (!scratchBuffer)
1530 {
1531 return Fail("%s: Could not read output 0: scratchBuffer", __func__);
1532 }
1533 // 01: The output state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size].
1534 const Operand* outputStateOut = GetOutputOperand<hal_1_2::HalPolicy>(operation, 1, model);
1535 if (!outputStateOut)
1536 {
1537 return Fail("%s: Could not read output 1: outputStateOut", __func__);
1538 }
1539 // 02: The cell state (out): A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, num_units].
1540 const Operand* cellStateOut = GetOutputOperand<hal_1_2::HalPolicy>(operation, 2, model);
1541 if (!cellStateOut)
1542 {
1543 return Fail("%s: Could not read output 2: cellStateOut", __func__);
1544 }
1545 // 03: The output: A 2-D tensor of ANEURALNETWORKS_TENSOR_FLOAT32, of shape [batch_size, output_size]. This is
1546 // effectively the same as the current “output state (out)” value.
1547 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 3, model);
1548 if (!output)
1549 {
1550 return Fail("%s: Could not read output 3: output", __func__);
1551 }
1552
1553 // set the params structure for the AddLstmLayer call
1554 armnn::LstmInputParams params;
1555 params.m_InputToInputWeights = inputToInputWeightsPin.GetConstTensorPtr();
1556 params.m_InputToForgetWeights = inputToForgetWeightsPin.GetConstTensorPtr();
1557 params.m_InputToCellWeights = inputToCellWeightsPin.GetConstTensorPtr();
1558 params.m_InputToOutputWeights = inputToOutputWeightsPin.GetConstTensorPtr();
1559 params.m_RecurrentToInputWeights = recurrentToInputWeightsPin.GetConstTensorPtr();
1560 params.m_RecurrentToForgetWeights = recurrentToForgetWeightsPin.GetConstTensorPtr();
1561 params.m_RecurrentToCellWeights = recurrentToCellWeightsPin.GetConstTensorPtr();
1562 params.m_RecurrentToOutputWeights = recurrentToOutputWeightsPin.GetConstTensorPtr();
1563 params.m_CellToInputWeights = cellToInputWeightsPin.GetConstTensorPtr();
1564 params.m_CellToForgetWeights = cellToForgetWeightsPin.GetConstTensorPtr();
1565 params.m_CellToOutputWeights = cellToOutputWeightsPin.GetConstTensorPtr();
1566 params.m_InputGateBias = inputGateBiasPin.GetConstTensorPtr();
1567 params.m_ForgetGateBias = forgetGateBiasPin.GetConstTensorPtr();
1568 params.m_CellBias = cellBiasPin.GetConstTensorPtr();
1569 params.m_OutputGateBias = outputGateBiasPin.GetConstTensorPtr();
1570 params.m_ProjectionWeights = projectionWeightsPin.GetConstTensorPtr();
1571 params.m_ProjectionBias = projectionBiasPin.GetConstTensorPtr();
1572 params.m_InputLayerNormWeights = inputLayerNormWeightsPin.GetConstTensorPtr();
1573 params.m_ForgetLayerNormWeights = forgetLayerNormWeightsPin.GetConstTensorPtr();
1574 params.m_CellLayerNormWeights = cellLayerNormWeightsPin.GetConstTensorPtr();
1575 params.m_OutputLayerNormWeights = outputLayerNormWeightsPin.GetConstTensorPtr();
1576
1577 // set the layer descriptor
1578 armnn::LstmDescriptor desc;
1579 desc.m_ActivationFunc = activation;
1580 desc.m_ClippingThresCell = cellClip;
1581 desc.m_ClippingThresProj = projClip;
1582 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr ||
1583 params.m_RecurrentToInputWeights == nullptr ||
1584 params.m_InputGateBias == nullptr);
1585 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr ||
1586 params.m_CellToOutputWeights != nullptr);
1587 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
1588 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr ||
1589 params.m_ForgetLayerNormWeights != nullptr ||
1590 params.m_CellLayerNormWeights != nullptr ||
1591 params.m_OutputLayerNormWeights != nullptr);
1592
1593 // validate the optional input groups
1594 if (desc.m_CifgEnabled &&
1595 (params.m_InputToInputWeights != nullptr ||
1596 params.m_RecurrentToInputWeights != nullptr ||
1597 params.m_InputGateBias != nullptr))
1598 {
1599 return Fail("%s: All, or none, of input-to-input weights, recurrent-to-input weights,"
1600 " and input gate bias must be provided", __func__);
1601 }
1602
1603 if (!desc.m_ProjectionEnabled && params.m_ProjectionBias != nullptr)
1604 {
1605 return Fail("%s: projection bias should not be provided without projection weights", __func__);
1606 }
1607
1608 if (desc.m_PeepholeEnabled &&
1609 (params.m_CellToForgetWeights == nullptr ||
1610 params.m_CellToOutputWeights == nullptr ||
1611 (!desc.m_CifgEnabled && params.m_CellToInputWeights == nullptr)))
1612 {
1613 return Fail("%s: All, or none, of cell-to-forget weights and cell-to-output weights must be provided"
1614 " and, if CIFG is not enabled, cell-to-input weights must also be provided", __func__);
1615 }
1616
1617 if (desc.m_LayerNormEnabled &&
1618 (params.m_ForgetLayerNormWeights == nullptr ||
1619 params.m_CellLayerNormWeights == nullptr ||
1620 params.m_OutputLayerNormWeights == nullptr ||
1621 (!desc.m_CifgEnabled && params.m_InputLayerNormWeights == nullptr)))
1622 {
1623 return Fail("%s: All, or none, of forget-norm weights, cell-norm weights and output-norm weights must be"
1624 " provided and, if CIFG is not enabled, input-norm weights must also be provided", __func__);
1625 }
1626
1627 // Check if the layer is supported
1628 // Inputs
1629 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
1630 const armnn::TensorInfo& outputStateInInfo = outputStateIn.GetTensorInfo();
1631 const armnn::TensorInfo& cellStateInInfo = cellStateIn.GetTensorInfo();
1632
1633 // Outputs
1634 const armnn::TensorInfo& scratchBufferInfo = GetTensorInfoForOperand(*scratchBuffer);
1635 const armnn::TensorInfo& outputStateOutInfo = GetTensorInfoForOperand(*outputStateOut);
1636 const armnn::TensorInfo& cellStateOutInfo = GetTensorInfoForOperand(*cellStateOut);
1637 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
1638
Ferran Balaguera4a629a2019-07-30 10:16:13 +01001639 if (IsDynamicTensor(scratchBufferInfo) ||
1640 IsDynamicTensor(outputStateOutInfo) ||
1641 IsDynamicTensor(cellStateOutInfo) ||
1642 IsDynamicTensor(outputInfo))
1643 {
1644 return Fail("%s: Dynamic output tensors are not supported", __func__);
1645 }
1646
Ferran Balaguerb2397fd2019-07-25 12:12:39 +01001647 // Basic parameters
1648 armnn::LstmInputParamsInfo paramsInfo;
1649 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
1650 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
1651 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
1652 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
1653 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
1654 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
1655 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
1656 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
1657 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
1658
1659 // Optional parameters
1660 if(!desc.m_CifgEnabled)
1661 {
1662 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
1663 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
1664 if (params.m_CellToInputWeights != nullptr)
1665 {
1666 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
1667 }
1668 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
1669 }
1670
1671 if(desc.m_ProjectionEnabled)
1672 {
1673 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
1674 if (params.m_ProjectionBias != nullptr)
1675 {
1676 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
1677 }
1678 }
1679
1680 if(desc.m_PeepholeEnabled)
1681 {
1682 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
1683 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
1684 }
1685
1686 if (desc.m_LayerNormEnabled)
1687 {
1688 if(!desc.m_CifgEnabled)
1689 {
1690 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
1691 }
1692 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
1693 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
1694 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
1695 }
1696
1697 bool isSupported = false;
1698 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1699 IsLstmSupported,
1700 data.m_Backends,
1701 isSupported,
1702 inputInfo,
1703 outputStateInInfo,
1704 cellStateInInfo,
1705 scratchBufferInfo,
1706 outputStateOutInfo,
1707 cellStateOutInfo,
1708 outputInfo,
1709 desc,
1710 paramsInfo);
1711 if (!isSupported)
1712 {
1713 return false;
1714 }
1715
1716 // Add the layer
1717 armnn::IConnectableLayer* layer = data.m_Network->AddLstmLayer(desc, params, "Lstm");
1718
1719 input.Connect(layer->GetInputSlot(0));
1720 outputStateIn.Connect(layer->GetInputSlot(1));
1721 cellStateIn.Connect(layer->GetInputSlot(2));
1722
1723 return (SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *layer, 0, model, data) &&
1724 SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 1, *layer, 1, model, data) &&
1725 SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 2, *layer, 2, model, data) &&
1726 SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 3, *layer, 3, model, data));
1727}
1728
Aron Virginas-Tar8b991682019-07-31 12:54:59 +01001729bool HalPolicy::ConvertTransposeConv2d(const Operation& operation, const Model& model, ConversionData& data)
David Monahan613b49c2019-06-27 11:37:47 +01001730{
1731 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_2::HalPolicy>(operation, 0, model, data);
1732
1733 if (!input.IsValid())
1734 {
1735 return Fail("%s: Operation has invalid inputs", __func__);
1736 }
1737
1738 const Operand* output = GetOutputOperand<hal_1_2::HalPolicy>(operation, 0, model);
1739
1740 if (!output)
1741 {
1742 return Fail("%s: Could not read output 0", __func__);
1743 }
1744
1745 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
1746 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
1747 if (IsDynamicTensor(outputInfo))
1748 {
1749 return Fail("%s: Dynamic output tensors are not supported", __func__);
1750 }
1751
1752 // ArmNN does not currently support non-fixed weights or bias
1753 // Find the shape of the weights tensor. In AndroidNN this will be [ 1, H, W, I * M ]
1754 const Operand* weightsOperand = GetInputOperand<hal_1_2::HalPolicy>(operation, 1, model);
1755
1756 if (weightsOperand == nullptr)
1757 {
1758 return Fail("%s: Operand is invalid", __func__);
1759 }
1760 armnn::TransposeConvolution2dDescriptor desc;
1761 desc.m_DataLayout = armnn::DataLayout::NHWC;
1762
1763 // Determine whether padding is implicit or explicit
1764 bool implicitPadding = operation.inputs.size() == 9;
1765
1766 if (implicitPadding )
1767 {
1768 desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 8, model, data);
1769 }
1770 else
1771 {
1772 desc.m_DataLayout = OptionalDataLayout<hal_1_2::HalPolicy>(operation, 10, model, data);
1773 }
1774
1775 armnnUtils::DataLayoutIndexed dataLayoutIndexed(desc.m_DataLayout);
1776 unsigned int widthIndex = dataLayoutIndexed.GetWidthIndex();
1777 unsigned int heightIndex = dataLayoutIndexed.GetHeightIndex();
1778
1779 const armnn::PermutationVector OHWIToOIHW = {0, 2, 3, 1};
1780
1781 // The shape of the weight is [depth_out, filter_height, filter_width, depth_in].
1782 // We have to permute it to OIHW if the data layout is NCHW.
1783 const ConstTensorPin weightsPin = (desc.m_DataLayout == armnn::DataLayout::NCHW) ?
1784 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data, OHWIToOIHW) :
1785 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 1, model, data);
1786
1787 // Bias is a 1D tensor
1788 const ConstTensorPin biasPin =
1789 ConvertOperationInputToConstTensorPin<hal_1_2::HalPolicy>(operation, 2, model, data);
1790
1791 if (!weightsPin.IsValid())
1792 {
1793 return Fail("%s: Operation has invalid weights", __func__);
1794 }
1795
1796 if (!biasPin.IsValid())
1797 {
1798 return Fail("%s: Operation has invalid biases", __func__);
1799 }
1800
1801 armnn::ConstTensor weights = weightsPin.GetConstTensor();
1802 armnn::ConstTensor bias = biasPin.GetConstTensor();
1803 SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo);
1804
1805 ActivationFn activation;
1806
1807 if (implicitPadding)
1808 {
1809 android::nn::PaddingScheme paddingScheme;
1810 if (!GetInputPaddingScheme<hal_1_2::HalPolicy>(operation, 4, paddingScheme, model, data) ||
1811 !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_StrideX, model, data) ||
1812 !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_StrideY, model, data) ||
1813 !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 7, activation, model, data))
1814 {
1815 return Fail("%s: Operation has invalid inputs (implicit padding)", __func__);
1816 }
1817
1818 const uint32_t kernelX = weights.GetShape()[widthIndex];
1819 const uint32_t kernelY = weights.GetShape()[heightIndex];
Narumol Prangnawaratc8bdb392019-08-01 15:51:44 +01001820 const uint32_t outputX = outputInfo.GetShape()[widthIndex];
1821 const uint32_t outputY = outputInfo.GetShape()[heightIndex];
David Monahan613b49c2019-06-27 11:37:47 +01001822
Narumol Prangnawaratc8bdb392019-08-01 15:51:44 +01001823 int32_t padLeft{0};
1824 int32_t padRight{0};
1825 int32_t padTop{0};
1826 int32_t padBottom{0};
1827
1828 CalcPaddingTransposeConv(outputX, kernelX, desc.m_StrideX, padLeft, padRight, paddingScheme);
1829 CalcPaddingTransposeConv(outputY, kernelY, desc.m_StrideY, padTop, padBottom, paddingScheme);
1830
1831 // NOTE: The Android NN API allows for negative padding values in TransposeConv2d,
1832 // but Arm NN only supports values >= 0
1833 if (padLeft < 0 || padRight < 0 || padTop < 0 || padBottom < 0)
1834 {
1835 return Fail("%s: Negative padding values are not supported", __func__);
1836 }
1837
1838 desc.m_PadLeft = boost::numeric_cast<uint32_t>(padLeft);
1839 desc.m_PadRight = boost::numeric_cast<uint32_t>(padRight);
1840 desc.m_PadTop = boost::numeric_cast<uint32_t>(padTop);
1841 desc.m_PadBottom = boost::numeric_cast<uint32_t>(padBottom);
David Monahan613b49c2019-06-27 11:37:47 +01001842 }
1843 else if (operation.inputs.size() == 11)
1844 {
1845 // explicit padding
1846 if (!GetInputScalar<hal_1_2::HalPolicy>(operation, 3, OperandType::INT32, desc.m_PadLeft, model, data) ||
1847 !GetInputScalar<hal_1_2::HalPolicy>(operation, 4, OperandType::INT32, desc.m_PadRight, model, data) ||
1848 !GetInputScalar<hal_1_2::HalPolicy>(operation, 5, OperandType::INT32, desc.m_PadTop, model, data) ||
1849 !GetInputScalar<hal_1_2::HalPolicy>(operation, 6, OperandType::INT32, desc.m_PadBottom, model, data) ||
1850 !GetInputScalar<hal_1_2::HalPolicy>(operation, 7, OperandType::INT32, desc.m_StrideX, model, data) ||
1851 !GetInputScalar<hal_1_2::HalPolicy>(operation, 8, OperandType::INT32, desc.m_StrideY, model, data) ||
1852 !GetInputActivationFunction<hal_1_2::HalPolicy>(operation, 9, activation, model, data))
1853 {
1854 return Fail("%s: Operation has invalid inputs (explicit padding)", __func__);
1855 }
1856 }
1857 else
1858 {
1859 return Fail("%s: Unsupported number of operation inputs", __func__);
1860 }
1861
1862 desc.m_BiasEnabled = true;
1863 armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo());
1864
1865 bool isSupported = false;
1866 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1867 IsTransposeConvolution2dSupported,
1868 data.m_Backends,
1869 isSupported,
1870 inputInfo,
1871 outputInfo,
1872 desc,
1873 weights.GetInfo(),
1874 biases);
1875 if (!isSupported)
1876 {
1877 return false;
1878 }
1879
1880 armnn::IConnectableLayer* startLayer =
1881 data.m_Network->AddTransposeConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias));
1882 if (!startLayer)
1883 {
1884 return Fail("%s: AddTransposeConvolution2dLayer failed", __func__);
1885 }
1886
1887 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data);
1888 if (!endLayer)
1889 {
1890 return Fail("%s: ProcessActivation failed", __func__);
1891 }
1892
1893 input.Connect(startLayer->GetInputSlot(0));
1894
1895 return SetupAndTrackLayerOutputSlot<hal_1_2::HalPolicy>(operation, 0, *endLayer, model, data);
1896}
1897
Mike Kellyb5fdf382019-06-11 16:35:25 +01001898} // namespace hal_1_2
Matteo Martincigh17ffff32019-06-27 14:12:55 +01001899} // namespace armnn_driver