quant_gemm.cpp 6.09 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
    check_shapes{input_shapes}.not_broadcasted();
Shucai Xiao's avatar
Shucai Xiao committed
60

Shucai Xiao's avatar
Shucai Xiao committed
61
62
63
64
    return op.compute_shape(input_shapes);
}

argument miopen_quant_gemm::compute(context& ctx,
Shucai Xiao's avatar
Shucai Xiao committed
65
66
                                    const shape& output_shape,
                                    const std::vector<argument>& args) const
Shucai Xiao's avatar
Shucai Xiao committed
67
{
68
69
70
71
72
73
74
    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
75
    rocblas_int ldc = args[2].get_shape().strides()[dim_0];
76
77
78

    if(!transb)
    {
Shucai Xiao's avatar
Shucai Xiao committed
79
        device::pack_a(ctx.get_stream().get(), arg_b, args[1]);
80
81
    }

Shucai Xiao's avatar
Shucai Xiao committed
82
    // need to pack A in this scenario, use the algorithm to pack B in the
83
84
85
    // comment of the API
    if(transa)
    {
Shucai Xiao's avatar
Shucai Xiao committed
86
        device::pack_b(ctx.get_stream().get(), arg_a, args[0]);
87
88
    }

Shucai Xiao's avatar
Shucai Xiao committed
89
    bool is_3inputs = (args.size() == 4);
Shucai Xiao's avatar
Shucai Xiao committed
90
    int32_t beta    = 0;
Shucai Xiao's avatar
Shucai Xiao committed
91
92
93
94
95
96
97
98
    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
99
100
101
102
103
104
        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];
105
106
        auto to_pointer = [&](auto&& arg) { return to_rocblas_type(as.from(arg.data())); };
        assert(k % 4 == 0);
Shucai Xiao's avatar
Shucai Xiao committed
107

Shucai Xiao's avatar
Shucai Xiao committed
108
109
110
111
        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
112
            // the rocblas_gemm API handles inputs and output matrices as
Shucai Xiao's avatar
Shucai Xiao committed
113
114
115
            // 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.
116
            generic_rocblas_gemm_ex(ctx.get_stream().get_rocblas(),
117
                                    transb ? rocblas_operation_transpose : rocblas_operation_none,
118
                                    transa ? rocblas_operation_transpose : rocblas_operation_none,
119
                                    n,
120
                                    m,
Shucai Xiao's avatar
Shucai Xiao committed
121
122
                                    k,
                                    &alpha_r,
Shucai Xiao's avatar
Shucai Xiao committed
123
                                    (!transb) ? to_pointer(arg_b) : to_pointer(args.at(1)),
124
125
                                    rocblas_datatype_i8_r,
                                    ldb,
Shucai Xiao's avatar
Shucai Xiao committed
126
                                    transa ? to_pointer(arg_a) : to_pointer(args.at(0)),
127
128
                                    rocblas_datatype_i8_r,
                                    lda,
Shucai Xiao's avatar
Shucai Xiao committed
129
130
131
132
                                    &beta_r,
                                    to_pointer(args[2]),
                                    rocblas_datatype_i32_r,
                                    ldc,
Shucai Xiao's avatar
Shucai Xiao committed
133
                                    is_3inputs ? to_pointer(args.at(3)) : to_pointer(args[2]),
Shucai Xiao's avatar
Shucai Xiao committed
134
135
136
137
138
139
140
141
                                    rocblas_datatype_i32_r,
                                    ldc,
                                    rocblas_datatype_i32_r,
                                    rocblas_gemm_algo_standard,
                                    0,
                                    0,
                                    nullptr,
                                    nullptr);
Shucai Xiao's avatar
Shucai Xiao committed
142
143
144
        }
        else
        {
145
            generic_rocblas_batched_gemm_ex(
Shucai Xiao's avatar
Shucai Xiao committed
146
147
148
149
150
151
152
                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
153
                (!transb) ? to_pointer(arg_b) : to_pointer(args.at(1)),
154
                rocblas_datatype_i8_r,
Shucai Xiao's avatar
Shucai Xiao committed
155
156
                ldb,
                k * n,
Shucai Xiao's avatar
Shucai Xiao committed
157
                transa ? to_pointer(arg_a) : to_pointer(args.at(0)),
158
                rocblas_datatype_i8_r,
Shucai Xiao's avatar
Shucai Xiao committed
159
160
161
                lda,
                m * k,
                &beta_r,
162
163
164
165
                to_pointer(args[2]),
                rocblas_datatype_i32_r,
                ldc,
                m * n,
Shucai Xiao's avatar
Shucai Xiao committed
166
                is_3inputs ? to_pointer(args.at(3)) : to_pointer(args[2]),
167
                rocblas_datatype_i32_r,
Shucai Xiao's avatar
Shucai Xiao committed
168
169
                ldc,
                m * n,
170
171
172
                num_matrices,
                rocblas_datatype_i32_r,
                rocblas_gemm_algo_standard,
Shucai Xiao's avatar
Shucai Xiao committed
173
174
175
176
                0,
                0,
                nullptr,
                nullptr);
Shucai Xiao's avatar
Shucai Xiao committed
177
178
179
        }
    });

Shucai Xiao's avatar
Shucai Xiao committed
180
    return is_3inputs ? args.at(3) : args[2];
Shucai Xiao's avatar
Shucai Xiao committed
181
182
183
184
185
}

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