blob: 2fab47458abb3b3cf0710f750594d5bee324931e [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
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +01008#include "Utils.hpp"
Aron Virginas-Tar366e0a62019-07-10 13:01:41 +01009
arovir01b0717b52018-09-05 17:03:25 +010010#include "../1.0/HalPolicy.hpp"
11
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010012namespace
13{
14static std::vector<V1_0::OperationType> opsEquivalentInV10({
15 V1_0::OperationType::ADD,
16 V1_0::OperationType::AVERAGE_POOL_2D,
17 V1_0::OperationType::CONCATENATION,
18 V1_0::OperationType::CONV_2D,
19 V1_0::OperationType::DEPTHWISE_CONV_2D,
David Monahand5bfae12019-05-30 12:07:44 +010020 V1_0::OperationType::DEQUANTIZE,
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010021 V1_0::OperationType::FLOOR,
22 V1_0::OperationType::FULLY_CONNECTED,
23 V1_0::OperationType::LOCAL_RESPONSE_NORMALIZATION,
24 V1_0::OperationType::LOGISTIC,
25 V1_0::OperationType::LSTM,
26 V1_0::OperationType::L2_NORMALIZATION,
27 V1_0::OperationType::L2_POOL_2D,
28 V1_0::OperationType::MAX_POOL_2D,
29 V1_0::OperationType::MUL,
30 V1_0::OperationType::RELU,
31 V1_0::OperationType::RELU1,
32 V1_0::OperationType::RELU6,
33 V1_0::OperationType::SOFTMAX,
Keith Davisa6bc52f2019-06-26 09:39:49 +010034 V1_0::OperationType::SPACE_TO_DEPTH,
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010035 V1_0::OperationType::TANH,
36 V1_0::OperationType::RESHAPE,
37 V1_0::OperationType::RESIZE_BILINEAR,
38});
39
40bool CompliantWithVersion10(const V1_1::Operation & operation)
41{
42 std::vector<V1_0::OperationType>::iterator it;
43 it = std::find(opsEquivalentInV10.begin(), opsEquivalentInV10.end(),
44 static_cast<V1_0::OperationType>(operation.type));
45
46 if(it != opsEquivalentInV10.end())
47 {
48 return true;
49 }
50 return false;
51}
52
53V1_0::Operation ConvertOperationToVersion10(const V1_1::Operation & operation)
54{
55 V1_0::Operation v10Operation;
56 v10Operation.type = static_cast<V1_0::OperationType>(operation.type);
57 v10Operation.inputs = operation.inputs;
58 v10Operation.outputs = operation.outputs;
59 return v10Operation;
60}
61}
62
arovir01b0717b52018-09-05 17:03:25 +010063namespace armnn_driver
64{
65namespace hal_1_1
66{
67
68bool HalPolicy::ConvertOperation(const Operation& operation, const Model& model, ConversionData& data)
69{
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010070 if (CompliantWithVersion10(operation))
arovir01b0717b52018-09-05 17:03:25 +010071 {
Éanna Ó Catháin2fc21f72019-05-13 11:01:33 +010072 hal_1_0::HalPolicy::Operation v10Operation = ConvertOperationToVersion10(operation);
arovir01b0717b52018-09-05 17:03:25 +010073 hal_1_0::HalPolicy::Model v10Model = convertToV1_0(model);
74
75 return hal_1_0::HalPolicy::ConvertOperation(v10Operation, v10Model, data);
76 }
77 else
78 {
79 switch (operation.type)
80 {
81 case V1_1::OperationType::DIV:
82 return ConvertDiv(operation, model, data);
David Beck38e12942018-09-12 16:02:24 +010083 case V1_1::OperationType::SUB:
84 return ConvertSub(operation, model, data);
narpra013c052562018-09-17 14:25:04 +010085 case V1_1::OperationType::MEAN:
86 return ConvertMean(operation, model, data);
Nina Drozd62a4a9f2018-10-01 14:20:25 +010087 case V1_1::OperationType::PAD:
Aron Virginas-Tarc921f6b2019-07-25 10:14:33 +010088 return ConvertPad(operation, model, data);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +000089 case V1_1::OperationType::SPACE_TO_BATCH_ND:
90 return ConvertSpaceToBatchNd(operation, model, data);
saoste01b8471482018-10-10 09:44:51 +010091 case V1_1::OperationType::SQUEEZE:
92 return ConvertSqueeze(operation, model, data);
Sadik Armagan758eee82018-11-15 15:34:49 +000093 case V1_1::OperationType::STRIDED_SLICE:
94 return ConvertStridedSlice(operation, model, data);
saoste01fe463152018-10-18 17:49:56 +010095 case V1_1::OperationType::TRANSPOSE:
96 return ConvertTranspose(operation, model, data);
Éanna Ó Catháin2cd99b92018-11-14 14:33:52 +000097 case V1_1::OperationType::BATCH_TO_SPACE_ND:
98 return ConvertBatchToSpaceNd(operation, model, data);
arovir01b0717b52018-09-05 17:03:25 +010099 default:
100 return Fail("%s: Operation type %s not supported in ArmnnDriver",
101 __func__, toString(operation.type).c_str());
102 }
103 }
104}
105
106bool HalPolicy::ConvertDiv(const Operation& operation, const Model& model, ConversionData& data)
107{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100108 ALOGV("hal_1_1::HalPolicy::ConvertDiv()");
109
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100110 LayerInputHandle input0 = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data);
111 LayerInputHandle input1 = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 1, model, data);
arovir01b0717b52018-09-05 17:03:25 +0100112
113 if (!input0.IsValid() || !input1.IsValid())
114 {
115 return Fail("%s: Operation has invalid inputs", __func__);
116 }
117
118 // The FuseActivation parameter is always the input index 2
119 // and it should be optional
120 ActivationFn activationFunction;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100121 if (!GetOptionalInputActivation<hal_1_1::HalPolicy>(operation, 2, activationFunction, model, data))
arovir01b0717b52018-09-05 17:03:25 +0100122 {
123 return Fail("%s: Operation has invalid inputs", __func__);
124 }
125
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100126 const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model);
127 if (!output)
arovir01b0717b52018-09-05 17:03:25 +0100128 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100129 return Fail("%s: Could not read output 0", __func__);
arovir01b0717b52018-09-05 17:03:25 +0100130 }
131
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100132 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
133 if (IsDynamicTensor(outputInfo))
134 {
135 return Fail("%s: Dynamic output tensors are not supported", __func__);
136 }
arovir01b0717b52018-09-05 17:03:25 +0100137
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100138 bool isSupported = false;
139 FORWARD_LAYER_SUPPORT_FUNC(__func__,
140 IsDivisionSupported,
141 data.m_Backends,
142 isSupported,
143 input0.GetTensorInfo(),
144 input1.GetTensorInfo(),
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100145 outputInfo);
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100146 if (!isSupported)
arovir01b0717b52018-09-05 17:03:25 +0100147 {
148 return false;
149 }
150
151 armnn::IConnectableLayer* const startLayer = data.m_Network->AddDivisionLayer();
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100152 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
arovir01b0717b52018-09-05 17:03:25 +0100153
154 const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo();
155 const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo();
156
157 if (endLayer)
158 {
159 BroadcastTensor(input0, input1, startLayer, *data.m_Network);
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100160 return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *endLayer, model, data);
arovir01b0717b52018-09-05 17:03:25 +0100161 }
162
163 return Fail("%s: ProcessActivation failed", __func__);
164}
165
David Beck38e12942018-09-12 16:02:24 +0100166bool HalPolicy::ConvertSub(const Operation& operation, const Model& model, ConversionData& data)
167{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100168 ALOGV("hal_1_1::HalPolicy::ConvertSub()");
Mike Kelly0a879362019-07-29 16:56:31 +0100169 return ::ConvertSub<hal_1_1::HalPolicy>(operation, model, data);
David Beck38e12942018-09-12 16:02:24 +0100170}
171
narpra013c052562018-09-17 14:25:04 +0100172bool HalPolicy::ConvertMean(const Operation& operation, const Model& model, ConversionData& data)
173{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100174 ALOGV("hal_1_1::HalPolicy::ConvertMean()");
175
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100176 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data);
narpra013c052562018-09-17 14:25:04 +0100177 if (!input.IsValid())
178 {
179 return Fail("%s: Operation has invalid inputs", __func__);
180 }
181
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100182 const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model);
183 if (!output)
184 {
185 return Fail("%s: Could not read output 0", __func__);
186 }
187
188 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
189 if (IsDynamicTensor(outputInfo))
190 {
191 return Fail("%s: Dynamic output tensors are not supported", __func__);
192 }
193
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100194 const Operand* axisOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model);
Matteo Martincighae622b72018-10-23 18:25:38 +0100195 if (!axisOperand)
196 {
197 return Fail("%s: Could not read input 1", __func__);
198 }
199
200 std::vector<int32_t> axis;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100201 if (!GetTensorInt32Values<hal_1_1::HalPolicy>(*axisOperand, axis, model, data))
Matteo Martincighae622b72018-10-23 18:25:38 +0100202 {
203 return Fail("%s: Input 1 has invalid values", __func__);
204 }
205
206 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
207
208 // Convert the axis to unsigned int and remove duplicates.
209 unsigned int rank = inputInfo.GetNumDimensions();
210 std::set<unsigned int> uniqueAxis;
211 std::transform(axis.begin(), axis.end(),
212 std::inserter(uniqueAxis, uniqueAxis.begin()),
213 [rank](int i) -> unsigned int { return (i + rank) % rank; });
214
215 // Get the "keep dims" flag.
216 int32_t keepDims = 0;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100217 if (!GetInputInt32<hal_1_1::HalPolicy>(operation, 2, keepDims, model, data))
Matteo Martincighae622b72018-10-23 18:25:38 +0100218 {
219 return Fail("%s: Could not read input 2", __func__);
220 }
narpra013c052562018-09-17 14:25:04 +0100221
222 armnn::MeanDescriptor descriptor;
Matteo Martincighae622b72018-10-23 18:25:38 +0100223 descriptor.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
224 descriptor.m_KeepDims = keepDims > 0;
narpra013c052562018-09-17 14:25:04 +0100225
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100226 bool isSupported = false;
227 FORWARD_LAYER_SUPPORT_FUNC(__func__,
228 IsMeanSupported,
229 data.m_Backends,
230 isSupported,
231 inputInfo,
232 outputInfo,
233 descriptor);
234 if (!isSupported)
narpra013c052562018-09-17 14:25:04 +0100235 {
236 return false;
237 }
238
239 armnn::IConnectableLayer* const layer = data.m_Network->AddMeanLayer(descriptor);
narpra0196bedf02018-09-26 16:57:28 +0100240 assert(layer != nullptr);
241 input.Connect(layer->GetInputSlot(0));
narpra013c052562018-09-17 14:25:04 +0100242
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100243 return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data);
narpra013c052562018-09-17 14:25:04 +0100244}
245
Aron Virginas-Tarc921f6b2019-07-25 10:14:33 +0100246bool HalPolicy::ConvertPad(const Operation& operation, const Model& model, ConversionData& data)
247{
248 ALOGV("hal_1_1::HalPolicy::ConvertPad()");
249 return ::ConvertPad<hal_1_1::HalPolicy>(operation, model, data);
250}
251
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000252bool HalPolicy::ConvertSpaceToBatchNd(const Operation& operation, const Model& model, ConversionData& data)
253{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100254 ALOGV("hal_1_1::HalPolicy::ConvertSpaceToBatchNd()");
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000255
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100256 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000257 if (!input.IsValid())
258 {
259 return Fail("%s: Operation has invalid inputs", __func__);
260 }
261
262 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
263 unsigned int rank = inputInfo.GetNumDimensions();
264 unsigned int spatialDim = rank - 2;
265
266 if (rank != 4)
267 {
268 Fail("%s: Only inputs with rank 4 are supported", __func__);
269 }
270
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100271 const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model);
272 if (!output)
273 {
274 return Fail("%s: Could not read output 0", __func__);
275 }
276
277 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
278 if (IsDynamicTensor(outputInfo))
279 {
280 return Fail("%s: Dynamic output tensors are not supported", __func__);
281 }
282
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100283 const Operand* blockShapeOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model);
284 const Operand* paddingsOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 2, model);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000285
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100286 armnn::TensorShape blockShapeOperandShape = GetTensorShapeForOperand(*blockShapeOperand);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000287 if (blockShapeOperandShape.GetNumDimensions() != 1 || blockShapeOperandShape.GetNumElements() != spatialDim)
288 {
289 return Fail("%s: Operation has invalid block shape operand: expected shape [%d]", __func__, spatialDim);
290 }
291
292 std::vector<int32_t> blockShape;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100293 GetTensorInt32Values<hal_1_1::HalPolicy>(*blockShapeOperand, blockShape, model, data);
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000294 if (std::any_of(blockShape.cbegin(), blockShape.cend(), [](int32_t i){ return i < 1; }))
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000295 {
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000296 return Fail("%s: Block shape must be at least 1 in all dimensions.", __func__);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000297 }
298
299 armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand);
300 if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != 2 * spatialDim)
301 {
302 return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, spatialDim);
303 }
304
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000305 std::vector<std::pair<unsigned int, unsigned int>> paddingList;
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000306 std::vector<int32_t> paddings;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100307 GetTensorInt32Values<hal_1_1::HalPolicy>(*paddingsOperand, paddings, model, data);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000308 for (unsigned int i = 0; i < paddings.size() - 1; i += 2)
309 {
310 int paddingBeforeInput = paddings[i];
311 int paddingAfterInput = paddings[i + 1];
312 if (paddingBeforeInput < 0 || paddingAfterInput < 0)
313 {
314 return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__);
315 }
316
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000317 paddingList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000318 }
319
Sadik Armagan8bef7b32018-12-20 14:14:12 +0000320 armnn::SpaceToBatchNdDescriptor descriptor;
321 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
322 descriptor.m_BlockShape.assign(blockShape.cbegin(), blockShape.cend());
323 descriptor.m_PadList.assign(paddingList.cbegin(), paddingList.cend());
324
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100325 bool isSupported = false;
326 FORWARD_LAYER_SUPPORT_FUNC(__func__,
327 IsSpaceToBatchNdSupported,
328 data.m_Backends,
329 isSupported,
330 inputInfo,
331 outputInfo,
332 descriptor);
333 if (!isSupported)
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000334 {
335 return false;
336 }
337
338 armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToBatchNdLayer(descriptor);
339 assert(layer != nullptr);
340 input.Connect(layer->GetInputSlot(0));
341
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100342 return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data);
Nattapat Chaimanowong81a68342018-11-05 14:04:47 +0000343}
344
saoste01b8471482018-10-10 09:44:51 +0100345bool HalPolicy::ConvertSqueeze(const Operation& operation, const Model& model, ConversionData& data)
346{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100347 ALOGV("hal_1_1::HalPolicy::ConvertSqueeze()");
saoste01b8471482018-10-10 09:44:51 +0100348
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100349 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data);
saoste01b8471482018-10-10 09:44:51 +0100350 if (!input.IsValid())
351 {
352 return Fail("%s: Operation has invalid inputs", __func__);
353 }
354
355 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
saoste01b8471482018-10-10 09:44:51 +0100356 unsigned int rank = inputInfo.GetNumDimensions();
saoste01fe463152018-10-18 17:49:56 +0100357 if (rank > 4)
saoste01b8471482018-10-10 09:44:51 +0100358 {
saoste01fe463152018-10-18 17:49:56 +0100359 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
saoste01b8471482018-10-10 09:44:51 +0100360 }
361
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100362 const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model);
363 if (!output)
364 {
365 return Fail("%s: Could not read output 0", __func__);
366 }
367
368 if (IsDynamicTensor(GetTensorInfoForOperand(*output)))
369 {
370 return Fail("%s: Dynamic output tensors are not supported", __func__);
371 }
372
saoste01b8471482018-10-10 09:44:51 +0100373 // NOTE: Axis is an optional parameter to SQUEEZE, therefore we do not want to generate a failure
374 // if the operand index is out of bounds.
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100375 const Operand* axisOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model, false);
saoste01b8471482018-10-10 09:44:51 +0100376
saoste01fe463152018-10-18 17:49:56 +0100377 const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
378
saoste01b8471482018-10-10 09:44:51 +0100379 std::vector<int32_t> axis;
saoste01fe463152018-10-18 17:49:56 +0100380 if (!axisOperand)
saoste01b8471482018-10-10 09:44:51 +0100381 {
382 axis.assign(dimensionSequence,
saoste01fe463152018-10-18 17:49:56 +0100383 dimensionSequence + rank);
saoste01b8471482018-10-10 09:44:51 +0100384 }
385 else
386 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100387 GetTensorInt32Values<hal_1_1::HalPolicy>(*axisOperand, axis, model, data);
saoste01b8471482018-10-10 09:44:51 +0100388 }
389
saoste01b8471482018-10-10 09:44:51 +0100390
saoste01a893efa2018-10-13 11:56:12 +0100391 std::vector<uint32_t> outputDims;
saoste01fe463152018-10-18 17:49:56 +0100392 for (unsigned int i = 0; i < rank; i++)
saoste01a893efa2018-10-13 11:56:12 +0100393 {
394 bool skipSqueeze = (std::find(axis.begin(), axis.end(), i) == axis.end());
395 auto currentDimension = inputInfo.GetShape()[i];
saoste01b8471482018-10-10 09:44:51 +0100396 if (skipSqueeze || currentDimension != 1)
397 {
398 outputDims.push_back(currentDimension);
399 }
400 }
401
saoste01fe463152018-10-18 17:49:56 +0100402 armnn::TensorShape outShape = armnn::TensorShape(outputDims.size(), outputDims.data());
saoste01b8471482018-10-10 09:44:51 +0100403
404 armnn::TensorInfo outputInfo = inputInfo;
405 outputInfo.SetShape(outShape);
406
407 armnn::ReshapeDescriptor reshapeDesc;
408 reshapeDesc.m_TargetShape = outputInfo.GetShape();
409
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100410 bool isSupported = false;
411 FORWARD_LAYER_SUPPORT_FUNC(__func__,
412 IsReshapeSupported,
413 data.m_Backends,
414 isSupported,
415 inputInfo,
416 reshapeDesc);
417 if (!isSupported)
saoste01b8471482018-10-10 09:44:51 +0100418 {
419 return false;
420 }
421
422 armnn::IConnectableLayer* const layer = data.m_Network->AddReshapeLayer(reshapeDesc);
423 assert(layer != nullptr);
424 input.Connect(layer->GetInputSlot(0));
saoste01fe463152018-10-18 17:49:56 +0100425
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100426 return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data);
saoste01fe463152018-10-18 17:49:56 +0100427}
428
Sadik Armagan758eee82018-11-15 15:34:49 +0000429bool HalPolicy::ConvertStridedSlice(const Operation& operation, const Model& model, ConversionData& data)
430{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100431 ALOGV("hal_1_1::HalPolicy::ConvertStridedSlice()");
432
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100433 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data);
Sadik Armagan758eee82018-11-15 15:34:49 +0000434 if (!input.IsValid())
435 {
436 return Fail("%s: Operation has invalid inputs", __func__);
437 }
Sadik Armagan758eee82018-11-15 15:34:49 +0000438
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100439 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
Sadik Armagan758eee82018-11-15 15:34:49 +0000440 unsigned int rank = inputInfo.GetNumDimensions();
441 if (rank > 4)
442 {
443 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
444 }
445
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +0100446 const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model);
447 if (!output)
448 {
449 return Fail("%s: Could not read output 0", __func__);
450 }
451
452 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
453 if (IsDynamicTensor(outputInfo))
454 {
455 return Fail("%s: Dynamic output tensors are not supported", __func__);
456 }
457
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100458 const Operand* beginOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model);
459 const Operand* endOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 2, model);
460 const Operand* stridesOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 3, model);
Sadik Armagan758eee82018-11-15 15:34:49 +0000461
462 std::vector<int32_t> beginValues;
463 std::vector<int32_t> endValues;
464 std::vector<int32_t> stridesValues;
465
466 // The length of the beginOperand, endOperand and stridesOperand must be of a rank(input)
467 auto ValidateInputOperands = [&] (const Operand& operand, std::vector<int32_t>& operandValues)
468 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100469 if (!GetTensorInt32Values<hal_1_1::HalPolicy>(operand, operandValues, model, data))
Sadik Armagan758eee82018-11-15 15:34:49 +0000470 {
471 return false;
472 }
473
474 if (operandValues.size() != rank)
475 {
476 return false;
477 }
478
479 return true;
480 };
481
482 if (!ValidateInputOperands(*beginOperand, beginValues)
483 || !ValidateInputOperands(*endOperand, endValues)
484 || !ValidateInputOperands(*stridesOperand, stridesValues))
485 {
486 return Fail("%s: Operation has invalid input operand", __func__);
487 }
488
489 // Stride cannot have value '0'
490 if (std::any_of(stridesValues.cbegin(), stridesValues.cend(), [](int32_t i){ return i == 0; }))
491 {
492 return Fail("%s: Stride must be non-zero value.", __func__);
493 }
494
495 armnn::StridedSliceDescriptor descriptor;
496 descriptor.m_Begin.assign(beginValues.cbegin(), beginValues.cend());
497 descriptor.m_End.assign(endValues.cbegin(), endValues.cend());
498 descriptor.m_Stride.assign(stridesValues.cbegin(), stridesValues.cend());
499 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
500
501 // Get the "begin_mask", "end_mask", and "shrink_axis_mask" flags
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100502 if (!GetInputInt32<hal_1_1::HalPolicy>(operation, 4, descriptor.m_BeginMask, model, data) ||
503 !GetInputInt32<hal_1_1::HalPolicy>(operation, 5, descriptor.m_EndMask, model, data) ||
504 !GetInputInt32<hal_1_1::HalPolicy>(operation, 6, descriptor.m_ShrinkAxisMask, model, data))
Sadik Armagan758eee82018-11-15 15:34:49 +0000505 {
506 return Fail("%s: Operation has invalid inputs", __func__);
507 }
508
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100509 bool isSupported = false;
510 FORWARD_LAYER_SUPPORT_FUNC(__func__,
511 IsStridedSliceSupported,
512 data.m_Backends,
513 isSupported,
514 inputInfo,
515 outputInfo,
516 descriptor);
517 if (!isSupported)
Sadik Armagan758eee82018-11-15 15:34:49 +0000518 {
519 return false;
520 }
521
522 armnn::IConnectableLayer* const layer = data.m_Network->AddStridedSliceLayer(descriptor);
523 assert(layer != nullptr);
524 input.Connect(layer->GetInputSlot(0));
525
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100526 return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data);
Sadik Armagan758eee82018-11-15 15:34:49 +0000527}
528
saoste01fe463152018-10-18 17:49:56 +0100529bool HalPolicy::ConvertTranspose(const Operation& operation, const Model& model, ConversionData& data)
530{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100531 ALOGV("hal_1_1::HalPolicy::ConvertTranspose()");
saoste01fe463152018-10-18 17:49:56 +0100532
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100533 LayerInputHandle input = ConvertToLayerInputHandle<hal_1_1::HalPolicy>(operation, 0, model, data);
saoste01fe463152018-10-18 17:49:56 +0100534 if (!input.IsValid())
535 {
536 return Fail("%s: Operation has invalid inputs", __func__);
537 }
538
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100539 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
saoste01fe463152018-10-18 17:49:56 +0100540 unsigned int rank = inputInfo.GetNumDimensions();
541 if (rank > 4)
542 {
543 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
544 }
545
546 // NOTE: Axis is an optional parameter to TRANSPOSE, therefore we do not want to generate a failure
547 // if the operand index is out of bounds.
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100548 const Operand* permOperand = GetInputOperand<hal_1_1::HalPolicy>(operation, 1, model, false);
saoste01fe463152018-10-18 17:49:56 +0100549
550 std::vector<int32_t> perm(rank);
551 if (!permOperand)
552 {
553 // NOTE: If perm is not given, it is set to (n-1...0), where n is the rank of the tensor
554 for (unsigned int i = rank; i > 0; i--)
555 {
556 perm[rank - i] = boost::numeric_cast<int> (i - 1);
557 }
558 }
559 else
560 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100561 GetTensorInt32Values<hal_1_1::HalPolicy>(*permOperand, perm, model, data);
saoste01fe463152018-10-18 17:49:56 +0100562 }
563
564 std::vector<uint32_t> outputDims(perm.begin(), perm.begin() + rank);
565
566 auto permutationVector = armnn::PermutationVector(outputDims.data(), outputDims.size());
567 if (!permutationVector.IsEqual(NHWCToArmNN)
568 && !permutationVector.IsEqual(ArmNNToNHWC)
569 && !permutationVector.IsEqual({ 3, 2, 0, 1 }))
570 {
571 return Fail("%s: Only [0, 3, 1, 2], [0, 2, 3, 1] and [3, 2, 0, 1] permutations are supported.", __func__);
572 }
573
574 armnn::PermuteDescriptor permuteDesc;
575 permuteDesc.m_DimMappings = permutationVector;
576
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100577 const Operand* output = GetOutputOperand<hal_1_1::HalPolicy>(operation, 0, model);
saoste01fe463152018-10-18 17:49:56 +0100578 if (!output)
579 {
580 return Fail("%s: Could not read output 0", __func__);
581 }
582
583 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
584
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100585 bool isSupported = false;
586 FORWARD_LAYER_SUPPORT_FUNC(__func__,
587 IsPermuteSupported,
588 data.m_Backends,
589 isSupported,
590 inputInfo,
591 outputInfo,
592 permuteDesc);
593 if (!isSupported)
saoste01fe463152018-10-18 17:49:56 +0100594 {
595 return false;
596 }
597
598 armnn::IConnectableLayer* const layer = data.m_Network->AddPermuteLayer(permuteDesc);
599 assert(layer != nullptr);
600 input.Connect(layer->GetInputSlot(0));
saoste01b8471482018-10-10 09:44:51 +0100601
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100602 return SetupAndTrackLayerOutputSlot<hal_1_1::HalPolicy>(operation, 0, *layer, model, data);
saoste01b8471482018-10-10 09:44:51 +0100603}
604
Éanna Ó Catháin2cd99b92018-11-14 14:33:52 +0000605bool HalPolicy::ConvertBatchToSpaceNd(const Operation& operation, const Model& model, ConversionData& data)
606{
Aron Virginas-Tar29404fb2019-07-24 13:55:31 +0100607 ALOGV("hal_1_1::HalPolicy::ConvertBatchToSpaceNd()");
Finn Williams23b87b32019-07-30 11:44:05 +0100608 return ::ConvertBatchToSpaceNd<hal_1_1::HalPolicy>(operation, model, data);
Éanna Ó Catháin2cd99b92018-11-14 14:33:52 +0000609}
610
arovir01b0717b52018-09-05 17:03:25 +0100611} // namespace hal_1_1
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100612} // namespace armnn_driver