"example/1_gemm_xdl/README.md" did not exist on "6014185ac65e75f2a84cb67ef6ba83b48ae0fcb3"
lowering.cpp 6.29 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
19
20
21
#include <migraph/gpu/contiguous.hpp>
#include <migraph/gpu/relu.hpp>
#include <migraph/gpu/softmax.hpp>
#include <migraph/gpu/add.hpp>
#include <migraph/gpu/batchnorm.hpp>
wsttiger's avatar
wsttiger committed
22
23
#include <migraph/gpu/pooling.hpp>
#include <migraph/gpu/gemm.hpp>
Paul's avatar
Paul committed
24
#include <utility>
Paul's avatar
Paul committed
25
26

namespace migraph {
Paul's avatar
Paul committed
27
namespace gpu {
Paul's avatar
Paul committed
28
29
30

struct miopen_apply
{
Paul's avatar
Paul committed
31
    program* prog = nullptr;
Paul's avatar
Paul committed
32
    context ctx{};
Paul's avatar
Paul committed
33

Paul's avatar
Paul committed
34
35
36
37
38
39
40
    void check_shape(shape x, instruction_ref i)
    {
        assert(x == i->get_shape());
        (void)x;
        (void)i;
    }

Paul's avatar
Paul committed
41
42
    void apply()
    {
Paul's avatar
Paul committed
43
44
        for(auto it = prog->begin(); it != prog->end(); it++)
        {
Paul's avatar
Paul committed
45
            auto s = it->get_shape();
Paul's avatar
Paul committed
46
            if(it->name() == "convolution")
Paul's avatar
Paul committed
47
            {
Paul's avatar
Paul committed
48
                check_shape(s, apply_convolution(it));
Paul's avatar
Paul committed
49
            }
Paul's avatar
Paul committed
50
            else if(it->name() == "activation")
Paul's avatar
Paul committed
51
            {
Paul's avatar
Paul committed
52
                check_shape(s, apply_activation(it));
Paul's avatar
Paul committed
53
            }
Paul's avatar
Paul committed
54
            else if(it->name() == "pooling")
Paul's avatar
Paul committed
55
            {
Paul's avatar
Paul committed
56
                check_shape(s, apply_pooling(it));
Paul's avatar
Paul committed
57
            }
Paul's avatar
Paul committed
58
            else if(it->name() == "add")
Paul's avatar
Paul committed
59
            {
Paul's avatar
Paul committed
60
                check_shape(s, apply_add(it));
Paul's avatar
Paul committed
61
            }
Paul's avatar
Paul committed
62
            else if(it->name() == "gemm")
Paul's avatar
Paul committed
63
            {
Paul's avatar
Paul committed
64
                check_shape(s, apply_gemm(it));
Paul's avatar
Paul committed
65
            }
Paul's avatar
Paul committed
66
            else if(it->name() == "contiguous")
67
            {
Paul's avatar
Paul committed
68
                check_shape(s, apply_contiguous(it));
69
            }
Paul's avatar
Paul committed
70
            else if(it->name() == "batch_norm_inference")
71
            {
Paul's avatar
Paul committed
72
                check_shape(s, apply_batch_norm_inference(it));
73
            }
Paul's avatar
Paul committed
74
75
76
77
            else if(it->name() == "softmax")
            {
                check_shape(s, apply_softmax(it));
            }
Paul's avatar
Paul committed
78
79
80
        }
    }

Paul's avatar
Paul committed
81
    instruction_ref insert_allocation(instruction_ref ins, const shape& s, std::string tag = "")
Paul's avatar
Paul committed
82
    {
Paul's avatar
Paul committed
83
        if(ins == --prog->end() and tag.empty())
Paul's avatar
Paul committed
84
85
86
87
88
        {
            return prog->add_parameter("output", s);
        }
        else
        {
Paul's avatar
Paul committed
89
            auto is     = prog->add_outline(s);
Paul's avatar
Paul committed
90
            auto result = prog->insert_instruction(ins, hip_allocate{std::move(tag)}, is);
Paul's avatar
Paul committed
91
92
93
94
            return result;
        }
    }

Paul's avatar
Paul committed
95
    instruction_ref apply_convolution(instruction_ref ins)
Paul's avatar
Paul committed
96
    {
wsttiger's avatar
wsttiger committed
97
        auto&& op = any_cast<op::convolution>(ins->get_operator());
Paul's avatar
Paul committed
98

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

102
        auto workspace = insert_allocation(ins, ws, "workspace");
Paul's avatar
Paul committed
103
        auto output    = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
104

Paul's avatar
Paul committed
105
        return prog->replace_instruction(
Paul's avatar
Paul committed
106
            ins, conv, ins->inputs().at(0), ins->inputs().at(1), workspace, output);
Paul's avatar
Paul committed
107
108
    }

Paul's avatar
Paul committed
109
    instruction_ref apply_pooling(instruction_ref ins)
Paul's avatar
Paul committed
110
    {
wsttiger's avatar
wsttiger committed
111
        auto&& op   = any_cast<op::pooling>(ins->get_operator());
Paul's avatar
Paul committed
112
        auto pd     = make_pooling(op);
Paul's avatar
Paul committed
113
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
114

Paul's avatar
Paul committed
115
        return prog->replace_instruction(
Paul's avatar
Paul committed
116
            ins, miopen_pooling{op, std::move(pd)}, ins->inputs().at(0), output);
Paul's avatar
Paul committed
117
118
    }

Paul's avatar
Paul committed
119
    instruction_ref apply_activation(instruction_ref ins)
Paul's avatar
Paul committed
120
    {
wsttiger's avatar
wsttiger committed
121
        auto&& op = any_cast<op::activation>(ins->get_operator());
Paul's avatar
Paul committed
122
123
        auto ad   = make_relu();
        if(op.mode == "relu")
Paul's avatar
Paul committed
124
        {
Paul's avatar
Paul committed
125
            auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
126
            return prog->replace_instruction(
Paul's avatar
Paul committed
127
                ins, miopen_relu{std::move(ad)}, ins->inputs().at(0), output);
Paul's avatar
Paul committed
128
        }
Paul's avatar
Paul committed
129
        return ins;
Paul's avatar
Paul committed
130
    }
Paul's avatar
Paul committed
131

Paul's avatar
Paul committed
132
133
    instruction_ref apply_softmax(instruction_ref ins)
    {
wsttiger's avatar
wsttiger committed
134
        auto&& op   = any_cast<op::softmax>(ins->get_operator());
Paul's avatar
Paul committed
135
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
136
        return prog->replace_instruction(ins, miopen_softmax{op}, ins->inputs().at(0), output);
Paul's avatar
Paul committed
137
138
    }

Paul's avatar
Paul committed
139
    instruction_ref apply_add(instruction_ref ins)
Paul's avatar
Paul committed
140
    {
Paul's avatar
Paul committed
141
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
142
        return prog->replace_instruction(
Paul's avatar
Paul committed
143
            ins, hip_add{}, ins->inputs().at(0), ins->inputs().at(1), output);
Paul's avatar
Paul committed
144
    }
Paul's avatar
Paul committed
145

Paul's avatar
Paul committed
146
    instruction_ref apply_gemm(instruction_ref ins)
Paul's avatar
Paul committed
147
    {
wsttiger's avatar
wsttiger committed
148
        auto&& op   = any_cast<op::gemm>(ins->get_operator());
Paul's avatar
Paul committed
149
        auto output = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
150
        return prog->replace_instruction(
Paul's avatar
Paul committed
151
            ins, miopen_gemm{op}, ins->inputs().at(0), ins->inputs().at(1), output);
Paul's avatar
Paul committed
152
    }
153

Paul's avatar
Paul committed
154
    instruction_ref apply_contiguous(instruction_ref ins)
155
    {
wsttiger's avatar
wsttiger committed
156
        auto&& op   = any_cast<op::contiguous>(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_contiguous{op}, ins->inputs().at(0), output);
159
    }
160

Paul's avatar
Paul committed
161
    instruction_ref apply_batch_norm_inference(instruction_ref ins)
162
    {
wsttiger's avatar
wsttiger committed
163
        auto&& op       = any_cast<op::batch_norm_inference>(ins->get_operator());
Paul's avatar
Paul committed
164
        auto output     = insert_allocation(ins, ins->get_shape());
Paul's avatar
Paul committed
165
        shape old_shape = ins->inputs().at(1)->get_shape();
wsttiger's avatar
wsttiger committed
166
        std::vector<int64_t> new_shape{1, static_cast<int64_t>(old_shape.elements()), 1, 1};
wsttiger's avatar
wsttiger committed
167
        auto reshape_op = op::reshape{new_shape};
Paul's avatar
Paul committed
168
        std::vector<instruction_ref> reshapes;
Paul's avatar
Paul committed
169
170
        std::transform(ins->inputs().begin() + 1,
                       ins->inputs().end(),
Paul's avatar
Paul committed
171
172
                       std::back_inserter(reshapes),
                       [&](auto i) { return prog->insert_instruction(ins, reshape_op, i); });
Paul's avatar
Paul committed
173
        return prog->replace_instruction(ins,
Paul's avatar
Paul committed
174
                                         miopen_batch_norm_inference{op},
Paul's avatar
Paul committed
175
                                         ins->inputs().at(0),
Paul's avatar
Paul committed
176
177
178
179
180
                                         reshapes[0],
                                         reshapes[1],
                                         reshapes[2],
                                         reshapes[3],
                                         output);
181
    }
Paul's avatar
Paul committed
182
183
};

Paul's avatar
Paul committed
184
void lowering::apply(program& p) const { miopen_apply{&p, ctx}.apply(); }
Paul's avatar
Paul committed
185
} // namespace gpu
Paul's avatar
Paul committed
186
} // namespace migraph