| 238 | } GPT2; |
| 239 | |
| 240 | void gpt2_build_from_checkpoint(Context& ctx, GPT2 *model, const char* checkpoint_path) { |
| 241 | |
| 242 | // read in model from a checkpoint file |
| 243 | FILE *model_file = fopenCheck(checkpoint_path, "rb"); |
| 244 | int model_header[256]; |
| 245 | freadCheck(model_header, sizeof(int), 256, model_file); |
| 246 | if (model_header[0] != 20240326) { printf("Bad magic model file\n"); exit(1); } |
| 247 | if (model_header[1] != 3) { |
| 248 | printf("Bad version in model file\n"); |
| 249 | printf("---> HINT: try to re-run `python train_gpt2.py`\n"); |
| 250 | exit(1); |
| 251 | } |
| 252 | |
| 253 | // read in hyperparameters |
| 254 | size_t maxT, V, Vp, L, NH, C; // size_t to prevent int overflow |
| 255 | model->config.max_seq_len = maxT = model_header[2]; |
| 256 | model->config.vocab_size = V = model_header[3]; |
| 257 | #ifdef __EMSCRIPTEN__ |
| 258 | model->config.num_layers = L = 12; // TODO(avh): Debugging only hack - revert this |
| 259 | #else |
| 260 | model->config.num_layers = L = model_header[4]; |
| 261 | #endif |
| 262 | model->config.num_heads = NH = model_header[5]; |
| 263 | model->config.channels = C = model_header[6]; |
| 264 | model->config.padded_vocab_size = Vp = model_header[7]; |
| 265 | printf("[GPT-2]\n"); |
| 266 | printf("max_seq_len: %zu\n", maxT); |
| 267 | printf("vocab_size: %zu\n", V); |
| 268 | printf("padded_vocab_size: %zu\n", Vp); |
| 269 | printf("num_layers: %zu\n", L); |
| 270 | printf("num_heads: %zu\n", NH); |
| 271 | printf("channels: %zu\n", C); |
| 272 | |
| 273 | // allocate space for all the parameters and read them in |
| 274 | fill_in_parameter_sizes(model->param_sizes, model->config); |
| 275 | |
| 276 | // count the number of parameters |
| 277 | size_t num_parameters = 0; |
| 278 | for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++) { |
| 279 | num_parameters += model->param_sizes[i]; |
| 280 | } |
| 281 | printf("num_parameters: %zu\n", num_parameters); |
| 282 | model->num_parameters = num_parameters; |
| 283 | |
| 284 | // read in all the parameters from file |
| 285 | model->params_memory = malloc_and_point_parameters(&model->params, model->param_sizes); |
| 286 | freadCheck(model->params_memory, sizeof(float), num_parameters, model_file); |
| 287 | fcloseCheck(model_file); |
| 288 | |
| 289 | // other inits |
| 290 | model->acts_memory = NULL; |
| 291 | model->grads_memory = NULL; |
| 292 | model->m_memory = NULL; |
| 293 | model->v_memory = NULL; |
| 294 | model->grads_acts_memory = NULL; |
| 295 | model->inputs = NULL; |
| 296 | model->targets = NULL; |
| 297 | model->batch_size = 0; |
no test coverage detected