blob: 5e130b27f2f477d7eb88755f6b4c414f738c49c7 [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Finn Williams6f9f9902020-11-13 13:23:15 +00008#include <armnn/utility/IgnoreUnused.hpp>
9
David Monahan1670b0c2020-11-18 14:40:27 +000010#include "DelegateUtils.hpp"
11
Sadik Armagan62483be2020-10-23 17:14:43 +010012#include <tensorflow/lite/builtin_ops.h>
13#include <tensorflow/lite/c/builtin_op_data.h>
14#include <tensorflow/lite/c/common.h>
15#include <tensorflow/lite/minimal_logging.h>
David Monahan1670b0c2020-11-18 14:40:27 +000016#include <numeric>
Sadik Armagan62483be2020-10-23 17:14:43 +010017
18namespace armnnDelegate
19{
20
David Monahan1670b0c2020-11-18 14:40:27 +000021TfLiteStatus CreateOutputTensorShape(const armnn::TensorInfo& inputTensorInfo,
Matthew Sloyanf00f6c22020-12-07 13:33:24 +000022 const std::vector<int32_t>& targetShape,
23 armnn::ReshapeDescriptor& reshapeDesc)
David Monahan1670b0c2020-11-18 14:40:27 +000024{
25 std::vector<unsigned int> outputDims(targetShape.begin(), targetShape.end());
26 const auto stretchDim = std::find(targetShape.begin(), targetShape.end(), -1);
27
28 if (stretchDim != targetShape.end())
29 {
30 if (std::find(std::next(stretchDim), targetShape.end(), -1) != targetShape.end())
31 {
32 // Return kTfLiteError and log the error after returning
33 return kTfLiteError;
34 }
35
36 auto targetNumElements =
37 armnn::numeric_cast<unsigned int>(
38 std::accumulate(targetShape.begin(), targetShape.end(), -1, std::multiplies<int32_t>()));
39
40 auto stretchIndex = static_cast<size_t>(std::distance(targetShape.begin(), stretchDim));
41 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
42 }
43
44 armnn::TensorShape outputShape = armnn::TensorShape(static_cast<unsigned int>(outputDims.size()),
45 outputDims.data());
46 reshapeDesc.m_TargetShape = outputShape;
47 return kTfLiteOk;
48}
49
Sadik Armagan62483be2020-10-23 17:14:43 +010050TfLiteStatus VisitReshapeOperator(DelegateData& delegateData,
51 TfLiteContext* tfLiteContext,
52 TfLiteNode* tfLiteNode,
53 int nodeIndex,
54 int32_t operatorCode)
55{
David Monahan1670b0c2020-11-18 14:40:27 +000056 auto numInputs = tfLiteNode->inputs->size;
Finn Williams6f9f9902020-11-13 13:23:15 +000057
David Monahan1670b0c2020-11-18 14:40:27 +000058 if (numInputs == 2)
59 {
60 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
61 }
62 else
63 {
64 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
65 }
66 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
67
68 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
69 const TfLiteTensor& tfLiteInputTensor0 = tfLiteTensors[tfLiteNode->inputs->data[0]];
Matthew Sloyanf00f6c22020-12-07 13:33:24 +000070 if (!IsValid(tfLiteContext, tfLiteInputTensor0, operatorCode, nodeIndex))
David Monahan1670b0c2020-11-18 14:40:27 +000071 {
David Monahan1670b0c2020-11-18 14:40:27 +000072 return kTfLiteError;
73 }
74
75 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
Matthew Sloyanf00f6c22020-12-07 13:33:24 +000076 if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex))
David Monahan1670b0c2020-11-18 14:40:27 +000077 {
David Monahan1670b0c2020-11-18 14:40:27 +000078 return kTfLiteError;
79 }
80
81 const armnn::TensorInfo& inputTensorInfo0 = GetTensorInfoForTfLiteTensor(tfLiteInputTensor0);
82 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor);
83
84 armnn::ReshapeDescriptor reshapeDesc;
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +000085 std::vector<int32_t> targetShape;
Matthew Sloyanf00f6c22020-12-07 13:33:24 +000086 bool shapeSet = false;
David Monahan1670b0c2020-11-18 14:40:27 +000087
88 // The new shape can be defined by either a second input tensor or by a builtin option, we need to check for both.
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +000089 if (numInputs == 2)
David Monahan1670b0c2020-11-18 14:40:27 +000090 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +000091 // Get shape from the second input tensor
92 const TfLiteTensor& tfLiteShapeInputTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
Matthew Sloyanf00f6c22020-12-07 13:33:24 +000093 if (!IsValid(tfLiteContext, tfLiteShapeInputTensor, operatorCode, nodeIndex))
David Monahane03d9c22020-11-20 09:58:54 +000094 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +000095 return kTfLiteError;
96 }
97
98 if (tfLiteShapeInputTensor.dims->size != 1)
99 {
100 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
101 "TfLiteArmnnDelegate: Target 'shape' input is not a 1D tensor in "
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000102 "operator #%d node #%d: Falling back to TfLiteOptions.",
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000103 operatorCode, nodeIndex);
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000104 }
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000105 else
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000106 {
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000107 // Get the shape data out of the input tensor
108 auto* shapeTensorDataPtr = tflite::GetTensorData<int32_t>(&tfLiteShapeInputTensor);
109 auto shapeTensorNumValues = tfLiteShapeInputTensor.dims->data[0];
110 for (auto i=0; i < shapeTensorNumValues; ++i)
111 {
112 targetShape.push_back(*(shapeTensorDataPtr+i));
113 }
114 shapeSet = true;
David Monahane03d9c22020-11-20 09:58:54 +0000115 }
116 }
Matthew Sloyanf00f6c22020-12-07 13:33:24 +0000117 if (!shapeSet)
David Monahane03d9c22020-11-20 09:58:54 +0000118 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000119 // Get shape from the builtin data
120 TfLiteReshapeParams* reshapeOptions = reinterpret_cast<TfLiteReshapeParams*>(tfLiteNode->builtin_data);
121
122 if (reshapeOptions != nullptr)
David Monahane03d9c22020-11-20 09:58:54 +0000123 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000124 // Options might be set without valid data. we need to check the dimensions are in a valid range.
125 if (reshapeOptions->num_dimensions > 0 && reshapeOptions->num_dimensions <= 8)
David Monahane03d9c22020-11-20 09:58:54 +0000126 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000127 for (int i=0; i < reshapeOptions->num_dimensions; ++i)
128 {
129 targetShape.push_back(reshapeOptions->shape[i]);
130 }
David Monahane03d9c22020-11-20 09:58:54 +0000131 }
132 }
133 else
David Monahan1670b0c2020-11-18 14:40:27 +0000134 {
135 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
David Monahane03d9c22020-11-20 09:58:54 +0000136 "Target shape not defined in reshape parameters or input tensor. "
137 "At least one method required in operator #%d node #%d: ",
David Monahan1670b0c2020-11-18 14:40:27 +0000138 operatorCode, nodeIndex);
139 return kTfLiteError;
140 }
141 }
David Monahane03d9c22020-11-20 09:58:54 +0000142
143 // Use the data to create the required tensor shape.
144 if (CreateOutputTensorShape(inputTensorInfo0, targetShape, reshapeDesc) != kTfLiteOk)
David Monahan1670b0c2020-11-18 14:40:27 +0000145 {
146 TF_LITE_MAYBE_KERNEL_LOG(tfLiteContext,
David Monahane03d9c22020-11-20 09:58:54 +0000147 "TfLiteArmnnDelegate: At most one component of shape can be -1 in: "
148 "operator #%d node #%d: ",
David Monahan1670b0c2020-11-18 14:40:27 +0000149 operatorCode, nodeIndex);
David Monahane03d9c22020-11-20 09:58:54 +0000150 return kTfLiteError;
151 }
152
153 if (reshapeDesc.m_TargetShape.GetNumElements() != inputTensorInfo0.GetNumElements())
154 {
Narumol Prangnawarat7f6c6672020-11-24 18:40:42 +0000155 TF_LITE_MAYBE_KERNEL_LOG(
156 tfLiteContext,
157 "TfLiteArmnnDelegate: Reshape, number of elements in output shape does not match input "
158 "operator #%d node #%d: ",
159 operatorCode, nodeIndex);
David Monahane03d9c22020-11-20 09:58:54 +0000160 return kTfLiteError;
David Monahan1670b0c2020-11-18 14:40:27 +0000161 }
162
163 bool isSupported = false;
164 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
165 {
166 FORWARD_LAYER_SUPPORT_FUNC(__func__,
167 tfLiteContext,
168 IsReshapeSupported,
169 delegateData.m_Backends,
170 isSupported,
171 inputTensorInfo0,
172 outInfo,
173 reshapeDesc);
174 };
175
176 if (!delegateData.m_Network)
177 {
178 validateFunc(outputTensorInfo, isSupported);
179 return isSupported ? kTfLiteOk : kTfLiteError;
180 }
181
182 armnn::IConnectableLayer* layer = delegateData.m_Network->AddReshapeLayer(reshapeDesc);
183 ARMNN_ASSERT(layer != nullptr);
184
185 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
186 outputSlot.SetTensorInfo(outputTensorInfo);
187
188 // Connect
189 return Connect(layer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100190}
191
192TfLiteStatus VisitSqueezeOperator(DelegateData& delegateData,
193 TfLiteContext* tfLiteContext,
194 TfLiteNode* tfLiteNode,
195 int nodeIndex,
196 int32_t operatorCode)
197{
Finn Williams6f9f9902020-11-13 13:23:15 +0000198 armnn::IgnoreUnused(delegateData,
199 tfLiteContext,
200 tfLiteNode,
201 nodeIndex,
202 operatorCode);
203
Sadik Armagan62483be2020-10-23 17:14:43 +0100204 return kTfLiteError;
205}
206
207TfLiteStatus VisitExpandDimsOperator(DelegateData& delegateData,
208 TfLiteContext* tfLiteContext,
209 TfLiteNode* tfLiteNode,
210 int nodeIndex,
211 int32_t operatorCode)
212{
Finn Williams6f9f9902020-11-13 13:23:15 +0000213 armnn::IgnoreUnused(delegateData,
214 tfLiteContext,
215 tfLiteNode,
216 nodeIndex,
217 operatorCode);
218
Sadik Armagan62483be2020-10-23 17:14:43 +0100219 return kTfLiteError;
220}
221
222} // namespace armnnDelegate