• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "DelegateUtils.hpp"
9 
10 #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 
15 namespace armnnDelegate
16 {
17 
ValidateActivationOperator(DelegateData & delegateData,TfLiteContext * tfLiteContext,const armnn::TensorInfo & inputInfo,const armnn::TensorInfo & outputInfo,armnn::ActivationDescriptor & activationDesc)18 TfLiteStatus 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 
VisitActivationOperator(DelegateData & delegateData,TfLiteContext * tfLiteContext,TfLiteNode * tfLiteNode,int nodeIndex,int32_t operatorCode)41 TfLiteStatus VisitActivationOperator(DelegateData& delegateData,
42                                      TfLiteContext* tfLiteContext,
43                                      TfLiteNode* tfLiteNode,
44                                      int nodeIndex,
45                                      int32_t operatorCode)
46 {
47     TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
48     TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
49 
50     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     }
69 
70     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);
120 }
121 
122 } // namespace armnnDelegate
123