"src/vscode:/vscode.git/clone" did not exist on "dcc7b0a5e896f843957b725c3f761d1b8bbb71a7"
ck_gemm.cpp 5.59 KB
Newer Older
Paul's avatar
Paul 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
/*
 * 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 <fstream>
#include <filesystem>
#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>
#include <migraphx/env.hpp>

Paul's avatar
Paul committed
41
42
#include "ck_gemm_instances.hpp"

Paul's avatar
Paul committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

namespace gpu {

// NOLINTNEXTLINE
static const char* const ck_gemm_kernel = R"__migraphx__(
#include <args.hpp>
#include <migraphx/kernels/ck_gemm.hpp>

#include <hip/hip_runtime_api.h>

namespace migraphx {

Paul's avatar
Paul committed
57
using gemm_t = CKDeviceGemm<${instance}, ${m}, ${k}, ${n}, ${sa}, ${sb}, ${sc}>;
Paul's avatar
Paul committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

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

extern "C" {

__global__ void ck_gemm_kernel(void* a_p, void* b_p, void* c_p)
{
    make_tensors()(a_p, b_p, c_p)([&](auto a_t, auto b_t, auto c_t) {
        constexpr ck::index_t shared_block_size =
            GridwiseGemm::GetSharedMemoryNumberOfByte();
        __shared__ char p_shared_block[shared_block_size];
        make_tensors()(p_shared_block)([&](auto p_t) {
            ck_gemm<gemm_t>(a_t, b_t, c_t, p_t);
        });
    });
}

}

} // namespace migraphx

)__migraphx__";

Paul's avatar
Paul committed
82
83
84
static std::size_t int_div_ceil(std::size_t x, std::size_t y) { return (x + y - 1) / y; }

static std::size_t block_size_index = 13;
Paul's avatar
Paul committed
85

Paul's avatar
Paul committed
86
static std::size_t get_block_size(const std::vector<std::string>& s)
Paul's avatar
Paul committed
87
{
Paul's avatar
Paul committed
88
    return std::stoull(s[block_size_index]);
Paul's avatar
Paul committed
89
90
}

Paul's avatar
Paul committed
91
static std::size_t get_grid_size(const std::vector<std::string>& s, std::size_t m, std::size_t n)
Paul's avatar
Paul committed
92
{
Paul's avatar
Format  
Paul committed
93
94
    auto mpb = std::stoull(s[block_size_index + 1]);
    auto npb = std::stoull(s[block_size_index + 2]);
Paul's avatar
Paul committed
95
96
    return int_div_ceil(m, mpb) * int_div_ceil(n, npb);
}
Paul's avatar
Paul committed
97
98
99

struct ck_gemm_compiler : compiler<ck_gemm_compiler>
{
Paul's avatar
Paul committed
100
101
    static std::string get_layout(const shape& s)
    {
Paul's avatar
Format  
Paul committed
102
103
        return s.transposed() ? "ck::tensor_layout::gemm::ColumnMajor"
                              : "ck::tensor_layout::gemm::RowMajor";
Paul's avatar
Paul committed
104
105
106
    }

    static std::string get_type(const shape& s)
Paul's avatar
Paul committed
107
    {
Paul's avatar
Format  
Paul committed
108
        if(s.type() == shape::half_type)
Paul's avatar
Paul committed
109
110
111
            return "ck::half_t";
        return shape::cpp_type(s.type());
    }
Paul's avatar
Paul committed
112
113
114
115
116

    std::vector<std::string> names() const { return {"ck_gemm", "gpu::ck_gemm"}; }

    operation compile_op(context& /* ctx */, const std::vector<shape>& inputs, const value& v) const
    {
Paul's avatar
Paul committed
117
118
119
        auto a_shape = inputs[0];
        auto b_shape = inputs[1];
        auto c_shape = inputs[2];
Paul's avatar
Paul committed
120

Paul's avatar
Format  
Paul committed
121
122
123
124
125
126
        auto m  = c_shape.lens().front();
        auto n  = c_shape.lens().back();
        auto k  = a_shape.lens().back();
        auto sa = a_shape.strides().front();
        auto sb = b_shape.strides().front();
        auto sc = c_shape.strides().front();
Paul's avatar
Paul committed
127

Paul's avatar
Format  
Paul committed
128
        int i                = v.get("tuning_val", 4);
Paul's avatar
Paul committed
129
        const auto& instance = get_instance(i, [&](const auto& x) -> bool {
Paul's avatar
Format  
Paul committed
130
131
132
            return get_layout(a_shape) == x[0] and get_layout(b_shape) == x[1] and
                   get_layout(c_shape) == x[2] and get_type(a_shape) == x[3] and
                   get_type(b_shape) == x[4] and get_type(c_shape) == x[5];
Paul's avatar
Paul committed
133
        });
Paul's avatar
Paul committed
134

Paul's avatar
Paul committed
135
136
        hip_compile_options options;
        options.set_launch_params(v, get_grid_size(instance, m, n), get_block_size(instance));
Paul's avatar
Paul committed
137
        options.inputs         = inputs;
Paul's avatar
Paul committed
138
        options.output         = c_shape;
Paul's avatar
Paul committed
139
140
141
        options.kernel_name    = "ck_gemm_kernel";
        options.virtual_inputs = inputs;

Paul's avatar
Format  
Paul committed
142
        auto src = interpolate_string(ck_gemm_kernel,
Paul's avatar
Paul committed
143
                                      {{"instance", join_strings(instance, ",")},
Paul's avatar
Format  
Paul committed
144
145
146
147
148
149
150
                                       {"m", to_string(m)},
                                       {"k", to_string(k)},
                                       {"n", to_string(n)},
                                       {"sa", to_string(sa)},
                                       {"sb", to_string(sb)},
                                       {"sc", to_string(sc)}});

Paul's avatar
Paul committed
151
152
153
154
155
156
157
158
159
160
161
162
        return compile_hip_code_object(src, options);
    }

    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