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

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

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

Chao Liu's avatar
Chao Liu committed
11
12
#define CK_TIME_KERNEL 1

13
14
#define ENABLE_DUMP_CLOCK 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

Chao Liu's avatar
Chao Liu committed
32
// buffer resource
33
34
35
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_BUFFER_RESOURCE_3RD_DWORD -1
#elif defined(__gfx803__) || defined(__gfx900__) || defined(__gfx906__) || defined(__gfx908__) || \
36
    defined(__gfx90a__) || defined(__gfx940__) // for GPU code
Chao Liu's avatar
Chao Liu committed
37
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x00020000
38
#elif defined(__gfx1030__) // for GPU code
Chao Liu's avatar
Chao Liu committed
39
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x31014000
40
#elif defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) // for GPU code
41
#define CK_BUFFER_RESOURCE_3RD_DWORD 0x31004000
Chao Liu's avatar
Chao Liu committed
42
43
#endif

44
// FMA instruction
45
46
#ifndef __HIP_DEVICE_COMPILE__                   // for host code, define nothing
#elif defined(__gfx803__) || defined(__gfx900__) // for GPU code
47
#define CK_USE_AMD_V_MAC_F32
48
49
#elif defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__) || \
    defined(__gfx940__) // for GPU code
50
51
52
53
54
#define CK_USE_AMD_V_FMAC_F32
#define CK_USE_AMD_V_DOT2_F32_F16
#define CK_USE_AMD_V_DOT4_I32_I8
#endif

55
56
57
// MFMA instruction
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_MFMA
58
#elif defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx940__) // for GPU code
59
#define CK_USE_AMD_MFMA
60
61
#endif

62
#if(defined(__gfx90a__) || defined(__gfx940__))
63
#define CK_USE_AMD_MFMA_BF16_1K_OP
Chao Liu's avatar
Chao Liu committed
64
65
#endif

66
67
68
69
#if defined(__gfx940__)
#define CK_USE_AMD_MFMA_GFX940
#endif

70
71
72
// WMMA instruction
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_WMMA
73
#elif defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) // for GPU code
74
75
76
#define CK_USE_AMD_WMMA
#endif

77
// buffer load
Jianfeng Yan's avatar
Jianfeng Yan committed
78
#define CK_USE_AMD_BUFFER_LOAD 1
79

80
// buffer store
Jianfeng Yan's avatar
Jianfeng Yan committed
81
82
#define CK_USE_AMD_BUFFER_STORE 1

83
84
// buffer atomic add: integer
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_INTEGER 1
85

86
87
88
// buffer atomic add: floating point
#ifndef __HIP_DEVICE_COMPILE__ // for host code
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 1
89
#elif defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx940__) // for GPU code
90
91
92
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 1
#else // for GPU code
#define CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT 0
93
94
#endif

95
#if(defined(__gfx90a__) || defined(__gfx940__)) // for GPU code
rocking5566's avatar
rocking5566 committed
96
97
98
99
100
#define CK_USE_AMD_BUFFER_ATOMIC_MAX_FLOAT64 1
#else
#define CK_USE_AMD_BUFFER_ATOMIC_MAX_FLOAT64 0
#endif

101
102
103
104
105
106
// inline asm
#define CK_USE_AMD_INLINE_ASM 1

// inner product (DLOP)
#define CK_USE_AMD_INNER_PRODUCT_INLINE_ASM 1

Chao Liu's avatar
Chao Liu committed
107
// block synchronization only s_wait lgkmcnt(0), not vmcnt(0)
108
#define CK_EXPERIMENTAL_BLOCK_SYNC_LDS_WITHOUT_SYNC_VMEM 1
Chao Liu's avatar
Chao Liu committed
109

110
111
// experimental feature: multi index implemented as array
#define CK_EXPERIMENTAL_USE_DYNAMICALLY_INDEXED_MULTI_INDEX 0
Chao Liu's avatar
Chao Liu committed
112

113
114
// experimental feature: static tensor descriptor
#define CK_EXPERIMENTAL_STATIC_TENSOR_DESCRIPTOR 0
Chao Liu's avatar
Chao Liu committed
115

rocking5566's avatar
rocking5566 committed
116
// experimental feature: buffer load/store/atomic-add/ OOB trick
117
// 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
118
119
// setting. Don't use this hack unless absolutely necessary!
// FIXME: make the behavior of buffer load a configurable (template) parameter for each usage
120
#ifndef CK_EXPERIMENTAL_USE_BUFFER_LOAD_OOB_CHECK_OFFSET_TRICK
121
#define CK_EXPERIMENTAL_USE_BUFFER_LOAD_OOB_CHECK_OFFSET_TRICK 0
122
#endif
123
#define CK_EXPERIMENTAL_USE_BUFFER_STORE_OOB_CHECK_OFFSET_TRICK 1
zjing14's avatar
zjing14 committed
124
#define CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_ADD_OOB_CHECK_OFFSET_TRICK 1
rocking5566's avatar
rocking5566 committed
125
#define CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_MAX_OOB_CHECK_OFFSET_TRICK 1
126

127
// experimental feature: in-regsiter sub-dword transpose
128
#define CK_EXPERIMENTAL_USE_IN_REGISTER_SUB_DWORD_TRANSPOSE 1
Chao Liu's avatar
Chao Liu committed
129

130
// experimental feature: merge transformation use magic number division
131
#define CK_EXPERIMENTAL_MERGE_USE_MAGIC_DIVISION 1
132

133
134
// experimental feature: use __builtin_memcpy instead of pointer cast to access a vector from
// pointer of scalar
135
136
#define CK_EXPERIMENTAL_USE_MEMCPY_FOR_VECTOR_ACCESS 0

137
// experimental feature: use __builtin_memcpy instead of union to do bit_cast
138
#define CK_EXPERIMENTAL_USE_MEMCPY_FOR_BIT_CAST 1
139

140
// experimental feature: optimize for inter-wave scheduling policy
141
#define CK_EXPERIMENTAL_INTER_WAVE_SCHEDULING 1
142
#define CK_EXPERIMENTAL_INTER_WAVE_SCHEDULING_MAC_CLUSTERS 1
143
144
145
146
147
148
// 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
149

Chao Liu's avatar
Chao Liu committed
150
151
152
153
// 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
154
#define CK_HACK_MERGE_CALCULATE_IDX_DIFF_LOW_CONST_USE_AMD_GCN_READ_FIRST_LANE 0
Chao Liu's avatar
Chao Liu committed
155

156
// workaround: compiler crash when compiling recursive lambda
Chao Liu's avatar
Chao Liu committed
157
#define CK_WORKAROUND_SWDEV_275126 1
158

159
// workaround: compiler crash when using buffer load/store for i8
160
161
#define CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE 1

162
// workaround: compiler gnerating inefficient ds_write instructions
163
#define CK_WORKAROUND_SWDEV_XXXXXX_INT8_DS_WRITE_ISSUE 1
Chao Liu's avatar
Chao Liu committed
164

165
// workaround: verifaction failure, due to compiler regression, for conv bwd-data fp16 using some
166
// tuning parameter
167
#define CK_WORKAROUND_SWDEV_325164 0
168

169
170
171
// workaround: compiler not emiting reciprocal instruction frm __frcp_rn()
#define CK_WORKAROUND_SWDEV_383542 1

ltqin's avatar
ltqin committed
172
173
// workaround: compiler issue on gfx908
#define CK_WORKAROUND_SWDEV_388832 1
174
175
176
// flag to enable (1) or disable (0) the debugging output in some kernels
#define DEBUG_LOG 0

177
178
179
// denorm test fix, required to work around dissue
#ifndef CK_WORKAROUND_DENORM_FIX
#define CK_WORKAROUND_DENORM_FIX 0
180
181
182
183
#elif
// enable only on MI200
#define CK_WORKAROUND_DENORM_FIX = CK_WORKAROUND_DENORM_FIX && defined(__gfx90a__)
#endif // CK_WORKAROUND_DENORM_FIX
184

185
186
namespace ck {

187
enum struct InMemoryDataOperationEnum
Chao Liu's avatar
Chao Liu committed
188
{
Chao Liu's avatar
Chao Liu committed
189
    Set,
190
    AtomicAdd,
rocking5566's avatar
rocking5566 committed
191
    AtomicMax,
192
193
194
    Add
};

Chao Liu's avatar
Chao Liu committed
195
// FIXME: use regular Sequence and remove this
rocking5566's avatar
rocking5566 committed
196
197
198
199
200
201
202
203
204
205
206
207
208
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
209
// index type
Jianfeng Yan's avatar
Jianfeng Yan committed
210
211
using index_t      = int32_t;
using long_index_t = int64_t;
212
213

} // namespace ck