blob: 5291aac418dfece0c669f107bb76d6a45ef3620d [file] [log] [blame]
Tracy Narine7306bbe2023-07-17 16:06:26 +01001//
2// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <OpaqueDelegateUtils.hpp>
9
10namespace armnnOpaqueDelegate
11{
12
13TfLiteStatus ValidateReverseV2Operator(DelegateData& delegateData,
14 TfLiteOpaqueContext* tfLiteContext,
15 const armnn::TensorInfo& inputInfo0,
16 const armnn::TensorInfo& inputInfo1,
17 const armnn::TensorInfo& outputInfo)
18{
19 bool isSupported = false;
20 FORWARD_LAYER_OPAQUE_SUPPORT_FUNC("REVERSEV2",
21 tfLiteContext,
22 IsReverseV2Supported,
23 delegateData.m_Backends,
24 isSupported,
25 armnn::BackendId(),
26 inputInfo0,
27 inputInfo1,
28 outputInfo);
29
30 return isSupported ? kTfLiteOk : kTfLiteError;
31}
32
33TfLiteStatus VisitReverseV2Operator(DelegateData& delegateData,
34 TfLiteOpaqueContext* tfLiteContext,
35 TfLiteOpaqueNode* tfLiteNode,
36 int nodeIndex,
37 int32_t reverseV2OperatorCode)
38{
39 TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex));
40 TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));
41
42 // Gather input indices and use to get input tensor.
43 auto numInputs = TfLiteOpaqueNodeNumberOfInputs(tfLiteNode);
44 const int* inputTensors;
45 if (TfLiteOpaqueNodeInputs(tfLiteNode, &inputTensors, &numInputs) != kTfLiteOk)
46 {
47 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
48 tfLiteContext,
49 "TfLiteArmnnOpaqueDelegate: Unable to gather input tensor indices from node #%d: ",
50 nodeIndex);
51 return kTfLiteError;
52 }
53
54 // The first input contains the data to be reversed
55 const TfLiteOpaqueTensor* tfLiteInputTensor =
56 TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[0]);
57 if (IsDynamicTensor(tfLiteInputTensor))
58 {
59 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
60 tfLiteContext,
61 "TfLiteArmnnOpaqueDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
62 reverseV2OperatorCode, nodeIndex);
63 return kTfLiteError;
64 }
65
66 // The second input contains the axis tensor
67 const TfLiteOpaqueTensor* tfLiteAxisTensor =
68 TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[1]);
69 if (IsDynamicTensor(tfLiteAxisTensor))
70 {
71 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
72 tfLiteContext,
73 "TfLiteArmnnOpaqueDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
74 reverseV2OperatorCode, nodeIndex);
75 return kTfLiteError;
76 }
77
78 // Gather output indices and use to get output tensors.
79 int numOutputs = 0;
80 const int* outputTensors;
81 if (TfLiteOpaqueNodeOutputs(tfLiteNode, &outputTensors, &numOutputs) != kTfLiteOk)
82 {
83 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
84 tfLiteContext,
85 "TfLiteArmnnOpaqueDelegate: Unable to gather output tensor indices from node #%d: ",
86 nodeIndex);
87 return kTfLiteError;
88 }
89
90 // Get the output tensor
91 const TfLiteOpaqueTensor* tfLiteOutputTensor =
92 TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, outputTensors[0]);
93 if (IsDynamicTensor(tfLiteOutputTensor))
94 {
95 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
96 tfLiteContext,
97 "TfLiteArmnnOpaqueDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ",
98 reverseV2OperatorCode, nodeIndex);
99 return kTfLiteError;
100 }
101
102 const armnn::TensorInfo& inputTensorInfo0 =
103 GetTensorInfoForTfLiteOpaqueTensor(tfLiteInputTensor);
104 const armnn::TensorInfo& inputTensorInfo1 =
105 GetTensorInfoForTfLiteOpaqueTensor(tfLiteAxisTensor);
106 const armnn::TensorInfo& outputTensorInfo =
107 GetTensorInfoForTfLiteOpaqueTensor(tfLiteOutputTensor, true);
108
109 if (inputTensorInfo0.GetNumDimensions() != outputTensorInfo.GetNumDimensions())
110 {
111 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
112 tfLiteContext,
113 "TfLiteArmnnOpaqueDelegate: input tensor dimension and output tensor dimension differ #%d node #%d: ",
114 reverseV2OperatorCode, nodeIndex);
115 return kTfLiteError;
116 }
117
118 for (unsigned i=0; i < inputTensorInfo0.GetNumDimensions(); i++)
119 {
120 if (inputTensorInfo0.GetShape()[i] != outputTensorInfo.GetShape()[i])
121 {
122 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
123 tfLiteContext,
124 "TfLiteArmnnOpaqueDelegate: input tensor dimension and output tensor differ #%d node #%d: ",
125 reverseV2OperatorCode, nodeIndex);
126 return kTfLiteError;
127 }
128 }
129
Tracy Narine7306bbe2023-07-17 16:06:26 +0100130 // Get axis tensor data
131 auto axisTensorNumValues = static_cast<unsigned int>(TfLiteOpaqueTensorDim(tfLiteAxisTensor,0));
132
133 const auto maxDimension = 4;
134
135 if (axisTensorNumValues > maxDimension)
136 {
137 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
138 tfLiteContext,
139 "TfLiteArmnnOpaqueDelegate: The Axis-Input-Tensor of the ReverseV2 operation requires a "
140 "dimension of <= %d but a tensor with a dimension of %d was given. "
141 "Operator: #%d node #%d: ",
142 maxDimension, axisTensorNumValues, reverseV2OperatorCode, nodeIndex);
143 return kTfLiteError;
144 }
145
146 // No network pointer indicates that only support for this operator should be checked
147 if (!delegateData.m_Network)
148 {
149 return ValidateReverseV2Operator(delegateData,
150 tfLiteContext,
151 inputTensorInfo0,
152 inputTensorInfo1,
153 outputTensorInfo);
154 }
155
Mike Kellya2806502023-08-03 10:42:11 +0100156 auto layerName = GetName(armnn::LayerType::ReverseV2, nodeIndex);
Tracy Narine7306bbe2023-07-17 16:06:26 +0100157 armnn::IConnectableLayer* reverseV2Layer = delegateData.m_Network->AddReverseV2Layer(layerName.c_str());
158
159 armnn::IOutputSlot& outputSlot = reverseV2Layer->GetOutputSlot(0);
160 outputSlot.SetTensorInfo(outputTensorInfo);
161
162 // try to connect the Constant Inputs if there are any
Mike Kellya2806502023-08-03 10:42:11 +0100163 if (ProcessInputs(reverseV2Layer, delegateData, tfLiteContext, tfLiteNode, nodeIndex) != kTfLiteOk)
Tracy Narine7306bbe2023-07-17 16:06:26 +0100164 {
165 return kTfLiteError;
166 }
167
168 ARMNN_ASSERT(reverseV2Layer != nullptr);
169
170 return Connect(reverseV2Layer, tfLiteContext, tfLiteNode, delegateData);
171}
172
173} // namespace armnnOpaqueDelegate