"git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "dcc79d325b31b5c3e266cf9fd66272558f8d3d8a"
schedule.cpp 1.16 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
#include <migraphx/schedule.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/functional.hpp>
#include <migraphx/ranges.hpp>
#include <unordered_map>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

void schedule::apply(program& p) const
{
    // Compute accumulated weights
    std::unordered_map<instruction_ref, std::size_t> weights;
    auto last = std::prev(p.end());
    fix<std::size_t>([&](auto self, auto ins) -> std::size_t {
Paul's avatar
Paul committed
18
        if(weights.count(ins) == 0)
Paul's avatar
Paul committed
19
        {
Paul's avatar
Paul committed
20
21
22
23
24
            weights[ins] =
                std::accumulate(ins->inputs().begin(),
                                ins->inputs().end(),
                                model.weight(ins->get_operator()),
                                [&](std::size_t w, instruction_ref i) { return w + self(i); });
Paul's avatar
Paul committed
25
26
27
28
29
30
        }
        return weights[ins];
    })(last);

    // Topo sort
    fix([&](auto self, auto ins) {
Paul's avatar
Paul committed
31
        for(auto i : ins->inputs())
Paul's avatar
Paul committed
32
            p.move_instruction(i, p.begin());
Paul's avatar
Paul committed
33
        for(auto i : ins->inputs())
Paul's avatar
Paul committed
34
35
36
37
38
39
            self(i);
    })(last);
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx