generate.hpp 3.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_GENERATE_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_GENERATE_HPP
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
28
29
30
#include <migraphx/argument.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/type_traits.hpp>
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
31
32
#include <random>

Paul's avatar
Paul committed
33
namespace migraphx {
Paul's avatar
Paul committed
34
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
35

Paul's avatar
Paul committed
36
template <class T, MIGRAPHX_REQUIRES(is_floating_point<T>{})>
Paul's avatar
Paul committed
37
constexpr T normalize(unsigned long z)
38
{
Paul's avatar
Paul committed
39
    if(z == 0)
Paul's avatar
Paul committed
40
        return T(0);
Paul's avatar
Paul committed
41
    const auto max     = 32;
Paul's avatar
Paul committed
42
    const double range = max / 2; // NOLINT
Paul's avatar
Paul committed
43
    double result      = double(z % max) / range;
Paul's avatar
Latest  
Paul committed
44
    result -= 1;
Paul's avatar
Paul committed
45
    return T(result);
46
47
}

Paul's avatar
Paul committed
48
template <class T, MIGRAPHX_REQUIRES(is_signed<T>{} and not is_floating_point<T>{})>
Paul's avatar
Paul committed
49
constexpr T normalize(unsigned long z)
50
{
51
    const auto max      = 1UL << (sizeof(T) * 5);
Paul's avatar
Paul committed
52
    const auto half_max = max / 2;
53
54
55
    return half_max - (z % max);
}

Shucai Xiao's avatar
Shucai Xiao committed
56
57
58
template <class T,
          MIGRAPHX_REQUIRES(not is_signed<T>{} and std::is_integral<T>{} and
                            not std::is_same<T, bool>{})>
Paul's avatar
Paul committed
59
constexpr T normalize(unsigned long z)
60
{
61
    const auto max = 1UL << (sizeof(T) * 5);
62
63
64
    return z % max;
}

65
66
67
68
69
70
template <class T, MIGRAPHX_REQUIRES(std::is_same<T, bool>{})>
constexpr bool normalize(unsigned long z)
{
    return static_cast<bool>(z % 2);
}

Paul's avatar
Paul committed
71
template <class T>
72
73
struct xorshf96_generator
{
Paul's avatar
Paul committed
74
75
    unsigned long x = 123456789;
    unsigned long y = 362436069;
Paul's avatar
Paul committed
76
77
    unsigned long z;

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

80
    constexpr T operator()() noexcept
81
    {
82
83
84
        x ^= x << 16U;
        x ^= x >> 5U;
        x ^= x << 1U;
85

Paul's avatar
Paul committed
86
        unsigned long t = x;
Paul's avatar
Paul committed
87
88
89
        x               = y;
        y               = z;
        z               = t ^ x ^ y;
90

Paul's avatar
Paul committed
91
        return normalize<T>(z);
92
93
94
    }
};

Paul's avatar
Latest  
Paul committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
111
template <class T>
112
auto generate_tensor_data(const migraphx::shape& s, unsigned long seed = 0)
Paul's avatar
Paul committed
113
{
114
115
    auto result = make_shared_array<T>(s.element_space());
    std::generate(result.get(), result.get() + s.element_space(), xorshf96_generator<T>{seed});
Paul's avatar
Paul committed
116
117
118
    return result;
}

Paul's avatar
Paul committed
119
template <class T>
120
auto fill_tensor_data(const migraphx::shape& s, unsigned long value = 0)
Paul's avatar
Paul committed
121
{
122
123
    auto result = make_shared_array<T>(s.element_space());
    std::generate(result.get(), result.get() + s.element_space(), [=] { return value; });
Paul's avatar
Paul committed
124
125
126
127
128
    return result;
}

argument fill_argument(shape s, unsigned long value = 0);

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

Paul's avatar
Paul committed
131
132
133
literal generate_literal(shape s, unsigned long seed = 0);

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

Paul's avatar
Paul committed
135
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
136
} // namespace migraphx
Paul's avatar
Paul committed
137
138

#endif