aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/workloads/LstmUtils.cpp
blob: 9d9b1a0a411b476fdd3a765d24e630413c3aeb00 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

//#pragma once

#include "LstmUtils.hpp"
#include "BaseIterator.hpp"
#include <armnn/backends/TensorHandle.hpp>


// Helper functions ported from the Android code base
// Refer to: android/external/tensorflow/tensorflow/contrib/lite/kernels/internal/reference/portable_tensor_utils.cc

void VectorBatchVectorAdd(armnn::Decoder<float>& vector,
                          uint32_t vSize,
                          armnn::Decoder<float>& batchVector,
                          uint32_t nBatch,
                          armnn::Encoder<float>& outResult )
{
    for (uint32_t b = 0; b < nBatch; b++)
    {
        for (uint32_t v = 0; v < vSize; v++)
        {
            outResult.Set(batchVector.Get() + vector.Get());
            ++outResult;
            ++vector;
            ++batchVector;
        }
        vector -= vSize;
    }
    batchVector -= vSize * nBatch;
    outResult -= vSize * nBatch;
}


// Layer norm for each batch.
// normalization_epsilon is added to avoid divergence.
void MeanStddevNormalization(armnn::Decoder<float>& input_vector,
                             armnn::Encoder<float>& output_vector,
                             uint32_t v_size,
                             uint32_t n_batch,
                             float normalization_epsilon)
{
    for (uint32_t batch = 0; batch < n_batch; ++batch) {
        float sum = 0.0f;
        float sum_sq = 0.0f;
        for (uint32_t i = 0; i < v_size; ++i) {
            sum += input_vector.Get();
            sum_sq += input_vector.Get() * input_vector.Get();
            ++input_vector;
        }
        input_vector -= v_size;

        const float mean = sum / static_cast<float>(v_size);
        float stddev_inv = 0.0f;
        const float variance = sum_sq / static_cast<float>(v_size) - mean * mean;
        if (variance == 0) {
            stddev_inv = 1.0f / std::sqrt(normalization_epsilon);
        } else {
            stddev_inv = 1.0f / std::sqrt(variance);
        }

        for (uint32_t i = 0; i < v_size; ++i) {
            output_vector.Set((input_vector.Get() - mean) * stddev_inv);
            ++output_vector;
            ++input_vector;
        }
        // Don't reset iterator to handle next batch
    }
    output_vector -= v_size * n_batch;
    input_vector -= v_size * n_batch;
}

void ZeroVector(armnn::Encoder<float>& vector,
                uint32_t vSize)
{
    for (uint32_t v = 0; v < vSize; v++)
    {
        vector.Set(0.0f);
        ++vector;
    }
    vector -= vSize;
}

void MatrixBatchVectorMultiplyAccumulate(armnn::Decoder<float>& matrix,
                                         uint32_t mRows,
                                         uint32_t mCols,
                                         armnn::Decoder<float>& vector,
                                         uint32_t nBatch,
                                         armnn::Encoder<float>& outResult)
{
    for (uint32_t b = 0; b < nBatch; b++)
    {
        for (uint32_t r = 0; r < mRows; r++)
        {
            vector += b * mCols;
            for (uint32_t c = 0; c < mCols; c++)
            {
                outResult.Set(outResult.Get() + matrix.Get() * vector.Get());
                ++matrix;
                ++vector;
            }
            outResult += 1;
            vector -= (b+1) * mCols;
        }
        matrix -= (mRows * mCols);
    }
    outResult -= (mRows * nBatch);
}

void VectorBatchVectorAssign(armnn::Decoder<float>& vector,
                             uint32_t vSize,
                             uint32_t nBatch,
                             armnn::Encoder<float>& outBatchVector)
{
    for (uint32_t b = 0; b < nBatch; b++)
    {
        for (uint32_t v = 0; v < vSize; v++)
        {
            outBatchVector.Set(vector.Get());
            ++outBatchVector;
            ++vector;
        }
        vector -= vSize;
    }
    outBatchVector -= (nBatch * vSize);
}

void VectorBatchVectorCwiseProductAccumulate(armnn::Decoder<float>& vector,
                                             uint32_t vSize,
                                             armnn::Decoder<float>& batchVector,
                                             uint32_t nBatch,
                                             armnn::Encoder<float>& outResult)
{
    for (uint32_t b = 0; b < nBatch; b++)
    {
        for (uint32_t v = 0; v < vSize; v++)
        {
            outResult.Set(outResult.Get() + vector.Get() * batchVector.Get());
            ++outResult;
            ++vector;
            ++batchVector;
        }
        vector -= vSize;
    }
    batchVector -= vSize * nBatch;
    outResult -= vSize * nBatch;
}

void VectorBatchVectorCwiseProduct(armnn::Decoder<float>& vector,
                                   uint32_t vSize,
                                   armnn::Decoder<float>& batchVector,
                                   uint32_t nBatch,
                                   armnn::Encoder<float>& outResult)
{
    for (uint32_t b = 0; b < nBatch; b++)
    {
        for (uint32_t v = 0; v < vSize; v++)
        {
            outResult.Set(vector.Get() * batchVector.Get());
            ++outResult;
            ++vector;
            ++batchVector;
        }
        vector -= vSize;
    }
    batchVector -= vSize * nBatch;
    outResult -= vSize * nBatch;
}

void Sub1Vector(armnn::Decoder<float>& vector,
                uint32_t vSize,
                armnn::Encoder<float>& result)
{
    for (uint32_t v = 0; v < vSize; v++)
    {
        result.Set(1.0f - vector.Get());
        ++vector;
        ++result;
    }
    vector -= vSize;
    result -= vSize;
}

void VectorVectorCwiseProduct(armnn::Decoder<float>& vector1,
                              armnn::Decoder<float>& vector2,
                              uint32_t vSize,
                              armnn::Encoder<float>& outResult)
{
    for (uint32_t v = 0; v < vSize; v++)
    {
        outResult.Set(vector1.Get() * vector2.Get());
        ++outResult;
        ++vector1;
        ++vector2;
    }
    outResult -= vSize;
    vector1 -= vSize;
    vector2 -= vSize;
}

void VectorVectorCwiseProductAccumulate(armnn::Decoder<float>& vector1,
                                        armnn::Decoder<float>& vector2,
                                        uint32_t vSize,
                                        armnn::Encoder<float>& outResult)
{
    for (uint32_t v = 0; v < vSize; v++)
    {
        outResult.Set(outResult.Get() + vector1.Get() * vector2.Get());
        ++outResult;
        ++vector1;
        ++vector2;
    }
    outResult -= vSize;
    vector1 -= vSize;
    vector2 -= vSize;
}

float Clip(float f,
           float absLimit)
{
    float result = (absLimit < f) ? absLimit : f;
    result = (-absLimit > result) ? -absLimit : result;
    return result;
}

void ClipVector(armnn::Decoder<float>& vector,
                uint32_t vSize,
                float absLimit,
                armnn::Encoder<float>& outResult)
{
    for (uint32_t v = 0; v < vSize; v++)
    {
        outResult.Set(Clip(vector.Get(), absLimit));
        ++vector;
        ++outResult;
    }
    vector -= vSize;
    outResult -= vSize;
}

void CopyVector(armnn::Decoder<float>& vector,
                uint32_t vSize,
                armnn::Encoder<float>& outResult)
{
    for (uint32_t v = 0; v < vSize; v++)
    {
        outResult.Set(vector.Get());
        ++outResult;
        ++vector;
    }
    outResult -= vSize;
    vector -= vSize;
}

void SetActivationParameters(uint32_t activation,
                             armnn::ActivationFunction& outArmnnActivation,
                             float& outA,
                             float& outB)
{
    switch (activation)
    {
        case 0: // None
            outA = 0;
            outB = 0;
            return;

        case 1: // Relu
            outArmnnActivation = armnn::ActivationFunction::ReLu;
            outA = 0;
            outB = 0;
            return;

        case 3: // Relu6
            outArmnnActivation = armnn::ActivationFunction::BoundedReLu;
            outA = 6;
            outB = 0;
            return;

        case 4: // Tanh
            outArmnnActivation = armnn::ActivationFunction::TanH;
            outA = 1;
            outB = 1;
            return;

        case 6: // Sigmoid
            outArmnnActivation = armnn::ActivationFunction::Sigmoid;
            outA = 0;
            outB = 0;
            return;

        default:
            throw armnn::Exception("Unsupported activation function: " + std::to_string(activation));
    }
}

std::unique_ptr<armnn::ScopedTensorHandle> AssignScopedTensorHandle(const armnn::ConstTensorHandle *ptr)
{
    if (!ptr)
    {
        return nullptr;
    }

    return std::make_unique<armnn::ScopedTensorHandle>(*ptr);
}