prefuse_ops.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.
 */
24
25
26
#include <migraphx/gpu/prefuse_ops.hpp>
#include <migraphx/match/layernorm.hpp>
#include <migraphx/make_op.hpp>
Paul's avatar
Paul committed
27
#include <migraphx/register_op.hpp>
28
29
30
31

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
Paul's avatar
Paul committed
32
namespace {
Paul's avatar
Paul committed
33

Paul's avatar
Format  
Paul committed
34
template <class Derived, std::size_t N>
Paul's avatar
Paul committed
35
36
struct layernorm_base
{
Khalique Ahmed's avatar
Khalique Ahmed committed
37
38
39
40
41
42
43
    float epsilon = 1e-12f;
    template <class Self, class F>
    static auto reflect(Self& self, F f)
    {
        return pack(
            f(self.epsilon, "epsilon"));
    }
Paul's avatar
Paul committed
44
    shape compute_shape(std::vector<shape> inputs, std::vector<module_ref> mods) const
Paul's avatar
Paul committed
45
    {
Paul's avatar
Paul committed
46
        std::size_t nargs = 1;
Paul's avatar
Format  
Paul committed
47
48
49
50
        if(not mods.empty())
        {
            auto* pm = mods.front();
            nargs    = pm->get_parameter_names().size();
Paul's avatar
Paul committed
51
        }
Paul's avatar
Format  
Paul committed
52
        check_shapes{inputs, static_cast<const Derived&>(*this)}.has(nargs + N);
Paul's avatar
Paul committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        auto s = inputs.at(0);
        if(s.scalar())
        {
            return s;
        }
        else if(s.broadcasted())
        {
            return {s.type(), s.lens()};
        }
        else
        {
            return s.with_lens(s.lens());
        }
    }
};
Paul's avatar
Paul committed
68
69
70

struct layernorm : layernorm_base<layernorm, 0>
{
Khalique Ahmed's avatar
Khalique Ahmed committed
71
    
Paul's avatar
Paul committed
72
73
    std::string name() const { return "gpu::prelayernorm"; }
};
Paul's avatar
Paul committed
74
MIGRAPHX_REGISTER_OP(layernorm);
Paul's avatar
Paul committed
75

Paul's avatar
Paul committed
76
77
78
79
80
81
struct add_layernorm : layernorm_base<add_layernorm, 1>
{
    std::string name() const { return "gpu::preadd_layernorm"; }
};
MIGRAPHX_REGISTER_OP(add_layernorm);

82
83
84
85
struct find_layernorm
{
    auto matcher() const { return match::layernorm(); }

Paul's avatar
Paul committed
86
87
88
89
    void apply(module& m, const match::matcher_result& r) const
    {
        auto ins   = r.result;
        auto x_ins = r.instructions["x"];
Khalique Ahmed's avatar
Khalique Ahmed committed
90
        auto eps = r.instructions["eps"]->eval().at<float>();
Paul's avatar
Paul committed
91

Khalique Ahmed's avatar
Khalique Ahmed committed
92
        m.replace_instruction(ins, layernorm{eps}, x_ins);
Paul's avatar
Paul committed
93
94
95
    }
};

Paul's avatar
Paul committed
96
97
struct find_add_layernorm
{
Paul's avatar
Format  
Paul committed
98
99
100
101
    auto matcher() const
    {
        return match::layernorm()(match::var("x")(match::name("add").bind("add")));
    }
Paul's avatar
Paul committed
102
103
104

    void apply(module& m, const match::matcher_result& r) const
    {
Paul's avatar
Format  
Paul committed
105
        auto ins     = r.result;
Paul's avatar
Paul committed
106
        auto add_ins = r.instructions["add"];
Khalique Ahmed's avatar
Khalique Ahmed committed
107
        auto eps = r.instructions["eps"]->eval().at<float>();
Paul's avatar
Paul committed
108

Khalique Ahmed's avatar
Khalique Ahmed committed
109
        m.replace_instruction(ins, add_layernorm{eps}, add_ins->inputs());
Paul's avatar
Paul committed
110
111
    }
};
112
113
114
115
} // namespace

void prefuse_ops::apply(module& m) const
{
Paul's avatar
Paul committed
116
    match::find_matches(m, find_add_layernorm{}, find_layernorm{});
117
118
119
120
121
}

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