parse_matmul.cpp 6.81 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.
 */
Paul Fultz II's avatar
Paul Fultz II committed
24
25
26
#include <migraphx/onnx/op_parser.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/instruction.hpp>
27
#include <migraphx/common.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <migraphx/make_op.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace onnx {

struct parse_matmul : op_parser<parse_matmul>
{
    std::vector<op_desc> operators() const
    {
        return {{"MatMul", "dot"}, {"MatMulInteger", "quant_dot"}};
    }

    instruction_ref parse(const op_desc& opd,
                          const onnx_parser& /*parser*/,
                          const onnx_parser::node_info& info,
                          std::vector<instruction_ref> args) const
    {
Charlie Lin's avatar
Charlie Lin committed
46
47
48
49
        auto a0 = args[0];
        auto a1 = args[1];
        auto s0 = a0->get_shape();
        auto s1 = a1->get_shape();
Paul Fultz II's avatar
Paul Fultz II committed
50

Charlie Lin's avatar
Charlie Lin committed
51
        instruction_ref dot_res;
Paul Fultz II's avatar
Paul Fultz II committed
52
        bool is_a_prepended = false;
Charlie Lin's avatar
Charlie Lin committed
53
54
        bool is_b_appended  = false;
        if(s0.ndim() == 1)
Paul Fultz II's avatar
Paul Fultz II committed
55
56
        {
            is_a_prepended = true;
Charlie Lin's avatar
Charlie Lin committed
57
            a0             = info.add_instruction(make_op("unsqueeze", {{"axes", {0}}}), args[0]);
Paul Fultz II's avatar
Paul Fultz II committed
58
        }
Charlie Lin's avatar
Charlie Lin committed
59
        if(s1.ndim() == 1)
Paul Fultz II's avatar
Paul Fultz II committed
60
61
        {
            is_b_appended = true;
Charlie Lin's avatar
Charlie Lin committed
62
            a1            = info.add_instruction(make_op("unsqueeze", {{"axes", {1}}}), args[1]);
Paul Fultz II's avatar
Paul Fultz II committed
63
64
        }

Gyula Zakor's avatar
Gyula Zakor committed
65
        auto is_quant_dot = opd.op_name == "quant_dot";
Charlie Lin's avatar
Charlie Lin committed
66
        if(s0.dynamic() or s1.dynamic())
Paul Fultz II's avatar
Paul Fultz II committed
67
        {
Gyula Zakor's avatar
Gyula Zakor committed
68
            if(is_quant_dot)
Charlie Lin's avatar
Charlie Lin committed
69
70
71
72
73
74
75
76
77
            {
                MIGRAPHX_THROW("PARSE_MATMUL: dynamic MatMulInteger not supported");
            }
            auto s0_dds = a0->get_shape().to_dynamic().dyn_dims();
            auto s1_dds = a1->get_shape().to_dynamic().dyn_dims();

            // TODO: handling this case requires a new multibroadcast mode
            if(not std::equal(
                   s0_dds.rbegin() + 2, s0_dds.rend(), s1_dds.rbegin() + 2, s1_dds.rend()))
Paul Fultz II's avatar
Paul Fultz II committed
78
            {
Charlie Lin's avatar
Charlie Lin committed
79
                MIGRAPHX_THROW("PARSE_MATMUL: dynamic shape broadcasting not supported");
Paul Fultz II's avatar
Paul Fultz II committed
80
            }
Charlie Lin's avatar
Charlie Lin committed
81
82
83
84
85
86
87
88
89
90
91
92

            dot_res = info.add_instruction(make_op(opd.op_name), a0, a1);
        }
        else
        {
            auto s0_lens        = a0->get_shape().lens();
            auto s1_lens        = a1->get_shape().lens();
            instruction_ref ba0 = a0;
            instruction_ref ba1 = a1;
            // try broadcasting if dimensions other than last two do not match
            if(not std::equal(
                   s0_lens.rbegin() + 2, s0_lens.rend(), s1_lens.rbegin() + 2, s1_lens.rend()))
Paul Fultz II's avatar
Paul Fultz II committed
93
            {
Charlie Lin's avatar
Charlie Lin committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                auto l0_it = s0_lens.begin() + s0_lens.size() - 2;
                std::vector<std::size_t> l0_broadcasted_lens(s0_lens.begin(), l0_it);
                auto l1_it = s1_lens.begin() + s1_lens.size() - 2;
                std::vector<std::size_t> l1_broadcasted_lens(s1_lens.begin(), l1_it);
                auto output_lens =
                    compute_broadcasted_lens(l0_broadcasted_lens, l1_broadcasted_lens);
                l0_broadcasted_lens = output_lens;
                l0_broadcasted_lens.insert(l0_broadcasted_lens.end(), l0_it, s0_lens.end());
                l1_broadcasted_lens = output_lens;
                l1_broadcasted_lens.insert(l1_broadcasted_lens.end(), l1_it, s1_lens.end());
                if(s0_lens != l0_broadcasted_lens)
                {
                    ba0 = info.add_instruction(
                        make_op("multibroadcast", {{"out_lens", l0_broadcasted_lens}}), a0);
                }
                if(s1_lens != l1_broadcasted_lens)
                {
                    ba1 = info.add_instruction(
                        make_op("multibroadcast", {{"out_lens", l1_broadcasted_lens}}), a1);
                }
Paul Fultz II's avatar
Paul Fultz II committed
114
            }
Gyula Zakor's avatar
Gyula Zakor committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

            // MatMulInteger can accept uint8 as input type or have zero point values
            // In these case fall back to dot with half float inputs
            auto ba0_type          = ba0->get_shape().type();
            auto ba1_type          = ba1->get_shape().type();
            auto has_a0_zero_point = args.size() > 2;
            auto has_a1_zero_point = args.size() > 3;
            if(is_quant_dot and (ba0_type == migraphx::shape::uint8_type or
                                 ba1_type == migraphx::shape::uint8_type or has_a0_zero_point))
            {
                // gpu implementation (gemm) only accepts floating point types for dot
                ba0 = info.add_instruction(
                    make_op("convert", {{"target_type", migraphx::shape::half_type}}), ba0);
                ba1 = info.add_instruction(
                    make_op("convert", {{"target_type", migraphx::shape::half_type}}), ba1);

                if(has_a0_zero_point)
                {
                    ba0 = info.add_common_op("sub", ba0, args[2]);
                }
                if(has_a1_zero_point)
                {
                    ba1 = info.add_common_op("sub", ba1, args[3]);
                }
                dot_res = info.add_instruction(make_op("dot"), ba0, ba1);
                dot_res = info.add_instruction(
                    make_op("convert", {{"target_type", migraphx::shape::int32_type}}), dot_res);
            }
            else
            {
                dot_res = info.add_instruction(make_op(opd.op_name), ba0, ba1);
            }
Paul Fultz II's avatar
Paul Fultz II committed
147
        }
Charlie Lin's avatar
Charlie Lin committed
148
149
150

        // squeeze the appended or prepended dimensions
        int64_t num_axis = dot_res->get_shape().ndim();
Paul Fultz II's avatar
Paul Fultz II committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        if(is_a_prepended)
        {
            dot_res = info.add_instruction(make_op("squeeze", {{"axes", {num_axis - 2}}}), dot_res);
            --num_axis;
        }
        if(is_b_appended)
        {
            dot_res = info.add_instruction(make_op("squeeze", {{"axes", {num_axis - 1}}}), dot_res);
        }

        return dot_res;
    }
};

} // namespace onnx
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx