"vscode:/vscode.git/clone" did not exist on "80587d4c54776e08b6c00ef709de2115a6d1d758"
schedule_test.cpp 5.07 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <migraphx/schedule.hpp>
#include <migraphx/operators.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#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; }
};

struct binary_op
{
    std::string name() const { return "binary"; }
    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
48
49
50
51
52
53
54
55
56
57
58
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
59
60
61
    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>&) const
Paul's avatar
Paul committed
62
63
64
65
66
    {
        return {};
    }
};

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

struct schedule_model_test
{
    instruction_map* ins2stream;
Paul's avatar
Paul committed
72
73
    std::size_t concurrency() const { return 4; }
    void
Paul's avatar
Paul committed
74
    schedule_instruction(migraphx::program&, migraphx::instruction_ref ins, std::size_t n) const
Paul's avatar
Paul committed
75
76
77
78
79
80
81
82
    {
        (*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
83
84
        (*ins2stream)[ins] = wait_on;
        p.insert_instruction(ins, wait_event{wait_for});
Paul's avatar
Paul committed
85
86
87
    }
    std::size_t weight(const migraphx::operation& op) const
    {
Paul's avatar
Paul committed
88
        if(op.name() == "binary" or op.name() == "unary")
Paul's avatar
Paul committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
            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
108
    for(auto ins : migraphx::iterator_for(p))
Paul's avatar
Paul committed
109
    {
Paul's avatar
Paul committed
110
        if(ins->name() != "identity")
Paul's avatar
Paul committed
111
            continue;
Paul's avatar
Paul committed
112
        if(ins->inputs().size() != 2)
Paul's avatar
Paul committed
113
            continue;
Paul's avatar
Paul committed
114
        if(ins->inputs() == std::vector<migraphx::instruction_ref>{x, y})
Paul's avatar
Paul committed
115
            return true;
Paul's avatar
Paul committed
116
        if(ins->inputs() == std::vector<migraphx::instruction_ref>{y, x})
Paul's avatar
Paul committed
117
118
119
120
121
            return true;
    }
    return false;
}

Paul's avatar
Paul committed
122
123
124
125
126
127
128
129
130
131
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
132
    if(wait_ins->name() != "wait_event")
Paul's avatar
Paul committed
133
134
135
136
137
138
        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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
TEST_CASE(single_entry)
{
    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(binary_op{}, onep1, onep2);
    p.compile(schedule_target{&stream});
    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
155
{
Paul's avatar
Paul committed
156
    instruction_map stream;
Paul's avatar
Paul committed
157
    migraphx::program p;
Paul's avatar
Paul committed
158
159
160
161
    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
162
    auto binary = p.add_instruction(binary_op{}, onep, twop);
Paul's avatar
Paul committed
163
164
165
166
    p.compile(schedule_target{&stream});
    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
167
168
169
170
    EXPECT(check_conflicts(p, onep, twop));
}

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