simplify_algebra.cpp 3.09 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include <migraphx/simplify_algebra.hpp>
#include <migraphx/program.hpp>
3
#include <migraphx/op/add.hpp>
Paul's avatar
Paul committed
4
5
#include <migraphx/op/mul.hpp>
#include <migraphx/op/broadcast.hpp>
Paul's avatar
Paul committed
6
7
#include <migraphx/matcher.hpp>
#include <migraphx/literal.hpp>
Paul's avatar
Paul committed
8

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

Paul's avatar
Paul committed
12
auto lit_broadcast()
Paul's avatar
Paul committed
13
{
Paul's avatar
Paul committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    return match::any_of(match::name("@literal"), match::name("broadcast"));
}
auto not_lit_broadcast()
{
    return match::none_of(match::name("@literal"), match::name("broadcast"));
}
auto op_lit_broadcast(std::string op, std::string x, std::string y)
{
    return match::name(op)(match::either_arg(0, 1)(lit_broadcast().bind(std::move(x)),
                                                      not_lit_broadcast().bind(std::move(y))));
}

struct find_mul_conv
{
    auto matcher() const
Paul's avatar
Paul committed
29
    {
Paul's avatar
Paul committed
30
31
        return match::name("mul")(
            match::either_arg(0, 1)(match::name("conv")(match::used_once(), match::args(match::any(), match::is_constant().bind("w"))).bind("conv"), match::name("broadcast").bind("a")));
Paul's avatar
Paul committed
32
    }
Paul's avatar
Paul committed
33
34

    void apply(program& p, match::matcher_result r) const
Paul's avatar
Paul committed
35
    {
Paul's avatar
Paul committed
36
37
38
39
40
41
42
43
44
45
46
47
48
        auto ins   = r.result;
        auto conv_ins = r.instructions["conv"];
        auto a_ins = r.instructions["a"];
        auto w_ins = r.instructions["w"];
        
        auto broadcast_op = any_cast<op::broadcast>(a_ins->get_operator());
        if (broadcast_op.axis != 1)
            return;

        auto new_a = p.insert_instruction(ins, op::broadcast{0, w_ins->get_shape().lens()}, a_ins->inputs().front());
        auto new_mul = p.insert_instruction(ins, op::mul{}, new_a, w_ins);
        auto new_conv = p.insert_instruction(ins, conv_ins->get_operator(), conv_ins->inputs().front(), new_mul);
        p.replace_instruction(ins, new_conv);
Paul's avatar
Paul committed
49
    }
Paul's avatar
Paul committed
50
51
52
53
};

struct find_add_lit_broadcast
{
Paul's avatar
Paul committed
54
55
    auto matcher() const
    {
Paul's avatar
Paul committed
56
        return match::name("add")(
Paul's avatar
Paul committed
57
            match::args(op_lit_broadcast("add", "a", "x"), op_lit_broadcast("add", "b", "y")));
Paul's avatar
Paul committed
58
59
60
61
    }

    void apply(program& p, match::matcher_result r) const
    {
Paul's avatar
Paul committed
62
63
64
65
66
        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"];
Paul's avatar
Paul committed
67
68
69
70
71
72
73
74
75

        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;
Paul's avatar
Paul committed
76
77
78
79
            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);
Paul's avatar
Paul committed
80
81
82
83
84
85
86
87
88
89
90
        }
        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);
    }
};

Paul's avatar
Paul committed
91
void simplify_algebra::apply(program& p) const { match::find_matches(p, find_add_lit_broadcast{}, find_mul_conv{}); }
Paul's avatar
Paul committed
92

Paul's avatar
Paul committed
93
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
94
} // namespace migraphx