blob: 57d3b460f524d506f3b4f5370e6da3229f2642c0 [file] [log] [blame]
Matthew Sloyana7a12f52021-05-06 10:05:28 +01001//
Cathal Corbett53837672022-09-01 11:34:37 +01002// Copyright © 2021,2022 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyana7a12f52021-05-06 10:05:28 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <tensorflow/lite/builtin_ops.h>
9#include <tensorflow/lite/c/builtin_op_data.h>
10#include <tensorflow/lite/c/common.h>
11#include <tensorflow/lite/minimal_logging.h>
12
13namespace armnnDelegate
14{
15
16TfLiteStatus VisitPackOperator(DelegateData& delegateData,
17 TfLiteContext* tfLiteContext,
18 TfLiteNode* tfLiteNode,
19 int nodeIndex,
20 int32_t operatorCode)
21{
22 unsigned int numInputs = tfLiteNode->inputs->size;
23 if (numInputs < 1)
24 {
25 TF_LITE_MAYBE_KERNEL_LOG(
26 tfLiteContext, "TfLiteArmnnDelegate: Must have at least one input in (%d != %d) in node #%d",
27 1, numInputs, nodeIndex);
28 return kTfLiteError;
29 }
30
31 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
32
33 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
34
35 // Validate all inputs and get TensorInfo
36 std::vector<armnn::TensorInfo> inputTensorInfos;
37 for (unsigned int i = 0; i < numInputs; ++i)
38 {
39 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[i]];
40 if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex))
41 {
42 return kTfLiteError;
43 }
44
45 armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
46 inputTensorInfos.emplace_back(inputTensorInfo);
47 }
48
49 // Convert input tensors to const armnn::TensorInfo* type for FORWARD_LAYER_SUPPORT_FUNC.
50 std::vector<const armnn::TensorInfo*> inputConstTensorInfos;
51 std::transform(inputTensorInfos.begin(),
52 inputTensorInfos.end(),
53 std::back_inserter(inputConstTensorInfos),
54 [](armnn::TensorInfo& t)->const armnn::TensorInfo*{ return &t; });
55
56 // Validate output and get TensorInfo
57 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
58 if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex))
59 {
60 return kTfLiteError;
61 }
62
Sadik Armagan90a119b2022-08-05 16:12:49 +010063 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Matthew Sloyana7a12f52021-05-06 10:05:28 +010064
65 armnn::StackDescriptor desc;
66 desc.m_NumInputs = static_cast<uint32_t>(numInputs);
67
68 // Get axis from TfLite parameters
69 auto* params = reinterpret_cast<TfLitePackParams*>(tfLiteNode->builtin_data);
70 desc.m_Axis = static_cast<uint32_t>(params->axis);
71
72 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
73 desc.m_InputShape = inputTensorInfos[0].GetShape();
74
75 // Check if supported
76 bool isSupported = false;
Cathal Corbett53837672022-09-01 11:34:37 +010077 armnn::BackendId setBackend;
Matthew Sloyana7a12f52021-05-06 10:05:28 +010078 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
79 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000080 FORWARD_LAYER_SUPPORT_FUNC("STACK",
Matthew Sloyana7a12f52021-05-06 10:05:28 +010081 tfLiteContext,
82 IsStackSupported,
83 delegateData.m_Backends,
84 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +010085 setBackend,
Matthew Sloyana7a12f52021-05-06 10:05:28 +010086 inputConstTensorInfos,
87 outputTensorInfo,
88 desc);
89 };
90
91 // If the m_Network is a nullptr, this signals that a prerequisite TfLite callback is required to clarify the
92 // support for the operator
93 // If supported, VisitPackOperator will be called again to add the layer to the network as seen below
94 if (!delegateData.m_Network)
95 {
96 validateFunc(outputTensorInfo, isSupported);
97 return isSupported ? kTfLiteOk : kTfLiteError;
98 }
99
100 // The TfLite Pack operator is equivalent to the ArmNN Stack operator
101 armnn::IConnectableLayer* layer = delegateData.m_Network->AddStackLayer(desc);
Cathal Corbett53837672022-09-01 11:34:37 +0100102 layer->SetBackendId(setBackend);
Matthew Sloyana7a12f52021-05-06 10:05:28 +0100103 ARMNN_ASSERT(layer != nullptr);
104
Sadik Armagan529195f2022-01-14 12:56:35 +0000105 // Connect the Constant Inputs
106 auto inputsTensorsProcess = ProcessInputs(layer,
107 delegateData,
108 tfLiteContext,
109 tfLiteNode);
110 if (inputsTensorsProcess == kTfLiteError)
111 {
112 return inputsTensorsProcess;
113 }
114
Matthew Sloyana7a12f52021-05-06 10:05:28 +0100115 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
116 outputSlot.SetTensorInfo(outputTensorInfo);
117
118 // Connect
119 return Connect(layer, tfLiteNode, delegateData);
120}
121
122} // namespace armnnDelegate