aboutsummaryrefslogtreecommitdiff
path: root/delegate/opaque/src/ScatterNd.hpp
blob: 08bbed7f0ec68cfebb19975e8b01ed05edaa87f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#pragma once

#include <OpaqueDelegateUtils.hpp>

namespace armnnOpaqueDelegate
{
TfLiteStatus ValidateScatterNdOperator(DelegateData& delegateData,
                                       TfLiteOpaqueContext *tfLiteContext,
                                       const armnn::TensorInfo& indicesInfo,
                                       const armnn::TensorInfo& updatesInfo,
                                       const armnn::TensorInfo& shapeInfo,
                                       const armnn::TensorInfo& outputInfo,
                                       const armnn::ScatterNdDescriptor& descriptor)
{
    bool isSupported = false;
    FORWARD_LAYER_OPAQUE_SUPPORT_FUNC("SCATTER_ND",
                                      tfLiteContext,
                                      IsScatterNdSupported,
                                      delegateData.m_Backends,
                                      isSupported,
                                      armnn::BackendId(),
                                      shapeInfo,
                                      indicesInfo,
                                      updatesInfo,
                                      outputInfo,
                                      descriptor);
    return isSupported ? kTfLiteOk : kTfLiteError;
}

TfLiteStatus VisitScatterNdOperator(DelegateData& delegateData,
                                    TfLiteOpaqueContext* tfLiteContext,
                                    TfLiteOpaqueNode* tfLiteNode,
                                    int nodeIndex,
                                    int32_t scatterNdOperatorCode)
{
    TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 3, nodeIndex));
    TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex));

    // Gather input indices and use to get input tensor.
    auto numInputs = TfLiteOpaqueNodeNumberOfInputs(tfLiteNode);
    const int* inputTensors;
    if (TfLiteOpaqueNodeInputs(tfLiteNode, &inputTensors, &numInputs) != kTfLiteOk)
    {
        TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
            tfLiteContext,
            "TfLiteArmnnOpaqueDelegate: Unable to gather input tensor indices from node #%d: ",
            nodeIndex);
        return kTfLiteError;
    }

    // Gather input indices and use to get output tensor.
    int numOutputs = 0;
    const int* outputTensors;
    if (TfLiteOpaqueNodeOutputs(tfLiteNode, &outputTensors, &numOutputs) != kTfLiteOk)
    {
        TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
            tfLiteContext,
            "TfLiteArmnnOpaqueDelegate: Unable to gather output tensor indices from node #%d: ",
            nodeIndex);
        return kTfLiteError;
    }

    // The indices tensor are the positions the data is updated/scattered into
    const TfLiteOpaqueTensor* tfLiteIndicesTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[0]);
    if (IsDynamicTensor(tfLiteIndicesTensor))
    {
        TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
            tfLiteContext,
            "TfLiteArmnnOpaqueDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
            scatterNdOperatorCode, nodeIndex);
        return kTfLiteError;
    }

    // The updates tensor provides the data which will be updated/scattered into the relevant indices
    const TfLiteOpaqueTensor* tfLiteUpdatesTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[1]);
    if (IsDynamicTensor(tfLiteUpdatesTensor))
    {
        TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
            tfLiteContext,
            "TfLiteArmnnOpaqueDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
            scatterNdOperatorCode, nodeIndex);
        return kTfLiteError;
    }

    // For TFLite ScatterNd there is no input tensor
    // The shape tensor is a 1D tensor which represents the shape of an input tensor to be filled with zeros
    const TfLiteOpaqueTensor* tfLiteShapeTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, inputTensors[2]);
    if (IsDynamicTensor(tfLiteShapeTensor))
    {
        TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
                tfLiteContext,
                "TfLiteArmnnOpaqueDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ",
                scatterNdOperatorCode, nodeIndex);
        return kTfLiteError;
    }

    // The output tensor
    const TfLiteOpaqueTensor* tfLiteOutputTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, outputTensors[0]);
    if (IsDynamicTensor(tfLiteOutputTensor))
    {
        TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
            tfLiteContext,
            "TfLiteArmnnOpaqueDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ",
            scatterNdOperatorCode, nodeIndex);
        return kTfLiteError;
    }

    const armnn::TensorInfo& shapeTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteShapeTensor);
    const armnn::TensorInfo& indicesTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteIndicesTensor);
    const armnn::TensorInfo& updatesTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteUpdatesTensor);
    const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteOutputTensor, true);

    armnn::ScatterNdDescriptor scatterNdDescriptor;
    scatterNdDescriptor.m_Function     = armnn::ScatterNdFunction::Update;
    scatterNdDescriptor.m_InputEnabled = false;
    scatterNdDescriptor.m_Axis         = 0;
    scatterNdDescriptor.m_AxisEnabled  = false;

    // Check output dimensions
    if (shapeTensorInfo.GetShape().GetNumElements() != outputTensorInfo.GetNumDimensions())
    {
        TF_LITE_OPAQUE_MAYBE_KERNEL_LOG(
                tfLiteContext,
                "TfLiteArmnnOpaqueDelegate: Input tensor dimension and output tensor dimension differ",
                "Operator: #%d node #%d: ",
                scatterNdOperatorCode, nodeIndex);
        return kTfLiteError;
    }

    // No network pointer indicates that only support for this operator should be checked
    if (!delegateData.m_Network)
    {
        return ValidateScatterNdOperator(delegateData,
                                         tfLiteContext,
                                         indicesTensorInfo,
                                         updatesTensorInfo,
                                         shapeTensorInfo,
                                         outputTensorInfo,
                                         scatterNdDescriptor);
    }

    auto layerName = GetName(armnn::LayerType::ScatterNd, nodeIndex);
    armnn::IConnectableLayer* layer = delegateData.m_Network->AddScatterNdLayer(scatterNdDescriptor, layerName.c_str());

    if (layer == nullptr)
    {
        return kTfLiteError;
    }

    layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);

    if (ProcessInputs(layer, delegateData, tfLiteContext, tfLiteNode, nodeIndex) != kTfLiteOk)
    {
        return kTfLiteError;
    }

    delegateData.m_OutputSlotForNode[inputTensors[2]]->Connect(layer->GetInputSlot(0));
    delegateData.m_OutputSlotForNode[inputTensors[0]]->Connect(layer->GetInputSlot(1));
    delegateData.m_OutputSlotForNode[inputTensors[1]]->Connect(layer->GetInputSlot(2));

    // Prepare output slots
    armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
    delegateData.m_OutputSlotForNode[static_cast<unsigned long>(outputTensors[0])] = &outputSlot;

    return kTfLiteOk;
}

} // namespace armnnOpaqueDelegate