generate.hpp 1015 Bytes
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>
11
12
struct xorshf96_generator
{
Paul's avatar
Paul committed
13
14
15
    unsigned long x = 123456789;
    unsigned long y = 362436069;
    unsigned long z = 521288629;
16
17
18

    constexpr T operator()()
    {
19
20
21
        x ^= x << 16U;
        x ^= x >> 5U;
        x ^= x << 1U;
22

Paul's avatar
Paul committed
23
        unsigned long t = x;
Paul's avatar
Paul committed
24
25
26
        x               = y;
        y               = z;
        z               = t ^ x ^ y;
27

Paul's avatar
Paul committed
28
        return z;
29
30
31
    }
};

Paul's avatar
Paul committed
32
template <class T>
Paul's avatar
Paul committed
33
std::vector<T> generate_tensor_data(migraph::shape s, std::mt19937::result_type)
Paul's avatar
Paul committed
34
35
{
    std::vector<T> result(s.elements());
36
    std::generate(result.begin(), result.end(), xorshf96_generator<T>{});
Paul's avatar
Paul committed
37
38
39
    return result;
}

Paul's avatar
Paul committed
40
41
42
argument generate_argument(shape s, std::mt19937::result_type seed = 0);

literal generate_literal(shape s, std::mt19937::result_type seed = 0);
Paul's avatar
Paul committed
43
44
45
46

} // namespace migraph

#endif