test_layernorm.cpp 8.06 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.
 */
24
25
26

#include "verify_program.hpp"
#include <migraphx/program.hpp>
27
#include <migraphx/instruction.hpp>
28
#include <migraphx/generate.hpp>
29
30
#include <migraphx/make_op.hpp>

31
32
33
34
migraphx::instruction_ref add_layernorm(migraphx::module& m,
                                        migraphx::instruction_ref x,
                                        std::vector<size_t> dims,
                                        float eps = 1e-12f)
35
{
36
37
38
39
40
41
    auto mgx_type = x->get_shape().type();
    auto scale    = m.add_parameter("scale", migraphx::shape{mgx_type, {dims.back()}});
    auto bias     = m.add_parameter("bias", migraphx::shape{mgx_type, {dims.back()}});

    auto epsilon  = m.add_literal(migraphx::literal{migraphx::shape{mgx_type}, {eps}});
    auto exponent = m.add_literal(migraphx::literal{migraphx::shape{mgx_type}, {2.0f}});
42

43
    auto mean = m.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2}}}), x);
44
    auto mean_mbcast =
45
        m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", dims}}), mean);
46
47
    auto sub = m.add_instruction(migraphx::make_op("sub"), x, mean_mbcast);
    auto exponent_mbcast =
48
        m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", dims}}), exponent);
49
    auto pow            = m.add_instruction(migraphx::make_op("pow"), sub, exponent_mbcast);
50
    auto var            = m.add_instruction(migraphx::make_op("reduce_mean", {{"axes", {2}}}), pow);
51
    auto epsilon_mbcast = m.add_instruction(
52
53
        migraphx::make_op("multibroadcast", {{"out_lens", {dims.at(0), dims.at(1), 1}}}), epsilon);

54
55
56
    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 =
57
        m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", dims}}), sqrt);
58
59
    auto div = m.add_instruction(migraphx::make_op("div"), sub, sqrt_mbcast);
    auto scale_mbcast =
60
        m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", dims}}), scale);
61
62
    auto mul = m.add_instruction(migraphx::make_op("mul"), div, scale_mbcast);

63
    auto bias_mbcast =
64
        m.add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", dims}}), bias);
65
    return m.add_instruction(migraphx::make_op("add"), mul, bias_mbcast);
66
67
68
69
70
71
72
}

struct test_layernorm : verify_program<test_layernorm>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
Paul Fultz II's avatar
Paul Fultz II committed
73
        auto* mm                 = p.get_main_module();
74
        std::vector<size_t> dims = {1, 2, 5};
Paul Fultz II's avatar
Paul Fultz II committed
75
76
        auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, dims});
        add_layernorm(*mm, x, dims);
77
78
79
80
81
82
83
84
85
        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
86
87
88
89
90
91
92
93
        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;
    }
};

94
95
96
97
98
99
100
101
102
103
104
105
106
struct test_layernorm_large : verify_program<test_layernorm_large>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 32, 262144};
        auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, dims});
        add_layernorm(*mm, x, dims);
        return p;
    }
};

107
108
109
110
111
112
113
114
115
116
117
118
119
struct test_layernorm_fp16 : verify_program<test_layernorm_fp16>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 24, 64};
        auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::half_type, dims});
        add_layernorm(*mm, x, dims);
        return p;
    }
};

Umang Yadav's avatar
Umang Yadav committed
120
121
122
123
124
125
126
127
128
129
130
131
132
struct test_layernorm_fp8 : verify_program<test_layernorm_fp8>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 24, 64};
        auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::fp8e4m3fnuz_type, dims});
        add_layernorm(*mm, x, dims);
        return p;
    }
};

133
134
135
136
137
138
139
140
141
142
143
144
145
struct test_layernorm_eps : verify_program<test_layernorm_eps>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 2, 5};
        auto x = mm->add_parameter("x", migraphx::shape{migraphx::shape::float_type, dims});
        add_layernorm(*mm, x, dims, 1e-5f);
        return p;
    }
};

Paul Fultz II's avatar
Paul Fultz II committed
146
147
148
149
150
151
152
153
154
155
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});
156
157
        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
158
        add_layernorm(*mm, add2, dims);
159
160
161
        return p;
    }
};
kahmed10's avatar
kahmed10 committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

struct test_layernorm_triadd_large : verify_program<test_layernorm_triadd_large>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
        auto* mm                 = p.get_main_module();
        std::vector<size_t> dims = {1, 384, 1024};
        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});
        auto add1 = mm->add_instruction(migraphx::make_op("add"), x, y);
        auto add2 = mm->add_instruction(migraphx::make_op("add"), add1, z);
        add_layernorm(*mm, add2, dims);
        return p;
    }
};
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

struct test_add_layernorm_add_gemm_nonstd : verify_program<test_add_layernorm_add_gemm_nonstd>
{
    migraphx::program create_program() const
    {
        migraphx::program p;
        auto* mm = p.get_main_module();
        auto s =
            migraphx::shape::from_permutation(migraphx::shape::float_type, {8, 1, 16}, {1, 2, 0});
        auto x = mm->add_parameter("x", s);
        auto y = mm->add_parameter("y", s);
        auto z = mm->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {8, 16, 64}});
        auto add           = mm->add_instruction(migraphx::make_op("add"), x, y);
        auto layernorm_ins = add_layernorm(*mm, add, s.lens());
        mm->add_instruction(migraphx::make_op("dot"), layernorm_ins, z);
        return p;
    }
};