make init code much less sketchy
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
Inference for Llama-2 Transformer model in pure C.
|
Inference for Llama-2 Transformer model in pure C.
|
||||||
|
|
||||||
Compile simply with:
|
Example compile: (see README for more details)
|
||||||
$ gcc -o run run.c
|
$ gcc -O3 -o run run.c -lm
|
||||||
Or if that doesn't work then:
|
|
||||||
$ gcc -o run run.c -lm
|
|
||||||
|
|
||||||
Then run with:
|
Then run with:
|
||||||
$ ./run
|
$ ./run
|
||||||
@@ -402,16 +400,24 @@ int main(int argc, char *argv[]) {
|
|||||||
srand((unsigned int)current_time);
|
srand((unsigned int)current_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
// read in the config header
|
// read in the checkpoint .bin file
|
||||||
Config config;
|
Config config;
|
||||||
FILE *file = fopen(checkpoint, "rb");
|
TransformerWeights weights;
|
||||||
if (!file) {
|
{
|
||||||
printf("Unable to open file!");
|
FILE *file = fopen(checkpoint, "rb");
|
||||||
return 1;
|
if (!file) {
|
||||||
|
printf("Unable to open file!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// read in the config header
|
||||||
|
fread(&config, sizeof(Config), 1, file);
|
||||||
|
// read in the Transformer weights
|
||||||
|
malloc_weights(&weights, &config);
|
||||||
|
checkpoint_init_weights(&weights, &config, file);
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
fread(&config, sizeof(Config), 1, file);
|
|
||||||
|
|
||||||
// init the Tokenizer
|
// read in the tokenizer vocab
|
||||||
char** vocab = (char**)malloc(config.vocab_size * sizeof(char*));
|
char** vocab = (char**)malloc(config.vocab_size * sizeof(char*));
|
||||||
{
|
{
|
||||||
FILE *file = fopen("tokenizer.bin", "r");
|
FILE *file = fopen("tokenizer.bin", "r");
|
||||||
@@ -427,14 +433,9 @@ int main(int argc, char *argv[]) {
|
|||||||
fread(vocab[i], len, 1, file);
|
fread(vocab[i], len, 1, file);
|
||||||
vocab[i][len] = '\0'; // add the string terminating token
|
vocab[i][len] = '\0'; // add the string terminating token
|
||||||
}
|
}
|
||||||
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create and init the Transformer
|
|
||||||
TransformerWeights weights;
|
|
||||||
malloc_weights(&weights, &config);
|
|
||||||
checkpoint_init_weights(&weights, &config, file);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
// create and init the application RunState
|
// create and init the application RunState
|
||||||
RunState state;
|
RunState state;
|
||||||
malloc_run_state(&state, &config);
|
malloc_run_state(&state, &config);
|
||||||
@@ -469,7 +470,7 @@ int main(int argc, char *argv[]) {
|
|||||||
pos++;
|
pos++;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// report our achieved tok/s
|
// report our achieved tok/s
|
||||||
clock_t end = clock();
|
clock_t end = clock();
|
||||||
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
|
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
|
||||||
|
|||||||
Reference in New Issue
Block a user