schedule_model.cpp 4.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#include <migraphx/gpu/schedule_model.hpp>
#include <migraphx/gpu/context.hpp>
26
#include <migraphx/register_op.hpp>
Paul's avatar
Paul committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/operation.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

struct record_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::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 {};
    }

52
    void finalize(context& ctx, const shape&, const std::vector<shape>&) const
Paul's avatar
Paul committed
53
54
55
56
57
58
59
60
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
    {
        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 {};
    }
};

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 {};
    }
92
93
94
95
    void finalize(context& ctx, const shape&, const std::vector<shape>&) const
    {
        ctx.set_stream(stream);
    }
Paul's avatar
Paul committed
96
97
};

98
99
100
101
MIGRAPHX_REGISTER_OP(record_event)
MIGRAPHX_REGISTER_OP(wait_event)
MIGRAPHX_REGISTER_OP(set_stream)

Paul's avatar
Paul committed
102
std::size_t schedule_model::concurrency() const { return streams; }
103
void schedule_model::sched(module& m, instruction_ref ins, std::size_t n) const
Paul's avatar
Paul committed
104
105
{
    auto last_stream = std::find_if(std::make_reverse_iterator(ins),
106
                                    std::make_reverse_iterator(m.begin()),
Paul's avatar
Paul committed
107
                                    [&](auto&& i) { return i.name() == "gpu::set_stream"; });
108
    if(last_stream != std::make_reverse_iterator(m.begin()))
Paul's avatar
Paul committed
109
110
111
112
113
114
    {
        auto&& op = any_cast<set_stream>(last_stream->get_operator());
        // If the same stream was set earlier then skip
        if(op.stream == n)
            return;
    }
115
    m.insert_instruction(ins, set_stream{n});
Paul's avatar
Paul committed
116
117
}

118
void schedule_model::wait(module& m, instruction_ref ins, std::size_t wait_id) const
Paul's avatar
Paul committed
119
{
120
    m.insert_instruction(ins, wait_event{wait_id});
Paul's avatar
Paul committed
121
}
122
void schedule_model::record(module& m, instruction_ref ins, std::size_t wait_id) const
Paul's avatar
Paul committed
123
{
124
    m.insert_instruction(std::next(ins), record_event{wait_id});
Paul's avatar
Paul committed
125
126
127
128
}

static std::unordered_map<std::string, std::size_t> create_weight_map()
{
kahmed10's avatar
kahmed10 committed
129
    return {{"hip::load_literal", 0},
130
            {"hip::hip_allocate_memory", 0},
131
            {"hip::hip_load_memory", 0},
kahmed10's avatar
kahmed10 committed
132
            {"hip::allocate", 0},
133
134
135
136
            {"gpu::convolution", 8},
            {"gpu::conv_bias_relu", 8},
            {"gpu::pooling", 4},
            {"gpu::gemm", 4}};
Paul's avatar
Paul committed
137
138
139
140
}

static const std::unordered_map<std::string, std::size_t>& weight_map()
{
141
    static const std::unordered_map<std::string, std::size_t> m = create_weight_map();
Paul's avatar
Paul committed
142
143
144
145
146
147
148
    return m;
}

std::size_t schedule_model::weight(const operation& op) const
{
    if(weight_map().count(op.name()) == 0)
    {
149
        return 2;
Paul's avatar
Paul committed
150
151
152
153
154
155
156
    }
    return weight_map().at(op.name());
}

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