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 | |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 8 | #include <armnn/utility/IgnoreUnused.hpp> |
| 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> |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 14 | #include <fmt/format.h> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 15 | |
| 16 | namespace armnnDelegate |
| 17 | { |
| 18 | |
| 19 | TfLiteStatus VisitSliceOperator(DelegateData& delegateData, |
| 20 | TfLiteContext* tfLiteContext, |
| 21 | TfLiteNode* tfLiteNode, |
| 22 | int nodeIndex, |
| 23 | int32_t sliceOperatorCode) |
| 24 | { |
Cathal Corbett | 839b932 | 2022-11-18 08:52:18 +0000 | [diff] [blame] | 25 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 3, nodeIndex)); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 26 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
Finn Williams | 6f9f990 | 2020-11-13 13:23:15 +0000 | [diff] [blame] | 27 | |
Cathal Corbett | 839b932 | 2022-11-18 08:52:18 +0000 | [diff] [blame] | 28 | // Read inputs [input, begin, size] |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 29 | int numInputs = tfLiteNode->inputs->size; |
| 30 | std::vector<const TfLiteTensor*> tfLiteInputs; |
| 31 | tfLiteInputs.reserve(numInputs); |
| 32 | const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; |
| 33 | for (int i = 0; i < numInputs; i++) |
| 34 | { |
| 35 | const TfLiteTensor* inputTensor = &tfLiteTensors[tfLiteNode->inputs->data[i]]; |
| 36 | tfLiteInputs.push_back(inputTensor); |
| 37 | if (!IsValid(tfLiteContext, *inputTensor, sliceOperatorCode, nodeIndex)) |
| 38 | { |
| 39 | return kTfLiteError; |
| 40 | } |
| 41 | } |
| 42 | |
Cathal Corbett | 839b932 | 2022-11-18 08:52:18 +0000 | [diff] [blame] | 43 | // We save the begin and size tensors in our descriptor. Therefore we have to read those values from inputs |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 44 | int inputRank = tfLiteInputs[0]->dims->size; |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 45 | auto ReadInt32Input = [&](int inputIndex, std::vector<int32_t>& outputData, const char* name) -> TfLiteStatus |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 46 | { |
| 47 | if (tfLiteInputs[inputIndex]->type != kTfLiteInt32) |
| 48 | { |
| 49 | TF_LITE_MAYBE_KERNEL_LOG( |
| 50 | tfLiteContext, |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 51 | "TfLiteArmnnDelegate: The %s Tensor of the Slice operation needs to " |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 52 | "be of type int32. Operator: #%d node #%d: ", |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 53 | name, sliceOperatorCode, nodeIndex); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 54 | return kTfLiteError; |
| 55 | } |
| 56 | int rank = tfLiteInputs[inputIndex]->dims->size; |
| 57 | if (rank != 1) |
| 58 | { |
| 59 | TF_LITE_MAYBE_KERNEL_LOG( |
| 60 | tfLiteContext, |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 61 | "TfLiteArmnnDelegate: The %s Tensor of the Slice operation needs to " |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 62 | "be a 1D-Tensor. Operator: #%d node #%d: ", |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 63 | name, sliceOperatorCode, nodeIndex); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 64 | return kTfLiteError; |
| 65 | } |
| 66 | int numValues = tfLiteInputs[inputIndex]->dims->data[0]; |
| 67 | if (numValues != inputRank) |
| 68 | { |
| 69 | TF_LITE_MAYBE_KERNEL_LOG( |
| 70 | tfLiteContext, |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 71 | "TfLiteArmnnDelegate: The number of values in the %s Tensor of the " |
| 72 | "Slice operation needs to be equal to the rank of the Input Tensor. Operator: #%d node #%d: ", |
| 73 | name, sliceOperatorCode, nodeIndex); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 74 | return kTfLiteError; |
| 75 | } |
| 76 | // return tensor data |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 77 | auto* tensorDataPtr = tflite::GetTensorData<int32_t>(tfLiteInputs[inputIndex]); |
| 78 | outputData.assign(tensorDataPtr, tensorDataPtr + numValues); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 79 | return kTfLiteOk; |
| 80 | }; |
| 81 | |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 82 | std::vector<int32_t> signedBegin; |
| 83 | if (ReadInt32Input(1, signedBegin, "Begin") != kTfLiteOk) |
| 84 | { |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 85 | return kTfLiteError; |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | std::vector<int32_t> signedSize; |
| 89 | if (ReadInt32Input(2, signedSize, "Size") != kTfLiteOk) |
| 90 | { |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 91 | return kTfLiteError; |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 92 | } |
| 93 | std::vector<uint32_t> begin({ signedBegin.begin(), signedBegin.end() }); |
| 94 | std::vector<uint32_t> size(signedSize.size()); |
| 95 | |
| 96 | for (unsigned int i = 0; i < signedSize.size(); ++i) |
| 97 | { |
| 98 | int signedValue = signedSize[i]; |
| 99 | if (signedValue < -1 || signedValue > tfLiteInputs[0]->dims->data[i] - signedBegin[i]) |
| 100 | { |
| 101 | TF_LITE_MAYBE_KERNEL_LOG( |
| 102 | tfLiteContext, |
| 103 | "TfLiteArmnnDelegate: Invalid value for Size. Size must be in range [-1, inputDimSize - begin] " |
| 104 | "[-1, %d] inclusive but was %d Operator: #%d node #%d: ", |
| 105 | tfLiteInputs[0]->dims->data[i] - signedBegin[i], signedValue, sliceOperatorCode, |
| 106 | nodeIndex); |
| 107 | return kTfLiteError; |
| 108 | } |
| 109 | if (signedValue == -1) |
| 110 | { |
| 111 | size[i] = tfLiteInputs[0]->dims->data[i] - signedBegin[i]; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | size[i] = static_cast<uint32_t>(signedValue); |
| 116 | } |
| 117 | } |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 118 | |
| 119 | // Write all data to the descriptor |
Cathal Corbett | 839b932 | 2022-11-18 08:52:18 +0000 | [diff] [blame] | 120 | armnn::SliceDescriptor descriptor(begin, size); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 121 | |
| 122 | // Validate output |
| 123 | const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; |
| 124 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, sliceOperatorCode, nodeIndex)) |
| 125 | { |
| 126 | return kTfLiteError; |
| 127 | } |
| 128 | |
| 129 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(*tfLiteInputs[0]); |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 130 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor, true); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 131 | |
| 132 | bool isSupported = false; |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 133 | armnn::BackendId setBackend; |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 134 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 135 | { |
Sadik Armagan | bfa767c | 2022-02-09 14:58:03 +0000 | [diff] [blame] | 136 | FORWARD_LAYER_SUPPORT_FUNC("SLICE", |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 137 | tfLiteContext, |
Cathal Corbett | 839b932 | 2022-11-18 08:52:18 +0000 | [diff] [blame] | 138 | IsSliceSupported, |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 139 | delegateData.m_Backends, |
| 140 | isSupported, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 141 | setBackend, |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 142 | inputTensorInfo, |
| 143 | outInfo, |
| 144 | descriptor); |
| 145 | }; |
| 146 | |
| 147 | if (!delegateData.m_Network) |
| 148 | { |
| 149 | validateFunc(outputTensorInfo, isSupported); |
| 150 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 151 | } |
| 152 | |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 153 | // Add a Slice layer |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 154 | auto layerName = GetLayerName(armnn::LayerType::Slice, nodeIndex); |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 155 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddSliceLayer(descriptor, layerName.c_str()); |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 156 | layer->SetBackendId(setBackend); |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 157 | ARMNN_ASSERT(layer != nullptr); |
| 158 | |
| 159 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 160 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 161 | |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 162 | // try to connect the Constant Inputs if there are any |
Mike Kelly | 07169c8 | 2023-08-02 13:23:09 +0100 | [diff] [blame] | 163 | if (ProcessInputs(layer, delegateData, tfLiteContext, tfLiteNode, nodeIndex) != kTfLiteOk) |
Ryan OShea | 4c231de | 2023-01-17 15:19:20 +0000 | [diff] [blame] | 164 | { |
| 165 | return kTfLiteError; |
| 166 | } |
| 167 | |
Jan Eilers | 2ffddda | 2021-02-03 09:14:30 +0000 | [diff] [blame] | 168 | // Connect |
| 169 | return Connect(layer, tfLiteNode, delegateData); |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | } // namespace armnnDelegate |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 173 | |