aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src/generate/generate_pseudo_random.cc
blob: 865483bb526484ab80f7a51fe1d45483a5c92ffb (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
// Copyright (c) 2023, 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.h"
#include "generate_utils.h"
#include "half.hpp"

#include <algorithm>
#include <array>
#include <iterator>
#include <limits>
#include <numeric>
#include <random>
#include <string>
#include <type_traits>
#include <vector>

namespace
{

// Random FP generator
template <typename FP>
class PseudoRandomGeneratorFloat
{
public:
    PseudoRandomGeneratorFloat(uint64_t seed)
        : _gen(seed)
    {
        // Uniform real distribution generates real values in the range [a, b]
        // and requires that b - a <= std::numeric_limits<FP>::max() so here
        // we choose some arbitrary values that satisfy that condition.
        constexpr auto min = std::numeric_limits<FP>::lowest() / 2;
        constexpr auto max = std::numeric_limits<FP>::max() / 2;
        static_assert(max <= std::numeric_limits<FP>::max() + min);

        setDistribution(min, max);
    }

    PseudoRandomGeneratorFloat(uint64_t seed, FP min, FP max)
        : _gen(seed)
    {
        setDistribution(min, max);
    }

    FP getRandomFloat()
    {
        if (_useUniform)
            return _unidis(_gen);
        else
            return _pwcdis(_gen);
    }

private:
    void setDistribution(FP min, FP max)
    {
        _unidis = std::uniform_real_distribution<FP>(min, max);

        // Piecewise Constant distribution for larger ranges
        double range = std::abs(max - min);
        double mid;
        if (max == -min)
            mid = 0.f;
        else
            mid = (range / 2) + min;
        double segment = std::min<double>(1000.0, range / 5);

        const std::array<double, 7> intervals{
            min, min + segment, mid - segment, mid, mid + segment, max - segment, max
        };
        const std::array<double, 7> weights{ 1.0, 0.1, 1.0, 2.0, 1.0, 0.1, 1.0 };
        _pwcdis = std::piecewise_constant_distribution<FP>(intervals.begin(), intervals.end(), weights.begin());

        // Uniform distribution works well on smaller ranges
        _useUniform = (range < 2000.0);
    }

    std::mt19937 _gen;
    std::uniform_real_distribution<FP> _unidis;
    std::piecewise_constant_distribution<FP> _pwcdis;
    bool _useUniform;
};

template <typename DataType>
bool generateFP(const TosaReference::GenerateConfig& cfg, DataType* data, size_t size)
{
    const TosaReference::PseudoRandomInfo& prinfo = cfg.pseudoRandomInfo;

    PseudoRandomGeneratorFloat<float>* generator;
    bool roundMode = prinfo.round;

    if (prinfo.range.size() == 2)
    {
        const float min = std::stof(prinfo.range[0]);
        const float max = std::stof(prinfo.range[1]);
        generator       = new PseudoRandomGeneratorFloat<float>(prinfo.rngSeed, min, max);
    }
    else
    {
        generator = new PseudoRandomGeneratorFloat<float>(prinfo.rngSeed);
    }

    const auto T = TosaReference::numElementsFromShape(cfg.shape);
    const bool comparisonOp =
        (cfg.opType == Op::Op_EQUAL) || (cfg.opType == Op::Op_GREATER_EQUAL) || (cfg.opType == Op::Op_GREATER);
    for (auto t = 0; t < T; ++t)
    {
        data[t] = static_cast<DataType>(generator->getRandomFloat());
        if (comparisonOp && (t % 4 == 0))
        {
            // Set every 4th value to 0 to enable better comparison testing
            data[t] = static_cast<DataType>(0.f);
        }
        else if (roundMode)
        {
            data[t] = static_cast<DataType>(std::roundf(data[t]));
        }
    }
    return true;
}

// Random INT generator
template <typename INT>
class PseudoRandomGeneratorInteger
{
public:
    PseudoRandomGeneratorInteger(uint64_t seed)
        : _gen(seed)
    {
        constexpr auto min = std::numeric_limits<INT>::min();
        constexpr auto max = std::numeric_limits<INT>::max();

        setDistribution(min, max);
    }

    PseudoRandomGeneratorInteger(uint64_t seed, INT min, INT max)
        : _gen(seed)
    {
        setDistribution(min, max);
    }

    INT getRandomInteger()
    {
        return _unidis(_gen);
    }

    INT getRandomInteger(INT min, INT max)
    {
        typename std::uniform_int_distribution<INT>::param_type range(min, max);
        return _unidis(_gen, range);
    }

private:
    void setDistribution(INT min, INT max)
    {
        _unidis = std::uniform_int_distribution<INT>(min, max);
    }

    std::mt19937 _gen;
    std::uniform_int_distribution<INT> _unidis;
};

template <typename DataType>
bool shuffleINTbyRow(const TosaReference::GenerateConfig& cfg, DataType* data, size_t size)
{
    const TosaReference::PseudoRandomInfo& prinfo = cfg.pseudoRandomInfo;
    PseudoRandomGeneratorInteger<DataType>* generator;

    if (cfg.shape.size() != 2)
    {
        WARNING("[Generator][PR][INT] Shuffle only supports 2 dimensional tensors.");
        return false;
    }
    if (prinfo.range.size() != 2)
    {
        WARNING("[Generator][PR][INT] Cannot create un-ranged shuffle data.");
        return false;
    }

    const int32_t min = std::stoi(prinfo.range[0]);
    const int32_t max = std::stoi(prinfo.range[1]);
    generator         = new PseudoRandomGeneratorInteger<DataType>(prinfo.rngSeed, min, max);

    // Work out inclusive range
    const auto range = std::abs(max - min) + 1;
    const auto N     = cfg.shape[0];    // Number of rows
    const auto W     = cfg.shape[1];    // Width of rows
    if (W > range)
    {
        WARNING("[Generator][PR][INT] Cannot fill data size %d with given shuffle range %d.", W, range);
        return false;
    }

    std::vector<DataType> numbers(range);
    for (int n = 0; n < N; ++n)
    {
        // Fill in the numbers in range
        std::iota(numbers.begin(), numbers.end(), min);

        // Perform random shuffling
        for (auto num = numbers.begin(); num < numbers.end(); ++num)
        {
            std::swap(*num, numbers[generator->getRandomInteger()]);
        }
        // Copy amount of data required
        for (auto w = 0; w < W; ++w)
        {
            data[(n * W) + w] = numbers[w];
        }
    }
    return true;
}

template <typename DataType>
bool generateINT(const TosaReference::GenerateConfig& cfg, DataType* data, size_t size)
{
    const TosaReference::PseudoRandomInfo& prinfo = cfg.pseudoRandomInfo;
    PseudoRandomGeneratorInteger<DataType>* generator;

    const auto T = TosaReference::numElementsFromShape(cfg.shape);

    if (prinfo.range.size() == 2)
    {
        const int32_t min = std::stoi(prinfo.range[0]);
        const int32_t max = std::stoi(prinfo.range[1]);
        generator         = new PseudoRandomGeneratorInteger<DataType>(prinfo.rngSeed, min, max);
    }
    else
    {
        generator = new PseudoRandomGeneratorInteger<DataType>(prinfo.rngSeed);
    }

    for (auto t = 0; t < T; ++t)
    {
        data[t] = generator->getRandomInteger();
    }
    return true;
}
}    // namespace

namespace TosaReference
{
bool generatePseudoRandom(const GenerateConfig& cfg, void* data, size_t size)
{
    // Check we support the operator
    if (cfg.opType == Op::Op_UNKNOWN)
    {
        WARNING("[Generator][PR] Unknown operator.");
        return false;
    }
    if (cfg.pseudoRandomInfo.range.size() != 0 && cfg.pseudoRandomInfo.range.size() != 2)
    {
        WARNING("[Generator][PR] Invalid range");
        return false;
    }

    switch (cfg.dataType)
    {
        case DType::DType_FP32: {
            float* outData = reinterpret_cast<float*>(data);
            return generateFP(cfg, outData, size);
        }
        case DType::DType_FP16: {
            half_float::half* outData = reinterpret_cast<half_float::half*>(data);
            return generateFP(cfg, outData, size);
        }
        case DType::DType_INT32: {
            int32_t* outData = reinterpret_cast<int32_t*>(data);
            if (cfg.opType == Op::Op_SCATTER && cfg.inputPos == 1)
            {
                // Indices for SCATTER must not repeat - perform data shuffle
                return shuffleINTbyRow(cfg, outData, size);
            }
            else
            {
                return generateINT(cfg, outData, size);
            }
        }
        default:
            WARNING("[Generator][PR] Unsupported type.");
            return false;
    }
}
}    // namespace TosaReference