schedule_test.cpp 10.9 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#include <migraphx/schedule.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
Paul's avatar
Paul committed
6
#include <migraphx/ranges.hpp>
Paul's avatar
Paul committed
7
#include <migraphx/dfor.hpp>
Paul's avatar
Paul committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <basic_ops.hpp>
#include <test.hpp>

struct unary_op
{
    std::string name() const { return "unary"; }
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
    {
        if(args.empty())
            return {};
        return args.front();
    }

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
};

Paul's avatar
Paul committed
31
struct nary_op
Paul's avatar
Paul committed
32
{
Paul's avatar
Paul committed
33
    std::string name() const { return "nary"; }
Paul's avatar
Paul committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    migraphx::argument
    compute(migraphx::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
    {
        if(args.empty())
            return {};
        return args.front();
    }

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
};

Paul's avatar
Paul committed
50
51
52
53
54
55
56
57
58
59
60
struct wait_event
{
    std::vector<std::size_t> wait_for;
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return migraphx::pack(f(self.wait_for, "wait_for"));
    }
    std::string name() const { return "wait_event"; }
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const { return {}; }

Paul's avatar
Paul committed
61
62
63
    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>&) const
Paul's avatar
Paul committed
64
65
66
67
68
    {
        return {};
    }
};

Paul's avatar
Paul committed
69
70
71
72
73
using instruction_map = std::unordered_map<migraphx::instruction_ref, std::size_t>;

struct schedule_model_test
{
    instruction_map* ins2stream;
Paul's avatar
Paul committed
74
75
    std::size_t concurrency() const { return 4; }
    void
Paul's avatar
Paul committed
76
    schedule_instruction(migraphx::program&, migraphx::instruction_ref ins, std::size_t n) const
Paul's avatar
Paul committed
77
78
79
80
81
82
83
84
    {
        (*ins2stream)[ins] = n;
    }
    void wait(migraphx::program& p,
              migraphx::instruction_ref ins,
              std::size_t wait_on,
              const std::vector<std::size_t>& wait_for) const
    {
Paul's avatar
Paul committed
85
86
        (*ins2stream)[ins] = wait_on;
        p.insert_instruction(ins, wait_event{wait_for});
Paul's avatar
Paul committed
87
88
89
    }
    std::size_t weight(const migraphx::operation& op) const
    {
Paul's avatar
Paul committed
90
        if(op.name() == "binary" or op.name() == "unary")
Paul's avatar
Paul committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
            return 4;
        else
            return 1;
    }
};

struct schedule_target
{
    instruction_map* ins2stream;
    std::string name() const { return "schedule"; }
    std::vector<migraphx::pass> get_passes(migraphx::context&) const
    {
        return {migraphx::schedule{schedule_model_test{ins2stream}}};
    }
    migraphx::context get_context() const { return {}; }
};

bool check_conflicts(migraphx::program& p, migraphx::instruction_ref x, migraphx::instruction_ref y)
{
Paul's avatar
Paul committed
110
    for(auto ins : migraphx::iterator_for(p))
Paul's avatar
Paul committed
111
    {
Paul's avatar
Paul committed
112
        if(ins->name() != "identity")
Paul's avatar
Paul committed
113
            continue;
Paul's avatar
Paul committed
114
        if (not migraphx::contains(ins->inputs(), x))
Paul's avatar
Paul committed
115
            continue;
Paul's avatar
Paul committed
116
117
118
        if (not migraphx::contains(ins->inputs(), y))
            continue;
        return true;
Paul's avatar
Paul committed
119
120
121
122
    }
    return false;
}

Paul's avatar
Paul committed
123
void check_conflicts(migraphx::program& p,
Paul's avatar
Paul committed
124
125
                     std::vector<std::vector<migraphx::instruction_ref>> conflicts,
                     bool result = true)
Paul's avatar
Paul committed
126
127
{
    migraphx::dfor(conflicts.size(), conflicts.size())([&](auto i, auto j) {
Paul's avatar
Paul committed
128
        if(i == j)
Paul's avatar
Paul committed
129
            return;
Paul's avatar
Paul committed
130
131
        for(auto ins1 : conflicts[i])
            for(auto ins2 : conflicts[j])
Paul's avatar
Paul committed
132
                CHECK(check_conflicts(p, ins1, ins2) == result);
Paul's avatar
Paul committed
133
134
135
    });
}

Paul's avatar
Paul committed
136
137
138
139
140
141
142
143
144
145
std::vector<std::size_t> get_wait_for(std::size_t wait_on, std::vector<std::size_t> wait_for)
{
    wait_for.erase(std::find(wait_for.begin(), wait_for.end(), wait_on));
    std::sort(wait_for.begin(), wait_for.end());
    return wait_for;
}

std::vector<std::size_t> get_wait_for(migraphx::instruction_ref ins)
{
    auto wait_ins = std::prev(ins);
Paul's avatar
Paul committed
146
    if(wait_ins->name() != "wait_event")
Paul's avatar
Paul committed
147
148
149
150
151
152
        return {};
    auto wf = migraphx::any_cast<wait_event>(wait_ins->get_operator()).wait_for;
    std::sort(wf.begin(), wf.end());
    return wf;
}

Paul's avatar
Paul committed
153
154
155
template <class T>
std::vector<migraphx::instruction_ref>
chain(migraphx::program& p, std::size_t n, T x, migraphx::instruction_ref input)
Paul's avatar
Paul committed
156
157
{
    std::vector<migraphx::instruction_ref> result;
Paul's avatar
Paul committed
158
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
159
160
161
162
163
164
165
    {
        result.push_back(p.add_instruction(x, input));
        input = result.back();
    }
    return result;
}

Paul's avatar
Paul committed
166
167
168
169
170
TEST_CASE(single_entry)
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
Paul's avatar
Paul committed
171
172
    auto onep1  = p.add_instruction(unary_op{}, one);
    auto onep2  = p.add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
173
    auto binary = p.add_instruction(nary_op{}, onep1, onep2);
Paul's avatar
Paul committed
174
    p.compile(schedule_target{&stream});
Paul's avatar
Paul committed
175
    EXPECT(stream.count(one) == 0);
Paul's avatar
Paul committed
176
177
178
179
180
181
182
    EXPECT(stream.at(onep1) != stream.at(onep2));
    EXPECT(stream.at(binary) == 0);
    EXPECT(get_wait_for(binary) == get_wait_for(stream[binary], {stream[onep1], stream[onep2]}));
    EXPECT(check_conflicts(p, onep1, onep2));
}

TEST_CASE(double_entry)
Paul's avatar
Paul committed
183
{
Paul's avatar
Paul committed
184
    instruction_map stream;
Paul's avatar
Paul committed
185
    migraphx::program p;
Paul's avatar
Paul committed
186
187
188
189
    auto one    = p.add_literal(1);
    auto two    = p.add_literal(2);
    auto onep   = p.add_instruction(unary_op{}, one);
    auto twop   = p.add_instruction(unary_op{}, two);
Paul's avatar
Paul committed
190
    auto binary = p.add_instruction(nary_op{}, onep, twop);
Paul's avatar
Paul committed
191
    p.compile(schedule_target{&stream});
Paul's avatar
Paul committed
192
193
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.count(two) == 0);
Paul's avatar
Paul committed
194
195
196
    EXPECT(stream.at(onep) != stream.at(twop));
    EXPECT(stream.at(binary) == 0);
    EXPECT(get_wait_for(binary) == get_wait_for(stream[binary], {stream[onep], stream[twop]}));
Paul's avatar
Paul committed
197
198
199
    // EXPECT(check_conflicts(p, onep, twop));
}

Paul's avatar
Paul committed
200
TEST_CASE(two_branches)
Paul's avatar
Paul committed
201
202
203
204
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
Paul's avatar
Paul committed
205
206
    auto c1     = chain(p, 2, unary_op{}, one);
    auto i1     = p.add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
207
208
209
210
    auto binary = p.add_instruction(nary_op{}, i1, c1.back());
    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.at(i1) == 1);
Paul's avatar
Paul committed
211
    for(auto ins : c1)
Paul's avatar
Paul committed
212
213
214
215
216
217
        EXPECT(stream.at(ins) == 0);
    EXPECT(stream.at(binary) == 0);
    EXPECT(get_wait_for(binary) == get_wait_for(stream[binary], {stream[c1.back()], stream[i1]}));
    check_conflicts(p, {c1, {i1}});
}

Paul's avatar
Paul committed
218
TEST_CASE(four_branches)
Paul's avatar
Paul committed
219
220
221
222
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
Paul's avatar
Paul committed
223
224
225
226
    auto c1     = chain(p, 4, unary_op{}, one);
    auto c2     = chain(p, 3, unary_op{}, one);
    auto c3     = chain(p, 2, unary_op{}, one);
    auto i1     = p.add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
227
    auto binary = p.add_instruction(nary_op{}, i1, c1.back(), c2.back(), c3.back());
Paul's avatar
Paul committed
228
229
230
    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.at(i1) == 3);
Paul's avatar
Paul committed
231
232
233
234
235
236
    for(auto ins : c1)
        EXPECT(stream.at(ins) == 0);
    for(auto ins : c2)
        EXPECT(stream.at(ins) == 1);
    for(auto ins : c3)
        EXPECT(stream.at(ins) == 2);
Paul's avatar
Paul committed
237
    EXPECT(stream.at(binary) == 0);
Paul's avatar
Paul committed
238
239
240
    EXPECT(get_wait_for(binary) ==
           get_wait_for(stream[binary],
                        {stream[c1.back()], stream[c2.back()], stream[c3.back()], stream[i1]}));
Paul's avatar
Paul committed
241
    check_conflicts(p, {c1, c2, c3, {i1}});
Paul's avatar
Paul committed
242
243
}

Paul's avatar
Paul committed
244
TEST_CASE(five_branches)
Paul's avatar
Paul committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
    auto c1     = chain(p, 5, unary_op{}, one);
    auto c2     = chain(p, 4, unary_op{}, one);
    auto c3     = chain(p, 3, unary_op{}, one);
    auto c4     = chain(p, 2, unary_op{}, one);
    auto i1     = p.add_instruction(unary_op{}, one);
    auto binary = p.add_instruction(nary_op{}, i1, c1.back(), c2.back(), c3.back(), c4.back());
    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.at(i1) == 3);
    for(auto ins : c1)
        EXPECT(stream.at(ins) == 0);
    for(auto ins : c2)
        EXPECT(stream.at(ins) == 1);
    for(auto ins : c3)
        EXPECT(stream.at(ins) == 2);
    for(auto ins : c4)
        EXPECT(stream.at(ins) == 3);
    EXPECT(stream.at(binary) == 0);
    EXPECT(get_wait_for(binary) ==
           get_wait_for(stream[binary],
                        {stream[c1.back()], stream[c2.back()], stream[c3.back()], stream[i1]}));
    check_conflicts(p, {c1, c2, c3, c4});
    check_conflicts(p, {c1, c2, c3, {i1}});
}

Paul's avatar
Paul committed
274
275
276
277
TEST_CASE(seq_merge)
{
    instruction_map stream;
    migraphx::program p;
Paul's avatar
Paul committed
278
279
280
    auto one     = p.add_literal(1);
    auto c1      = chain(p, 2, unary_op{}, one);
    auto i1      = p.add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
281
282
    auto binary1 = p.add_instruction(nary_op{}, i1, c1.back());

Paul's avatar
Paul committed
283
284
    auto c2      = chain(p, 2, unary_op{}, binary1);
    auto i2      = p.add_instruction(unary_op{}, binary1);
Paul's avatar
Paul committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
    auto binary2 = p.add_instruction(nary_op{}, i2, c2.back());

    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);

    EXPECT(stream.at(i1) == 1);
    for(auto ins : c1)
        EXPECT(stream.at(ins) == 0);
    EXPECT(stream.at(binary1) == 0);
    EXPECT(get_wait_for(binary1) == get_wait_for(stream[binary1], {stream[c1.back()], stream[i1]}));
    check_conflicts(p, {c1, {i1}});

    EXPECT(stream.at(i2) == 1);
    for(auto ins : c2)
        EXPECT(stream.at(ins) == 0);
    EXPECT(stream.at(binary2) == 0);
    EXPECT(get_wait_for(binary2) == get_wait_for(stream[binary2], {stream[c2.back()], stream[i2]}));
    check_conflicts(p, {c2, {i2}});
}

TEST_CASE(par_merge)
{
    instruction_map stream;
    migraphx::program p;
Paul's avatar
Paul committed
309
310
311
312
    auto one     = p.add_literal(1);
    auto start1  = p.add_instruction(unary_op{}, one);
    auto c1      = chain(p, 3, unary_op{}, start1);
    auto i1      = p.add_instruction(unary_op{}, start1);
Paul's avatar
Paul committed
313
314
    auto binary1 = p.add_instruction(nary_op{}, i1, c1.back());

Paul's avatar
Paul committed
315
316
317
    auto start2  = p.add_instruction(unary_op{}, one);
    auto c2      = chain(p, 2, unary_op{}, start2);
    auto i2      = p.add_instruction(unary_op{}, start2);
Paul's avatar
Paul committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    auto binary2 = p.add_instruction(nary_op{}, i2, c2.back());

    auto binary3 = p.add_instruction(nary_op{}, binary1, binary2);

    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.at(binary3) == 0);

    EXPECT(stream.at(i1) == 1);
    for(auto ins : c1)
        EXPECT(stream.at(ins) == 0);
    EXPECT(stream.at(binary1) == 0);
    EXPECT(get_wait_for(binary1) == get_wait_for(stream[binary1], {stream[c1.back()], stream[i1]}));
    check_conflicts(p, {c1, {i1}});

    EXPECT(stream.at(i2) == 2);
    for(auto ins : c2)
        EXPECT(stream.at(ins) == 1);
    EXPECT(stream.at(binary2) == 1);
    EXPECT(get_wait_for(binary2) == get_wait_for(stream[binary2], {stream[c2.back()], stream[i2]}));
    check_conflicts(p, {c2, {i2}});

    EXPECT(check_conflicts(p, binary1, binary2));
}

Paul's avatar
Paul committed
343
int main(int argc, const char* argv[]) { test::run(argc, argv); }