"tests/vscode:/vscode.git/clone" did not exist on "8b119e2261428b19bbbc1de09c17681795ab01a4"
lowering.cpp 7.65 KB
Newer Older
1
#include <rocblas.h>
Paul's avatar
Paul committed
2
#include <migraph/gpu/lowering.hpp>
Paul's avatar
Paul committed
3
4
5
#include <migraph/manage_ptr.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
Paul's avatar
Paul committed
6
#include <migraph/generate.hpp>
Paul's avatar
Paul committed
7
#include <migraph/shape_for_each.hpp>
Paul's avatar
Paul committed
8
9
#include <migraph/gpu/miopen.hpp>
#include <migraph/gpu/hip.hpp>
Paul's avatar
Paul committed
10
#include <migraph/dfor.hpp>
11
#include <migraph/gpu/device/contiguous.hpp>
Paul's avatar
Paul committed
12
#include <migraph/gpu/device/add.hpp>
Paul's avatar
Paul committed
13
#include <migraph/iterator_for.hpp>
Paul's avatar
Paul committed
14
15
#include <migraph/gpu/rocblas.hpp>
#include <migraph/gpu/context.hpp>
wsttiger's avatar
wsttiger committed
16
#include <migraph/gpu/convolution.hpp>
17
18
#include <migraph/gpu/contiguous.hpp>
#include <migraph/gpu/relu.hpp>
Khalique's avatar
Khalique committed
19
#include <migraph/gpu/leaky_relu.hpp>
20
21
#include <migraph/gpu/softmax.hpp>
#include <migraph/gpu/add.hpp>
Khalique's avatar
Khalique committed
22
#include <migraph/gpu/mul.hpp>
23
#include <migraph/gpu/batchnorm.hpp>
wsttiger's avatar
wsttiger committed
24
25
#include <migraph/gpu/pooling.hpp>
#include <migraph/gpu/gemm.hpp>
26
#include <migraph/gpu/concat.hpp>
Paul's avatar
Paul committed
27
#include <utility>
Paul's avatar
Paul committed
28
29

namespace migraph {
30
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
31
namespace gpu {
Paul's avatar
Paul committed
32
33
34

struct miopen_apply
{
Paul's avatar
Paul committed
35
    program* prog = nullptr;
Paul's avatar
Paul committed
36
    context ctx{};
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
39
40
41
42
43
44
    void check_shape(shape x, instruction_ref i)
    {
        assert(x == i->get_shape());
        (void)x;
        (void)i;
    }

Paul's avatar
Paul committed
45
46
    void apply()
    {
Paul's avatar
Paul committed
47
48
        for(auto it = prog->begin(); it != prog->end(); it++)
        {
Paul's avatar
Paul committed
49
            auto s = it->get_shape();
Paul's avatar
Paul committed
50
            if(it->name() == "convolution")
Paul's avatar
Paul committed
51
            {
Paul's avatar
Paul committed
52
                check_shape(s, apply_convolution(it));
Paul's avatar
Paul committed
53
            }
Khalique's avatar
Khalique committed
54
            else if(it->name() == "relu")
Paul's avatar
Paul committed
55
            {
Khalique's avatar
Khalique committed
56
                check_shape(s, apply_relu(it));
Paul's avatar
Paul committed
57
            }
Khalique's avatar
Khalique committed
58
59
60
61
            else if(it->name() == "leaky_relu")
            {
                check_shape(s, apply_leaky_relu(it));
            }
Paul's avatar
Paul committed
62
            else if(it->name() == "pooling")
Paul's avatar
Paul committed
63
            {
Paul's avatar
Paul committed
64
                check_shape(s, apply_pooling(it));
Paul's avatar
Paul committed
65
            }
Paul's avatar
Paul committed
66
            else if(it->name() == "add")
Paul's avatar
Paul committed
67
            {
Paul's avatar
Paul committed
68
                check_shape(s, apply_add(it));
Paul's avatar
Paul committed
69
            }
Khalique's avatar
Khalique committed
70
71
72
73
            else if(it->name() == "mul")
            {
                check_shape(s, apply_mul(it));
            }
Shucai Xiao's avatar
Shucai Xiao committed
74
            else if(it->name() == "dot")
Paul's avatar
Paul committed
75
            {
Paul's avatar
Paul committed
76
                check_shape(s, apply_gemm(it));
Paul's avatar
Paul committed
77
            }
Paul's avatar
Paul committed
78
            else if(it->name() == "contiguous")
79
            {
Paul's avatar
Paul committed
80
                check_shape(s, apply_contiguous(it));
81
            }
82
83
84
85
            else if(it->name() == "concat")
            {
                check_shape(s, apply_concat(it));
            }
Paul's avatar
Paul committed
86
            else if(it->name() == "batch_norm_inference")
87
            {
Paul's avatar
Paul committed
88
                check_shape(s, apply_batch_norm_inference(it));
89
            }
Paul's avatar
Paul committed
90
91
92
93
            else if(it->name() == "softmax")
            {
                check_shape(s, apply_softmax(it));
            }
Paul's avatar
Paul committed
94
95
96
        }
    }

Paul's avatar
Paul committed
97
    instruction_ref insert_allocation(instruction_ref ins, const shape& s, std::string tag = "")
Paul's avatar
Paul committed
98
    {
Paul's avatar
Paul committed
99
        if(ins == --prog->end() and tag.empty())
Paul's avatar
Paul committed
100
101
102
103
104
        {
            return prog->add_parameter("output", s);
        }
        else
        {
Paul's avatar
Paul committed
105
            auto is     = prog->add_outline(s);
Paul's avatar
Paul committed
106
            auto result = prog->insert_instruction(ins, hip_allocate{std::move(tag)}, is);
Paul's avatar
Paul committed
107
108
109
110
            return result;
        }
    }

Paul's avatar
Paul committed
111
    instruction_ref apply_convolution(instruction_ref ins)
Paul's avatar
Paul committed
112
    {
wsttiger's avatar
wsttiger committed
113
        auto&& op = any_cast<op::convolution>(ins->get_operator());
Paul's avatar
Paul committed
114

Paul's avatar
Paul committed
115
        auto conv = miopen_convolution{op, make_conv(op)};
Paul's avatar
Paul committed
116
        auto ws   = conv.compile(ctx, ins->get_shape(), ins->inputs());
Paul's avatar
Paul committed
117

118
        auto workspace = insert_allocation(ins, ws, "workspace");
Paul's avatar
Paul committed
119
        auto output    = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
120

Paul's avatar
Paul committed
121
        return prog->replace_instruction(
Paul's avatar
Paul committed
122
            ins, conv, ins->inputs().at(0), ins->inputs().at(1), workspace, output);
Paul's avatar
Paul committed
123
124
    }

Paul's avatar
Paul committed
125
    instruction_ref apply_pooling(instruction_ref ins)
Paul's avatar
Paul committed
126
    {
wsttiger's avatar
wsttiger committed
127
        auto&& op   = any_cast<op::pooling>(ins->get_operator());
Paul's avatar
Paul committed
128
        auto pd     = make_pooling(op);
Paul's avatar
Paul committed
129
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
130

Paul's avatar
Paul committed
131
        return prog->replace_instruction(
Paul's avatar
Paul committed
132
            ins, miopen_pooling{op, std::move(pd)}, ins->inputs().at(0), output);
Paul's avatar
Paul committed
133
134
    }

Khalique's avatar
Khalique committed
135
    instruction_ref apply_relu(instruction_ref ins)
Paul's avatar
Paul committed
136
    {
Khalique's avatar
Khalique committed
137
        auto ad = make_relu();
Khalique's avatar
Khalique committed
138
139
140
141

        auto output = insert_allocation(ins, ins->get_shape());
        return prog->replace_instruction(
            ins, miopen_relu{std::move(ad)}, ins->inputs().at(0), output);
Paul's avatar
Paul committed
142
    }
143

Khalique's avatar
Khalique committed
144
145
146
147
    instruction_ref apply_leaky_relu(instruction_ref ins)
    {
        auto&& op = any_cast<op::leaky_relu>(ins->get_operator());
        auto ad   = make_leaky_relu(op.alpha);
148

Khalique's avatar
Khalique committed
149
150
151
152
        auto output = insert_allocation(ins, ins->get_shape());
        return prog->replace_instruction(
            ins, miopen_leaky_relu{std::move(ad)}, ins->inputs().at(0), output);
    }
Paul's avatar
Paul committed
153

Paul's avatar
Paul committed
154
155
    instruction_ref apply_softmax(instruction_ref ins)
    {
wsttiger's avatar
wsttiger committed
156
        auto&& op   = any_cast<op::softmax>(ins->get_operator());
Paul's avatar
Paul committed
157
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
158
        return prog->replace_instruction(ins, miopen_softmax{op}, ins->inputs().at(0), output);
Paul's avatar
Paul committed
159
160
    }

Paul's avatar
Paul committed
161
    instruction_ref apply_add(instruction_ref ins)
Paul's avatar
Paul committed
162
    {
Paul's avatar
Paul committed
163
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
164
        return prog->replace_instruction(
Paul's avatar
Paul committed
165
            ins, hip_add{}, ins->inputs().at(0), ins->inputs().at(1), output);
Paul's avatar
Paul committed
166
    }
Paul's avatar
Paul committed
167

Khalique's avatar
Khalique committed
168
169
170
171
172
173
174
    instruction_ref apply_mul(instruction_ref ins)
    {
        auto output = insert_allocation(ins, ins->get_shape());
        return prog->replace_instruction(
            ins, hip_mul{}, ins->inputs().at(0), ins->inputs().at(1), output);
    }

Paul's avatar
Paul committed
175
    instruction_ref apply_gemm(instruction_ref ins)
Paul's avatar
Paul committed
176
    {
Shucai Xiao's avatar
Shucai Xiao committed
177
        auto&& op   = any_cast<op::dot>(ins->get_operator());
Paul's avatar
Paul committed
178
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
179
        return prog->replace_instruction(
Paul's avatar
Paul committed
180
            ins, miopen_gemm{op}, ins->inputs().at(0), ins->inputs().at(1), output);
Paul's avatar
Paul committed
181
    }
182

Paul's avatar
Paul committed
183
    instruction_ref apply_contiguous(instruction_ref ins)
184
    {
wsttiger's avatar
wsttiger committed
185
        auto&& op   = any_cast<op::contiguous>(ins->get_operator());
Paul's avatar
Paul committed
186
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
187
        return prog->replace_instruction(ins, miopen_contiguous{op}, ins->inputs().at(0), output);
188
    }
189

190
191
    instruction_ref apply_concat(instruction_ref ins)
    {
wsttiger's avatar
wsttiger committed
192
193
        auto&& op                         = any_cast<op::concat>(ins->get_operator());
        auto output                       = insert_allocation(ins, ins->get_shape());
194
195
196
197
198
        std::vector<instruction_ref> refs = ins->inputs();
        refs.push_back(output);
        return prog->replace_instruction(ins, hip_concat{op}, refs);
    }

Paul's avatar
Paul committed
199
    instruction_ref apply_batch_norm_inference(instruction_ref ins)
200
    {
wsttiger's avatar
wsttiger committed
201
        auto&& op       = any_cast<op::batch_norm_inference>(ins->get_operator());
Paul's avatar
Paul committed
202
        auto output     = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
203
        shape old_shape = ins->inputs().at(1)->get_shape();
wsttiger's avatar
wsttiger committed
204
        std::vector<int64_t> new_shape{1, static_cast<int64_t>(old_shape.elements()), 1, 1};
wsttiger's avatar
wsttiger committed
205
        auto reshape_op = op::reshape{new_shape};
Paul's avatar
Paul committed
206
        std::vector<instruction_ref> reshapes;
Paul's avatar
Paul committed
207
208
        std::transform(ins->inputs().begin() + 1,
                       ins->inputs().end(),
Paul's avatar
Paul committed
209
210
                       std::back_inserter(reshapes),
                       [&](auto i) { return prog->insert_instruction(ins, reshape_op, i); });
Paul's avatar
Paul committed
211
        return prog->replace_instruction(ins,
Paul's avatar
Paul committed
212
                                         miopen_batch_norm_inference{op},
Paul's avatar
Paul committed
213
                                         ins->inputs().at(0),
Paul's avatar
Paul committed
214
215
216
217
218
                                         reshapes[0],
                                         reshapes[1],
                                         reshapes[2],
                                         reshapes[3],
                                         output);
219
    }
Paul's avatar
Paul committed
220
221
};

Paul's avatar
Paul committed
222
void lowering::apply(program& p) const { miopen_apply{&p, ctx}.apply(); }
Paul's avatar
Paul committed
223
} // namespace gpu
224
} // namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
225
} // namespace migraph