ck.hpp 8.63 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
3

Chao Liu's avatar
Chao Liu committed
4
#pragma once
5

6
7
#include "ck/config.h"

JD's avatar
JD committed
8
#ifndef CK_DONT_USE_HIP_RUNTIME_HEADERS
9
10
#include "hip/hip_runtime.h"
#include "hip/hip_fp16.h"
Chao Liu's avatar
Chao Liu committed
11
#endif
12

Chao Liu's avatar
Chao Liu committed
13
14
#define CK_TIME_KERNEL 1

15
16
17
// constant address space for kernel parameter
// https://llvm.org/docs/AMDGPUUsage.html#address-spaces
#define CK_CONSTANT_ADDRESS_SPACE __attribute__((address_space(4)))
Chao Liu's avatar
Chao Liu committed
18
19

// launch bounds
zjing14's avatar
zjing14 committed
20
#define CK_USE_LAUNCH_BOUNDS 1
Chao Liu's avatar
Chao Liu committed
21
22

#ifdef CK_USE_LAUNCH_BOUNDS
23
// for most kernels
Chao Liu's avatar
Chao Liu committed
24
#define CK_MAX_THREAD_PER_BLOCK 256
Chao Liu's avatar
Chao Liu committed
25
#define CK_MIN_BLOCK_PER_CU 2
26
27
28
29

// for wavelet GEMM kernel
#define CK_WAVELET_MAX_THREAD_PER_BLOCK 512
#define CK_WAVELET_MIN_BLOCK_PER_CU 2
Chao Liu's avatar
Chao Liu committed
30
31
#endif

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// kernel attribute: amdgpu_waves_per_eu()
#ifdef CK_USE_WAVES_PER_EU
// for 1-wave kernels, control arguments of amdgpu_waves_per_eu() attribute
#ifndef CK_MIN_WAVES_PER_EU
#define CK_MIN_WAVES_PER_EU 0
#endif

#ifndef CK_MAX_WAVES_PER_EU
#define CK_MAX_WAVES_PER_EU 0
#endif

#else
#define CK_USE_WAVES_PER_EU 0
#endif

47
// define general macros for various architectures
illsilin's avatar
illsilin committed
48
#if defined(__gfx940__) || defined(__gfx941__) || defined(__gfx942__) || defined(__gfx950__)
49
50
51
52
53
54
55
56
57
58
59
60
61
#define __gfx94__
#endif
#if defined(__gfx1010__) || defined(__gfx1011__) || defined(__gfx1012__)
#define __gfx101__
#endif
#if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || \
    defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__)
#define __gfx103__
#endif
#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__)
#define __gfx11__
#endif

Chao Liu's avatar
Chao Liu committed
62
// buffer resource
63
64
65
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_BUFFER_RESOURCE_3RD_DWORD -1
#elif defined(__gfx803__) || defined(__gfx900__) || defined(__gfx906__) || defined(__gfx908__) || \
66
    defined(__gfx90a__) || defined(__gfx94__)
Chao Liu's avatar
Chao Liu committed
67
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x00020000
68
#elif defined(__gfx103__)
Chao Liu's avatar
Chao Liu committed
69
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x31014000
70
#elif defined(__gfx11__)
71
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x31004000
Chao Liu's avatar
Chao Liu committed
72
73
#endif

74
// FMA instruction
75
76
#ifndef __HIP_DEVICE_COMPILE__                   // for host code, define nothing
#elif defined(__gfx803__) || defined(__gfx900__) // for GPU code
77
#define CK_USE_AMD_V_MAC_F32
78
79
#elif defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx103__) || \
    defined(__gfx94__) // for GPU code
80
81
82
#define CK_USE_AMD_V_FMAC_F32
#define CK_USE_AMD_V_DOT2_F32_F16
#define CK_USE_AMD_V_DOT4_I32_I8
83
#elif defined(__gfx11__)
84
85
86
#define CK_USE_AMD_V_FMAC_F32
#define CK_USE_AMD_V_DOT2_F32_F16
#define CK_USE_AMD_V_DOT4_I32_I8_GFX11
87
88
#endif

89
90
91
// MFMA instruction
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_MFMA
92
#elif defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx94__) // for GPU code
93
#define CK_USE_AMD_MFMA
94
95
#endif

96
#if(defined(__gfx90a__) || defined(__gfx94__))
97
#define CK_USE_AMD_MFMA_BF16_1K_OP
Chao Liu's avatar
Chao Liu committed
98
99
#endif

100
#if defined(__gfx94__)
101
102
103
#define CK_USE_AMD_MFMA_GFX940
#endif

104
105
106
// WMMA instruction
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_WMMA
107
#elif defined(__gfx11__) // for GPU code
108
109
110
#define CK_USE_AMD_WMMA
#endif

111
// buffer load
Jianfeng Yan's avatar
Jianfeng Yan committed
112
#define CK_USE_AMD_BUFFER_LOAD 1
113

114
// buffer store
Jianfeng Yan's avatar
Jianfeng Yan committed
115
116
#define CK_USE_AMD_BUFFER_STORE 1

117
118
// buffer atomic add: integer
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_INTEGER 1
119

120
121
122
// buffer atomic add: floating point
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 1
123
#elif defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx94__) // for GPU code
124
125
126
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 1
#else // for GPU code
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 0
127
128
#endif

129
#if(defined(__gfx90a__) || defined(__gfx94__)) // for GPU code
rocking5566's avatar
rocking5566 committed
130
131
132
133
134
#define CK_USE_AMD_BUFFER_ATOMIC_MAX_FLOAT64 1
#else
#define CK_USE_AMD_BUFFER_ATOMIC_MAX_FLOAT64 0
#endif

135
136
137
// inline asm
#define CK_USE_AMD_INLINE_ASM 1

138
139
140
141
142
143
// inner product (V_MAC/V_FMAC)
#define CK_USE_AMD_V_MAC_INLINE_ASM 1

// V_DOT inline instructions, less efficient since they require adding
// `s_nop`s to avoid hazard
#define CK_USE_AMD_V_DOT_INLINE_ASM 0
144

145
146
147
// inner product using V_DOT with DPP8 modifiers
#define CK_USE_AMD_V_DOT_DPP8_INLINE_ASM 1

148
149
150
// LDS direct loads using inline assembly
#define CK_USE_AMD_LDS_DIRECT_LOAD_INLINE_ASM 1

151
152
153
// set stochastic rounding as default for f8 conversions
#define CK_USE_SR_F8_CONVERSION 1

Chao Liu's avatar
Chao Liu committed
154
// block synchronization only s_wait lgkmcnt(0), not vmcnt(0)
155
#define CK_EXPERIMENTAL_BLOCK_SYNC_LDS_WITHOUT_SYNC_VMEM 1
Chao Liu's avatar
Chao Liu committed
156

157
158
// experimental feature: multi index implemented as array
#define CK_EXPERIMENTAL_USE_DYNAMICALLY_INDEXED_MULTI_INDEX 0
Chao Liu's avatar
Chao Liu committed
159

160
161
// experimental feature: static tensor descriptor
#define CK_EXPERIMENTAL_STATIC_TENSOR_DESCRIPTOR 0
Chao Liu's avatar
Chao Liu committed
162

rocking5566's avatar
rocking5566 committed
163
// experimental feature: buffer load/store/atomic-add/ OOB trick
164
// This (ifndef) is a hack to use customized behavior for buffer load rather than using default
Po Yen Chen's avatar
Po Yen Chen committed
165
166
// setting. Don't use this hack unless absolutely necessary!
// FIXME: make the behavior of buffer load a configurable (template) parameter for each usage
167
#ifndef CK_EXPERIMENTAL_USE_BUFFER_LOAD_OOB_CHECK_OFFSET_TRICK
168
#define CK_EXPERIMENTAL_USE_BUFFER_LOAD_OOB_CHECK_OFFSET_TRICK 0
169
#endif
170
#define CK_EXPERIMENTAL_USE_BUFFER_STORE_OOB_CHECK_OFFSET_TRICK 1
zjing14's avatar
zjing14 committed
171
#define CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_ADD_OOB_CHECK_OFFSET_TRICK 1
rocking5566's avatar
rocking5566 committed
172
#define CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_MAX_OOB_CHECK_OFFSET_TRICK 1
173

174
// experimental feature: in-regsiter sub-dword transpose
175
#define CK_EXPERIMENTAL_USE_IN_REGISTER_SUB_DWORD_TRANSPOSE 1
Chao Liu's avatar
Chao Liu committed
176

177
// experimental feature: merge transformation use magic number division
178
#define CK_EXPERIMENTAL_MERGE_USE_MAGIC_DIVISION 1
179

180
181
// experimental feature: use __builtin_memcpy instead of pointer cast to access a vector from
// pointer of scalar
182
183
#define CK_EXPERIMENTAL_USE_MEMCPY_FOR_VECTOR_ACCESS 0

184
// experimental feature: use __builtin_memcpy instead of union to do bit_cast
185
#define CK_EXPERIMENTAL_USE_MEMCPY_FOR_BIT_CAST 1
186

187
// experimental feature: optimize for inter-wave scheduling policy
188
#define CK_EXPERIMENTAL_INTER_WAVE_SCHEDULING 1
189
#define CK_EXPERIMENTAL_INTER_WAVE_SCHEDULING_MAC_CLUSTERS 1
190
191
192
193
194
195
// this will let make_default_loop_scheduler() return interwave scheduling flag by default
#define CK_EXPERIMENTAL_DEFAULT_TO_INTER_WAVE_SCHEDULING 0
// experimental feature: add instances using interwave scheduling
#define CK_EXPERIMENTAL_INTER_WAVE_INSTANCES 1
// experimental feature: add instances using pipeline v2
#define CK_EXPERIMENTAL_PIPELINE_V2_INSTANCES 1
196
197
198
199
// experimental feature: optimize pipeline v2 by IGLP strategy (value=ID of strategy)
#ifndef CK_EXPERIMENTAL_PIPELINE_V2_IGLP_OPT
#define CK_EXPERIMENTAL_PIPELINE_V2_IGLP_OPT 0
#endif
200

Chao Liu's avatar
Chao Liu committed
201
202
203
204
// hack: have underlying assumption that need to be satsified, otherwise it's a bug
// hack for forcing register to keep idx_diff_low_const in SGPR. idx_diff_low_const must be
// thread-invariant, otherwise it's a bug
// TODO: separate index calculation into "compile-time", "global", "block", "wave", "thread"
Chao Liu's avatar
rename  
Chao Liu committed
205
#define CK_HACK_MERGE_CALCULATE_IDX_DIFF_LOW_CONST_USE_AMD_GCN_READ_FIRST_LANE 0
Chao Liu's avatar
Chao Liu committed
206

207
// workaround: compiler crash when compiling recursive lambda
Chao Liu's avatar
Chao Liu committed
208
#define CK_WORKAROUND_SWDEV_275126 1
209

210
// workaround: compiler crash when using buffer load/store for i8
211
212
#define CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE 1

213
// workaround: compiler gnerating inefficient ds_write instructions
214
#define CK_WORKAROUND_SWDEV_XXXXXX_INT8_DS_WRITE_ISSUE 1
Chao Liu's avatar
Chao Liu committed
215

216
// workaround: verifaction failure, due to compiler regression, for conv bwd-data fp16 using some
217
// tuning parameter
218
#define CK_WORKAROUND_SWDEV_325164 0
219

220
221
222
// workaround: compiler not emiting reciprocal instruction frm __frcp_rn()
#define CK_WORKAROUND_SWDEV_383542 1

ltqin's avatar
ltqin committed
223
224
// workaround: compiler issue on gfx908
#define CK_WORKAROUND_SWDEV_388832 1
225

226
227
228
// flag to enable (1) or disable (0) the debugging output in some kernels
#define DEBUG_LOG 0

229
230
231
// denorm test fix, required to work around dissue
#ifndef CK_WORKAROUND_DENORM_FIX
#define CK_WORKAROUND_DENORM_FIX 0
Illia Silin's avatar
Illia Silin committed
232
#else
233
234
235
// enable only on MI200
#define CK_WORKAROUND_DENORM_FIX = CK_WORKAROUND_DENORM_FIX && defined(__gfx90a__)
#endif // CK_WORKAROUND_DENORM_FIX
236

237
238
namespace ck {

239
enum struct InMemoryDataOperationEnum
Chao Liu's avatar
Chao Liu committed
240
{
Chao Liu's avatar
Chao Liu committed
241
    Set,
242
    AtomicAdd,
rocking5566's avatar
rocking5566 committed
243
    AtomicMax,
244
245
246
    Add
};

Chao Liu's avatar
Chao Liu committed
247
// FIXME: use regular Sequence and remove this
rocking5566's avatar
rocking5566 committed
248
249
250
251
252
253
254
255
256
257
258
259
260
template <InMemoryDataOperationEnum... Is>
struct InMemoryDataOperationEnumSequence
{
    static constexpr int mSize = sizeof...(Is);

    __host__ __device__ static constexpr InMemoryDataOperationEnum At(int I)
    {
        // the last dummy element is to prevent compiler complain about empty array, when mSize = 0
        const InMemoryDataOperationEnum mData[mSize + 1] = {Is..., InMemoryDataOperationEnum::Set};
        return mData[I];
    }
};

Chao Liu's avatar
Chao Liu committed
261
// index type
Jianfeng Yan's avatar
Jianfeng Yan committed
262
263
using index_t      = int32_t;
using long_index_t = int64_t;
264
265

} // namespace ck