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

Paul's avatar
Paul committed
9
10
namespace rtg {
namespace miopen {
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
13
14
struct miopen_convolution
{
    convolution op;
Paul's avatar
Paul committed
15
    shared<convolution_descriptor> cd;
Paul's avatar
Paul committed
16
17

    std::string name() const { return "miopen::convolution"; }
Paul's avatar
Paul committed
18
19
    shape compute_shape(std::vector<shape> inputs) const
    {
Paul's avatar
Paul committed
20
        check_shapes{inputs}.has(4);
Paul's avatar
Paul committed
21
        return op.compute_shape({inputs.at(1), inputs.at(2)});
Paul's avatar
Paul committed
22
23
24
25
26
27
28
    }
    argument compute(shape output_shape, std::vector<argument> args) const
    {
        auto x_desc = make_tensor(args[1].get_shape());
        auto w_desc = make_tensor(args[2].get_shape());
        auto y_desc = make_tensor(output_shape);

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

Paul's avatar
Paul committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
struct miopen_pooling
{
    pooling op;
    shared<pooling_descriptor> pd;

    std::string name() const { return "miopen::pooling"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs}.has(3);
        return op.compute_shape({inputs.at(1)});
    }
    argument compute(shape output_shape, std::vector<argument> args) const
    {
        auto x_desc = make_tensor(args[1].get_shape());
        auto y_desc = make_tensor(output_shape);

        float alpha = 1, beta = 0;

        miopenPoolingForward(args[0].implicit(),
Paul's avatar
Paul committed
82
83
84
85
86
87
88
89
90
91
                             pd.get(),
                             &alpha,
                             x_desc.get(),
                             args[1].implicit(),
                             &beta,
                             y_desc.get(),
                             args[2].implicit(),
                             false,
                             nullptr,
                             0);
Paul's avatar
Paul committed
92
93
94
95
96

        return args[2];
    }
};

Paul's avatar
Paul committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
struct miopen_add
{
    std::string name() const { return "miopen::add"; }
    shape compute_shape(std::vector<shape> inputs) const
    {
        check_shapes{inputs}.has(4);
        return inputs.at(1);
    }

    argument compute(shape output_shape, std::vector<argument> args) const
    {
        if(args[2].get_shape().broadcasted()) {
            argument result{output_shape};

            visit_all(result, from_gpu(args[1]), from_gpu(args[2]))([&](auto output, auto input1, auto input2) {
                shape_for_each(output.get_shape(), [&](const auto& idx) {
                        output(idx.begin(), idx.end()) =
                            input1(idx.begin(), idx.end()) + input2(idx.begin(), idx.end());
                    });
            });
            return to_gpu(result);
        } else {
        float alpha = 1, beta = 0;
        auto a_desc = make_tensor(args[1].get_shape());
        auto b_desc = make_tensor(args[2].get_shape());
        auto c_desc = make_tensor(output_shape);
        miopenOpTensor(args[0].implicit(),
                                            miopenTensorOpAdd,
                                            &alpha,
                                            a_desc.get(),
                                            args[1].implicit(),
                                            &alpha,
                                            b_desc.get(),
                                            args[2].implicit(),
                                            &beta,
                                            c_desc.get(),
                                            args[3].implicit());
        return args[3];
        }
    }
};

Paul's avatar
Paul committed
139
140
141
142
struct miopen_relu
{
    shared<activation_descriptor> ad;
    std::string name() const { return "miopen::relu"; }
Paul's avatar
Paul committed
143
    shape compute_shape(std::vector<shape> inputs) const
Paul's avatar
Paul committed
144
    {
Paul's avatar
Paul committed
145
146
        check_shapes{inputs}.has(3);
        return inputs.at(1);
Paul's avatar
Paul committed
147
148
149
150
151
152
153
    }

    argument compute(shape output_shape, std::vector<argument> args) const
    {
        float alpha = 1, beta = 0;
        auto x_desc = make_tensor(args[1].get_shape());
        auto y_desc = make_tensor(output_shape);
154
        miopenActivationForward(args[0].implicit(),
Paul's avatar
Paul committed
155
156
157
                                ad.get(),
                                &alpha,
                                x_desc.get(),
158
                                args[1].implicit(),
Paul's avatar
Paul committed
159
160
                                &beta,
                                y_desc.get(),
161
                                args[2].implicit());
Paul's avatar
Paul committed
162
163

        return args[2];
Paul's avatar
Paul committed
164
165
166
    }
};

Paul's avatar
Paul committed
167
168
struct miopen_apply
{
Paul's avatar
Paul committed
169
170
    program* prog = nullptr;
    instruction_ref handle{};
Paul's avatar
Paul committed
171
172
173

    void apply()
    {
Paul's avatar
Paul committed
174
        handle = prog->add_parameter("handle", shape{shape::any_type});
Paul's avatar
Paul committed
175
176
177
178
        for(auto it = prog->begin(); it != prog->end(); it++)
        {
            if(it->op.name() == "convolution")
            {
Paul's avatar
Paul committed
179
                apply_convolution(it);
Paul's avatar
Paul committed
180
181
182
            }
            else if(it->op.name() == "activation")
            {
Paul's avatar
Paul committed
183
184
                apply_activation(it);
            }
Paul's avatar
Paul committed
185
186
187
188
            else if(it->op.name() == "pooling")
            {
                apply_pooling(it);
            }
Paul's avatar
Paul committed
189
190
191
192
            else if(it->op.name() == "add")
            {
                apply_add(it);
            }
Paul's avatar
Paul committed
193
194
195
        }
    }

Paul's avatar
Paul committed
196
197
    instruction_ref insert_allocation(instruction_ref ins, const shape& s)
    {
Paul's avatar
Paul committed
198
        if(ins == --prog->end())
Paul's avatar
Paul committed
199
200
201
202
203
        {
            return prog->add_parameter("output", s);
        }
        else
        {
Paul's avatar
Paul committed
204
            auto is     = prog->add_outline(s);
Paul's avatar
Paul committed
205
206
207
208
209
            auto result = prog->insert_instruction(ins, hip_allocate{}, is);
            return result;
        }
    }

Paul's avatar
Paul committed
210
211
    void apply_convolution(instruction_ref ins)
    {
Paul's avatar
Paul committed
212
213
        auto&& op   = any_cast<convolution>(ins->op);
        auto cd     = make_conv(op);
Paul's avatar
Paul committed
214
215
        auto output = insert_allocation(ins, ins->result);

Paul's avatar
Paul committed
216
217
218
219
220
221
        prog->replace_instruction(ins,
                                  miopen_convolution{op, std::move(cd)},
                                  handle,
                                  ins->arguments.at(0),
                                  ins->arguments.at(1),
                                  output);
Paul's avatar
Paul committed
222
223
    }

Paul's avatar
Paul committed
224
225
226
227
228
229
    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
230
231
        prog->replace_instruction(
            ins, miopen_pooling{op, std::move(pd)}, handle, ins->arguments.at(0), output);
Paul's avatar
Paul committed
232
233
    }

Paul's avatar
Paul committed
234
    void apply_activation(instruction_ref ins)
Paul's avatar
Paul committed
235
236
    {
        auto&& op = any_cast<activation>(ins->op);
Paul's avatar
Paul committed
237
238
        auto ad   = make_relu();
        if(op.mode == "relu")
Paul's avatar
Paul committed
239
240
        {
            auto output = insert_allocation(ins, ins->result);
Paul's avatar
Paul committed
241
242
            prog->replace_instruction(
                ins, miopen_relu{std::move(ad)}, handle, ins->arguments.at(0), output);
Paul's avatar
Paul committed
243
244
        }
    }
Paul's avatar
Paul committed
245
246
247
248
249
250
251

    void apply_add(instruction_ref ins)
    {
        auto output = insert_allocation(ins, ins->result);
        prog->replace_instruction(
            ins, miopen_add{}, handle, ins->arguments.at(0), ins->arguments.at(1), output);
    }
Paul's avatar
Paul committed
252
253
};

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

Paul's avatar
Paul committed
256
void miopen_target::apply(program& p) const { miopen_apply{&p}.apply(); }
Paul's avatar
Paul committed
257
258
259
260

} // namespace miopen

} // namespace rtg