blob: 2dc266bc471819e227a6fa0a0aaa0c2216c6fe3f [file] [log] [blame]
Matthew Sloyana7a12f52021-05-06 10:05:28 +01001//
2// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3// 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
63 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor);
64
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;
77 auto validateFunc = [&](const armnn::TensorInfo& outputTensorInfo, bool& isSupported)
78 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000079 FORWARD_LAYER_SUPPORT_FUNC("STACK",
Matthew Sloyana7a12f52021-05-06 10:05:28 +010080 tfLiteContext,
81 IsStackSupported,
82 delegateData.m_Backends,
83 isSupported,
84 inputConstTensorInfos,
85 outputTensorInfo,
86 desc);
87 };
88
89 // If the m_Network is a nullptr, this signals that a prerequisite TfLite callback is required to clarify the
90 // support for the operator
91 // If supported, VisitPackOperator will be called again to add the layer to the network as seen below
92 if (!delegateData.m_Network)
93 {
94 validateFunc(outputTensorInfo, isSupported);
95 return isSupported ? kTfLiteOk : kTfLiteError;
96 }
97
98 // The TfLite Pack operator is equivalent to the ArmNN Stack operator
99 armnn::IConnectableLayer* layer = delegateData.m_Network->AddStackLayer(desc);
100 ARMNN_ASSERT(layer != nullptr);
101
Sadik Armagan529195f2022-01-14 12:56:35 +0000102 // Connect the Constant Inputs
103 auto inputsTensorsProcess = ProcessInputs(layer,
104 delegateData,
105 tfLiteContext,
106 tfLiteNode);
107 if (inputsTensorsProcess == kTfLiteError)
108 {
109 return inputsTensorsProcess;
110 }
111
Matthew Sloyana7a12f52021-05-06 10:05:28 +0100112 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
113 outputSlot.SetTensorInfo(outputTensorInfo);
114
115 // Connect
116 return Connect(layer, tfLiteNode, delegateData);
117}
118
119} // namespace armnnDelegate