"examples/vqgan/requirements.txt" did not exist on "67ec9cf513fc314639f0ad91b11a20e0aab32a8f"
ck.hpp 9.52 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2025, 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
#ifdef _HIPCC_RTC_
#define CK_CODE_GEN_RTC
#endif
8

9
#include "ck/config.h"
Mirza Halilcevic's avatar
Mirza Halilcevic committed
10

11
#ifndef __HIPCC_RTC__
Mirza Halilcevic's avatar
Mirza Halilcevic committed
12
#include "ck/utility/env.hpp"
arai713's avatar
arai713 committed
13
#ifndef CK_CODE_GEN_RTC
JD's avatar
JD committed
14
#ifndef CK_DONT_USE_HIP_RUNTIME_HEADERS
15
16
#include "hip/hip_runtime.h"
#include "hip/hip_fp16.h"
Chao Liu's avatar
Chao Liu committed
17
#endif
18
#endif
19
20
21
22

// environment variable to enable logging:
// export CK_LOGGING=ON or CK_LOGGING=1 or CK_LOGGING=ENABLED
CK_DECLARE_ENV_VAR_BOOL(CK_LOGGING)
arai713's avatar
arai713 committed
23
#endif
24
25
// to do: add various levels of logging with CK_LOG_LEVEL

26
#ifndef CK_TIME_KERNEL
Chao Liu's avatar
Chao Liu committed
27
#define CK_TIME_KERNEL 1
28
#endif
Chao Liu's avatar
Chao Liu committed
29

30
31
32
// 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
33
34

// launch bounds
zjing14's avatar
zjing14 committed
35
#define CK_USE_LAUNCH_BOUNDS 1
Chao Liu's avatar
Chao Liu committed
36
37

#ifdef CK_USE_LAUNCH_BOUNDS
38
// for most kernels
Chao Liu's avatar
Chao Liu committed
39
#define CK_MAX_THREAD_PER_BLOCK 256
Chao Liu's avatar
Chao Liu committed
40
#define CK_MIN_BLOCK_PER_CU 2
41
42
43
44

// 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
45
46
#endif

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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

62
// define general macros for various architectures
63
#if defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx940__) || defined(__gfx941__) || \
Illia Silin's avatar
Illia Silin committed
64
    defined(__gfx942__) || defined(__gfx950__)
65
66
#define __gfx9__
#endif
Illia Silin's avatar
Illia Silin committed
67
#if defined(__gfx940__) || defined(__gfx941__) || defined(__gfx942__) || defined(__gfx950__)
68
69
70
71
72
73
#define __gfx94__
#endif
#if defined(__gfx1010__) || defined(__gfx1011__) || defined(__gfx1012__)
#define __gfx101__
#endif
#if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || \
74
75
    defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || \
    defined(__gfx10_3_generic__)
76
77
#define __gfx103__
#endif
78
79
#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || \
    defined(__gfx1103__) || defined(__gfx11_generic__)
80
81
#define __gfx11__
#endif
82
#if defined(__gfx1200__) || defined(__gfx1201__) || defined(__gfx12_generic__)
83
84
#define __gfx12__
#endif
85

Chao Liu's avatar
Chao Liu committed
86
// buffer resource
87
88
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_BUFFER_RESOURCE_3RD_DWORD -1
89
#elif defined(__gfx803__) || defined(__gfx900__) || defined(__gfx906__) || defined(__gfx9__)
Chao Liu's avatar
Chao Liu committed
90
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x00020000
91
#elif defined(__gfx103__)
Chao Liu's avatar
Chao Liu committed
92
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x31014000
93
#elif defined(__gfx11__) || defined(__gfx12__)
94
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x31004000
Chao Liu's avatar
Chao Liu committed
95
96
#endif

97
// FMA instruction
98
99
#ifndef __HIP_DEVICE_COMPILE__                   // for host code, define nothing
#elif defined(__gfx803__) || defined(__gfx900__) // for GPU code
100
#define CK_USE_AMD_V_MAC_F32
101
#elif defined(__gfx906__) || defined(__gfx9__) || defined(__gfx103__) // for GPU code
102
103
104
#define CK_USE_AMD_V_FMAC_F32
#define CK_USE_AMD_V_DOT2_F32_F16
#define CK_USE_AMD_V_DOT4_I32_I8
105
#elif defined(__gfx11__) || defined(__gfx12__)
106
107
108
#define CK_USE_AMD_V_FMAC_F32
#define CK_USE_AMD_V_DOT2_F32_F16
#define CK_USE_AMD_V_DOT4_I32_I8_GFX11
109
110
#endif

111
112
113
// MFMA instruction
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_MFMA
114
#elif defined(__gfx9__) // for GPU code
115
#define CK_USE_AMD_MFMA
116
117
#endif

118
#if(defined(__gfx90a__) || defined(__gfx94__))
119
#define CK_USE_AMD_MFMA_BF16_1K_OP
Chao Liu's avatar
Chao Liu committed
120
121
#endif

122
#if defined(__gfx94__)
123
124
125
#define CK_USE_AMD_MFMA_GFX940
#endif

126
// buffer load
Jianfeng Yan's avatar
Jianfeng Yan committed
127
#define CK_USE_AMD_BUFFER_LOAD 1
128

129
// buffer store
Jianfeng Yan's avatar
Jianfeng Yan committed
130
131
#define CK_USE_AMD_BUFFER_STORE 1

132
133
// buffer atomic add: integer
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_INTEGER 1
134

135
136
137
// buffer atomic add: floating point
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 1
138
#elif defined(__gfx9__) // for GPU code
139
140
141
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 1
#else // for GPU code
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 0
142
143
#endif

144
#if(defined(__gfx90a__) || defined(__gfx94__)) // for GPU code
rocking5566's avatar
rocking5566 committed
145
146
147
148
149
#define CK_USE_AMD_BUFFER_ATOMIC_MAX_FLOAT64 1
#else
#define CK_USE_AMD_BUFFER_ATOMIC_MAX_FLOAT64 0
#endif

150
151
152
// inline asm
#define CK_USE_AMD_INLINE_ASM 1

153
154
155
156
157
158
// 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
159

160
161
162
// inner product using V_DOT with DPP8 modifiers
#define CK_USE_AMD_V_DOT_DPP8_INLINE_ASM 1

163
// LDS direct loads using inline assembly
164
#define CK_USE_AMD_LDS_DIRECT_LOAD_INLINE_ASM 0
165

166
167
168
// set rounding to nearest even as default for bf16 conversions
#define CK_USE_RNE_BF16_CONVERSION 1

169
170
// set rounding to nearest even as default for f8 conversions
#define CK_USE_SR_F8_CONVERSION 0
171

Illia Silin's avatar
Illia Silin committed
172
173
174
175
176
177
// set rounding to nearest even as default for f6 conversions
#define CK_USE_SR_F6_CONVERSION 0

// set rounding to nearest even as default for f4 conversions
#define CK_USE_SR_F4_CONVERSION 0

178
179
180
181
// shuffle pk_i4 values during conversion to optimize number of binary
// operations
#define CK_USE_PK4_LAYOUT_SHUFFLE 1

Chao Liu's avatar
Chao Liu committed
182
// block synchronization only s_wait lgkmcnt(0), not vmcnt(0)
183
#define CK_EXPERIMENTAL_BLOCK_SYNC_LDS_WITHOUT_SYNC_VMEM 1
Chao Liu's avatar
Chao Liu committed
184

185
186
// experimental feature: multi index implemented as array
#define CK_EXPERIMENTAL_USE_DYNAMICALLY_INDEXED_MULTI_INDEX 0
Chao Liu's avatar
Chao Liu committed
187

188
189
// experimental feature: static tensor descriptor
#define CK_EXPERIMENTAL_STATIC_TENSOR_DESCRIPTOR 0
Chao Liu's avatar
Chao Liu committed
190

rocking5566's avatar
rocking5566 committed
191
// experimental feature: buffer load/store/atomic-add/ OOB trick
192
// 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
193
194
// setting. Don't use this hack unless absolutely necessary!
// FIXME: make the behavior of buffer load a configurable (template) parameter for each usage
195
#ifndef CK_EXPERIMENTAL_USE_BUFFER_LOAD_OOB_CHECK_OFFSET_TRICK
196
#define CK_EXPERIMENTAL_USE_BUFFER_LOAD_OOB_CHECK_OFFSET_TRICK 0
197
#endif
198
#define CK_EXPERIMENTAL_USE_BUFFER_STORE_OOB_CHECK_OFFSET_TRICK 1
zjing14's avatar
zjing14 committed
199
#define CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_ADD_OOB_CHECK_OFFSET_TRICK 1
rocking5566's avatar
rocking5566 committed
200
#define CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_MAX_OOB_CHECK_OFFSET_TRICK 1
201

202
// experimental feature: in-regsiter sub-dword transpose
203
#define CK_EXPERIMENTAL_USE_IN_REGISTER_SUB_DWORD_TRANSPOSE 1
Chao Liu's avatar
Chao Liu committed
204

205
// experimental feature: merge transformation use magic number division
206
#define CK_EXPERIMENTAL_MERGE_USE_MAGIC_DIVISION 1
207

208
209
// experimental feature: use __builtin_memcpy instead of pointer cast to access a vector from
// pointer of scalar
210
211
#define CK_EXPERIMENTAL_USE_MEMCPY_FOR_VECTOR_ACCESS 0

212
// experimental feature: use __builtin_memcpy instead of union to do bit_cast
213
#define CK_EXPERIMENTAL_USE_MEMCPY_FOR_BIT_CAST 1
214

215
// experimental feature: optimize for inter-wave scheduling policy
216
#define CK_EXPERIMENTAL_INTER_WAVE_SCHEDULING 1
217
#define CK_EXPERIMENTAL_INTER_WAVE_SCHEDULING_MAC_CLUSTERS 1
218
219
220
221
222
223
// 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
224
225
226
227
// 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
228

Chao Liu's avatar
Chao Liu committed
229
230
231
232
// 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
233
#define CK_HACK_MERGE_CALCULATE_IDX_DIFF_LOW_CONST_USE_AMD_GCN_READ_FIRST_LANE 0
Chao Liu's avatar
Chao Liu committed
234

235
// workaround: compiler crash when compiling recursive lambda
Chao Liu's avatar
Chao Liu committed
236
#define CK_WORKAROUND_SWDEV_275126 1
237

238
// workaround: compiler crash when using buffer load/store for i8
239
240
#define CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE 1

241
// workaround: compiler gnerating inefficient ds_write instructions
242
#define CK_WORKAROUND_SWDEV_XXXXXX_INT8_DS_WRITE_ISSUE 1
Chao Liu's avatar
Chao Liu committed
243

244
// workaround: verifaction failure, due to compiler regression, for conv bwd-data fp16 using some
245
// tuning parameter
246
#define CK_WORKAROUND_SWDEV_325164 0
247

248
249
250
// workaround: compiler not emiting reciprocal instruction frm __frcp_rn()
#define CK_WORKAROUND_SWDEV_383542 1

ltqin's avatar
ltqin committed
251
252
// workaround: compiler issue on gfx908
#define CK_WORKAROUND_SWDEV_388832 1
253

254
255
256
257
258
259
260
261
262
// denorm test fix, necessary for gfx90a
#ifndef CK_GFX90A_DENORM_WORKAROUND
#define CK_GFX90A_DENORM_WORKAROUND 0
#endif // CK_GFX90A_DENORM_WORKAROUND
// Enable only for gfx90a
#if defined(__gfx90a__)
#if CK_GFX90A_DENORM_WORKAROUND
#define CK_GFX90A_DENORM_WORKAROUND 1
#endif // CK_GFX90A_DENORM_WORKAROUND is set to 1
Illia Silin's avatar
Illia Silin committed
263
#else
264
265
#define CK_GFX90A_DENORM_WORKAROUND 0
#endif // gfx90a
266

267
268
269
// set flag to 1 to build deprecated instances
#define CK_BUILD_DEPRECATED 1

270
271
namespace ck {

272
enum struct InMemoryDataOperationEnum
Chao Liu's avatar
Chao Liu committed
273
{
Chao Liu's avatar
Chao Liu committed
274
    Set,
275
    AtomicAdd,
rocking5566's avatar
rocking5566 committed
276
    AtomicMax,
277
278
279
    Add
};

Chao Liu's avatar
Chao Liu committed
280
// FIXME: use regular Sequence and remove this
rocking5566's avatar
rocking5566 committed
281
282
283
284
285
286
287
288
289
290
291
292
293
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
294
// index type
Jianfeng Yan's avatar
Jianfeng Yan committed
295
296
using index_t      = int32_t;
using long_index_t = int64_t;
297
298

} // namespace ck