"git@developer.sourcefind.cn:gaoqiong/migraphx.git" did not exist on "87446fc4d1246c551f1aa5998d368eb4ee17a833"
parse_layernorm.cpp 2.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/instruction.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

struct parse_layernorm : op_parser<parse_layernorm>
{
    std::vector<op_desc> operators() const { return {{"LayerNormalization"}}; }

    instruction_ref parse(const op_desc& /*opd*/,
                          const onnx_parser& parser,
                          onnx_parser::node_info info,
                          const std::vector<instruction_ref>& args) const
    {
20
        // un-fuse layernorm op so migraphx can handle fusion instead
turneram's avatar
turneram committed
21
22
23
24
25

        auto x       = args.front();
        auto x_type  = x->get_shape().type();
        auto weights = args.at(1);
        auto bias    = args.at(2);
26
27

        float epsilon = 1e-12f;
turneram's avatar
turneram committed
28
        int64_t axis  = -1;
29
30
31
        if(contains(info.attributes, "epsilon"))
        {
            epsilon = parser.parse_value(info.attributes.at("epsilon")).at<float>();
turneram's avatar
turneram committed
32
        }
33
34
        if(contains(info.attributes, "axis"))
        {
turneram's avatar
turneram committed
35
            axis = parser.parse_value(info.attributes.at("axis")).at<int64_t>();
36
        }
turneram's avatar
turneram committed
37
38
39
40

        auto epsilon_lit = info.add_literal(literal{shape{x_type, {1}}, {epsilon}});
        auto exponent    = info.add_literal(literal{shape{x_type, {1}}, {2.0}});
        auto dims        = x->get_shape().lens();
41

42
43
44
        auto mean = info.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {axis}}}), x);
        auto mean_mbcast =
            info.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", dims}}), mean);
turneram's avatar
turneram committed
45
46
47
        auto sub             = info.add_instruction(migraphx::make_op("sub"), x, mean_mbcast);
        auto exponent_mbcast = info.add_instruction(
            migraphx::make_op("multibroadcast", {{"out_lens", dims}}), exponent);
48
49
50
        auto pow = info.add_instruction(migraphx::make_op("pow"), sub, exponent_mbcast);
        auto var = info.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {axis}}}), pow);
        auto add_epsilon = info.add_broadcastable_binary_op("add", var, epsilon_lit);
turneram's avatar
turneram committed
51
52
53
        auto sqrt        = info.add_instruction(migraphx::make_op("sqrt"), add_epsilon);
        auto div         = info.add_broadcastable_binary_op("div", sub, sqrt);
        auto mul         = info.add_broadcastable_binary_op("mul", div, weights);
turneram's avatar
turneram committed
54

55
        return info.add_broadcastable_binary_op("add", mul, bias);
56
57
58
59
60
61
    }
};

} // namespace onnx
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx