blob: 859f5fffc2aa8ee45f4bc2b915bf08d83ca04cf5 [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
Sadik Armagan62483be2020-10-23 17:14:43 +010010#include <tensorflow/lite/builtin_ops.h>
11#include <tensorflow/lite/c/builtin_op_data.h>
12#include <tensorflow/lite/c/common.h>
13#include <tensorflow/lite/minimal_logging.h>
14
15namespace armnnDelegate
16{
17
18TfLiteStatus VisitFillOperator(DelegateData& delegateData,
19 TfLiteContext* tfLiteContext,
20 TfLiteNode* tfLiteNode,
21 int nodeIndex,
Sadik Armagan29b49cf2021-02-22 18:09:07 +000022 int32_t tfLiteFillOperatorCode)
Sadik Armagan62483be2020-10-23 17:14:43 +010023{
Sadik Armagan29b49cf2021-02-22 18:09:07 +000024 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
Finn Williams6f9f9902020-11-13 13:23:15 +000025
Sadik Armagan29b49cf2021-02-22 18:09:07 +000026 switch(tfLiteFillOperatorCode)
27 {
28 case kTfLiteBuiltinFill:
29 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
30 break;
31 default:
32 return kTfLiteError;
33 }
34
35 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
36 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
37 if (!IsValid(tfLiteContext, tfLiteInputTensor, tfLiteFillOperatorCode, nodeIndex))
38 {
39 return kTfLiteError;
40 }
41
42 const TfLiteTensor& tfLiteFillTensor = tfLiteTensors[tfLiteNode->inputs->data[1]];
43 if (!IsValid(tfLiteContext, tfLiteFillTensor, tfLiteFillOperatorCode, nodeIndex))
44 {
45 return kTfLiteError;
46 }
47
48 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
49 if (!IsValid(tfLiteContext, tfLiteOutputTensor, tfLiteFillOperatorCode, nodeIndex))
50 {
51 return kTfLiteError;
52 }
53
54 armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
Sadik Armagan29b49cf2021-02-22 18:09:07 +000055 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor);
56
57 armnn::FillDescriptor descriptor;
58 switch (tfLiteFillTensor.type)
59 {
60 case kTfLiteFloat32:
61 descriptor.m_Value = tflite::GetTensorData<float>(&tfLiteFillTensor)[0];
62 break;
63 case kTfLiteInt32:
64 descriptor.m_Value = tflite::GetTensorData<int32_t>(&tfLiteFillTensor)[0];
65 break;
66 default:
67 TF_LITE_MAYBE_KERNEL_LOG(
68 tfLiteContext,
69 "TfLiteArmnnDelegate: FILL value data type is not supported in operator #%d node #%d: ",
70 tfLiteFillOperatorCode, nodeIndex);
71 return kTfLiteError;
72 }
73
74 bool isSupported = false;
75 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
76 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000077 FORWARD_LAYER_SUPPORT_FUNC("FILL",
Sadik Armagan29b49cf2021-02-22 18:09:07 +000078 tfLiteContext,
79 IsFillSupported,
80 delegateData.m_Backends,
81 isSupported,
82 inputTensorInfo,
83 outInfo,
84 descriptor);
85 };
86
87 if (!delegateData.m_Network)
88 {
89 validateFunc(outputTensorInfo, isSupported);
90 return isSupported ? kTfLiteOk : kTfLiteError;
91 }
92
93 armnn::IConnectableLayer* layer = delegateData.m_Network->AddFillLayer(descriptor);
94 ARMNN_ASSERT(layer != nullptr);
95
96 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
97 outputSlot.SetTensorInfo(outputTensorInfo);
98
Sadik Armaganf7ac72c2021-05-05 15:03:50 +010099 auto inputsTensorsProcess = ProcessInputs(layer,
100 delegateData,
101 tfLiteContext,
102 tfLiteNode);
103 if (inputsTensorsProcess == kTfLiteError)
Sadik Armagan29b49cf2021-02-22 18:09:07 +0000104 {
Sadik Armaganf7ac72c2021-05-05 15:03:50 +0100105 return inputsTensorsProcess;
Sadik Armagan29b49cf2021-02-22 18:09:07 +0000106 }
107
108 return Connect(layer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100109}
110
111} // namespace armnnDelegate