ck_gemm.cpp 9.91 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
/*
 * 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.
 */
Alan Turner's avatar
Alan Turner committed
24
25
#include <fstream>
#include <filesystem>
turneram's avatar
turneram committed
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <migraphx/gpu/compiler.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/gpu/context.hpp>

#include <migraphx/gpu/compile_hip_code_object.hpp>
#include <migraphx/gpu/compile_hip.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/reduce_dims.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_common_subexpression.hpp>
#include <migraphx/module.hpp>
#include <migraphx/pass_manager.hpp>
Alan Turner's avatar
Alan Turner committed
39
#include <migraphx/env.hpp>
turneram's avatar
turneram committed
40
41
42

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Alan Turner's avatar
Alan Turner committed
43

turneram's avatar
turneram committed
44
45
46
47
48
namespace gpu {

// NOLINTNEXTLINE
static const char* const ck_gemm_kernel = R"__migraphx__(
#include <args.hpp>
Alan Turner's avatar
Alan Turner committed
49
#include <migraphx/kernels/ck_gemm2.hpp>
turneram's avatar
turneram committed
50

turneram's avatar
turneram committed
51
52
#include <hip/hip_runtime_api.h>

turneram's avatar
turneram committed
53
54
namespace migraphx {

Alan Turner's avatar
Alan Turner committed
55
56
57
58
59
using gemm_t = ${instance}, ${m}, ${k}, ${n}, ${sa}, ${sb}, ${sc}>;

constexpr __device__ gemm_t ckdg{};
using GridwiseGemm = decltype(ckdg.gridwisegemm);

turneram's avatar
turneram committed
60
61
extern "C" {

turneram's avatar
turneram committed
62
__global__ void ck_gemm_kernel(void* a_p, void* b_p, void* c_p)
turneram's avatar
turneram committed
63
{
Alan Turner's avatar
Alan Turner committed
64
    make_tensors()(a_p, b_p, c_p)([&](auto a_t, auto b_t, auto c_t) {
turneram's avatar
turneram committed
65
        constexpr ck::index_t shared_block_size =
Alan Turner's avatar
Alan Turner committed
66
            GridwiseGemm::GetSharedMemoryNumberOfByte();
turneram's avatar
turneram committed
67
        __shared__ char p_shared_block[shared_block_size];
turneram's avatar
turneram committed
68
        make_tensors()(p_shared_block)([&](auto p_t) {
Alan Turner's avatar
Alan Turner committed
69
            ck_gemm<gemm_t>(a_t, b_t, c_t, p_t);
turneram's avatar
turneram committed
70
        });
turneram's avatar
turneram committed
71
    });
turneram's avatar
turneram committed
72
73
74
75
76
77
78
79
}

}

} // namespace migraphx

)__migraphx__";

Alan Turner's avatar
Alan Turner committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

std::size_t int_div_ceil(std::size_t x, std::size_t y)
{
    return (x + y - 1) / y;
}

std::size_t get_grid_size(std::size_t m, std::size_t mpb, std::size_t n, std::size_t npb)
{
    return int_div_ceil(m, mpb) * int_div_ceil(n, npb);
}

struct block_settings
{
    int bs;
    int mpb;
    int npb;
};

namespace fs = std::filesystem;

turneram's avatar
turneram committed
100
101
struct ck_gemm_compiler : compiler<ck_gemm_compiler>
{
Alan Turner's avatar
Alan Turner committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    const std::vector<std::string> instances{
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 256, 128, 32, 8, 2, 32, 32, 4, 2, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<8, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 256, 128, 32, 8, 8, 32, 32, 4, 2, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 64, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 2, 8, 1, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 128, 256, 32, 8, 2, 32, 32, 2, 4, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 64, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 128, 256, 32, 8, 8, 32, 32, 2, 4, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 64, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 8, 1, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 128, 128, 32, 8, 2, 32, 32, 2, 2, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<8, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 128, 128, 32, 8, 8, 32, 32, 2, 2, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 64, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 2, 8, 1, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 128, 64, 32, 8, 2, 32, 32, 2, 1, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<16,16, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 128, 64, 32, 8, 8, 32, 32, 2, 1, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 64, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 1, 8, 1, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 64, 128, 32, 8, 2, 32, 32, 1, 2, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<8, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 256, 64, 128, 32, 8, 8, 32, 32, 1, 2, S<4, 64, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 64, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 2, 8, 1, 1, 1, S<1, 32, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 128, 128, 128, 32, 8, 2, 32, 32, 4, 2, S<4, 32, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 16, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 128, 128, 128, 32, 8, 8, 32, 32, 4, 2, S<4, 32, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 8, 1, 1, 1, S<1, 16, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 128, 128, 64, 32, 8, 2, 32, 32, 2, 2, S<4, 32, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<8, 16, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 32, 1, 4>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 128, 128, 64, 32, 8, 8, 32, 32, 2, 2, S<4, 32, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 2, 8, 1, 1, 1, S<1, 32, 1, 4>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 128, 64, 128, 32, 8, 2, 32, 32, 2, 2, S<4, 32, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 2, 0, 1, 1, S<1, 16, 1, 8>, 8",
        "       CKDeviceGemm< Row, Row, Row, F16, F16, F16, F32, F16, PassThrough, PassThrough, PassThrough, GemmDefault, 1, 128, 64, 128, 32, 8, 8, 32, 32, 2, 2, S<4, 32, 1>, S<1, 0, 2>, S<1, 0, 2>, 2, 8, 8, 1, S<4, 32, 1>, S<0, 2, 1>, S<0, 2, 1>, 1, 4, 8, 1, 1, 1, S<1, 16, 1, 8>, 8"};

    const std::vector<block_settings> params {
        {256, 256, 128},
        {256, 256, 128},
        {256, 128, 256},
        {256, 128, 256},
        {256, 128, 128},
        {256, 128, 128},
        {256, 128, 64},
        {256, 128, 64},
        {256, 64, 128},
        {256, 64, 128},
        {128, 128, 128},
        {128, 128, 128},
        {128, 128, 64},
        {128, 128, 64},
        {128, 64, 128},
        {128, 64, 128}};

turneram's avatar
turneram committed
138
139
    std::vector<std::string> names() const { return {"ck_gemm"}; }

Alan Turner's avatar
Alan Turner committed
140
    operation compile_op(context& /* ctx */, const std::vector<shape>& inputs, const value& v) const
turneram's avatar
turneram committed
141
    {
Alan Turner's avatar
Alan Turner committed
142
143
144
145
146
        int i = 4;
        if (contains(v, "tuning_val"))
            i = v.at("tuning_val").to<int>();
        assert(i >= 0 and i < instances.size());

turneram's avatar
turneram committed
147
148
        hip_compile_options options;
        auto out_s = inputs.back();
Alan Turner's avatar
Alan Turner committed
149
150
151
152
153
154
155
156
157
158

        auto b_s = params[i];
        auto block_size = b_s.bs;
        auto m_per_block = b_s.mpb;
        auto n_per_block = b_s.npb;
        auto m = out_s.lens().front();
        auto n = out_s.lens().back();
        auto grid_size = get_grid_size(m, m_per_block, n, n_per_block);

        options.set_launch_params(v, grid_size * block_size, block_size);
turneram's avatar
turneram committed
159
160
161
162
163
        options.inputs         = inputs;
        options.output         = out_s;
        options.kernel_name    = "ck_gemm_kernel";
        options.virtual_inputs = inputs;

Alan Turner's avatar
Alan Turner committed
164
165
166
167
168
169
170
171
172
173
174
175
176
        auto k = inputs.front().lens().back();
        auto sa = inputs.front().strides().front();
        auto sb = inputs.at(1).strides().front();
        auto sc = inputs.back().strides().front();
        auto src = interpolate_string(ck_gemm_kernel, {{"instance", instances[i]},
                                                       {"m", to_string(m)},
                                                       {"k", to_string(k)},
                                                       {"n", to_string(n)},
                                                       {"sa", to_string(sa)},
                                                       {"sb", to_string(sb)},
                                                       {"sc", to_string(sc)}});
        
        return compile_hip_code_object(src, options);
turneram's avatar
turneram committed
177
178
179
180
181
182
183
184
185
186
187
    }

    compiler_replace compile(context& ctx, instruction_ref ins, const operation& op) const
    {
        return replace(compile_op(ctx, to_shapes(ins->inputs()), op.to_value()));
    }
};

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