generate.cpp 3.35 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
#include <migraphx/generate.hpp>
Paul's avatar
Paul committed
25

Paul's avatar
Paul committed
26
namespace migraphx {
Paul's avatar
Paul committed
27
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
28

Paul's avatar
Paul committed
29
30
31
argument fill_argument(shape s, unsigned long value)
{
    argument result;
Shucai Xiao's avatar
Shucai Xiao committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    if(s.type() == shape::tuple_type)
    {
        std::vector<argument> sub_args;
        const auto& sub_ss = s.sub_shapes();
        std::transform(sub_ss.begin(), sub_ss.end(), std::back_inserter(sub_args), [&](auto ss) {
            return fill_argument(ss, value);
        });

        result = argument(sub_args);
    }
    else
    {
        s.visit_type([&](auto as) {
            using type = typename decltype(as)::type;
            auto v     = fill_tensor_data<type>(s, value);
            result     = {s, v};
        });
    }
Paul's avatar
Paul committed
50
51
52
    return result;
}

Paul's avatar
Paul committed
53
argument generate_argument(shape s, unsigned long seed)
Paul's avatar
Paul committed
54
{
Paul's avatar
Paul committed
55
    argument result;
Shucai Xiao's avatar
Shucai Xiao committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    if(s.type() == shape::tuple_type)
    {
        const auto& sub_ss = s.sub_shapes();
        std::vector<argument> sub_args;
        std::transform(sub_ss.begin(), sub_ss.end(), std::back_inserter(sub_args), [&](auto ss) {
            return generate_argument(ss, seed);
        });

        result = argument(sub_args);
    }
    else
    {
        s.visit_type([&](auto as) {
            // we use char type to store bool type internally, so bool_type
            // needs special processing to generate data
            if(s.type() == shape::bool_type)
            {
                auto v = generate_tensor_data<bool>(s, seed);
                result = {s, v};
            }
            else
            {
                using type = typename decltype(as)::type;
                auto v     = generate_tensor_data<type>(s, seed);
                result     = {s, v};
            }
        });
    }

Paul's avatar
Paul committed
85
86
87
    return result;
}

Paul's avatar
Paul committed
88
literal generate_literal(shape s, unsigned long seed)
Paul's avatar
Paul committed
89
90
91
92
93
{
    literal result;
    s.visit_type([&](auto as) {
        using type = typename decltype(as)::type;
        auto v     = generate_tensor_data<type>(s, seed);
94
        result     = {s, reinterpret_cast<char*>(v.get())};
Paul's avatar
Paul committed
95
96
97
98
    });
    return result;
}

Paul's avatar
Paul committed
99
100
101
// TODO: Move to literal.cpp
literal abs(literal l)
{
Paul's avatar
Paul committed
102
    return transform(std::move(l), [](auto x) { return std::fabs(x); });
Paul's avatar
Paul committed
103
104
}

Paul's avatar
Paul committed
105
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
106
} // namespace migraphx