schedule_model.cpp 3.58 KB
Newer Older
Paul's avatar
Paul committed
1
#include <migraphx/gpu/schedule_model.hpp>
Paul's avatar
Paul committed
2
#include <migraphx/gpu/context.hpp>
Paul's avatar
Paul committed
3
#include <migraphx/program.hpp>
4
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
5
6
7
8
9
10
#include <migraphx/operation.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

Paul's avatar
Paul committed
11
struct record_event
Paul's avatar
Paul committed
12
{
Paul's avatar
Paul committed
13
    std::size_t event = 0;
Paul's avatar
Paul committed
14
15
16
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
Paul's avatar
Paul committed
17
        return pack(f(self.event, "event"));
Paul's avatar
Paul committed
18
    }
Paul's avatar
Paul committed
19
    std::string name() const { return "gpu::record_event"; }
Paul's avatar
Paul committed
20
21
22
23
    shape compute_shape(const std::vector<shape>&) const { return {}; }

    argument compute(context& ctx, const shape&, const std::vector<argument>&) const
    {
Paul's avatar
Paul committed
24
        ctx.get_stream().record(ctx.get_event(event));
Paul's avatar
Paul committed
25
26
27
        return {};
    }

Paul's avatar
Paul committed
28
    void finalize(context& ctx, const shape&, std::vector<shape>)
Paul's avatar
Paul committed
29
    {
Paul's avatar
Paul committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        ctx.create_events(event);
    }
};

struct wait_event
{
    std::size_t event = 0;
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.event, "event"));
    }
    std::string name() const { return "gpu::wait_event"; }
    shape compute_shape(const std::vector<shape>&) const { return {}; }

    argument compute(context& ctx, const shape&, const std::vector<argument>&) const
    {
        ctx.get_stream().wait(ctx.get_event(event));
        return {};
Paul's avatar
Paul committed
49
    }
Paul's avatar
Paul committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
};

struct set_stream
{
    std::size_t stream = 0;
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.stream, "stream"));
    }
    std::string name() const { return "gpu::set_stream"; }
    shape compute_shape(const std::vector<shape>&) const { return {}; }

    argument compute(context& ctx, const shape&, const std::vector<argument>&) const
    {
        ctx.set_stream(stream);
        return {};
    }
    void finalize(context& ctx, const shape&, const std::vector<shape>&) { ctx.set_stream(stream); }
};

71
std::size_t schedule_model::concurrency() const { return streams; }
Paul's avatar
Paul committed
72
void schedule_model::sched(program& p, instruction_ref ins, std::size_t n) const
Paul's avatar
Paul committed
73
{
Paul's avatar
Paul committed
74
75
76
77
    auto last_stream = std::find_if(std::make_reverse_iterator(ins),
                                    std::make_reverse_iterator(p.begin()),
                                    [&](auto&& i) { return i.name() == "gpu::set_stream"; });
    if(last_stream != std::make_reverse_iterator(p.begin()))
78
79
80
    {
        auto&& op = any_cast<set_stream>(last_stream->get_operator());
        // If the same stream was set earlier then skip
Paul's avatar
Paul committed
81
        if(op.stream == n)
82
83
            return;
    }
Paul's avatar
Paul committed
84
85
    p.insert_instruction(ins, set_stream{n});
}
Paul's avatar
Paul committed
86
87
88
89
90
91

void schedule_model::wait(program& p, instruction_ref ins, std::size_t wait_id) const
{
    p.insert_instruction(ins, wait_event{wait_id});
}
void schedule_model::record(program& p, instruction_ref ins, std::size_t wait_id) const
Paul's avatar
Paul committed
92
{
Paul's avatar
Paul committed
93
    p.insert_instruction(std::next(ins), record_event{wait_id});
Paul's avatar
Paul committed
94
95
96
97
98
}

static std::unordered_map<std::string, std::size_t> create_weight_map()
{
    return {
Paul's avatar
Paul committed
99
        {"hip::load_literal", 0},
Paul's avatar
Paul committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        {"hip::allocate", 0},
        {"gpu::convolution", 4},
        {"gpu::conv_bias_relu", 4},
        {"gpu::pooling", 2},
        {"gpu::gemm", 2},
        {"gpu::concat", 1},
        {"hip::add_relu", 2},
    };
}

static const std::unordered_map<std::string, std::size_t>& weight_map()
{
    static std::unordered_map<std::string, std::size_t> m = create_weight_map();
    return m;
}

Paul's avatar
Paul committed
116
std::size_t schedule_model::weight(const operation& op) const
Paul's avatar
Paul committed
117
{
Paul's avatar
Paul committed
118
    if(weight_map().count(op.name()) == 0)
119
120
121
    {
        return 1;
    }
Paul's avatar
Paul committed
122
    return weight_map().at(op.name());
Paul's avatar
Paul committed
123
124
125
126
}

} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
127
} // namespace migraphx