forked from yassa9/qwen600
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_loader.hip.h
More file actions
339 lines (289 loc) · 11.3 KB
/
static_loader.hip.h
File metadata and controls
339 lines (289 loc) · 11.3 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// static_loader.hip.h - AMD ROCm/HIP version
#pragma once
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
#include <numeric>
#include <hip/hip_runtime.h>
#include <hip/hip_bfloat16.h>
// memory mapping on Linux/macOS
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "config.h"
#define HIP_CHECK(call) \
do { \
hipError_t err = call; \
if (err != hipSuccess) { \
fprintf(stderr, "HIP error at %s %d: %s\n", __FILE__, __LINE__, \
hipGetErrorString(err)); \
exit(EXIT_FAILURE); \
} \
} while (0)
namespace qwen_loader
{
using bf16 = hip_bfloat16;
struct
AttentionWeights
{
bf16* q_proj_weight;
bf16* k_proj_weight;
bf16* v_proj_weight;
bf16* o_proj_weight;
bf16* q_norm_weight;
bf16* k_norm_weight;
};
struct
FFNWeights
{
bf16* gate_proj_weight;
bf16* up_proj_weight;
bf16* down_proj_weight;
};
struct
TransformerBlockWeights
{
AttentionWeights attention;
FFNWeights ffn;
bf16* input_layernorm_weight;
bf16* post_attention_layernorm_weight;
};
struct
QwenWeights
{
bf16* token_embedding_table;
bf16* output_head_weight;
bf16* final_norm_weight;
std::vector<TransformerBlockWeights> layers;
};
// Copy bf16 bytes from mapped host memory to device memory
inline void copy_bf16_bytes_to_device(const void* host_src_bf16,
bf16* device_dst_bf16,
size_t count)
{
HIP_CHECK(hipMemcpy(device_dst_bf16,
host_src_bf16,
count * sizeof(bf16),
hipMemcpyHostToDevice));
}
// Memory mapping utilities
class MemoryMappedFile
{
private:
int fd;
void* mapped_data;
size_t file_size;
public:
MemoryMappedFile(const std::string& filename)
: fd(-1), mapped_data(nullptr), file_size(0)
{
fd = open(filename.c_str(), O_RDONLY);
if (fd == -1)
{
throw std::runtime_error("Failed to open file: " + filename);
}
struct stat st;
if (fstat(fd, &st) == -1)
{
close(fd);
throw std::runtime_error("Failed to get file size: " + filename);
}
file_size = st.st_size;
mapped_data = mmap(nullptr, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapped_data == MAP_FAILED)
{
close(fd);
throw std::runtime_error("Failed to map file: " + filename);
}
}
~MemoryMappedFile()
{
if (mapped_data != nullptr)
{
munmap(mapped_data, file_size);
}
if (fd != -1)
{
close(fd);
}
}
const void* data() const { return mapped_data; }
size_t size() const { return file_size; }
};
// SafeTensor parsing utilities
class SafeTensorParser
{
private:
const uint8_t* data;
size_t data_size;
size_t offset;
public:
SafeTensorParser(const void* file_data, size_t file_size)
: data(static_cast<const uint8_t*>(file_data)), data_size(file_size), offset(0) {}
template<typename T>
T read()
{
if (offset + sizeof(T) > data_size)
{
throw std::runtime_error("Read beyond file bounds");
}
T value = *reinterpret_cast<const T*>(data + offset);
offset += sizeof(T);
return value;
}
void skip(size_t bytes)
{
if (offset + bytes > data_size)
{
throw std::runtime_error("Skip beyond file bounds");
}
offset += bytes;
}
size_t tell() const { return offset; }
void seek(size_t pos) { offset = pos; }
const void* raw_data() const { return data; }
};
// Load weights
// Prefer ordered blob (weights_qwen600.bin) if present; otherwise fallback to safetensors
void load_qwen_weights(const std::string& checkpoint_path, QwenWeights& weights)
{
try
{
// Try ordered blob alongside the safetensors file
std::string dir = ".";
{
size_t p = checkpoint_path.find_last_of("/\\");
if (p != std::string::npos) dir = checkpoint_path.substr(0, p);
}
std::string ordered_blob = dir + "/weights_qwen600.bin";
bool use_blob = false;
size_t cursor = 0;
// Map whichever source we will read from, with robust checks
MemoryMappedFile st_file(checkpoint_path);
MemoryMappedFile* blob_file_ptr = nullptr;
struct stat st_blob;
if (stat(ordered_blob.c_str(), &st_blob) == 0 && S_ISREG(st_blob.st_mode) && st_blob.st_size > 0)
{
try {
blob_file_ptr = new MemoryMappedFile(ordered_blob);
use_blob = blob_file_ptr->size() > 0;
} catch (const std::exception& e) {
fprintf(stderr, "Warning: Failed to open ordered blob '%s': %s\n", ordered_blob.c_str(), e.what());
use_blob = false;
}
}
SafeTensorParser parser(st_file.data(), st_file.size());
if (!use_blob)
{
// Parse SafeTensor header and skip to first tensor
uint64_t header_size = parser.read<uint64_t>();
(void)header_size;
parser.seek(8 + header_size);
}
// Allocate memory for weights
weights.token_embedding_table = nullptr;
weights.output_head_weight = nullptr;
weights.final_norm_weight = nullptr;
weights.layers.resize(N_LAYERS);
auto copy_next = [&](bf16* dst, size_t count){
if (use_blob)
{
const uint8_t* base = static_cast<const uint8_t*>(blob_file_ptr->data());
const void* src = base + cursor;
copy_bf16_bytes_to_device(src, dst, count);
cursor += count * sizeof(bf16);
}
else
{
const void* src = static_cast<const uint8_t*>(parser.raw_data()) + parser.tell();
copy_bf16_bytes_to_device(src, dst, count);
parser.skip(count * sizeof(bf16));
}
};
// Load token embedding table
size_t token_embedding_size = VOCAB_SIZE * DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&weights.token_embedding_table, token_embedding_size));
copy_next(weights.token_embedding_table, VOCAB_SIZE * DIM);
// Load output head weight
size_t output_head_size = VOCAB_SIZE * DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&weights.output_head_weight, output_head_size));
copy_next(weights.output_head_weight, VOCAB_SIZE * DIM);
// Load final norm weight
size_t final_norm_size = DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&weights.final_norm_weight, final_norm_size));
copy_next(weights.final_norm_weight, DIM);
// Load layer weights
for (int l = 0; l < N_LAYERS; l++)
{
auto& layer = weights.layers[l];
// Attention weights
size_t q_proj_size = Q_DIM * DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.attention.q_proj_weight, q_proj_size));
copy_next(layer.attention.q_proj_weight, Q_DIM * DIM);
size_t k_proj_size = KV_DIM * DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.attention.k_proj_weight, k_proj_size));
copy_next(layer.attention.k_proj_weight, KV_DIM * DIM);
size_t v_proj_size = KV_DIM * DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.attention.v_proj_weight, v_proj_size));
copy_next(layer.attention.v_proj_weight, KV_DIM * DIM);
size_t o_proj_size = DIM * Q_DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.attention.o_proj_weight, o_proj_size));
copy_next(layer.attention.o_proj_weight, DIM * Q_DIM);
// QK norm weights
size_t q_norm_size = HEAD_DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.attention.q_norm_weight, q_norm_size));
copy_next(layer.attention.q_norm_weight, HEAD_DIM);
size_t k_norm_size = HEAD_DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.attention.k_norm_weight, k_norm_size));
copy_next(layer.attention.k_norm_weight, HEAD_DIM);
// FFN weights
size_t gate_proj_size = HIDDEN_DIM * DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.ffn.gate_proj_weight, gate_proj_size));
copy_next(layer.ffn.gate_proj_weight, HIDDEN_DIM * DIM);
size_t up_proj_size = HIDDEN_DIM * DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.ffn.up_proj_weight, up_proj_size));
copy_next(layer.ffn.up_proj_weight, HIDDEN_DIM * DIM);
size_t down_proj_size = DIM * HIDDEN_DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.ffn.down_proj_weight, down_proj_size));
copy_next(layer.ffn.down_proj_weight, DIM * HIDDEN_DIM);
// Layer norm weights
size_t input_layernorm_size = DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.input_layernorm_weight, input_layernorm_size));
copy_next(layer.input_layernorm_weight, DIM);
size_t post_attention_layernorm_size = DIM * sizeof(bf16);
HIP_CHECK(hipMalloc(&layer.post_attention_layernorm_weight, post_attention_layernorm_size));
copy_next(layer.post_attention_layernorm_weight, DIM);
}
std::cout << "Successfully loaded Qwen weights from: " << (use_blob ? ordered_blob : checkpoint_path) << std::endl;
if (blob_file_ptr) { delete blob_file_ptr; blob_file_ptr = nullptr; }
}
catch (const std::exception& e)
{
std::cerr << "Error loading weights: " << e.what() << std::endl;
throw;
}
}
// Free weights memory
void free_qwen_weights(QwenWeights& weights)
{
if (weights.token_embedding_table) hipFree(weights.token_embedding_table);
if (weights.output_head_weight) hipFree(weights.output_head_weight);
if (weights.final_norm_weight) hipFree(weights.final_norm_weight);
for (auto& layer : weights.layers)
{
if (layer.attention.q_proj_weight) hipFree(layer.attention.q_proj_weight);
if (layer.attention.k_proj_weight) hipFree(layer.attention.k_proj_weight);
if (layer.attention.v_proj_weight) hipFree(layer.attention.v_proj_weight);
if (layer.attention.o_proj_weight) hipFree(layer.attention.o_proj_weight);
if (layer.attention.q_norm_weight) hipFree(layer.attention.q_norm_weight);
if (layer.attention.k_norm_weight) hipFree(layer.attention.k_norm_weight);
if (layer.ffn.gate_proj_weight) hipFree(layer.ffn.gate_proj_weight);
if (layer.ffn.up_proj_weight) hipFree(layer.ffn.up_proj_weight);
if (layer.ffn.down_proj_weight) hipFree(layer.ffn.down_proj_weight);
if (layer.input_layernorm_weight) hipFree(layer.input_layernorm_weight);
if (layer.post_attention_layernorm_weight) hipFree(layer.post_attention_layernorm_weight);
}
}
} // namespace qwen_loader