generate.hpp 2.29 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_GENERATE_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_GENERATE_HPP

#include <migraph/argument.hpp>
Paul's avatar
Paul committed
5
#include <migraph/literal.hpp>
Paul's avatar
Paul committed
6
7
8
9
#include <random>

namespace migraph {

Paul's avatar
Paul committed
10
template <class T, MIGRAPH_REQUIRES(std::is_floating_point<T>{})>
Paul's avatar
Paul committed
11
constexpr T normalize(unsigned long z)
12
{
Paul's avatar
Paul committed
13
14
    if(z == 0)
        return 0;
Paul's avatar
Paul committed
15
    const auto max     = 32;
Paul's avatar
Paul committed
16
    const double range = max / 2; // NOLINT
Paul's avatar
Paul committed
17
    double result      = (z % max) / range;
Paul's avatar
Latest  
Paul committed
18
19
    result -= 1;
    return result;
20
21
}

Paul's avatar
Paul committed
22
template <class T, MIGRAPH_REQUIRES(std::is_signed<T>{} and not std::is_floating_point<T>{})>
Paul's avatar
Paul committed
23
constexpr T normalize(unsigned long z)
24
{
Paul's avatar
Paul committed
25
26
    const auto max      = std::numeric_limits<T>::max();
    const auto half_max = max / 2;
27
28
29
    return half_max - (z % max);
}

Paul's avatar
Paul committed
30
template <class T, MIGRAPH_REQUIRES(not std::is_signed<T>{} and std::is_integral<T>{})>
Paul's avatar
Paul committed
31
constexpr T normalize(unsigned long z)
32
33
34
35
36
{
    const auto max = std::numeric_limits<T>::max();
    return z % max;
}

Paul's avatar
Paul committed
37
template <class T>
38
39
struct xorshf96_generator
{
Paul's avatar
Paul committed
40
41
    unsigned long x = 123456789;
    unsigned long y = 362436069;
Paul's avatar
Paul committed
42
43
    unsigned long z;

Paul's avatar
Paul committed
44
    xorshf96_generator(unsigned long seed = 0) : z(521288629ULL ^ seed) {}
45

46
    constexpr T operator()() noexcept
47
    {
48
49
50
        x ^= x << 16U;
        x ^= x >> 5U;
        x ^= x << 1U;
51

Paul's avatar
Paul committed
52
        unsigned long t = x;
Paul's avatar
Paul committed
53
54
55
        x               = y;
        y               = z;
        z               = t ^ x ^ y;
56

Paul's avatar
Paul committed
57
        return normalize<T>(z);
58
59
60
    }
};

Paul's avatar
Latest  
Paul committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
template <class T>
struct xorshift_generator
{
    unsigned long x;

    xorshift_generator(unsigned long seed = 0) : x(521288629ULL ^ seed) {}

    constexpr T operator()() noexcept
    {
        x ^= x >> 12U;
        x ^= x << 25U;
        x ^= x >> 27U;
        return normalize<T>(x * 0x2545F4914F6CDD1D);
    }
};

Paul's avatar
Paul committed
77
template <class T>
Paul's avatar
Paul committed
78
std::vector<T> generate_tensor_data(const migraph::shape& s, unsigned long seed = 0)
Paul's avatar
Paul committed
79
80
{
    std::vector<T> result(s.elements());
Paul's avatar
Paul committed
81
    std::generate(result.begin(), result.end(), xorshf96_generator<T>{seed});
Paul's avatar
Latest  
Paul committed
82
    // std::generate(result.begin(), result.end(), [&]{ return seed % 7; });
Paul's avatar
Paul committed
83
    // std::generate(result.begin(), result.end(), []{ return 1; });
Paul's avatar
Paul committed
84
85
86
    return result;
}

Paul's avatar
Paul committed
87
argument generate_argument(shape s, unsigned long seed = 0);
Paul's avatar
Paul committed
88

Paul's avatar
Paul committed
89
90
91
literal generate_literal(shape s, unsigned long seed = 0);

literal abs(literal l);
Paul's avatar
Paul committed
92
93
94
95

} // namespace migraph

#endif