propagate_constant.cpp 1.5 KB
Newer Older
Paul's avatar
Paul committed
1
#include <migraphx/propagate_constant.hpp>
Paul's avatar
Paul committed
2
3
4
#include <migraphx/program.hpp>
#include <migraphx/matcher.hpp>
#include <migraphx/literal.hpp>
Paul's avatar
Paul committed
5
#include <migraphx/functional.hpp>
6
#include <unordered_set>
Paul's avatar
Paul committed
7

Paul's avatar
Paul committed
8
namespace migraphx {
Paul's avatar
Paul committed
9
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
bool skip_propogate(instruction_ref ins)
Paul's avatar
Paul committed
12
{
Paul's avatar
Paul committed
13
    if(ins->name() == "@literal")
Paul's avatar
Paul committed
14
        return true;
Paul's avatar
Paul committed
15
16
    auto&& s = ins->get_shape();
    if(s.broadcasted() and not s.scalar())
Paul's avatar
Paul committed
17
        return true;
Paul's avatar
Paul committed
18
    if(s.scalar() and s.elements() != 1)
Paul's avatar
Paul committed
19
20
21
        return true;
    return false;
}
Paul's avatar
Paul committed
22

Paul's avatar
Paul committed
23
void propagate_constant::apply(program& p) const
Paul's avatar
Paul committed
24
{
25
26
27
28
29
30
31
32
33
    for(auto i:iterator_for(p))
    {
        if (i->name() != "@literal")
            continue;
        if (i->outputs().empty())
            continue;
        fix([&](auto self, auto ins) {
            std::unordered_set<instruction_ref> children(ins->outputs().begin(), ins->outputs().end());
            for(auto child : children)
Paul's avatar
Paul committed
34
            {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
                if(not skip_propogate(child))
                {
                    auto r = child->eval();
                    if(not r.empty())
                    {
                        assert(r.get_shape() == child->get_shape());
                        auto l = p.add_literal(r.get_shape(), r.data());
                        self(p.replace_instruction(child, l));
                    }
                } 
                else
                {
                    self(child);
                }
Paul's avatar
Paul committed
49
            }
50
51
        })(i);
    }
Paul's avatar
Paul committed
52
}
Paul's avatar
Paul committed
53

Paul's avatar
Paul committed
54
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
55
} // namespace migraphx