blob: 294c77ce5d25b2bf64cd73ab1ea4a96ad8d8c52a [file] [log] [blame]
arovir01b0717b52018-09-05 17:03:25 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "HalPolicy.hpp"
7
8#include "../1.0/HalPolicy.hpp"
9
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010010namespace
11{
12static std::vector<V1_0::OperationType> opsEquivalentInV10({
13 V1_0::OperationType::ADD,
14 V1_0::OperationType::AVERAGE_POOL_2D,
15 V1_0::OperationType::CONCATENATION,
16 V1_0::OperationType::CONV_2D,
17 V1_0::OperationType::DEPTHWISE_CONV_2D,
David Monahand5bfae12019-05-30 12:07:44 +010018 V1_0::OperationType::DEQUANTIZE,
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010019 V1_0::OperationType::FLOOR,
20 V1_0::OperationType::FULLY_CONNECTED,
21 V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION,
22 V1_0::OperationType::LOGISTIC,
23 V1_0::OperationType::LSTM,
24 V1_0::OperationType::L2_NORMALIZATION,
25 V1_0::OperationType::L2_POOL_2D,
26 V1_0::OperationType::MAX_POOL_2D,
27 V1_0::OperationType::MUL,
28 V1_0::OperationType::RELU,
29 V1_0::OperationType::RELU1,
30 V1_0::OperationType::RELU6,
31 V1_0::OperationType::SOFTMAX,
32 V1_0::OperationType::TANH,
33 V1_0::OperationType::RESHAPE,
34 V1_0::OperationType::RESIZE_BILINEAR,
35});
36
37bool CompliantWithVersion10(const V1_1::Operation & operation)
38{
39 std::vector<V1_0::OperationType>::iterator it;
40 it = std::find(opsEquivalentInV10.begin(), opsEquivalentInV10.end(),
41 static_cast<V1_0::OperationType>(operation.type));
42
43 if(it != opsEquivalentInV10.end())
44 {
45 return true;
46 }
47 return false;
48}
49
50V1_0::Operation ConvertOperationToVersion10(const V1_1::Operation & operation)
51{
52 V1_0::Operation v10Operation;
53 v10Operation.type = static_cast<V1_0::OperationType>(operation.type);
54 v10Operation.inputs = operation.inputs;
55 v10Operation.outputs = operation.outputs;
56 return v10Operation;
57}
58}
59
arovir01b0717b52018-09-05 17:03:25 +010060namespace armnn_driver
61{
62namespace hal_1_1
63{
64
65bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data)
66{
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010067 if (CompliantWithVersion10(operation))
arovir01b0717b52018-09-05 17:03:25 +010068 {
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010069 hal_1_0::HalPolicy::Operation v10Operation = ConvertOperationToVersion10(operation);
arovir01b0717b52018-09-05 17:03:25 +010070 hal_1_0::HalPolicy::Model v10Model = convertToV1_0(model);
71
72 return hal_1_0::HalPolicy::ConvertOperation(v10Operation, v10Model, data);
73 }
74 else
75 {
76 switch (operation.type)
77 {
78 case V1_1::OperationType::DIV:
79 return ConvertDiv(operation, model, data);
David Beck38e12942018-09-12 16:02:24 +010080 case V1_1::OperationType::SUB:
81 return ConvertSub(operation, model, data);
narpra013c052562018-09-17 14:25:04 +010082 case V1_1::OperationType::MEAN:
83 return ConvertMean(operation, model, data);
Nina Drozd62a4a9f2018-10-01 14:20:25 +010084 case V1_1::OperationType::PAD:
85 return ConvertPad(operation, model, data);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +000086 case V1_1::OperationType::SPACE_TO_BATCH_ND:
87 return ConvertSpaceToBatchNd(operation, model, data);
saoste01b8471482018-10-10 09:44:51 +010088 case V1_1::OperationType::SQUEEZE:
89 return ConvertSqueeze(operation, model, data);
Sadik Armagan758eee82018-11-15 15:34:49 +000090 case V1_1::OperationType::STRIDED_SLICE:
91 return ConvertStridedSlice(operation, model, data);
saoste01fe463152018-10-18 17:49:56 +010092 case V1_1::OperationType::TRANSPOSE:
93 return ConvertTranspose(operation, model, data);
Éanna Ó Catháin2cd99b92018-11-14 14:33:52 +000094 case V1_1::OperationType::BATCH_TO_SPACE_ND:
95 return ConvertBatchToSpaceNd(operation, model, data);
arovir01b0717b52018-09-05 17:03:25 +010096 default:
97 return Fail("%s: Operation type %s not supported in ArmnnDriver",
98 __func__, toString(operation.type).c_str());
99 }
100 }
101}
102
103bool HalPolicy::ConvertDiv(const Operation& operation, const Model& model, ConversionData& data)
104{
105 LayerInputHandle input0 = ConvertToLayerInputHandle(operation, 0, model, data);
106 LayerInputHandle input1 = ConvertToLayerInputHandle(operation, 1, model, data);
107
108 if (!input0.IsValid() || !input1.IsValid())
109 {
110 return Fail("%s: Operation has invalid inputs", __func__);
111 }
112
113 // The FuseActivation parameter is always the input index 2
114 // and it should be optional
115 ActivationFn activationFunction;
116 if (!GetOptionalInputActivation(operation, 2, activationFunction, model, data))
117 {
118 return Fail("%s: Operation has invalid inputs", __func__);
119 }
120
121 const Operand* outputOperand = GetOutputOperand(operation, 0, model);
122 if (!outputOperand)
123 {
124 return false;
125 }
126
127 const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand);
128
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100129 if (!IsLayerSupportedForAnyBackend(__func__,
130 armnn::IsDivisionSupported,
131 data.m_Backends,
132 input0.GetTensorInfo(),
133 input1.GetTensorInfo(),
134 outInfo))
arovir01b0717b52018-09-05 17:03:25 +0100135 {
136 return false;
137 }
138
139 armnn::IConnectableLayer* const startLayer = data.m_Network->AddDivisionLayer();
140 armnn::IConnectableLayer* const endLayer = ProcessActivation(outInfo, activationFunction, startLayer, data);
141
142 const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo();
143 const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo();
144
145 if (endLayer)
146 {
147 BroadcastTensor(input0, input1, startLayer, *data.m_Network);
148 return SetupAndTrackLayerOutputSlot(operation, 0, *endLayer, model, data);
149 }
150
151 return Fail("%s: ProcessActivation failed", __func__);
152}
153
David Beck38e12942018-09-12 16:02:24 +0100154bool HalPolicy::ConvertSub(const Operation& operation, const Model& model, ConversionData& data)
155{
156 LayerInputHandle input0 = ConvertToLayerInputHandle(operation, 0, model, data);
157 LayerInputHandle input1 = ConvertToLayerInputHandle(operation, 1, model, data);
158
159 if (!input0.IsValid() || !input1.IsValid())
160 {
161 return Fail("%s: Operation has invalid inputs", __func__);
162 }
163
164 // The FuseActivation parameter is always the input index 2
165 // and it should be optional
166 ActivationFn activationFunction;
167 if (!GetOptionalInputActivation(operation, 2, activationFunction, model, data))
168 {
169 return Fail("%s: Operation has invalid inputs", __func__);
170 }
171
172 const Operand* outputOperand = GetOutputOperand(operation, 0, model);
173 if (!outputOperand)
174 {
175 return false;
176 }
177
178 const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand);
179
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100180 if (!IsLayerSupportedForAnyBackend(__func__,
181 armnn::IsSubtractionSupported,
182 data.m_Backends,
183 input0.GetTensorInfo(),
184 input1.GetTensorInfo(),
185 outInfo))
David Beck38e12942018-09-12 16:02:24 +0100186 {
187 return false;
188 }
189
190 armnn::IConnectableLayer* const startLayer = data.m_Network->AddSubtractionLayer();
191 armnn::IConnectableLayer* const endLayer = ProcessActivation(outInfo, activationFunction, startLayer, data);
192
193 const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo();
194 const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo();
195
196 if (endLayer)
197 {
198 BroadcastTensor(input0, input1, startLayer, *data.m_Network);
199 return SetupAndTrackLayerOutputSlot(operation, 0, *endLayer, model, data);
200 }
201
202 return Fail("%s: ProcessActivation failed", __func__);
203}
204
narpra013c052562018-09-17 14:25:04 +0100205bool HalPolicy::ConvertMean(const Operation& operation, const Model& model, ConversionData& data)
206{
207 LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data);
narpra013c052562018-09-17 14:25:04 +0100208 if (!input.IsValid())
209 {
210 return Fail("%s: Operation has invalid inputs", __func__);
211 }
212
Matteo Martincighae622b72018-10-23 18:25:38 +0100213 const Operand* axisOperand = GetInputOperand(operation, 1, model);
214 if (!axisOperand)
215 {
216 return Fail("%s: Could not read input 1", __func__);
217 }
218
219 std::vector<int32_t> axis;
220 if (!GetTensorInt32Values(*axisOperand, axis, model, data))
221 {
222 return Fail("%s: Input 1 has invalid values", __func__);
223 }
224
225 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
226
227 // Convert the axis to unsigned int and remove duplicates.
228 unsigned int rank = inputInfo.GetNumDimensions();
229 std::set<unsigned int> uniqueAxis;
230 std::transform(axis.begin(), axis.end(),
231 std::inserter(uniqueAxis, uniqueAxis.begin()),
232 [rank](int i) -> unsigned int { return (i + rank) % rank; });
233
234 // Get the "keep dims" flag.
235 int32_t keepDims = 0;
236 if (!GetInputInt32(operation, 2, keepDims, model, data))
237 {
238 return Fail("%s: Could not read input 2", __func__);
239 }
narpra013c052562018-09-17 14:25:04 +0100240
241 armnn::MeanDescriptor descriptor;
Matteo Martincighae622b72018-10-23 18:25:38 +0100242 descriptor.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
243 descriptor.m_KeepDims = keepDims > 0;
narpra013c052562018-09-17 14:25:04 +0100244
245 const Operand* output = GetOutputOperand(operation, 0, model);
246 if (!output)
247 {
248 return Fail("%s: Could not read output 0", __func__);
249 }
250
251 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
252
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100253 if (!IsLayerSupportedForAnyBackend(__func__,
254 armnn::IsMeanSupported,
255 data.m_Backends,
256 inputInfo,
257 outputInfo,
258 descriptor))
narpra013c052562018-09-17 14:25:04 +0100259 {
260 return false;
261 }
262
263 armnn::IConnectableLayer* const layer = data.m_Network->AddMeanLayer(descriptor);
narpra0196bedf02018-09-26 16:57:28 +0100264 assert(layer != nullptr);
265 input.Connect(layer->GetInputSlot(0));
narpra013c052562018-09-17 14:25:04 +0100266
267 return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data);
268}
269
Nina Drozd62a4a9f2018-10-01 14:20:25 +0100270bool HalPolicy::ConvertPad(const Operation& operation, const Model& model, ConversionData& data)
271{
272 LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data);
273
274 if (!input.IsValid())
275 {
276 return Fail("%s: Operation has invalid inputs", __func__);
277 }
278
279 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
280
281 const Operand* paddingsOperand = GetInputOperand(operation, 1, model);
282
283 if (!paddingsOperand)
284 {
285 return Fail("%s: Could not read paddings operand", __func__);
286 }
287
288 unsigned int rank = inputInfo.GetNumDimensions();
289 armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand);
Éanna Ó Catháin074f1ec2019-01-14 16:18:49 +0000290 if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != rank * 2)
Nina Drozd62a4a9f2018-10-01 14:20:25 +0100291 {
292 return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, rank);
293 }
294
295 std::vector<int32_t> paddings;
296 GetTensorInt32Values(*paddingsOperand, paddings, model, data);
297
298 // add padding for each dimension of input tensor.
299 armnn::PadDescriptor descriptor;
300 for (unsigned int i = 0; i < paddings.size() - 1; i += 2)
301 {
302 int paddingBeforeInput = paddings[i];
303 int paddingAfterInput = paddings[i + 1];
304 if (paddingBeforeInput < 0 || paddingAfterInput < 0)
305 {
306 return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__);
307 }
308 descriptor.m_PadList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput);
309 }
310
311 const Operand* output = GetOutputOperand(operation, 0, model);
312 if (!output)
313 {
314 return Fail("%s: Could not read output 0", __func__);
315 }
316
317 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
318
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100319 if (!IsLayerSupportedForAnyBackend(__func__,
320 armnn::IsPadSupported,
321 data.m_Backends,
322 inputInfo,
323 outputInfo,
324 descriptor))
Nina Drozd62a4a9f2018-10-01 14:20:25 +0100325 {
326 return false;
327 }
328
329 armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor);
330 assert(layer != nullptr);
331 input.Connect(layer->GetInputSlot(0));
332 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
333
334 return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data);
335}
336
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000337bool HalPolicy::ConvertSpaceToBatchNd(const Operation& operation, const Model& model, ConversionData& data)
338{
339 LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data);
340
341 if (!input.IsValid())
342 {
343 return Fail("%s: Operation has invalid inputs", __func__);
344 }
345
346 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
347 unsigned int rank = inputInfo.GetNumDimensions();
348 unsigned int spatialDim = rank - 2;
349
350 if (rank != 4)
351 {
352 Fail("%s: Only inputs with rank 4 are supported", __func__);
353 }
354
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000355 const Operand* blockShapeOperand = GetInputOperand(operation, 1, model);
356 const Operand* paddingsOperand = GetInputOperand(operation, 2, model);
357
358 armnn::TensorShape blockShapeOperandShape = GetTensorShapeForOperand(*blockShapeOperand);
359 if (blockShapeOperandShape.GetNumDimensions() != 1 || blockShapeOperandShape.GetNumElements() != spatialDim)
360 {
361 return Fail("%s: Operation has invalid block shape operand: expected shape [%d]", __func__, spatialDim);
362 }
363
364 std::vector<int32_t> blockShape;
365 GetTensorInt32Values(*blockShapeOperand, blockShape, model, data);
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000366 if (std::any_of(blockShape.cbegin(), blockShape.cend(), [](int32_t i){ return i < 1; }))
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000367 {
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000368 return Fail("%s: Block shape must be at least 1 in all dimensions.", __func__);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000369 }
370
371 armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand);
372 if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != 2 * spatialDim)
373 {
374 return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, spatialDim);
375 }
376
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000377 std::vector<std::pair<unsigned int, unsigned int>> paddingList;
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000378 std::vector<int32_t> paddings;
379 GetTensorInt32Values(*paddingsOperand, paddings, model, data);
380 for (unsigned int i = 0; i < paddings.size() - 1; i += 2)
381 {
382 int paddingBeforeInput = paddings[i];
383 int paddingAfterInput = paddings[i + 1];
384 if (paddingBeforeInput < 0 || paddingAfterInput < 0)
385 {
386 return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__);
387 }
388
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000389 paddingList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000390 }
391
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000392 armnn::SpaceToBatchNdDescriptor descriptor;
393 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
394 descriptor.m_BlockShape.assign(blockShape.cbegin(), blockShape.cend());
395 descriptor.m_PadList.assign(paddingList.cbegin(), paddingList.cend());
396
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000397 const Operand* output = GetOutputOperand(operation, 0, model);
398 if (!output)
399 {
400 return Fail("%s: Could not read output 0", __func__);
401 }
402
403 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100404 if (!IsLayerSupportedForAnyBackend(__func__,
405 armnn::IsSpaceToBatchNdSupported,
406 data.m_Backends,
407 inputInfo,
408 outputInfo,
409 descriptor))
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000410 {
411 return false;
412 }
413
414 armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToBatchNdLayer(descriptor);
415 assert(layer != nullptr);
416 input.Connect(layer->GetInputSlot(0));
417
418 return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data);
419}
420
saoste01b8471482018-10-10 09:44:51 +0100421bool HalPolicy::ConvertSqueeze(const Operation& operation, const Model& model, ConversionData& data)
422{
saoste01b8471482018-10-10 09:44:51 +0100423 LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data);
424
425 if (!input.IsValid())
426 {
427 return Fail("%s: Operation has invalid inputs", __func__);
428 }
429
430 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
431
432 unsigned int rank = inputInfo.GetNumDimensions();
saoste01fe463152018-10-18 17:49:56 +0100433 if (rank > 4)
saoste01b8471482018-10-10 09:44:51 +0100434 {
saoste01fe463152018-10-18 17:49:56 +0100435 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
saoste01b8471482018-10-10 09:44:51 +0100436 }
437
438 // NOTE: Axis is an optional parameter to SQUEEZE, therefore we do not want to generate a failure
439 // if the operand index is out of bounds.
440 const Operand* axisOperand = GetInputOperand(operation, 1, model, false);
441
saoste01fe463152018-10-18 17:49:56 +0100442 const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
443
saoste01b8471482018-10-10 09:44:51 +0100444 std::vector<int32_t> axis;
saoste01fe463152018-10-18 17:49:56 +0100445 if (!axisOperand)
saoste01b8471482018-10-10 09:44:51 +0100446 {
447 axis.assign(dimensionSequence,
saoste01fe463152018-10-18 17:49:56 +0100448 dimensionSequence + rank);
saoste01b8471482018-10-10 09:44:51 +0100449 }
450 else
451 {
452 GetTensorInt32Values(*axisOperand, axis, model, data);
453 }
454
saoste01b8471482018-10-10 09:44:51 +0100455
saoste01a893efa2018-10-13 11:56:12 +0100456 std::vector<uint32_t> outputDims;
saoste01fe463152018-10-18 17:49:56 +0100457 for (unsigned int i = 0; i < rank; i++)
saoste01a893efa2018-10-13 11:56:12 +0100458 {
459 bool skipSqueeze = (std::find(axis.begin(), axis.end(), i) == axis.end());
460 auto currentDimension = inputInfo.GetShape()[i];
saoste01b8471482018-10-10 09:44:51 +0100461 if (skipSqueeze || currentDimension != 1)
462 {
463 outputDims.push_back(currentDimension);
464 }
465 }
466
saoste01fe463152018-10-18 17:49:56 +0100467 armnn::TensorShape outShape = armnn::TensorShape(outputDims.size(), outputDims.data());
saoste01b8471482018-10-10 09:44:51 +0100468
469 armnn::TensorInfo outputInfo = inputInfo;
470 outputInfo.SetShape(outShape);
471
472 armnn::ReshapeDescriptor reshapeDesc;
473 reshapeDesc.m_TargetShape = outputInfo.GetShape();
474
475 const Operand* output = GetOutputOperand(operation, 0, model);
476 if (!output)
477 {
478 return Fail("%s: Could not read output 0", __func__);
479 }
480
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100481 if (!IsLayerSupportedForAnyBackend(__func__,
482 armnn::IsReshapeSupported,
483 data.m_Backends,
484 inputInfo,
485 reshapeDesc))
saoste01b8471482018-10-10 09:44:51 +0100486 {
487 return false;
488 }
489
490 armnn::IConnectableLayer* const layer = data.m_Network->AddReshapeLayer(reshapeDesc);
491 assert(layer != nullptr);
492 input.Connect(layer->GetInputSlot(0));
saoste01fe463152018-10-18 17:49:56 +0100493
494 return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data);
495}
496
Sadik Armagan758eee82018-11-15 15:34:49 +0000497bool HalPolicy::ConvertStridedSlice(const Operation& operation, const Model& model, ConversionData& data)
498{
499 LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data);
500 if (!input.IsValid())
501 {
502 return Fail("%s: Operation has invalid inputs", __func__);
503 }
504 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
505
506 unsigned int rank = inputInfo.GetNumDimensions();
507 if (rank > 4)
508 {
509 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
510 }
511
512 const Operand* beginOperand = GetInputOperand(operation, 1, model);
513 const Operand* endOperand = GetInputOperand(operation, 2, model);
514 const Operand* stridesOperand = GetInputOperand(operation, 3, model);
515
516 std::vector<int32_t> beginValues;
517 std::vector<int32_t> endValues;
518 std::vector<int32_t> stridesValues;
519
520 // The length of the beginOperand, endOperand and stridesOperand must be of a rank(input)
521 auto ValidateInputOperands = [&] (const Operand& operand, std::vector<int32_t>& operandValues)
522 {
523 if (!GetTensorInt32Values(operand, operandValues, model, data))
524 {
525 return false;
526 }
527
528 if (operandValues.size() != rank)
529 {
530 return false;
531 }
532
533 return true;
534 };
535
536 if (!ValidateInputOperands(*beginOperand, beginValues)
537 || !ValidateInputOperands(*endOperand, endValues)
538 || !ValidateInputOperands(*stridesOperand, stridesValues))
539 {
540 return Fail("%s: Operation has invalid input operand", __func__);
541 }
542
543 // Stride cannot have value '0'
544 if (std::any_of(stridesValues.cbegin(), stridesValues.cend(), [](int32_t i){ return i == 0; }))
545 {
546 return Fail("%s: Stride must be non-zero value.", __func__);
547 }
548
549 armnn::StridedSliceDescriptor descriptor;
550 descriptor.m_Begin.assign(beginValues.cbegin(), beginValues.cend());
551 descriptor.m_End.assign(endValues.cbegin(), endValues.cend());
552 descriptor.m_Stride.assign(stridesValues.cbegin(), stridesValues.cend());
553 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
554
555 // Get the "begin_mask", "end_mask", and "shrink_axis_mask" flags
556 if (!GetInputInt32(operation, 4, descriptor.m_BeginMask, model, data)
557 || !GetInputInt32(operation, 5, descriptor.m_EndMask, model, data)
558 || !GetInputInt32(operation, 6, descriptor.m_ShrinkAxisMask, model, data))
559 {
560 return Fail("%s: Operation has invalid inputs", __func__);
561 }
562
563 const Operand* output = GetOutputOperand(operation, 0, model);
564 if (!output)
565 {
566 return Fail("%s: Could not read output 0", __func__);
567 }
568 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
569
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100570 if (!IsLayerSupportedForAnyBackend(__func__,
571 armnn::IsStridedSliceSupported,
572 data.m_Backends,
573 inputInfo,
574 outputInfo,
575 descriptor))
Sadik Armagan758eee82018-11-15 15:34:49 +0000576 {
577 return false;
578 }
579
580 armnn::IConnectableLayer* const layer = data.m_Network->AddStridedSliceLayer(descriptor);
581 assert(layer != nullptr);
582 input.Connect(layer->GetInputSlot(0));
583
584 return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data);
585}
586
saoste01fe463152018-10-18 17:49:56 +0100587bool HalPolicy::ConvertTranspose(const Operation& operation, const Model& model, ConversionData& data)
588{
589 LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data);
590
591 if (!input.IsValid())
592 {
593 return Fail("%s: Operation has invalid inputs", __func__);
594 }
595
596 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
597
598 unsigned int rank = inputInfo.GetNumDimensions();
599 if (rank > 4)
600 {
601 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
602 }
603
604 // NOTE: Axis is an optional parameter to TRANSPOSE, therefore we do not want to generate a failure
605 // if the operand index is out of bounds.
606 const Operand* permOperand = GetInputOperand(operation, 1, model, false);
607
608 std::vector<int32_t> perm(rank);
609 if (!permOperand)
610 {
611 // NOTE: If perm is not given, it is set to (n-1...0), where n is the rank of the tensor
612 for (unsigned int i = rank; i > 0; i--)
613 {
614 perm[rank - i] = boost::numeric_cast<int> (i - 1);
615 }
616 }
617 else
618 {
619 GetTensorInt32Values(*permOperand, perm, model, data);
620 }
621
622 std::vector<uint32_t> outputDims(perm.begin(), perm.begin() + rank);
623
624 auto permutationVector = armnn::PermutationVector(outputDims.data(), outputDims.size());
625 if (!permutationVector.IsEqual(NHWCToArmNN)
626 && !permutationVector.IsEqual(ArmNNToNHWC)
627 && !permutationVector.IsEqual({ 3, 2, 0, 1 }))
628 {
629 return Fail("%s: Only [0, 3, 1, 2], [0, 2, 3, 1] and [3, 2, 0, 1] permutations are supported.", __func__);
630 }
631
632 armnn::PermuteDescriptor permuteDesc;
633 permuteDesc.m_DimMappings = permutationVector;
634
635 const Operand* output = GetOutputOperand(operation, 0, model);
636 if (!output)
637 {
638 return Fail("%s: Could not read output 0", __func__);
639 }
640
641 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
642
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100643 if (!IsLayerSupportedForAnyBackend(__func__,
644 armnn::IsPermuteSupported,
645 data.m_Backends,
646 inputInfo,
647 outputInfo,
648 permuteDesc))
saoste01fe463152018-10-18 17:49:56 +0100649 {
650 return false;
651 }
652
653 armnn::IConnectableLayer* const layer = data.m_Network->AddPermuteLayer(permuteDesc);
654 assert(layer != nullptr);
655 input.Connect(layer->GetInputSlot(0));
saoste01b8471482018-10-10 09:44:51 +0100656
657 return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data);
658}
659
Éanna Ó Catháin2cd99b92018-11-14 14:33:52 +0000660bool HalPolicy::ConvertBatchToSpaceNd(const Operation& operation, const Model& model, ConversionData& data)
661{
662 LayerInputHandle input = ConvertToLayerInputHandle(operation, 0, model, data);
663 if (!input.IsValid())
664 {
665 return Fail("%s: Operation has invalid inputs", __func__);
666 }
667
668 const Operand* blockOperand = GetInputOperand(operation, 1, model);
669 if (!blockOperand)
670 {
671 return Fail("%s: Could not read input 1", __func__);
672 }
673
674 // Convert the block operand to int32
675 std::vector<int32_t> block;
676 if (!GetTensorInt32Values(*blockOperand, block, model, data))
677 {
678 return Fail("%s: Input 1 has invalid values", __func__);
679 }
680
681 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
682
683 unsigned int rank = inputInfo.GetNumDimensions();
684 if (rank != 4)
685 {
686 Fail("%s: Only inputs with rank equal to 4 are supported", __func__);
687 }
688
689 if (std::any_of(block.cbegin(), block.cend(), [](int32_t i){ return i < 1; }))
690 {
691 return Fail("%s: Block sizes for each spatial dimension of the input tensor must be"
692 " greater than or equal to 1", __func__);
693 }
694
695 armnn::BatchToSpaceNdDescriptor batchToSpaceNdDesc;
696 batchToSpaceNdDesc.m_BlockShape.assign(block.cbegin(), block.cend());
697 batchToSpaceNdDesc.m_DataLayout = armnn::DataLayout::NHWC;
698
699 // Setting crops to 0,0 0,0 as it is not supported in Android NN API
700 batchToSpaceNdDesc.m_Crops = {{0, 0}, {0, 0}};
701
702 const Operand* output = GetOutputOperand(operation, 0, model);
703 if (!output)
704 {
705 return Fail("%s: Could not read output 0", __func__);
706 }
707
708 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
709
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100710 if (!IsLayerSupportedForAnyBackend(__func__,
711 armnn::IsBatchToSpaceNdSupported,
712 data.m_Backends,
713 inputInfo,
714 outputInfo,
715 batchToSpaceNdDesc))
Éanna Ó Catháin2cd99b92018-11-14 14:33:52 +0000716 {
717 return false;
718 }
719
720 armnn::IConnectableLayer* const layer = data.m_Network->AddBatchToSpaceNdLayer(batchToSpaceNdDesc);
721 assert(layer != nullptr);
722 input.Connect(layer->GetInputSlot(0));
723
724 return SetupAndTrackLayerOutputSlot(operation, 0, *layer, model, data);
725}
726
727
arovir01b0717b52018-09-05 17:03:25 +0100728} // namespace hal_1_1
Matteo Martincigh265d1ad2019-01-08 18:14:53 +0000729} // namespace armnn_driver