rewrite_batched_gemms.cpp 6.07 KB
Newer Older
turneram's avatar
turneram committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * 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.
 */
#include <migraphx/rewrite_batched_gemms.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/op/broadcast.hpp>
#include <migraphx/op/add.hpp>
#include <migraphx/op/mul.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>

#include <migraphx/dfor.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

void rewrite_batched_gemms::apply(module& m) const
{
    for(auto ins : iterator_for(m))
    {
        if(ins->name() != "dot")
            continue;
turneram's avatar
turneram committed
45
46
47
48
        // std::cout << "Rewrite Batched GEMMS" << std::endl;
        // ins->debug_print();
        // m.debug_print();
        // return;
turneram's avatar
turneram committed
49
        auto inputs = ins->inputs();
turneram's avatar
turneram committed
50
51
        auto a_mat  = inputs.front();
        auto b_mat  = inputs.at(1); //.back()?
turneram's avatar
turneram committed
52
53
        auto a_lens = a_mat->get_shape().lens();
        auto b_lens = b_mat->get_shape().lens();
turneram's avatar
turneram committed
54
        if(a_lens.size() > 2)
turneram's avatar
turneram committed
55
56
57
        {
            auto batch_size = std::accumulate(
                a_lens.rbegin() + 2, a_lens.rend(), std::size_t{1}, std::multiplies<std::size_t>());
turneram's avatar
turneram committed
58
59
60
61
62
63
64
            auto reshape_a = m.insert_instruction(
                ins,
                make_op("reshape",
                        {{"dims", {batch_size * a_lens[a_lens.size() - 2], a_lens.back()}}}),
                a_mat);
            // reshape_a->debug_print();
            // std::cout << b_mat->get_operator().name() << std::endl;
turneram's avatar
turneram committed
65
            instruction_ref unbc_b;
turneram's avatar
turneram committed
66
            if(b_mat->get_operator().name() == "concat")
turneram's avatar
turneram committed
67
68
69
            {
                auto concat_inputs = b_mat->inputs();
                std::vector<instruction_ref> concat_lits;
turneram's avatar
turneram committed
70
                int concat_axis   = 1;
turneram's avatar
turneram committed
71
                bool return_early = false;
turneram's avatar
turneram committed
72
                for(auto c : concat_inputs)
turneram's avatar
turneram committed
73
                {
turneram's avatar
turneram committed
74
                    if(c->get_operator().name() == "contiguous")
turneram's avatar
turneram committed
75
                        c = c->inputs().front();
turneram's avatar
turneram committed
76
77
78
                    // std::cout << c->get_operator().name() << ", " << c->get_shape() << ", " <<
                    // c->get_shape().broadcasted() <<std::endl;
                    if(c->get_shape().broadcasted())
turneram's avatar
turneram committed
79
                    {
turneram's avatar
turneram committed
80
81
                        // std::cout << c->inputs().front()->get_operator() <<std::endl;
                        auto lit      = c->inputs().front();
turneram's avatar
turneram committed
82
                        auto lit_dims = lit->get_shape().lens().size();
turneram's avatar
turneram committed
83
                        if(lit_dims > 2)
turneram's avatar
turneram committed
84
85
86
87
88
                            return_early = true;
                        concat_axis = lit_dims - 1;
                        concat_lits.push_back(lit);
                    }
                }
turneram's avatar
turneram committed
89
                if(return_early)
turneram's avatar
turneram committed
90
                    continue;
turneram's avatar
turneram committed
91
92
                unbc_b = m.insert_instruction(
                    ins, make_op("concat", {{"axis", concat_axis}}), concat_lits);
turneram's avatar
turneram committed
93
            }
turneram's avatar
turneram committed
94
            else if(b_mat->get_operator().name() == "contiguous")
turneram's avatar
turneram committed
95
            {
turneram's avatar
turneram committed
96
97
                // std::cout << "Contiguous B" <<std::endl;
                // b_mat->debug_print();
turneram's avatar
turneram committed
98
                auto b_input = b_mat->inputs().front();
turneram's avatar
turneram committed
99
100
101
                // std::cout << b_input->get_operator().name() << ", " <<
                // b_input->get_shape().broadcasted() << ", " << b_input->can_eval() << std::endl;
                if(b_input->get_shape().broadcasted())
turneram's avatar
turneram committed
102
                {
turneram's avatar
turneram committed
103
                    auto lit      = b_input->inputs().front();
turneram's avatar
turneram committed
104
                    auto lit_dims = lit->get_shape().lens().size();
turneram's avatar
turneram committed
105
                    if(lit_dims > 2)
turneram's avatar
turneram committed
106
107
                        continue;
                    unbc_b = lit;
turneram's avatar
turneram committed
108
                    // unbc_b->debug_print();
turneram's avatar
turneram committed
109
110
111
112
                }
                else
                    continue;
            }
turneram's avatar
turneram committed
113
            else
turneram's avatar
turneram committed
114
            {
turneram's avatar
turneram committed
115
                // std::cout << "Else" << std::endl;
turneram's avatar
turneram committed
116
117
                continue;
            }
turneram's avatar
turneram committed
118
            auto new_dot  = m.insert_instruction(ins, make_op("dot"), reshape_a, unbc_b);
turneram's avatar
turneram committed
119
120
121
122
            auto out_lens = a_lens;
            out_lens.pop_back();
            out_lens.push_back(b_lens.back());

turneram's avatar
turneram committed
123
            // std::cout << std::next(ins)->get_operator().name() << std::endl;
turneram's avatar
turneram committed
124
            auto next_ins = std::next(ins);
turneram's avatar
turneram committed
125
            if(next_ins->get_operator().name() == "add")
turneram's avatar
turneram committed
126
            {
turneram's avatar
turneram committed
127
128
129
130
131
132
133
134
                auto add_in = next_ins->inputs().back() == ins ? next_ins->inputs().front()
                                                               : next_ins->inputs().back();
                // add_in->debug_print();
                auto reshape_add = m.insert_instruction(
                    next_ins,
                    make_op("reshape",
                            {{"dims", {batch_size * a_lens[a_lens.size() - 2], b_lens.back()}}}),
                    add_in);
turneram's avatar
turneram committed
135
136
                new_dot = m.replace_instruction(next_ins, make_op("add"), reshape_add, new_dot);
            }
turneram's avatar
turneram committed
137
            // std::cout << "here" <<std::endl;
turneram's avatar
turneram committed
138
139
140
            m.replace_instruction(ins, make_op("reshape", {{"dims", out_lens}}), new_dot);
        }

turneram's avatar
turneram committed
141
        // m.debug_print();
turneram's avatar
turneram committed
142
143
144
145
146
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx