blob: 5e8d87611027cfa85d9f93fcbb43bc3c1dd721c8 [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
David Monahan0cf84422020-11-16 15:53:03 +00008#include "DelegateUtils.hpp"
Finn Williams6f9f9902020-11-13 13:23:15 +00009
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
David Monahan0cf84422020-11-16 15:53:03 +000018TfLiteStatus ValidateActivationOperator(DelegateData& delegateData,
19 TfLiteContext* tfLiteContext,
20 const armnn::TensorInfo& inputInfo,
21 const armnn::TensorInfo& outputInfo,
22 armnn::ActivationDescriptor& activationDesc)
23{
24 bool isSupported = false;
25 auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported)
26 {
27 FORWARD_LAYER_SUPPORT_FUNC(__func__,
28 tfLiteContext,
29 IsActivationSupported,
30 delegateData.m_Backends,
31 isSupported,
32 inputInfo,
33 outputInfo,
34 activationDesc);
35 };
36
37 validateFunc(outputInfo, isSupported);
38 return isSupported ? kTfLiteOk : kTfLiteError;
39}
40
Sadik Armagan62483be2020-10-23 17:14:43 +010041TfLiteStatus VisitActivationOperator(DelegateData& delegateData,
42 TfLiteContext* tfLiteContext,
43 TfLiteNode* tfLiteNode,
44 int nodeIndex,
David Monahan0cf84422020-11-16 15:53:03 +000045 int32_t operatorCode)
Sadik Armagan62483be2020-10-23 17:14:43 +010046{
David Monahan0cf84422020-11-16 15:53:03 +000047 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
48 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
Finn Williams6f9f9902020-11-13 13:23:15 +000049
David Monahan0cf84422020-11-16 15:53:03 +000050 const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors;
51 const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]];
52 if (IsDynamicTensor(tfLiteInputTensor))
53 {
54 TF_LITE_MAYBE_KERNEL_LOG(
55 tfLiteContext,
56 "TfLiteArmnnDelegate: Dynamic input tensors are not supported in node #%d: ",
57 nodeIndex);
58 return kTfLiteError;
59 }
60 const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]];
61 if (IsDynamicTensor(tfLiteOutputTensor))
62 {
63 TF_LITE_MAYBE_KERNEL_LOG(
64 tfLiteContext,
65 "TfLiteArmnnDelegate: Dynamic output tensors are not supported in node #%d: ",
66 nodeIndex);
67 return kTfLiteError;
68 }
Finn Williams6f9f9902020-11-13 13:23:15 +000069
David Monahan0cf84422020-11-16 15:53:03 +000070 const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor);
71 const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor);
72
73 armnn::ActivationDescriptor activationDesc;
74 switch(operatorCode)
75 {
76 case kTfLiteBuiltinRelu:
77 {
78 activationDesc.m_Function = armnn::ActivationFunction::ReLu;
79 break;
80 }
81 case kTfLiteBuiltinRelu6:
82 {
83 activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu;
84 activationDesc.m_A = 6.0f;
85 break;
86 }
87 case kTfLiteBuiltinLogistic:
88 {
89 activationDesc.m_Function = armnn::ActivationFunction::Sigmoid;
90 break;
91 }
92 case kTfLiteBuiltinTanh:
93 {
94 activationDesc.m_Function = armnn::ActivationFunction::TanH;
95 activationDesc.m_A = 1.0f;
96 activationDesc.m_B = 1.0f;
97 break;
98 }
99 default:
100 {
101 return kTfLiteError;
102 }
103 }
104 if (!delegateData.m_Network)
105 {
106 return ValidateActivationOperator(delegateData,
107 tfLiteContext,
108 inputTensorInfo,
109 outputTensorInfo,
110 activationDesc);
111 }
112 armnn::IConnectableLayer* activationLayer = delegateData.m_Network->AddActivationLayer(activationDesc);
113 ARMNN_ASSERT(activationLayer != nullptr);
114
115 armnn::IOutputSlot& outputSlot = activationLayer->GetOutputSlot(0);
116 outputSlot.SetTensorInfo(outputTensorInfo);
117
118 // Connect
119 return Connect(activationLayer, tfLiteNode, delegateData);
Sadik Armagan62483be2020-10-23 17:14:43 +0100120}
121
122} // namespace armnnDelegate