compile_ops.cpp 3.68 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
{
Paul's avatar
Format  
Paul committed
42
43
44
    operation op                 = op::identity{};
    std::size_t additional_args  = 1;
    bool ignore_modules          = false;
Paul's avatar
Paul committed
45
    optional<shape> output_shape = {};
46
47
48
49

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
50
51
        return pack(f(self.op, "op"),
                    f(self.additional_args, "additional_args"),
Paul's avatar
Paul committed
52
53
                    f(self.ignore_modules, "ignore_modules"),
                    f(self.output_shape, "output_shape"));
54
55
56
57
58
59
    }

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

    shape compute_shape(std::vector<shape> inputs, const std::vector<module_ref>& mods) const
    {
60
61
        // Pop off additional args
        inputs.resize(inputs.size() - additional_args);
Paul's avatar
Paul committed
62
        shape r{};
63
        if(ignore_modules)
Paul's avatar
Paul committed
64
65
66
            r = op.compute_shape(inputs);
        else
            r = op.compute_shape(inputs, mods);
Paul's avatar
Format  
Paul committed
67
        if(output_shape.has_value())
Paul's avatar
Paul committed
68
69
            r = *output_shape;
        return r;
70
71
72
73
74
75
76
77
78
79
    }

    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
80
81
struct compiled_result
{
82
    compiler_replace replace;
Paul Fultz II's avatar
Paul Fultz II committed
83
84
85
    instruction_ref ins;
};

86
87
88
89
90
91
92
93
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);
}

94
95
void compile_ops::apply(module& m) const
{
Paul Fultz II's avatar
Paul Fultz II committed
96
97
    std::vector<std::function<compiled_result()>> compiles;

98
99
100
101
102
    for(auto ins : iterator_for(m))
    {
        if(ins->name() != "gpu::precompile_op")
            continue;
        operation preop = any_cast<precompile_op>(ins->get_operator()).op;
103
104
105
        compiles.emplace_back([=]() -> compiled_result {
            return {compile(*ctx, ins, preop), ins};
        });
Paul Fultz II's avatar
Paul Fultz II committed
106
107
    }
    std::vector<compiled_result> results(compiles.size());
108
    par_compile(compiles.size(), [&](auto i) { results[i] = compiles[i](); });
Paul Fultz II's avatar
Paul Fultz II committed
109
110
    for(const auto& cr : results)
    {
111
        cr.replace(m, cr.ins);
112
113
114
115
116
117
118
    }
}

} // namespace gpu

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx