fwd_conv_batchnorm_rewrite.cpp 2.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include <migraph/fwd_conv_batchnorm_rewrite.hpp>
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/operators.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/dfor.hpp>

namespace migraph {
void fwd_conv_batchnorm_rewrite::apply(program& p) const
{
wsttiger's avatar
wsttiger committed
11
    for(auto ins : iterator_for(p))
12
    {
Paul's avatar
Paul committed
13
        if(ins->name() != "batch_norm_inference")
14
            continue;
Paul's avatar
Paul committed
15
        if(not std::all_of(ins->inputs().begin() + 1, ins->inputs().end(), [](auto arg) {
Paul's avatar
Paul committed
16
               return arg->name() == "@literal";
Paul's avatar
Paul committed
17
           }))
18
19
            continue;

Paul's avatar
Paul committed
20
        auto conv_ins = ins->inputs()[0];
Paul's avatar
Paul committed
21
        if(conv_ins->name() != "convolution")
22
            continue;
Paul's avatar
Paul committed
23
        if(conv_ins->inputs()[1]->name() != "@literal")
24
25
            continue;
        // Get scale, bias, mean, variance from instruction_ref
Paul's avatar
Paul committed
26
27
28
29
        const auto& gamma    = ins->inputs()[1]->get_literal();
        const auto& bias     = ins->inputs()[2]->get_literal();
        const auto& mean     = ins->inputs()[3]->get_literal();
        const auto& variance = ins->inputs()[4]->get_literal();
30
        // Get epsilon
31
        auto bn_op   = any_cast<op::batch_norm_inference>(ins->get_operator());
32
33
        auto epsilon = bn_op.epsilon;
        // Get convolution weights
Paul's avatar
Paul committed
34
        const auto& weights = conv_ins->inputs()[1]->get_literal();
35
        // Get convolution op
36
        auto conv_op      = conv_ins->get_operator();
Paul's avatar
Paul committed
37
        auto weights_lens = weights.get_shape().lens();
Paul's avatar
Paul committed
38
        auto conv_lens    = conv_ins->get_shape().lens();
39
        argument new_weights{weights.get_shape()};
Paul's avatar
Paul committed
40
        argument new_bias{bias.get_shape()};
41
42
43
44
45
46
47
48
        visit_all(weights, gamma, bias, mean, variance, new_weights, new_bias)(
            [&](auto weights2,
                auto gamma2,
                auto bias2,
                auto mean2,
                auto variance2,
                auto new_weights2,
                auto new_bias2) {
Paul's avatar
Paul committed
49
                dfor(weights_lens[0], weights_lens[1], weights_lens[2], weights_lens[3])(
50
                    [&](std::size_t k, std::size_t c, std::size_t h, std::size_t w) {
Paul's avatar
Paul committed
51
52
                        new_weights2(k, c, h, w) =
                            gamma2(k) / std::sqrt(variance2(k) + epsilon) * weights2(k, c, h, w);
Paul's avatar
Paul committed
53
                    });
Paul's avatar
Paul committed
54
55
56
                dfor(new_bias.get_shape().elements())([&](std::size_t c) {
                    new_bias2(c) = bias2(c) - (mean2(c) / std::sqrt(variance2(c) + epsilon));
                });
57
58
59
            });
        // Replace convolution instruction with updated weights
        auto l_weights = p.add_literal({weights.get_shape(), new_weights.data()});
Paul's avatar
Paul committed
60
        auto l_bias    = p.add_literal({new_bias.get_shape(), new_bias.data()});
Paul's avatar
Paul committed
61
        auto c = p.replace_instruction(conv_ins, conv_op, {conv_ins->inputs()[0], l_weights});
Scott Thornton's avatar
Scott Thornton committed
62
        auto b = p.insert_instruction(ins, op::broadcast{1, c->get_shape()}, l_bias);
63
        p.replace_instruction(ins, op::add{}, {c, b});
64
65
66
    }
}
} // namespace migraph