compile_ops.cpp 4.82 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.
 */
24
25
26
27
28
#include <migraphx/gpu/compile_ops.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/module.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
29
#include <migraphx/par_for.hpp>
30
31
#include <migraphx/register_op.hpp>
#include <migraphx/op/identity.hpp>
32
#include <migraphx/gpu/compiler.hpp>
33
34
35
36
37

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

38
39
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_GPU_COMPILE_PARALLEL);

40
41
struct precompile_op
{
42
43
44
    operation op                = op::identity{};
    std::size_t additional_args = 1;
    bool ignore_modules         = false;
45
46
47
48

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
49
50
51
        return pack(f(self.op, "op"),
                    f(self.additional_args, "additional_args"),
                    f(self.ignore_modules, "ignore_modules"));
52
53
54
55
56
57
    }

    std::string name() const { return "gpu::precompile_op"; }

    shape compute_shape(std::vector<shape> inputs, const std::vector<module_ref>& mods) const
    {
58
59
60
61
        // Pop off additional args
        inputs.resize(inputs.size() - additional_args);
        if(ignore_modules)
            return op.compute_shape(inputs);
62
63
64
65
66
67
68
69
70
71
72
        return op.compute_shape(inputs, mods);
    }

    std::ptrdiff_t output_alias(const std::vector<shape>& shapes) const
    {
        return shapes.size() - 1;
    }
};

MIGRAPHX_REGISTER_OP(precompile_op);

Paul Fultz II's avatar
Paul Fultz II committed
73
74
struct compiled_result
{
75
    compiler_replace replace;
Paul Fultz II's avatar
Paul Fultz II committed
76
77
78
    instruction_ref ins;
};

79
80
81
82
83
struct compile_plan
{
    context* ctx;
    operation preop;
    instruction_ref ins;
Paul's avatar
Paul committed
84
    optional<tuning_config> config = nullopt;
85
    std::vector<compiled_result> results = {};
Paul's avatar
Paul committed
86
87
88
89
90
    void update_config()
    {
        config = get_tuning_config(*ctx, ins, preop);
    }
    template<class Vector>
91
92
    void add_compiles(Vector& compiles)
    {
Paul's avatar
Paul committed
93
        if (config.has_value())
94
95
96
        {
            const auto& solutions = config.value().solutions;
            results.resize(solutions.size());
Paul's avatar
Paul committed
97
            for(auto i:range(solutions.size()))
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
            {
                auto solution = solutions[i];
                compiles.emplace_back([=] {
                    results[i] = compiled_result{compile(*ctx, ins, preop, solution), ins};
                });
            }
        }
        else
        {
            results.resize(1);
            compiles.emplace_back([=] {
                results[0] = compiled_result{compile(*ctx, ins, preop, value{}), ins};
            });
        }
    }
    void replace(module& m) const
    {
        if(results.size() == 1)
        {
            results.front().replace.replace(m, results.front().ins);
        }
        else
        {
            // TODO: Benchmark
        }
    }
};

126
127
128
129
130
131
132
133
template <class F>
void par_compile(std::size_t n, F f)
{
    if(n == 0)
        return;
    par_for(n, n / value_of(MIGRAPHX_GPU_COMPILE_PARALLEL{}, n), f);
}

134
135
void compile_ops::apply(module& m) const
{
136
    std::vector<compile_plan> cps;
Paul's avatar
Paul committed
137
    // Find all precompile opes
138
139
140
141
142
    for(auto ins : iterator_for(m))
    {
        if(ins->name() != "gpu::precompile_op")
            continue;
        operation preop = any_cast<precompile_op>(ins->get_operator()).op;
143
        cps.push_back({ctx, preop, ins});
Paul Fultz II's avatar
Paul Fultz II committed
144
    }
Paul's avatar
Paul committed
145
146
147
148
149
    // Get the tuning configs for all ops
    par_compile(cps.size(), [&](auto i) {
        cps[i].update_config();
    });
    // Compile everything in parallel
150
    std::vector<std::function<void()>> compiles;
Paul's avatar
Paul committed
151
    for(auto& cp:cps)
152
153
154
155
156
    {
        cp.add_compiles(compiles);
    }
    par_compile(compiles.size(), [&](auto i) { compiles[i](); });

Paul's avatar
Paul committed
157
158
    // Replace and/or benchmark
    for(const auto& cp:cps)
Paul Fultz II's avatar
Paul Fultz II committed
159
    {
160
        cp.replace(m);
161
162
163
164
165
166
167
    }
}

} // namespace gpu

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx