simplify_algebra.cpp 5.02 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() { return match::any_of(match::is_constant(), match::name("broadcast")); }
Paul's avatar
Paul committed
13
auto not_lit_broadcast() { return match::none_of(match::is_constant(), match::name("broadcast")); }
Paul's avatar
Paul committed
14
15
auto op_lit_broadcast(std::string op, std::string x, std::string y)
{
Paul's avatar
Paul committed
16
17
    return match::name(std::move(op))(match::either_arg(0, 1)(
        lit_broadcast().bind(std::move(x)), not_lit_broadcast().bind(std::move(y))));
Paul's avatar
Paul committed
18
19
}

Paul's avatar
Paul committed
20
21
auto conv_const_weights()
{
Paul's avatar
Paul committed
22
    return match::name("convolution")(match::used_once_recursive(4),
Paul's avatar
Paul committed
23
                                      match::args(match::any(), match::is_constant().bind("w")));
Paul's avatar
Paul committed
24
25
}

Paul's avatar
Paul committed
26
27
28
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)(conv_const_weights().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
        auto ins      = r.result;
Paul's avatar
Paul committed
37
        auto conv_ins = r.instructions["conv"];
Paul's avatar
Paul committed
38
39
40
        auto a_ins    = r.instructions["a"];
        auto w_ins    = r.instructions["w"];

Paul's avatar
Paul committed
41
        auto broadcast_op = any_cast<op::broadcast>(a_ins->get_operator());
Paul's avatar
Paul committed
42
        if(broadcast_op.axis != 1)
Paul's avatar
Paul committed
43
44
            return;

Paul's avatar
Paul committed
45
46
47
48
49
        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);
Paul's avatar
Paul committed
50
        p.replace_instruction(ins, new_conv);
Paul's avatar
Paul committed
51
    }
Paul's avatar
Paul committed
52
53
};

Paul's avatar
Paul committed
54
55
56
57
58
struct find_mul_add
{
    auto matcher() const
    {
        return match::name("mul")(match::either_arg(0, 1)(
Paul's avatar
Paul committed
59
60
61
62
            match::name("add")(
                match::either_arg(0, 1)(
                    match::any().bind("x"),
                    match::any_of(conv_const_weights(), match::is_constant()).bind("y")),
Paul's avatar
Paul committed
63
64
                match::none_of(match::args(match::is_constant(), match::is_constant())),
                match::used_once_recursive(4)),
Paul's avatar
Paul committed
65
            match::is_constant().bind("a")));
Paul's avatar
Paul committed
66
67
68
69
    }

    void apply(program& p, match::matcher_result r) const
    {
Paul's avatar
Paul committed
70
        auto ins   = r.result;
Paul's avatar
Paul committed
71
        auto a_ins = r.instructions["a"];
Paul's avatar
Paul committed
72
73
        auto x_ins = r.instructions["x"];
        auto y_ins = r.instructions["y"];
Paul's avatar
Paul committed
74

Paul's avatar
Paul committed
75
76
        auto xa_ins = p.insert_instruction(ins, op::mul{}, x_ins, a_ins);
        auto ya_ins = p.insert_instruction(ins, op::mul{}, y_ins, a_ins);
Paul's avatar
Paul committed
77
        p.replace_instruction(ins, op::add{}, xa_ins, ya_ins);
Paul's avatar
Paul committed
78
79
80
    }
};

Paul's avatar
Paul committed
81
struct find_add_lit_broadcast
Paul's avatar
Paul committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{
    auto matcher() const
    {
        return match::name("add")(
            match::either_arg(0, 1)(op_lit_broadcast("add", "a", "x"), lit_broadcast().bind("b")));
    }

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

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

struct find_double_add_lit_broadcast
Paul's avatar
Paul committed
102
{
Paul's avatar
Paul committed
103
104
    auto matcher() const
    {
Paul's avatar
Paul committed
105
        return match::name("add")(
Paul's avatar
Paul committed
106
            match::args(op_lit_broadcast("add", "a", "x"), op_lit_broadcast("add", "b", "y")));
Paul's avatar
Paul committed
107
108
109
110
    }

    void apply(program& p, match::matcher_result r) const
    {
Paul's avatar
Paul committed
111
112
113
114
115
        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
116
117
118

        instruction_ref sumab;

Paul's avatar
Paul committed
119
        if(a_ins->name() == "broadcast" and b_ins->name() == "broadcast")
Paul's avatar
Paul committed
120
121
122
        {
            if(a_ins->inputs().at(0)->get_shape() != b_ins->inputs().at(0)->get_shape())
                return;
Paul's avatar
Paul committed
123
124
125
126
            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
127
128
129
130
131
132
133
134
135
136
137
        }
        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
138
139
void simplify_algebra::apply(program& p) const
{
Paul's avatar
Paul committed
140
    // Run simplifications multiple times
Paul's avatar
Paul committed
141
    for(int i = 0; i < 2; i++)
Paul's avatar
Paul committed
142
143
144
145
146
147
        match::find_matches(p,
                            match::skip_matches(match::is_unused(), match::is_constant()),
                            find_double_add_lit_broadcast{},
                            find_add_lit_broadcast{},
                            find_mul_conv{},
                            find_mul_add{});
Paul's avatar
Paul committed
148
}
Paul's avatar
Paul committed
149

Paul's avatar
Paul committed
150
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
151
} // namespace migraphx