make init code much less sketchy

This commit is contained in:
Andrej Karpathy
2023-07-24 04:22:32 +00:00
parent bd9e837b14
commit 6a61831e19
+18 -17
View File
@@ -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);