fwd_conv_batchnorm_rewrite.cpp 2.87 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->arguments.begin() + 1, ins->arguments.end(), [](auto arg) {
Paul's avatar
Paul committed
16
               return arg->name() == "@literal";
Paul's avatar
Paul committed
17
           }))
18
19
20
            continue;

        auto conv_ins = ins->arguments[0];
Paul's avatar
Paul committed
21
        if(conv_ins->name() != "convolution")
22
            continue;
Paul's avatar
Paul committed
23
        if(conv_ins->arguments[1]->name() != "@literal")
24
25
26
27
28
29
30
31
32
33
34
35
36
            continue;
        // Get scale, bias, mean, variance from instruction_ref
        const auto& gamma    = ins->arguments[1]->get_literal();
        const auto& bias     = ins->arguments[2]->get_literal();
        const auto& mean     = ins->arguments[3]->get_literal();
        const auto& variance = ins->arguments[4]->get_literal();
        // Get epsilon
        auto bn_op   = any_cast<batch_norm_inference>(ins->op);
        auto epsilon = bn_op.epsilon;
        // Get convolution weights
        const auto& weights = conv_ins->arguments[1]->get_literal();
        // Get convolution op
        auto conv_op      = conv_ins->op;
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->arguments[0], l_weights});
Paul's avatar
Paul committed
62
63
        auto b = p.insert_instruction(ins, broadcast{1}, c, l_bias);
        p.replace_instruction(ins, add{}, {c, b});
64
65
66
    }
}
} // namespace migraph