aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/ScatterNd.cpp
blob: 8eb53b00a8ab9de54ea9892fed4b1348fb3a4e1e (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//
// Copyright © 2024 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ScatterNd.hpp"
#include "Encoders.hpp"
#include <armnn/backends/WorkloadData.hpp>
#include <armnn/Logging.hpp>

#include <fmt/format.h>

#include <numeric>

namespace armnn
{

float ScatterOperation(ScatterNdFunction operation,
                       float input,
                       float update)
{
    switch (operation)
    {
        case ScatterNdFunction::Update:
            return update;
        case ScatterNdFunction::Add:
            return input + update;
        case ScatterNdFunction::Sub:
            return input - update;
        case ScatterNdFunction::Max:
            return std::max(input, update);
        case ScatterNdFunction::Min:
            return std::min(input, update);
        case ScatterNdFunction::Mul:
            return input * update;
        default:
            throw InvalidArgumentException("ScatterNd: cannot execute this operation.");
    }
}

void ScatterNd(const TensorInfo& inputInfo,
               const TensorInfo& indicesInfo,
               const TensorInfo& updatesInfo,
               Decoder<float>& input,
               Decoder<int>& indices,
               Decoder<float>& updates,
               Encoder<float>& output,
               ScatterNdDescriptor descriptor)
{
    // Axis Unsupported
    if (descriptor.m_AxisEnabled)
    {
        throw InvalidArgumentException("ScatterNd: axis param not supported.");
    }

    // Get the shape for indices, updates, and input
    TensorShape indicesShape = indicesInfo.GetShape();
    TensorShape updatesShape = updatesInfo.GetShape();
    TensorShape inputShape = inputInfo.GetShape();

    // Get the dimensions for indices and updates
    unsigned int dimension = inputInfo.GetNumDimensions();
    unsigned int indicesDim = indicesInfo.GetNumDimensions();
    unsigned int updatesDim = updatesInfo.GetNumDimensions();

    // Calculate the outter and inner dimensions
    unsigned int outterDim = indicesShape[indicesDim - 1];
    unsigned int innerDim = dimension - outterDim;

    // Calculate the number of elements in each dimension
    unsigned int numElementsCount = 1;
    std::vector<unsigned int> elementInDim(dimension);
    for (unsigned int dimIndex = dimension; dimIndex > 0; --dimIndex)
    {
        elementInDim[dimIndex - 1] = numElementsCount;
        numElementsCount *= inputShape[dimIndex - 1];
    }

    // Number of updates per index
    unsigned int numUpdatesPerIndex = elementInDim[dimension - innerDim - 1];

    // Number of indices to update
    unsigned int numIndices = indicesShape[0];

    // Check Input Requirements
    // Requirement 1: Indices and Updates must have rank at least 1
    if (indicesDim < 1 || updatesDim < 1)
    {
        throw InvalidArgumentException("ScatterNd: indices and updates must have rank >= 1.");
    }

    // Requirement 2: Input, Indices and Updates must have values
    if (inputInfo.GetNumElements() == 0 ||
        indicesInfo.GetNumElements() == 0 ||
        updatesInfo.GetNumElements() == 0)
    {
        throw InvalidArgumentException("ScatterNd: input, indices and updates tensor must have values.");
    }

    // Requirement 3: Indices and Updates must match in shape
    // The updates dimension should equals to 1 + inner dimension
    if (updatesDim != 1 + innerDim)
    {
        throw InvalidArgumentException("ScatterNd: updates dimension should equal to 1 + inner dimension.");
    }
    // The inner dimension of updates has to match with shape of input
    for (unsigned int dimBackIndex = 0; dimBackIndex < innerDim; ++dimBackIndex)
    {
        if (updatesShape[updatesDim - dimBackIndex - 1] != inputShape[dimension - dimBackIndex - 1])
        {
            throw InvalidArgumentException(
                fmt::format("ScatterNd: input and updates shape not match on dimension {}",
                            dimension - dimBackIndex));
        }
    }

    // Requirement 4: Check duplicate indices and out of bound indices
    std::set<int> indicesSet;
    std::vector<int> flattenIndices(numIndices);
    for (unsigned int indicesIdx = 0; indicesIdx < numIndices; ++indicesIdx)
    {
        // Get the index
        int flattenIndex = 0;

        for (unsigned int outterIdx = 0; outterIdx < outterDim; ++outterIdx) {

            int outterIndexValue = indices.Get();

            // Check bounds
            if (outterIndexValue < 0 || outterIndexValue >= int(inputShape[outterIdx]))
            {
                throw InvalidArgumentException(
                    fmt::format("ScatterNd: indices {} out of bound [0, {})",
                                outterIndexValue, inputShape[outterIdx]));
            }

            flattenIndex += int(elementInDim[outterIdx]) * outterIndexValue;
            ++indices;
        }

        // Check duplicates when executing ScatterNd::Update
        if (descriptor.m_Function == ScatterNdFunction::Update &&
            indicesSet.find(flattenIndex) != indicesSet.end())
        {
            throw InvalidArgumentException(
                    fmt::format("ScatterNd: duplicate indices occurs {}", flattenIndex));
        }

        flattenIndices[indicesIdx] = flattenIndex;
        indicesSet.insert(flattenIndex);
    }

    // Set the input data to output
    for (unsigned int idx = 0; idx < inputInfo.GetNumElements(); ++idx)
    {
        float inputValue = input.Get();
        ++input;
        output.Set(inputValue);
        ++output;
    }

    // Iterate through all indices to scatter updates
    for (unsigned int indicesIdx = 0; indicesIdx < numIndices; ++indicesIdx)
    {
        // Get the index and calculate the flatten index
        int flattenIndex = flattenIndices[indicesIdx];

        // FlattenIndex is the place that we are going to update the elements
        unsigned int updatesStartIdx = indicesIdx * numUpdatesPerIndex;
        for (unsigned int updatesIdx = 0; updatesIdx < numUpdatesPerIndex; ++updatesIdx)
        {
            updates[updatesStartIdx + updatesIdx];
            input[static_cast<unsigned int>(flattenIndex) + updatesIdx];
            float updateValue = ScatterOperation(descriptor.m_Function, input.Get(), updates.Get());
            output[static_cast<unsigned int>(flattenIndex) + updatesIdx];
            output.Set(updateValue);
        }
    }
}

void ScatterNd(const TensorInfo& indicesInfo,
               const TensorInfo& updatesInfo,
               const TensorInfo& shapeInfo,
               Decoder<int>& indices,
               Decoder<float>& updates,
               Decoder<int>& shape,
               Encoder<float>& output,
               ScatterNdDescriptor descriptor)
{
    // Axis Unsupported
    if (descriptor.m_AxisEnabled)
    {
        throw InvalidArgumentException("ScatterNd: axis param not supported.");
    }

    // Get the shape for indices, updates, and input
    TensorShape indicesShape = indicesInfo.GetShape();
    TensorShape updatesShape = updatesInfo.GetShape();

    // Get the shape values
    std::vector<float> shapeValues = shape.DecodeTensor(shapeInfo.GetShape());
    // Check the shape
    if (shapeInfo.GetNumElements() == 0)
    {
        throw InvalidArgumentException("ScatterNd: shape must have values.");
    }
    for (auto shapeValue : shapeValues)
    {
        if (shapeValue <= 0)
        {
            throw InvalidArgumentException("ScatterNd: shape values must >= 0.");
        }
    }
    // Get the input shape
    std::vector<unsigned int> inputShape (shapeValues.begin(), shapeValues.end());
    unsigned int inputElementsNum = static_cast<unsigned int>(
                std::accumulate(inputShape.begin(), inputShape.end(), 1, std::multiplies<unsigned int>()));

    // Get the dimensions for indices and updates
    unsigned int dimension = shapeInfo.GetNumElements();
    unsigned int indicesDim = indicesInfo.GetNumDimensions();
    unsigned int updatesDim = updatesInfo.GetNumDimensions();

    // Calculate the outter and inner dimensions
    unsigned int outterDim = indicesShape[indicesDim - 1];
    unsigned int innerDim = dimension - outterDim;

    // Calculate the number of elements in each dimension
    unsigned int numElementsCount = 1;
    std::vector<unsigned int> elementInDim(dimension);
    for (unsigned int dimIndex = dimension; dimIndex > 0; --dimIndex)
    {
        elementInDim[dimIndex - 1] = numElementsCount;
        numElementsCount *= inputShape[dimIndex - 1];
    }

    // Number of updates per index
    unsigned int numUpdatesPerIndex = elementInDim[dimension - innerDim - 1];

    // Number of indices to update
    unsigned int numIndices = indicesShape[0];

    // Check Input Requirements
    // Requirement 1: Indices and Updates must have rank at least 1
    if (indicesDim < 1 || updatesDim < 1)
    {
        throw InvalidArgumentException("ScatterNd: indices and updates must have rank >= 1.");
    }

    // Requirement 2: shape, Indices and Updates must have values
    if (indicesInfo.GetNumElements() == 0 ||
        updatesInfo.GetNumElements() == 0)
    {
        throw InvalidArgumentException("ScatterNd: indices and updates tensor must have values.");
    }

    // Requirement 3: Indices and Updates must match in shape
    // The updates dimension should equals to 1 + inner dimension
    if (updatesDim != 1 + innerDim)
    {
        throw InvalidArgumentException("ScatterNd: updates dimension should equal to 1 + inner dimension.");
    }
    // The inner dimension of updates has to match with shape of input
    for (unsigned int dimBackIndex = 0; dimBackIndex < innerDim; ++dimBackIndex)
    {
        if (updatesShape[updatesDim - dimBackIndex - 1] != inputShape[dimension - dimBackIndex - 1])
        {
            throw InvalidArgumentException(
                    fmt::format("ScatterNd: input and updates shape not match on dimension {}",
                                dimension - dimBackIndex));
        }
    }

    // Requirement 4: Check duplicate indices and out of bound indices
    std::set<int> indicesSet;
    std::vector<int> flattenIndices(numIndices);
    for (unsigned int indicesIdx = 0; indicesIdx < numIndices; ++indicesIdx)
    {
        // Get the index
        int flattenIndex = 0;

        for (unsigned int outterIdx = 0; outterIdx < outterDim; ++outterIdx) {

            int outterIndexValue = indices.Get();

            // Check bounds
            if (outterIndexValue < 0 || outterIndexValue >= int(inputShape[outterIdx]))
            {
                throw InvalidArgumentException(
                        fmt::format("ScatterNd: indices {} out of bound [0, {})",
                                    outterIndexValue, inputShape[outterIdx]));
            }

            flattenIndex += int(elementInDim[outterIdx]) * outterIndexValue;
            ++indices;
        }

        // Check duplicates when executing ScatterNd::Update
        if (descriptor.m_Function == ScatterNdFunction::Update &&
            indicesSet.find(flattenIndex) != indicesSet.end())
        {
            throw InvalidArgumentException(
                    fmt::format("ScatterNd: duplicate indices {} occurs when executing ScatterNd::Update.",
                                flattenIndex));
        }

        flattenIndices[indicesIdx] = flattenIndex;
        indicesSet.insert(flattenIndex);
    }

    // Set zeros to output
    for (unsigned int idx = 0; idx < inputElementsNum; ++idx)
    {
        output.Set(0.0f);
        ++output;
    }

    // Iterate through all indices to scatter updates
    for (unsigned int indicesIdx = 0; indicesIdx < numIndices; ++indicesIdx)
    {
        // Get the index and calculate the flatten index
        int flattenIndex = flattenIndices[indicesIdx];

        // FlattenIndex is the place that we are going to update the elements
        unsigned int updatesStartIdx = indicesIdx * numUpdatesPerIndex;
        for (unsigned int updatesIdx = 0; updatesIdx < numUpdatesPerIndex; ++updatesIdx)
        {
            updates[updatesStartIdx + updatesIdx];
            float updateValue = ScatterOperation(descriptor.m_Function, 0.0f, updates.Get());
            output[static_cast<unsigned int>(flattenIndex) + updatesIdx];
            output.Set(updateValue);
        }
    }
}

} // namespace armnn