-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_13B.cc
377 lines (305 loc) · 14.8 KB
/
run_13B.cc
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* Inference for Llama-2 Transformer model in pure C */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fstream>
#include <iostream>
#include <hip/hip_runtime.h>
#include <omp.h>
#include <pthread.h>
#include "build_pipeline.h"
#include "kernels_pipeline.h"
// Macros for error checking
#define CHECK_HIP(cmd) \
do { \
hipError_t error = (cmd); \
if (error != hipSuccess) \
{ \
std::cerr << "HIP error (" << hipGetErrorString(error) << ") at line " \
<< __LINE__ << " in file " << __FILE__ << "\n"; \
exit(-1); \
} \
} while (0)
// ----------------------------------------------------------------------------
// Forward inference
float* forward(Transformer* transformer, int token, int pos) {
// a few convenience variables
Config* p = &transformer->config;
int dim = p->dim;
int kv_dim = (p->dim * p->n_kv_heads) / p->n_heads;
int kv_mul = p->n_heads / p->n_kv_heads; // integer multiplier of the kv sharing in multiquery
int hidden_dim = p->hidden_dim;
int head_size = dim / p->n_heads;
TransformerWeights* w[NUM_GPU];
RunState* s[NUM_GPU];
for (int device_id = 0; device_id < NUM_GPU; device_id++) {
CHECK_HIP(hipSetDevice(device_id));
w[device_id] = &transformer->weights_gpu[device_id];
s[device_id] = &transformer->state[device_id];
}
CHECK_HIP(hipSetDevice(0));
float *x[NUM_GPU];
x[0] = s[0]->x;
// copy the token embedding into x
float* content_row = w[0]->token_embedding_table + token * dim;
CHECK_HIP(hipMemcpyAsync(x[0], content_row, dim*sizeof(*x[0]), hipMemcpyDeviceToDevice, streams[0]));
CHECK_HIP(hipStreamSynchronize(streams[0]));
for (int device_id = 0; device_id < NUM_GPU; device_id++) {
CHECK_HIP(hipSetDevice(device_id));
if (device_id > 0) {
CHECK_HIP(hipStreamWaitEvent(streams[device_id], events[device_id - 1], 0));
}
x[device_id] = s[device_id]->x;
// forward all the layers
int num_layers = layer_end[device_id] - layer_begin[device_id];
for(int l = 0; l < num_layers; l++) {
// attention rmsnorm
gpu_rmsnorm(s[device_id]->xb, x[device_id], w[device_id]->rms_att_weight + l*dim, dim, streams[device_id]);
// save key,value at this time step (pos) to our kv cache
int loff = (layer_begin[device_id] + l) * p->seq_len * kv_dim;
s[device_id]->k = s[device_id]->key_cache + loff + pos * kv_dim;
s[device_id]->v = s[device_id]->value_cache + loff + pos * kv_dim;
// qkv matmuls for this position
gpu_matmul(s[device_id]->q, s[device_id]->xb, w[device_id]->wq + l*dim*dim, dim, dim, streams[device_id]);
gpu_matmul(s[device_id]->k, s[device_id]->xb, w[device_id]->wk + l*dim*kv_dim, dim, kv_dim, streams[device_id]);
gpu_matmul(s[device_id]->v, s[device_id]->xb, w[device_id]->wv + l*dim*kv_dim, dim, kv_dim, streams[device_id]);
gpu_RoPE(s[device_id]->q, s[device_id]->k, pos, dim, head_size, kv_dim, streams[device_id]);
gpu_MultiHeadAttention(s[device_id]->xb, s[device_id]->q,
s[device_id]->key_cache, s[device_id]->value_cache,
kv_dim, kv_mul, p->n_heads, head_size, loff, pos+1, streams[device_id]);
// final matmul to get the output of the attention
gpu_matmul(s[device_id]->xb2, s[device_id]->xb, w[device_id]->wo + l*dim*dim, dim, dim, streams[device_id]);
// residual connection back into x
gpu_accum(x[device_id], s[device_id]->xb2, dim, streams[device_id]);
// ffn rmsnorm
gpu_rmsnorm(s[device_id]->xb, x[device_id], w[device_id]->rms_ffn_weight + l*dim, dim, streams[device_id]);
// Now for FFN in PyTorch we have: self.w2(F.silu(self.w1(x)) * self.w3(x))
// first calculate self.w1(x) and self.w3(x)
gpu_matmul(s[device_id]->hb, s[device_id]->xb, w[device_id]->w1 + l*dim*hidden_dim, dim, hidden_dim, streams[device_id]);
gpu_matmul(s[device_id]->hb2, s[device_id]->xb, w[device_id]->w3 + l*dim*hidden_dim, dim, hidden_dim, streams[device_id]);
// SwiGLU non-linearity
gpu_swiglu(s[device_id]->hb, s[device_id]->hb2, hidden_dim, streams[device_id]);
// final matmul to get the output of the ffn
gpu_matmul(s[device_id]->xb, s[device_id]->hb, w[device_id]->w2 + l*dim*hidden_dim, hidden_dim, dim, streams[device_id]);
// residual connection
gpu_accum(x[device_id], s[device_id]->xb, dim, streams[device_id]);
}
if (device_id < NUM_GPU - 1) {
CHECK_HIP(hipMemcpyAsync(s[device_id + 1]->x, s[device_id]->x,
dim * sizeof(float), hipMemcpyDeviceToDevice, streams[device_id]));
CHECK_HIP(hipEventRecord(events[device_id], streams[device_id]));
}
else {
// final rmsnorm
gpu_rmsnorm(x[device_id], x[device_id], w[device_id]->rms_final_weight, dim, streams[device_id]);
// classifier into logits
gpu_matmul(s[device_id]->logits_gpu, x[device_id], w[device_id]->wcls, p->dim, p->vocab_size, streams[device_id]);
CHECK_HIP(hipMemcpyAsync(s[device_id]->logits, s[device_id]->logits_gpu, p->vocab_size * sizeof(float), hipMemcpyDeviceToHost, streams[device_id]));
CHECK_HIP(hipStreamSynchronize(streams[device_id]));
return s[device_id]->logits;
}
}
return NULL;
}
// ----------------------------------------------------------------------------
// You should parallelize and optimize from this function exploiting multiple GPUs
//
int test(Transformer *transformer, Tokenizer *tokenizer, Requests * requests, int batch=1) {
// Count the number of the generated tokens
int gen_cnt = 0;
Sampler samplers[requests->num_reqs];
for(int idx = 0; idx < requests->num_reqs; idx++) {
build_sampler(&samplers[idx], transformer->config.vocab_size, 1.0f, 0.9f, 314028);
}
for(int idx = 0; idx < 1; idx++) {
// for(int idx = 0; idx < requests->num_reqs; idx++) {
// Loop for the multiple requests
std::string gen_str = "";
char* prompt = get_str_req_ptr(requests, idx);
int* prompt_tokens = (int*)malloc((strlen(prompt)+3) * sizeof(int)); // +3 for '\0', ?BOS, ?EOS
// encode the (string) prompt into tokens sequence
int num_prompt_tokens = 0;
encode(tokenizer, prompt, 1, 0, prompt_tokens, &num_prompt_tokens);
if (num_prompt_tokens < 1) {
fprintf(stderr, "something is wrong, expected at least 1 prompt token\n");
exit(EXIT_FAILURE);
}
// start the main loop
long start = 0; // used to time our code, only initialized after first iteration
int next; // will store the next token in the sequence
int token = prompt_tokens[0]; // kick off with the first token in the prompt
int pos = 0; // position in the sequence
int steps = requests->max_seq_len; // max sequence length
while (pos < steps) {
// -------------------------------------------------------------
// Divide device and run in GPUs here
float* logits = forward(transformer, token, pos);
// float* logits = 0;
// End of GPUs run
// -------------------------------------------------------------
// advance the state machine
if (pos < num_prompt_tokens - 1) {
// if we are still processing the input prompt, force the next prompt token
next = prompt_tokens[pos + 1];
} else {
// otherwise sample the next token from the logits
next = sample(&samplers[idx], logits);
//next = sample_greedy(sampler, logits);
//next = sample_determin(sampler, logits, rng_states, idx);
}
pos++;
// data-dependent terminating condition: the BOS (=1) token delimits sequences
if (next == 1 || next == 2) {
break;
}
// print the token as string, decode it with the Tokenizer object
char* piece = decode(tokenizer, token, next);
// You don't need to print every tokens are generated.
// {
safe_printf(piece); // same as printf("%s", piece), but skips "unsafe" bytes
fflush(stdout);
// }
// gen_str += piece;
append_str(piece, gen_str);
token = next;
// init the timer here because the first iteration can be slower
// this timer is not important
if (start == 0) { start = time_in_ms(); }
}
printf("\n");
gen_str += "\n";
strcpy(get_str_gen_ptr(requests, idx), gen_str.c_str());
free(prompt_tokens);
// report achieved tok/s (pos-1 because the timer starts after first iteration)
if (pos > 1) {
long end = time_in_ms();
fprintf(stderr, "achieved tok/s: %f\n", (pos-1) / (double)(end-start)*1000);
gen_cnt += pos-1;
}
}
for(int idx = 0; idx < requests->num_reqs; idx++) {
free_sampler(&samplers[idx]);
}
return gen_cnt;
}
// ----------------------------------------------------------------------------
// CLI, include only if not testing
// #ifndef TESTING
void error_usage() {
fprintf(stderr, "Usage: run <checkpoint> [options]\n");
fprintf(stderr, "Example: run model.bin -n 256 -i \"Once upon a time\"\n");
fprintf(stderr, "Example: run model.bin -m test -f <input_filename> -o <output_filename>\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -t <float> temperature in [0,inf], default 1.0 (ignore the arg for test mode)\n");
fprintf(stderr, " -p <float> p value in top-p (nucleus) sampling in [0,1] default 0.9 (ignore the arg for test mode)\n");
fprintf(stderr, " -s <int> random seed, default time(NULL) (ignore the arg for test mode)\n");
fprintf(stderr, " -n <int> number of steps to run for, default 256. 0 = max_seq_len (for test mode steps = max_seq_len)\n");
fprintf(stderr, " -i <string> input prompt (ignore the arg for test mode)\n");
fprintf(stderr, " -z <string> optional path to custom tokenizer\n");
fprintf(stderr, " -m <string> mode: generate|chat|test, default: generate\n");
fprintf(stderr, " -y <string> (optional) system prompt in chat mode\n");
fprintf(stderr, " -f <string> (only for test mode) input filename\n");
fprintf(stderr, " -o <string> (only for test mode) output filename\n");
fprintf(stderr, " -b <string> batch size\n");
exit(EXIT_FAILURE);
}
#ifndef KERNEL_TEST
int main(int argc, char *argv[]) {
printf("Enter main\n");
// default parameters
char *checkpoint_path = NULL; // e.g. out/model.bin
char *tokenizer_path = (char*)"tokenizer.bin";
float temperature = 1.0f; // 0.0 = greedy deterministic. 1.0 = original. don't set higher
float topp = 0.9f; // top-p in nucleus sampling. 1.0 = off. 0.9 works well, but slower
int steps = 256; // number of steps to run for
char *prompt = NULL; // prompt string
unsigned long long rng_seed = 0; // seed rng with time by default
char *mode = (char*)"generate"; // generate|chat|test
char *system_prompt = NULL; // the (optional) system prompt to use in chat mode
char *input_filename = NULL; // Input Filename
char *output_filename = NULL; // Output Filename
int batch = 1;
// poor man's C argparse so we can override the defaults above from the command line
if (argc >= 2) { checkpoint_path = argv[1]; } else { error_usage(); }
for (int i = 2; i < argc; i+=2) {
// do some basic validation
if (i + 1 >= argc) { error_usage(); } // must have arg after flag
if (argv[i][0] != '-') { error_usage(); } // must start with dash
if (strlen(argv[i]) != 2) { error_usage(); } // must be -x (one dash, one letter)
// read in the args
if (argv[i][1] == 't') { temperature = atof(argv[i + 1]); }
else if (argv[i][1] == 'p') { topp = atof(argv[i + 1]); }
else if (argv[i][1] == 's') { rng_seed = atoi(argv[i + 1]); }
else if (argv[i][1] == 'n') { steps = atoi(argv[i + 1]); }
else if (argv[i][1] == 'i') { prompt = argv[i + 1]; }
else if (argv[i][1] == 'z') { tokenizer_path = argv[i + 1]; }
else if (argv[i][1] == 'm') { mode = argv[i + 1]; }
else if (argv[i][1] == 'y') { system_prompt = argv[i + 1]; }
else if (argv[i][1] == 'f') { input_filename = argv[i + 1]; }
else if (argv[i][1] == 'o') { output_filename = argv[i + 1]; }
else if (argv[i][1] == 'b') { batch = atoi(argv[i + 1]); }
else { error_usage(); }
}
// parameter validation/overrides
if (rng_seed <= 0) rng_seed = (unsigned int)time(NULL);
if (temperature < 0.0) temperature = 0.0;
if (topp < 0.0 || 1.0 < topp) topp = 0.9;
if (steps < 0) steps = 0;
// build the Transformer via the model .bin file
CHECK_HIP(hipGetDeviceCount(&NUM_GPU));
printf("Number of Devices: %d\n", NUM_GPU);
Transformer transformer;
build_transformer(&transformer, checkpoint_path);
if (steps == 0 || steps > transformer.config.seq_len) steps = transformer.config.seq_len; // ovrerride to ~max length
// build the Tokenizer via the tokenizer .bin file
Tokenizer tokenizer;
build_tokenizer(&tokenizer, tokenizer_path, transformer.config.vocab_size);
Requests requests;
// run!
if (strcmp(mode, "generate") == 0) {
// generate(&transformer, &tokenizer, &sampler, prompt, steps);
}
else if (strcmp(mode, "chat") == 0) {
//chat(&transformer, &tokenizer, &sampler, prompt, system_prompt, steps);
}
else if (strcmp(mode, "test") == 0) {
steps = transformer.config.seq_len;
if(input_filename == NULL || output_filename == NULL) {
error_usage();
}
if(EXIT_FAILURE == read_inputfile(input_filename, tokenizer.max_token_length, steps, &requests)) {
fprintf(stderr, "cannot read input file: %s\n", input_filename);
exit(EXIT_FAILURE);
}
// Don't modify this parts for evaluation
// {
long start, end;
start = time_in_ms();
int num_gen_tokens = test(&transformer, &tokenizer, &requests, batch);
end = time_in_ms();
// Your goal is to achieve best throughput(=reduce elapsed time)!
fprintf(stdout, "elapsed time(s): %f, achieved throughput(tok/s): %f\n", (double)(end-start)/1000, (num_gen_tokens) / (double)(end-start)*1000);
//}
if(EXIT_FAILURE == write_outputfile(output_filename, &requests)) {
fprintf(stderr, "cannot write output file: %s\n", input_filename);
exit(EXIT_FAILURE);
}
free_requests(&requests);
} else {
fprintf(stderr, "unknown mode: %s\n", mode);
error_usage();
}
destroyStreams();
// // memory and file handles cleanup
// free_sampler(&sampler);
// free_tokenizer(&tokenizer);
// free_transformer(&transformer);
return 0;
}
#endif