simplify_algebra.cpp 1.9 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
18
19
20
21
22
23
24
25
26
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
52
53
54
55
56
57
58
59
60
#include <migraph/simplify_algebra.hpp>
#include <migraph/program.hpp>
#include <migraph/operators.hpp>
#include <migraph/matcher.hpp>
#include <migraph/literal.hpp>

namespace migraph {

struct find_add_lit_broadcast
{
    auto lit_broadcast() const
    {
        return match::any_of(match::name("@literal"), match::name("broadcast"));
    }
    auto not_lit_broadcast() const
    {
        return match::none_of(match::name("@literal"), match::name("broadcast"));
    }
    auto add_lit_broadcast(std::string x, std::string y) const
    {
        return match::name("add")(match::either_arg(0, 1)(lit_broadcast().bind(x), not_lit_broadcast().bind(y)));
    }
    auto matcher() const
    {
        return match::name("add")(match::args(add_lit_broadcast("a", "x"), add_lit_broadcast("b", "y")));
    }

    void apply(program& p, match::matcher_result r) const
    {
        auto ins  = r.result;
        auto x_ins    = r.instructions["x"];
        auto y_ins    = r.instructions["y"];
        auto a_ins    = r.instructions["a"];
        auto b_ins    = r.instructions["b"];

        if(a_ins->name() != b_ins->name())
            return;
        instruction_ref sumab;

        if(a_ins->name() == "broadcast")
        {
            if(a_ins->inputs().at(0)->get_shape() != b_ins->inputs().at(0)->get_shape())
                return;
            auto op     = a_ins->get_operator();
            auto presum = p.insert_instruction(ins, op::add{}, a_ins->inputs().at(0), b_ins->inputs().at(0));
            sumab       = p.insert_instruction(ins, op, presum);
        }
        else
        {
            sumab = p.insert_instruction(ins, op::add{}, a_ins, b_ins);
        }

        auto sumxy = p.insert_instruction(ins, op::add{}, x_ins, y_ins);
        p.replace_instruction(ins, op::add{}, sumxy, sumab);
    }
};

void simplify_algebra::apply(program& p) const { match::find_matches(p, find_add_lit_broadcast{}); }

} // namespace migraph