blob: 6f1f100dc476b66d3002d1d2fb427ff76ca573c1 [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
Sadik Armagan7a13acc2019-11-21 15:54:36 +00002223 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2224 const armnn::Optional<unsigned int>& quantizationDim = inputInfo.GetQuantizationDim();
2225 if (quantizationDim.has_value() && quantizationDim.value() != 0)
2226 {
2227 return Fail("%s: Operation has quantization dimension different than 0", __func__);
2228 }
2229
Mike Kelly46272802019-08-14 17:00:48 +01002230 const Operand* const outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2231 if (!outputOperand)
2232 {
2233 return Fail("%s: Operation has invalid outputs", __func__);
2234 }
2235
2236 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
2237 if (IsDynamicTensor(outputInfo))
2238 {
2239 return Fail("%s: Dynamic output tensors are not supported", __func__);
2240 }
2241
2242 bool isSupported = false;
2243 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2244 IsDequantizeSupported,
2245 data.m_Backends,
2246 isSupported,
Sadik Armagan7a13acc2019-11-21 15:54:36 +00002247 inputInfo,
2248 outputInfo);
Mike Kelly46272802019-08-14 17:00:48 +01002249 if (!isSupported)
2250 {
2251 return false;
2252 }
2253
2254 armnn::IConnectableLayer* const layer = data.m_Network->AddDequantizeLayer();
2255 assert(layer != nullptr);
2256 input.Connect(layer->GetInputSlot(0));
2257
2258 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2259}
2260
2261template<typename HalPolicy,
2262 typename Operation = typename HalPolicy::Operation,
2263 typename Model = typename HalPolicy::Model>
2264bool ConvertDiv(const Operation& operation, const Model& model, ConversionData& data)
2265{
2266 using Operand = typename HalPolicy::Operand;
2267
2268 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2269 LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data);
2270
2271 if (!input0.IsValid() || !input1.IsValid())
2272 {
2273 return Fail("%s: Operation has invalid inputs", __func__);
2274 }
2275
2276 // The FuseActivation parameter is always the input index 2
2277 // and it should be optional
2278 ActivationFn activationFunction;
2279 if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data))
2280 {
2281 return Fail("%s: Operation has invalid inputs", __func__);
2282 }
2283
2284 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2285 if (!output)
2286 {
2287 return Fail("%s: Could not read output 0", __func__);
2288 }
2289
2290 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2291 if (IsDynamicTensor(outputInfo))
2292 {
2293 return Fail("%s: Dynamic output tensors are not supported", __func__);
2294 }
2295
2296 bool isSupported = false;
2297 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2298 IsDivisionSupported,
2299 data.m_Backends,
2300 isSupported,
2301 input0.GetTensorInfo(),
2302 input1.GetTensorInfo(),
2303 outputInfo);
2304 if (!isSupported)
2305 {
2306 return false;
2307 }
2308
2309 armnn::IConnectableLayer* const startLayer = data.m_Network->AddDivisionLayer();
2310 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
2311
2312 if (endLayer)
2313 {
Sadik Armagan64b19b52019-08-19 09:49:58 +01002314 bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data);
2315 if (!isReshapeSupported)
2316 {
2317 return false;
2318 }
2319
Mike Kelly46272802019-08-14 17:00:48 +01002320 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
2321 }
2322 return Fail("%s: ProcessActivation failed", __func__);
2323}
2324
2325template<typename HalPolicy,
2326 typename Operation = typename HalPolicy::Operation,
2327 typename Model = typename HalPolicy::Model>
2328bool ConvertFloor(const Operation& operation, const Model& model, ConversionData& data)
2329{
2330 using Operand = typename HalPolicy::Operand;
2331
2332 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2333 if (!input.IsValid())
2334 {
2335 return Fail("%s: Operation has invalid inputs", __func__);
2336 }
2337
2338 const Operand* const outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2339 if (!outputOperand)
2340 {
2341 return Fail("%s: Operation has invalid outputs", __func__);
2342 }
2343
2344 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
2345 if (IsDynamicTensor(outputInfo))
2346 {
2347 return Fail("%s: Dynamic output tensors are not supported", __func__);
2348 }
2349
2350 bool isSupported = false;
2351 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2352 IsFloorSupported,
2353 data.m_Backends,
2354 isSupported,
2355 input.GetTensorInfo(),
2356 outputInfo);
2357 if (!isSupported)
2358 {
2359 return false;
2360 }
2361
2362 armnn::IConnectableLayer* layer = data.m_Network->AddFloorLayer();
2363 assert(layer != nullptr);
2364 input.Connect(layer->GetInputSlot(0));
2365
2366 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2367}
2368
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002369inline bool IsQSymm8(const V1_0::Operand&)
2370{
2371 return false;
2372}
2373
2374#ifdef ARMNN_ANDROID_NN_V1_2
2375
2376inline bool IsQSymm8(const V1_2::Operand& operand)
2377{
2378 return operand.type == V1_2::OperandType::TENSOR_QUANT8_SYMM;
2379}
2380
2381#endif
2382
2383template<typename HalPolicy,
2384 typename Operation = typename HalPolicy::Operation,
2385 typename Model = typename HalPolicy::Model>
Sadik Armaganac23b032019-11-18 17:11:21 +00002386std::tuple<std::unique_ptr<float[]>, size_t, armnn::TensorInfo, int>
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002387DequantizeIfRequired(size_t operand_index, const Operation& operation, const Model& model, const ConversionData& data)
2388{
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002389 using HalOperand = typename HalPolicy::Operand;
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002390
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002391 const HalOperand* weightsOperand = GetInputOperand<HalPolicy>(operation, operand_index, model);
Sadik Armaganac23b032019-11-18 17:11:21 +00002392 if (!weightsOperand)
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002393 {
Sadik Armaganac23b032019-11-18 17:11:21 +00002394 // Invalid Operand will return with error code '-1'
2395 return { nullptr, 0, armnn::TensorInfo(), -1 };
2396 }
2397
2398 if (IsOperandConstant<HalPolicy>(*weightsOperand))
2399 {
2400 // Weights are already constant
2401 return { nullptr, 0, armnn::TensorInfo(), 0 };
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002402 }
2403
2404 const size_t weightsInputIndex = operation.inputs[operand_index];
2405
2406 // The weights are a non const tensor, this indicates they might be the output of a dequantize op.
2407 // Iterate over the nodes and find the previous operation which should be DEQUANTIZE
2408 for (uint32_t operationIdx = 0; operationIdx < model.operations.size(); ++operationIdx)
2409 {
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002410 // Search for the DEQUANTIZE op which has the operand with index equal to operandIndex
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002411 const auto& operationIt = model.operations[operationIdx];
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002412 if (operationIt.type != HalPolicy::OperationType::DEQUANTIZE)
2413 {
2414 continue;
2415 }
2416
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002417 size_t outOpIndex = weightsInputIndex + 1;
2418 for (size_t i = 0; outOpIndex != weightsInputIndex && i < operationIt.outputs.size(); ++i)
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002419 {
2420 outOpIndex = operationIt.outputs[i];
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002421 }
2422
2423 if (outOpIndex != weightsInputIndex)
2424 {
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002425 continue;
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002426 }
2427
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002428 const HalOperand* operand = GetInputOperand<HalPolicy>(operationIt, 0, model);
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002429 BOOST_ASSERT(operand);
2430
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002431 if (!IsQSymm8(*operand))
2432 {
2433 // Only supporting dequantize from QSYMM8 to FLOAT
2434 break;
2435 }
2436
2437 // Allocate a new buffer for the dequantized data and manually dequantize
2438 const void* startValue = GetOperandValueReadOnlyAddress<HalPolicy>(*operand, model, data);
2439 if (!startValue)
2440 {
2441 // Failed to get the operand address
2442 break;
2443 }
2444
2445 const uint8_t* quantizedBuffer = reinterpret_cast<const uint8_t*>(startValue);
2446 size_t dequantizedBufferLength = operand->location.length;
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002447 const float quantizationScale = operand->scale;
2448
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002449 auto dequantizedBuffer = std::make_unique<float[]>(dequantizedBufferLength + 1);
2450 for (size_t i = 0; i < dequantizedBufferLength; ++i)
2451 {
2452 float* dstPtr = dequantizedBuffer.get();
2453 BOOST_ASSERT(dstPtr);
2454 *dstPtr++ = quantizedBuffer[i] * quantizationScale;
2455 }
2456
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002457 // Construct tensor info for dequantized ConstTensor
2458 armnn::TensorInfo tensorInfo(operand->dimensions.size(),
2459 operand->dimensions.data(),
2460 armnn::DataType::Float32);
2461
Sadik Armaganac23b032019-11-18 17:11:21 +00002462 return { std::move(dequantizedBuffer), dequantizedBufferLength * sizeof(float), std::move(tensorInfo), 0 };
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002463 }
2464
Sadik Armaganac23b032019-11-18 17:11:21 +00002465 return { nullptr, 0, armnn::TensorInfo() , 0};
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002466}
2467
2468template<typename HalPolicy,
2469 typename Operation = typename HalPolicy::Operation,
2470 typename Model = typename HalPolicy::Model>
2471ConstTensorPin DequantizeAndMakeConstTensorPin(const Operation& operation,
2472 const Model& model,
2473 const ConversionData& data,
2474 size_t operandIndex,
2475 bool optional = false)
2476{
2477 auto dequantized = DequantizeIfRequired<HalPolicy, Operation, Model>(operandIndex,operation, model, data);
Sadik Armaganac23b032019-11-18 17:11:21 +00002478 if (std::get<3>(dequantized) == -1)
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002479 {
Sadik Armaganac23b032019-11-18 17:11:21 +00002480 // Return it as invalid, tensor with no values is not really an error
2481 return ConstTensorPin();
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002482 }
2483
Sadik Armaganac23b032019-11-18 17:11:21 +00002484 if (std::get<1>(dequantized) == 0)
2485 {
2486 return ConvertOperationInputToConstTensorPin<HalPolicy>(
2487 operation, operandIndex, model, data, g_DontPermute, nullptr, optional);
2488
2489 }
2490
2491 return ConstTensorPin(std::get<2>(dequantized), std::get<0>(dequantized).get(),
2492 std::get<1>(dequantized), g_DontPermute);
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002493}
2494
2495
Mike Kelly46272802019-08-14 17:00:48 +01002496template<typename HalPolicy,
2497 typename Operation = typename HalPolicy::Operation,
2498 typename Model = typename HalPolicy::Model>
2499bool ConvertFullyConnected(const Operation& operation, const Model& model, ConversionData& data)
2500{
2501 using Operand = typename HalPolicy::Operand;
Mike Kelly46272802019-08-14 17:00:48 +01002502 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2503 if (!input.IsValid())
2504 {
2505 return Fail("%s: Operation has invalid inputs", __func__);
2506 }
2507
2508 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2509 if (!output)
2510 {
2511 return Fail("%s: Could not read output 0", __func__);
2512 }
2513
2514 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2515 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2516
2517 if (IsDynamicTensor(outputInfo))
2518 {
2519 return Fail("%s: Dynamic output tensors are not supported", __func__);
2520 }
2521
Aron Virginas-Tar65a1b1d2019-11-15 15:59:51 +00002522 ConstTensorPin weightsPin = DequantizeAndMakeConstTensorPin<HalPolicy>(operation, model, data, 1);
2523 ConstTensorPin biasPin = ConvertOperationInputToConstTensorPin<HalPolicy>(operation, 2, model, data); // 1D
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002524
2525 if (!weightsPin.IsValid())
Mike Kelly46272802019-08-14 17:00:48 +01002526 {
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002527 return Fail("%s: Operation has invalid weights", __func__);
2528 }
2529
2530 if (!biasPin.IsValid())
2531 {
2532 return Fail("%s: Operation has invalid bias", __func__);
Mike Kelly46272802019-08-14 17:00:48 +01002533 }
2534
2535 armnn::ConstTensor weights = weightsPin.GetConstTensor();
2536 armnn::ConstTensor bias = biasPin.GetConstTensor();
2537 armnn::TensorInfo reshapedInfo = inputInfo;
2538
2539 try
2540 {
2541 reshapedInfo.SetShape(FlattenFullyConnectedInput(inputInfo.GetShape(), weights.GetInfo().GetShape()));
Pablo Tellofb45e2f2019-10-18 16:51:57 +01002542 }
2543 catch (const std::exception& e)
2544 {
Mike Kelly46272802019-08-14 17:00:48 +01002545 return Fail("%s: %s", __func__, e.what());
2546 }
2547
2548 // ensuring that the bias value is within 1% of the weights input (small float differences can exist)
2549 SanitizeBiasQuantizationScale(bias.GetInfo(), weights.GetInfo(), reshapedInfo);
2550
2551 ActivationFn activationFunction;
2552 if (!GetInputActivationFunction<HalPolicy>(operation, 3, activationFunction, model, data))
2553 {
2554 return Fail("%s: Operation has invalid inputs", __func__);
2555 }
2556
2557 armnn::FullyConnectedDescriptor desc;
2558 desc.m_TransposeWeightMatrix = true;
2559 desc.m_BiasEnabled = true;
2560
2561 bool isSupported = false;
2562 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2563 IsFullyConnectedSupported,
2564 data.m_Backends,
2565 isSupported,
2566 reshapedInfo,
2567 outputInfo,
2568 weights.GetInfo(),
2569 bias.GetInfo(),
2570 desc);
2571 if (!isSupported)
2572 {
2573 return false;
2574 }
2575
2576 armnn::IConnectableLayer* startLayer =
2577 data.m_Network->AddFullyConnectedLayer(desc, weights, armnn::Optional<armnn::ConstTensor>(bias));
2578 armnn::IConnectableLayer* endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
2579
2580 if (endLayer != nullptr)
2581 {
2582 if (inputInfo.GetNumDimensions() > 2U)
2583 {
2584 armnn::ReshapeDescriptor reshapeDescriptor;
2585 reshapeDescriptor.m_TargetShape = reshapedInfo.GetShape();
2586
2587 armnn::IConnectableLayer* reshapeLayer = data.m_Network->AddReshapeLayer(reshapeDescriptor);
2588 assert(reshapeLayer != nullptr);
2589 input.Connect(reshapeLayer->GetInputSlot(0));
2590 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedInfo);
2591 reshapeLayer->GetOutputSlot(0).Connect(startLayer->GetInputSlot(0));
2592 }
2593 else
2594 {
2595 input.Connect(startLayer->GetInputSlot(0));
2596 }
2597
2598 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
2599 }
2600 else
2601 {
2602 return Fail("%s: ProcessActivation failed", __func__);
2603 }
2604}
2605
2606template<typename HalPolicy,
2607 typename Operation = typename HalPolicy::Operation,
2608 typename Model = typename HalPolicy::Model>
2609bool ConvertL2Normalization(const Operation& operation, const Model& model, ConversionData& data)
2610{
Mike Kelly999e2092019-08-15 10:46:46 +01002611 if (operation.inputs.size() != 1)
2612 {
2613 return Fail("%s: Optional inputs are not supported", __func__);
2614 }
2615
Mike Kelly46272802019-08-14 17:00:48 +01002616 using Operand = typename HalPolicy::Operand;
2617
2618 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2619 if (!input.IsValid())
2620 {
2621 return Fail("%s: Operation has invalid inputs", __func__);
2622 }
2623
2624 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2625 if (!output)
2626 {
2627 return Fail("%s: Could not read output 0", __func__);
2628 }
2629
2630 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2631 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2632
2633 if (IsDynamicTensor(outputInfo))
2634 {
2635 return Fail("%s: Dynamic output tensors are not supported", __func__);
2636 }
2637 if (outputInfo.GetNumDimensions() != 4u)
2638 {
2639 return Fail("%s: Tensor Rank other than 4 is not supported", __func__);
2640 }
2641
2642 armnn::L2NormalizationDescriptor desc;
2643 desc.m_DataLayout = armnn::DataLayout::NHWC;
2644
2645 bool isSupported = false;
2646 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2647 IsL2NormalizationSupported,
2648 data.m_Backends,
2649 isSupported,
2650 inputInfo,
2651 outputInfo,
2652 desc);
2653 if (!isSupported)
2654 {
2655 return false;
2656 }
2657
2658 armnn::IConnectableLayer* layer = data.m_Network->AddL2NormalizationLayer(desc);
2659 assert(layer != nullptr);
2660 input.Connect(layer->GetInputSlot(0));
2661
2662 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2663}
2664
2665template<typename HalPolicy,
2666 typename Operation = typename HalPolicy::Operation,
2667 typename Model = typename HalPolicy::Model>
2668bool ConvertLocalResponseNormalization(const Operation& operation,
2669 const Model& model,
2670 ConversionData& data)
2671{
Mike Kelly999e2092019-08-15 10:46:46 +01002672 if (operation.inputs.size() != 5)
2673 {
2674 return Fail("%s: Optional inputs are not supported", __func__);
2675 }
2676
Mike Kelly46272802019-08-14 17:00:48 +01002677 using Operand = typename HalPolicy::Operand;
2678 using OperandType = typename HalPolicy::OperandType;
2679
2680 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2681 if (!input.IsValid())
2682 {
2683 return Fail("%s: Operation has invalid inputs", __func__);
2684 }
2685
2686 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2687 if (!output)
2688 {
2689 return Fail("%s: Could not read output 0", __func__);
2690 }
2691
2692 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2693 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2694
2695 if (IsDynamicTensor(outputInfo))
2696 {
2697 return Fail("%s: Dynamic output tensors are not supported", __func__);
2698 }
2699 if (outputInfo.GetNumDimensions() != 4u)
2700 {
2701 return Fail("%s: Tensor Rank other than 4 is not supported", __func__);
2702 }
2703
2704 armnn::NormalizationDescriptor descriptor;
2705 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
2706 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
2707 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
2708
2709 if (!input.IsValid() ||
2710 !GetInputScalar<HalPolicy>(operation, 1, OperandType::INT32, descriptor.m_NormSize, model, data) ||
2711 !GetInputFloat32<HalPolicy>(operation, 2, descriptor.m_K, model, data) ||
2712 !GetInputFloat32<HalPolicy>(operation, 3, descriptor.m_Alpha, model, data) ||
2713 !GetInputFloat32<HalPolicy>(operation, 4, descriptor.m_Beta, model, data))
2714 {
2715 return Fail("%s: Operation has invalid inputs", __func__);
2716 }
2717
2718 // ArmNN expects normSize to be the full size of the normalization
2719 // window rather than the radius as in AndroidNN.
2720 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
2721
2722 bool isSupported = false;
2723 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2724 IsNormalizationSupported,
2725 data.m_Backends,
2726 isSupported,
2727 inputInfo,
2728 outputInfo,
2729 descriptor);
2730 if (!isSupported)
2731 {
2732 return false;
2733 }
2734
2735
2736 armnn::IConnectableLayer* layer = data.m_Network->AddNormalizationLayer(descriptor);
2737 assert(layer != nullptr);
2738 input.Connect(layer->GetInputSlot(0));
2739
2740 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2741}
2742
2743template<typename HalPolicy,
2744 typename Operation = typename HalPolicy::Operation,
2745 typename Model = typename HalPolicy::Model>
2746bool ConvertLogistic(const Operation& operation, const Model& model, ConversionData& data)
2747{
2748 using Operand = typename HalPolicy::Operand;
2749
2750 armnn::ActivationDescriptor desc;
2751 desc.m_Function = armnn::ActivationFunction::Sigmoid;
2752
2753 return ConvertToActivation<HalPolicy>(operation, __func__, desc, model, data);
2754}
2755
2756template<typename HalPolicy,
2757 typename Operation = typename HalPolicy::Operation,
2758 typename Model = typename HalPolicy::Model>
2759bool ConvertMean(const Operation& operation, const Model& model, ConversionData& data)
2760{
2761 using Operand = typename HalPolicy::Operand;
2762
2763 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2764 if (!input.IsValid())
2765 {
2766 return Fail("%s: Operation has invalid inputs", __func__);
2767 }
2768
2769 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
2770 if (!output)
2771 {
2772 return Fail("%s: Could not read output 0", __func__);
2773 }
2774
2775 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
2776 if (IsDynamicTensor(outputInfo))
2777 {
2778 return Fail("%s: Dynamic output tensors are not supported", __func__);
2779 }
2780
2781 const Operand* axisOperand = GetInputOperand<HalPolicy>(operation, 1, model);
2782 if (!axisOperand)
2783 {
2784 return Fail("%s: Could not read input 1", __func__);
2785 }
2786
2787 std::vector<int32_t> axis;
2788 if (!GetTensorInt32Values<HalPolicy>(*axisOperand, axis, model, data))
2789 {
2790 return Fail("%s: Input 1 has invalid values", __func__);
2791 }
2792
2793 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2794
2795 // Convert the axis to unsigned int and remove duplicates.
2796 unsigned int rank = inputInfo.GetNumDimensions();
2797 std::set<unsigned int> uniqueAxis;
2798 std::transform(axis.begin(), axis.end(),
2799 std::inserter(uniqueAxis, uniqueAxis.begin()),
2800 [rank](int i) -> unsigned int { return (i + rank) % rank; });
2801
2802 // Get the "keep dims" flag.
2803 int32_t keepDims = 0;
2804 if (!GetInputInt32<HalPolicy>(operation, 2, keepDims, model, data))
2805 {
2806 return Fail("%s: Could not read input 2", __func__);
2807 }
2808
2809 armnn::MeanDescriptor descriptor;
2810 descriptor.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
2811 descriptor.m_KeepDims = keepDims > 0;
2812
2813 bool isSupported = false;
2814 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2815 IsMeanSupported,
2816 data.m_Backends,
2817 isSupported,
2818 inputInfo,
2819 outputInfo,
2820 descriptor);
2821 if (!isSupported)
2822 {
2823 return false;
2824 }
2825
2826 armnn::IConnectableLayer* const layer = data.m_Network->AddMeanLayer(descriptor);
2827 assert(layer != nullptr);
2828 input.Connect(layer->GetInputSlot(0));
2829
2830 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
2831}
2832
2833template<typename HalPolicy,
2834 typename Operation = typename HalPolicy::Operation,
2835 typename Model = typename HalPolicy::Model>
2836bool ConvertMul(const Operation& operation, const Model& model, ConversionData& data)
2837{
2838 using Operand = typename HalPolicy::Operand;
2839
2840 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2841 LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data);
2842
2843 if (!input0.IsValid() || !input1.IsValid())
2844 {
2845 return Fail("%s: Operation has invalid inputs", __func__);
2846 }
2847
2848 // The FuseActivation parameter is always the input index 2
2849 // and it should be optional
2850 ActivationFn activationFunction;
2851 if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data))
2852 {
2853 return Fail("%s: Operation has invalid inputs", __func__);
2854 }
2855
2856 const Operand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2857
2858 if (outputOperand == nullptr)
2859 {
2860 return false;
2861 }
2862
2863 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*outputOperand);
2864 if (IsDynamicTensor(outputInfo))
2865 {
2866 return Fail("%s: Dynamic output tensors are not supported", __func__);
2867 }
2868
2869 bool isSupported = false;
2870 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2871 IsMultiplicationSupported,
2872 data.m_Backends,
2873 isSupported,
2874 input0.GetTensorInfo(),
2875 input1.GetTensorInfo(),
2876 outputInfo);
2877 if (!isSupported)
2878 {
2879 return false;
2880 }
2881
2882 armnn::IConnectableLayer* const startLayer = data.m_Network->AddMultiplicationLayer();
2883 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
2884
2885 const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo();
2886 const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo();
2887
2888 if (endLayer != nullptr)
2889 {
Sadik Armagan64b19b52019-08-19 09:49:58 +01002890 bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data);
2891 if (!isReshapeSupported)
2892 {
2893 return false;
2894 }
2895
Mike Kelly46272802019-08-14 17:00:48 +01002896 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
2897 }
2898 else
2899 {
2900 return Fail("%s: ProcessActivation failed", __func__);
2901 }
2902}
2903
2904template<typename HalPolicy,
2905 typename Operation = typename HalPolicy::Operation,
2906 typename Model = typename HalPolicy::Model>
2907bool ConvertPad(Operation& operation, const Model& model, ConversionData& data)
2908{
2909 using Operand = typename HalPolicy::Operand;
2910
Mike Kelly3c673942019-07-25 09:26:06 +01002911 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
2912 if (!input.IsValid())
2913 {
2914 return Fail("%s: Operation has invalid inputs", __func__);
2915 }
2916
2917 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
2918 unsigned int rank = inputInfo.GetNumDimensions();
2919
2920 armnn::PadDescriptor descriptor;
2921 if (!ConvertPaddings<HalPolicy>(operation, model, data, rank, descriptor))
2922 {
2923 return Fail("%s: Could not convert paddings", __func__);
2924 }
2925
2926 // Before Android Q, the pad value for ANEURALNETWORKS_TENSOR_QUANT8_ASYMM was undefined. Since Android Q the pad
2927 // value must be "logical zero" we set it to be equal to the QuantizationOffset so effectively it ends up as
2928 // (QuantizationOffset - QuantizationOffset) * scale = 0.
2929 if (inputInfo.GetDataType() == armnn::DataType::QuantisedAsymm8)
2930 {
2931 descriptor.m_PadValue = inputInfo.GetQuantizationOffset();
2932 }
2933
Mike Kelly46272802019-08-14 17:00:48 +01002934 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
Mike Kelly3c673942019-07-25 09:26:06 +01002935 if (!output)
2936 {
2937 return Fail("%s: Could not read output", __func__);
2938 }
2939
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002940 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
Mike Kelly3c673942019-07-25 09:26:06 +01002941 if (IsDynamicTensor(outputInfo))
2942 {
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002943 return Fail("%s: Dynamic output tensors are not supported", __func__);
Mike Kelly3c673942019-07-25 09:26:06 +01002944 }
2945
2946 bool isSupported = false;
2947 FORWARD_LAYER_SUPPORT_FUNC(__func__,
2948 IsPadSupported,
2949 data.m_Backends,
2950 isSupported,
2951 inputInfo,
2952 outputInfo,
2953 descriptor);
2954 if (!isSupported)
2955 {
2956 return false;
2957 }
2958
2959 armnn::IConnectableLayer* const layer = data.m_Network->AddPadLayer(descriptor);
2960 assert(layer != nullptr);
2961 input.Connect(layer->GetInputSlot(0));
2962 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
2963
Aron Virginas-Tarb7421e52019-07-26 13:14:39 +01002964 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
Mike Kelly3c673942019-07-25 09:26:06 +01002965}
2966
Mike Kelly0a879362019-07-29 16:56:31 +01002967template<typename HalPolicy,
2968 typename Operation = typename HalPolicy::Operation,
Mike Kelly46272802019-08-14 17:00:48 +01002969 typename Model = typename HalPolicy::Model>
2970bool ConvertReshape(const Operation& operation, const Model& model, ConversionData& data)
2971{
2972 using Operand = typename HalPolicy::Operand;
2973
2974 const Operand* inputOperand = GetInputOperand<HalPolicy>(operation, 0, model);
2975 const Operand* requestedShapeOperand = GetInputOperand<HalPolicy>(operation, 1, model);
2976 const Operand* outputOperand = GetOutputOperand<HalPolicy>(operation, 0, model);
2977
2978 if (inputOperand == nullptr
2979 || requestedShapeOperand == nullptr
2980 || outputOperand == nullptr)
2981 {
2982 return Fail("%s: Operation has invalid inputs", __func__);
2983 }
2984
2985 if (requestedShapeOperand->dimensions.size() != 1)
2986 {
2987 return Fail("%s: Input 1 expected to be one-dimensional (found %i dimensions)",
2988 __func__, requestedShapeOperand->dimensions.size());
2989 }
2990
2991 std::vector<int32_t> targetDimensions;
2992 if (!GetTensorInt32Values<HalPolicy>(*requestedShapeOperand, targetDimensions, model, data))
2993 {
2994 return Fail("%s: Could not read values of input 1", __func__);
2995 }
2996
2997 const Shape inputOperandShape = GetOperandShape(*inputOperand);
2998
2999 Shape requestedShape;
3000 // targetDimensions may contain special values (e.g. -1). reshapePrepare() is an AndroidNN provided utility
3001 // function that resolves these values into a fully specified tensor shape.
3002 if (!reshapePrepare(inputOperandShape, targetDimensions.data(), targetDimensions.size(), &requestedShape))
3003 {
3004 return Fail("%s: Failed to resolve the requested shape", __func__);
3005 }
3006
3007 const Shape outputOperandShape = GetOperandShape(*outputOperand);
3008 if (!SameShape(requestedShape, outputOperandShape))
3009 {
3010 return Fail("%s: Shape of output operand does not match resolved requested shape", __func__);
3011 }
3012
3013 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3014 if (!input.IsValid())
3015 {
3016 return Fail("%s: Could not read input 0", __func__);
3017 }
3018
3019 armnn::ReshapeDescriptor reshapeDescriptor;
3020 reshapeDescriptor.m_TargetShape = armnn::TensorShape(requestedShape.dimensions.size(),
3021 requestedShape.dimensions.data());
3022
3023 bool isSupported = false;
3024 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3025 IsReshapeSupported,
3026 data.m_Backends,
3027 isSupported,
3028 input.GetTensorInfo(),
3029 reshapeDescriptor);
3030 if (!isSupported)
3031 {
3032 return false;
3033 }
3034
3035 armnn::IConnectableLayer* layer = data.m_Network->AddReshapeLayer(reshapeDescriptor);
3036 assert(layer != nullptr);
3037 input.Connect(layer->GetInputSlot(0));
3038
3039 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3040}
3041
3042template<typename HalPolicy,
3043 typename Operation = typename HalPolicy::Operation,
Mike Kelly0a879362019-07-29 16:56:31 +01003044 typename Model = typename HalPolicy::Model>
3045bool ConvertSub(const Operation& operation, const Model& model, ConversionData& data)
3046{
Mike Kelly46272802019-08-14 17:00:48 +01003047 using Operand = typename HalPolicy::Operand;
3048
Mike Kelly0a879362019-07-29 16:56:31 +01003049 LayerInputHandle input0 = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3050 LayerInputHandle input1 = ConvertToLayerInputHandle<HalPolicy>(operation, 1, model, data);
3051
3052 if (!input0.IsValid() || !input1.IsValid())
3053 {
3054 return Fail("%s: Operation has invalid inputs", __func__);
3055 }
3056
3057 // The FuseActivation parameter is always the input index 2
3058 // and it should be optional
3059 ActivationFn activationFunction;
3060 if (!GetOptionalInputActivation<HalPolicy>(operation, 2, activationFunction, model, data))
3061 {
3062 return Fail("%s: Operation has invalid inputs", __func__);
3063 }
3064
3065 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3066 if (!output)
3067 {
3068 return Fail("%s: Could not read output 0", __func__);
3069 }
3070
3071 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3072 if (IsDynamicTensor(outputInfo))
3073 {
3074 return Fail("%s: Dynamic output tensors are not supported", __func__);
3075 }
3076
3077 bool isSupported = false;
3078 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3079 IsSubtractionSupported,
3080 data.m_Backends,
3081 isSupported,
3082 input0.GetTensorInfo(),
3083 input1.GetTensorInfo(),
3084 outputInfo);
3085 if (!isSupported)
3086 {
3087 return false;
3088 }
3089
3090 armnn::IConnectableLayer* const startLayer = data.m_Network->AddSubtractionLayer();
3091 armnn::IConnectableLayer* const endLayer = ProcessActivation(outputInfo, activationFunction, startLayer, data);
3092
3093 const armnn::TensorInfo& inputTensorInfo0 = input0.GetTensorInfo();
3094 const armnn::TensorInfo& inputTensorInfo1 = input1.GetTensorInfo();
3095
3096 if (endLayer)
3097 {
Sadik Armagan64b19b52019-08-19 09:49:58 +01003098 bool isReshapeSupported = BroadcastTensor(input0, input1, startLayer, data);
3099 if (!isReshapeSupported)
3100 {
3101 return false;
3102 }
Mike Kelly0a879362019-07-29 16:56:31 +01003103 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *endLayer, model, data);
3104 }
3105
3106 return Fail("%s: ProcessActivation failed", __func__);
3107}
3108
Finn Williams23b87b32019-07-30 11:44:05 +01003109template<typename HalPolicy,
Mike Kelly46272802019-08-14 17:00:48 +01003110 typename Operation = typename HalPolicy::Operation,
3111 typename Model = typename HalPolicy::Model>
3112bool ConvertSqueeze(const Operation& operation, const Model& model, ConversionData& data)
3113{
3114 using Operand = typename HalPolicy::Operand;
3115
3116 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3117 if (!input.IsValid())
3118 {
3119 return Fail("%s: Operation has invalid inputs", __func__);
3120 }
3121
3122 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3123 unsigned int rank = inputInfo.GetNumDimensions();
3124 if (rank > 4)
3125 {
3126 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
3127 }
3128
3129 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3130 if (!output)
3131 {
3132 return Fail("%s: Could not read output 0", __func__);
3133 }
3134
3135 if (IsDynamicTensor(GetTensorInfoForOperand(*output)))
3136 {
3137 return Fail("%s: Dynamic output tensors are not supported", __func__);
3138 }
3139
3140 // NOTE: Axis is an optional parameter to SQUEEZE, therefore we do not want to generate a failure
3141 // if the operand index is out of bounds.
3142 const Operand* axisOperand = GetInputOperand<HalPolicy>(operation, 1, model, false);
3143
3144 const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
3145
3146 std::vector<int32_t> axis;
3147 if (!axisOperand)
3148 {
3149 axis.assign(dimensionSequence,
3150 dimensionSequence + rank);
3151 }
3152 else
3153 {
3154 GetTensorInt32Values<HalPolicy>(*axisOperand, axis, model, data);
3155 }
3156
3157 std::vector<uint32_t> outputDims;
3158 for (unsigned int i = 0; i < rank; i++)
3159 {
3160 bool skipSqueeze = (std::find(axis.begin(), axis.end(), i) == axis.end());
3161 auto currentDimension = inputInfo.GetShape()[i];
3162 if (skipSqueeze || currentDimension != 1)
3163 {
3164 outputDims.push_back(currentDimension);
3165 }
3166 }
3167
3168 armnn::TensorShape outShape = armnn::TensorShape(outputDims.size(), outputDims.data());
3169
3170 armnn::TensorInfo outputInfo = inputInfo;
3171 outputInfo.SetShape(outShape);
3172
3173 armnn::ReshapeDescriptor reshapeDesc;
3174 reshapeDesc.m_TargetShape = outputInfo.GetShape();
3175
3176 bool isSupported = false;
3177 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3178 IsReshapeSupported,
3179 data.m_Backends,
3180 isSupported,
3181 inputInfo,
3182 reshapeDesc);
3183 if (!isSupported)
3184 {
3185 return false;
3186 }
3187
3188 armnn::IConnectableLayer* const layer = data.m_Network->AddReshapeLayer(reshapeDesc);
3189 assert(layer != nullptr);
3190 input.Connect(layer->GetInputSlot(0));
3191
3192 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3193}
3194
3195template<typename HalPolicy,
3196 typename Operation = typename HalPolicy::Operation,
3197 typename Model = typename HalPolicy::Model>
3198bool ConvertStridedSlice(const Operation& operation, const Model& model, ConversionData& data)
3199{
3200 using Operand = typename HalPolicy::Operand;
3201
3202 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3203 if (!input.IsValid())
3204 {
3205 return Fail("%s: Operation has invalid inputs", __func__);
3206 }
3207
3208 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3209 unsigned int rank = inputInfo.GetNumDimensions();
3210 if (rank > 4)
3211 {
3212 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
3213 }
3214
3215 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3216 if (!output)
3217 {
3218 return Fail("%s: Could not read output 0", __func__);
3219 }
3220
3221 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3222 if (IsDynamicTensor(outputInfo))
3223 {
3224 return Fail("%s: Dynamic output tensors are not supported", __func__);
3225 }
3226
3227 const Operand* beginOperand = GetInputOperand<HalPolicy>(operation, 1, model);
3228 const Operand* endOperand = GetInputOperand<HalPolicy>(operation, 2, model);
3229 const Operand* stridesOperand = GetInputOperand<HalPolicy>(operation, 3, model);
3230
3231 std::vector<int32_t> beginValues;
3232 std::vector<int32_t> endValues;
3233 std::vector<int32_t> stridesValues;
3234
3235 // The length of the beginOperand, endOperand and stridesOperand must be of a rank(input)
3236 auto ValidateInputOperands = [&] (const Operand& operand, std::vector<int32_t>& operandValues)
3237 {
3238 if (!GetTensorInt32Values<HalPolicy>(operand, operandValues, model, data))
3239 {
3240 return false;
3241 }
3242
3243 if (operandValues.size() != rank)
3244 {
3245 return false;
3246 }
3247
3248 return true;
3249 };
3250
3251 if (!ValidateInputOperands(*beginOperand, beginValues)
3252 || !ValidateInputOperands(*endOperand, endValues)
3253 || !ValidateInputOperands(*stridesOperand, stridesValues))
3254 {
3255 return Fail("%s: Operation has invalid input operand", __func__);
3256 }
3257
3258 // Stride cannot have value '0'
3259 if (std::any_of(stridesValues.cbegin(), stridesValues.cend(), [](int32_t i){ return i == 0; }))
3260 {
3261 return Fail("%s: Stride must be non-zero value.", __func__);
3262 }
3263
3264 armnn::StridedSliceDescriptor descriptor;
3265 descriptor.m_Begin.assign(beginValues.cbegin(), beginValues.cend());
3266 descriptor.m_End.assign(endValues.cbegin(), endValues.cend());
3267 descriptor.m_Stride.assign(stridesValues.cbegin(), stridesValues.cend());
3268 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
3269
3270 // Get the "begin_mask", "end_mask", and "shrink_axis_mask" flags
3271 if (!GetInputInt32<HalPolicy>(operation, 4, descriptor.m_BeginMask, model, data) ||
3272 !GetInputInt32<HalPolicy>(operation, 5, descriptor.m_EndMask, model, data) ||
3273 !GetInputInt32<HalPolicy>(operation, 6, descriptor.m_ShrinkAxisMask, model, data))
3274 {
3275 return Fail("%s: Operation has invalid inputs", __func__);
3276 }
3277
3278 bool isSupported = false;
3279 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3280 IsStridedSliceSupported,
3281 data.m_Backends,
3282 isSupported,
3283 inputInfo,
3284 outputInfo,
3285 descriptor);
3286 if (!isSupported)
3287 {
3288 return false;
3289 }
3290
3291 armnn::IConnectableLayer* const layer = data.m_Network->AddStridedSliceLayer(descriptor);
3292 assert(layer != nullptr);
3293 input.Connect(layer->GetInputSlot(0));
3294
3295 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3296}
3297
3298template<typename HalPolicy,
3299 typename Operation = typename HalPolicy::Operation,
3300 typename Model = typename HalPolicy::Model>
3301bool ConvertTranspose(const Operation& operation, const Model& model, ConversionData& data)
3302{
3303 using Operand = typename HalPolicy::Operand;
3304
3305 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3306 if (!input.IsValid())
3307 {
3308 return Fail("%s: Operation has invalid inputs", __func__);
3309 }
3310
3311 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3312 unsigned int rank = inputInfo.GetNumDimensions();
3313 if (rank > 4)
3314 {
3315 Fail("%s: Inputs with rank greater than 4 are not supported", __func__);
3316 }
3317
3318 // NOTE: Axis is an optional parameter to TRANSPOSE, therefore we do not want to generate a failure
3319 // if the operand index is out of bounds.
3320 const Operand* permOperand = GetInputOperand<HalPolicy>(operation, 1, model, false);
3321
3322 std::vector<int32_t> perm(rank);
3323 if (!permOperand)
3324 {
3325 // NOTE: If perm is not given, it is set to (n-1...0), where n is the rank of the tensor
3326 for (unsigned int i = rank; i > 0; i--)
3327 {
3328 perm[rank - i] = boost::numeric_cast<int> (i - 1);
3329 }
3330 }
3331 else
3332 {
3333 GetTensorInt32Values<HalPolicy>(*permOperand, perm, model, data);
3334 }
3335
3336 std::vector<uint32_t> outputDims(perm.begin(), perm.begin() + rank);
3337
3338 auto permutationVector = armnn::PermutationVector(outputDims.data(), outputDims.size());
3339 if (!permutationVector.IsEqual(NHWCToArmNN)
3340 && !permutationVector.IsEqual(ArmNNToNHWC)
3341 && !permutationVector.IsEqual({ 3, 2, 0, 1 }))
3342 {
3343 return Fail("%s: Only [0, 3, 1, 2], [0, 2, 3, 1] and [3, 2, 0, 1] permutations are supported.", __func__);
3344 }
3345
3346 armnn::PermuteDescriptor permuteDesc;
3347 permuteDesc.m_DimMappings = permutationVector;
3348
3349 const Operand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3350 if (!output)
3351 {
3352 return Fail("%s: Could not read output 0", __func__);
3353 }
3354
3355 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3356
3357 bool isSupported = false;
3358 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3359 IsPermuteSupported,
3360 data.m_Backends,
3361 isSupported,
3362 inputInfo,
3363 outputInfo,
3364 permuteDesc);
3365 if (!isSupported)
3366 {
3367 return false;
3368 }
3369
3370 armnn::IConnectableLayer* const layer = data.m_Network->AddPermuteLayer(permuteDesc);
3371 assert(layer != nullptr);
3372 input.Connect(layer->GetInputSlot(0));
3373
3374 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3375}
3376
3377template<typename HalPolicy,
Finn Williams23b87b32019-07-30 11:44:05 +01003378 typename HalOperation = typename HalPolicy::Operation,
Finn Williams0e4e4392019-07-31 10:56:27 +01003379 typename HalOperand = typename HalPolicy::Operand,
Finn Williams23b87b32019-07-30 11:44:05 +01003380 typename HalModel = typename HalPolicy::Model>
3381bool ConvertBatchToSpaceNd(const HalOperation& operation,
3382 const HalModel& model,
3383 ConversionData& data)
3384{
Finn Williams23b87b32019-07-30 11:44:05 +01003385
3386 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3387 if (!input.IsValid())
3388 {
3389 return Fail("%s: Operation has invalid inputs", __func__);
3390 }
3391
3392 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3393 if (!output)
3394 {
3395 return Fail("%s: Could not read output 0", __func__);
3396 }
3397
3398 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3399 if (IsDynamicTensor(outputInfo))
3400 {
3401 return Fail("%s: Dynamic output tensors are not supported", __func__);
3402 }
3403
3404 const HalOperand* blockOperand = GetInputOperand<HalPolicy>(operation, 1, model);
3405 if (!blockOperand)
3406 {
3407 return Fail("%s: Could not read input 1", __func__);
3408 }
3409
3410 // Convert the block operand to int32
3411 std::vector<int32_t> block;
3412 if (!GetTensorInt32Values<HalPolicy>(*blockOperand, block, model, data))
3413 {
3414 return Fail("%s: Input 1 has invalid values", __func__);
3415 }
3416
3417 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3418
3419 unsigned int rank = inputInfo.GetNumDimensions();
3420 if (rank != 4)
3421 {
3422 Fail("%s: Only inputs with rank equal to 4 are supported", __func__);
3423 }
3424
3425 if (std::any_of(block.cbegin(), block.cend(), [](int32_t i){ return i < 1; }))
3426 {
3427 return Fail("%s: Block sizes for each spatial dimension of the input tensor must be"
3428 " greater than or equal to 1", __func__);
3429 }
3430
3431 armnn::BatchToSpaceNdDescriptor batchToSpaceNdDesc;
3432 batchToSpaceNdDesc.m_BlockShape.assign(block.cbegin(), block.cend());
3433 batchToSpaceNdDesc.m_DataLayout = armnn::DataLayout::NHWC;
3434
3435 if (Is12Operand(*output))
3436 {
Finn Williams0e4e4392019-07-31 10:56:27 +01003437 batchToSpaceNdDesc.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 2, model, data);
Finn Williams23b87b32019-07-30 11:44:05 +01003438 }
3439 // Setting crops to 0,0 0,0 as it is not supported in Android NN API
3440 batchToSpaceNdDesc.m_Crops = {{0, 0}, {0, 0}};
3441
3442 bool isSupported = false;
3443 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3444 IsBatchToSpaceNdSupported,
3445 data.m_Backends,
3446 isSupported,
3447 inputInfo,
3448 outputInfo,
3449 batchToSpaceNdDesc);
3450 if (!isSupported)
3451 {
3452 return false;
3453 }
3454
3455 armnn::IConnectableLayer* const layer = data.m_Network->AddBatchToSpaceNdLayer(batchToSpaceNdDesc);
3456 assert(layer != nullptr);
3457 input.Connect(layer->GetInputSlot(0));
3458
3459 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3460}
Mike Kelly0a879362019-07-29 16:56:31 +01003461
Finn Williamsd74c5052019-07-30 17:06:00 +01003462template<typename HalPolicy,
3463 typename HalOperation = typename HalPolicy::Operation,
3464 typename HalOperand = typename HalPolicy::Operand,
3465 typename HalModel = typename HalPolicy::Model>
3466bool ConvertSpaceToBatchNd(const HalOperation& operation, const HalModel& model, ConversionData& data)
3467{
3468 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3469 if (!input.IsValid())
3470 {
3471 return Fail("%s: Operation has invalid inputs", __func__);
3472 }
3473
3474 const armnn::TensorInfo& inputInfo = input.GetTensorInfo();
3475 unsigned int rank = inputInfo.GetNumDimensions();
3476 unsigned int spatialDim = rank - 2;
3477
3478 if (rank != 4)
3479 {
3480 Fail("%s: Only inputs with rank 4 are supported", __func__);
3481 }
3482
3483 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3484 if (!output)
3485 {
3486 return Fail("%s: Could not read output 0", __func__);
3487 }
3488
3489 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3490 if (IsDynamicTensor(outputInfo))
3491 {
3492 return Fail("%s: Dynamic output tensors are not supported", __func__);
3493 }
3494
3495 const HalOperand* blockShapeOperand = GetInputOperand<HalPolicy>(operation, 1, model);
3496 const HalOperand* paddingsOperand = GetInputOperand<HalPolicy>(operation, 2, model);
3497
3498 armnn::TensorShape blockShapeOperandShape = GetTensorShapeForOperand(*blockShapeOperand);
3499 if (blockShapeOperandShape.GetNumDimensions() != 1 || blockShapeOperandShape.GetNumElements() != spatialDim)
3500 {
3501 return Fail("%s: Operation has invalid block shape operand: expected shape [%d]", __func__, spatialDim);
3502 }
3503
3504 std::vector<int32_t> blockShape;
3505 GetTensorInt32Values<HalPolicy>(*blockShapeOperand, blockShape, model, data);
3506 if (std::any_of(blockShape.cbegin(), blockShape.cend(), [](int32_t i){ return i < 1; }))
3507 {
3508 return Fail("%s: Block shape must be at least 1 in all dimensions.", __func__);
3509 }
3510
3511 armnn::TensorShape paddingsOperandShape = GetTensorShapeForOperand(*paddingsOperand);
3512 if (paddingsOperandShape.GetNumDimensions() != 2 || paddingsOperandShape.GetNumElements() != 2 * spatialDim)
3513 {
3514 return Fail("%s: Operation has invalid paddings operand: expected shape [%d, 2]", __func__, spatialDim);
3515 }
3516
3517 std::vector<std::pair<unsigned int, unsigned int>> paddingList;
3518 std::vector<int32_t> paddings;
3519 GetTensorInt32Values<HalPolicy>(*paddingsOperand, paddings, model, data);
3520 for (unsigned int i = 0; i < paddings.size() - 1; i += 2)
3521 {
3522 int paddingBeforeInput = paddings[i];
3523 int paddingAfterInput = paddings[i + 1];
3524 if (paddingBeforeInput < 0 || paddingAfterInput < 0)
3525 {
3526 return Fail("%s: Operation has invalid paddings operand, invalid padding values.", __func__);
3527 }
3528
3529 paddingList.emplace_back((unsigned int) paddingBeforeInput, (unsigned int) paddingAfterInput);
3530 }
3531
3532 armnn::SpaceToBatchNdDescriptor descriptor;
3533 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
3534 descriptor.m_BlockShape.assign(blockShape.cbegin(), blockShape.cend());
3535 descriptor.m_PadList.assign(paddingList.cbegin(), paddingList.cend());
3536
3537 if (Is12Operand(*output))
3538 {
3539 descriptor.m_DataLayout = OptionalDataLayout<HalPolicy>(operation, 3, model, data);
3540 }
3541
3542 bool isSupported = false;
3543 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3544 IsSpaceToBatchNdSupported,
3545 data.m_Backends,
3546 isSupported,
3547 inputInfo,
3548 outputInfo,
3549 descriptor);
3550 if (!isSupported)
3551 {
3552 return false;
3553 }
3554
3555 armnn::IConnectableLayer* const layer = data.m_Network->AddSpaceToBatchNdLayer(descriptor);
3556 assert(layer != nullptr);
3557 input.Connect(layer->GetInputSlot(0));
3558
3559 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3560}
3561
Kevin May407718f2019-09-09 14:46:41 +01003562template<typename HalPolicy,
3563 typename HalOperation = typename HalPolicy::Operation,
3564 typename HalModel = typename HalPolicy::Model>
3565bool ConvertAbs(const HalOperation& operation, const HalModel& model, ConversionData& data)
3566{
3567 LayerInputHandle input = ConvertToLayerInputHandle<HalPolicy>(operation, 0, model, data);
3568
3569 if (!input.IsValid())
3570 {
3571 return Fail("%s: Operation has invalid input", __func__);
3572 }
3573
3574 using HalOperand = typename HalPolicy::Operand;
3575 const HalOperand* output = GetOutputOperand<HalPolicy>(operation, 0, model);
3576 if (!output)
3577 {
3578 return Fail("%s: Could not read output 0", __func__);
3579 }
3580
3581 const armnn::TensorInfo& outputInfo = GetTensorInfoForOperand(*output);
3582 if (IsDynamicTensor(outputInfo))
3583 {
3584 return Fail("%s: Dynamic output tensors are not supported", __func__);
3585 }
3586
3587 bool isSupported = false;
3588 FORWARD_LAYER_SUPPORT_FUNC(__func__,
3589 IsAbsSupported,
3590 data.m_Backends,
3591 isSupported,
3592 input.GetTensorInfo(),
3593 outputInfo);
3594
3595 if (!isSupported)
3596 {
3597 return false;
3598 }
3599
3600 armnn::IConnectableLayer* const layer = data.m_Network->AddAbsLayer();
3601 assert(layer != nullptr);
3602 input.Connect(layer->GetInputSlot(0));
3603
3604 return SetupAndTrackLayerOutputSlot<HalPolicy>(operation, 0, *layer, model, data);
3605}
3606
3607
saoste01b8471482018-10-10 09:44:51 +01003608} // namespace armnn_driver