forked from NVIDIA/recsys-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
255 lines (215 loc) · 8.8 KB
/
utils.h
File metadata and controls
255 lines (215 loc) · 8.8 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/******************************************************************************
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
******************************************************************************/
#ifndef UTILS_H
#define UTILS_H
#include <cstdint>
#include <cuda_bf16.h>
#include <cuda_fp16.h>
#include <cuda_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdint>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cuda_bf16.h>
#include "check.h"
namespace dyn_emb {
enum class DataType : uint32_t {
Float32 = 0,
Float16,
BFloat16,
Int64,
UInt64,
Int32,
UInt32,
Size_t,
};
// The EvictStrategy is consistent with HKV's EvictStrategy. If modifications are needed, please refer to the HKV documentation.
// TODO: need be changed to the form static_cast<uint32_t>(nv::merlin::EvictStrategy::EvictStrategyEnum::kLru)
enum class EvictStrategy : uint32_t {
kLru = 0,
kLfu = 1, // dynamicemb don't use
kEpochLru = 2, // dynamicemb don't use
kEpochLfu = 3, // dynamicemb don't use
kCustomized = 4,
};
#define CASE_TYPE_USING_HINT(enum_type, type, HINT, ...) \
case (enum_type): { \
using HINT = type; \
__VA_ARGS__(); \
break; \
}
#define CASE_ENUM_USING_HINT(enum_type, HINT, ...) \
case (enum_type): { \
constexpr auto HINT = enum_type; \
__VA_ARGS__(); \
break; \
}
#define DISPATCH_INTEGER_DATATYPE_FUNCTION(DATA_TYPE, HINT, ...) \
switch (DATA_TYPE) { \
CASE_TYPE_USING_HINT(DataType::Int64, int64_t, HINT, __VA_ARGS__) \
CASE_TYPE_USING_HINT(DataType::UInt64, uint64_t, HINT, __VA_ARGS__) \
default: \
exit(EXIT_FAILURE); \
}
#define DISPATCH_OFFSET_INT_TYPE(DATA_TYPE, HINT, ...) \
switch (DATA_TYPE) { \
CASE_TYPE_USING_HINT(DataType::Int64, int64_t, HINT, __VA_ARGS__) \
CASE_TYPE_USING_HINT(DataType::UInt64, uint64_t, HINT, __VA_ARGS__) \
CASE_TYPE_USING_HINT(DataType::Int32, int, HINT, __VA_ARGS__) \
CASE_TYPE_USING_HINT(DataType::UInt32, uint32_t, HINT, __VA_ARGS__) \
default: \
exit(EXIT_FAILURE); \
}
#define DISPATCH_FLOAT_DATATYPE_FUNCTION(DATA_TYPE, HINT, ...) \
switch (DATA_TYPE) { \
CASE_TYPE_USING_HINT(DataType::Float32, float, HINT, __VA_ARGS__) \
CASE_TYPE_USING_HINT(DataType::Float16, __half, HINT, __VA_ARGS__) \
CASE_TYPE_USING_HINT(DataType::BFloat16, __nv_bfloat16, HINT, __VA_ARGS__) \
default: \
exit(EXIT_FAILURE); \
}
#define DISPATCH_FLOAT_ACCUM_TYPE_FUNC(ACCUM_TYPE, HINT, ...) \
switch (ACCUM_TYPE) { \
CASE_TYPE_USING_HINT(DataType::Float32, float, HINT, __VA_ARGS__) \
default: \
exit(EXIT_FAILURE); \
}
#define DISPATCH_EVICTYPE_FUNCTION(EVICT_TYPE, HINT, ...) \
switch (EVICT_TYPE) { \
CASE_ENUM_USING_HINT(EvictStrategy::kLru, HINT, __VA_ARGS__) \
CASE_ENUM_USING_HINT(EvictStrategy::kCustomized, HINT, __VA_ARGS__) \
CASE_ENUM_USING_HINT(EvictStrategy::kLfu, HINT, __VA_ARGS__) \
default: \
exit(EXIT_FAILURE); \
}
#define HOST_INLINE __host__ __forceinline__
#define DEVICE_INLINE __device__ __forceinline__
#define HOST_DEVICE_INLINE __host__ __device__ __forceinline__
#define CUDA_1D_KERNEL_LOOP(i, n) \
for (int32_t i = blockIdx.x * blockDim.x + threadIdx.x, \
step = blockDim.x * gridDim.x; \
i < (n); i += step)
class DeviceProp {
public:
static DeviceProp &getDeviceProp(int device_id = 0);
// DeviceProp(const DeviceProp&) = delete; //TODO: whether to remove
DeviceProp &operator=(const DeviceProp &) = delete;
int num_sms;
int warp_size;
int max_thread_per_sm;
int max_thread_per_block;
int total_threads;
private:
explicit DeviceProp(int device_id);
~DeviceProp() = default;
};
template <typename TOUT, typename TIN> struct TypeConvertFunc;
template <> struct TypeConvertFunc<__half, float> {
static __forceinline__ __device__ __half convert(float val) {
return __float2half(val);
}
};
template <> struct TypeConvertFunc<float, __half> {
static __forceinline__ __device__ float convert(__half val) {
return __half2float(val);
}
};
template <> struct TypeConvertFunc<nv_bfloat16, float> {
static __forceinline__ __device__ nv_bfloat16 convert(float val) {
return __float2bfloat16(val);
}
};
template <> struct TypeConvertFunc<float, nv_bfloat16> {
static __forceinline__ __device__ float convert(nv_bfloat16 val) {
return __bfloat162float(val);
}
};
template <> struct TypeConvertFunc<nv_bfloat16, __half> {
static __forceinline__ __device__ nv_bfloat16 convert(__half val) {
float temp = __half2float(val);
return __float2bfloat16(temp);
}
};
template <> struct TypeConvertFunc<__half, nv_bfloat16> {
static __forceinline__ __device__ __half convert(nv_bfloat16 val) {
float temp = __bfloat162float(val);
return __float2half(temp);
}
};
template <> struct TypeConvertFunc<float, float> {
static __forceinline__ __device__ float convert(float val) { return val; }
};
template <> struct TypeConvertFunc<__half, __half> {
static __forceinline__ __device__ __half convert(__half val) { return val; }
};
template <> struct TypeConvertFunc<nv_bfloat16, nv_bfloat16> {
static __forceinline__ __device__ nv_bfloat16 convert(nv_bfloat16 val) {
return val;
}
};
template <> struct TypeConvertFunc<float, long long> {
static __forceinline__ __device__ float convert(long long val) {
return static_cast<float>(val);
}
};
template <> struct TypeConvertFunc<float, unsigned int> {
static __forceinline__ __device__ float convert(unsigned int val) {
return static_cast<float>(val);
}
};
template <> struct TypeConvertFunc<int, long long> {
static __forceinline__ __device__ int convert(long long val) {
return static_cast<int>(val);
}
};
template <> struct TypeConvertFunc<int, unsigned int> {
static __forceinline__ __device__ int convert(unsigned int val) {
return static_cast<int>(val);
}
};
class DeviceCounter {
public:
DeviceCounter() {
CUDACHECK(cudaMalloc((void**)&d_counter, sizeof(uint64_t)));
}
~DeviceCounter() {
CUDACHECK(cudaFree(d_counter));
}
DeviceCounter& reset(const cudaStream_t& stream) {
CUDACHECK(cudaMemsetAsync(d_counter, 0, sizeof(uint64_t), stream));
return *this;
}
uint64_t* get() {
return d_counter;
}
DeviceCounter& sync(const cudaStream_t& stream) {
CUDACHECK(cudaMemcpyAsync(&h_counter, d_counter, sizeof(uint64_t),
cudaMemcpyDeviceToHost, stream));
CUDACHECK(cudaStreamSynchronize(stream));
CUDACHECK(cudaGetLastError());
return *this;
}
uint64_t result() {
return h_counter;
}
private:
uint64_t* d_counter {nullptr};
uint64_t h_counter {0};
};
} // namespace dyn_emb
#endif // UTILS_H