test_layernorm.cpp 3.53 KB
Newer Older
1
2
3
4

#include "verify_program.hpp"
#include <migraphx/program.hpp>
#include <migraphx/generate.hpp>
5
6
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
7
#include <migraphx/op/reduce_mean.hpp>
8

Paul Fultz II's avatar
Paul Fultz II committed
9
10
migraphx::instruction_ref
add_layernorm(migraphx::module& m, migraphx::instruction_ref x, std::vector<size_t> dims)
11
12
{
    auto scale =
Paul Fultz II's avatar
Paul Fultz II committed
13
        m.add_parameter("scale", migraphx::shape{migraphx::shape::float_type, {dims.back()}});
14
    auto bias =
Paul Fultz II's avatar
Paul Fultz II committed
15
16
17
        m.add_parameter("bias", migraphx::shape{migraphx::shape::float_type, {dims.back()}});
    auto epsilon  = m.add_literal(1e-12f);
    auto exponent = m.add_literal(2.0f);
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    auto mean = m.add_instruction(migraphx::op::reduce_mean({2}), x);
    auto mean_mbcast =
        m.add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", dims}}), mean);
    auto sub = m.add_instruction(migraphx::make_op("sub"), x, mean_mbcast);
    auto exponent_mbcast =
        m.add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", dims}}), exponent);
    auto pow            = m.add_instruction(migraphx::make_op("pow"), sub, exponent_mbcast);
    auto var            = m.add_instruction(migraphx::op::reduce_mean({2}), pow);
    auto epsilon_mbcast = m.add_instruction(
        migraphx::make_op("multibroadcast", {{"output_lens", {1, dims.at(1), 1}}}), epsilon);
    auto add_epsilon = m.add_instruction(migraphx::make_op("add"), var, epsilon_mbcast);
    auto sqrt        = m.add_instruction(migraphx::make_op("sqrt"), add_epsilon);
    auto sqrt_mbcast =
        m.add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", dims}}), sqrt);
    auto div = m.add_instruction(migraphx::make_op("div"), sub, sqrt_mbcast);
    auto scale_mbcast =
        m.add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", dims}}), scale);
    auto mul = m.add_instruction(migraphx::make_op("mul"), scale_mbcast, div);
    auto bias_mbcast =
        m.add_instruction(migraphx::make_op("multibroadcast", {{"output_lens", dims}}), bias);
    return m.add_instruction(migraphx::make_op("add"), mul, bias_mbcast);
40
41
42
43
44
45
46
}

struct test_layernorm : verify_program<test_layernorm>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
Paul Fultz II's avatar
Paul Fultz II committed
47
48
49
50
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 1, 5};
        auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, dims});
        add_layernorm(*mm, x, dims);
51
52
53
54
55
56
57
58
59
        return p;
    }
};

struct test_layernorm2 : verify_program<test_layernorm2>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
Paul Fultz II's avatar
Paul Fultz II committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 4, 24};
        auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, dims});
        add_layernorm(*mm, x, dims);
        return p;
    }
};

struct test_layernorm_triadd : verify_program<test_layernorm_triadd>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 4, 24};
        auto x    = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, dims});
        auto y    = mm->add_parameter("y", migraphx::shape{migraphx::shape::float_type, dims});
        auto z    = mm->add_parameter("z", migraphx::shape{migraphx::shape::float_type, dims});
78
79
        auto add1 = mm->add_instruction(migraphx::make_op("add"), x, y);
        auto add2 = mm->add_instruction(migraphx::make_op("add"), add1, z);
Paul Fultz II's avatar
Paul Fultz II committed
80
        add_layernorm(*mm, add2, dims);
81
82
83
        return p;
    }
};