Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2023 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <OpaqueDelegateUtils.hpp> |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 9 | #include <fmt/format.h> |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 10 | |
| 11 | namespace armnnOpaqueDelegate |
| 12 | { |
| 13 | |
| 14 | TfLiteStatus VisitSliceOperator(DelegateData& delegateData, |
| 15 | TfLiteOpaqueContext* tfLiteContext, |
| 16 | TfLiteOpaqueNode* tfLiteNode, |
| 17 | int nodeIndex, |
| 18 | int32_t tfLiteSliceOperatorCode) |
| 19 | { |
| 20 | |
| 21 | TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 3, nodeIndex)); |
| 22 | TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); |
| 23 | |
| 24 | // Read inputs [input, begin, size] |
| 25 | // Gather input indices and use to get input tensor. |
| 26 | const int* inputTensors; |
| 27 | int numInputs; |
| 28 | if (TfLiteOpaqueNodeInputs(tfLiteNode, &inputTensors, &numInputs) != kTfLiteOk) |
| 29 | { |
| 30 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 31 | tfLiteContext, |
| 32 | "TfLiteArmnnOpaqueDelegate: Unable to gather input tensor indices from node #%d: ", |
| 33 | nodeIndex); |
| 34 | return kTfLiteError; |
| 35 | } |
| 36 | |
| 37 | std::vector<const TfLiteOpaqueTensor*> tfLiteInputTensors; |
| 38 | tfLiteInputTensors.reserve(numInputs); |
| 39 | for (int i = 0; i < numInputs; i++) |
| 40 | { |
| 41 | const TfLiteOpaqueTensor* inputTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[i]); |
| 42 | tfLiteInputTensors.push_back(inputTensor); |
| 43 | if (!IsValid(tfLiteContext, inputTensor, tfLiteSliceOperatorCode, nodeIndex)) |
| 44 | { |
| 45 | return kTfLiteError; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteInputTensors[0]); |
| 50 | |
| 51 | // We save the begin and size tensors in our descriptor. Therefore we have to read those values from inputs |
| 52 | unsigned int inputRank = inputTensorInfo.GetNumDimensions(); |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 53 | auto ReadInt32Input = [&](int inputIndex, std::vector<int32_t>& outputData, const char* name) -> TfLiteStatus |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 54 | { |
| 55 | if (TfLiteOpaqueTensorType(tfLiteInputTensors[inputIndex]) != kTfLiteInt32) |
| 56 | { |
| 57 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 58 | tfLiteContext, |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 59 | "TfLiteArmnnOpaqueDelegate: The %s Tensor of the Slice operation needs to " |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 60 | "be of type int32. Operator: #%d node #%d: ", |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 61 | name, tfLiteSliceOperatorCode, nodeIndex); |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 62 | return kTfLiteError; |
| 63 | } |
| 64 | uint32_t rank = TfLiteOpaqueTensorNumDims(tfLiteInputTensors[inputIndex]); |
| 65 | if (rank != 1) |
| 66 | { |
| 67 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 68 | tfLiteContext, |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 69 | "TfLiteArmnnOpaqueDelegate: The %s Tensor of the Slice operation needs to " |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 70 | "be a 1D-Tensor. Operator: #%d node #%d: ", |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 71 | name, tfLiteSliceOperatorCode, nodeIndex); |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 72 | return kTfLiteError; |
| 73 | } |
| 74 | uint32_t numValues = TfLiteOpaqueTensorDim(tfLiteInputTensors[inputIndex], 0); |
| 75 | if (numValues != inputRank) |
| 76 | { |
| 77 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 78 | tfLiteContext, |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 79 | "TfLiteArmnnOpaqueDelegate: The number of values in the %s Tensor of the " |
| 80 | "Slice operation needs to be equal to the rank of the Input Tensor. Operator: #%d node #%d: ", |
| 81 | name, tfLiteSliceOperatorCode, nodeIndex); |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 82 | return kTfLiteError; |
| 83 | } |
| 84 | // return tensor data |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 85 | auto* tensorDataPtr = static_cast<int32_t*>(TfLiteOpaqueTensorData(tfLiteInputTensors[inputIndex])); |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 86 | outputData.assign(tensorDataPtr, tensorDataPtr + numValues); |
| 87 | return kTfLiteOk; |
| 88 | }; |
| 89 | |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 90 | std::vector<int32_t> signedBegin; |
| 91 | if (ReadInt32Input(1, signedBegin, "Begin") != kTfLiteOk) |
| 92 | { |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 93 | return kTfLiteError; |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | std::vector<int32_t> signedSize; |
| 97 | if (ReadInt32Input(2, signedSize, "Size") != kTfLiteOk) |
| 98 | { |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 99 | return kTfLiteError; |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | std::vector<uint32_t> begin({ signedBegin.begin(), signedBegin.end() }); |
| 103 | std::vector<uint32_t> size(signedSize.size()); |
| 104 | |
| 105 | for (unsigned int i = 0; i < signedSize.size(); ++i) |
| 106 | { |
| 107 | int signedValue = signedSize[i]; |
| 108 | if (signedValue < -1 || signedValue > TfLiteOpaqueTensorDim(tfLiteInputTensors[0], i) - signedBegin[i]) |
| 109 | { |
| 110 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 111 | tfLiteContext, |
| 112 | "TfLiteArmnnDelegate: Invalid value for Size. Size must be in range [-1, inputDimSize - begin] " |
| 113 | "[-1, %d] inclusive but was %d Operator: #%d node #%d: ", |
| 114 | TfLiteOpaqueTensorDim(tfLiteInputTensors[0], i) - signedBegin[i], signedValue, |
| 115 | tfLiteSliceOperatorCode, nodeIndex); |
| 116 | return kTfLiteError; |
| 117 | } |
| 118 | if (signedValue == -1) |
| 119 | { |
| 120 | size[i] = TfLiteOpaqueTensorDim(tfLiteInputTensors[0], i) - signedBegin[i]; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | size[i] = static_cast<uint32_t>(signedValue); |
| 125 | } |
| 126 | } |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 127 | |
| 128 | // Write all data to the descriptor |
| 129 | armnn::SliceDescriptor descriptor(begin, size); |
| 130 | |
| 131 | // Validate output |
| 132 | // Gather output indices and use to get output tensor. |
| 133 | const int* outputTensors; |
| 134 | int numOutputs; |
| 135 | if (TfLiteOpaqueNodeOutputs(tfLiteNode, &outputTensors, &numOutputs) != kTfLiteOk) |
| 136 | { |
| 137 | TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( |
| 138 | tfLiteContext, |
| 139 | "TfLiteArmnnOpaqueDelegate: Unable to gather output tensor indices from node #%d: ", |
| 140 | nodeIndex); |
| 141 | return kTfLiteError; |
| 142 | } |
| 143 | |
| 144 | const TfLiteOpaqueTensor* tfLiteOutputTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, outputTensors[0]); |
| 145 | if (!IsValid(tfLiteContext, tfLiteOutputTensor, tfLiteSliceOperatorCode, nodeIndex)) |
| 146 | { |
| 147 | return kTfLiteError; |
| 148 | } |
| 149 | |
| 150 | const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteOutputTensor, true); |
| 151 | |
| 152 | bool isSupported = false; |
| 153 | armnn::BackendId setBackend; |
| 154 | auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) |
| 155 | { |
| 156 | FORWARD_LAYER_OPAQUE_SUPPORT_FUNC("SLICE", |
| 157 | tfLiteContext, |
| 158 | IsSliceSupported, |
| 159 | delegateData.m_Backends, |
| 160 | isSupported, |
| 161 | setBackend, |
| 162 | inputTensorInfo, |
| 163 | outInfo, |
| 164 | descriptor); |
| 165 | }; |
| 166 | |
| 167 | if (!delegateData.m_Network) |
| 168 | { |
| 169 | validateFunc(outputTensorInfo, isSupported); |
| 170 | return isSupported ? kTfLiteOk : kTfLiteError; |
| 171 | } |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 172 | auto layerName = fmt::format("Slice:{}", nodeIndex); |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 173 | |
| 174 | // Add a Slice layer |
Mike Kelly | 04f7120 | 2023-05-05 15:35:18 +0100 | [diff] [blame] | 175 | armnn::IConnectableLayer* layer = delegateData.m_Network->AddSliceLayer(descriptor, layerName.c_str()); |
Teresa Charlin | 86b0357 | 2023-04-28 13:19:12 +0100 | [diff] [blame] | 176 | layer->SetBackendId(setBackend); |
| 177 | ARMNN_ASSERT(layer != nullptr); |
| 178 | |
| 179 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
| 180 | outputSlot.SetTensorInfo(outputTensorInfo); |
| 181 | |
| 182 | // try to connect the Constant Inputs if there are any |
| 183 | if(ProcessInputs(layer,delegateData, tfLiteContext, tfLiteNode) != kTfLiteOk ) |
| 184 | { |
| 185 | return kTfLiteError; |
| 186 | } |
| 187 | |
| 188 | // Connect |
| 189 | return Connect(layer, tfLiteContext, tfLiteNode, delegateData); |
| 190 | } |
| 191 | |
| 192 | } // namespace armnnOpaqueDelegate |
| 193 | |