Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matthew Sloyan | 1157232 | 2023-03-16 10:17:51 +0000 | [diff] [blame] | 8 | #include <ClassicDelegateUtils.hpp> |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 9 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 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 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 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; |
Keith Davis | 892fafe | 2020-11-26 17:40:35 +0000 | [diff] [blame] | 25 | auto validateFunc = [&](const armnn::TensorInfo& outputInfo, bool& isSupported) |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 26 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 27 | FORWARD_LAYER_SUPPORT_FUNC("ACTIVATION", |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 28 | tfLiteContext, |
| 29 | IsActivationSupported, |
| 30 | delegateData.m_Backends, |
| 31 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 32 | armnn::BackendId(), |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 33 | inputInfo, |
| 34 | outputInfo, |
| 35 | activationDesc); |
| 36 | }; |
| 37 | |
| 38 | validateFunc(outputInfo, isSupported); |
| 39 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 40 | } |
| 41 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 42 | TfLiteStatus VisitActivationOperator(DelegateData& delegateData, |
| 43 | TfLiteContext* tfLiteContext, |
| 44 | TfLiteNode* tfLiteNode, |
| 45 | int nodeIndex, |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 46 | int32_t operatorCode) |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 47 | { |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 48 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 49 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 50 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 51 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 52 | const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 53 | if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 54 | { |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 55 | return kTfLiteError; |
| 56 | } |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 57 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 58 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 59 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 60 | { |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 61 | return kTfLiteError; |
| 62 | } |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 63 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 64 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 65 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 66 | |
| 67 | armnn::ActivationDescriptor activationDesc; |
| 68 | switch(operatorCode) |
| 69 | { |
| 70 | case kTfLiteBuiltinRelu: |
| 71 | { |
| 72 | activationDesc.m_Function = armnn::ActivationFunction::ReLu; |
| 73 | break; |
| 74 | } |
| 75 | case kTfLiteBuiltinRelu6: |
| 76 | { |
| 77 | activationDesc.m_Function = armnn::ActivationFunction::BoundedReLu; |
| 78 | activationDesc.m_A = 6.0f; |
| 79 | break; |
| 80 | } |
| 81 | case kTfLiteBuiltinLogistic: |
| 82 | { |
| 83 | activationDesc.m_Function = armnn::ActivationFunction::Sigmoid; |
| 84 | break; |
| 85 | } |
| 86 | case kTfLiteBuiltinTanh: |
| 87 | { |
| 88 | activationDesc.m_Function = armnn::ActivationFunction::TanH; |
| 89 | activationDesc.m_A = 1.0f; |
| 90 | activationDesc.m_B = 1.0f; |
| 91 | break; |
| 92 | } |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 93 | case kTfLiteBuiltinElu: |
| 94 | { |
| 95 | activationDesc.m_Function = armnn::ActivationFunction::Elu; |
| 96 | activationDesc.m_A = 1.0f; |
| 97 | break; |
| 98 | } |
| 99 | case kTfLiteBuiltinHardSwish: |
| 100 | { |
| 101 | activationDesc.m_Function = armnn::ActivationFunction::HardSwish; |
| 102 | break; |
| 103 | } |
Tianle Cheng | ae93173 | 2023-07-28 11:53:04 +0100 | [diff] [blame] | 104 | case kTfLiteBuiltinLeakyRelu: |
| 105 | { |
| 106 | // Get the alpha param from builtin data |
| 107 | auto* leakyReluParameters = reinterpret_cast<TfLiteLeakyReluParams*>(tfLiteNode->builtin_data); |
| 108 | activationDesc.m_Function = armnn::ActivationFunction::LeakyReLu; |
| 109 | activationDesc.m_A = leakyReluParameters->alpha; |
| 110 | break; |
| 111 | } |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 112 | default: |
| 113 | { |
| 114 | return kTfLiteError; |
| 115 | } |
| 116 | } |
| 117 | if (!delegateData.m_Network) |
| 118 | { |
| 119 | return ValidateActivationOperator(delegateData, |
| 120 | tfLiteContext, |
| 121 | inputTensorInfo, |
| 122 | outputTensorInfo, |
| 123 | activationDesc); |
| 124 | } |
| 125 | armnn::IConnectableLayer* activationLayer = delegateData.m_Network->AddActivationLayer(activationDesc); |
| 126 | ARMNN_ASSERT(activationLayer != nullptr); |
| 127 | |
| 128 | armnn::IOutputSlot& outputSlot = activationLayer->GetOutputSlot(0); |
| 129 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 130 | |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 131 | // try to connect the Constant Inputs if there are any |
| 132 | if(ProcessInputs(activationLayer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk ) |
| 133 | { |
| 134 | return kTfLiteError; |
| 135 | } |
| 136 | |
David Monahan | 0cf8442 | 2020-11-16 15:53:03 +0000 | [diff] [blame] | 137 | // Connect |
| 138 | return Connect(activationLayer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } // namespace armnnDelegate |