random_uniform.cpp 6.93 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2023 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.
 */

#include <migraphx/instruction.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/onnx.hpp>
#include <migraphx/register_target.hpp>
#include <migraphx/verify.hpp>
#include <random>

#include <test.hpp>

/**
 * Reference test for the random_uniform operation.  Also invokes the random_seed operation.
 */

TEST_CASE(random_uniform_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    uint64_t seed(0);
    size_t sample_size(200);

    //      Shape of the random data
    migraphx::shape rs{migraphx::shape::float_type, {1, sample_size}};

    // data tensor must be allocated at this point but does not need to be initialized.
    std::vector<float> data(sample_size);
    auto input = mm->add_literal(migraphx::literal(rs, data));

    // Runtime randomization seed
    migraphx::shape seed_shape{migraphx::shape::uint64_type, {1}};
    std::vector<uint64_t> seed_data{seed};
    auto seed_input = mm->add_literal(migraphx::literal(seed_shape, seed_data));

    mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, input);
    p.compile(migraphx::make_target("ref"));

    // no params_map needed
    auto result = p.eval({}).back();
    std::vector<float> result_vec(sample_size);
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });

    // Compare result with the STL's mt19937 generator
    std::mt19937 gen(seed);
    std::uniform_real_distribution<> dis(0.0, 1.0);
    std::vector<float> rand_samples(sample_size);
    std::generate(rand_samples.begin(), rand_samples.end(), [&]() { return dis(gen); });
71
72
73
    EXPECT(migraphx::verify::verify_range_with_tolerance(result_vec,
                                                         migraphx::verify::expected{rand_samples},
                                                         migraphx::verify::tolerance{0.00001}));
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}

TEST_CASE(random_uniform_int_test)
{
    // random uniform distribution with an integer type input shape
    migraphx::program p;
    auto* mm = p.get_main_module();
    float seed(0.1);
    size_t sample_size(200);

    //      Shape of the random data
    migraphx::shape rs{migraphx::shape::uint16_type, {1, sample_size}};

    // data tensor must be allocated at this point but does not need to be initialized.
    std::vector<uint16_t> data(sample_size);
    auto input = mm->add_literal(migraphx::literal(rs, data));

    // Runtime randomization seed
    migraphx::shape seed_shape{migraphx::shape::float_type, {1}};
    std::vector<float> seed_data{seed};
    auto seed_input = mm->add_literal(migraphx::literal(seed_shape, seed_data));

    mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, input);
    p.compile(migraphx::make_target("ref"));

    migraphx::parameter_map params0;
    auto result = p.eval(params0).back();
    std::vector<uint16_t> result_vec(sample_size);
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });

    // Compare result with the STL's mt19937 generator
    std::mt19937 gen(seed);
    std::uniform_int_distribution<uint16_t> dis;
107
108
109
    std::vector<uint16_t> gold_rand_samples(sample_size);
    std::generate(gold_rand_samples.begin(), gold_rand_samples.end(), [&]() { return dis(gen); });
    EXPECT(migraphx::verify::verify_rms_range(result_vec, gold_rand_samples));
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
}

TEST_CASE(random_uniform_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    uint64_t seed(17);
    size_t sample_size(200);

    //      Shape of the random data
    migraphx::shape rs{migraphx::shape::float_type, {{1, 2}, {2, sample_size + 1}}};
    auto input = mm->add_parameter("Input_1", rs);

    // Runtime randomization seed
    migraphx::shape seed_shape{migraphx::shape::uint64_type, {1}};
    auto seed_input = mm->add_parameter("Seed", seed_shape);

    mm->add_instruction(migraphx::make_op("random_uniform", {}), seed_input, input);
    p.compile(migraphx::make_target("ref"));

    // Create a dummy input to hold the random data
    migraphx::shape input_fixed_shape1{migraphx::shape::float_type, {sample_size}};

    migraphx::parameter_map params0;
    params0["Input_1"] = migraphx::argument(input_fixed_shape1);

    std::vector<uint64_t> seed_data = {seed};
    params0["Seed"]                 = migraphx::argument(seed_shape, seed_data.data());
    auto result                     = p.eval(params0).back();

    std::vector<float> result_vec(sample_size);
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });

    // Compare result with the STL's mt19937 generator
    std::mt19937 gen(seed);
    std::uniform_real_distribution<> dis(0.0, 1.0);
146
147
148
    std::vector<float> gold_rand_samples(sample_size);
    std::generate(gold_rand_samples.begin(), gold_rand_samples.end(), [&]() { return dis(gen); });
    EXPECT(migraphx::verify::verify_rms_range(result_vec, gold_rand_samples));
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
}

TEST_CASE(random_uniform_and_seed_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    size_t sample_size(20000);

    //      Shape of the random data
    migraphx::shape rs{migraphx::shape::float_type, {{1, 2}, {2, sample_size + 1}}};
    auto input = mm->add_parameter("Input_1", rs);

    // Runtime randomization seed
    auto seed_input = mm->add_instruction(migraphx::make_op("random_seed"));
    mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, input);
    p.compile(migraphx::make_target("ref"));

    // Create a dummy input to hold the random data
    migraphx::shape input_fixed_shape1{migraphx::shape::float_type, {sample_size}};

    migraphx::parameter_map params0;
    params0["Input_1"] = migraphx::argument(input_fixed_shape1);
    auto result        = p.eval(params0).back();

    result.visit([&](auto output) { EXPECT(output.size() == sample_size); });
    // Do not check the content of the data since it's not repeatable
}