quant_gemm.cpp 6.29 KB
Newer Older
Shucai Xiao's avatar
Shucai Xiao committed
1
#include <migraphx/gpu/quant_gemm.hpp>
2
#include <migraphx/gpu/device/pack.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
3
#include <migraphx/gpu/context.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
4
#include <migraphx/generate.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
5
6
7
8
9
10

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

template <class... Ts>
11
rocblas_status generic_rocblas_gemm_ex(Ts&&... xs)
Shucai Xiao's avatar
Shucai Xiao committed
12
{
13
    return rocblas_gemm_ex(std::forward<Ts>(xs)...);
Shucai Xiao's avatar
Shucai Xiao committed
14
15
16
}

template <class... Ts>
17
rocblas_status generic_rocblas_batched_gemm_ex(Ts&&... xs)
Shucai Xiao's avatar
Shucai Xiao committed
18
{
19
    return rocblas_gemm_strided_batched_ex(std::forward<Ts>(xs)...);
Shucai Xiao's avatar
Shucai Xiao committed
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
47
48
49
50
51
52
53
54
55
56
}

template <class T>
struct compute_rocblas_type
{
    using type = T;
};

template <class T>
struct compute_rocblas_type<const T>
{
    using type = const typename compute_rocblas_type<T>::type;
};

template <>
struct compute_rocblas_type<half>
{
    using type = rocblas_half;
};

template <class T>
using rb_type = typename compute_rocblas_type<T>::type;

template <class T>
rb_type<T> to_rocblas_type(T x)
{
    return reinterpret_cast<const rb_type<T>&>(x);
}

template <class T>
rb_type<T>* to_rocblas_type(T* x)
{
    return reinterpret_cast<rb_type<T>*>(x);
}

shape miopen_quant_gemm::compute_shape(const std::vector<shape>& inputs) const
{
57
58
    std::vector<shape> input_shapes(inputs);
    input_shapes.pop_back();
Shucai Xiao's avatar
Shucai Xiao committed
59
60
61
62
63
    check_shapes{input_shapes}.not_broadcasted();
    return op.compute_shape(input_shapes);
}

argument miopen_quant_gemm::compute(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
64
65
                                    const shape& output_shape,
                                    const std::vector<argument>& args) const
Shucai Xiao's avatar
Shucai Xiao committed
66
{
67
68
69
70
71
72
73
    bool transa     = args[0].get_shape().transposed();
    bool transb     = args[1].get_shape().transposed();
    auto n_dim      = output_shape.lens().size();
    auto dim_1      = n_dim - 1;
    auto dim_0      = n_dim - 2;
    rocblas_int lda = args[0].get_shape().strides()[transa ? dim_1 : dim_0];
    rocblas_int ldb = args[1].get_shape().strides()[transb ? dim_1 : dim_0];
Shucai Xiao's avatar
Shucai Xiao committed
74
    rocblas_int ldc = args[2].get_shape().strides()[dim_0];
75
76
77

    if(!transb)
    {
Shucai Xiao's avatar
Shucai Xiao committed
78
        if(arg_b.empty())
Shucai Xiao's avatar
Shucai Xiao committed
79
80
81
        {
            arg_b = allocate_gpu(args[1].get_shape());
        }
Shucai Xiao's avatar
Shucai Xiao committed
82
        device::pack_a(ctx.get_stream().get(), arg_b, args[1]);
83
84
    }

Shucai Xiao's avatar
Shucai Xiao committed
85
    // need to pack A in this scenario, use the algorithm to pack B in the
86
87
88
    // comment of the API
    if(transa)
    {
Shucai Xiao's avatar
Shucai Xiao committed
89
        if(arg_a.empty())
Shucai Xiao's avatar
Shucai Xiao committed
90
91
92
        {
            arg_a = allocate_gpu(args.at(0).get_shape());
        }
Shucai Xiao's avatar
Shucai Xiao committed
93
        device::pack_b(ctx.get_stream().get(), arg_a, args[0]);
94
95
    }

Shucai Xiao's avatar
Shucai Xiao committed
96
    bool is_3inputs = (args.size() == 4);
97
    int32_t beta     = 0;
Shucai Xiao's avatar
Shucai Xiao committed
98
99
100
101
102
103
104
105
    if(is_3inputs)
    {
        beta = op.beta;
    }

    auto a_lens = args[0].get_shape().lens();
    auto b_lens = args[1].get_shape().lens();
    output_shape.visit_type([&](auto as) {
Shucai Xiao's avatar
Shucai Xiao committed
106
107
108
109
110
111
        auto alpha_r    = to_rocblas_type(as(op.alpha));
        auto beta_r     = to_rocblas_type(as(beta));
        auto out_lens   = output_shape.lens();
        rocblas_int m   = out_lens[dim_0];
        rocblas_int n   = out_lens[dim_1];
        rocblas_int k   = args[0].get_shape().lens()[dim_1];
112
113
        auto to_pointer = [&](auto&& arg) { return to_rocblas_type(as.from(arg.data())); };
        assert(k % 4 == 0);
Shucai Xiao's avatar
Shucai Xiao committed
114

Shucai Xiao's avatar
Shucai Xiao committed
115
116
117
118
        auto num_matrices = std::accumulate(
            out_lens.rbegin() + 2, out_lens.rend(), std::size_t{1}, std::multiplies<std::size_t>());
        if(num_matrices == 1)
        {
Shucai Xiao's avatar
Shucai Xiao committed
119
            // the rocblas_gemm API handles inputs and output matrices as
Shucai Xiao's avatar
Shucai Xiao committed
120
121
122
            // column-major format. When doing a C = A * B, we actually do
            // C^T = (B^T) * (A^T). That is the reason we input args[1] as
            // A and args[0] as B in calling the rocblas_gemm.
123
            generic_rocblas_gemm_ex(ctx.get_stream().get_rocblas(),
124
                                    transb ? rocblas_operation_transpose : rocblas_operation_none,
125
                                    transa ? rocblas_operation_transpose : rocblas_operation_none,
126
                                    n,
127
                                    m,
Shucai Xiao's avatar
Shucai Xiao committed
128
129
                                    k,
                                    &alpha_r,
Shucai Xiao's avatar
Shucai Xiao committed
130
                                    (!transb) ? to_pointer(arg_b) : to_pointer(args.at(1)),
131
132
                                    rocblas_datatype_i8_r,
                                    ldb,
Shucai Xiao's avatar
Shucai Xiao committed
133
                                    transa ? to_pointer(arg_a) : to_pointer(args.at(0)),
134
135
                                    rocblas_datatype_i8_r,
                                    lda,
Shucai Xiao's avatar
Shucai Xiao committed
136
137
138
139
                                    &beta_r,
                                    to_pointer(args[2]),
                                    rocblas_datatype_i32_r,
                                    ldc,
Shucai Xiao's avatar
Shucai Xiao committed
140
                                    is_3inputs ? to_pointer(args.at(3)) : to_pointer(args[2]),
Shucai Xiao's avatar
Shucai Xiao committed
141
142
143
144
145
146
147
148
                                    rocblas_datatype_i32_r,
                                    ldc,
                                    rocblas_datatype_i32_r,
                                    rocblas_gemm_algo_standard,
                                    0,
                                    0,
                                    nullptr,
                                    nullptr);
Shucai Xiao's avatar
Shucai Xiao committed
149
150
151
        }
        else
        {
152
            generic_rocblas_batched_gemm_ex(
Shucai Xiao's avatar
Shucai Xiao committed
153
154
155
156
157
158
159
                ctx.get_stream().get_rocblas(),
                transb ? rocblas_operation_transpose : rocblas_operation_none,
                transa ? rocblas_operation_transpose : rocblas_operation_none,
                n,
                m,
                k,
                &alpha_r,
Shucai Xiao's avatar
Shucai Xiao committed
160
                (!transb) ? to_pointer(arg_b) : to_pointer(args.at(1)),
161
                rocblas_datatype_i8_r,
Shucai Xiao's avatar
Shucai Xiao committed
162
163
                ldb,
                k * n,
Shucai Xiao's avatar
Shucai Xiao committed
164
                transa ? to_pointer(arg_a) : to_pointer(args.at(0)),
165
                rocblas_datatype_i8_r,
Shucai Xiao's avatar
Shucai Xiao committed
166
167
168
                lda,
                m * k,
                &beta_r,
169
170
171
172
                to_pointer(args[2]),
                rocblas_datatype_i32_r,
                ldc,
                m * n,
Shucai Xiao's avatar
Shucai Xiao committed
173
                is_3inputs ? to_pointer(args.at(3)) : to_pointer(args[2]),
174
                rocblas_datatype_i32_r,
Shucai Xiao's avatar
Shucai Xiao committed
175
176
                ldc,
                m * n,
177
178
179
                num_matrices,
                rocblas_datatype_i32_r,
                rocblas_gemm_algo_standard,
Shucai Xiao's avatar
Shucai Xiao committed
180
181
182
183
                0,
                0,
                nullptr,
                nullptr);
Shucai Xiao's avatar
Shucai Xiao committed
184
185
186
        }
    });

Shucai Xiao's avatar
Shucai Xiao committed
187
    return is_3inputs ? args.at(3) : args[2];
Shucai Xiao's avatar
Shucai Xiao committed
188
189
190
191
192
}

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