aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/generate/generate_dot_product_states.cc
blob: b78be7107e2d2163433373c9328a9235b4a3b45e (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) 2023-2024, ARM Limited.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#include "generate_dot_product.h"
#include "generate_utils.h"

#include <cmath>
#include <cstdint>

namespace
{

// Input index global variables
inline constexpr uint32_t P0 = 0;
inline constexpr uint32_t P1 = 1;
inline constexpr uint32_t P2 = 2;

// Unused helper function
template <typename... Args>
inline void unused(Args&&...)
{}

// Primitive generator class
//
// Yields a new value on function operator access and increases the
// index by one
class PrimitiveGenerator
{
public:
    PrimitiveGenerator(uint32_t S)
        : _S(S)
        , _m(0)
        , _r(0)
        , _index(0)
    {
        _m = (8 * _S + 1) * 0x705A5E75;
        _r = _m + 1;
    }

    [[nodiscard]] float operator()()
    {
        float sign   = (_r >> 31) == 0 ? +1 : -1;
        float pseudo = sign * (float)(_r & 0x7FFFFFFF) / (float)(0x7FFFFFFF);

        // Move index and calculate r value for the next index
        ++_index;
        _r = _r * _m + 1;

        return pseudo;
    }

    uint32_t nextIndex()
    {
        return _index;
    }

private:
    uint32_t _S;
    uint32_t _m;
    uint32_t _r;
    uint32_t _index;
};

//----------------------------------------------------------------------------//
// State generators - equivalent to tosa_mi_data() in the TOSA specification
//
// Each call to the generator returns the next generated value with an
// auto incrementing index
//----------------------------------------------------------------------------//

// Test set 0 generator
// The aim of this generator is to check that sum of products with zero gives zero result.
class GeneratorS0 : public TosaReference::IDotProductGenerator
{
public:
    GeneratorS0(uint32_t p)
        : _p(p)
        , _set_data0(2 * 0)
        , _set_data1(2 * 0 + 1)
    {}
    float operator()(uint32_t k) override
    {
        unused(k);
        const float s0 = _set_data0();
        const float s1 = _set_data1();
        if (_p == P0)
            return s0 < 0.f ? 0.f : s1;
        else if (_p == P1)
            return s0 < 0.f ? s1 : 0.f;
        else
            return 0.f;
    }
    uint32_t nextIndex()
    {
        ASSERT_MSG(_set_data0.nextIndex() == _set_data1.nextIndex(), "Internal index inconsistency in GeneratorS0")
        return _set_data0.nextIndex();
    }

private:
    uint32_t _p;
    PrimitiveGenerator _set_data0;
    PrimitiveGenerator _set_data1;
};

// Test set 1 generator
// The aim of this test set is to check values with large exponents.
class GeneratorS1 : public TosaReference::IDotProductGenerator
{
public:
    GeneratorS1(uint32_t p, uint32_t KS, float B)
        : _p(p)
        , _KS(KS)
        , _B(B)
        , _set_data(3 * 1 + p)
    {}
    float operator()(uint32_t k) override
    {
        unused(k);
        const float s = _set_data();
        float v       = 0.75f + 0.25f * s;
        if (_p != P2)
            return (_B / std::sqrt(_KS + 1)) * v;
        else
            return (_B * _B / (_KS + 1)) * v;
    }
    uint32_t nextIndex()
    {
        return _set_data.nextIndex();
    }

private:
    uint32_t _p;
    uint32_t _KS;
    float _B;
    PrimitiveGenerator _set_data;
};

// Test set 2 generator
// The aim of this test set is to check rounding error when accumulating small values
// onto a large value. In this case the small values are of similar magnitude. If the
// implementation changes the order of the sum, then the test data must also be reordered
// so that the largest values occur first in the sum.
class GeneratorS2 : public TosaReference::IDotProductGenerator
{
public:
    GeneratorS2(uint32_t p, uint32_t KS)
        : _p(p)
        , _KS(KS)
        , _set_data(2 * 2 + p)
    {}
    float operator()(uint32_t k) override
    {
        const float s = _set_data();
        if (_p != P2)
            return k == 0 ? 1.f : s / std::sqrt(_KS);
        else
            return 0.f;
    }
    uint32_t nextIndex()
    {
        return _set_data.nextIndex();
    }

private:
    uint32_t _p;
    uint32_t _KS;
    PrimitiveGenerator _set_data;
};

// Test set 3 generator
// The aim of this test set is to check rounding error when accumulating small values
// onto a large value. In this case the small values are of varying magnitude. If the
// implementation changes the order of the sum, then the test data must also be reordered
// so that the largest values occur first in the sum.
class GeneratorS3 : public TosaReference::IDotProductGenerator
{
public:
    GeneratorS3(uint32_t p)
        : _p(p)
        , _set_data(2 * 3 + p)
    {}
    float operator()(uint32_t k) override
    {
        const float s0 = _set_data();
        const float s1 = _set_data();
        if (_p != P2)
            return k == 0 ? 16.f : std::exp(2 * s0) * s1;
        else
            return 0.f;
    }
    uint32_t nextIndex()
    {
        return _set_data.nextIndex();
    }

private:
    uint32_t _p;
    PrimitiveGenerator _set_data;
};

// Test set 4 generator
// The aim of this test set is to check a mixture of zero and non-zero products.
class GeneratorS4 : public TosaReference::IDotProductGenerator
{
public:
    GeneratorS4(uint32_t p, uint32_t KS, float B)
        : _p(p)
        , _KS(KS)
        , _B(B)
        , _set_data0(2 * 4 + 0)
        , _set_data1(2 * 4 + 1)
    {}
    float operator()(uint32_t k) override
    {
        const float s0 = _set_data0();
        const float s1 = _set_data1();
        if (_p == P0)
            if (k == _KS / 2)
            {
                return s0 < 0 ? -0.5f : +0.5f;
            }
            else
            {
                return s0 < 0 ? 0.f : (_B / std::sqrt(_KS)) * s1;
            }
        else if (_p == P1)
            if (k == _KS / 2)
            {
                return s0 < 0 ? +0.5f : -0.5f;
            }
            else
            {
                return s0 < 0 ? (_B / std::sqrt(_KS)) * s1 : 0.f;
            }
        else
            return 0.f;
    }
    uint32_t nextIndex()
    {
        ASSERT_MSG(_set_data0.nextIndex() == _set_data1.nextIndex(), "Internal index inconsistency in GeneratorS4")
        return _set_data0.nextIndex();
    }

private:
    uint32_t _p;
    uint32_t _KS;
    float _B;
    PrimitiveGenerator _set_data0;
    PrimitiveGenerator _set_data1;
};

// Test set 5 generator
// The aim of this test set is to check signed inputs of large range.
class GeneratorS5 : public TosaReference::IDotProductGenerator
{
public:
    GeneratorS5(uint32_t p, uint32_t KS, float B)
        : _p(p)
        , _KS(KS)
        , _B(B)
        , _set_data(3 * 5 + p)
    {}
    float operator()(uint32_t k) override
    {
        unused(k);
        const float s = _set_data();
        if (_p != P2)
            return (_B / std::sqrt(_KS + 1)) * s;
        else
            return 0.f;
    }
    uint32_t nextIndex()
    {
        return _set_data.nextIndex();
    }

private:
    uint32_t _p;
    uint32_t _KS;
    float _B;
    PrimitiveGenerator _set_data;
};

float getBoundParameter(const DType& dataType, const DType& accType)
{
    // Work out the bounds parameter value B for the given data and accumulator types
    // Returns value > 0.f on success
    float B = 0.f;
    if (dataType == DType::DType_FP16)
    {
        if (accType == DType::DType_FP16)
            B = 255.875f;    // (1<<8) - (1/8);
        else if (accType == DType::DType_FP32)
            B = 65504.f;    // (1<<16) - (1<<5);
    }
    else if (dataType == DType::DType_BF16)
    {
        if (accType == DType::DType_FP32)
            B = 18374686479671623680.f;    // (1<<64) - (1<<56)
    }
    else if (dataType == DType::DType_FP32)
    {
        if (accType == DType::DType_FP32)
            B = 18446742974197923840.f;    // (1<<64) - (1<<40)
    }
    return B;
}

}    // namespace

namespace TosaReference
{

std::unique_ptr<IDotProductGenerator> pickDotProductGenerator(const GenerateConfig& cfg)
{
    // Generators can only support 3 inputs
    if (cfg.inputPos > 2)
        return nullptr;

    const DotProductInfo& dpinfo = cfg.dotProductInfo;

    float B = getBoundParameter(cfg.dataType, dpinfo.accType);
    if (B > 0.f)
    {
        auto param = cfg.inputPos;
        if (cfg.opType == Op_FFT2D)
        {
            // We only use param of zero for FFT2D tensors
            param = 0;
        }
        // Create the generator
        switch (dpinfo.s)
        {
            case 0:
                return std::make_unique<GeneratorS0>(param);
            case 1:
                return std::make_unique<GeneratorS1>(param, dpinfo.ks, B);
            case 2:
                return std::make_unique<GeneratorS2>(param, dpinfo.ks);
            case 3:
                return std::make_unique<GeneratorS3>(param);
            case 4:
                return std::make_unique<GeneratorS4>(param, dpinfo.ks, B);
            case 5:
                return std::make_unique<GeneratorS5>(param, dpinfo.ks, B);
            default:
                WARNING("[Generator][DP] Unsupported dot product test series for generator.");
                return nullptr;
        }
    }
    WARNING("[Generator][DP] Unsupported data types for generator.");
    return nullptr;
}

}    // namespace TosaReference