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

10
11
namespace migraph {
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
12

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

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

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

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

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

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

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

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

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

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

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

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

96
} // namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
97
98
99
} // namespace migraph

#endif