rewrite_gemm.cpp 4.04 KB
Newer Older
Khalique Ahmed's avatar
Khalique Ahmed 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
45
46
/*
 * 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_gemm.hpp>
#include <migraphx/program.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/serialize.hpp>

#include <migraphx/make_op.hpp>

#include <migraphx/pass_config.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

void rewrite_gemm::apply(module& m) const
{
    for(auto ins : iterator_for(m))
    {
        if(ins->name() != "dot")
            continue;
        auto inputs = ins->inputs();
Khalique Ahmed's avatar
Khalique Ahmed committed
47
        auto in0    = inputs.at(0);
Khalique Ahmed's avatar
Khalique Ahmed committed
48

Khalique Ahmed's avatar
Khalique Ahmed committed
49
50
51
        if(in0->get_shape().lens().size() > 2)
            if(in0->get_shape().lens().at(0) != 1) // only batch size = 1
                continue;
Khalique Ahmed's avatar
Khalique Ahmed committed
52
53
54
55
56
        auto in_size = in0->get_shape().lens().size();
        if(in_size == 4 and in0->get_shape().lens().at(1) != 1)
        {
            continue;
        }
Khalique Ahmed's avatar
Khalique Ahmed committed
57
58
        auto in1 = inputs.at(1);

Khalique Ahmed's avatar
Khalique Ahmed committed
59
60
        if(in_size < 4)
        {
Khalique Ahmed's avatar
Khalique Ahmed committed
61
62
63
64
65
            std::vector<size_t> new_lens0(in0->get_shape().lens().begin(),
                                          in0->get_shape().lens().end());
            std::vector<size_t> new_lens1(in1->get_shape().lens().begin(),
                                          in1->get_shape().lens().end());
            std::vector<size_t> ones(4 - in_size, 1);
Khalique Ahmed's avatar
Khalique Ahmed committed
66
67
68
            new_lens0.insert(new_lens0.begin(), ones.begin(), ones.end());
            new_lens1.insert(new_lens1.begin(), ones.begin(), ones.end());
            if(not in0->get_shape().standard())
Khalique Ahmed's avatar
Khalique Ahmed committed
69
70
                in0 = m.insert_instruction(ins, make_op("contiguous"), in0);
            in0 = m.insert_instruction(ins, make_op("reshape", {{"dims", new_lens0}}), in0);
Khalique Ahmed's avatar
Khalique Ahmed committed
71
            if(not in1->get_shape().standard())
Khalique Ahmed's avatar
Khalique Ahmed committed
72
73
                in1 = m.insert_instruction(ins, make_op("contiguous"), in1);
            in1 = m.insert_instruction(ins, make_op("reshape", {{"dims", new_lens1}}), in1);
Khalique Ahmed's avatar
Khalique Ahmed committed
74
75
        }

Khalique Ahmed's avatar
Khalique Ahmed committed
76
77
78
79
80
        auto in0_transposed =
            m.insert_instruction(ins, make_op("transpose", {{"permutation", {0, 3, 1, 2}}}), in0);
        auto in1_transposed =
            m.insert_instruction(ins, make_op("transpose", {{"permutation", {3, 2, 1, 0}}}), in1);

Khalique Ahmed's avatar
Khalique Ahmed committed
81
82
83
84
        auto conv =
            m.insert_instruction(ins, make_op("convolution"), {in0_transposed, in1_transposed});
        auto conv_transpose =
            m.insert_instruction(ins, make_op("transpose", {{"permutation", {0, 2, 3, 1}}}), conv);
Khalique Ahmed's avatar
Khalique Ahmed committed
85

Khalique Ahmed's avatar
Khalique Ahmed committed
86
        auto out_lens           = conv_transpose->get_shape().lens();
Khalique Ahmed's avatar
Khalique Ahmed committed
87
        auto conv_transpose_out = conv_transpose;
Khalique Ahmed's avatar
Khalique Ahmed committed
88
89
        if(out_lens.size() != in_size)
        {
Khalique Ahmed's avatar
Khalique Ahmed committed
90
            out_lens.erase(out_lens.begin(), out_lens.begin() + (out_lens.size() - in_size));
Khalique Ahmed's avatar
Khalique Ahmed committed
91
92
            conv_transpose_out =
                m.insert_instruction(ins, make_op("reshape", {{"dims", out_lens}}), conv_transpose);
Khalique Ahmed's avatar
Khalique Ahmed committed
93
        }
Khalique Ahmed's avatar
Khalique Ahmed committed
94
        m.replace_instruction(ins, conv_transpose_out);
Khalique Ahmed's avatar
Khalique Ahmed committed
95
96
97
98
99
    }
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx