miopen_target.cpp 12 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
#include <migraph/miopen/miopen_target.hpp>
#include <migraph/manage_ptr.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/shape_for_each.hpp>
#include <migraph/miopen/miopen.hpp>
#include <migraph/miopen/hip.hpp>
#include <migraph/dfor.hpp>

namespace migraph {
Paul's avatar
Paul committed
11
namespace miopen {
Paul's avatar
Paul committed
12

Paul's avatar
Paul committed
13
14
15
16
17
struct miopen_context
{
    shared<miopen_handle> handle;
};

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
23

    std::string name() const { return "miopen::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& gctx, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
30
    {
Paul's avatar
Paul committed
31
        auto& ctx   = any_cast<miopen_context>(gctx);
Paul's avatar
Paul committed
32
33
        auto x_desc = make_tensor(args[0].get_shape());
        auto w_desc = make_tensor(args[1].get_shape());
Paul's avatar
Paul committed
34
35
        auto y_desc = make_tensor(output_shape);

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

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

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

        float alpha = 1, beta = 0;

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

Paul's avatar
Paul committed
101
        return args[1];
Paul's avatar
Paul committed
102
103
104
    }
};

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

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

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

Paul's avatar
Paul committed
152
153
154
155
156
157
struct miopen_gemm
{
    gemm op;
    std::string name() const { return "miopen::convolution"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
158
159
        check_shapes{inputs, *this}.has(3);
        return op.compute_shape({inputs.at(0), inputs.at(1)});
Paul's avatar
Paul committed
160
    }
Paul's avatar
Paul committed
161
    argument compute(context&, shape output_shape, std::vector<argument> args) const
Paul's avatar
Paul committed
162
163
164
    {
        argument result{output_shape};

Paul's avatar
Paul committed
165
        visit_all(result, from_gpu(args[0]), from_gpu(args[1]))(
Paul's avatar
Paul committed
166
            [&](auto output, auto input1, auto input2) {
Paul's avatar
Paul committed
167
168
169
170
                dfor(input1.get_shape().lens()[0],
                     input2.get_shape().lens()[1],
                     input2.get_shape().lens()[0])(
                    [&](auto i, auto j, auto k) { output(i, j) += input1(i, k) * input2(k, j); });
Paul's avatar
Paul committed
171
172
173
174
175
            });
        return to_gpu(result);
    }
};

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
struct miopen_transpose
{
    transpose op;

    std::string name() const { return "miopen::transpose"; }
    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)};
    }
};

192
193
194
195
struct miopen_contiguous
{
    contiguous op;
    std::string name() const { return "miopen::contiguous"; }
196
197
198
199
200
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs, *this}.has(2);
        return op.compute_shape({inputs.at(0)});
    }
201
202
203
204
205
206
207
208
209
210
211
212
    argument compute(context&, shape output_shape, std::vector<argument> args) const
    {
        argument result{output_shape};
        visit_all(result, from_gpu(args[0]))([&](auto output, auto input) {
            shape_for_each(output.get_shape(), [&](const auto& idx) {
                output(idx.begin(), idx.end()) = input(idx.begin(), idx.end());
            });
        });
        return to_gpu(result);
    }
};

Paul's avatar
Paul committed
213
214
215
216
struct miopen_relu
{
    shared<activation_descriptor> ad;
    std::string name() const { return "miopen::relu"; }
Paul's avatar
Paul committed
217
    shape compute_shape(std::vector<shape> inputs) const
Paul's avatar
Paul committed
218
    {
Paul's avatar
Paul committed
219
        check_shapes{inputs, *this}.has(2);
Paul's avatar
Paul committed
220
        return inputs.at(1);
Paul's avatar
Paul committed
221
222
    }

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

Paul's avatar
Paul committed
238
        return args[1];
Paul's avatar
Paul committed
239
240
241
    }
};

Paul's avatar
Paul committed
242
243
struct miopen_apply
{
Paul's avatar
Paul committed
244
    program* prog = nullptr;
Paul's avatar
Paul committed
245
246
247

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

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

Paul's avatar
Paul committed
296
297
    void apply_convolution(instruction_ref ins)
    {
Paul's avatar
Paul committed
298
299
        auto&& op   = any_cast<convolution>(ins->op);
        auto cd     = make_conv(op);
Paul's avatar
Paul committed
300
301
        auto output = insert_allocation(ins, ins->result);

Paul's avatar
Paul committed
302
303
304
305
306
        prog->replace_instruction(ins,
                                  miopen_convolution{op, std::move(cd)},
                                  ins->arguments.at(0),
                                  ins->arguments.at(1),
                                  output);
Paul's avatar
Paul committed
307
308
    }

Paul's avatar
Paul committed
309
310
311
312
313
314
    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
315
        prog->replace_instruction(
Paul's avatar
Paul committed
316
            ins, miopen_pooling{op, std::move(pd)}, ins->arguments.at(0), output);
Paul's avatar
Paul committed
317
318
    }

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

    void apply_add(instruction_ref ins)
    {
        auto output = insert_allocation(ins, ins->result);
        prog->replace_instruction(
Paul's avatar
Paul committed
335
            ins, miopen_add{}, ins->arguments.at(0), ins->arguments.at(1), output);
Paul's avatar
Paul committed
336
    }
Paul's avatar
Paul committed
337
338
339

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

346
347
348
349
350
351
352
    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);
    }

353
354
355
356
357
358
    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
359
360
};

Paul's avatar
Paul committed
361
362
363
364
365
366
367
368
struct miopen_pass
{
    std::string name() const { return "miopen::pass"; }

    void apply(program& p) const { miopen_apply{&p}.apply(); }
};

std::vector<pass> miopen_target::get_passes(context&) const { return {miopen_pass{}}; }
Paul's avatar
Paul committed
369

Paul's avatar
Paul committed
370
std::string miopen_target::name() const { return "miopen"; }
Paul's avatar
Paul committed
371

Paul's avatar
Paul committed
372
373
374
375
376
context miopen_target::get_context() const
{
    return miopen_context{share(make_obj<miopen_handle>(&miopenCreate))};
}

Paul's avatar
Paul committed
377
378
} // namespace miopen

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