blob: e79133e15cea8413c7796e44adc1a786f24f3534 [file] [log] [blame]
Sadik Armagan62483be2020-10-23 17:14:43 +01001//
Sadik Armagan90a119b2022-08-05 16:12:49 +01002// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan62483be2020-10-23 17:14:43 +01003// 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 Armagan90a119b2022-08-05 16:12:49 +010055 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true);
Sadik Armagan29b49cf2021-02-22 18:09:07 +000056
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;
Cathal Corbett53837672022-09-01 11:34:37 +010075 armnn::BackendId setBackend;
Sadik Armagan29b49cf2021-02-22 18:09:07 +000076 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
77 {
Sadik Armaganbfa767c2022-02-09 14:58:03 +000078 FORWARD_LAYER_SUPPORT_FUNC("FILL",
Sadik Armagan29b49cf2021-02-22 18:09:07 +000079 tfLiteContext,
80 IsFillSupported,
81 delegateData.m_Backends,
82 isSupported,
Cathal Corbett53837672022-09-01 11:34:37 +010083 setBackend,
Sadik Armagan29b49cf2021-02-22 18:09:07 +000084 inputTensorInfo,
85 outInfo,
86 descriptor);
87 };
88
89 if (!delegateData.m_Network)
90 {
91 validateFunc(outputTensorInfo, isSupported);
92 return isSupported ? kTfLiteOk : kTfLiteError;
93 }
94
95 armnn::IConnectableLayer* layer = delegateData.m_Network->AddFillLayer(descriptor);
Cathal Corbett53837672022-09-01 11:34:37 +010096 layer->SetBackendId(setBackend);
Sadik Armagan29b49cf2021-02-22 18:09:07 +000097 ARMNN_ASSERT(layer != nullptr);
98
99 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
100 outputSlot.SetTensorInfo(outputTensorInfo);
101
Sadik Armaganf7ac72c2021-05-05 15:03:50 +0100102 auto inputsTensorsProcess = ProcessInputs(layer,
103 delegateData,
104 tfLiteContext,
105 tfLiteNode);
106 if (inputsTensorsProcess == kTfLiteError)
Sadik Armagan29b49cf2021-02-22 18:09:07 +0000107 {
Sadik Armaganf7ac72c2021-05-05 15:03:50 +0100108 return inputsTensorsProcess;
Sadik Armagan29b49cf2021-02-22 18:09:07 +0000109 }
110
111 return Connect(layer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100112}
113
114} // namespace armnnDelegate