Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | 04d8229 | 2023-01-19 18:29:40 +0000 | [diff] [blame] | 2 | // Copyright © 2020-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 | 81ec994 | 2021-10-12 10:26:30 +0100 | [diff] [blame] | 8 | #include <armnn_delegate.hpp> |
| 9 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 10 | #include <armnn/ArmNN.hpp> |
| 11 | #include <armnn/BackendHelper.hpp> |
| 12 | #include <armnn/utility/Assert.hpp> |
Sadik Armagan | 67e95f2 | 2020-10-29 16:14:54 +0000 | [diff] [blame] | 13 | #include <armnn/utility/NumericCast.hpp> |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 14 | |
Sadik Armagan | 6e36a64 | 2020-11-10 21:18:41 +0000 | [diff] [blame] | 15 | #include <armnnUtils/Permute.hpp> |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 16 | #include <armnnUtils/TensorUtils.hpp> |
Sadik Armagan | 6e36a64 | 2020-11-10 21:18:41 +0000 | [diff] [blame] | 17 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 18 | #include <tensorflow/lite/builtin_ops.h> |
| 19 | #include <tensorflow/lite/c/builtin_op_data.h> |
| 20 | #include <tensorflow/lite/c/common.h> |
| 21 | #include <tensorflow/lite/minimal_logging.h> |
Matthew Sloyan | 1157232 | 2023-03-16 10:17:51 +0000 | [diff] [blame] | 22 | #include <tensorflow/lite/kernels/kernel_util.h> |
Sadik Armagan | 05e9fd2 | 2020-11-17 12:01:47 +0000 | [diff] [blame] | 23 | |
Matthew Sloyan | c49aacc | 2023-04-28 17:27:26 +0100 | [diff] [blame] | 24 | #include <numeric> |
| 25 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 26 | namespace |
| 27 | { |
| 28 | |
Sadik Armagan | 32ca144 | 2020-11-13 17:51:56 +0000 | [diff] [blame] | 29 | uint32_t NonNegative(int32_t value, int nodeIndex) |
| 30 | { |
| 31 | if (value < 0) |
| 32 | { |
Keith Davis | 892fafe | 2020-11-26 17:40:35 +0000 | [diff] [blame] | 33 | throw armnn::Exception( |
Matthew Sloyan | 1157232 | 2023-03-16 10:17:51 +0000 | [diff] [blame] | 34 | "TfLiteArmnnDelegate: Non-negative value in node " + std::to_string(static_cast<int>(nodeIndex))); |
Sadik Armagan | 32ca144 | 2020-11-13 17:51:56 +0000 | [diff] [blame] | 35 | } |
| 36 | else |
| 37 | { |
| 38 | return static_cast<uint32_t>(value); |
| 39 | } |
| 40 | } |
| 41 | |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 42 | void ExpandTensorRankToEqual(armnn::TensorInfo& inputInfo0, |
| 43 | armnn::TensorInfo& inputInfo1) |
Sadik Armagan | 67e95f2 | 2020-10-29 16:14:54 +0000 | [diff] [blame] | 44 | { |
| 45 | unsigned int inputDimensions0 = inputInfo0.GetNumDimensions(); |
| 46 | unsigned int inputDimensions1 = inputInfo1.GetNumDimensions(); |
| 47 | |
| 48 | if (inputDimensions0 == inputDimensions1) |
| 49 | { |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 50 | return; |
Sadik Armagan | 67e95f2 | 2020-10-29 16:14:54 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | unsigned int biggerInputDimensions = std::max(inputDimensions0, inputDimensions1); |
Sadik Armagan | 67e95f2 | 2020-10-29 16:14:54 +0000 | [diff] [blame] | 54 | |
| 55 | bool input0IsSmaller = inputDimensions0 < inputDimensions1; |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 56 | armnn::TensorInfo& smallInfo = input0IsSmaller ? inputInfo0 : inputInfo1; |
| 57 | const armnn::TensorShape& newShape = armnnUtils::ExpandDimsToRank(smallInfo.GetShape(), biggerInputDimensions); |
Sadik Armagan | 67e95f2 | 2020-10-29 16:14:54 +0000 | [diff] [blame] | 58 | |
Ryan OShea | a544f0f | 2023-01-25 18:10:20 +0000 | [diff] [blame] | 59 | smallInfo.SetShape(newShape); |
Narumol Prangnawarat | 7684b18 | 2021-08-12 14:48:15 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Sadik Armagan | 32ca144 | 2020-11-13 17:51:56 +0000 | [diff] [blame] | 62 | void CalcPadding(uint32_t inputSize, |
| 63 | uint32_t filterSize, |
| 64 | uint32_t stride, |
| 65 | uint32_t dilation, |
| 66 | uint32_t& paddingFront, |
| 67 | uint32_t& paddingBack, |
| 68 | TfLitePadding padding) |
| 69 | { |
| 70 | paddingFront = 0; |
| 71 | paddingBack = 0; |
| 72 | if (padding == kTfLitePaddingSame) |
| 73 | { |
| 74 | uint32_t outputSize = (inputSize + stride - 1) / stride; |
| 75 | uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1); |
| 76 | uint32_t temp = (outputSize - 1) * stride + dilatedSize; |
| 77 | if (temp > inputSize) |
| 78 | { |
| 79 | paddingFront = (temp - inputSize) / 2; |
| 80 | paddingBack = (temp - inputSize) - paddingFront; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
Matthew Sloyan | c52190a | 2023-05-08 11:33:55 +0100 | [diff] [blame] | 85 | // Function that calculates explicit padding when the output shape is known. |
| 86 | // At the moment the output is only given as an input parameter in Transpose Convolution, |
| 87 | // not in Convolution and Depthwise Convolution |
| 88 | void CalcPadding(uint32_t inputSize, |
| 89 | uint32_t filterSize, |
| 90 | uint32_t stride, |
| 91 | uint32_t dilation, |
| 92 | uint32_t& paddingFront, |
| 93 | uint32_t& paddingBack, |
| 94 | TfLitePadding padding, |
| 95 | uint32_t outputSize) |
| 96 | { |
| 97 | armnn::IgnoreUnused(dilation); |
| 98 | paddingFront = 0; |
| 99 | paddingBack = 0; |
| 100 | if (padding == kTfLitePaddingSame) |
| 101 | { |
| 102 | uint32_t totalPadding = (inputSize - 1) * stride + filterSize - outputSize; |
| 103 | paddingFront = totalPadding / 2; |
| 104 | paddingBack = totalPadding - paddingFront; |
| 105 | } |
| 106 | } |
| 107 | |
Matthew Sloyan | d30bfb5 | 2021-04-18 16:40:00 +0100 | [diff] [blame] | 108 | unsigned int ComputeWrappedIndex(int index, unsigned int numDimensions) |
| 109 | { |
| 110 | int numDims = armnn::numeric_cast<int>(numDimensions); |
| 111 | int wrappedIndex = index < 0 ? numDims + index : index; |
Ryan OShea | c229b3f | 2023-06-27 22:34:54 +0100 | [diff] [blame] | 112 | |
| 113 | if (wrappedIndex < 0 || wrappedIndex >= numDims) |
| 114 | { |
| 115 | throw armnn::ParseException("Unable to compute wrapped index"); |
| 116 | } |
Matthew Sloyan | d30bfb5 | 2021-04-18 16:40:00 +0100 | [diff] [blame] | 117 | |
| 118 | return static_cast<unsigned int>(wrappedIndex); |
| 119 | }; |
| 120 | |
Jim Flynn | 4b2f347 | 2021-10-13 21:20:07 +0100 | [diff] [blame] | 121 | bool AreAllSigned32(const armnn::TensorInfo& inputInfo1, |
| 122 | const armnn::TensorInfo& inputInfo2, |
| 123 | const armnn::TensorInfo& outputInfo) |
| 124 | { |
| 125 | return (armnn::DataType::Signed32 == inputInfo1.GetDataType()) && |
| 126 | (armnn::DataType::Signed32 == inputInfo2.GetDataType()) && |
| 127 | (armnn::DataType::Signed32 == outputInfo.GetDataType()); |
| 128 | } |
| 129 | |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 130 | void UpdateConstantTensorOutputs(const armnn::TensorInfo& inputInfo, armnn::TensorInfo& outputInfo) |
| 131 | { |
| 132 | // If input tensor info is constant and output tensor info shape is not specified |
| 133 | // set the output shape from input shape |
| 134 | if (inputInfo.IsConstant() && outputInfo.GetShape().GetDimensionality() == armnn::Dimensionality::NotSpecified) |
| 135 | { |
| 136 | outputInfo.SetShape(inputInfo.GetShape()); |
| 137 | } |
Sadik Armagan | 90a119b | 2022-08-05 16:12:49 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Matthew Sloyan | 2b04ec3 | 2023-04-26 11:42:46 +0100 | [diff] [blame] | 140 | void SetupConcatViewOrigin(const armnn::TensorInfo& inputTensorInfo, |
| 141 | armnn::OriginsDescriptor& concatDescriptor, |
| 142 | const unsigned int concatAxis, |
| 143 | unsigned int inputIndex, |
| 144 | unsigned int& mergeDimOrigin) |
| 145 | { |
| 146 | const uint32_t inputRank = concatDescriptor.GetNumDimensions(); |
| 147 | |
| 148 | // double check dimensions of the tensors |
| 149 | if (inputTensorInfo.GetNumDimensions() != inputRank) |
| 150 | { |
| 151 | throw armnn::ParseException("The number of dimensions for input tensors " |
| 152 | "of the concatenation operator should be: " + std::to_string(inputRank)); |
| 153 | } |
| 154 | |
| 155 | for (unsigned int j = 0; j < concatAxis; ++j) |
| 156 | { |
| 157 | concatDescriptor.SetViewOriginCoord(inputIndex, j, 0); |
| 158 | } |
| 159 | |
| 160 | concatDescriptor.SetViewOriginCoord(inputIndex, concatAxis, mergeDimOrigin); |
| 161 | mergeDimOrigin += inputTensorInfo.GetShape()[concatAxis]; |
| 162 | |
| 163 | for (unsigned int j = concatAxis + 1; j < inputRank; ++j) |
| 164 | { |
| 165 | concatDescriptor.SetViewOriginCoord(inputIndex, j, 0); |
| 166 | } |
| 167 | } |
| 168 | |
Matthew Sloyan | c49aacc | 2023-04-28 17:27:26 +0100 | [diff] [blame] | 169 | TfLiteStatus CreateOutputTensorShape(const armnn::TensorInfo& inputTensorInfo, |
| 170 | const std::vector<int32_t>& targetShape, |
| 171 | armnn::ReshapeDescriptor& reshapeDesc) |
| 172 | { |
| 173 | std::vector<unsigned int> outputDims(targetShape.begin(), targetShape.end()); |
| 174 | const auto stretchDim = std::find(targetShape.begin(), targetShape.end(), -1); |
| 175 | |
| 176 | if (stretchDim != targetShape.end()) |
| 177 | { |
| 178 | if (std::find(std::next(stretchDim), targetShape.end(), -1) != targetShape.end()) |
| 179 | { |
| 180 | // Return kTfLiteError and log the error after returning |
| 181 | return kTfLiteError; |
| 182 | } |
| 183 | |
| 184 | auto targetNumElements = |
| 185 | armnn::numeric_cast<unsigned int>( |
| 186 | std::accumulate(targetShape.begin(), targetShape.end(), -1, std::multiplies<int32_t>())); |
| 187 | |
| 188 | auto stretchIndex = static_cast<size_t>(std::distance(targetShape.begin(), stretchDim)); |
Tianle Cheng | 2077348 | 2023-10-03 12:01:11 +0100 | [diff] [blame] | 189 | |
| 190 | if (targetNumElements == 0) |
| 191 | { |
| 192 | // To handle the edge case that input and output both have zero elements |
| 193 | outputDims[stretchIndex] = 0; |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements; |
| 198 | } |
Matthew Sloyan | c49aacc | 2023-04-28 17:27:26 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | armnn::TensorShape outputShape = armnn::TensorShape(static_cast<unsigned int>(outputDims.size()), |
| 202 | outputDims.data()); |
| 203 | reshapeDesc.m_TargetShape = outputShape; |
| 204 | return kTfLiteOk; |
| 205 | } |
| 206 | |
Matthew Sloyan | 3504e42 | 2023-05-03 13:53:02 +0100 | [diff] [blame] | 207 | armnn::TensorInfo OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims, |
| 208 | const armnn::TensorInfo& inputTensorInfo) |
| 209 | { |
| 210 | static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; |
| 211 | |
| 212 | if (inputTensorInfo.GetNumDimensions() > 4) |
| 213 | { |
| 214 | std::stringstream ss; |
| 215 | ss << "Input tensor has unexpected number of dimensions:" |
| 216 | << inputTensorInfo.GetNumDimensions() |
| 217 | << " shape:" << inputTensorInfo.GetShape() |
| 218 | << " " |
| 219 | << CHECK_LOCATION().AsString(); |
| 220 | throw armnn::ParseException(ss.str()); |
| 221 | } |
| 222 | |
| 223 | if (squeezeDims.empty()) |
| 224 | { |
| 225 | squeezeDims.assign(dimensionSequence, dimensionSequence + inputTensorInfo.GetNumDimensions()); |
| 226 | } |
| 227 | |
| 228 | std::vector<uint32_t> outputDims; |
| 229 | for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++) |
| 230 | { |
| 231 | bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end()); |
| 232 | auto currentDimension = inputTensorInfo.GetShape()[i]; |
| 233 | if (skipSqueeze || currentDimension != 1) |
| 234 | { |
| 235 | outputDims.push_back(currentDimension); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (outputDims.size() > 4) |
| 240 | { |
| 241 | std::stringstream ss; |
| 242 | ss << "Output tensor has unexpected number of dimensions:" |
| 243 | << inputTensorInfo.GetNumDimensions() |
| 244 | << " shape:" << inputTensorInfo.GetShape() |
| 245 | << " " |
| 246 | << CHECK_LOCATION().AsString(); |
| 247 | throw armnn::ParseException(ss.str()); |
| 248 | } |
| 249 | |
| 250 | armnn::TensorShape outShape = armnn::TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data()); |
| 251 | |
| 252 | // We need to preserve the tensor type and the quantization data as well |
| 253 | armnn::TensorInfo outTensorInfo = inputTensorInfo; |
| 254 | outTensorInfo.SetShape(outShape); |
| 255 | |
| 256 | return outTensorInfo; |
| 257 | } |
| 258 | |
Sadik Armagan | 62483be | 2020-10-23 17:14:43 +0100 | [diff] [blame] | 259 | } // namespace anonymous |