"results/sync_overhead.py" did not exist on "a7c9986148e1182dee4033de02084b9ccb7957ce"
schedule_model.cpp 3.7 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include <migraphx/gpu/schedule_model.hpp>
#include <migraphx/gpu/context.hpp>
3
#include <migraphx/register_op.hpp>
Paul's avatar
Paul committed
4
5
6
7
8
9
10
11
12
13
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operation.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

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

    argument compute(context& ctx, const shape&, const std::vector<argument>&) const
    {
        ctx.get_stream().record(ctx.get_event(event));
        return {};
    }

29
    void finalize(context& ctx, const shape&, const std::vector<shape>&) const
Paul's avatar
Paul committed
30
31
32
33
34
35
36
    {
        ctx.create_events(event);
    }
};

struct wait_event
{
Shucai Xiao's avatar
Shucai Xiao committed
37
    int event = 0;
Paul's avatar
Paul committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    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 {};
    }
};

struct set_stream
{
Shucai Xiao's avatar
Shucai Xiao committed
55
    int stream = 0;
Paul's avatar
Paul committed
56
57
58
59
60
61
62
63
64
65
66
67
68
    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 {};
    }
69
70
71
72
    void finalize(context& ctx, const shape&, const std::vector<shape>&) const
    {
        ctx.set_stream(stream);
    }
Paul's avatar
Paul committed
73
74
};

75
76
77
78
MIGRAPHX_REGISTER_OP(record_event)
MIGRAPHX_REGISTER_OP(wait_event)
MIGRAPHX_REGISTER_OP(set_stream)

Shucai Xiao's avatar
Shucai Xiao committed
79
80
int schedule_model::concurrency() const { return streams; }
void schedule_model::sched(module& p, instruction_ref ins, int n) const
Paul's avatar
Paul committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{
    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()))
    {
        auto&& op = any_cast<set_stream>(last_stream->get_operator());
        // If the same stream was set earlier then skip
        if(op.stream == n)
            return;
    }
    p.insert_instruction(ins, set_stream{n});
}

Shucai Xiao's avatar
Shucai Xiao committed
95
void schedule_model::wait(module& p, instruction_ref ins, int wait_id) const
Paul's avatar
Paul committed
96
97
98
{
    p.insert_instruction(ins, wait_event{wait_id});
}
Shucai Xiao's avatar
Shucai Xiao committed
99
void schedule_model::record(module& p, instruction_ref ins, int wait_id) const
Paul's avatar
Paul committed
100
101
102
103
{
    p.insert_instruction(std::next(ins), record_event{wait_id});
}

Shucai Xiao's avatar
Shucai Xiao committed
104
static std::unordered_map<std::string, int> create_weight_map()
Paul's avatar
Paul committed
105
{
kahmed10's avatar
kahmed10 committed
106
    return {{"hip::load_literal", 0},
107
            {"hip::hip_allocate_memory", 0},
108
            {"hip::hip_load_memory", 0},
kahmed10's avatar
kahmed10 committed
109
            {"hip::allocate", 0},
110
111
112
113
            {"gpu::convolution", 8},
            {"gpu::conv_bias_relu", 8},
            {"gpu::pooling", 4},
            {"gpu::gemm", 4}};
Paul's avatar
Paul committed
114
115
}

Shucai Xiao's avatar
Shucai Xiao committed
116
static const std::unordered_map<std::string, int>& weight_map()
Paul's avatar
Paul committed
117
{
Shucai Xiao's avatar
Shucai Xiao committed
118
    static const std::unordered_map<std::string, int> m = create_weight_map();
Paul's avatar
Paul committed
119
120
121
    return m;
}

Shucai Xiao's avatar
Shucai Xiao committed
122
int schedule_model::weight(const operation& op) const
Paul's avatar
Paul committed
123
124
125
{
    if(weight_map().count(op.name()) == 0)
    {
126
        return 2;
Paul's avatar
Paul committed
127
128
129
130
131
132
133
    }
    return weight_map().at(op.name());
}

} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx