schedule_test.cpp 13.5 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
    {
Paul's avatar
Paul committed
65
        assert(not wait_for.empty());
Paul's avatar
Paul committed
66
67
68
69
        return {};
    }
};

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

struct schedule_model_test
{
    instruction_map* ins2stream;
Paul's avatar
Paul committed
75
76
    std::size_t concurrency() const { return 4; }
    void
Paul's avatar
Paul committed
77
    schedule_instruction(migraphx::program&, migraphx::instruction_ref ins, std::size_t n) const
Paul's avatar
Paul committed
78
79
80
81
82
    {
        (*ins2stream)[ins] = n;
    }
    void wait(migraphx::program& p,
              migraphx::instruction_ref ins,
Paul's avatar
Paul committed
83
              std::size_t,
Paul's avatar
Paul committed
84
85
              const std::vector<std::size_t>& wait_for) const
    {
Paul's avatar
Paul committed
86
        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
        if(not migraphx::contains(ins->inputs(), y))
Paul's avatar
Paul committed
117
118
            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
146
147
148
149
150
151
152
153
154
155
156
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)
{
    std::sort(wait_for.begin(), wait_for.end());
    return wait_for;
}

Paul's avatar
Paul committed
157
158
159
160
161
162
163
164
165
166
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
167
    if(wait_ins->name() != "wait_event")
Paul's avatar
Paul committed
168
169
170
171
172
173
        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
174
175
176
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
177
178
{
    std::vector<migraphx::instruction_ref> result;
Paul's avatar
Paul committed
179
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
180
181
182
183
184
185
186
    {
        result.push_back(p.add_instruction(x, input));
        input = result.back();
    }
    return result;
}

Paul's avatar
Paul committed
187
188
189
190
191
TEST_CASE(single_entry)
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
Paul's avatar
Paul committed
192
193
    auto onep1  = p.add_instruction(unary_op{}, one);
    auto onep2  = p.add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
194
    auto binary = p.add_instruction(nary_op{}, onep1, onep2);
Paul's avatar
Paul committed
195
    p.compile(schedule_target{&stream});
Paul's avatar
Paul committed
196
    EXPECT(stream.count(one) == 0);
Paul's avatar
Paul committed
197
198
199
200
201
202
    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));
}

Paul's avatar
Paul committed
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
TEST_CASE(zero_merge1)
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
    auto onep1  = p.add_instruction(unary_op{}, one);
    auto onep2  = p.add_instruction(unary_op{}, one);
    auto binary = p.add_instruction(migraphx::op::identity{}, onep1, onep2);
    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.at(onep1) != stream.at(onep2));
    // No stream assignment
    EXPECT(stream.count(binary) == 0);
    // There is no wait
    EXPECT(get_wait_for(binary).empty());
    EXPECT(check_conflicts(p, onep1, onep2));
}

TEST_CASE(zero_merge2)
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
    auto onep1  = p.add_instruction(unary_op{}, one);
    auto onep2  = p.add_instruction(unary_op{}, one);
    auto binary = p.add_instruction(migraphx::op::identity{}, p.add_instruction(migraphx::op::identity{}, onep1), p.add_instruction(migraphx::op::identity{}, onep2));
    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.at(onep1) != stream.at(onep2));
    // No stream assignment
    EXPECT(stream.count(binary) == 0);
    // There is no wait
    EXPECT(get_wait_for(binary).empty());
    EXPECT(check_conflicts(p, onep1, onep2));
}

Paul's avatar
Paul committed
239
TEST_CASE(double_entry)
Paul's avatar
Paul committed
240
{
Paul's avatar
Paul committed
241
    instruction_map stream;
Paul's avatar
Paul committed
242
    migraphx::program p;
Paul's avatar
Paul committed
243
244
245
246
    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
247
    auto binary = p.add_instruction(nary_op{}, onep, twop);
Paul's avatar
Paul committed
248
    p.compile(schedule_target{&stream});
Paul's avatar
Paul committed
249
250
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.count(two) == 0);
Paul's avatar
Paul committed
251
252
253
    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
254
255
256
    // EXPECT(check_conflicts(p, onep, twop));
}

Paul's avatar
Paul committed
257
TEST_CASE(two_branches)
Paul's avatar
Paul committed
258
259
260
261
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
Paul's avatar
Paul committed
262
263
    auto c1     = chain(p, 2, unary_op{}, one);
    auto i1     = p.add_instruction(unary_op{}, one);
Paul's avatar
Paul committed
264
265
266
267
    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
268
    for(auto ins : c1)
Paul's avatar
Paul committed
269
270
271
272
273
274
        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
275
TEST_CASE(four_branches)
Paul's avatar
Paul committed
276
277
278
279
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
Paul's avatar
Paul committed
280
281
282
283
    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
284
    auto binary = p.add_instruction(nary_op{}, i1, c1.back(), c2.back(), c3.back());
Paul's avatar
Paul committed
285
286
287
    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(stream.at(i1) == 3);
Paul's avatar
Paul committed
288
289
290
291
292
293
    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
294
    EXPECT(stream.at(binary) == 0);
Paul's avatar
Paul committed
295
296
297
    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
298
    check_conflicts(p, {c1, c2, c3, {i1}});
Paul's avatar
Paul committed
299
300
}

Paul's avatar
Paul committed
301
TEST_CASE(five_branches)
Paul's avatar
Paul committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
{
    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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
TEST_CASE(four_branches_eq)
{
    instruction_map stream;
    migraphx::program p;
    auto one    = p.add_literal(1);
    auto onep1  = p.add_instruction(unary_op{}, one);
    auto onep2  = p.add_instruction(unary_op{}, one);
    auto onep3  = p.add_instruction(unary_op{}, one);
    auto onep4  = p.add_instruction(unary_op{}, one);
    auto binary = p.add_instruction(nary_op{}, onep1, onep2, onep3, onep4);
    p.compile(schedule_target{&stream});
    EXPECT(stream.count(one) == 0);
    EXPECT(sorted<std::size_t>({stream.at(onep1), stream.at(onep2), stream.at(onep3), stream.at(onep4)}) == unique<std::size_t>({stream.at(onep1), stream.at(onep2), stream.at(onep3), stream.at(onep4)}));
    EXPECT(stream.at(binary) == 0);
    EXPECT(get_wait_for(binary) == get_wait_for(stream[binary], {stream[onep1], stream[onep2], stream[onep3], stream[onep4]}));
    check_conflicts(p, {{onep1}, {onep2}, {onep3}, {onep4}});
}

Paul's avatar
Paul committed
349
350
351
352
TEST_CASE(seq_merge)
{
    instruction_map stream;
    migraphx::program p;
Paul's avatar
Paul committed
353
354
355
    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
356
357
    auto binary1 = p.add_instruction(nary_op{}, i1, c1.back());

Paul's avatar
Paul committed
358
359
    auto c2      = chain(p, 2, unary_op{}, binary1);
    auto i2      = p.add_instruction(unary_op{}, binary1);
Paul's avatar
Paul committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
    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
384
385
386
387
    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
388
389
    auto binary1 = p.add_instruction(nary_op{}, i1, c1.back());

Paul's avatar
Paul committed
390
391
392
    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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
    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
418
int main(int argc, const char* argv[]) { test::run(argc, argv); }