blob: a284a50a0cfc47de556b07630c7a69bda29e3287 [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#pragma once
7
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +01008#include "Utils.hpp"
9
arovir01b0717b52018-09-05 17:03:25 +010010#include <armnn/ArmNN.hpp>
Ferran Balaguerd30093c2019-07-09 17:04:47 +010011#include <armnn/ILayerSupport.hpp>
12#include <armnn/BackendHelper.hpp>
arovir01b0717b52018-09-05 17:03:25 +010013
Mike Kellyb5fdf382019-06-11 16:35:25 +010014#include "armnn/src/armnnUtils/DataLayoutIndexed.hpp"
arovir01b0717b52018-09-05 17:03:25 +010015#include "armnn/src/armnnUtils/Permute.hpp"
arovir01b0717b52018-09-05 17:03:25 +010016
Mike Kelly46272802019-08-14 17:00:48 +010017#include "1.0/FullyConnected.hpp"
18
arovir01b0717b52018-09-05 17:03:25 +010019#include <ActivationFunctor.h>
20#include <CpuExecutor.h>
21#include <OperationsUtils.h>
22
23#include <boost/assert.hpp>
24#include <boost/core/ignore_unused.hpp>
Aron Virginas-Tar0e7ab542019-04-10 15:02:31 +010025#include <boost/numeric/conversion/cast.hpp>
arovir01b0717b52018-09-05 17:03:25 +010026#include <boost/test/tools/floating_point_comparison.hpp>
27
28#include <log/log.h>
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010029#include <vector>
arovir01b0717b52018-09-05 17:03:25 +010030
31namespace armnn_driver
32{
33
34///
35/// Helper classes
36///
37
38struct ConversionData
39{
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010040 ConversionData(const std::vector<armnn::BackendId>& backends)
41 : m_Backends(backends)
42 , m_Network(nullptr, nullptr)
arovir01b0717b52018-09-05 17:03:25 +010043 {}
44
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010045 const std::vector<armnn::BackendId> m_Backends;
arovir01b0717b52018-09-05 17:03:25 +010046 armnn::INetworkPtr m_Network;
47 std::vector<armnn::IOutputSlot*> m_OutputSlotForOperand;
48 std::vector<android::nn::RunTimePoolInfo> m_MemPools;
49};
50
51class LayerInputHandle
52{
53public:
54 LayerInputHandle();
55 LayerInputHandle(bool valid, armnn::IOutputSlot* outputSlot, armnn::TensorInfo tensorInfo);
56
57 bool IsValid() const;
58
59 void Connect(armnn::IInputSlot& inputSlot);
60
61 const armnn::TensorInfo& GetTensorInfo() const;
62
63private:
64 armnn::IOutputSlot* m_OutputSlot;
65 bool m_Valid;
66 armnn::TensorInfo m_TensorInfo;
67};
68
69class ConstTensorPin
70{
71public:
72 // Creates an invalid tensor pin (can be used to signal errors)
73 // The optional flag can be set to indicate the tensor values were missing, but it was otherwise valid
74 ConstTensorPin(bool optional = false);
75
76 // @param tensorInfo TensorInfo associated with the tensor.
77 // @param valueStart Start address of tensor data. Belongs to one of the memory pools associated with
78 // the model being converted.
79 // @param numBytes Number of bytes for the tensor data.
80 ConstTensorPin(const armnn::TensorInfo& tensorInfo, const void* valueStart, uint32_t numBytes,
81 const armnn::PermutationVector& mappings);
82
83 ConstTensorPin(const ConstTensorPin& other) = delete;
84 ConstTensorPin(ConstTensorPin&& other) = default;
85
86 bool IsValid() const;
87 bool IsOptional() const;
88
89 const armnn::ConstTensor& GetConstTensor() const;
90 const armnn::ConstTensor* GetConstTensorPtr() const;
91
92private:
93 armnn::ConstTensor m_ConstTensor;
94
95 // Owned memory for swizzled tensor data, only required if the tensor needed
96 // swizzling. Otherwise, @ref m_ConstTensor will reference memory from one of
97 // the pools associated with the model being converted.
98 std::vector<uint8_t> m_SwizzledTensorData;
99
100 // optional flag to indicate that an invalid tensor pin is not an error, but the optional values were not given
101 bool m_Optional;
102};
103
104} // namespace armnn_driver
105
106///
107/// Utility functions
108///
109
110namespace
111{
112
113using namespace armnn_driver;
114using namespace android::nn;
115
116// Convenience function to log the reason for failing to convert a model.
117// @return Always returns false (so that it can be used by callers as a quick way to signal an error and return)
118template<class... Args>
119static bool Fail(const char* formatStr, Args&&... args)
120{
121 ALOGD(formatStr, std::forward<Args>(args)...);
122 return false;
123}
124
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100125// Convenience macro to call an Is*Supported function and log caller name together with reason for lack of support.
126// Called as: FORWARD_LAYER_SUPPORT_FUNC(__func__, Is*Supported, backends, a, b, c, d, e)
127#define FORWARD_LAYER_SUPPORT_FUNC(funcName, func, backends, supported, ...) \
Teresa Charlin8f6429d2019-10-01 13:10:15 +0100128try \
129{ \
130 for (auto&& backendId : backends) \
131 { \
132 auto layerSupportObject = armnn::GetILayerSupportByBackendId(backendId); \
133 if (layerSupportObject) \
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100134 { \
Teresa Charlin8f6429d2019-10-01 13:10:15 +0100135 std::string reasonIfUnsupported; \
136 supported = \
137 layerSupportObject->func(__VA_ARGS__, armnn::Optional<std::string&>(reasonIfUnsupported)); \
138 if (supported) \
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100139 { \
Teresa Charlin8f6429d2019-10-01 13:10:15 +0100140 break; \
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100141 } \
142 else \
143 { \
Teresa Charlin8f6429d2019-10-01 13:10:15 +0100144 if (reasonIfUnsupported.size() > 0) \
145 { \
146 ALOGD("%s: not supported by armnn: %s", funcName, reasonIfUnsupported.c_str()); \
147 } \
148 else \
149 { \
150 ALOGD("%s: not supported by armnn", funcName); \
151 } \
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100152 } \
153 } \
Teresa Charlin8f6429d2019-10-01 13:10:15 +0100154 else \
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100155 { \
Teresa Charlin8f6429d2019-10-01 13:10:15 +0100156 ALOGD("%s: backend not registered: %s", funcName, backendId.Get().c_str()); \
Ferran Balaguerd30093c2019-07-09 17:04:47 +0100157 } \
Teresa Charlin8f6429d2019-10-01 13:10:15 +0100158 } \
159 if (!supported) \
160 { \
161 ALOGD("%s: not supported by any specified backend", funcName); \
162 } \
163} \
164catch (const armnn::InvalidArgumentException &e) \
165{ \
166 throw armnn::InvalidArgumentException(e, "Failed to check layer support", CHECK_LOCATION()); \
167}
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100168
Mike Kellyb5fdf382019-06-11 16:35:25 +0100169template<typename Operand>
170armnn::TensorShape GetTensorShapeForOperand(const Operand& operand)
arovir01b0717b52018-09-05 17:03:25 +0100171{
172 return armnn::TensorShape(operand.dimensions.size(), operand.dimensions.data());
173}
174
Matthew Bentham912b3622019-05-03 15:49:14 +0100175inline bool IsOperandTypeSupportedForTensors(V1_0::OperandType type)
arovir01b0717b52018-09-05 17:03:25 +0100176{
Matthew Bentham912b3622019-05-03 15:49:14 +0100177 return type == V1_0::OperandType::TENSOR_FLOAT32 ||
178 type == V1_0::OperandType::TENSOR_QUANT8_ASYMM ||
179 type == V1_0::OperandType::TENSOR_INT32;
arovir01b0717b52018-09-05 17:03:25 +0100180}
181
Mike Kellyb5fdf382019-06-11 16:35:25 +0100182#ifdef ARMNN_ANDROID_NN_V1_2
183
184inline bool IsOperandTypeSupportedForTensors(V1_2::OperandType type)
185{
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000186 return type == V1_2::OperandType::BOOL ||
187 type == V1_2::OperandType::TENSOR_FLOAT16 ||
188 type == V1_2::OperandType::TENSOR_FLOAT32 ||
189 type == V1_2::OperandType::TENSOR_QUANT8_ASYMM ||
190 type == V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL ||
191 type == V1_2::OperandType::TENSOR_QUANT16_SYMM ||
Mike Kellyb5fdf382019-06-11 16:35:25 +0100192 type == V1_2::OperandType::TENSOR_INT32;
193}
194
195#endif
196
197inline bool IsBool(V1_0::Operand)
198{
199 return false;
200}
201
Sadik Armagan61113162019-07-25 09:09:40 +0100202inline bool Is12Operand(V1_0::Operand)
203{
204 return false;
205}
206
Mike Kellyb5fdf382019-06-11 16:35:25 +0100207#ifdef ARMNN_ANDROID_NN_V1_2
208
209inline bool IsBool(V1_2::Operand operand)
210{
211 return operand.type == V1_2::OperandType::BOOL;
212}
213
Sadik Armagan61113162019-07-25 09:09:40 +0100214/// Checks if a operand is 1_2 Operand
215inline bool Is12Operand(V1_2::Operand)
216{
217 return true;
218}
219
Mike Kellyb5fdf382019-06-11 16:35:25 +0100220#endif
221
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100222template<typename LayerHandleType>
223armnn::IConnectableLayer& AddReshapeLayer(armnn::INetwork& network, LayerHandleType& inputLayer,
224 armnn::TensorInfo reshapeInfo)
225{
226 armnn::ReshapeDescriptor reshapeDescriptor;
227 reshapeDescriptor.m_TargetShape = reshapeInfo.GetShape();
228
229 armnn::IConnectableLayer* reshapeLayer = network.AddReshapeLayer(reshapeDescriptor);
230 BOOST_ASSERT(reshapeLayer != nullptr);
231
232 // Attach the input layer to the reshape layer
233 inputLayer.Connect(reshapeLayer->GetInputSlot(0));
234 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapeInfo);
235
236 return *reshapeLayer;
237}
238
Sadik Armagan64b19b52019-08-19 09:49:58 +0100239bool BroadcastTensor(LayerInputHandle& input0, LayerInputHandle& input1,
240 armnn::IConnectableLayer* startLayer, ConversionData& data)
arovir01b0717b52018-09-05 17:03:25 +0100241{
242 BOOST_ASSERT(startLayer != nullptr);
arovir01b0717b52018-09-05 17:03:25 +0100243
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100244 const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo();
245 const armnn::TensorInfo& inputInfo1 = input1.GetTensorInfo();
246
247 unsigned int inputDimensions0 = inputInfo0.GetNumDimensions();
248 unsigned int inputDimensions1 = inputInfo1.GetNumDimensions();
249
250 if (inputDimensions0 == inputDimensions1)
arovir01b0717b52018-09-05 17:03:25 +0100251 {
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100252 // The inputs have the same number of dimensions, simply connect them to the given layer as they are
253 input0.Connect(startLayer->GetInputSlot(0));
254 input1.Connect(startLayer->GetInputSlot(1));
255
Sadik Armagan64b19b52019-08-19 09:49:58 +0100256 return true;
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100257 }
258
259 // Since the number of dimensions do not match then we need to add degenerate dimensions
260 // to the "smaller" tensor using a reshape, while keeping the order of the inputs.
261
262 unsigned int maxInputDimensions = std::max(inputDimensions0, inputDimensions1);
263 unsigned int sizeDifference = std::abs(boost::numeric_cast<int>(inputDimensions0) -
264 boost::numeric_cast<int>(inputDimensions1));
265
266 bool input0IsSmaller = inputDimensions0 < inputDimensions1;
267 LayerInputHandle& smallInputHandle = input0IsSmaller ? input0 : input1;
268 const armnn::TensorInfo& smallInfo = smallInputHandle.GetTensorInfo();
269
270 const armnn::TensorShape& smallShape = smallInfo.GetShape();
271 std::vector<unsigned int> reshapedDimensions(maxInputDimensions, 1);
272 for (unsigned int i = sizeDifference; i < maxInputDimensions; i++)
273 {
274 reshapedDimensions[i] = smallShape[i - sizeDifference];
275 }
276
277 armnn::TensorInfo reshapedInfo = smallInfo;
278 reshapedInfo.SetShape(armnn::TensorShape{ boost::numeric_cast<unsigned int>(reshapedDimensions.size()),
279 reshapedDimensions.data() });
Sadik Armagan64b19b52019-08-19 09:49:58 +0100280
281 // RehsapeDescriptor that is ignored in the IsReshapeSupported function
282 armnn::ReshapeDescriptor reshapeDescriptor;
283
284 bool isSupported = false;
285 FORWARD_LAYER_SUPPORT_FUNC(__func__,
286 IsReshapeSupported,
287 data.m_Backends,
288 isSupported,
289 reshapedInfo,
290 reshapeDescriptor);
291 if (!isSupported)
292 {
293 return false;
294 }
295
296 BOOST_ASSERT(data.m_Network != nullptr);
297 armnn::IConnectableLayer& reshapeLayer = AddReshapeLayer(*data.m_Network, smallInputHandle, reshapedInfo);
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100298
299 if (input0IsSmaller)
300 {
301 // Input0 is the "smaller" tensor, connect the reshape layer as follows:
302 //
303 // Input0 Input1
arovir01b0717b52018-09-05 17:03:25 +0100304 // | |
305 // Reshape |
306 // \ /
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100307 // StartLayer
arovir01b0717b52018-09-05 17:03:25 +0100308
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100309 reshapeLayer.GetOutputSlot(0).Connect(startLayer->GetInputSlot(0));
310 input1.Connect(startLayer->GetInputSlot(1));
arovir01b0717b52018-09-05 17:03:25 +0100311 }
312 else
313 {
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100314 // Input1 is the "smaller" tensor, connect the reshape layer as follows:
315 //
316 // Input0 Input1
317 // | |
318 // | Reshape
319 // \ /
320 // StartLayer
321
arovir01b0717b52018-09-05 17:03:25 +0100322 input0.Connect(startLayer->GetInputSlot(0));
Matteo Martincigh0bd89a82019-07-02 16:53:10 +0100323 reshapeLayer.GetOutputSlot(0).Connect(startLayer->GetInputSlot(1));
arovir01b0717b52018-09-05 17:03:25 +0100324 }
Sadik Armagan64b19b52019-08-19 09:49:58 +0100325
326 return true;
arovir01b0717b52018-09-05 17:03:25 +0100327}
328
329void CalcPadding(uint32_t input, uint32_t kernel, uint32_t stride, uint32_t& outPadHead, uint32_t& outPadTail,
330 android::nn::PaddingScheme scheme)
331{
332 int32_t padHead;
333 int32_t padTail;
334 calculateExplicitPadding(input, stride, kernel, scheme, &padHead, &padTail);
335 outPadHead = boost::numeric_cast<uint32_t>(padHead);
336 outPadTail = boost::numeric_cast<uint32_t>(padTail);
337}
338
Mike Kelly86b36d42019-07-12 16:39:33 +0100339#ifdef ARMNN_ANDROID_NN_V1_2
340
341void CalcPadding(uint32_t input, uint32_t kernel, uint32_t stride, uint32_t dilation, uint32_t& outPadHead,
342 uint32_t& outPadTail, android::nn::PaddingScheme scheme)
343{
344 int32_t padHead;
345 int32_t padTail;
346 calculateExplicitPadding(input, stride, dilation, kernel, scheme, &padHead, &padTail);
347 outPadHead = boost::numeric_cast<uint32_t>(padHead);
348 outPadTail = boost::numeric_cast<uint32_t>(padTail);
349}
350
Narumol Prangnawaratc8bdb392019-08-01 15:51:44 +0100351void CalcPaddingTransposeConv(uint32_t output, uint32_t kernel, uint32_t stride, int32_t& outPadHead,
352 int32_t& outPadTail, android::nn::PaddingScheme scheme)
353{
354 calculateExplicitPaddingTransposeConv(output, stride, kernel, scheme, &outPadHead, &outPadTail);
355}
356
Mike Kelly86b36d42019-07-12 16:39:33 +0100357#endif
358
Matthew Bentham912b3622019-05-03 15:49:14 +0100359Shape GetOperandShape(const V1_0::Operand& operand)
arovir01b0717b52018-09-05 17:03:25 +0100360{
361 Shape shape;
Matthew Bentham912b3622019-05-03 15:49:14 +0100362 shape.type = OperandType(operand.type);
arovir01b0717b52018-09-05 17:03:25 +0100363 shape.dimensions = operand.dimensions;
364 shape.scale = operand.scale;
365 shape.offset = operand.zeroPoint;
366 return shape;
367}
368
Mike Kelly46272802019-08-14 17:00:48 +0100369#ifdef ARMNN_ANDROID_NN_V1_2
370
371Shape GetOperandShape(const V1_2::Operand& operand)
372{
373 Shape shape;
374 shape.type = OperandType(operand.type);
375 shape.dimensions = operand.dimensions;
376 shape.scale = operand.scale;
377 shape.offset = operand.zeroPoint;
378 return shape;
379}
380
381#endif
382
arovir01b0717b52018-09-05 17:03:25 +0100383// ArmNN requires the bias scale to be equal to the product of the weight and input scales, which is also
384// what AndroidNN requires. However for some of the AndroidNN tests the values don't exactly match so
Aron Virginas-Tara0baa172019-08-01 11:24:08 +0100385// we accept some tolerance. We don't want ArmNN itself to accept these inconsistencies as it is up to the
386// user (us, in this case) to ensure they match.
arovir01b0717b52018-09-05 17:03:25 +0100387void SanitizeBiasQuantizationScale(armnn::TensorInfo& biasInfo,
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000388 const armnn::TensorInfo& weightInfo,
389 const armnn::TensorInfo& inputInfo)
arovir01b0717b52018-09-05 17:03:25 +0100390{
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000391 if (weightInfo.HasPerAxisQuantization())
arovir01b0717b52018-09-05 17:03:25 +0100392 {
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000393 // NOTE: Bias scale is always set to 0 for per-axis quantization and
394 // it needs to be calculated: scale[i] = input_scale * weight_scale[i]
395 auto UpdateBiasScaleValue = [&inputInfo](float biasScale) -> float
arovir01b0717b52018-09-05 17:03:25 +0100396 {
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000397 return biasScale * inputInfo.GetQuantizationScale();
398 };
399
400 std::vector<float> biasScales(weightInfo.GetQuantizationScales());
401 std::transform(biasScales.begin(), biasScales.end(), biasScales.begin(), UpdateBiasScaleValue);
402
403 biasInfo.SetQuantizationScales(biasScales);
404 biasInfo.SetQuantizationDim(weightInfo.GetQuantizationDim());
405
406 ALOGV("Bias quantization params have been updated for per-axis quantization");
407 }
408 else
409 {
410 const float expectedBiasScale = weightInfo.GetQuantizationScale() * inputInfo.GetQuantizationScale();
411 if (biasInfo.GetQuantizationScale() != expectedBiasScale)
412 {
413 boost::math::fpc::close_at_tolerance<float> comparer(boost::math::fpc::percent_tolerance(1.0f));
414 if (comparer(biasInfo.GetQuantizationScale(), expectedBiasScale))
415 {
416 ALOGW("Bias quantization scale has been modified to match input * weights");
417 biasInfo.SetQuantizationScale(expectedBiasScale);
418 }
arovir01b0717b52018-09-05 17:03:25 +0100419 }
420 }
421}
422
423// 4D Tensor Permutations
424const armnn::PermutationVector IdentityPermutation4D({ 0U, 1U, 2U, 3U });
425const armnn::PermutationVector NHWCToArmNN({ 0U, 2U, 3U, 1U });
426const armnn::PermutationVector ArmNNToNHWC({ 0U, 3U, 1U, 2U });
427const armnn::PermutationVector SwapDim1And2({ 0U, 2U, 1U, 3U });
428
429// 3D Permutation Vectors
430const armnn::PermutationVector IdentityPermutation3D({ 0U, 1U, 2U });
431const armnn::PermutationVector RotateTensorLeft({ 2U, 0U, 1U });
432const armnn::PermutationVector RotateTensorRight({ 1U, 2U, 0U });
433
434template<typename OSlot>
435armnn::IConnectableLayer& AddPermuteLayer(armnn::INetwork& network, OSlot& input,
436 const armnn::PermutationVector& mappings)
437{
438 // Add swizzle layer
439 armnn::IConnectableLayer* const layer = network.AddPermuteLayer(mappings);
440
441 BOOST_ASSERT(layer != nullptr);
442
443 // Connect input to swizzle layer
444 input.Connect(layer->GetInputSlot(0));
445
446 // Setup swizzled output
447 const armnn::TensorInfo outInfo = armnnUtils::Permuted(input.GetTensorInfo(), mappings);
448 layer->GetOutputSlot(0).SetTensorInfo(outInfo);
449
450 return *layer;
451}
452
453void SwizzleIn(armnn::INetwork& network, LayerInputHandle& input, armnn::IConnectableLayer& layer, unsigned int index)
454{
455 // Add swizzle layer
456 armnn::IConnectableLayer& swizzleLayer = AddPermuteLayer(network, input, NHWCToArmNN);
457 // Connect swizzled input to layer
458 swizzleLayer.GetOutputSlot(0).Connect(layer.GetInputSlot(index));
459}
460
461armnn::IConnectableLayer& DeswizzleOut(armnn::INetwork& network, armnn::IConnectableLayer& layer, unsigned int index)
462{
463 // Add deswizzle layer
464 armnn::IConnectableLayer& deswizzleLayer = AddPermuteLayer(network, layer.GetOutputSlot(index), ArmNNToNHWC);
465 return deswizzleLayer;
466}
467
468// only suitable for input/output slot index 0, for other slots, use SwizzleIn and DeswizzleOut directly
469armnn::IConnectableLayer& SwizzleInDeswizzleOut(armnn::INetwork& network,
470 LayerInputHandle& input,
471 armnn::IConnectableLayer& firstLayer,
472 armnn::IConnectableLayer& lastLayer)
473{
474 SwizzleIn(network, input, firstLayer, 0);
475 return DeswizzleOut(network, lastLayer, 0);
476}
477
478// only suitable for input/output slot index 0, for other slots, use SwizzleIn and DeswizzleOut directly
479armnn::IConnectableLayer& SwizzleInDeswizzleOut(armnn::INetwork& network, LayerInputHandle& input,
480 armnn::IConnectableLayer& layer)
481{
482 return SwizzleInDeswizzleOut(network, input, layer, layer);
483}
484
485bool ValidateConcatOutputShape(const std::vector<armnn::TensorShape> & inputShapes,
486 const armnn::TensorShape & outputShape,
487 uint32_t concatDim)
488{
489 // Validate the output shape is correct given the input shapes (which have just been validated)
490 unsigned int numDimensions = inputShapes[0].GetNumDimensions();
491 if (outputShape.GetNumDimensions() != numDimensions)
492 {
493 return Fail("%s: Output shape has wrong number of dimensions", __func__);
494 }
495
496 unsigned int outputSizeAlongConcatenatedDimension = 0;
497 for (unsigned int i = 0; i < inputShapes.size(); i++)
498 {
499 outputSizeAlongConcatenatedDimension += inputShapes[i][concatDim];
500 }
501
502 for (unsigned int i = 0; i < numDimensions; ++i)
503 {
504 if (i == concatDim)
505 {
506 if (outputShape[i] != outputSizeAlongConcatenatedDimension)
507 {
508 return Fail(
509 "%s: Invalid output shape for dimension %d (%d != %d)",
510 __func__,
511 i,
512 outputShape[i],
513 outputSizeAlongConcatenatedDimension);
514 }
515 }
516 else
517 {
518 if (outputShape[i] != inputShapes[0][i])
519 {
520 return Fail("%s: Invalid output shape", __func__);
521 }
522 }
523 }
524
525 return true;
526}
527
528bool RequiresReshape(armnn::TensorShape & inputShape)
529{
530 return inputShape.GetNumDimensions() < 3;
531}
532
arovir01b0717b52018-09-05 17:03:25 +0100533void SwizzleInputs(armnn::INetwork& network,
534 std::vector<LayerInputHandle>& inputs,
535 std::vector<armnn::TensorShape>& inputShapes,
536 const armnn::PermutationVector& mapping)
537{
538 if (!mapping.IsEqual(IdentityPermutation4D))
539 {
540 size_t nInputs = inputs.size();
541 for (size_t i=0; i<nInputs; ++i)
542 {
543 // add swizzle layer
544 armnn::IConnectableLayer& swizzleLayer = AddPermuteLayer(network, inputs[i], mapping);
545 auto& outputSlot = swizzleLayer.GetOutputSlot(0);
546 auto& outputInfo = outputSlot.GetTensorInfo();
547 // replace inputs with the swizzled ones
548 inputs[i] = LayerInputHandle(true, &outputSlot, outputInfo);
549 inputShapes[i] = inputs[i].GetTensorInfo().GetShape();
550 }
551 }
552}
553
narpra01f176d5a2018-11-18 20:17:48 +0000554bool CreateConcatPermutationParameters(const unsigned int numberOfDimensions,
555 int32_t & concatDimension,
556 std::pair<armnn::PermutationVector, armnn::PermutationVector> & permutationPair)
arovir01b0717b52018-09-05 17:03:25 +0100557{
narpra01f176d5a2018-11-18 20:17:48 +0000558 bool needPermute = false;
arovir01b0717b52018-09-05 17:03:25 +0100559 BOOST_ASSERT(numberOfDimensions >= 3);
560
561 // ArmNN uses Compute Library subtensors to perform concatenation
narpra01f176d5a2018-11-18 20:17:48 +0000562 // This only works when concatenating along dimension 0, 1 or 3 for a 4-D tensor,
563 // or along dimension 0 or 2 for a 3-D tensor.
564 if (numberOfDimensions == 4 && concatDimension == 2)
arovir01b0717b52018-09-05 17:03:25 +0100565 {
narpra01f176d5a2018-11-18 20:17:48 +0000566 concatDimension = 1;
567 permutationPair = std::make_pair(SwapDim1And2, SwapDim1And2);
568 needPermute = true;
arovir01b0717b52018-09-05 17:03:25 +0100569 }
narpra01f176d5a2018-11-18 20:17:48 +0000570 else if (numberOfDimensions == 3 && concatDimension == 1)
arovir01b0717b52018-09-05 17:03:25 +0100571 {
narpra01f176d5a2018-11-18 20:17:48 +0000572 concatDimension = 0;
573 permutationPair = std::make_pair(RotateTensorLeft, RotateTensorRight);
574 needPermute = true;
arovir01b0717b52018-09-05 17:03:25 +0100575 }
narpra01f176d5a2018-11-18 20:17:48 +0000576 return needPermute;
arovir01b0717b52018-09-05 17:03:25 +0100577}
578
579} // anonymous namespace
580
581namespace armnn_driver
582{
583
584//// Creates an ArmNN activation layer and connects it to the given layer, if the
585//// passed in AndroidNN activation function requires so.
586//// @return The end layer of the sequence of layers built for the given AndroidNN
587//// activation function or nullptr if an error occurred (e.g. unsupported activation).
588//// Note that the end layer matches the input layer if no activation is required
589//// (the sequence of layers has length 1).
590armnn::IConnectableLayer* ProcessActivation(const armnn::TensorInfo& tensorInfo,
591 ActivationFn activation,
592 armnn::IConnectableLayer* prevLayer,
593 ConversionData& data);
594
595} // namespace armnn_driver
596
597///
598/// Utility templates
599///
600
601namespace armnn_driver
602{
603
604using namespace android::nn;
605
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100606template<typename HalPolicy,
607 typename HalOperand = typename HalPolicy::Operand,
608 typename HalOperation = typename HalPolicy::Operation,
609 typename HalModel = typename HalPolicy::Model>
610const HalOperand* GetInputOperand(const HalOperation& operation,
611 uint32_t inputIndex,
612 const HalModel& model,
Mike Kellyb5fdf382019-06-11 16:35:25 +0100613 bool failOnIndexOutOfBounds = true)
arovir01b0717b52018-09-05 17:03:25 +0100614{
615 if (inputIndex >= operation.inputs.size())
616 {
saoste01b8471482018-10-10 09:44:51 +0100617 if (failOnIndexOutOfBounds)
618 {
619 Fail("%s: invalid input index: %i out of %i", __func__, inputIndex, operation.inputs.size());
620 }
arovir01b0717b52018-09-05 17:03:25 +0100621 return nullptr;
622 }
623
624 BOOST_ASSERT(operation.inputs[inputIndex] < model.operands.size()); // Model should have been validated beforehand
625 return &model.operands[operation.inputs[inputIndex]];
626}
627
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100628template<typename HalPolicy,
629 typename HalOperand = typename HalPolicy::Operand,
630 typename HalOperation = typename HalPolicy::Operation,
631 typename HalModel = typename HalPolicy::Model>
632const HalOperand* GetOutputOperand(const HalOperation& operation,
633 uint32_t outputIndex,
634 const HalModel& model)
arovir01b0717b52018-09-05 17:03:25 +0100635{
636 if (outputIndex >= operation.outputs.size())
637 {
638 Fail("%s: invalid output index: %i out of %i", __func__, outputIndex, operation.outputs.size());
639 return nullptr;
640 }
641
642 // Model should have been validated beforehand
643 BOOST_ASSERT(operation.outputs[outputIndex] < model.operands.size());
644
645 return &model.operands[operation.outputs[outputIndex]];
646}
647
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100648template<typename HalPolicy,
Pablo Tellofb45e2f2019-10-18 16:51:57 +0100649 typename HalOperand = typename HalPolicy::Operand,
650 typename HalModel = typename HalPolicy::Model>
Mike Kellyb5fdf382019-06-11 16:35:25 +0100651const void* GetOperandValueReadOnlyAddress(const HalOperand& operand,
Matthew Bentham912b3622019-05-03 15:49:14 +0100652 const HalModel& model,
653 const ConversionData& data,
Kevin Mayf29a2c52019-03-14 11:56:32 +0000654 bool optional = false)
arovir01b0717b52018-09-05 17:03:25 +0100655{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100656 using HalOperandLifeTime = typename HalPolicy::OperandLifeTime;
arovir01b0717b52018-09-05 17:03:25 +0100657
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100658 const void* valueStart = nullptr;
arovir01b0717b52018-09-05 17:03:25 +0100659 switch (operand.lifetime)
660 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100661 case HalOperandLifeTime::CONSTANT_COPY:
arovir01b0717b52018-09-05 17:03:25 +0100662 {
663 // Constant found in model.operandValues
664 valueStart = &model.operandValues[operand.location.offset];
665 break;
666 }
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100667 case HalOperandLifeTime::CONSTANT_REFERENCE:
arovir01b0717b52018-09-05 17:03:25 +0100668 {
669 // Constant specified via a Memory object
670 valueStart = GetMemoryFromPool(operand.location, data.m_MemPools);
671 break;
672 }
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100673 case HalOperandLifeTime::NO_VALUE:
Kevin Mayf29a2c52019-03-14 11:56:32 +0000674 {
675 // An optional input tensor with no values is not an error so should not register as a fail
676 if (optional)
677 {
678 valueStart = nullptr;
679 break;
680 }
Matthew Bentham912b3622019-05-03 15:49:14 +0100681 [[fallthrough]];
Kevin Mayf29a2c52019-03-14 11:56:32 +0000682 }
arovir01b0717b52018-09-05 17:03:25 +0100683 default:
684 {
685 // Unsupported/invalid (e.g. can't get value of an input to the model)
686 Fail("%s: unsupported/invalid operand lifetime: %s",
687 __func__, toString(operand.lifetime).c_str());
688 valueStart = nullptr;
689 }
690 }
691
692 return valueStart;
693}
694
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100695template<typename HalPolicy,
Aron Virginas-Tar7a6d11b2019-07-03 15:27:08 +0100696 typename HalOperation = typename HalPolicy::Operation,
697 typename HalModel = typename HalPolicy::Model,
698 typename HalOperandType = typename HalPolicy::OperandType>
699bool GetOperandType(const HalOperation& operation,
700 uint32_t inputIndex,
701 const HalModel& model,
702 HalOperandType& type)
703{
704 using HalOperand = typename HalPolicy::Operand;
705
706 const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model);
707 if (!operand)
708 {
709 return Fail("%s: invalid input operand at index %i", __func__, inputIndex);
710 }
711
712 type = operand->type;
713 return true;
714}
715
716template<typename HalPolicy,
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +0000717 typename HalOperand = typename HalPolicy::Operand>
718bool IsOperandConstant(const HalOperand& operand)
719{
720 using HalOperandLifeTime = typename HalPolicy::OperandLifeTime;
721
722 HalOperandLifeTime lifetime = operand.lifetime;
723
724 return lifetime == HalOperandLifeTime::CONSTANT_COPY ||
725 lifetime == HalOperandLifeTime::CONSTANT_REFERENCE ||
726 lifetime == HalOperandLifeTime::NO_VALUE;
727}
728
729template<typename HalPolicy,
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100730 typename HalOperand = typename HalPolicy::Operand,
731 typename HalModel = typename HalPolicy::Model>
732ConstTensorPin ConvertOperandToConstTensorPin(const HalOperand& operand,
733 const HalModel& model,
734 const ConversionData& data,
735 const armnn::PermutationVector& dimensionMappings = g_DontPermute,
736 const armnn::TensorShape* overrideTensorShape = nullptr,
737 bool optional = false)
738{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100739 if (!IsOperandTypeSupportedForTensors(operand.type))
740 {
741 Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand.type).c_str());
742 return ConstTensorPin();
743 }
744
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +0000745 if (!optional && !IsOperandConstant<HalPolicy>(operand))
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100746 {
747 Fail("%s: invalid operand lifetime: %s", __func__, toString(operand.lifetime).c_str());
748 return ConstTensorPin();
749 }
750
751 const void* const valueStart = GetOperandValueReadOnlyAddress<HalPolicy>(operand, model, data, optional);
752 if (!valueStart)
753 {
754 if (optional)
755 {
756 // optional tensor with no values is not really an error; return it as invalid, but marked as optional
757 return ConstTensorPin(true);
758 }
759 // mandatory tensor with no values
760 Fail("%s: failed to get operand address", __func__);
761 return ConstTensorPin();
762 }
763
764 armnn::TensorInfo tensorInfo = GetTensorInfoForOperand(operand);
Teresa Charlin02dce092019-11-11 17:06:23 +0000765 // Android datalayout might be different than armnn datalayout, e.g. the kernel for the depthwise convolution.
766 if (tensorInfo.HasPerAxisQuantization())
767 {
768 tensorInfo.SetQuantizationDim(dimensionMappings[tensorInfo.GetQuantizationDim().value()]);
769 }
770
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100771 if (overrideTensorShape != nullptr)
772 {
773 tensorInfo.SetShape(*overrideTensorShape);
774 }
775 return ConstTensorPin(tensorInfo, valueStart, operand.location.length, dimensionMappings);
776}
777
778template<typename HalPolicy,
779 typename HalOperation = typename HalPolicy::Operation,
780 typename HalModel = typename HalPolicy::Model>
781ConstTensorPin ConvertOperationInputToConstTensorPin(const HalOperation& operation,
782 uint32_t inputIndex,
783 const HalModel& model,
784 const ConversionData& data,
785 const armnn::PermutationVector& dimensionMappings = g_DontPermute,
786 const armnn::TensorShape* overrideTensorShape = nullptr,
787 bool optional = false)
788{
789 using HalOperand = typename HalPolicy::Operand;
790
791 const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model);
792 if (!operand)
793 {
794 Fail("%s: failed to get input operand: index=%u", __func__, inputIndex);
795 return ConstTensorPin();
796 }
797 return ConvertOperandToConstTensorPin<HalPolicy>(*operand,
798 model,
799 data,
800 dimensionMappings,
801 overrideTensorShape,
802 optional);
803}
804
805template<typename HalPolicy,
806 typename OutputType,
807 typename HalOperandType = typename HalPolicy::OperandType,
808 typename HalOperation = typename HalPolicy::Operation,
809 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +0100810bool GetInputScalar(const HalOperation& operation,
811 uint32_t inputIndex,
Mike Kellyb5fdf382019-06-11 16:35:25 +0100812 HalOperandType type,
arovir01b0717b52018-09-05 17:03:25 +0100813 OutputType& outValue,
814 const HalModel& model,
815 const ConversionData& data)
816{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100817 using HalOperand = typename HalPolicy::Operand;
818
819 const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model);
arovir01b0717b52018-09-05 17:03:25 +0100820 if (!operand)
821 {
822 return Fail("%s: invalid input operand at index %i", __func__, inputIndex);
823 }
824
825 if (operand->type != type)
826 {
827 return Fail("%s: unexpected operand type: %s (should be %s)",
828 __func__, toString(operand->type).c_str(), toString(type).c_str());
829 }
830
831 if (operand->location.length != sizeof(OutputType))
832 {
833 return Fail("%s: incorrect operand location length: %i (should be %i)",
834 __func__, operand->location.length, sizeof(OutputType));
835 }
836
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100837 const void* valueAddress = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data);
arovir01b0717b52018-09-05 17:03:25 +0100838 if (!valueAddress)
839 {
840 return Fail("%s: failed to get address for operand", __func__);
841 }
842
843 outValue = *(static_cast<const OutputType*>(valueAddress));
844 return true;
845}
846
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100847template<typename HalPolicy,
848 typename HalOperation = typename HalPolicy::Operation,
849 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +0100850bool GetInputInt32(const HalOperation& operation,
851 uint32_t inputIndex,
852 int32_t& outValue,
853 const HalModel& model,
854 const ConversionData& data)
855{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100856 return GetInputScalar<HalPolicy>(operation, inputIndex, HalPolicy::OperandType::INT32, outValue, model, data);
arovir01b0717b52018-09-05 17:03:25 +0100857}
858
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100859template<typename HalPolicy,
860 typename HalOperation = typename HalPolicy::Operation,
861 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +0100862bool GetInputFloat32(const HalOperation& operation,
863 uint32_t inputIndex,
864 float& outValue,
865 const HalModel& model,
866 const ConversionData& data)
867{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100868 return GetInputScalar<HalPolicy>(operation, inputIndex, HalPolicy::OperandType::FLOAT32, outValue, model, data);
arovir01b0717b52018-09-05 17:03:25 +0100869}
870
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100871template<typename HalPolicy,
872 typename HalOperation = typename HalPolicy::Operation,
873 typename HalOperandType = typename HalPolicy::OperandType,
874 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +0100875bool GetInputActivationFunctionImpl(const HalOperation& operation,
876 uint32_t inputIndex,
Mike Kellyb5fdf382019-06-11 16:35:25 +0100877 HalOperandType type,
arovir01b0717b52018-09-05 17:03:25 +0100878 ActivationFn& outActivationFunction,
879 const HalModel& model,
880 const ConversionData& data)
881{
Mike Kellyb5fdf382019-06-11 16:35:25 +0100882 if (type != HalOperandType::INT32 && type != HalOperandType::TENSOR_INT32)
arovir01b0717b52018-09-05 17:03:25 +0100883 {
884 return Fail("%s: unexpected operand type: %s (should be %s or %s)",
885 __func__,
886 toString(type).c_str(),
887 toString(OperandType::INT32).c_str(),
888 toString(OperandType::TENSOR_INT32).c_str());
889 }
890
891 int32_t activationFunctionAsInt;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100892 if (!GetInputScalar<HalPolicy>(operation, inputIndex, type, activationFunctionAsInt, model, data))
arovir01b0717b52018-09-05 17:03:25 +0100893 {
894 return Fail("%s: failed to get activation input value", __func__);
895 }
896 outActivationFunction = static_cast<ActivationFn>(activationFunctionAsInt);
897 return true;
898}
899
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100900template<typename HalPolicy,
901 typename HalOperation = typename HalPolicy::Operation,
902 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +0100903bool GetInputActivationFunction(const HalOperation& operation,
904 uint32_t inputIndex,
905 ActivationFn& outActivationFunction,
906 const HalModel& model,
907 const ConversionData& data)
908{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100909 return GetInputActivationFunctionImpl<HalPolicy>(operation,
910 inputIndex,
911 HalPolicy::OperandType::INT32,
912 outActivationFunction,
913 model,
914 data);
arovir01b0717b52018-09-05 17:03:25 +0100915}
916
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100917template<typename HalPolicy,
918 typename HalOperation = typename HalPolicy::Operation,
919 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +0100920bool GetInputActivationFunctionFromTensor(const HalOperation& operation,
921 uint32_t inputIndex,
922 ActivationFn& outActivationFunction,
923 const HalModel& model,
924 const ConversionData& data)
925{
926 // This only accepts a 1-D tensor of size 1
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100927 return GetInputActivationFunctionImpl<HalPolicy>(operation,
928 inputIndex,
929 HalPolicy::OperandType::INT32,
930 outActivationFunction,
931 model,
932 data);
arovir01b0717b52018-09-05 17:03:25 +0100933}
934
935
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100936template<typename HalPolicy,
937 typename HalOperation = typename HalPolicy::Operation,
938 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +0100939bool GetOptionalInputActivation(const HalOperation& operation,
940 uint32_t inputIndex,
941 ActivationFn& activationFunction,
942 const HalModel& model,
943 const ConversionData& data)
944{
945 if (operation.inputs.size() <= inputIndex)
946 {
947 activationFunction = ActivationFn::kActivationNone;
948 }
949 else
950 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100951 if (!GetInputActivationFunction<HalPolicy>(operation, inputIndex, activationFunction, model, data))
arovir01b0717b52018-09-05 17:03:25 +0100952 {
953 return Fail("%s: Operation has invalid inputs", __func__);
954 }
955 }
956 return true;
957}
958
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100959template<typename HalPolicy,
960 typename ConvolutionDescriptor,
961 typename HalOperation = typename HalPolicy::Operation,
962 typename HalModel = typename HalPolicy::Model>
Aron Virginas-Tar07c7c9a2019-06-12 14:03:35 +0100963bool GetOptionalConvolutionDilationParams(const HalOperation& operation,
964 uint32_t dilationXIndex,
965 ConvolutionDescriptor& descriptor,
966 const HalModel& model,
967 const ConversionData& data)
968{
969 bool success = true;
970 if (operation.inputs.size() >= dilationXIndex + 2)
971 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100972 success &= GetInputScalar<HalPolicy>(operation,
973 dilationXIndex,
974 HalPolicy::OperandType::INT32,
975 descriptor.m_DilationX,
976 model,
977 data);
978 success &= GetInputScalar<HalPolicy>(operation,
979 dilationXIndex + 1,
980 HalPolicy::OperandType::INT32,
981 descriptor.m_DilationY,
982 model,
983 data);
Aron Virginas-Tar07c7c9a2019-06-12 14:03:35 +0100984 }
985
986 return success;
987}
988
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100989template<typename HalPolicy,
990 typename HalOperand = typename HalPolicy::Operand,
991 typename HalModel = typename HalPolicy::Model>
Mike Kellyb5fdf382019-06-11 16:35:25 +0100992bool GetTensorInt32Values(const HalOperand& operand,
arovir01b0717b52018-09-05 17:03:25 +0100993 std::vector<int32_t>& outValues,
994 const HalModel& model,
995 const ConversionData& data)
996{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +0100997 if (operand.type != HalPolicy::OperandType::TENSOR_INT32)
arovir01b0717b52018-09-05 17:03:25 +0100998 {
999 return Fail("%s: invalid operand type: %s", __func__, toString(operand.type).c_str());
1000 }
1001
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001002 const void* startAddress = GetOperandValueReadOnlyAddress<HalPolicy>(operand, model, data);
arovir01b0717b52018-09-05 17:03:25 +01001003 if (!startAddress)
1004 {
1005 return Fail("%s: failed to get operand address", __func__, operand.type);
1006 }
1007
1008 // Check number of bytes is sensible
1009 const uint32_t numBytes = operand.location.length;
1010 if (numBytes % sizeof(int32_t) != 0)
1011 {
1012 return Fail("%s: invalid number of bytes: %i, expected to be a multiple of %i",
1013 __func__, numBytes, sizeof(int32_t));
1014 }
1015
1016 outValues.resize(numBytes / sizeof(int32_t));
1017 memcpy(outValues.data(), startAddress, numBytes);
1018 return true;
1019}
1020
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001021template<typename HalPolicy,
1022 typename HalOperation = typename HalPolicy::Operation,
1023 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +01001024bool GetInputPaddingScheme(const HalOperation& operation,
1025 uint32_t inputIndex,
1026 PaddingScheme& outPaddingScheme,
1027 const HalModel& model,
1028 const ConversionData& data)
1029{
1030 int32_t paddingSchemeAsInt;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001031 if (!GetInputInt32<HalPolicy>(operation, inputIndex, paddingSchemeAsInt, model, data))
arovir01b0717b52018-09-05 17:03:25 +01001032 {
1033 return Fail("%s: failed to get padding scheme input value", __func__);
1034 }
1035
1036 outPaddingScheme = static_cast<android::nn::PaddingScheme>(paddingSchemeAsInt);
1037 return true;
1038}
1039
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001040template<typename HalPolicy,
1041 typename HalOperation = typename HalPolicy::Operation,
1042 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +01001043LayerInputHandle ConvertToLayerInputHandle(const HalOperation& operation,
1044 uint32_t inputIndex,
1045 const HalModel& model,
1046 ConversionData& data)
1047{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001048 using HalOperand = typename HalPolicy::Operand;
Sadik Armagan44bcc022019-06-18 17:21:36 +01001049 using HalOperandType = typename HalPolicy::OperandType;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001050 using HalOperandLifeTime = typename HalPolicy::OperandLifeTime;
1051
1052 const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model);
arovir01b0717b52018-09-05 17:03:25 +01001053 if (!operand)
1054 {
1055 Fail("%s: failed to get input operand %i", __func__, inputIndex);
1056 return LayerInputHandle();
1057 }
1058
1059 if (!IsOperandTypeSupportedForTensors(operand->type))
1060 {
1061 Fail("%s: unsupported operand type for tensor %s", __func__, toString(operand->type).c_str());
1062 return LayerInputHandle();
1063 }
1064
Sadik Armagan44bcc022019-06-18 17:21:36 +01001065 try
arovir01b0717b52018-09-05 17:03:25 +01001066 {
Sadik Armagan44bcc022019-06-18 17:21:36 +01001067 armnn::TensorInfo operandTensorInfo = GetTensorInfoForOperand(*operand);
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +01001068 if (IsDynamicTensor(operandTensorInfo))
1069 {
1070 Fail("%s: dynamic input tensors are not supported", __func__);
1071 return LayerInputHandle();
1072 }
arovir01b0717b52018-09-05 17:03:25 +01001073
Sadik Armagan44bcc022019-06-18 17:21:36 +01001074 switch (operand->lifetime)
arovir01b0717b52018-09-05 17:03:25 +01001075 {
Sadik Armagan44bcc022019-06-18 17:21:36 +01001076 case HalOperandLifeTime::MODEL_INPUT:
Aron Virginas-Tar000117b2019-07-25 16:24:49 +01001077 {
1078 // NOTE: We must check whether we can support the input tensor on at least one
1079 // of the provided backends; otherwise we cannot convert the operation
1080 bool isInputSupported = false;
1081 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1082 IsInputSupported,
1083 data.m_Backends,
1084 isInputSupported,
1085 operandTensorInfo);
1086
1087 if (!isInputSupported)
1088 {
1089 Fail("%s: unsupported input tensor", __func__);
1090 return LayerInputHandle();
1091 }
1092
1093 BOOST_FALLTHROUGH; // intentional fallthrough
1094 }
1095 case HalOperandLifeTime::TEMPORARY_VARIABLE: // intentional fallthrough
Sadik Armagan44bcc022019-06-18 17:21:36 +01001096 case HalOperandLifeTime::MODEL_OUTPUT:
arovir01b0717b52018-09-05 17:03:25 +01001097 {
Sadik Armagan44bcc022019-06-18 17:21:36 +01001098 // The tensor is either an operand internal to the model, or a model input.
1099 // It can be associated with an ArmNN output slot for an existing layer.
1100
1101 // m_OutputSlotForOperand[...] can be nullptr if the previous layer could not be converted
1102 const uint32_t operandIndex = operation.inputs[inputIndex];
1103 return LayerInputHandle(true, data.m_OutputSlotForOperand[operandIndex], operandTensorInfo);
Sadik Armagan44bcc022019-06-18 17:21:36 +01001104 }
Aron Virginas-Tar000117b2019-07-25 16:24:49 +01001105 case HalOperandLifeTime::CONSTANT_COPY: // intentional fallthrough
Sadik Armagan44bcc022019-06-18 17:21:36 +01001106 case HalOperandLifeTime::CONSTANT_REFERENCE:
1107 {
1108 // The tensor has an already known constant value, and can be converted into an ArmNN Constant layer.
1109 ConstTensorPin tensorPin = ConvertOperandToConstTensorPin<HalPolicy>(*operand, model, data);
1110 if (tensorPin.IsValid())
arovir01b0717b52018-09-05 17:03:25 +01001111 {
Ferran Balaguerd30093c2019-07-09 17:04:47 +01001112 bool isSupported = false;
1113 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1114 IsConstantSupported,
1115 data.m_Backends,
1116 isSupported,
1117 tensorPin.GetConstTensor().GetInfo());
Mike Kelly28e3d9f2019-08-07 14:55:04 +01001118 if (!isSupported)
Sadik Armagan44bcc022019-06-18 17:21:36 +01001119 {
1120 return LayerInputHandle();
1121 }
1122
1123 armnn::IConnectableLayer* constantLayer =
1124 data.m_Network->AddConstantLayer(tensorPin.GetConstTensor());
1125 armnn::IOutputSlot& outputSlot = constantLayer->GetOutputSlot(0);
1126 outputSlot.SetTensorInfo(tensorPin.GetConstTensor().GetInfo());
1127
1128 return LayerInputHandle(true, &outputSlot, operandTensorInfo);
1129 }
1130 else
1131 {
1132 Fail("%s: invalid operand tensor", __func__);
arovir01b0717b52018-09-05 17:03:25 +01001133 return LayerInputHandle();
1134 }
Sadik Armagan44bcc022019-06-18 17:21:36 +01001135 break;
arovir01b0717b52018-09-05 17:03:25 +01001136 }
Sadik Armagan44bcc022019-06-18 17:21:36 +01001137 default:
arovir01b0717b52018-09-05 17:03:25 +01001138 {
Sadik Armagan44bcc022019-06-18 17:21:36 +01001139 // Unsupported lifetime for an input tensor
1140 Fail("%s: unsupported lifetime for input tensor: %s",
1141 __func__, toString(operand->lifetime).c_str());
arovir01b0717b52018-09-05 17:03:25 +01001142 return LayerInputHandle();
1143 }
arovir01b0717b52018-09-05 17:03:25 +01001144 }
Sadik Armagan44bcc022019-06-18 17:21:36 +01001145 }
1146 catch (UnsupportedOperand<HalOperandType>& e)
1147 {
1148 Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str());
1149 return LayerInputHandle();
arovir01b0717b52018-09-05 17:03:25 +01001150 }
1151}
1152
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001153template<typename HalPolicy,
1154 typename HalOperation = typename HalPolicy::Operation,
1155 typename HalModel = typename HalPolicy::Model>
Mike Kellyb5fdf382019-06-11 16:35:25 +01001156bool SetupAndTrackLayerOutputSlot(const HalOperation& operation,
1157 uint32_t operationOutputIndex,
1158 armnn::IConnectableLayer& layer,
1159 uint32_t layerOutputIndex,
1160 const HalModel& model,
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001161 ConversionData& data)
Mike Kellyb5fdf382019-06-11 16:35:25 +01001162{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001163 using HalOperand = typename HalPolicy::Operand;
1164
1165 const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, operationOutputIndex, model);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001166 if ((outputOperand == nullptr) || (operationOutputIndex >= layer.GetNumOutputSlots()))
1167 {
1168 return false;
1169 }
1170
1171 armnn::IOutputSlot& outputSlot = layer.GetOutputSlot(layerOutputIndex);
1172
1173 const uint32_t operandIndex = operation.outputs[operationOutputIndex];
1174 data.m_OutputSlotForOperand[operandIndex] = &outputSlot;
1175
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001176 outputSlot.SetTensorInfo(GetTensorInfoForOperand(*outputOperand));
Mike Kellyb5fdf382019-06-11 16:35:25 +01001177
1178 return true;
1179}
1180
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001181template<typename HalPolicy,
1182 typename HalOperation = typename HalPolicy::Operation,
1183 typename HalModel = typename HalPolicy::Model>
Mike Kellyb5fdf382019-06-11 16:35:25 +01001184armnn::DataLayout OptionalDataLayout(const HalOperation& operation,
1185 uint32_t inputIndex,
1186 const HalModel& model,
1187 ConversionData& data)
1188{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001189 using HalOperand = typename HalPolicy::Operand;
1190
1191 const HalOperand* operand = GetInputOperand<HalPolicy>(operation, inputIndex, model);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001192 if (!operand)
1193 {
1194 return armnn::DataLayout::NHWC;
1195 }
1196
1197 if (!IsBool(*operand))
1198 {
1199 return armnn::DataLayout::NHWC;
1200 }
1201
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001202 const void* valueAddress = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001203 if (!valueAddress)
1204 {
1205 return armnn::DataLayout::NHWC;
1206 }
1207
1208 if (*(static_cast<const bool*>(valueAddress)))
1209 {
1210 return armnn::DataLayout::NCHW;
1211 }
1212 else
1213 {
1214 return armnn::DataLayout::NHWC;
1215 }
1216}
1217
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001218template<typename HalPolicy,
1219 typename HalOperation = typename HalPolicy::Operation,
1220 typename HalModel = typename HalPolicy::Model>
Mike Kellyb5fdf382019-06-11 16:35:25 +01001221bool SetupAndTrackLayerOutputSlot(const HalOperation& operation,
1222 uint32_t outputIndex,
1223 armnn::IConnectableLayer& layer,
1224 const HalModel& model,
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001225 ConversionData& data)
Mike Kellyb5fdf382019-06-11 16:35:25 +01001226{
Aron Virginas-Tarf03fcf02019-07-09 17:44:24 +01001227 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation,
1228 outputIndex,
1229 layer,
1230 outputIndex,
1231 model,
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001232 data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001233}
1234
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001235template<typename HalPolicy,
1236 typename HalOperation = typename HalPolicy::Operation,
1237 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +01001238bool ConvertToActivation(const HalOperation& operation,
1239 const char* operationName,
1240 const armnn::ActivationDescriptor& activationDesc,
1241 const HalModel& model,
1242 ConversionData& data)
1243{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001244 using HalOperand = typename HalPolicy::Operand;
1245
1246 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
arovir01b0717b52018-09-05 17:03:25 +01001247 if (!input.IsValid())
1248 {
1249 return Fail("%s: Input 0 is invalid", operationName);
1250 }
1251
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001252 const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
arovir01b0717b52018-09-05 17:03:25 +01001253 if (!outputOperand)
1254 {
1255 return false;
1256 }
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001257
1258 const armnn::TensorInfo& outInfo = GetTensorInfoForOperand(*outputOperand);
Sadik Armagan2050c232019-07-23 16:59:58 +01001259 if (IsDynamicTensor(outInfo))
1260 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001261 return Fail("%s: Dynamic output tensors are not supported", __func__);
Sadik Armagan2050c232019-07-23 16:59:58 +01001262 }
Ferran Balaguerd30093c2019-07-09 17:04:47 +01001263
1264 bool isSupported = false;
1265 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1266 IsActivationSupported,
1267 data.m_Backends,
1268 isSupported,
1269 input.GetTensorInfo(),
1270 outInfo,
1271 activationDesc);
1272 if (!isSupported)
arovir01b0717b52018-09-05 17:03:25 +01001273 {
1274 return false;
1275 }
1276
1277 armnn::IConnectableLayer* layer = data.m_Network->AddActivationLayer(activationDesc);
1278 BOOST_ASSERT(layer != nullptr);
1279 input.Connect(layer->GetInputSlot(0));
1280
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001281 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
arovir01b0717b52018-09-05 17:03:25 +01001282}
1283
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001284template<typename HalPolicy,
Sadik Armagan61113162019-07-25 09:09:40 +01001285 typename HalOperation = typename HalPolicy::Operation,
1286 typename HalModel = typename HalPolicy::Model>
1287bool ConvertReLu(const HalOperation& operation, const HalModel& model, ConversionData& data)
1288{
1289 armnn::ActivationDescriptor desc;
1290 desc.m_Function = armnn::ActivationFunction::ReLu;
1291
1292 return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data);
1293}
1294
1295template<typename HalPolicy,
1296 typename HalOperation = typename HalPolicy::Operation,
1297 typename HalModel = typename HalPolicy::Model>
1298bool ConvertReLu1(const HalOperation& operation, const HalModel& model, ConversionData& data)
1299{
1300 armnn::ActivationDescriptor desc;
1301 desc.m_Function = armnn::ActivationFunction::BoundedReLu;
1302 desc.m_A = 1.0f;
1303 desc.m_B = -1.0f;
1304
1305 return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data);
1306}
1307
1308template<typename HalPolicy,
1309 typename HalOperation = typename HalPolicy::Operation,
1310 typename HalModel = typename HalPolicy::Model>
1311bool ConvertReLu6(const HalOperation& operation, const HalModel& model, ConversionData& data)
1312{
1313 armnn::ActivationDescriptor desc;
1314 desc.m_Function = armnn::ActivationFunction::BoundedReLu;
1315 desc.m_A = 6.0f;
1316
1317 return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data);
1318}
1319
1320template<typename HalPolicy,
1321 typename HalOperation = typename HalPolicy::Operation,
1322 typename HalModel = typename HalPolicy::Model>
1323bool ConvertTanH(const HalOperation& operation, const HalModel& model, ConversionData& data)
1324{
1325 armnn::ActivationDescriptor desc;
1326 desc.m_Function = armnn::ActivationFunction::TanH;
1327 desc.m_A = 1.0f; // android nn does not support tanH parameters
1328 desc.m_B = 1.0f; // set to 1.0f for unity scaling
1329
1330 return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data);
1331}
1332
1333template<typename HalPolicy,
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001334 typename HalOperation = typename HalPolicy::Operation,
1335 typename HalModel = typename HalPolicy::Model>
Aron Virginas-Tarcb8ac842019-07-05 15:47:07 +01001336bool ConvertPaddings(const HalOperation& operation,
1337 const HalModel& model,
1338 ConversionData& data,
1339 unsigned int rank,
1340 armnn::PadDescriptor& padDescriptor)
1341{
1342 using HalOperand = typename HalPolicy::Operand;
1343
1344 const HalOperand* paddingsOperand = GetInputOperand<HalPolicy>(operation, 1, model);
1345 if (!paddingsOperand)
1346 {
1347 return Fail("%s: Could not read paddings operand", __func__);
1348 }
1349
1350 armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand);
1351 if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != rank * 2)
1352 {
1353 return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, rank);
1354 }
1355
1356 std::vector<int32_t> paddings;
1357 GetTensorInt32Values<HalPolicy>(*paddingsOperand, paddings, model, data);
1358
1359 // add padding for each dimension of input tensor.
1360 for (unsigned int i = 0; i < paddings.size() - 1; i += 2)
1361 {
1362 int paddingBeforeInput = paddings[i];
1363 int paddingAfterInput = paddings[i + 1];
1364
1365 if (paddingBeforeInput < 0 || paddingAfterInput < 0)
1366 {
1367 return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__);
1368 }
1369
1370 padDescriptor.m_PadList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput);
1371 }
1372
1373 return true;
1374}
1375
1376template<typename HalPolicy,
1377 typename HalOperation = typename HalPolicy::Operation,
1378 typename HalModel = typename HalPolicy::Model>
arovir01b0717b52018-09-05 17:03:25 +01001379bool ConvertPooling2d(const HalOperation& operation,
1380 const char* operationName,
1381 armnn::PoolingAlgorithm poolType,
1382 const HalModel& model,
1383 ConversionData& data)
1384{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001385 using HalOperand = typename HalPolicy::Operand;
1386 using HalOperandType = typename HalPolicy::OperandType;
1387
1388 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
arovir01b0717b52018-09-05 17:03:25 +01001389 if (!input.IsValid())
1390 {
1391 return Fail("%s: Could not read input 0", operationName);
1392 }
1393
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001394 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
arovir01b0717b52018-09-05 17:03:25 +01001395 if (!output)
1396 {
1397 return Fail("%s: Could not read output 0", __func__);
1398 }
1399
1400 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
1401 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
1402
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001403 if (IsDynamicTensor(outputInfo))
1404 {
1405 return Fail("%s: Dynamic output tensors are not supported", __func__);
1406 }
1407
arovir01b0717b52018-09-05 17:03:25 +01001408 armnn::Pooling2dDescriptor desc;
1409 desc.m_PoolType = poolType;
1410 desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Floor;
Matteo Martincigh39fc5472018-10-26 16:39:28 +01001411 desc.m_DataLayout = armnn::DataLayout::NHWC;
arovir01b0717b52018-09-05 17:03:25 +01001412
1413 ActivationFn activation;
1414
Sadik Armagan15d63e22019-07-26 16:59:35 +01001415 auto inputSize = operation.inputs.size();
1416
1417 if (inputSize >= 10)
1418 {
1419 // one input, 9 parameters (padding l r t b, stridex, stridey, width, height, activation type)
1420 if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, desc.m_PadLeft, model, data) ||
1421 !GetInputScalar<HalPolicy>(operation, 2, HalOperandType::INT32, desc.m_PadRight, model, data) ||
1422 !GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadTop, model, data) ||
1423 !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadBottom, model, data) ||
1424 !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideX, model, data) ||
1425 !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_StrideY, model, data) ||
1426 !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_PoolWidth, model, data) ||
1427 !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_PoolHeight, model, data) ||
1428 !GetInputActivationFunction<HalPolicy>(operation, 9, activation, model, data))
1429 {
1430 return Fail("%s: Operation has invalid inputs", operationName);
1431 }
1432
1433 if (Is12Operand(*output))
1434 {
1435 desc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 10, model, data);
1436 }
1437 }
1438 else
arovir01b0717b52018-09-05 17:03:25 +01001439 {
1440 // one input, 6 parameters (padding, stridex, stridey, width, height, activation type)
1441 android::nn::PaddingScheme scheme;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001442 if (!GetInputPaddingScheme<HalPolicy>(operation, 1, scheme, model, data) ||
1443 !GetInputScalar<HalPolicy>(operation, 2, HalOperandType::INT32, desc.m_StrideX, model, data) ||
1444 !GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_StrideY, model, data) ||
1445 !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PoolWidth, model, data) ||
1446 !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PoolHeight, model, data) ||
1447 !GetInputActivationFunction<HalPolicy>(operation, 6, activation, model, data))
arovir01b0717b52018-09-05 17:03:25 +01001448 {
1449 return Fail("%s: Operation has invalid inputs", operationName);
1450 }
1451
Matteo Martincigh39fc5472018-10-26 16:39:28 +01001452 const unsigned int inputWidth = inputInfo.GetShape()[2];
1453 const unsigned int inputHeight = inputInfo.GetShape()[1];
arovir01b0717b52018-09-05 17:03:25 +01001454
1455 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, scheme);
1456 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, scheme);
Sadik Armagan15d63e22019-07-26 16:59:35 +01001457
1458 if (Is12Operand(*output))
arovir01b0717b52018-09-05 17:03:25 +01001459 {
Sadik Armagan15d63e22019-07-26 16:59:35 +01001460 desc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 7, model, data);
arovir01b0717b52018-09-05 17:03:25 +01001461 }
1462 }
1463
Ferran Balaguerd30093c2019-07-09 17:04:47 +01001464 bool isSupported = false;
1465 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1466 IsPooling2dSupported,
1467 data.m_Backends,
1468 isSupported,
1469 inputInfo,
1470 outputInfo,
1471 desc);
1472 if (!isSupported)
arovir01b0717b52018-09-05 17:03:25 +01001473 {
Éanna Ó Catháin3d1059c2018-10-11 15:53:04 +01001474 return false;
arovir01b0717b52018-09-05 17:03:25 +01001475 }
arovir01b0717b52018-09-05 17:03:25 +01001476
Matteo Martincigh39fc5472018-10-26 16:39:28 +01001477 armnn::IConnectableLayer* pooling2dLayer = data.m_Network->AddPooling2dLayer(desc);
1478 if (!pooling2dLayer)
arovir01b0717b52018-09-05 17:03:25 +01001479 {
Matteo Martincigh39fc5472018-10-26 16:39:28 +01001480 return Fail("%s: AddPooling2dLayer failed", __func__);
arovir01b0717b52018-09-05 17:03:25 +01001481 }
Matteo Martincigh39fc5472018-10-26 16:39:28 +01001482
1483 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, pooling2dLayer, data);
1484 if (!endLayer)
arovir01b0717b52018-09-05 17:03:25 +01001485 {
Matteo Martincigh39fc5472018-10-26 16:39:28 +01001486 return Fail("%s: ProcessActivation failed", __func__);
arovir01b0717b52018-09-05 17:03:25 +01001487 }
Matteo Martincigh39fc5472018-10-26 16:39:28 +01001488
1489 input.Connect(pooling2dLayer->GetInputSlot(0));
1490
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001491 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001492}
1493
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001494template<typename HalPolicy,
Mike Kellyb8805202019-07-31 17:25:43 +01001495 typename Operation = typename HalPolicy::Operation,
1496 typename Model = typename HalPolicy::Model>
Mike Kelly46272802019-08-14 17:00:48 +01001497bool ConvertAdd(const Operation& operation, const Model& model, ConversionData& data)
1498{
1499 using Operand = typename HalPolicy::Operand;
1500
1501 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
1502 LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data);
1503
1504 if (!input0.IsValid() || !input1.IsValid())
1505 {
1506 return Fail("%s: Operation has invalid inputs", __func__);
1507 }
1508
1509 // The FuseActivation parameter is always the input index 2
1510 // and it should be optional
1511 ActivationFn activationFunction;
1512 if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data))
1513 {
1514 return Fail("%s: Operation has invalid inputs", __func__);
1515 }
1516
1517 const Operand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
1518 if (!outputOperand)
1519 {
1520 return false;
1521 }
1522
1523 const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo();
1524 const armnn::TensorInfo& inputInfo1 = input1.GetTensorInfo();
1525
1526 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
1527 if (IsDynamicTensor(outputInfo))
1528 {
1529 return Fail("%s: Dynamic output tensors are not supported", __func__);
1530 }
1531
1532 bool isSupported = false;
1533 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1534 IsAdditionSupported,
1535 data.m_Backends,
1536 isSupported,
1537 inputInfo0,
1538 inputInfo1,
1539 outputInfo);
1540 if (!isSupported)
1541 {
1542 return false;
1543 }
1544
1545 armnn::IConnectableLayer* const startLayer = data.m_Network->AddAdditionLayer();
1546 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
1547
1548 if (endLayer != nullptr)
1549 {
Sadik Armagan64b19b52019-08-19 09:49:58 +01001550 bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data);
1551 if (!isReshapeSupported)
1552 {
1553 return false;
1554 }
1555
Mike Kelly46272802019-08-14 17:00:48 +01001556 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
1557 }
1558 else
1559 {
1560 return Fail("%s: ProcessActivation failed", __func__);
1561 }
1562}
1563
1564template<typename HalPolicy,
1565 typename Operation = typename HalPolicy::Operation,
1566 typename Model = typename HalPolicy::Model>
Francis Murtagha23334e2019-11-19 12:06:47 +00001567bool ConvertArgMinMax(const Operation& operation,
1568 const Model& model,
1569 ConversionData& data,
1570 armnn::ArgMinMaxFunction argMinMaxFunction)
1571{
1572 ALOGV("argMinMaxFunction = %s", GetArgMinMaxFunctionAsCString(argMinMaxFunction));
1573
1574 using HalOperand = typename HalPolicy::Operand;
1575 using HalOperandType = typename HalPolicy::OperandType;
1576
1577 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
1578
1579 if (!input0.IsValid())
1580 {
1581 return Fail("%s: Operation has invalid inputs", __func__);
1582 }
1583
1584 int32_t axis;
1585 if (!GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, axis, model, data))
1586 {
1587 return Fail("%s: Operation has invalid inputs. Failed to read axis.", __func__);
1588 }
1589
1590 const armnn::TensorInfo& inputInfo = input0.GetTensorInfo();
1591 int rank = static_cast<int>(inputInfo.GetNumDimensions());
1592
1593 if (((axis < -rank) && (axis < 0)) || ((axis >= rank) && (axis > 0)))
1594 {
1595 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
1596 // E.g. Rank 4 tensor can have axis in range [-4, 3)
1597 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
1598 return Fail("%s: Axis must be in range [-n, n)", __func__);
1599 }
1600
1601 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
1602 if (!output)
1603 {
1604 return Fail("%s: Could not read output 0", __func__);
1605 }
1606
1607 const armnn::TensorInfo& inputInfo0 = input0.GetTensorInfo();
1608
1609 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
1610 if (IsDynamicTensor(outputInfo))
1611 {
1612 return Fail("%s: Dynamic output tensors are not supported", __func__);
1613 }
1614
1615 armnn::ArgMinMaxDescriptor descriptor;
1616 descriptor.m_Function = argMinMaxFunction;
1617 descriptor.m_Axis = axis;
1618
1619 bool isSupported = false;
1620 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1621 IsArgMinMaxSupported,
1622 data.m_Backends,
1623 isSupported,
1624 inputInfo0,
1625 outputInfo,
1626 descriptor);
1627 if (!isSupported)
1628 {
1629 return false;
1630 }
1631
1632 armnn::IConnectableLayer* layer = data.m_Network->AddArgMinMaxLayer(descriptor);
1633 assert(layer != nullptr);
1634
1635 input0.Connect(layer->GetInputSlot(0));
1636
1637 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
1638}
1639
1640template<typename HalPolicy,
1641 typename Operation = typename HalPolicy::Operation,
1642 typename Model = typename HalPolicy::Model>
Mike Kellyb8805202019-07-31 17:25:43 +01001643bool ConvertConcatenation(const Operation& operation, const Model& model, ConversionData& data)
1644{
1645 using HalOperand = typename HalPolicy::Operand;
1646 using HalOperandType = typename HalPolicy::OperandType;
1647
1648 // The first N (0..N-1) inputs are tensors. The Nth input is the concatenation axis.
1649 if (operation.inputs.size() <= 1)
1650 {
1651 return Fail("%s: Operation has insufficient arguments", __func__);
1652 }
1653
1654 // Get inputs and outputs
1655 const std::size_t numInputTensors = operation.inputs.size() - 1;
1656
1657 int32_t concatDim;
1658 if (!GetInputScalar<HalPolicy>(operation, numInputTensors, HalOperandType::INT32, concatDim, model, data))
1659 {
1660 return Fail("%s: Operation has invalid inputs", __func__);
1661 }
1662
1663 const HalOperand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
1664 if (!outputOperand)
1665 {
1666 return Fail("%s: Operation has no outputs", __func__);
1667 }
1668
1669
1670 armnn::TensorInfo outputInfo = GetTensorInfoForOperand(*outputOperand);
1671 armnn::TensorShape outputShape = outputInfo.GetShape();
1672
1673 //
1674 // handle negative concat dims along the lines of tensorflow as described here:
1675 // https://www.tensorflow.org/api_docs/python/tf/concat
1676 // "negative axis refers to axis + rank(values)-th dimension"
1677 //
1678 if (concatDim < 0)
1679 {
1680 concatDim += outputShape.GetNumDimensions();
1681 }
1682
1683 if (concatDim >= static_cast<int32_t>(outputShape.GetNumDimensions()) || concatDim < 0)
1684 {
1685 return Fail("%s: Operation has invalid concat axis: %d", __func__, concatDim);
1686 }
1687
1688 std::vector<LayerInputHandle> inputHandles;
1689 std::vector<armnn::TensorShape> inputShapes;
1690
1691 inputHandles.reserve(numInputTensors);
1692 inputShapes.reserve(numInputTensors);
1693
1694 bool inputsHaveBeenReshaped = false;
1695 unsigned int tensorDimensionsAdded = 0;
1696
1697 for (uint32_t i = 0; i < numInputTensors; ++i)
1698 {
1699 const HalOperand* operand = GetInputOperand<HalPolicy>(operation, i, model);
1700 if (!operand)
1701 {
1702 return Fail("%s: Operation has invalid inputs", __func__);
1703 }
1704
Teresa Charlin3b959602019-10-31 17:05:47 +00001705 LayerInputHandle operandInputHandle = ConvertToLayerInputHandle<HalPolicy>(operation, i, model, data);
1706 if (!operandInputHandle.IsValid())
1707 {
1708 return Fail("%s: Operation has invalid inputs", __func__);
1709 }
Mike Kellyb8805202019-07-31 17:25:43 +01001710
Teresa Charlin3b959602019-10-31 17:05:47 +00001711 armnn::TensorShape operandShape = GetTensorShapeForOperand(*operand);
Mike Kellyb8805202019-07-31 17:25:43 +01001712 if (operandShape.GetNumDimensions() == 0)
1713 {
1714 return Fail("%s: Operands with rank 0 are not supported", __func__);
1715 }
1716
1717 if (RequiresReshape(operandShape))
1718 {
1719 inputsHaveBeenReshaped = true;
1720
1721 armnn::TensorInfo reshapeInfo = operandInputHandle.GetTensorInfo();
1722
1723 // Expand the tensor to three dimensions
1724 if (operandShape.GetNumDimensions() == 2)
1725 {
1726 reshapeInfo.SetShape(armnn::TensorShape({1, operandShape[0], operandShape[1]}));
1727 tensorDimensionsAdded = 1;
1728 }
1729 else
1730 {
1731 reshapeInfo.SetShape(armnn::TensorShape({1, 1, operandShape[0]}));
1732 tensorDimensionsAdded = 2;
1733 }
1734
1735 armnn::IConnectableLayer& newReshape = AddReshapeLayer(
1736 *data.m_Network,
1737 operandInputHandle,
1738 reshapeInfo
1739 );
1740
1741 // Point to the reshape operation rather then the input operation
1742 operandShape = reshapeInfo.GetShape();
1743 operandInputHandle = LayerInputHandle(true, &newReshape.GetOutputSlot(0), reshapeInfo);
1744 }
1745
1746 inputShapes.emplace_back(operandShape);
1747 inputHandles.emplace_back(operandInputHandle);
1748
1749 if (!inputHandles.back().IsValid())
1750 {
1751 return Fail("%s: Operation has invalid inputs", __func__);
1752 }
1753 }
1754
1755 BOOST_ASSERT(inputShapes.size() == inputHandles.size());
1756
1757 if (inputsHaveBeenReshaped)
1758 {
1759 // Adjust the concatenation dimension by the amount of dimensions added (if any)
1760 concatDim += tensorDimensionsAdded;
1761
1762 // Add extra dimensions to the output shape to reflect the addition of the reshape layers
1763 if (tensorDimensionsAdded == 1)
1764 {
1765 outputShape = armnn::TensorShape({1, outputShape[0], outputShape[1]});
1766 }
1767 else if (tensorDimensionsAdded == 2)
1768 {
1769 outputShape = armnn::TensorShape({1, 1, outputShape[0]});
1770 }
1771 }
1772
1773 // Check if permutations is required and get the pair of permutations required for the concatenation.
1774 // Permutation is required when the concat dimension is 2 for a 4D tensor or 1 for a 3D tensor.
1775 std::pair<armnn::PermutationVector, armnn::PermutationVector> permutationPair =
1776 std::make_pair(IdentityPermutation4D, IdentityPermutation4D);
1777
1778 bool needPermute =
1779 CreateConcatPermutationParameters(inputShapes[0].GetNumDimensions(), concatDim, permutationPair);
1780
1781 if (needPermute)
1782 {
1783 outputShape = armnnUtils::Permuted(outputShape, permutationPair.first);
1784 }
1785
1786 outputInfo.SetShape(outputShape);
1787
1788 // this is no-op for identity swizzles, otherwise it replaces both
1789 // the handles and shapes with the swizzled layer output handles and shapes
1790 SwizzleInputs(*data.m_Network, inputHandles, inputShapes, permutationPair.first);
1791
1792 // Create an armnn concat layer descriptor - this will also perform validation on the input shapes
1793 armnn::OriginsDescriptor concatDescriptor;
1794
1795 try
1796 {
1797 // The concat descriptor is always created across the only supported concat dimension
1798 // which is 0, 1 or 3 for a 4-D tensor, or 0 or 2 for a 3-D tensor.
1799 concatDescriptor =
1800 armnn::CreateDescriptorForConcatenation(inputShapes.begin(), inputShapes.end(), concatDim);
1801 }
1802 catch (const armnn::Exception& error)
1803 {
1804 return Fail("%s: Error preparing concat descriptor. %s", __func__, error.what());
1805 }
1806
1807 // Validate the output shape is correct given the input shapes based on the
1808 // only valid concat dimension which is 0, 1 or 3 for a 4-D tensor, or 0 or 2 for a 3-D tensor.
1809 if (!ValidateConcatOutputShape(inputShapes, outputShape, concatDim))
1810 {
1811 return Fail("%s: Error validating the output shape for concat", __func__);
1812 }
1813
1814 std::vector<const armnn::TensorInfo*> inputTensorInfos;
1815 std::transform(inputHandles.begin(), inputHandles.end(), std::back_inserter(inputTensorInfos),
1816 [](const LayerInputHandle& h) -> const armnn::TensorInfo*{ return &h.GetTensorInfo(); });
1817
1818 bool isSupported = false;
1819 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1820 IsConcatSupported,
1821 data.m_Backends,
1822 isSupported,
1823 inputTensorInfos,
1824 outputInfo,
1825 concatDescriptor);
1826 if (!isSupported)
1827 {
1828 return false;
1829 }
1830
1831 armnn::IConnectableLayer* layer = data.m_Network->AddConcatLayer(concatDescriptor);
1832 assert(layer != nullptr);
1833 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1834
1835 // Connect inputs to the layer
1836 const int numInputSlots = layer->GetNumInputSlots();
1837 assert(static_cast<std::size_t>(numInputSlots) == inputHandles.size());
1838 for (int i = 0; i < numInputSlots; ++i)
1839 {
1840 // connect the input directly to the merge (concat) layer
1841 inputHandles[static_cast<unsigned int>(i)].Connect(layer->GetInputSlot(i));
1842 }
1843
1844 if (needPermute)
1845 {
1846 // Add permutation layer and connect the output to it, the permutation becomes the output layer
1847 armnn::IConnectableLayer& deswizzleLayer = AddPermuteLayer(*data.m_Network,
1848 layer->GetOutputSlot(0),
1849 permutationPair.second);
1850 layer = &deswizzleLayer;
1851 }
1852
1853 if (inputsHaveBeenReshaped)
1854 {
1855 armnn::TensorInfo afterConcatInfo = layer->GetOutputSlot(0).GetTensorInfo();
1856
1857 // Undo the reshape knowing the amount of dimensions added
1858 if (tensorDimensionsAdded == 1)
1859 {
1860 afterConcatInfo.SetShape(armnn::TensorShape({ afterConcatInfo.GetShape()[1],
1861 afterConcatInfo.GetShape()[2] }));
1862 }
1863 else if (tensorDimensionsAdded == 2)
1864 {
1865 afterConcatInfo.SetShape(armnn::TensorShape({ afterConcatInfo.GetShape()[2] }));
1866 }
1867
1868 layer = &AddReshapeLayer(
1869 *data.m_Network,
1870 layer->GetOutputSlot(0),
1871 afterConcatInfo
1872 );
1873 }
1874
1875 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
1876}
1877
1878template<typename HalPolicy,
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001879 typename HalOperation = typename HalPolicy::Operation,
1880 typename HalModel = typename HalPolicy::Model>
Mike Kellyb5fdf382019-06-11 16:35:25 +01001881bool ConvertConv2d(const HalOperation& operation, const HalModel& model, ConversionData& data)
1882{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001883 using HalOperand = typename HalPolicy::Operand;
1884 using HalOperandType = typename HalPolicy::OperandType;
1885
1886 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001887 if (!input.IsValid())
1888 {
1889 return Fail("%s: Operation has invalid inputs", __func__);
1890 }
1891
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001892 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001893 if (!output)
1894 {
1895 return Fail("%s: Could not read output 0", __func__);
1896 }
1897
1898 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001899 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001900
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01001901 if (IsDynamicTensor(outputInfo))
1902 {
1903 return Fail("%s: Dynamic output tensors are not supported", __func__);
1904 }
1905
Mike Kellyb5fdf382019-06-11 16:35:25 +01001906 // ArmNN does not currently support non-fixed weights or bias
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001907 const ConstTensorPin weightsPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 1, model, data);
1908 const ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001909
1910 if (!weightsPin.IsValid() || !biasPin.IsValid())
1911 {
1912 return Fail("%s: Operation has invalid inputs", __func__);
1913 }
1914
1915 armnn::ConstTensor weights = weightsPin.GetConstTensor();
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01001916 armnn::ConstTensor bias = biasPin.GetConstTensor();
Mike Kellyb5fdf382019-06-11 16:35:25 +01001917 SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo);
1918
1919 armnn::Convolution2dDescriptor desc;
1920 desc.m_DataLayout = armnn::DataLayout::NHWC;
1921 ActivationFn activation;
1922
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01001923 if (operation.inputs.size() == 10)
Mike Kellyb5fdf382019-06-11 16:35:25 +01001924 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001925 if (!GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadLeft, model, data) ||
1926 !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadRight, model, data) ||
1927 !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PadTop, model, data) ||
1928 !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_PadBottom, model, data) ||
1929 !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_StrideX, model, data) ||
1930 !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_StrideY, model, data) ||
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01001931 !GetInputActivationFunction<HalPolicy>(operation, 9, activation, model, data))
Mike Kellyb5fdf382019-06-11 16:35:25 +01001932 {
1933 return Fail("%s: Operation has invalid inputs", __func__);
1934 }
Mike Kellyb5fdf382019-06-11 16:35:25 +01001935 }
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01001936 else if (operation.inputs.size() == 7)
Mike Kellyb5fdf382019-06-11 16:35:25 +01001937 {
1938 android::nn::PaddingScheme paddingScheme;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001939 if (!GetInputPaddingScheme<HalPolicy>(operation, 3, paddingScheme, model, data) ||
1940 !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_StrideX, model, data) ||
1941 !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideY, model, data) ||
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01001942 !GetInputActivationFunction<HalPolicy>(operation, 6, activation, model, data))
Mike Kellyb5fdf382019-06-11 16:35:25 +01001943 {
1944 return Fail("%s: Operation has invalid inputs", __func__);
1945 }
1946
1947 const uint32_t kernelX = weights.GetShape()[2];
1948 const uint32_t kernelY = weights.GetShape()[1];
1949 const uint32_t inputX = inputInfo.GetShape()[2];
1950 const uint32_t inputY = inputInfo.GetShape()[1];
1951
1952 CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, paddingScheme);
1953 CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, paddingScheme);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001954 }
1955 else
1956 {
1957 return Fail("%s: Unsupported number of operation inputs", __func__);
1958 }
1959
1960 desc.m_BiasEnabled = true;
1961 armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo());
1962
Ferran Balaguerd30093c2019-07-09 17:04:47 +01001963 bool isSupported = false;
1964 FORWARD_LAYER_SUPPORT_FUNC(__func__,
1965 IsConvolution2dSupported,
1966 data.m_Backends,
1967 isSupported,
1968 inputInfo,
1969 outputInfo,
1970 desc,
1971 weights.GetInfo(),
1972 biases);
1973 if (!isSupported)
Mike Kellyb5fdf382019-06-11 16:35:25 +01001974 {
1975 return false;
1976 }
1977
1978 armnn::IConnectableLayer* startLayer =
1979 data.m_Network->AddConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias));
1980
1981 if (!startLayer)
1982 {
1983 return Fail("%s: AddConvolution2dLayer failed", __func__);
1984 }
1985
1986 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data);
1987
1988 if (!endLayer)
1989 {
1990 return Fail("%s: ProcessActivation failed", __func__);
1991 }
1992
1993 input.Connect(startLayer->GetInputSlot(0));
1994
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01001995 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01001996}
1997
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01001998template<typename HalPolicy,
1999 typename HalOperation = typename HalPolicy::Operation,
2000 typename HalModel = typename HalPolicy::Model>
Aron Virginas-Tar8edb16d2019-10-01 13:34:59 +01002001bool ConvertDepthToSpace(const HalOperation& operation, const HalModel& model, ConversionData& data)
2002{
2003 using HalOperand = typename HalPolicy::Operand;
2004 using HalOperandType = typename HalPolicy::OperandType;
2005
2006 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2007 if (!input.IsValid() )
2008 {
2009 return Fail("%s: Operation has invalid inputs", __func__);
2010 }
2011
2012 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2013 unsigned int rank = inputInfo.GetNumDimensions();
2014 if (rank != 4)
2015 {
2016 return Fail("%s: Only inputs with rank 4 are supported", __func__);
2017 }
2018
2019 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2020 if (!output)
2021 {
2022 return Fail("%s: Could not read output 0", __func__);
2023 }
2024
2025 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2026 if (IsDynamicTensor(outputInfo))
2027 {
2028 return Fail("%s: Dynamic output tensors are not supported", __func__);
2029 }
2030
2031 armnn::DepthToSpaceDescriptor descriptor;
2032
2033 GetInputScalar<HalPolicy>(operation, 1, HalOperandType::INT32, descriptor.m_BlockSize, model, data);
2034 if (descriptor.m_BlockSize <= 1)
2035 {
2036 return Fail("%s: Block size must be at least 1 in all dimensions");
2037 }
2038
2039 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
2040 if (Is12Operand(*output))
2041 {
2042 descriptor.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 2, model, data);
2043 }
2044
2045 bool isSupported = false;
2046 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2047 IsDepthToSpaceSupported,
2048 data.m_Backends,
2049 isSupported,
2050 inputInfo,
2051 outputInfo,
2052 descriptor);
2053 if (!isSupported)
2054 {
2055 return false;
2056 }
2057
2058 armnn::IConnectableLayer* const layer = data.m_Network->AddDepthToSpaceLayer(descriptor);
2059 assert(layer != nullptr);
2060 input.Connect(layer->GetInputSlot(0));
2061
2062 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2063}
2064
2065template<typename HalPolicy,
2066 typename HalOperation = typename HalPolicy::Operation,
2067 typename HalModel = typename HalPolicy::Model>
Mike Kellyb5fdf382019-06-11 16:35:25 +01002068bool ConvertDepthwiseConv2d(const HalOperation& operation, const HalModel& model, ConversionData& data)
2069{
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01002070 using HalOperand = typename HalPolicy::Operand;
2071 using HalOperandType = typename HalPolicy::OperandType;
2072
2073 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01002074
2075 if (!input.IsValid())
2076 {
2077 return Fail("%s: Operation has invalid inputs", __func__);
2078 }
2079
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01002080 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
Mike Kellyb5fdf382019-06-11 16:35:25 +01002081
2082 if (!output)
2083 {
2084 return Fail("%s: Could not read output 0", __func__);
2085 }
2086
2087 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002088 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Mike Kellyb5fdf382019-06-11 16:35:25 +01002089
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002090 if (IsDynamicTensor(outputInfo))
2091 {
2092 return Fail("%s: Dynamic output tensors are not supported", __func__);
2093 }
Mike Kellyb5fdf382019-06-11 16:35:25 +01002094
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002095 // ArmNN does not currently support non-fixed weights or bias
Mike Kellyb5fdf382019-06-11 16:35:25 +01002096 // Find the shape of the weights tensor. In AndroidNN this will be [ 1, H, W, I * M ]
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01002097 const HalOperand* weightsOperand = GetInputOperand<HalPolicy>(operation, 1, model);
Mike Kellyb5fdf382019-06-11 16:35:25 +01002098
2099 if (weightsOperand == nullptr)
2100 {
2101 return Fail("%s: Operand is invalid", __func__);
2102 }
2103 armnn::DepthwiseConvolution2dDescriptor desc;
2104 desc.m_DataLayout = armnn::DataLayout::NHWC;
2105
Mike Kellyb5fdf382019-06-11 16:35:25 +01002106 // Reinterpret weight data as [ H, W, I, M ]
2107 armnn::TensorShape weightsShape({ weightsOperand->dimensions[1],
2108 weightsOperand->dimensions[2],
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002109 inputInfo.GetShape()[3],
2110 weightsOperand->dimensions[3] / inputInfo.GetShape()[3] });
Mike Kellyb5fdf382019-06-11 16:35:25 +01002111
2112 // Swizzle weight data [ H, W, I, M ] -> [ M, I, H, W ]
2113 const armnn::PermutationVector HWIMToMIHW = { 2U, 3U, 1U, 0U };
2114
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01002115 const ConstTensorPin weightsPin =
2116 ConvertOperationInputToConstTensorPin<HalPolicy>(operation,
2117 1,
2118 model,
2119 data,
2120 HWIMToMIHW,
2121 &weightsShape);
Mike Kellyb5fdf382019-06-11 16:35:25 +01002122
2123 // Bias is a 1D tensor
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01002124 const ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data);
Mike Kellyb5fdf382019-06-11 16:35:25 +01002125
2126 if (!weightsPin.IsValid() || !biasPin.IsValid())
2127 {
2128 return Fail("%s: Operation has invalid inputs", __func__);
2129 }
2130
2131 armnn::ConstTensor weights = weightsPin.GetConstTensor();
2132 armnn::ConstTensor bias = biasPin.GetConstTensor();
2133 SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), inputInfo);
2134
2135 ActivationFn activation;
2136
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002137 if (operation.inputs.size() == 11)
Mike Kellyb5fdf382019-06-11 16:35:25 +01002138 {
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01002139 if (!GetInputScalar<HalPolicy>(operation, 3, HalOperandType::INT32, desc.m_PadLeft, model, data) ||
2140 !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_PadRight, model, data) ||
2141 !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_PadTop, model, data) ||
2142 !GetInputScalar<HalPolicy>(operation, 6, HalOperandType::INT32, desc.m_PadBottom, model, data) ||
2143 !GetInputScalar<HalPolicy>(operation, 7, HalOperandType::INT32, desc.m_StrideX, model, data) ||
2144 !GetInputScalar<HalPolicy>(operation, 8, HalOperandType::INT32, desc.m_StrideY, model, data) ||
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002145 !GetInputActivationFunction<HalPolicy>(operation, 10, activation, model, data))
Mike Kellyb5fdf382019-06-11 16:35:25 +01002146 {
2147 return Fail("%s: Operation has invalid inputs", __func__);
2148 }
2149 }
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002150 else if (operation.inputs.size() == 8)
Mike Kellyb5fdf382019-06-11 16:35:25 +01002151 {
2152 android::nn::PaddingScheme paddingScheme;
Aron Virginas-Tarcd700e42019-06-14 14:54:52 +01002153 if (!GetInputPaddingScheme<HalPolicy>(operation, 3, paddingScheme, model, data) ||
2154 !GetInputScalar<HalPolicy>(operation, 4, HalOperandType::INT32, desc.m_StrideX, model, data) ||
2155 !GetInputScalar<HalPolicy>(operation, 5, HalOperandType::INT32, desc.m_StrideY, model, data) ||
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002156 !GetInputActivationFunction<HalPolicy>(operation, 7, activation, model, data))
Mike Kellyb5fdf382019-06-11 16:35:25 +01002157 {
2158 return Fail("%s: Operation has invalid inputs", __func__);
2159 }
2160
2161 const uint32_t kernelX = weights.GetShape()[3];
2162 const uint32_t kernelY = weights.GetShape()[2];
Aron Virginas-Tara5e2a452019-07-29 16:13:19 +01002163 const uint32_t inputX = inputInfo.GetShape()[2];
2164 const uint32_t inputY = inputInfo.GetShape()[1];
Mike Kellyb5fdf382019-06-11 16:35:25 +01002165
2166 CalcPadding(inputX, kernelX, desc.m_StrideX, desc.m_PadLeft, desc.m_PadRight, paddingScheme);
2167 CalcPadding(inputY, kernelY, desc.m_StrideY, desc.m_PadTop, desc.m_PadBottom, paddingScheme);
2168 }
2169 else
2170 {
2171 return Fail("%s: Unsupported number of operation inputs", __func__);
2172 }
2173
2174 desc.m_BiasEnabled = true;
2175 armnn::Optional<armnn::TensorInfo> biases(bias.GetInfo());
2176
Ferran Balaguerd30093c2019-07-09 17:04:47 +01002177 bool isSupported = false;
2178 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2179 IsDepthwiseConvolutionSupported,
2180 data.m_Backends,
2181 isSupported,
2182 inputInfo,
2183 outputInfo,
2184 desc,
2185 weights.GetInfo(),
2186 biases);
2187 if (!isSupported)
Mike Kellyb5fdf382019-06-11 16:35:25 +01002188 {
2189 return false;
2190 }
2191
2192 armnn::IConnectableLayer* startLayer =
2193 data.m_Network->AddDepthwiseConvolution2dLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias));
2194 if (!startLayer)
2195 {
2196 return Fail("%s: AddDepthwiseConvolution2dLayer failed", __func__);
2197 }
2198
2199 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activation, startLayer, data);
2200 if (!endLayer)
2201 {
2202 return Fail("%s: ProcessActivation failed", __func__);
2203 }
2204
2205 input.Connect(startLayer->GetInputSlot(0));
2206
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002207 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
arovir01b0717b52018-09-05 17:03:25 +01002208}
2209
Mike Kelly3c673942019-07-25 09:26:06 +01002210template<typename HalPolicy,
Mike Kelly46272802019-08-14 17:00:48 +01002211 typename Operation = typename HalPolicy::Operation,
2212 typename Model = typename HalPolicy::Model>
2213bool ConvertDequantize(const Operation& operation, const Model& model, ConversionData& data)
Mike Kelly3c673942019-07-25 09:26:06 +01002214{
Mike Kelly46272802019-08-14 17:00:48 +01002215 using Operand = typename HalPolicy::Operand;
2216
2217 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2218 if (!input.IsValid())
2219 {
2220 return Fail("%s: Operation has invalid input", __func__);
2221 }
2222
2223 const Operand* const outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2224 if (!outputOperand)
2225 {
2226 return Fail("%s: Operation has invalid outputs", __func__);
2227 }
2228
2229 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
2230 if (IsDynamicTensor(outputInfo))
2231 {
2232 return Fail("%s: Dynamic output tensors are not supported", __func__);
2233 }
2234
2235 bool isSupported = false;
2236 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2237 IsDequantizeSupported,
2238 data.m_Backends,
2239 isSupported,
2240 input.GetTensorInfo(),
2241 GetTensorInfoForOperand(*outputOperand));
2242 if (!isSupported)
2243 {
2244 return false;
2245 }
2246
2247 armnn::IConnectableLayer* const layer = data.m_Network->AddDequantizeLayer();
2248 assert(layer != nullptr);
2249 input.Connect(layer->GetInputSlot(0));
2250
2251 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2252}
2253
2254template<typename HalPolicy,
2255 typename Operation = typename HalPolicy::Operation,
2256 typename Model = typename HalPolicy::Model>
2257bool ConvertDiv(const Operation& operation, const Model& model, ConversionData& data)
2258{
2259 using Operand = typename HalPolicy::Operand;
2260
2261 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2262 LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data);
2263
2264 if (!input0.IsValid() || !input1.IsValid())
2265 {
2266 return Fail("%s: Operation has invalid inputs", __func__);
2267 }
2268
2269 // The FuseActivation parameter is always the input index 2
2270 // and it should be optional
2271 ActivationFn activationFunction;
2272 if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data))
2273 {
2274 return Fail("%s: Operation has invalid inputs", __func__);
2275 }
2276
2277 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2278 if (!output)
2279 {
2280 return Fail("%s: Could not read output 0", __func__);
2281 }
2282
2283 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2284 if (IsDynamicTensor(outputInfo))
2285 {
2286 return Fail("%s: Dynamic output tensors are not supported", __func__);
2287 }
2288
2289 bool isSupported = false;
2290 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2291 IsDivisionSupported,
2292 data.m_Backends,
2293 isSupported,
2294 input0.GetTensorInfo(),
2295 input1.GetTensorInfo(),
2296 outputInfo);
2297 if (!isSupported)
2298 {
2299 return false;
2300 }
2301
2302 armnn::IConnectableLayer* const startLayer = data.m_Network->AddDivisionLayer();
2303 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
2304
2305 if (endLayer)
2306 {
Sadik Armagan64b19b52019-08-19 09:49:58 +01002307 bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data);
2308 if (!isReshapeSupported)
2309 {
2310 return false;
2311 }
2312
Mike Kelly46272802019-08-14 17:00:48 +01002313 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
2314 }
2315 return Fail("%s: ProcessActivation failed", __func__);
2316}
2317
2318template<typename HalPolicy,
2319 typename Operation = typename HalPolicy::Operation,
2320 typename Model = typename HalPolicy::Model>
2321bool ConvertFloor(const Operation& operation, const Model& model, ConversionData& data)
2322{
2323 using Operand = typename HalPolicy::Operand;
2324
2325 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2326 if (!input.IsValid())
2327 {
2328 return Fail("%s: Operation has invalid inputs", __func__);
2329 }
2330
2331 const Operand* const outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2332 if (!outputOperand)
2333 {
2334 return Fail("%s: Operation has invalid outputs", __func__);
2335 }
2336
2337 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
2338 if (IsDynamicTensor(outputInfo))
2339 {
2340 return Fail("%s: Dynamic output tensors are not supported", __func__);
2341 }
2342
2343 bool isSupported = false;
2344 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2345 IsFloorSupported,
2346 data.m_Backends,
2347 isSupported,
2348 input.GetTensorInfo(),
2349 outputInfo);
2350 if (!isSupported)
2351 {
2352 return false;
2353 }
2354
2355 armnn::IConnectableLayer* layer = data.m_Network->AddFloorLayer();
2356 assert(layer != nullptr);
2357 input.Connect(layer->GetInputSlot(0));
2358
2359 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2360}
2361
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002362inline bool IsQSymm8(const V1_0::Operand&)
2363{
2364 return false;
2365}
2366
2367#ifdef ARMNN_ANDROID_NN_V1_2
2368
2369inline bool IsQSymm8(const V1_2::Operand& operand)
2370{
2371 return operand.type == V1_2::OperandType::TENSOR_QUANT8_SYMM;
2372}
2373
2374#endif
2375
2376template<typename HalPolicy,
2377 typename Operation = typename HalPolicy::Operation,
2378 typename Model = typename HalPolicy::Model>
Sadik Armaganac23b032019-11-18 17:11:21 +00002379std::tuple<std::unique_ptr<float[]>, size_t, armnn::TensorInfo, int>
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002380DequantizeIfRequired(size_t operand_index, const Operation& operation, const Model& model, const ConversionData& data)
2381{
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002382 using HalOperand = typename HalPolicy::Operand;
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002383
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002384 const HalOperand* weightsOperand = GetInputOperand<HalPolicy>(operation, operand_index, model);
Sadik Armaganac23b032019-11-18 17:11:21 +00002385 if (!weightsOperand)
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002386 {
Sadik Armaganac23b032019-11-18 17:11:21 +00002387 // Invalid Operand will return with error code '-1'
2388 return { nullptr, 0, armnn::TensorInfo(), -1 };
2389 }
2390
2391 if (IsOperandConstant<HalPolicy>(*weightsOperand))
2392 {
2393 // Weights are already constant
2394 return { nullptr, 0, armnn::TensorInfo(), 0 };
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002395 }
2396
2397 const size_t weightsInputIndex = operation.inputs[operand_index];
2398
2399 // The weights are a non const tensor, this indicates they might be the output of a dequantize op.
2400 // Iterate over the nodes and find the previous operation which should be DEQUANTIZE
2401 for (uint32_t operationIdx = 0; operationIdx < model.operations.size(); ++operationIdx)
2402 {
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002403 // Search for the DEQUANTIZE op which has the operand with index equal to operandIndex
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002404 const auto& operationIt = model.operations[operationIdx];
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002405 if (operationIt.type != HalPolicy::OperationType::DEQUANTIZE)
2406 {
2407 continue;
2408 }
2409
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002410 size_t outOpIndex = weightsInputIndex + 1;
2411 for (size_t i = 0; outOpIndex != weightsInputIndex && i < operationIt.outputs.size(); ++i)
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002412 {
2413 outOpIndex = operationIt.outputs[i];
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002414 }
2415
2416 if (outOpIndex != weightsInputIndex)
2417 {
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002418 continue;
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002419 }
2420
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002421 const HalOperand* operand = GetInputOperand<HalPolicy>(operationIt, 0, model);
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002422 BOOST_ASSERT(operand);
2423
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002424 if (!IsQSymm8(*operand))
2425 {
2426 // Only supporting dequantize from QSYMM8 to FLOAT
2427 break;
2428 }
2429
2430 // Allocate a new buffer for the dequantized data and manually dequantize
2431 const void* startValue = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data);
2432 if (!startValue)
2433 {
2434 // Failed to get the operand address
2435 break;
2436 }
2437
2438 const uint8_t* quantizedBuffer = reinterpret_cast<const uint8_t*>(startValue);
2439 size_t dequantizedBufferLength = operand->location.length;
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002440 const float quantizationScale = operand->scale;
2441
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002442 auto dequantizedBuffer = std::make_unique<float[]>(dequantizedBufferLength + 1);
2443 for (size_t i = 0; i < dequantizedBufferLength; ++i)
2444 {
2445 float* dstPtr = dequantizedBuffer.get();
2446 BOOST_ASSERT(dstPtr);
2447 *dstPtr++ = quantizedBuffer[i] * quantizationScale;
2448 }
2449
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002450 // Construct tensor info for dequantized ConstTensor
2451 armnn::TensorInfo tensorInfo(operand->dimensions.size(),
2452 operand->dimensions.data(),
2453 armnn::DataType::Float32);
2454
Sadik Armaganac23b032019-11-18 17:11:21 +00002455 return { std::move(dequantizedBuffer), dequantizedBufferLength * sizeof(float), std::move(tensorInfo), 0 };
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002456 }
2457
Sadik Armaganac23b032019-11-18 17:11:21 +00002458 return { nullptr, 0, armnn::TensorInfo() , 0};
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002459}
2460
2461template<typename HalPolicy,
2462 typename Operation = typename HalPolicy::Operation,
2463 typename Model = typename HalPolicy::Model>
2464ConstTensorPin DequantizeAndMakeConstTensorPin(const Operation& operation,
2465 const Model& model,
2466 const ConversionData& data,
2467 size_t operandIndex,
2468 bool optional = false)
2469{
2470 auto dequantized = DequantizeIfRequired<HalPolicy, Operation, Model>(operandIndex,operation, model, data);
Sadik Armaganac23b032019-11-18 17:11:21 +00002471 if (std::get<3>(dequantized) == -1)
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002472 {
Sadik Armaganac23b032019-11-18 17:11:21 +00002473 // Return it as invalid, tensor with no values is not really an error
2474 return ConstTensorPin();
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002475 }
2476
Sadik Armaganac23b032019-11-18 17:11:21 +00002477 if (std::get<1>(dequantized) == 0)
2478 {
2479 return ConvertOperationInputToConstTensorPin<HalPolicy>(
2480 operation, operandIndex, model, data, g_DontPermute, nullptr, optional);
2481
2482 }
2483
2484 return ConstTensorPin(std::get<2>(dequantized), std::get<0>(dequantized).get(),
2485 std::get<1>(dequantized), g_DontPermute);
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002486}
2487
2488
Mike Kelly46272802019-08-14 17:00:48 +01002489template<typename HalPolicy,
2490 typename Operation = typename HalPolicy::Operation,
2491 typename Model = typename HalPolicy::Model>
2492bool ConvertFullyConnected(const Operation& operation, const Model& model, ConversionData& data)
2493{
2494 using Operand = typename HalPolicy::Operand;
Mike Kelly46272802019-08-14 17:00:48 +01002495 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2496 if (!input.IsValid())
2497 {
2498 return Fail("%s: Operation has invalid inputs", __func__);
2499 }
2500
2501 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2502 if (!output)
2503 {
2504 return Fail("%s: Could not read output 0", __func__);
2505 }
2506
2507 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2508 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2509
2510 if (IsDynamicTensor(outputInfo))
2511 {
2512 return Fail("%s: Dynamic output tensors are not supported", __func__);
2513 }
2514
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002515 ConstTensorPin weightsPin = DequantizeAndMakeConstTensorPin<HalPolicy>(operation, model, data, 1);
2516 ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data); // 1D
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002517
2518 if (!weightsPin.IsValid())
Mike Kelly46272802019-08-14 17:00:48 +01002519 {
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002520 return Fail("%s: Operation has invalid weights", __func__);
2521 }
2522
2523 if (!biasPin.IsValid())
2524 {
2525 return Fail("%s: Operation has invalid bias", __func__);
Mike Kelly46272802019-08-14 17:00:48 +01002526 }
2527
2528 armnn::ConstTensor weights = weightsPin.GetConstTensor();
2529 armnn::ConstTensor bias = biasPin.GetConstTensor();
2530 armnn::TensorInfo reshapedInfo = inputInfo;
2531
2532 try
2533 {
2534 reshapedInfo.SetShape(FlattenFullyConnectedInput(inputInfo.GetShape(), weights.GetInfo().GetShape()));
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002535 }
2536 catch (const std::exception& e)
2537 {
Mike Kelly46272802019-08-14 17:00:48 +01002538 return Fail("%s: %s", __func__, e.what());
2539 }
2540
2541 // ensuring that the bias value is within 1% of the weights input (small float differences can exist)
2542 SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), reshapedInfo);
2543
2544 ActivationFn activationFunction;
2545 if (!GetInputActivationFunction<HalPolicy>(operation, 3, activationFunction, model, data))
2546 {
2547 return Fail("%s: Operation has invalid inputs", __func__);
2548 }
2549
2550 armnn::FullyConnectedDescriptor desc;
2551 desc.m_TransposeWeightMatrix = true;
2552 desc.m_BiasEnabled = true;
2553
2554 bool isSupported = false;
2555 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2556 IsFullyConnectedSupported,
2557 data.m_Backends,
2558 isSupported,
2559 reshapedInfo,
2560 outputInfo,
2561 weights.GetInfo(),
2562 bias.GetInfo(),
2563 desc);
2564 if (!isSupported)
2565 {
2566 return false;
2567 }
2568
2569 armnn::IConnectableLayer* startLayer =
2570 data.m_Network->AddFullyConnectedLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias));
2571 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
2572
2573 if (endLayer != nullptr)
2574 {
2575 if (inputInfo.GetNumDimensions() > 2U)
2576 {
2577 armnn::ReshapeDescriptor reshapeDescriptor;
2578 reshapeDescriptor.m_TargetShape = reshapedInfo.GetShape();
2579
2580 armnn::IConnectableLayer* reshapeLayer = data.m_Network->AddReshapeLayer(reshapeDescriptor);
2581 assert(reshapeLayer != nullptr);
2582 input.Connect(reshapeLayer->GetInputSlot(0));
2583 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedInfo);
2584 reshapeLayer->GetOutputSlot(0).Connect(startLayer->GetInputSlot(0));
2585 }
2586 else
2587 {
2588 input.Connect(startLayer->GetInputSlot(0));
2589 }
2590
2591 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
2592 }
2593 else
2594 {
2595 return Fail("%s: ProcessActivation failed", __func__);
2596 }
2597}
2598
2599template<typename HalPolicy,
2600 typename Operation = typename HalPolicy::Operation,
2601 typename Model = typename HalPolicy::Model>
2602bool ConvertL2Normalization(const Operation& operation, const Model& model, ConversionData& data)
2603{
Mike Kelly999e2092019-08-15 10:46:46 +01002604 if (operation.inputs.size() != 1)
2605 {
2606 return Fail("%s: Optional inputs are not supported", __func__);
2607 }
2608
Mike Kelly46272802019-08-14 17:00:48 +01002609 using Operand = typename HalPolicy::Operand;
2610
2611 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2612 if (!input.IsValid())
2613 {
2614 return Fail("%s: Operation has invalid inputs", __func__);
2615 }
2616
2617 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2618 if (!output)
2619 {
2620 return Fail("%s: Could not read output 0", __func__);
2621 }
2622
2623 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2624 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2625
2626 if (IsDynamicTensor(outputInfo))
2627 {
2628 return Fail("%s: Dynamic output tensors are not supported", __func__);
2629 }
2630 if (outputInfo.GetNumDimensions() != 4u)
2631 {
2632 return Fail("%s: Tensor Rank other than 4 is not supported", __func__);
2633 }
2634
2635 armnn::L2NormalizationDescriptor desc;
2636 desc.m_DataLayout = armnn::DataLayout::NHWC;
2637
2638 bool isSupported = false;
2639 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2640 IsL2NormalizationSupported,
2641 data.m_Backends,
2642 isSupported,
2643 inputInfo,
2644 outputInfo,
2645 desc);
2646 if (!isSupported)
2647 {
2648 return false;
2649 }
2650
2651 armnn::IConnectableLayer* layer = data.m_Network->AddL2NormalizationLayer(desc);
2652 assert(layer != nullptr);
2653 input.Connect(layer->GetInputSlot(0));
2654
2655 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2656}
2657
2658template<typename HalPolicy,
2659 typename Operation = typename HalPolicy::Operation,
2660 typename Model = typename HalPolicy::Model>
2661bool ConvertLocalResponseNormalization(const Operation& operation,
2662 const Model& model,
2663 ConversionData& data)
2664{
Mike Kelly999e2092019-08-15 10:46:46 +01002665 if (operation.inputs.size() != 5)
2666 {
2667 return Fail("%s: Optional inputs are not supported", __func__);
2668 }
2669
Mike Kelly46272802019-08-14 17:00:48 +01002670 using Operand = typename HalPolicy::Operand;
2671 using OperandType = typename HalPolicy::OperandType;
2672
2673 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2674 if (!input.IsValid())
2675 {
2676 return Fail("%s: Operation has invalid inputs", __func__);
2677 }
2678
2679 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2680 if (!output)
2681 {
2682 return Fail("%s: Could not read output 0", __func__);
2683 }
2684
2685 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2686 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2687
2688 if (IsDynamicTensor(outputInfo))
2689 {
2690 return Fail("%s: Dynamic output tensors are not supported", __func__);
2691 }
2692 if (outputInfo.GetNumDimensions() != 4u)
2693 {
2694 return Fail("%s: Tensor Rank other than 4 is not supported", __func__);
2695 }
2696
2697 armnn::NormalizationDescriptor descriptor;
2698 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
2699 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
2700 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
2701
2702 if (!input.IsValid() ||
2703 !GetInputScalar<HalPolicy>(operation, 1, OperandType::INT32, descriptor.m_NormSize, model, data) ||
2704 !GetInputFloat32<HalPolicy>(operation, 2, descriptor.m_K, model, data) ||
2705 !GetInputFloat32<HalPolicy>(operation, 3, descriptor.m_Alpha, model, data) ||
2706 !GetInputFloat32<HalPolicy>(operation, 4, descriptor.m_Beta, model, data))
2707 {
2708 return Fail("%s: Operation has invalid inputs", __func__);
2709 }
2710
2711 // ArmNN expects normSize to be the full size of the normalization
2712 // window rather than the radius as in AndroidNN.
2713 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
2714
2715 bool isSupported = false;
2716 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2717 IsNormalizationSupported,
2718 data.m_Backends,
2719 isSupported,
2720 inputInfo,
2721 outputInfo,
2722 descriptor);
2723 if (!isSupported)
2724 {
2725 return false;
2726 }
2727
2728
2729 armnn::IConnectableLayer* layer = data.m_Network->AddNormalizationLayer(descriptor);
2730 assert(layer != nullptr);
2731 input.Connect(layer->GetInputSlot(0));
2732
2733 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2734}
2735
2736template<typename HalPolicy,
2737 typename Operation = typename HalPolicy::Operation,
2738 typename Model = typename HalPolicy::Model>
2739bool ConvertLogistic(const Operation& operation, const Model& model, ConversionData& data)
2740{
2741 using Operand = typename HalPolicy::Operand;
2742
2743 armnn::ActivationDescriptor desc;
2744 desc.m_Function = armnn::ActivationFunction::Sigmoid;
2745
2746 return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data);
2747}
2748
2749template<typename HalPolicy,
2750 typename Operation = typename HalPolicy::Operation,
2751 typename Model = typename HalPolicy::Model>
2752bool ConvertMean(const Operation& operation, const Model& model, ConversionData& data)
2753{
2754 using Operand = typename HalPolicy::Operand;
2755
2756 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2757 if (!input.IsValid())
2758 {
2759 return Fail("%s: Operation has invalid inputs", __func__);
2760 }
2761
2762 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2763 if (!output)
2764 {
2765 return Fail("%s: Could not read output 0", __func__);
2766 }
2767
2768 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2769 if (IsDynamicTensor(outputInfo))
2770 {
2771 return Fail("%s: Dynamic output tensors are not supported", __func__);
2772 }
2773
2774 const Operand* axisOperand = GetInputOperand<HalPolicy>(operation, 1, model);
2775 if (!axisOperand)
2776 {
2777 return Fail("%s: Could not read input 1", __func__);
2778 }
2779
2780 std::vector<int32_t> axis;
2781 if (!GetTensorInt32Values<HalPolicy>(*axisOperand, axis, model, data))
2782 {
2783 return Fail("%s: Input 1 has invalid values", __func__);
2784 }
2785
2786 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2787
2788 // Convert the axis to unsigned int and remove duplicates.
2789 unsigned int rank = inputInfo.GetNumDimensions();
2790 std::set<unsigned int> uniqueAxis;
2791 std::transform(axis.begin(), axis.end(),
2792 std::inserter(uniqueAxis, uniqueAxis.begin()),
2793 [rank](int i) -> unsigned int { return (i + rank) % rank; });
2794
2795 // Get the "keep dims" flag.
2796 int32_t keepDims = 0;
2797 if (!GetInputInt32<HalPolicy>(operation, 2, keepDims, model, data))
2798 {
2799 return Fail("%s: Could not read input 2", __func__);
2800 }
2801
2802 armnn::MeanDescriptor descriptor;
2803 descriptor.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
2804 descriptor.m_KeepDims = keepDims > 0;
2805
2806 bool isSupported = false;
2807 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2808 IsMeanSupported,
2809 data.m_Backends,
2810 isSupported,
2811 inputInfo,
2812 outputInfo,
2813 descriptor);
2814 if (!isSupported)
2815 {
2816 return false;
2817 }
2818
2819 armnn::IConnectableLayer* const layer = data.m_Network->AddMeanLayer(descriptor);
2820 assert(layer != nullptr);
2821 input.Connect(layer->GetInputSlot(0));
2822
2823 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2824}
2825
2826template<typename HalPolicy,
2827 typename Operation = typename HalPolicy::Operation,
2828 typename Model = typename HalPolicy::Model>
2829bool ConvertMul(const Operation& operation, const Model& model, ConversionData& data)
2830{
2831 using Operand = typename HalPolicy::Operand;
2832
2833 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2834 LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data);
2835
2836 if (!input0.IsValid() || !input1.IsValid())
2837 {
2838 return Fail("%s: Operation has invalid inputs", __func__);
2839 }
2840
2841 // The FuseActivation parameter is always the input index 2
2842 // and it should be optional
2843 ActivationFn activationFunction;
2844 if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data))
2845 {
2846 return Fail("%s: Operation has invalid inputs", __func__);
2847 }
2848
2849 const Operand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2850
2851 if (outputOperand == nullptr)
2852 {
2853 return false;
2854 }
2855
2856 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
2857 if (IsDynamicTensor(outputInfo))
2858 {
2859 return Fail("%s: Dynamic output tensors are not supported", __func__);
2860 }
2861
2862 bool isSupported = false;
2863 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2864 IsMultiplicationSupported,
2865 data.m_Backends,
2866 isSupported,
2867 input0.GetTensorInfo(),
2868 input1.GetTensorInfo(),
2869 outputInfo);
2870 if (!isSupported)
2871 {
2872 return false;
2873 }
2874
2875 armnn::IConnectableLayer* const startLayer = data.m_Network->AddMultiplicationLayer();
2876 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
2877
2878 const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo();
2879 const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo();
2880
2881 if (endLayer != nullptr)
2882 {
Sadik Armagan64b19b52019-08-19 09:49:58 +01002883 bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data);
2884 if (!isReshapeSupported)
2885 {
2886 return false;
2887 }
2888
Mike Kelly46272802019-08-14 17:00:48 +01002889 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
2890 }
2891 else
2892 {
2893 return Fail("%s: ProcessActivation failed", __func__);
2894 }
2895}
2896
2897template<typename HalPolicy,
2898 typename Operation = typename HalPolicy::Operation,
2899 typename Model = typename HalPolicy::Model>
2900bool ConvertPad(Operation& operation, const Model& model, ConversionData& data)
2901{
2902 using Operand = typename HalPolicy::Operand;
2903
Mike Kelly3c673942019-07-25 09:26:06 +01002904 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2905 if (!input.IsValid())
2906 {
2907 return Fail("%s: Operation has invalid inputs", __func__);
2908 }
2909
2910 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2911 unsigned int rank = inputInfo.GetNumDimensions();
2912
2913 armnn::PadDescriptor descriptor;
2914 if (!ConvertPaddings<HalPolicy>(operation, model, data, rank, descriptor))
2915 {
2916 return Fail("%s: Could not convert paddings", __func__);
2917 }
2918
2919 // Before Android Q, the pad value for ANEURALNETWORKS_TENSOR_QUANT8_ASYMM was undefined. Since Android Q the pad
2920 // value must be "logical zero" we set it to be equal to the QuantizationOffset so effectively it ends up as
2921 // (QuantizationOffset - QuantizationOffset) * scale = 0.
2922 if (inputInfo.GetDataType() == armnn::DataType::QuantisedAsymm8)
2923 {
2924 descriptor.m_PadValue = inputInfo.GetQuantizationOffset();
2925 }
2926
Mike Kelly46272802019-08-14 17:00:48 +01002927 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
Mike Kelly3c673942019-07-25 09:26:06 +01002928 if (!output)
2929 {
2930 return Fail("%s: Could not read output", __func__);
2931 }
2932
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002933 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Mike Kelly3c673942019-07-25 09:26:06 +01002934 if (IsDynamicTensor(outputInfo))
2935 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002936 return Fail("%s: Dynamic output tensors are not supported", __func__);
Mike Kelly3c673942019-07-25 09:26:06 +01002937 }
2938
2939 bool isSupported = false;
2940 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2941 IsPadSupported,
2942 data.m_Backends,
2943 isSupported,
2944 inputInfo,
2945 outputInfo,
2946 descriptor);
2947 if (!isSupported)
2948 {
2949 return false;
2950 }
2951
2952 armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor);
2953 assert(layer != nullptr);
2954 input.Connect(layer->GetInputSlot(0));
2955 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
2956
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002957 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
Mike Kelly3c673942019-07-25 09:26:06 +01002958}
2959
Mike Kelly0a879362019-07-29 16:56:31 +01002960template<typename HalPolicy,
2961 typename Operation = typename HalPolicy::Operation,
Mike Kelly46272802019-08-14 17:00:48 +01002962 typename Model = typename HalPolicy::Model>
2963bool ConvertReshape(const Operation& operation, const Model& model, ConversionData& data)
2964{
2965 using Operand = typename HalPolicy::Operand;
2966
2967 const Operand* inputOperand = GetInputOperand<HalPolicy>(operation, 0, model);
2968 const Operand* requestedShapeOperand = GetInputOperand<HalPolicy>(operation, 1, model);
2969 const Operand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2970
2971 if (inputOperand == nullptr
2972 || requestedShapeOperand == nullptr
2973 || outputOperand == nullptr)
2974 {
2975 return Fail("%s: Operation has invalid inputs", __func__);
2976 }
2977
2978 if (requestedShapeOperand->dimensions.size() != 1)
2979 {
2980 return Fail("%s: Input 1 expected to be one-dimensional (found %i dimensions)",
2981 __func__, requestedShapeOperand->dimensions.size());
2982 }
2983
2984 std::vector<int32_t> targetDimensions;
2985 if (!GetTensorInt32Values<HalPolicy>(*requestedShapeOperand, targetDimensions, model, data))
2986 {
2987 return Fail("%s: Could not read values of input 1", __func__);
2988 }
2989
2990 const Shape inputOperandShape = GetOperandShape(*inputOperand);
2991
2992 Shape requestedShape;
2993 // targetDimensions may contain special values (e.g. -1). reshapePrepare() is an AndroidNN provided utility
2994 // function that resolves these values into a fully specified tensor shape.
2995 if (!reshapePrepare(inputOperandShape, targetDimensions.data(), targetDimensions.size(), &requestedShape))
2996 {
2997 return Fail("%s: Failed to resolve the requested shape", __func__);
2998 }
2999
3000 const Shape outputOperandShape = GetOperandShape(*outputOperand);
3001 if (!SameShape(requestedShape, outputOperandShape))
3002 {
3003 return Fail("%s: Shape of output operand does not match resolved requested shape", __func__);
3004 }
3005
3006 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3007 if (!input.IsValid())
3008 {
3009 return Fail("%s: Could not read input 0", __func__);
3010 }
3011
3012 armnn::ReshapeDescriptor reshapeDescriptor;
3013 reshapeDescriptor.m_TargetShape = armnn::TensorShape(requestedShape.dimensions.size(),
3014 requestedShape.dimensions.data());
3015
3016 bool isSupported = false;
3017 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3018 IsReshapeSupported,
3019 data.m_Backends,
3020 isSupported,
3021 input.GetTensorInfo(),
3022 reshapeDescriptor);
3023 if (!isSupported)
3024 {
3025 return false;
3026 }
3027
3028 armnn::IConnectableLayer* layer = data.m_Network->AddReshapeLayer(reshapeDescriptor);
3029 assert(layer != nullptr);
3030 input.Connect(layer->GetInputSlot(0));
3031
3032 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3033}
3034
3035template<typename HalPolicy,
3036 typename Operation = typename HalPolicy::Operation,
Mike Kelly0a879362019-07-29 16:56:31 +01003037 typename Model = typename HalPolicy::Model>
3038bool ConvertSub(const Operation& operation, const Model& model, ConversionData& data)
3039{
Mike Kelly46272802019-08-14 17:00:48 +01003040 using Operand = typename HalPolicy::Operand;
3041
Mike Kelly0a879362019-07-29 16:56:31 +01003042 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3043 LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data);
3044
3045 if (!input0.IsValid() || !input1.IsValid())
3046 {
3047 return Fail("%s: Operation has invalid inputs", __func__);
3048 }
3049
3050 // The FuseActivation parameter is always the input index 2
3051 // and it should be optional
3052 ActivationFn activationFunction;
3053 if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data))
3054 {
3055 return Fail("%s: Operation has invalid inputs", __func__);
3056 }
3057
3058 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3059 if (!output)
3060 {
3061 return Fail("%s: Could not read output 0", __func__);
3062 }
3063
3064 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3065 if (IsDynamicTensor(outputInfo))
3066 {
3067 return Fail("%s: Dynamic output tensors are not supported", __func__);
3068 }
3069
3070 bool isSupported = false;
3071 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3072 IsSubtractionSupported,
3073 data.m_Backends,
3074 isSupported,
3075 input0.GetTensorInfo(),
3076 input1.GetTensorInfo(),
3077 outputInfo);
3078 if (!isSupported)
3079 {
3080 return false;
3081 }
3082
3083 armnn::IConnectableLayer* const startLayer = data.m_Network->AddSubtractionLayer();
3084 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
3085
3086 const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo();
3087 const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo();
3088
3089 if (endLayer)
3090 {
Sadik Armagan64b19b52019-08-19 09:49:58 +01003091 bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data);
3092 if (!isReshapeSupported)
3093 {
3094 return false;
3095 }
Mike Kelly0a879362019-07-29 16:56:31 +01003096 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
3097 }
3098
3099 return Fail("%s: ProcessActivation failed", __func__);
3100}
3101
Finn Williams23b87b32019-07-30 11:44:05 +01003102template<typename HalPolicy,
Mike Kelly46272802019-08-14 17:00:48 +01003103 typename Operation = typename HalPolicy::Operation,
3104 typename Model = typename HalPolicy::Model>
3105bool ConvertSqueeze(const Operation& operation, const Model& model, ConversionData& data)
3106{
3107 using Operand = typename HalPolicy::Operand;
3108
3109 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3110 if (!input.IsValid())
3111 {
3112 return Fail("%s: Operation has invalid inputs", __func__);
3113 }
3114
3115 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3116 unsigned int rank = inputInfo.GetNumDimensions();
3117 if (rank > 4)
3118 {
3119 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
3120 }
3121
3122 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3123 if (!output)
3124 {
3125 return Fail("%s: Could not read output 0", __func__);
3126 }
3127
3128 if (IsDynamicTensor(GetTensorInfoForOperand(*output)))
3129 {
3130 return Fail("%s: Dynamic output tensors are not supported", __func__);
3131 }
3132
3133 // NOTE: Axis is an optional parameter to SQUEEZE, therefore we do not want to generate a failure
3134 // if the operand index is out of bounds.
3135 const Operand* axisOperand = GetInputOperand<HalPolicy>(operation, 1, model, false);
3136
3137 const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
3138
3139 std::vector<int32_t> axis;
3140 if (!axisOperand)
3141 {
3142 axis.assign(dimensionSequence,
3143 dimensionSequence + rank);
3144 }
3145 else
3146 {
3147 GetTensorInt32Values<HalPolicy>(*axisOperand, axis, model, data);
3148 }
3149
3150 std::vector<uint32_t> outputDims;
3151 for (unsigned int i = 0; i < rank; i++)
3152 {
3153 bool skipSqueeze = (std::find(axis.begin(), axis.end(), i) == axis.end());
3154 auto currentDimension = inputInfo.GetShape()[i];
3155 if (skipSqueeze || currentDimension != 1)
3156 {
3157 outputDims.push_back(currentDimension);
3158 }
3159 }
3160
3161 armnn::TensorShape outShape = armnn::TensorShape(outputDims.size(), outputDims.data());
3162
3163 armnn::TensorInfo outputInfo = inputInfo;
3164 outputInfo.SetShape(outShape);
3165
3166 armnn::ReshapeDescriptor reshapeDesc;
3167 reshapeDesc.m_TargetShape = outputInfo.GetShape();
3168
3169 bool isSupported = false;
3170 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3171 IsReshapeSupported,
3172 data.m_Backends,
3173 isSupported,
3174 inputInfo,
3175 reshapeDesc);
3176 if (!isSupported)
3177 {
3178 return false;
3179 }
3180
3181 armnn::IConnectableLayer* const layer = data.m_Network->AddReshapeLayer(reshapeDesc);
3182 assert(layer != nullptr);
3183 input.Connect(layer->GetInputSlot(0));
3184
3185 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3186}
3187
3188template<typename HalPolicy,
3189 typename Operation = typename HalPolicy::Operation,
3190 typename Model = typename HalPolicy::Model>
3191bool ConvertStridedSlice(const Operation& operation, const Model& model, ConversionData& data)
3192{
3193 using Operand = typename HalPolicy::Operand;
3194
3195 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3196 if (!input.IsValid())
3197 {
3198 return Fail("%s: Operation has invalid inputs", __func__);
3199 }
3200
3201 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3202 unsigned int rank = inputInfo.GetNumDimensions();
3203 if (rank > 4)
3204 {
3205 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
3206 }
3207
3208 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3209 if (!output)
3210 {
3211 return Fail("%s: Could not read output 0", __func__);
3212 }
3213
3214 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3215 if (IsDynamicTensor(outputInfo))
3216 {
3217 return Fail("%s: Dynamic output tensors are not supported", __func__);
3218 }
3219
3220 const Operand* beginOperand = GetInputOperand<HalPolicy>(operation, 1, model);
3221 const Operand* endOperand = GetInputOperand<HalPolicy>(operation, 2, model);
3222 const Operand* stridesOperand = GetInputOperand<HalPolicy>(operation, 3, model);
3223
3224 std::vector<int32_t> beginValues;
3225 std::vector<int32_t> endValues;
3226 std::vector<int32_t> stridesValues;
3227
3228 // The length of the beginOperand, endOperand and stridesOperand must be of a rank(input)
3229 auto ValidateInputOperands = [&] (const Operand& operand, std::vector<int32_t>& operandValues)
3230 {
3231 if (!GetTensorInt32Values<HalPolicy>(operand, operandValues, model, data))
3232 {
3233 return false;
3234 }
3235
3236 if (operandValues.size() != rank)
3237 {
3238 return false;
3239 }
3240
3241 return true;
3242 };
3243
3244 if (!ValidateInputOperands(*beginOperand, beginValues)
3245 || !ValidateInputOperands(*endOperand, endValues)
3246 || !ValidateInputOperands(*stridesOperand, stridesValues))
3247 {
3248 return Fail("%s: Operation has invalid input operand", __func__);
3249 }
3250
3251 // Stride cannot have value '0'
3252 if (std::any_of(stridesValues.cbegin(), stridesValues.cend(), [](int32_t i){ return i == 0; }))
3253 {
3254 return Fail("%s: Stride must be non-zero value.", __func__);
3255 }
3256
3257 armnn::StridedSliceDescriptor descriptor;
3258 descriptor.m_Begin.assign(beginValues.cbegin(), beginValues.cend());
3259 descriptor.m_End.assign(endValues.cbegin(), endValues.cend());
3260 descriptor.m_Stride.assign(stridesValues.cbegin(), stridesValues.cend());
3261 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
3262
3263 // Get the "begin_mask", "end_mask", and "shrink_axis_mask" flags
3264 if (!GetInputInt32<HalPolicy>(operation, 4, descriptor.m_BeginMask, model, data) ||
3265 !GetInputInt32<HalPolicy>(operation, 5, descriptor.m_EndMask, model, data) ||
3266 !GetInputInt32<HalPolicy>(operation, 6, descriptor.m_ShrinkAxisMask, model, data))
3267 {
3268 return Fail("%s: Operation has invalid inputs", __func__);
3269 }
3270
3271 bool isSupported = false;
3272 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3273 IsStridedSliceSupported,
3274 data.m_Backends,
3275 isSupported,
3276 inputInfo,
3277 outputInfo,
3278 descriptor);
3279 if (!isSupported)
3280 {
3281 return false;
3282 }
3283
3284 armnn::IConnectableLayer* const layer = data.m_Network->AddStridedSliceLayer(descriptor);
3285 assert(layer != nullptr);
3286 input.Connect(layer->GetInputSlot(0));
3287
3288 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3289}
3290
3291template<typename HalPolicy,
3292 typename Operation = typename HalPolicy::Operation,
3293 typename Model = typename HalPolicy::Model>
3294bool ConvertTranspose(const Operation& operation, const Model& model, ConversionData& data)
3295{
3296 using Operand = typename HalPolicy::Operand;
3297
3298 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3299 if (!input.IsValid())
3300 {
3301 return Fail("%s: Operation has invalid inputs", __func__);
3302 }
3303
3304 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3305 unsigned int rank = inputInfo.GetNumDimensions();
3306 if (rank > 4)
3307 {
3308 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
3309 }
3310
3311 // NOTE: Axis is an optional parameter to TRANSPOSE, therefore we do not want to generate a failure
3312 // if the operand index is out of bounds.
3313 const Operand* permOperand = GetInputOperand<HalPolicy>(operation, 1, model, false);
3314
3315 std::vector<int32_t> perm(rank);
3316 if (!permOperand)
3317 {
3318 // NOTE: If perm is not given, it is set to (n-1...0), where n is the rank of the tensor
3319 for (unsigned int i = rank; i > 0; i--)
3320 {
3321 perm[rank - i] = boost::numeric_cast<int> (i - 1);
3322 }
3323 }
3324 else
3325 {
3326 GetTensorInt32Values<HalPolicy>(*permOperand, perm, model, data);
3327 }
3328
3329 std::vector<uint32_t> outputDims(perm.begin(), perm.begin() + rank);
3330
3331 auto permutationVector = armnn::PermutationVector(outputDims.data(), outputDims.size());
3332 if (!permutationVector.IsEqual(NHWCToArmNN)
3333 && !permutationVector.IsEqual(ArmNNToNHWC)
3334 && !permutationVector.IsEqual({ 3, 2, 0, 1 }))
3335 {
3336 return Fail("%s: Only [0, 3, 1, 2], [0, 2, 3, 1] and [3, 2, 0, 1] permutations are supported.", __func__);
3337 }
3338
3339 armnn::PermuteDescriptor permuteDesc;
3340 permuteDesc.m_DimMappings = permutationVector;
3341
3342 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3343 if (!output)
3344 {
3345 return Fail("%s: Could not read output 0", __func__);
3346 }
3347
3348 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3349
3350 bool isSupported = false;
3351 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3352 IsPermuteSupported,
3353 data.m_Backends,
3354 isSupported,
3355 inputInfo,
3356 outputInfo,
3357 permuteDesc);
3358 if (!isSupported)
3359 {
3360 return false;
3361 }
3362
3363 armnn::IConnectableLayer* const layer = data.m_Network->AddPermuteLayer(permuteDesc);
3364 assert(layer != nullptr);
3365 input.Connect(layer->GetInputSlot(0));
3366
3367 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3368}
3369
3370template<typename HalPolicy,
Finn Williams23b87b32019-07-30 11:44:05 +01003371 typename HalOperation = typename HalPolicy::Operation,
Finn Williams0e4e4392019-07-31 10:56:27 +01003372 typename HalOperand = typename HalPolicy::Operand,
Finn Williams23b87b32019-07-30 11:44:05 +01003373 typename HalModel = typename HalPolicy::Model>
3374bool ConvertBatchToSpaceNd(const HalOperation& operation,
3375 const HalModel& model,
3376 ConversionData& data)
3377{
Finn Williams23b87b32019-07-30 11:44:05 +01003378
3379 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3380 if (!input.IsValid())
3381 {
3382 return Fail("%s: Operation has invalid inputs", __func__);
3383 }
3384
3385 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3386 if (!output)
3387 {
3388 return Fail("%s: Could not read output 0", __func__);
3389 }
3390
3391 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3392 if (IsDynamicTensor(outputInfo))
3393 {
3394 return Fail("%s: Dynamic output tensors are not supported", __func__);
3395 }
3396
3397 const HalOperand* blockOperand = GetInputOperand<HalPolicy>(operation, 1, model);
3398 if (!blockOperand)
3399 {
3400 return Fail("%s: Could not read input 1", __func__);
3401 }
3402
3403 // Convert the block operand to int32
3404 std::vector<int32_t> block;
3405 if (!GetTensorInt32Values<HalPolicy>(*blockOperand, block, model, data))
3406 {
3407 return Fail("%s: Input 1 has invalid values", __func__);
3408 }
3409
3410 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3411
3412 unsigned int rank = inputInfo.GetNumDimensions();
3413 if (rank != 4)
3414 {
3415 Fail("%s: Only inputs with rank equal to 4 are supported", __func__);
3416 }
3417
3418 if (std::any_of(block.cbegin(), block.cend(), [](int32_t i){ return i < 1; }))
3419 {
3420 return Fail("%s: Block sizes for each spatial dimension of the input tensor must be"
3421 " greater than or equal to 1", __func__);
3422 }
3423
3424 armnn::BatchToSpaceNdDescriptor batchToSpaceNdDesc;
3425 batchToSpaceNdDesc.m_BlockShape.assign(block.cbegin(), block.cend());
3426 batchToSpaceNdDesc.m_DataLayout = armnn::DataLayout::NHWC;
3427
3428 if (Is12Operand(*output))
3429 {
Finn Williams0e4e4392019-07-31 10:56:27 +01003430 batchToSpaceNdDesc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 2, model, data);
Finn Williams23b87b32019-07-30 11:44:05 +01003431 }
3432 // Setting crops to 0,0 0,0 as it is not supported in Android NN API
3433 batchToSpaceNdDesc.m_Crops = {{0, 0}, {0, 0}};
3434
3435 bool isSupported = false;
3436 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3437 IsBatchToSpaceNdSupported,
3438 data.m_Backends,
3439 isSupported,
3440 inputInfo,
3441 outputInfo,
3442 batchToSpaceNdDesc);
3443 if (!isSupported)
3444 {
3445 return false;
3446 }
3447
3448 armnn::IConnectableLayer* const layer = data.m_Network->AddBatchToSpaceNdLayer(batchToSpaceNdDesc);
3449 assert(layer != nullptr);
3450 input.Connect(layer->GetInputSlot(0));
3451
3452 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3453}
Mike Kelly0a879362019-07-29 16:56:31 +01003454
Finn Williamsd74c5052019-07-30 17:06:00 +01003455template<typename HalPolicy,
3456 typename HalOperation = typename HalPolicy::Operation,
3457 typename HalOperand = typename HalPolicy::Operand,
3458 typename HalModel = typename HalPolicy::Model>
3459bool ConvertSpaceToBatchNd(const HalOperation& operation, const HalModel& model, ConversionData& data)
3460{
3461 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3462 if (!input.IsValid())
3463 {
3464 return Fail("%s: Operation has invalid inputs", __func__);
3465 }
3466
3467 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3468 unsigned int rank = inputInfo.GetNumDimensions();
3469 unsigned int spatialDim = rank - 2;
3470
3471 if (rank != 4)
3472 {
3473 Fail("%s: Only inputs with rank 4 are supported", __func__);
3474 }
3475
3476 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3477 if (!output)
3478 {
3479 return Fail("%s: Could not read output 0", __func__);
3480 }
3481
3482 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3483 if (IsDynamicTensor(outputInfo))
3484 {
3485 return Fail("%s: Dynamic output tensors are not supported", __func__);
3486 }
3487
3488 const HalOperand* blockShapeOperand = GetInputOperand<HalPolicy>(operation, 1, model);
3489 const HalOperand* paddingsOperand = GetInputOperand<HalPolicy>(operation, 2, model);
3490
3491 armnn::TensorShape blockShapeOperandShape = GetTensorShapeForOperand(*blockShapeOperand);
3492 if (blockShapeOperandShape.GetNumDimensions() != 1 || blockShapeOperandShape.GetNumElements() != spatialDim)
3493 {
3494 return Fail("%s: Operation has invalid block shape operand: expected shape [%d]", __func__, spatialDim);
3495 }
3496
3497 std::vector<int32_t> blockShape;
3498 GetTensorInt32Values<HalPolicy>(*blockShapeOperand, blockShape, model, data);
3499 if (std::any_of(blockShape.cbegin(), blockShape.cend(), [](int32_t i){ return i < 1; }))
3500 {
3501 return Fail("%s: Block shape must be at least 1 in all dimensions.", __func__);
3502 }
3503
3504 armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand);
3505 if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != 2 * spatialDim)
3506 {
3507 return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, spatialDim);
3508 }
3509
3510 std::vector<std::pair<unsigned int, unsigned int>> paddingList;
3511 std::vector<int32_t> paddings;
3512 GetTensorInt32Values<HalPolicy>(*paddingsOperand, paddings, model, data);
3513 for (unsigned int i = 0; i < paddings.size() - 1; i += 2)
3514 {
3515 int paddingBeforeInput = paddings[i];
3516 int paddingAfterInput = paddings[i + 1];
3517 if (paddingBeforeInput < 0 || paddingAfterInput < 0)
3518 {
3519 return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__);
3520 }
3521
3522 paddingList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput);
3523 }
3524
3525 armnn::SpaceToBatchNdDescriptor descriptor;
3526 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
3527 descriptor.m_BlockShape.assign(blockShape.cbegin(), blockShape.cend());
3528 descriptor.m_PadList.assign(paddingList.cbegin(), paddingList.cend());
3529
3530 if (Is12Operand(*output))
3531 {
3532 descriptor.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 3, model, data);
3533 }
3534
3535 bool isSupported = false;
3536 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3537 IsSpaceToBatchNdSupported,
3538 data.m_Backends,
3539 isSupported,
3540 inputInfo,
3541 outputInfo,
3542 descriptor);
3543 if (!isSupported)
3544 {
3545 return false;
3546 }
3547
3548 armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToBatchNdLayer(descriptor);
3549 assert(layer != nullptr);
3550 input.Connect(layer->GetInputSlot(0));
3551
3552 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3553}
3554
Kevin May407718f2019-09-09 14:46:41 +01003555template<typename HalPolicy,
3556 typename HalOperation = typename HalPolicy::Operation,
3557 typename HalModel = typename HalPolicy::Model>
3558bool ConvertAbs(const HalOperation& operation, const HalModel& model, ConversionData& data)
3559{
3560 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3561
3562 if (!input.IsValid())
3563 {
3564 return Fail("%s: Operation has invalid input", __func__);
3565 }
3566
3567 using HalOperand = typename HalPolicy::Operand;
3568 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3569 if (!output)
3570 {
3571 return Fail("%s: Could not read output 0", __func__);
3572 }
3573
3574 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3575 if (IsDynamicTensor(outputInfo))
3576 {
3577 return Fail("%s: Dynamic output tensors are not supported", __func__);
3578 }
3579
3580 bool isSupported = false;
3581 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3582 IsAbsSupported,
3583 data.m_Backends,
3584 isSupported,
3585 input.GetTensorInfo(),
3586 outputInfo);
3587
3588 if (!isSupported)
3589 {
3590 return false;
3591 }
3592
3593 armnn::IConnectableLayer* const layer = data.m_Network->AddAbsLayer();
3594 assert(layer != nullptr);
3595 input.Connect(layer->GetInputSlot(0));
3596
3597 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3598}
3599
3600
saoste01b8471482018-10-10 09:44:51 +01003601} // namespace armnn_driver