fuse_mlir.cpp 5.27 KB
Newer Older
Paul's avatar
Paul committed
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
26
27
28
#include <migraphx/gpu/fuse_mlir.hpp>
#include <migraphx/gpu/mlir.hpp>
#include <migraphx/matcher.hpp>
#include <migraphx/pass_manager.hpp>
#include <migraphx/make_op.hpp>
Paul's avatar
Paul committed
29
#include <migraphx/register_op.hpp>
Paul's avatar
Paul committed
30
31
32
33
34
35
36
37

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

struct module;

namespace gpu {

Paul's avatar
Paul committed
38
#ifdef MIGRAPHX_MLIR
Paul's avatar
Paul committed
39
40
41
42
43
44
45
46
47
48
49
struct mlir_conv
{
    operation op = make_op("convolution");

    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.op, "op"));
    }

    std::string name() const { return "gpu::mlir_conv"; }
Paul's avatar
Paul committed
50
    shape compute_shape(std::vector<shape> inputs, const std::vector<module_ref>& mods) const
Paul's avatar
Paul committed
51
52
53
54
55
56
57
    {
        check_shapes{inputs, *this}.standard();
        if(mods.size() != 1)
            MIGRAPHX_THROW("should have one submodule.");
        if(inputs.size() < 2)
            MIGRAPHX_THROW("should have at least two inputs.");
        auto n = inputs.size();
Paul's avatar
Format  
Paul committed
58
        return op.compute_shape({inputs[n - 2], inputs[n - 1]});
Paul's avatar
Paul committed
59
60
61
62
    }
};
MIGRAPHX_REGISTER_OP(mlir_conv);

Paul's avatar
Paul committed
63
namespace {
Paul's avatar
Paul committed
64
65
66
struct find_conv_pointwise
{
    // Find a convolution followed by a pointwise operation.
Paul's avatar
Paul committed
67
    auto matcher() const
Paul's avatar
Paul committed
68
    {
Paul's avatar
Format  
Paul committed
69
70
71
        auto convolution =
            match::skip(match::name("contiguous"))(match::name("convolution").bind("convolution"));
        return match::name("pointwise")(match::any_of[match::inputs()](convolution.bind("x")));
Paul's avatar
Paul committed
72
73
    }

Paul's avatar
Paul committed
74
    void apply(module_pass_manager& mpm, const match::matcher_result& r) const
Paul's avatar
Paul committed
75
    {
Paul's avatar
Format  
Paul committed
76
        auto ins      = r.result;
Paul's avatar
Paul committed
77
        auto conv_ins = r.instructions["convolution"];
Paul's avatar
Format  
Paul committed
78
        auto x_ins    = r.instructions["x"]; // input after contiguous
Paul's avatar
Format  
Paul committed
79
        auto* pm      = ins->module_inputs().front();
Paul's avatar
Format  
Paul committed
80
        auto names    = pm->get_parameter_names();
Paul's avatar
Paul committed
81
        // Whitelist pointwise operators
Paul's avatar
Format  
Paul committed
82
83
84
85
        if(std::any_of(pm->begin(), pm->end(), [](const auto& i) {
               return not contains({"@literal", "@param", "@return", "convolution", "add", "relu"},
                                   i.name());
           }))
Paul's avatar
Paul committed
86
            return;
Paul's avatar
Paul committed
87
        // Only fuse with fp32 for now
Paul's avatar
Format  
Paul committed
88
89
90
        if(std::any_of(ins->inputs().begin(), ins->inputs().end(), [&](auto i) {
               return i->get_shape().type() != shape::type_t::float_type;
           }))
Paul's avatar
Paul committed
91
            return;
Paul's avatar
Paul committed
92
        std::sort(names.begin(), names.end());
Paul's avatar
Paul committed
93
94
        module_ref mm = mpm.create_module("mlir_" + pm->name());
        mm->set_bypass();
Paul's avatar
Paul committed
95
        std::unordered_map<instruction_ref, instruction_ref> param_map;
Paul's avatar
Paul committed
96
        auto x    = mm->add_parameter("x" + std::to_string(names.size()),
Paul's avatar
Format  
Paul committed
97
                                   conv_ins->inputs().at(0)->get_shape());
Paul's avatar
Paul committed
98
        auto w    = mm->add_parameter("x" + std::to_string(names.size() + 1),
Paul's avatar
Format  
Paul committed
99
                                   conv_ins->inputs().at(1)->get_shape());
Paul's avatar
Paul committed
100
        auto conv = mm->add_instruction(conv_ins->get_operator(), {x, w});
Paul's avatar
Format  
Paul committed
101
102
103
104
105
106
107
108
        std::transform(names.begin(),
                       names.end(),
                       ins->inputs().begin(),
                       std::inserter(param_map, param_map.end()),
                       [&](auto name, auto input) {
                           if(input == x_ins)
                               return std::make_pair(pm->get_parameter(name), conv);
                           return std::make_pair(pm->get_parameter(name),
Paul's avatar
Paul committed
109
                                                 mm->add_parameter(name, input->get_shape()));
Paul's avatar
Format  
Paul committed
110
                       });
Paul's avatar
Paul committed
111
        mm->add_return(mm->insert_module_instructions(mm->end(), pm, param_map));
Paul's avatar
Paul committed
112

Paul's avatar
Paul committed
113
        std::vector<instruction_ref> inputs;
Paul's avatar
Format  
Paul committed
114
115
116
117
        std::copy_if(ins->inputs().begin(),
                     ins->inputs().end(),
                     std::back_inserter(inputs),
                     [&](auto input) { return input != conv_ins; });
Paul's avatar
Paul committed
118
        inputs.insert(inputs.end(), conv_ins->inputs().begin(), conv_ins->inputs().end());
Paul's avatar
Paul committed
119
120
        mpm.get_module().replace_instruction(
            ins, mlir_conv{conv_ins->get_operator()}, inputs, {mm});
Paul's avatar
Paul committed
121
122
    }
};
Paul's avatar
Format  
Paul committed
123
} // namespace
Paul's avatar
Paul committed
124

Paul's avatar
Paul committed
125
126
#endif

Paul's avatar
Format  
Paul committed
127
128
void fuse_mlir::apply(module_pass_manager& mpm) const
{
Paul's avatar
Paul committed
129
#ifdef MIGRAPHX_MLIR
Paul's avatar
Format  
Paul committed
130
    match::find_matches(mpm, find_conv_pointwise{});
Paul's avatar
Paul committed
131
132
133
#else
    (void)mpm;
#endif
Paul's avatar
Format  
Paul committed
134
}
Paul's avatar
Paul committed
135

Paul's avatar
Format  
Paul committed
136
} // namespace gpu
Paul's avatar
Paul committed
137
138
139

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx