"examples/vision/python_unet/README.md" did not exist on "be6e7e9f16d94955a9b04e2f9ec3551fa1cae740"
write_literals.cpp 1.54 KB
Newer Older
Paul's avatar
Paul committed
1
#include <migraph/gpu/write_literals.hpp>
Paul's avatar
Paul committed
2
#include <migraph/iterator_for.hpp>
Paul's avatar
Paul committed
3
#include <migraph/gpu/hip.hpp>
Paul's avatar
Paul committed
4
#include <migraph/instruction.hpp>
5
#include <migraph/env.hpp>
Paul's avatar
Paul committed
6
7

namespace migraph {
8
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
9
namespace gpu {
Paul's avatar
Paul committed
10

11
12
MIGRAPH_DECLARE_ENV_VAR(MIGRAPH_COPY_LITERALS)

Paul's avatar
Paul committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct hip_load_literal
{
    shape s;
    std::size_t n = 0;
    std::string name() const { return "hip::load_literal"; }
    shape compute_shape(const std::vector<shape>& inputs) const
    {
        check_shapes{inputs}.has(0);
        return s;
    }
    argument compute(context& ctx, const shape&, const std::vector<argument>&) const
    {
        return ctx.literals.at(n);
    }
};

Paul's avatar
Paul committed
29
void write_literals::apply(program& p) const
Paul's avatar
Paul committed
30
{
Paul's avatar
Paul committed
31
    assert(ctx != nullptr);
Paul's avatar
Paul committed
32
33
    for(auto ins : iterator_for(p))
    {
Paul's avatar
Paul committed
34
        if(ins->name() == "@literal")
Paul's avatar
Paul committed
35
        {
36
37
            if(enabled(MIGRAPH_COPY_LITERALS{}))
            {
Paul's avatar
Paul committed
38
39
                literal l  = ins->get_literal();
                auto pre   = p.add_literal(l);
40
                auto alloc = p.insert_instruction(std::next(pre), hip_allocate{l.get_shape()});
41
                p.replace_instruction(ins, hip_copy{}, pre, alloc);
Paul's avatar
Paul committed
42
            }
43
44
45
46
47
48
49
            else
            {
                argument a    = to_gpu(ins->get_literal().get_argument());
                std::size_t n = ctx->literals.size();
                ctx->literals.push_back(a);
                p.replace_instruction(ins, hip_load_literal{a.get_shape(), n});
            }
Paul's avatar
Paul committed
50
51
52
        }
    }
}
53

Paul's avatar
Paul committed
54
} // namespace gpu
55
} // namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
56
} // namespace migraph