"tools/git@developer.sourcefind.cn:gaoqiong/migraphx.git" did not exist on "6a09f672b680200898492f77caf72508f850c618"
rewrite_batchnorm.cpp 3.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
#include <migraphx/rewrite_batchnorm.hpp>
Paul's avatar
Paul committed
25
26
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
27
#include <migraphx/op/batch_norm_inference.hpp>
28
29
#include <migraphx/op/broadcast.hpp>
#include <migraphx/op/add.hpp>
Paul's avatar
Paul committed
30
#include <migraphx/op/mul.hpp>
Paul's avatar
Paul committed
31
#include <migraphx/iterator_for.hpp>
32
#include <migraphx/ranges.hpp>
33
34
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
35
#include <migraphx/dfor.hpp>
36

Paul's avatar
Paul committed
37
namespace migraphx {
Paul's avatar
Paul committed
38
inline namespace MIGRAPHX_INLINE_NS {
39

40
void rewrite_batchnorm::apply(module& m) const
41
{
42
    for(auto ins : iterator_for(m))
43
    {
Paul's avatar
Paul committed
44
        if(ins->name() != "batch_norm_inference")
45
            continue;
46
        // Get scale, bias, mean, variance from inputs
Paul's avatar
Paul committed
47
48
49
50
        auto gamma    = ins->inputs()[1]->eval();
        auto bias     = ins->inputs()[2]->eval();
        auto mean     = ins->inputs()[3]->eval();
        auto variance = ins->inputs()[4]->eval();
51
        if(any_of({gamma, bias, mean, variance}, [](auto arg) { return arg.empty(); }))
52
53
            continue;

Shucai Xiao's avatar
Shucai Xiao committed
54
55
        std::vector<std::size_t> lens = ins->inputs()[1]->get_shape().lens();
        shape s{ins->get_shape().type(), lens};
56
        // Get epsilon
57
        auto bn_op   = any_cast<op::batch_norm_inference>(ins->get_operator());
58
        auto epsilon = bn_op.epsilon;
Paul's avatar
Paul committed
59
60
61
62

        argument a{s};
        argument b{s};
        visit_all(gamma, bias, mean, variance, a, b)(
Paul's avatar
Paul committed
63
64
65
66
67
            [&](auto gamma2, auto bias2, auto mean2, auto variance2, auto a2, auto b2) {
                dfor(a.get_shape().elements())(
                    [&](std::size_t c) { a2[c] = gamma2[c] / std::sqrt(variance2[c] + epsilon); });
                dfor(b.get_shape().elements())([&](std::size_t c) {
                    b2[c] = bias2[c] - (gamma2[c] * mean2[c] / std::sqrt(variance2[c] + epsilon));
Paul's avatar
Paul committed
68
                });
69
            });
Paul's avatar
Paul committed
70

Paul's avatar
Paul committed
71
        auto broadcast   = op::broadcast{1, ins->get_shape().lens()};
72
73
74
75
76
77
78
        auto a_ins       = m.add_literal({a.get_shape(), a.data()});
        auto a_broadcast = m.insert_instruction(ins, broadcast, a_ins);
        auto mul   = m.insert_instruction(ins, make_op("mul"), ins->inputs().front(), a_broadcast);
        auto b_ins = m.add_literal({b.get_shape(), b.data()});
        auto b_broadcast = m.insert_instruction(ins, broadcast, b_ins);
        auto add         = m.insert_instruction(ins, make_op("add"), mul, b_broadcast);
        m.replace_instruction(ins, add);
79
80
    }
}
81

Paul's avatar
Paul committed
82
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
83
} // namespace migraphx