fuse_ops.cpp 6.37 KB
Newer Older
Paul's avatar
Paul committed
1
#include <migraph/gpu/fuse_ops.hpp>
Paul's avatar
Paul committed
2
#include <migraph/matcher.hpp>
Paul's avatar
Paul committed
3
4
#include <migraph/gpu/miopen.hpp>
#include <migraph/gpu/convolution.hpp>
Paul's avatar
Paul committed
5
6
7
8
9
10
11
#include <migraph/gpu/device/add_relu.hpp>
#include <migraph/instruction.hpp>

namespace migraph {

namespace gpu {

Paul's avatar
Paul committed
12
13
14
15
16
17
18
19
struct fusion
{
    using op_t = miopenFusionOpDescriptor_t;
    shared<fusion_plan_descriptor> fp;

    // Used as a temporary hack to keep descriptor references alive
    std::vector<std::shared_ptr<void>> storage;

Paul's avatar
Paul committed
20
    template <class T>
Paul's avatar
Paul committed
21
22
23
24
25
26
27
28
29
30
31
    auto keep_alive(T x)
    {
        auto result = share(std::move(x));
        storage.push_back(result);
        return result;
    }

    fusion(const shape& input)
    // : fp(make_fusion_plan(input))
    {
        auto t = make_tensor(input);
Paul's avatar
Paul committed
32
        fp     = make_fusion_plan(t);
Paul's avatar
Paul committed
33
34
35
36
37
38
39
40
41
42
43
44
        keep_alive(std::move(t));
    }

    op_t operator[](std::size_t i) const
    {
        op_t result;
        auto status = miopenFusionPlanGetOp(fp.get(), i, &result);
        if(status != miopenStatusSuccess)
            MIGRAPH_THROW("Failed retrieving operator at " + std::to_string(i));
        return result;
    }

Paul's avatar
Paul committed
45
    auto get() const { return fp.get(); }
Paul's avatar
Paul committed
46
47
48
49

    op_t create_bias(const shape& bias)
    {
        op_t result;
Paul's avatar
Paul committed
50
51
        auto b      = shape{bias.type(), {1, bias.lens().at(1), 1, 1}};
        auto t      = keep_alive(make_tensor(b));
Paul's avatar
Paul committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        auto status = miopenCreateOpBiasForward(fp.get(), &result, t.get());
        if(status != miopenStatusSuccess)
            MIGRAPH_THROW("Creating operator failed");
        return result;
    }

    op_t create_relu()
    {
        op_t result;
        auto status = miopenCreateOpActivationForward(fp.get(), &result, miopenActivationRELU);
        if(status != miopenStatusSuccess)
            MIGRAPH_THROW("Creating operator failed");
        return result;
    }

    op_t create_conv(const op::convolution& op, const shape& weights)
    {
        op_t result;
Paul's avatar
Paul committed
70
71
        auto cd     = keep_alive(make_conv(op));
        auto t      = keep_alive(make_tensor(weights));
Paul's avatar
Paul committed
72
73
74
75
76
77
78
        auto status = miopenCreateOpConvForward(fp.get(), &result, cd.get(), t.get());
        if(status != miopenStatusSuccess)
            MIGRAPH_THROW("Creating operator failed");
        return result;
    }
};

Paul's avatar
Paul committed
79
80
81
82
83
struct hip_add_relu
{
    std::string name() const { return "hip::add_relu"; }
    shape compute_shape(const std::vector<shape>& inputs) const
    {
Paul's avatar
Paul committed
84
        check_shapes{inputs, *this}.has(3);
Paul's avatar
Paul committed
85
86
        return inputs.front();
    }
Paul's avatar
Paul committed
87
    argument compute(context&, const shape&, const std::vector<argument>& args) const
Paul's avatar
Paul committed
88
    {
89
        device::add_relu(args.at(2), args.at(0), args.at(1));
Paul's avatar
Paul committed
90
91
92
93
        return args.at(2);
    }
};

Paul's avatar
Paul committed
94
struct match_add_relu
Paul's avatar
Paul committed
95
{
Paul's avatar
Paul committed
96
97
    auto matcher() const
    {
Paul's avatar
Paul committed
98
        return match::name("gpu::relu")(match::arg(0)(match::name("gpu::add").bind("add")));
Paul's avatar
Paul committed
99
    }
Paul's avatar
Paul committed
100

Paul's avatar
Paul committed
101
102
    void apply(program& p, match::matcher_result r) const
    {
Paul's avatar
Paul committed
103
        auto add_ins = r.instructions["add"];
Paul's avatar
Paul committed
104
105
        auto ins     = r.result;
        auto args    = add_ins->inputs();
Paul's avatar
Paul committed
106
        // Use the allocation from the relu operator
Paul's avatar
Paul committed
107
        args.back() = ins->inputs().back();
Paul's avatar
Paul committed
108
        p.replace_instruction(ins, hip_add_relu{}, args);
Paul's avatar
Paul committed
109
    }
Paul's avatar
Paul committed
110
111
};

Paul's avatar
Paul committed
112
113
114
115
116
117
118
struct miopen_conv_bias
{
    op::convolution op;
    fusion f;
    fusion::op_t conv;
    fusion::op_t bias;

Paul's avatar
Paul committed
119
    miopen_conv_bias(op::convolution c, shape input, shape weights, shape b) : op(c), f(input)
Paul's avatar
Paul committed
120
121
122
123
124
125
126
127
128
129
130
131
    {
        f.create_conv(op, weights);
        f.create_bias(b);
    }

    std::string name() const { return "gpu::conv_bias"; }
    shape compute_shape(const std::vector<shape>& inputs) const
    {
        check_shapes{inputs, *this}.has(5);
        // TODO: Check slices
        return op.compute_shape({inputs.at(0), inputs.at(1)});
    }
Paul's avatar
Paul committed
132
133
    argument
    compute(context& ctx, const shape& output_shape, const std::vector<argument>& args) const
Paul's avatar
Paul committed
134
    {
Paul's avatar
Paul committed
135
        auto fargs  = make_fused_args();
Paul's avatar
Paul committed
136
137
138
139
140
        float alpha = 1, beta = 0;
        auto x = make_tensor(args[0].get_shape());
        auto y = make_tensor(output_shape);
        miopenSetOpArgsConvForward(fargs.get(), conv, &alpha, &beta, args[1].implicit());
        miopenSetOpArgsBiasForward(fargs.get(), bias, &alpha, &beta, args[3].implicit());
Paul's avatar
Paul committed
141
142
143
144
145
146
147
        miopenExecuteFusionPlan(ctx.handle.get(),
                                f.get(),
                                x.get(),
                                args[0].implicit(),
                                y.get(),
                                args[4].implicit(),
                                fargs.get());
Paul's avatar
Paul committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        return args.at(4);
    }

    shape compile(context& ctx)
    {
        int algo_count = 1;
        miopenConvFwdAlgorithm_t algo;
        miopenFusionPlanConvolutionGetAlgo(f.get(), 1, &algo_count, &algo);
        std::size_t ws_size = 0;
        miopenFusionPlanGetWorkSpaceSize(ctx.handle.get(), f.get(), &ws_size, algo);
        auto status = miopenCompileFusionPlan(ctx.handle.get(), f.get());
        if(status != miopenStatusSuccess)
            MIGRAPH_THROW("Compiling fusion plan failed");
        return shape{shape::int8_type, {ws_size}};
    }
};

struct match_conv_bias
{
Paul's avatar
Paul committed
167
    context* ctx = nullptr;
Paul's avatar
Paul committed
168
169
170
    auto matcher() const
    {
        return match::name("gpu::add")(match::any_of(
Paul's avatar
Paul committed
171
172
173
174
            match::all_of(match::arg(0)(match::broadcast_shape().bind("bias")),
                          match::arg(1)(match::name("gpu::convolution").bind("conv"))),
            match::all_of(match::arg(1)(match::broadcast_shape().bind("bias")),
                          match::arg(0)(match::name("gpu::convolution").bind("conv")))));
Paul's avatar
Paul committed
175
176
177
178
    }

    void apply(program& p, match::matcher_result r) const
    {
Paul's avatar
Paul committed
179
180
181
        auto conv_ins    = r.instructions["conv"];
        auto bias_ins    = r.instructions["bias"];
        auto input_ins   = conv_ins->inputs().at(0);
Paul's avatar
Paul committed
182
        auto weights_ins = conv_ins->inputs().at(1);
Paul's avatar
Paul committed
183
184
185
186
        auto conv_op     = any_cast<miopen_convolution>(conv_ins->get_operator()).op;
        auto ins         = r.result;
        auto alloc_ins   = ins->inputs().back();
        auto old_ws_ins  = conv_ins->inputs().at(2);
Paul's avatar
Paul committed
187

Paul's avatar
Paul committed
188
189
        miopen_conv_bias cb{
            conv_op, input_ins->get_shape(), weights_ins->get_shape(), bias_ins->get_shape()};
Paul's avatar
Paul committed
190
        // TODO: Insert ws allocation
Paul's avatar
Paul committed
191
        auto ws = cb.compile(*ctx);
Paul's avatar
Paul committed
192
193
194
195
196

        p.replace_instruction(ins, cb, input_ins, weights_ins, old_ws_ins, bias_ins, alloc_ins);
    }
};

Paul's avatar
Paul committed
197
198
199
void fuse_ops::apply(program& p) const
{
    match::find_matches(p, match_add_relu{}, match_conv_bias{ctx});
Paul's avatar
Paul committed
200
}
Paul's avatar
Paul committed
201
202
203
204

} // namespace gpu

} // namespace migraph