lowering.cpp 11.7 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
6
#include <migraph/manage_ptr.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/shape_for_each.hpp>
Paul's avatar
Paul committed
7
8
#include <migraph/gpu/miopen.hpp>
#include <migraph/gpu/hip.hpp>
Paul's avatar
Paul committed
9
#include <migraph/dfor.hpp>
Paul's avatar
Paul committed
10
#include <migraph/gpu/kernels.hpp>
Paul's avatar
Paul committed
11
#include <migraph/iterator_for.hpp>
Paul's avatar
Paul committed
12
13
#include <migraph/gpu/rocblas.hpp>
#include <migraph/gpu/context.hpp>
Paul's avatar
Paul committed
14
15

namespace migraph {
Paul's avatar
Paul committed
16
namespace gpu {
Paul's avatar
Paul committed
17

Paul's avatar
Paul committed
18
19
20
struct miopen_convolution
{
    convolution op;
Paul's avatar
Paul committed
21
    shared<convolution_descriptor> cd;
Paul's avatar
Paul committed
22

Paul's avatar
Paul committed
23
    std::string name() const { return "gpu::convolution"; }
Paul's avatar
Paul committed
24
25
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
26
27
        check_shapes{inputs, *this}.has(3);
        return op.compute_shape({inputs.at(0), inputs.at(1)});
Paul's avatar
Paul committed
28
    }
Paul's avatar
Paul committed
29
    argument compute(context& ctx, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
30
    {
Paul's avatar
Paul committed
31
32
        auto x_desc = make_tensor(args[0].get_shape());
        auto w_desc = make_tensor(args[1].get_shape());
Paul's avatar
Paul committed
33
34
        auto y_desc = make_tensor(output_shape);

Paul's avatar
Paul committed
35
        float alpha = 1, beta = 0;
Paul's avatar
Paul committed
36
37
        int algo_count;
        miopenConvAlgoPerf_t perf;
Paul's avatar
Paul committed
38
        miopenFindConvolutionForwardAlgorithm(ctx.handle.get(),
Paul's avatar
Paul committed
39
                                              x_desc.get(),
Paul's avatar
Paul committed
40
                                              args[0].implicit(),
Paul's avatar
Paul committed
41
                                              w_desc.get(),
Paul's avatar
Paul committed
42
                                              args[1].implicit(),
Paul's avatar
Paul committed
43
                                              cd.get(),
Paul's avatar
Paul committed
44
                                              y_desc.get(),
Paul's avatar
Paul committed
45
                                              args[2].implicit(),
Paul's avatar
Paul committed
46
47
48
                                              1,
                                              &algo_count,
                                              &perf,
Paul's avatar
Paul committed
49
50
                                              nullptr,
                                              0,
Paul's avatar
Paul committed
51
                                              false);
Paul's avatar
Paul committed
52
        miopenConvolutionForward(ctx.handle.get(),
Paul's avatar
Paul committed
53
                                 &alpha,
Paul's avatar
Paul committed
54
                                 x_desc.get(),
Paul's avatar
Paul committed
55
                                 args[0].implicit(),
Paul's avatar
Paul committed
56
                                 w_desc.get(),
Paul's avatar
Paul committed
57
                                 args[1].implicit(),
Paul's avatar
Paul committed
58
59
60
                                 cd.get(),
                                 perf.fwd_algo,
                                 &beta,
Paul's avatar
Paul committed
61
                                 y_desc.get(),
Paul's avatar
Paul committed
62
                                 args[2].implicit(),
Paul's avatar
Paul committed
63
64
                                 nullptr,
                                 0);
Paul's avatar
Paul committed
65
        return args[2];
Paul's avatar
Paul committed
66
67
68
    }
};

Paul's avatar
Paul committed
69
70
71
72
73
struct miopen_pooling
{
    pooling op;
    shared<pooling_descriptor> pd;

Paul's avatar
Paul committed
74
    std::string name() const { return "gpu::pooling"; }
Paul's avatar
Paul committed
75
76
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
77
        check_shapes{inputs, *this}.has(2);
Paul's avatar
Paul committed
78
79
        return op.compute_shape({inputs.at(1)});
    }
Paul's avatar
Paul committed
80
    argument compute(context& ctx, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
81
    {
Paul's avatar
Paul committed
82
        auto x_desc = make_tensor(args[0].get_shape());
Paul's avatar
Paul committed
83
84
85
86
        auto y_desc = make_tensor(output_shape);

        float alpha = 1, beta = 0;

Paul's avatar
Paul committed
87
        miopenPoolingForward(ctx.handle.get(),
Paul's avatar
Paul committed
88
89
90
                             pd.get(),
                             &alpha,
                             x_desc.get(),
Paul's avatar
Paul committed
91
                             args[0].implicit(),
Paul's avatar
Paul committed
92
93
                             &beta,
                             y_desc.get(),
Paul's avatar
Paul committed
94
                             args[1].implicit(),
Paul's avatar
Paul committed
95
96
97
                             false,
                             nullptr,
                             0);
Paul's avatar
Paul committed
98

Paul's avatar
Paul committed
99
        return args[1];
Paul's avatar
Paul committed
100
101
102
    }
};

Paul's avatar
Paul committed
103
104
struct miopen_add
{
Paul's avatar
Paul committed
105
    std::string name() const { return "gpu::add"; }
Paul's avatar
Paul committed
106
107
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
108
109
        check_shapes{inputs, *this}.has(3);
        return inputs.at(0);
Paul's avatar
Paul committed
110
111
    }

Paul's avatar
Paul committed
112
    argument compute(context& ctx, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
113
    {
Paul's avatar
Paul committed
114
        if(args[1].get_shape().broadcasted())
Paul's avatar
Paul committed
115
        {
Paul's avatar
Paul committed
116
117
            argument result{output_shape};

Paul's avatar
Paul committed
118
            visit_all(result, from_gpu(args[0]), from_gpu(args[1]))(
Paul's avatar
Paul committed
119
120
                [&](auto output, auto input1, auto input2) {
                    shape_for_each(output.get_shape(), [&](const auto& idx) {
Paul's avatar
Paul committed
121
122
123
                        output(idx.begin(), idx.end()) =
                            input1(idx.begin(), idx.end()) + input2(idx.begin(), idx.end());
                    });
Paul's avatar
Paul committed
124
                });
Paul's avatar
Paul committed
125
            return to_gpu(result);
Paul's avatar
Paul committed
126
127
128
129
        }
        else
        {
            float alpha = 1, beta = 0;
Paul's avatar
Paul committed
130
131
            auto a_desc = make_tensor(args[0].get_shape());
            auto b_desc = make_tensor(args[1].get_shape());
Paul's avatar
Paul committed
132
            auto c_desc = make_tensor(output_shape);
Paul's avatar
Paul committed
133
            miopenOpTensor(ctx.handle.get(),
Paul's avatar
Paul committed
134
135
136
                           miopenTensorOpAdd,
                           &alpha,
                           a_desc.get(),
Paul's avatar
Paul committed
137
                           args[0].implicit(),
Paul's avatar
Paul committed
138
139
                           &alpha,
                           b_desc.get(),
Paul's avatar
Paul committed
140
                           args[1].implicit(),
Paul's avatar
Paul committed
141
142
                           &beta,
                           c_desc.get(),
Paul's avatar
Paul committed
143
144
                           args[2].implicit());
            return args[2];
Paul's avatar
Paul committed
145
146
147
148
        }
    }
};

Paul's avatar
Paul committed
149
150
151
struct miopen_gemm
{
    gemm op;
Paul's avatar
Paul committed
152
    std::string name() const { return "gpu::convolution"; }
Paul's avatar
Paul committed
153
154
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
155
156
        check_shapes{inputs, *this}.has(3);
        return op.compute_shape({inputs.at(0), inputs.at(1)});
Paul's avatar
Paul committed
157
    }
Paul's avatar
Paul committed
158
    argument compute(context& ctx, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
159
    {
160
161
162
163
164
165
166
167
        float alpha     = 1.0f;
        float beta      = 0.0f;
        rocblas_int lda = args[0].get_shape().lens()[1];
        rocblas_int ldb = args[1].get_shape().lens()[1];
        rocblas_int ldc = args[2].get_shape().lens()[1];
        rocblas_int m   = output_shape.lens()[0];
        rocblas_int n   = output_shape.lens()[1];
        rocblas_int k   = args[0].get_shape().lens()[1];
168
        rocblas_sgemm(ctx.rbhandle.get(),
169
170
171
172
173
174
175
176
177
178
179
180
181
182
                      rocblas_operation_none,
                      rocblas_operation_none,
                      n,
                      m,
                      k,
                      &alpha,
                      args[1].implicit(),
                      ldb,
                      args[0].implicit(),
                      lda,
                      &beta,
                      args[2].implicit(),
                      ldc);
        return args[2];
Paul's avatar
Paul committed
183
184
185
    }
};

186
187
188
189
struct miopen_transpose
{
    transpose op;

Paul's avatar
Paul committed
190
    std::string name() const { return "gpu::transpose"; }
191
192
193
194
195
196
197
198
199
200
201
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(2);
        return op.compute_shape({inputs.at(0)});
    }
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
        return {output_shape, std::move(args.front().data)};
    }
};

202
203
204
struct miopen_contiguous
{
    contiguous op;
Paul's avatar
Paul committed
205
    std::string name() const { return "gpu::contiguous"; }
206
207
208
209
210
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(2);
        return op.compute_shape({inputs.at(0)});
    }
211
212
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
213
214
        hip_contiguous(output_shape, args.at(0), args.at(1));
        return args.at(1);
215
216
217
    }
};

Paul's avatar
Paul committed
218
219
220
struct miopen_relu
{
    shared<activation_descriptor> ad;
Paul's avatar
Paul committed
221
    std::string name() const { return "gpu::relu"; }
Paul's avatar
Paul committed
222
    shape compute_shape(std::vector<shape> inputs) const
Paul's avatar
Paul committed
223
    {
Paul's avatar
Paul committed
224
        check_shapes{inputs, *this}.has(2);
Paul's avatar
Paul committed
225
        return inputs.at(1);
Paul's avatar
Paul committed
226
227
    }

Paul's avatar
Paul committed
228
    argument compute(context& ctx, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
229
230
    {
        float alpha = 1, beta = 0;
Paul's avatar
Paul committed
231
        auto x_desc = make_tensor(args[0].get_shape());
Paul's avatar
Paul committed
232
        auto y_desc = make_tensor(output_shape);
Paul's avatar
Paul committed
233
        miopenActivationForward(ctx.handle.get(),
Paul's avatar
Paul committed
234
235
236
                                ad.get(),
                                &alpha,
                                x_desc.get(),
Paul's avatar
Paul committed
237
                                args[0].implicit(),
Paul's avatar
Paul committed
238
239
                                &beta,
                                y_desc.get(),
Paul's avatar
Paul committed
240
                                args[1].implicit());
Paul's avatar
Paul committed
241

Paul's avatar
Paul committed
242
        return args[1];
Paul's avatar
Paul committed
243
244
245
    }
};

Paul's avatar
Paul committed
246
247
struct miopen_apply
{
Paul's avatar
Paul committed
248
    program* prog = nullptr;
Paul's avatar
Paul committed
249
250
251

    void apply()
    {
Paul's avatar
Paul committed
252
        prog->insert_instruction(prog->begin(), check_context<context>{});
Paul's avatar
Paul committed
253
254
255
256
        for(auto it = prog->begin(); it != prog->end(); it++)
        {
            if(it->op.name() == "convolution")
            {
Paul's avatar
Paul committed
257
                apply_convolution(it);
Paul's avatar
Paul committed
258
259
260
            }
            else if(it->op.name() == "activation")
            {
Paul's avatar
Paul committed
261
262
                apply_activation(it);
            }
Paul's avatar
Paul committed
263
264
265
266
            else if(it->op.name() == "pooling")
            {
                apply_pooling(it);
            }
Paul's avatar
Paul committed
267
268
269
270
            else if(it->op.name() == "add")
            {
                apply_add(it);
            }
Paul's avatar
Paul committed
271
272
273
274
            else if(it->op.name() == "gemm")
            {
                apply_gemm(it);
            }
275
276
277
278
            else if(it->op.name() == "transpose")
            {
                apply_transpose(it);
            }
279
280
281
282
            else if(it->op.name() == "contiguous")
            {
                apply_contiguous(it);
            }
Paul's avatar
Paul committed
283
284
285
        }
    }

Paul's avatar
Paul committed
286
287
    instruction_ref insert_allocation(instruction_ref ins, const shape& s)
    {
Paul's avatar
Paul committed
288
        if(ins == --prog->end())
Paul's avatar
Paul committed
289
290
291
292
293
        {
            return prog->add_parameter("output", s);
        }
        else
        {
Paul's avatar
Paul committed
294
            auto is     = prog->add_outline(s);
Paul's avatar
Paul committed
295
296
297
298
299
            auto result = prog->insert_instruction(ins, hip_allocate{}, is);
            return result;
        }
    }

Paul's avatar
Paul committed
300
301
    void apply_convolution(instruction_ref ins)
    {
Paul's avatar
Paul committed
302
303
        auto&& op   = any_cast<convolution>(ins->op);
        auto cd     = make_conv(op);
Paul's avatar
Paul committed
304
305
        auto output = insert_allocation(ins, ins->result);

Paul's avatar
Paul committed
306
307
308
309
310
        prog->replace_instruction(ins,
                                  miopen_convolution{op, std::move(cd)},
                                  ins->arguments.at(0),
                                  ins->arguments.at(1),
                                  output);
Paul's avatar
Paul committed
311
312
    }

Paul's avatar
Paul committed
313
314
315
316
317
318
    void apply_pooling(instruction_ref ins)
    {
        auto&& op   = any_cast<pooling>(ins->op);
        auto pd     = make_pooling(op);
        auto output = insert_allocation(ins, ins->result);

Paul's avatar
Paul committed
319
        prog->replace_instruction(
Paul's avatar
Paul committed
320
            ins, miopen_pooling{op, std::move(pd)}, ins->arguments.at(0), output);
Paul's avatar
Paul committed
321
322
    }

Paul's avatar
Paul committed
323
    void apply_activation(instruction_ref ins)
Paul's avatar
Paul committed
324
325
    {
        auto&& op = any_cast<activation>(ins->op);
Paul's avatar
Paul committed
326
327
        auto ad   = make_relu();
        if(op.mode == "relu")
Paul's avatar
Paul committed
328
329
        {
            auto output = insert_allocation(ins, ins->result);
Paul's avatar
Paul committed
330
            prog->replace_instruction(
Paul's avatar
Paul committed
331
                ins, miopen_relu{std::move(ad)}, ins->arguments.at(0), output);
Paul's avatar
Paul committed
332
333
        }
    }
Paul's avatar
Paul committed
334
335
336
337
338

    void apply_add(instruction_ref ins)
    {
        auto output = insert_allocation(ins, ins->result);
        prog->replace_instruction(
Paul's avatar
Paul committed
339
            ins, miopen_add{}, ins->arguments.at(0), ins->arguments.at(1), output);
Paul's avatar
Paul committed
340
    }
Paul's avatar
Paul committed
341
342
343

    void apply_gemm(instruction_ref ins)
    {
Paul's avatar
Paul committed
344
        auto&& op   = any_cast<gemm>(ins->op);
Paul's avatar
Paul committed
345
346
        auto output = insert_allocation(ins, ins->result);
        prog->replace_instruction(
Paul's avatar
Paul committed
347
            ins, miopen_gemm{op}, ins->arguments.at(0), ins->arguments.at(1), output);
Paul's avatar
Paul committed
348
    }
349

350
351
352
353
354
355
356
    void apply_transpose(instruction_ref ins)
    {
        auto&& op   = any_cast<transpose>(ins->op);
        auto output = insert_allocation(ins, ins->result);
        prog->replace_instruction(ins, miopen_transpose{op}, ins->arguments.at(0), output);
    }

357
358
359
360
361
362
    void apply_contiguous(instruction_ref ins)
    {
        auto&& op   = any_cast<contiguous>(ins->op);
        auto output = insert_allocation(ins, ins->result);
        prog->replace_instruction(ins, miopen_contiguous{op}, ins->arguments.at(0), output);
    }
Paul's avatar
Paul committed
363
364
};

Paul's avatar
Paul committed
365
void lowering::apply(program& p) const { miopen_apply{&p}.apply(); }
Paul's avatar
Paul committed
366

Paul's avatar
Paul committed
367
} // namespace gpu
Paul's avatar
Paul committed
368

Paul's avatar
Paul committed
369
} // namespace migraph