eliminate_workspace.cpp 1.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <migraph/gpu/eliminate_workspace.hpp>
#include <migraph/gpu/hip.hpp>
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/ranges.hpp>
#include <migraph/stringutils.hpp>

namespace migraph {
namespace gpu {

void eliminate_workspace::apply(program& p) const
{
    std::size_t n = 0;
    std::vector<instruction_ref> allocs;
    for(auto ins : iterator_for(p))
    {
Paul's avatar
Paul committed
19
        if(ins->outputs().size() != 1)
20
            continue;
Paul's avatar
Paul committed
21
        if(ins->name() != "hip::allocate")
22
            continue;
23
        auto&& a = any_cast<hip_allocate>(ins->get_operator());
Paul's avatar
Paul committed
24
        if(a.tag == "workspace")
25
26
27
28
29
30
        {
            n = std::max(n, ins->get_shape().bytes());
            allocs.push_back(ins);
        }
    }
    auto ws = p.add_parameter("workspace", shape{shape::int8_type, {n}});
Paul's avatar
Paul committed
31
    for(auto&& a : allocs)
32
33
34
35
36
37
38
    {
        p.replace_instruction(a, ws);
        p.remove_instruction(a);
    }
}
} // namespace gpu
} // namespace migraph