generate.hpp 2.31 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
#include <migraph/type_traits.hpp>
Paul's avatar
Paul committed
7
8
9
10
#include <random>

namespace migraph {

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

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

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

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

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

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

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

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

Paul's avatar
Latest  
Paul committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
78
template <class T>
Paul's avatar
Paul committed
79
std::vector<T> generate_tensor_data(const migraph::shape& s, unsigned long seed = 0)
Paul's avatar
Paul committed
80
81
{
    std::vector<T> result(s.elements());
Paul's avatar
Paul committed
82
    std::generate(result.begin(), result.end(), xorshf96_generator<T>{seed});
Paul's avatar
Latest  
Paul committed
83
    // std::generate(result.begin(), result.end(), [&]{ return seed % 7; });
Paul's avatar
Paul committed
84
    // std::generate(result.begin(), result.end(), []{ return 1; });
Paul's avatar
Paul committed
85
86
87
    return result;
}

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

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

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

} // namespace migraph

#endif