schedule_test.cpp 33.2 KB
Newer Older
Paul's avatar
Paul committed
1
#include <migraphx/schedule.hpp>
2
#include <migraphx/pass_manager.hpp>
Paul's avatar
Paul committed
3
4
5
6
7
8
#include <migraphx/generate.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/dfor.hpp>
#include <basic_ops.hpp>
9
10
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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; }
};

struct nary_op
{
35
    std::string comment;
Paul's avatar
Paul committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return migraphx::pack(f(self.comment, "comment"));
    }
    std::string name() const { return "nary"; }
    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();
    }
};

struct stream_free_op
{
60
    std::string comment;
Paul's avatar
Paul committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return migraphx::pack(f(self.comment, "comment"));
    }
    std::string name() const { return "stream_free"; }
    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();
    }
};

struct wait_event
{
    std::shared_ptr<std::vector<std::size_t>> wait_for =
        std::make_shared<std::vector<std::size_t>>();
    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 {}; }

    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>&) const
    {
        assert(wait_for != nullptr);
        assert(not wait_for->empty());
        return {};
    }
};

using instruction_map = std::unordered_map<migraphx::instruction_ref, std::size_t>;
using int_map         = std::unordered_map<std::size_t, std::size_t>;
using wait_map =
    std::unordered_map<migraphx::instruction_ref, std::shared_ptr<std::vector<std::size_t>>>;

struct schedule_model_test
{
    std::shared_ptr<instruction_map> ins2stream = std::make_shared<instruction_map>();
    std::shared_ptr<int_map> wait2stream        = std::make_shared<int_map>();
    std::shared_ptr<wait_map> ins2wait_for      = std::make_shared<wait_map>();
    std::size_t concurrency() const { return 4; }
116
    void sched(migraphx::module&, migraphx::instruction_ref ins, std::size_t n) const
Paul's avatar
Paul committed
117
118
119
    {
        (*ins2stream)[ins] = n;
    }
120
    void wait(migraphx::module& p, migraphx::instruction_ref ins, std::size_t wait_id) const
Paul's avatar
Paul committed
121
122
123
124
125
126
127
128
129
    {
        if(ins2wait_for->count(ins) == 0)
        {
            auto event = wait_event{};
            p.insert_instruction(ins, event);
            (*ins2wait_for)[ins] = event.wait_for;
        }
        (*ins2wait_for)[ins]->push_back(wait2stream->at(wait_id));
    }
130
    void record(migraphx::module&, migraphx::instruction_ref ins, std::size_t wait_id) const
Paul's avatar
Paul committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    {
        (*wait2stream)[wait_id] = ins2stream->at(ins);
    }
    std::size_t weight(const migraphx::operation& op) const
    {
        if(op.name() == "stream_free")
            return 0;
        else if(op.name() == "binary" or op.name() == "unary")
            return 4;
        else
            return 1;
    }
};

bool check_conflicts(migraphx::program& p, migraphx::instruction_ref x, migraphx::instruction_ref y)
{
Shucai Xiao's avatar
Shucai Xiao committed
147
148
    auto* mm = p.get_main_module();
    for(auto ins : migraphx::iterator_for(*mm))
Paul's avatar
Paul committed
149
150
151
152
153
154
155
156
157
158
159
160
    {
        if(ins->name() != "identity")
            continue;
        if(not migraphx::contains(ins->inputs(), x))
            continue;
        if(not migraphx::contains(ins->inputs(), y))
            continue;
        return true;
    }
    return false;
}

161
struct scheduler
Paul's avatar
Paul committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
    schedule_model_test model{};

    std::size_t get_stream(migraphx::instruction_ref ins) { return model.ins2stream->at(ins); }

    std::vector<std::size_t> get_streams(std::vector<migraphx::instruction_ref> inss)
    {
        std::vector<std::size_t> result;
        std::transform(inss.begin(), inss.end(), std::back_inserter(result), [&](auto ins) {
            return this->get_stream(ins);
        });
        return result;
    }

176
177
178
179
180
    void run_pass(migraphx::program& p)
    {
        auto* mm = p.get_main_module();
        migraphx::run_passes(*mm, {migraphx::schedule{model}});
    }
181

Paul's avatar
Paul committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    bool has_stream(migraphx::instruction_ref ins) { return model.ins2stream->count(ins) > 0; }

    void check_conflicts(migraphx::program& p,
                         std::vector<std::vector<migraphx::instruction_ref>> conflicts,
                         bool result = true)
    {
        migraphx::dfor(conflicts.size(), conflicts.size())([&](auto i, auto j) {
            if(i == j)
                return;
            for(auto ins1 : conflicts[i])
            {
                for(auto ins2 : conflicts[j])
                {
                    // If both instructions are on the same stream then dont check for a conflict
                    if(this->has_stream(ins1) and this->has_stream(ins2) and
                       this->get_stream(ins1) == this->get_stream(ins2))
                        continue;
                    CHECK(::check_conflicts(p, ins1, ins2) == result);
                }
            }
        });
    }
};

template <class T>
std::vector<T> sorted(std::vector<T> x)
{
    std::sort(x.begin(), x.end());
    return x;
}

template <class T>
std::vector<T> unique(std::vector<T> x)
{
    std::sort(x.begin(), x.end());
    x.erase(std::unique(x.begin(), x.end()), x.end());
    return x;
}

std::vector<std::size_t> get_wait_for(std::vector<std::size_t> wait_for)
{
    return unique(std::move(wait_for));
}

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));
    return unique(wait_for);
}

std::vector<std::size_t> get_wait_for(migraphx::instruction_ref ins)
{
    auto wait_ins = std::prev(ins);
    // Skip identity operators
    while(wait_ins->name() == "identity")
        wait_ins = std::prev(wait_ins);
    if(wait_ins->name() != "wait_event")
        return {};
    auto wf = *migraphx::any_cast<wait_event>(wait_ins->get_operator()).wait_for;
    std::sort(wf.begin(), wf.end());
    return wf;
}

template <class T>
std::vector<migraphx::instruction_ref>
chain(migraphx::program& p, std::size_t n, T x, migraphx::instruction_ref input)
{
    std::vector<migraphx::instruction_ref> result;
    for(std::size_t i = 0; i < n; i++)
    {
252
        result.push_back(p.get_main_module()->add_instruction(x, input));
Paul's avatar
Paul committed
253
254
255
256
257
258
        input = result.back();
    }
    return result;
}
TEST_CASE(single_entry)
{
259
    scheduler t{};
Paul's avatar
Paul committed
260
    migraphx::program p;
261
262
263
264
265
266

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
    auto onep1  = mm->add_instruction(unary_op{}, one);
    auto onep2  = mm->add_instruction(unary_op{}, one);
    auto binary = mm->add_instruction(nary_op{}, onep1, onep2);
267
    t.run_pass(p);
Paul's avatar
Paul committed
268
269
270
271
272
273
274
275
276
277
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(onep1) != t.get_stream(onep2));
    EXPECT(t.get_stream(binary) == 0);
    EXPECT(get_wait_for(binary) ==
           get_wait_for(t.get_stream(binary), {t.get_stream(onep1), t.get_stream(onep2)}));
    EXPECT(check_conflicts(p, onep1, onep2));
}

TEST_CASE(stream_free)
{
278
    scheduler t{};
Paul's avatar
Paul committed
279
    migraphx::program p;
280
281
282
283
284
285

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
    auto onep1  = mm->add_instruction(stream_free_op{}, one);
    auto onep2  = mm->add_instruction(stream_free_op{}, one);
    auto binary = mm->add_instruction(nary_op{}, onep1, onep2);
286
    t.run_pass(p);
Paul's avatar
Paul committed
287
288
289
    EXPECT(not t.has_stream(one));
    EXPECT(not t.has_stream(onep1));
    EXPECT(not t.has_stream(onep2));
290
    EXPECT(not t.has_stream(binary));
Paul's avatar
Paul committed
291
292
293
294
}

TEST_CASE(zero_record)
{
295
    scheduler t{};
Paul's avatar
Paul committed
296
    migraphx::program p;
297
298
299
300
301

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
    auto onep1  = mm->add_instruction(unary_op{}, one);
    auto onep2  = mm->add_instruction(unary_op{}, one);
302
303
    auto onei1  = mm->add_instruction(migraphx::make_op("identity"), onep1);
    auto onei2  = mm->add_instruction(migraphx::make_op("identity"), onep2);
304
    auto binary = mm->add_instruction(nary_op{}, onei1, onei2);
305
    t.run_pass(p);
Paul's avatar
Paul committed
306
307
308
309
310
311
312
313
314
315
316
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(onep1) != t.get_stream(onep2));
    EXPECT(t.has_stream(binary));
    EXPECT(get_wait_for(binary) ==
           get_wait_for(t.get_stream(binary), {t.get_stream(onep1), t.get_stream(onep2)}));
    EXPECT(check_conflicts(p, onep1, onep2));
    t.check_conflicts(p, {{onep1, onei1}, {onep2, onei2}});
}

TEST_CASE(zero_merge1)
{
317
    scheduler t{};
Paul's avatar
Paul committed
318
    migraphx::program p;
319
320
321
322
323

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
    auto onep1  = mm->add_instruction(unary_op{}, one);
    auto onep2  = mm->add_instruction(unary_op{}, one);
324
    auto binary = mm->add_instruction(migraphx::make_op("identity"), onep1, onep2);
325
    t.run_pass(p);
Paul's avatar
Paul committed
326
327
328
329
330
331
332
333
334
335
336
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(onep1) != t.get_stream(onep2));
    // No stream assignment
    EXPECT(not t.has_stream(binary));
    // There is no wait
    EXPECT(get_wait_for(binary).empty());
    EXPECT(check_conflicts(p, onep1, onep2));
}

TEST_CASE(zero_merge2)
{
337
    scheduler t{};
Paul's avatar
Paul committed
338
    migraphx::program p;
339
340
341
342
343

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
    auto onep1  = mm->add_instruction(unary_op{}, one);
    auto onep2  = mm->add_instruction(unary_op{}, one);
344
345
346
    auto binary = mm->add_instruction(migraphx::make_op("identity"),
                                      mm->add_instruction(migraphx::make_op("identity"), onep1),
                                      mm->add_instruction(migraphx::make_op("identity"), onep2));
347
    t.run_pass(p);
Paul's avatar
Paul committed
348
349
350
351
352
353
354
355
356
357
358
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(onep1) != t.get_stream(onep2));
    // No stream assignment
    EXPECT(not t.has_stream(binary));
    // There is no wait
    EXPECT(get_wait_for(binary).empty());
    EXPECT(check_conflicts(p, onep1, onep2));
}

TEST_CASE(zero_merge3)
{
359
    scheduler t{};
Paul's avatar
Paul committed
360
    migraphx::program p;
361
362
363
364
365

    auto* mm   = p.get_main_module();
    auto one   = mm->add_literal(1);
    auto onep1 = mm->add_instruction(unary_op{}, one);
    auto onep2 = mm->add_instruction(unary_op{}, one);
366
    auto id    = mm->add_instruction(migraphx::make_op("identity"), onep1, onep2);
367
    auto final = mm->add_instruction(unary_op{}, id);
368
    t.run_pass(p);
Paul's avatar
Paul committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(onep1) != t.get_stream(onep2));
    // No stream assignment
    EXPECT(not t.has_stream(id));
    // There is no wait
    EXPECT(get_wait_for(id).empty());
    // Stream assignment for final op
    EXPECT(t.get_stream(final) == 0);
    EXPECT(get_wait_for(final) ==
           get_wait_for(t.get_stream(final), {t.get_stream(onep1), t.get_stream(onep2)}));
    EXPECT(check_conflicts(p, onep1, onep2));
}

TEST_CASE(zero_merge4)
{
384
    scheduler t{};
Paul's avatar
Paul committed
385
    migraphx::program p;
386
387
388
389
390

    auto* mm   = p.get_main_module();
    auto one   = mm->add_literal(1);
    auto onep1 = mm->add_instruction(unary_op{}, one);
    auto onep2 = mm->add_instruction(unary_op{}, one);
391
392
393
    auto id    = mm->add_instruction(migraphx::make_op("identity"),
                                  mm->add_instruction(migraphx::make_op("identity"), onep1),
                                  mm->add_instruction(migraphx::make_op("identity"), onep2));
394
    auto final = mm->add_instruction(unary_op{}, id);
395
    t.run_pass(p);
Paul's avatar
Paul committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(onep1) != t.get_stream(onep2));
    // No stream assignment
    EXPECT(not t.has_stream(id));
    // There is no wait
    EXPECT(get_wait_for(id).empty());
    // Stream assignment for final op
    EXPECT(t.get_stream(final) == 0);
    EXPECT(get_wait_for(final) ==
           get_wait_for(t.get_stream(final), {t.get_stream(onep1), t.get_stream(onep2)}));
    EXPECT(check_conflicts(p, onep1, onep2));
}

TEST_CASE(double_entry)
{
411
    scheduler t{};
Paul's avatar
Paul committed
412
    migraphx::program p;
413
414
415
416
417
418
419

    auto* mm    = p.get_main_module();
    auto one    = mm->add_instruction(stream_free_op{}, mm->add_literal(1));
    auto two    = mm->add_instruction(stream_free_op{}, mm->add_literal(2));
    auto onep   = mm->add_instruction(unary_op{}, one);
    auto twop   = mm->add_instruction(unary_op{}, two);
    auto binary = mm->add_instruction(nary_op{}, onep, twop);
420
    t.run_pass(p);
Paul's avatar
Paul committed
421
422
423
424
425
426
427
428
429
430
431
    EXPECT(not t.has_stream(one));
    EXPECT(not t.has_stream(two));
    EXPECT(t.get_stream(onep) != t.get_stream(twop));
    EXPECT(t.get_stream(binary) == 0);
    EXPECT(get_wait_for(binary) ==
           get_wait_for(t.get_stream(binary), {t.get_stream(onep), t.get_stream(twop)}));
    t.check_conflicts(p, {{onep, one}, {twop, two}});
}

TEST_CASE(two_branches)
{
432
    scheduler t{};
Paul's avatar
Paul committed
433
    migraphx::program p;
434
435
436

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
Paul's avatar
Paul committed
437
    auto c1     = chain(p, 2, unary_op{}, one);
438
439
    auto i1     = mm->add_instruction(unary_op{}, one);
    auto binary = mm->add_instruction(nary_op{}, i1, c1.back());
440
    t.run_pass(p);
Paul's avatar
Paul committed
441
442
443
444
445
446
447
448
449
450
451
452
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(i1) == 1);
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == 0);
    EXPECT(t.get_stream(binary) == 0);
    EXPECT(get_wait_for(binary) ==
           get_wait_for(t.get_stream(binary), {t.get_stream(c1.back()), t.get_stream(i1)}));
    t.check_conflicts(p, {c1, {i1}});
}

TEST_CASE(four_branches)
{
453
    scheduler t{};
Paul's avatar
Paul committed
454
    migraphx::program p;
455
456
457

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
Paul's avatar
Paul committed
458
459
460
    auto c1     = chain(p, 4, unary_op{}, one);
    auto c2     = chain(p, 3, unary_op{}, one);
    auto c3     = chain(p, 2, unary_op{}, one);
461
462
    auto i1     = mm->add_instruction(unary_op{}, one);
    auto binary = mm->add_instruction(nary_op{}, i1, c1.back(), c2.back(), c3.back());
463
    t.run_pass(p);
Paul's avatar
Paul committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(i1) == 3);
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == 0);
    for(auto ins : c2)
        EXPECT(t.get_stream(ins) == 1);
    for(auto ins : c3)
        EXPECT(t.get_stream(ins) == 2);
    EXPECT(t.get_stream(binary) == 0);
    EXPECT(get_wait_for(binary) == get_wait_for(t.get_stream(binary),
                                                {t.get_stream(c1.back()),
                                                 t.get_stream(c2.back()),
                                                 t.get_stream(c3.back()),
                                                 t.get_stream(i1)}));
    t.check_conflicts(p, {c1, c2, c3, {i1}});
}

TEST_CASE(five_branches)
{
483
    scheduler t{};
Paul's avatar
Paul committed
484
    migraphx::program p;
485
486
487

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
Paul's avatar
Paul committed
488
489
490
491
    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);
492
493
    auto i1     = mm->add_instruction(unary_op{}, one);
    auto binary = mm->add_instruction(nary_op{}, i1, c1.back(), c2.back(), c3.back(), c4.back());
494
    t.run_pass(p);
Paul's avatar
Paul committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(i1) == 3);
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == 0);
    for(auto ins : c2)
        EXPECT(t.get_stream(ins) == 1);
    for(auto ins : c3)
        EXPECT(t.get_stream(ins) == 2);
    for(auto ins : c4)
        EXPECT(t.get_stream(ins) == 3);
    EXPECT(t.get_stream(binary) == 0);
    EXPECT(get_wait_for(binary) == get_wait_for(t.get_stream(binary),
                                                {t.get_stream(c1.back()),
                                                 t.get_stream(c2.back()),
                                                 t.get_stream(c3.back()),
                                                 t.get_stream(i1)}));
    t.check_conflicts(p, {c1, c2, c3, c4});
    t.check_conflicts(p, {c1, c2, c3, {i1}});
}

TEST_CASE(four_branches_eq)
{
517
    scheduler t{};
Paul's avatar
Paul committed
518
    migraphx::program p;
519
520
521
522
523
524
525
526

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
    auto onep1  = mm->add_instruction(unary_op{}, one);
    auto onep2  = mm->add_instruction(unary_op{}, one);
    auto onep3  = mm->add_instruction(unary_op{}, one);
    auto onep4  = mm->add_instruction(unary_op{}, one);
    auto binary = mm->add_instruction(nary_op{}, onep1, onep2, onep3, onep4);
527
    t.run_pass(p);
Paul's avatar
Paul committed
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
    EXPECT(not t.has_stream(one));
    EXPECT(
        sorted<std::size_t>(
            {t.get_stream(onep1), t.get_stream(onep2), t.get_stream(onep3), t.get_stream(onep4)}) ==
        unique<std::size_t>(
            {t.get_stream(onep1), t.get_stream(onep2), t.get_stream(onep3), t.get_stream(onep4)}));
    EXPECT(t.get_stream(binary) == 0);
    EXPECT(
        get_wait_for(binary) ==
        get_wait_for(
            t.get_stream(binary),
            {t.get_stream(onep1), t.get_stream(onep2), t.get_stream(onep3), t.get_stream(onep4)}));
    t.check_conflicts(p, {{onep1}, {onep2}, {onep3}, {onep4}});
}

TEST_CASE(seq_merge)
{
545
    scheduler t{};
Paul's avatar
Paul committed
546
    migraphx::program p;
547
548
549

    auto* mm     = p.get_main_module();
    auto one     = mm->add_literal(1);
Paul's avatar
Paul committed
550
    auto c1      = chain(p, 2, unary_op{}, one);
551
552
    auto i1      = mm->add_instruction(unary_op{}, one);
    auto binary1 = mm->add_instruction(nary_op{}, i1, c1.back());
Paul's avatar
Paul committed
553
554

    auto c2      = chain(p, 2, unary_op{}, binary1);
555
556
    auto i2      = mm->add_instruction(unary_op{}, binary1);
    auto binary2 = mm->add_instruction(nary_op{}, i2, c2.back());
Paul's avatar
Paul committed
557

558
    t.run_pass(p);
Paul's avatar
Paul committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
    EXPECT(not t.has_stream(one));

    EXPECT(t.get_stream(i1) != t.get_stream(c1.back()));
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == t.get_stream(c1.back()));
    EXPECT(t.get_stream(binary1) == t.get_stream(c1.back()));
    EXPECT(get_wait_for(binary1) ==
           get_wait_for(t.get_stream(binary1), {t.get_stream(c1.back()), t.get_stream(i1)}));
    t.check_conflicts(p, {c1, {i1}});

    EXPECT(t.get_stream(i2) != t.get_stream(c2.back()));
    for(auto ins : c2)
        EXPECT(t.get_stream(ins) == t.get_stream(c2.back()));
    EXPECT(t.get_stream(binary2) == 0);
    EXPECT(get_wait_for(binary2) ==
           get_wait_for(t.get_stream(binary2), {t.get_stream(c2.back()), t.get_stream(i2)}));
    t.check_conflicts(p, {c2, {i2}});
}

TEST_CASE(par_merge)
{
580
    scheduler t{};
Paul's avatar
Paul committed
581
    migraphx::program p;
582
583
584
585

    auto* mm     = p.get_main_module();
    auto one     = mm->add_literal(1);
    auto start1  = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
586
    auto c1      = chain(p, 3, unary_op{}, start1);
587
588
    auto i1      = mm->add_instruction(unary_op{}, start1);
    auto binary1 = mm->add_instruction(nary_op{}, i1, c1.back());
Paul's avatar
Paul committed
589

590
    auto start2  = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
591
    auto c2      = chain(p, 2, unary_op{}, start2);
592
593
    auto i2      = mm->add_instruction(unary_op{}, start2);
    auto binary2 = mm->add_instruction(nary_op{}, i2, c2.back());
Paul's avatar
Paul committed
594

595
    auto binary3 = mm->add_instruction(nary_op{}, binary1, binary2);
Paul's avatar
Paul committed
596

597
    t.run_pass(p);
Paul's avatar
Paul committed
598
599
600
601
602
603
604
605
606
607
608
609
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(binary3) == 0);

    EXPECT(t.get_stream(i1) != t.get_stream(i2));
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == 0);
    EXPECT(t.get_stream(binary1) == 0);
    EXPECT(get_wait_for(binary1) ==
           get_wait_for(t.get_stream(binary1), {t.get_stream(c1.back()), t.get_stream(i1)}));
    t.check_conflicts(p, {c1, {i1}});

    for(auto ins : c2)
Paul's avatar
Paul committed
610
611
612
        EXPECT(t.get_stream(ins) == t.get_stream(binary2));
    EXPECT(t.get_stream(binary2) != t.get_stream(i1));
    EXPECT(t.get_stream(binary2) != t.get_stream(i2));
Paul's avatar
Paul committed
613
614
615
616
617
618
619
620
621
622
    EXPECT(get_wait_for(binary2) ==
           get_wait_for(t.get_stream(binary2), {t.get_stream(c2.back()), t.get_stream(i2)}));
    t.check_conflicts(p, {c2, {i2}});

    EXPECT(check_conflicts(p, binary1, binary2));
    t.check_conflicts(p, {c1, {i1}, c2, {i2}});
}

TEST_CASE(inner_par_merge)
{
623
    scheduler t{};
Paul's avatar
Paul committed
624
    migraphx::program p;
625
626
627
628

    auto* mm     = p.get_main_module();
    auto one     = mm->add_literal(1);
    auto start1  = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
629
    auto c1      = chain(p, 3, unary_op{}, start1);
630
631
    auto i1      = mm->add_instruction(unary_op{}, start1);
    auto binary1 = mm->add_instruction(nary_op{}, i1, c1.back());
Paul's avatar
Paul committed
632

633
    auto start2  = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
634
    auto c2      = chain(p, 2, unary_op{}, start2);
635
636
    auto i2      = mm->add_instruction(unary_op{}, start2);
    auto binary2 = mm->add_instruction(nary_op{}, i2, c2.back());
Paul's avatar
Paul committed
637

638
639
    auto outer1 = mm->add_instruction(unary_op{}, one);
    auto outer2 = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
640

641
    auto output = mm->add_instruction(nary_op{}, binary1, binary2, outer1, outer2);
Paul's avatar
Paul committed
642

643
    t.run_pass(p);
Paul's avatar
Paul committed
644
645
646
647
648
649
650
651
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(output) == 0);
    EXPECT(get_wait_for(output) == get_wait_for(t.get_stream(output),
                                                {t.get_stream(binary1),
                                                 t.get_stream(binary2),
                                                 t.get_stream(outer1),
                                                 t.get_stream(outer2)}));

652
653
654
    EXPECT(t.get_stream(outer1) != t.get_stream(outer2));
    EXPECT(migraphx::contains({1, 2}, t.get_stream(outer1)));
    EXPECT(migraphx::contains({1, 2}, t.get_stream(outer2)));
Paul's avatar
Paul committed
655
656
657
658
659
660
661
662
663
664

    EXPECT(t.get_stream(i1) != t.get_stream(i2));
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == 0);
    EXPECT(t.get_stream(binary1) == 0);
    EXPECT(get_wait_for(binary1) ==
           get_wait_for(t.get_stream(binary1), {t.get_stream(c1.back()), t.get_stream(i1)}));
    t.check_conflicts(p, {c1, {i1}});

    for(auto ins : c2)
Paul's avatar
Paul committed
665
666
667
        EXPECT(t.get_stream(ins) == t.get_stream(binary2));
    EXPECT(t.get_stream(binary2) != t.get_stream(i1));
    EXPECT(t.get_stream(binary2) != t.get_stream(i2));
Paul's avatar
Paul committed
668
669
670
671
672
673
674
675
676
677
    EXPECT(get_wait_for(binary2) ==
           get_wait_for(t.get_stream(binary2), {t.get_stream(c2.back()), t.get_stream(i2)}));
    t.check_conflicts(p, {c2, {i2}});

    EXPECT(check_conflicts(p, binary1, binary2));
    t.check_conflicts(p, {c1, {i1}, c2, {i2}, {outer1}, {outer2}});
}

TEST_CASE(par_merge_multi_entry)
{
678
    scheduler t{};
Paul's avatar
Paul committed
679
    migraphx::program p;
680
681
682
683

    auto* mm     = p.get_main_module();
    auto one     = mm->add_literal(1);
    auto start1  = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
684
    auto c1      = chain(p, 3, unary_op{}, start1);
685
686
    auto i1      = mm->add_instruction(unary_op{}, start1);
    auto binary1 = mm->add_instruction(nary_op{}, i1, c1.back());
Paul's avatar
Paul committed
687

688
689
    auto two     = mm->add_literal(1);
    auto start2  = mm->add_instruction(unary_op{}, two);
Paul's avatar
Paul committed
690
    auto c2      = chain(p, 2, unary_op{}, start2);
691
692
    auto i2      = mm->add_instruction(unary_op{}, start2);
    auto binary2 = mm->add_instruction(nary_op{}, i2, c2.back());
Paul's avatar
Paul committed
693

694
    auto binary3 = mm->add_instruction(nary_op{}, binary1, binary2);
Paul's avatar
Paul committed
695

696
    t.run_pass(p);
Paul's avatar
Paul committed
697
698
699
700
701
702
703
704
705
706
707
708
709
    EXPECT(not t.has_stream(one));
    EXPECT(not t.has_stream(two));
    EXPECT(t.get_stream(binary3) == 0);

    EXPECT(t.get_stream(i1) != t.get_stream(i2));
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == 0);
    EXPECT(t.get_stream(binary1) == 0);
    EXPECT(get_wait_for(binary1) ==
           get_wait_for(t.get_stream(binary1), {t.get_stream(c1.back()), t.get_stream(i1)}));
    t.check_conflicts(p, {c1, {i1}});

    for(auto ins : c2)
Paul's avatar
Paul committed
710
711
712
        EXPECT(t.get_stream(ins) == t.get_stream(binary2));
    EXPECT(t.get_stream(binary2) != t.get_stream(i1));
    EXPECT(t.get_stream(binary2) != t.get_stream(i2));
Paul's avatar
Paul committed
713
714
715
716
717
718
719
720
721
722
    EXPECT(get_wait_for(binary2) ==
           get_wait_for(t.get_stream(binary2), {t.get_stream(c2.back()), t.get_stream(i2)}));
    t.check_conflicts(p, {c2, {i2}});

    EXPECT(check_conflicts(p, binary1, binary2));
    t.check_conflicts(p, {c1, {i1}, c2, {i2}});
}

TEST_CASE(inner_split1)
{
723
    scheduler t{};
Paul's avatar
Paul committed
724
    migraphx::program p;
725
726
727

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
Paul's avatar
Paul committed
728
    auto c1     = chain(p, 2, unary_op{}, one);
729
730
731
732
    auto i1     = mm->add_instruction(unary_op{}, one);
    auto s1     = mm->add_instruction(unary_op{}, c1);
    auto s2     = mm->add_instruction(unary_op{}, c1);
    auto output = mm->add_instruction(nary_op{}, i1, s1, s2);
733
    t.run_pass(p);
Paul's avatar
Paul committed
734
735
736
737
738
739
740
741
742
743
744
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(i1) != t.get_stream(s1));
    EXPECT(t.get_stream(i1) != t.get_stream(s2));
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) != t.get_stream(i1));
    EXPECT(t.get_stream(s1) != t.get_stream(s2));

    EXPECT(t.get_stream(output) == 0);
    EXPECT(
        get_wait_for(output) ==
        get_wait_for(t.get_stream(output), {t.get_stream(i1), t.get_stream(s1), t.get_stream(s2)}));
745
746
    // Either s1 or s2 has a wait depending on the sort order but not both
    EXPECT(get_wait_for(s1).empty() xor get_wait_for(s2).empty());
Paul's avatar
Paul committed
747
748
749
750
751
    t.check_conflicts(p, {c1, {i1}, {s1}, {s2}});
}

TEST_CASE(inner_split2)
{
752
    scheduler t{};
Paul's avatar
Paul committed
753
    migraphx::program p;
754
755
756

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
Paul's avatar
Paul committed
757
    auto c1     = chain(p, 2, unary_op{}, one);
758
    auto i1     = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
759
760
    auto s1     = chain(p, 3, unary_op{}, c1.back());
    auto s2     = chain(p, 4, unary_op{}, c1.back());
761
    auto output = mm->add_instruction(nary_op{}, i1, s1.back(), s2.back());
762
    t.run_pass(p);
Paul's avatar
Paul committed
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(i1) != t.get_stream(s1.back()));
    EXPECT(t.get_stream(i1) != t.get_stream(s2.back()));
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) != t.get_stream(i1));
    EXPECT(t.get_stream(s1.back()) != t.get_stream(s2.back()));

    EXPECT(t.get_stream(output) == 0);
    EXPECT(get_wait_for(output) ==
           get_wait_for(t.get_stream(output),
                        {t.get_stream(i1), t.get_stream(s1.back()), t.get_stream(s2.back())}));
    EXPECT(get_wait_for(s1.front()) == get_wait_for({t.get_stream(c1.back())}));
    t.check_conflicts(p, {c1, {i1}, s1, s2});
}

TEST_CASE(inception_resnet)
{
780
    scheduler t{};
Paul's avatar
Paul committed
781
    migraphx::program p;
782
783
784
785

    auto* mm    = p.get_main_module();
    auto one    = mm->add_literal(1);
    auto input  = mm->add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
786
    auto c1     = chain(p, 2, unary_op{}, input);
787
788
789
    auto i1     = mm->add_instruction(unary_op{}, input);
    auto binary = mm->add_instruction(nary_op{}, i1, c1.back());
    auto output = mm->add_instruction(nary_op{}, binary, input);
790
    t.run_pass(p);
Paul's avatar
Paul committed
791
792
793
794
795
796
797
798
799
800
801
802
803
804
    EXPECT(not t.has_stream(one));
    EXPECT(t.get_stream(i1) != 0);
    for(auto ins : c1)
        EXPECT(t.get_stream(ins) == 0);
    EXPECT(t.get_stream(binary) == 0);
    EXPECT(get_wait_for(binary) ==
           get_wait_for(t.get_stream(binary), {t.get_stream(c1.back()), t.get_stream(i1)}));
    EXPECT(t.get_stream(output) == 0);
    EXPECT(get_wait_for(output).empty());
    t.check_conflicts(p, {c1, {i1}});
}

TEST_CASE(inception1)
{
805
    scheduler t{};
Paul's avatar
Paul committed
806
807
    migraphx::program p;

808
809
810
811
812
813
814
815
    auto* mm = p.get_main_module();

    auto i1     = mm->add_literal(0);
    auto i2     = mm->add_literal(1);
    auto i3     = mm->add_literal(1);
    auto i4     = mm->add_literal(2);
    auto i7     = mm->add_instruction(nary_op{"i7"}, i1, i4, i3, i2);
    auto i8     = mm->add_literal(2);
816
    auto i9     = mm->add_instruction(migraphx::make_op("identity"), i8);
817
818
819
    auto i10    = mm->add_literal(1);
    auto i11    = mm->add_instruction(nary_op{"i11"}, i7, i9, i10);
    auto i12    = mm->add_literal(2);
820
    auto i13    = mm->add_instruction(migraphx::make_op("identity"), i12);
821
822
823
824
825
    auto i14    = mm->add_literal(1);
    auto i15    = mm->add_literal(1);
    auto i16    = mm->add_literal(2);
    auto i17    = mm->add_instruction(nary_op{"i17"}, i11, i16, i15, i13, i14);
    auto i18    = mm->add_literal(2);
826
    auto i19    = mm->add_instruction(migraphx::make_op("identity"), i18);
827
828
829
830
831
832
833
    auto i20    = mm->add_literal(1);
    auto i21    = mm->add_literal(1);
    auto i22    = mm->add_literal(2);
    auto i23    = mm->add_instruction(nary_op{"i23"}, i17, i22, i21, i19, i20);
    auto i24    = mm->add_literal(1);
    auto i25    = mm->add_instruction(nary_op{"i25"}, i23, i24);
    auto i26    = mm->add_literal(2);
834
    auto i27    = mm->add_instruction(migraphx::make_op("identity"), i26);
835
836
837
838
839
    auto i28    = mm->add_literal(1);
    auto i29    = mm->add_literal(1);
    auto i30    = mm->add_literal(2);
    auto i31    = mm->add_instruction(nary_op{"i31"}, i25, i30, i29, i27, i28);
    auto i32    = mm->add_literal(2);
840
    auto i33    = mm->add_instruction(migraphx::make_op("identity"), i32);
841
842
843
844
845
846
847
    auto i34    = mm->add_literal(1);
    auto i35    = mm->add_literal(1);
    auto i36    = mm->add_literal(2);
    auto i37    = mm->add_instruction(nary_op{"i37"}, i31, i36, i35, i33, i34);
    auto i38    = mm->add_literal(1);
    auto i39    = mm->add_instruction(nary_op{"i39"}, i37, i38);
    auto i41    = mm->add_literal(2);
848
    auto i42    = mm->add_instruction(migraphx::make_op("identity"), i41);
849
850
851
852
853
    auto i43    = mm->add_literal(1);
    auto i44    = mm->add_literal(1);
    auto i45    = mm->add_literal(2);
    auto i48    = mm->add_instruction(nary_op{"i48"}, i39, i45, i44, i42, i43);
    auto i49    = mm->add_literal(2);
854
    auto i50    = mm->add_instruction(migraphx::make_op("identity"), i49);
855
856
857
858
859
    auto i51    = mm->add_literal(1);
    auto i52    = mm->add_literal(1);
    auto i53    = mm->add_literal(2);
    auto i54    = mm->add_instruction(nary_op{"i54"}, i48, i53, i52, i50, i51);
    auto i55    = mm->add_literal(1);
860
    auto i56    = mm->add_instruction(migraphx::make_op("identity"), i55);
861
    auto i57    = mm->add_literal(2);
862
    auto i58    = mm->add_instruction(migraphx::make_op("identity"), i57);
863
864
865
866
    auto i59    = mm->add_literal(1);
    auto i60    = mm->add_literal(2);
    auto i61    = mm->add_instruction(nary_op{"i61"}, i54, i60, i59, i58, i56);
    auto i62    = mm->add_literal(2);
867
    auto i63    = mm->add_instruction(migraphx::make_op("identity"), i62);
868
869
870
871
    auto i64    = mm->add_literal(1);
    auto i65    = mm->add_literal(1);
    auto i66    = mm->add_literal(2);
    auto i69    = mm->add_instruction(nary_op{"i69"}, i39, i66, i65, i63, i64);
872
    auto i70    = mm->add_instruction(migraphx::make_op("identity"), i55);
873
    auto i71    = mm->add_literal(2);
874
    auto i72    = mm->add_instruction(migraphx::make_op("identity"), i71);
875
876
877
878
879
    auto i73    = mm->add_literal(1);
    auto i74    = mm->add_literal(2);
    auto i75    = mm->add_instruction(nary_op{"i75"}, i69, i74, i73, i72, i70);
    auto i77    = mm->add_literal(1);
    auto i80    = mm->add_instruction(nary_op{"i80"}, i39, i77);
880
    auto i81    = mm->add_instruction(migraphx::make_op("identity"), i55);
881
    auto i82    = mm->add_literal(2);
882
    auto i83    = mm->add_instruction(migraphx::make_op("identity"), i82);
883
884
885
    auto i84    = mm->add_literal(1);
    auto i85    = mm->add_literal(2);
    auto i86    = mm->add_instruction(nary_op{"i86"}, i80, i85, i84, i83, i81);
886
    auto i88    = mm->add_instruction(migraphx::make_op("identity"), i55);
887
    auto i89    = mm->add_literal(2);
888
    auto i90    = mm->add_instruction(migraphx::make_op("identity"), i89);
889
890
891
    auto i91    = mm->add_literal(1);
    auto i92    = mm->add_literal(2);
    auto i94    = mm->add_instruction(nary_op{"i94"}, i39, i92, i91, i90, i88);
892
    auto i96    = mm->add_instruction(migraphx::make_op("identity"), i55, i94, i75, i61, i86);
893
    auto i97    = mm->add_literal(2);
894
    auto i98    = mm->add_instruction(migraphx::make_op("identity"), i97);
895
896
897
898
    auto i99    = mm->add_literal(3);
    auto i100   = mm->add_literal(1);
    auto i101   = mm->add_literal(2);
    auto output = mm->add_instruction(nary_op{"output"}, i96, i101, i100, i98, i99);
Paul's avatar
Paul committed
899

900
    t.run_pass(p);
Paul's avatar
Paul committed
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926

    EXPECT(t.get_streams({i7, i11, i17, i23, i25, i31, i37, i39}) ==
           t.get_streams({i7, i7, i7, i7, i7, i7, i7, i7}));
    EXPECT(t.get_streams({i48, i54, i61, output}) ==
           t.get_streams({output, output, output, output}));
    EXPECT(t.get_streams({i80, i86}) == t.get_streams({i80, i80}));
    EXPECT(t.get_streams({i69, i75}) == t.get_streams({i69, i69}));

    EXPECT(t.get_stream(i7) != t.get_stream(i80));
    EXPECT(t.get_stream(i69) != t.get_stream(i80));
    EXPECT(t.get_stream(i69) != t.get_stream(i7));
    EXPECT(t.get_stream(output) != t.get_stream(i69));
    EXPECT(t.get_stream(output) != t.get_stream(i80));

    EXPECT(get_wait_for(i80) == get_wait_for({t.get_stream(i39)}));
    EXPECT(get_wait_for(i69) == get_wait_for({t.get_stream(i39)}));
    EXPECT(get_wait_for(i94) == get_wait_for({t.get_stream(i39)}));
    EXPECT(
        get_wait_for(output) ==
        get_wait_for(t.get_stream(output),
                     {t.get_stream(i94), t.get_stream(i75), t.get_stream(i61), t.get_stream(i86)}));

    t.check_conflicts(p, {{i80, i86}, {i69, i75}, {i48, i54, i61}, {i94}});
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }