• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2020-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn_delegate.hpp>
9 
10 #include <armnn/ArmNN.hpp>
11 #include <armnn/BackendHelper.hpp>
12 #include <armnn/utility/Assert.hpp>
13 #include <armnn/utility/NumericCast.hpp>
14 
15 #include <armnnUtils/Permute.hpp>
16 #include <armnnUtils/TensorUtils.hpp>
17 
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>
22 #include <tensorflow/lite/kernels/kernel_util.h>
23 
24 namespace
25 {
26 
NonNegative(int32_t value,int nodeIndex)27 uint32_t NonNegative(int32_t value, int nodeIndex)
28 {
29     if (value < 0)
30     {
31         throw armnn::Exception(
32                 "TfLiteArmnnDelegate: Non-negative value in node " + std::to_string(static_cast<int>(nodeIndex)));
33     }
34     else
35     {
36         return static_cast<uint32_t>(value);
37     }
38 }
39 
ExpandTensorRankToEqual(armnn::TensorInfo & inputInfo0,armnn::TensorInfo & inputInfo1)40 void ExpandTensorRankToEqual(armnn::TensorInfo& inputInfo0,
41                              armnn::TensorInfo& inputInfo1)
42 {
43     unsigned int inputDimensions0 = inputInfo0.GetNumDimensions();
44     unsigned int inputDimensions1 = inputInfo1.GetNumDimensions();
45 
46     if (inputDimensions0 == inputDimensions1)
47     {
48         return;
49     }
50 
51     unsigned int biggerInputDimensions = std::max(inputDimensions0, inputDimensions1);
52 
53     bool input0IsSmaller = inputDimensions0 < inputDimensions1;
54     armnn::TensorInfo& smallInfo = input0IsSmaller ? inputInfo0 : inputInfo1;
55     const armnn::TensorShape& newShape = armnnUtils::ExpandDimsToRank(smallInfo.GetShape(), biggerInputDimensions);
56 
57     smallInfo.SetShape(newShape);
58 }
59 
CalcPadding(uint32_t inputSize,uint32_t filterSize,uint32_t stride,uint32_t dilation,uint32_t & paddingFront,uint32_t & paddingBack,TfLitePadding padding)60 void CalcPadding(uint32_t inputSize,
61                  uint32_t filterSize,
62                  uint32_t stride,
63                  uint32_t dilation,
64                  uint32_t& paddingFront,
65                  uint32_t& paddingBack,
66                  TfLitePadding padding)
67 {
68     paddingFront = 0;
69     paddingBack = 0;
70     if (padding == kTfLitePaddingSame)
71     {
72         uint32_t outputSize = (inputSize + stride - 1) / stride;
73         uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
74         uint32_t temp = (outputSize - 1) * stride + dilatedSize;
75         if (temp > inputSize)
76         {
77             paddingFront = (temp - inputSize) / 2;
78             paddingBack = (temp - inputSize) - paddingFront;
79         }
80     }
81 }
82 
ComputeWrappedIndex(int index,unsigned int numDimensions)83 unsigned int ComputeWrappedIndex(int index, unsigned int numDimensions)
84 {
85     int numDims = armnn::numeric_cast<int>(numDimensions);
86     int wrappedIndex = index < 0 ? numDims + index : index;
87     ARMNN_ASSERT(wrappedIndex >= 0);
88     ARMNN_ASSERT(wrappedIndex < numDims);
89 
90     return static_cast<unsigned int>(wrappedIndex);
91 };
92 
AreAllSigned32(const armnn::TensorInfo & inputInfo1,const armnn::TensorInfo & inputInfo2,const armnn::TensorInfo & outputInfo)93 bool AreAllSigned32(const armnn::TensorInfo& inputInfo1,
94                     const armnn::TensorInfo& inputInfo2,
95                     const armnn::TensorInfo& outputInfo)
96 {
97     return (armnn::DataType::Signed32 == inputInfo1.GetDataType()) &&
98            (armnn::DataType::Signed32 == inputInfo2.GetDataType()) &&
99            (armnn::DataType::Signed32 == outputInfo.GetDataType());
100 }
101 
UpdateConstantTensorOutputs(const armnn::TensorInfo & inputInfo,armnn::TensorInfo & outputInfo)102 void UpdateConstantTensorOutputs(const armnn::TensorInfo& inputInfo, armnn::TensorInfo& outputInfo)
103 {
104     // If input tensor info is constant and output tensor info shape is not specified
105     // set the output shape from input shape
106     if (inputInfo.IsConstant() && outputInfo.GetShape().GetDimensionality() == armnn::Dimensionality::NotSpecified)
107     {
108         outputInfo.SetShape(inputInfo.GetShape());
109     }
110 }
111 
112 } // namespace anonymous
113