"docs/en_US/CommunitySharings/ModelCompressionComparison.rst" did not exist on "d165905d0ba24cfba414b8e0c20fa8d7c8ab6a6e"
prefuse_ops.cpp 4.1 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
#include <migraphx/gpu/prefuse_ops.hpp>
Artur Wojcik's avatar
Artur Wojcik committed
25
#if !defined(_MSC_VER)
26
#include <migraphx/match/layernorm.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
27
#include <migraphx/check_shapes.hpp>
28
#include <migraphx/make_op.hpp>
29
#include <migraphx/register_op.hpp>
Artur Wojcik's avatar
Artur Wojcik committed
30
#endif
31
#include <migraphx/pass_manager.hpp>
Artur Wojcik's avatar
Artur Wojcik committed
32
#if !defined(_MSC_VER)
33
#include <migraphx/dead_code_elimination.hpp>
Artur Wojcik's avatar
Artur Wojcik committed
34
#endif
35
36
37
38

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
Artur Wojcik's avatar
Artur Wojcik committed
39
40

#if !defined(_MSC_VER)
41
namespace {
42
43
44
45

template <class Derived, std::size_t N>
struct layernorm_base
{
46
47
48
49
50
51
    float epsilon = 1e-12f;
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(f(self.epsilon, "epsilon"));
    }
52
53
54
55
56
57
58
59
60
61
    shape compute_shape(std::vector<shape> inputs, std::vector<module_ref> mods) const
    {
        std::size_t nargs = 1;
        if(not mods.empty())
        {
            auto* pm = mods.front();
            nargs    = pm->get_parameter_names().size();
        }
        check_shapes{inputs, static_cast<const Derived&>(*this)}.has(nargs + N);
        auto s = inputs.at(0);
62
63
64
        auto t = s.type();
        if(not mods.empty())
            t = mods.front()->get_output_shapes().front().type();
65
66
67
68
69
70
        if(s.scalar())
        {
            return s;
        }
        else if(s.broadcasted())
        {
71
            return {t, s.lens()};
72
73
74
        }
        else
        {
75
            return s.with_lens(t, s.lens());
76
77
78
79
80
81
        }
    }
};

struct layernorm : layernorm_base<layernorm, 0>
{
82

83
84
85
86
87
88
89
90
91
92
    std::string name() const { return "gpu::prelayernorm"; }
};
MIGRAPHX_REGISTER_OP(layernorm);

struct add_layernorm : layernorm_base<add_layernorm, 1>
{
    std::string name() const { return "gpu::preadd_layernorm"; }
};
MIGRAPHX_REGISTER_OP(add_layernorm);

93
94
95
96
97
98
99
100
struct find_layernorm
{
    auto matcher() const { return match::layernorm(); }

    void apply(module& m, const match::matcher_result& r) const
    {
        auto ins   = r.result;
        auto x_ins = r.instructions["x"];
101
102
103
        float eps  = 0;
        if(contains(r.instructions, "eps"))
            eps = r.instructions["eps"]->eval().at<float>();
104

105
        m.replace_instruction(ins, layernorm{eps}, x_ins);
106
107
108
    }
};

109
struct find_add_layernorm
110
111
112
{
    auto matcher() const
    {
113
114
        return match::name("gpu::prelayernorm")(
            match::args(match::name("add")(match::used_once()).bind("add")));
115
116
117
118
    }

    void apply(module& m, const match::matcher_result& r) const
    {
119
120
        auto ins     = r.result;
        auto add_ins = r.instructions["add"];
121
        auto op      = any_cast<layernorm>(ins->get_operator());
122

123
        m.replace_instruction(ins, add_layernorm{op.epsilon}, add_ins->inputs());
124
125
126
    }
};
} // namespace
Artur Wojcik's avatar
Artur Wojcik committed
127
#endif
128

129
void prefuse_ops::apply(module_pass_manager& mpm) const
130
{
Artur Wojcik's avatar
Artur Wojcik committed
131
#if !defined(_MSC_VER)
132
133
134
    match::find_matches(mpm.get_module(), find_layernorm{});
    mpm.run_pass(dead_code_elimination{});
    match::find_matches(mpm.get_module(), find_add_layernorm{});
Artur Wojcik's avatar
Artur Wojcik committed
135
#endif
136
137
138
139
140
}

} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx