/* Implements: - DataLoader for model training. Reads and serves data shards. - EvalLoader for multiple-choice evaluation datasets, e.g. HellaSwag. */ #ifndef DATALOADER_H #define DATALOADER_H #include #include #include #include #include #include // defines: fopenCheck, freadCheck, fcloseCheck, fseekCheck // defines: mallocCheck #include "utils.h" #include "rand.h" // ---------------------------------------------------------------------------- // implementation of glob for Windows is in dev/unistd.h #ifndef _WIN32 #include #endif // ---------------------------------------------------------------------------- // Distributed Data Loader #define HEADER_SIZE 256 typedef struct { // variables related to distributed training // each process/worker has to access different parts of the data int process_rank; int num_processes; // batch and token information size_t B; size_t T; size_t num_tokens; // total number of tokens size_t shard_num_samples; // total number of samples in the current shard per process // shards and current position glob_t glob_result; // stores the result of glob, for all shards we want to iterate size_t current_shard_idx; // the current shard we are reading from size_t current_sample_idx; // the current sample we are reading from // file handle FILE* tokens_file; // data buffers uint16_t* buffer; // we fread data from file into this buffer int* inputs; // input tokens into transformer int* targets; // target tokens for the transformer // random shuffle related variables mt19937_state shuffle_rng; int should_shuffle; int* shard_indices; int* intra_shard_indices; // sizes in bytes size_t total_batch_size_bytes; // total across all processes size_t local_batch_offset_bytes; // inner-sample offset for this process size_t header_bytes; // header size in bytes int64_t file_size_bytes; } DataLoader; int64_t dataloader_load_shard_(DataLoader *loader, int shard_index) { if (loader->should_shuffle) { shard_index = loader->shard_indices[shard_index]; } // use the first glob match as the filename for now const char* filename = loader->glob_result.gl_pathv[shard_index]; // open the input file for reading. also only a single file can be opened at a time if (loader->tokens_file != NULL) { fcloseCheck(loader->tokens_file); } loader->tokens_file = fopenCheck(filename, "rb"); // validate the header int header[HEADER_SIZE]; freadCheck(header, sizeof(int), HEADER_SIZE, loader->tokens_file); if (header[0] != 20240520) { printf("Bad magic in the data file\n"); printf("---> HINT: Are you passing in a correct file?\n"); printf("---> HINT: The data encoding may have changed, re-run data prepro or refer again to README.\n"); exit(EXIT_FAILURE); } if (header[1] != 1) { printf("Bad version in data file\n"); exit(EXIT_FAILURE); } int64_t ntok = header[2]; // number of tokens in the file assert(ntok > 0); // we expect some tokens in the file. this should never trip, right? // determine the file size and make sure it is consistent with the number of tokens fseekCheck(loader->tokens_file, 0, SEEK_END); // seek to end of file loader->file_size_bytes = ftell(loader->tokens_file); // read the offset, i.e. file size fseekCheck(loader->tokens_file, 0, SEEK_SET); // seek back to the beginning // we expect ntok in the file to be consistent with filesize, assert that is the case int64_t expected_file_size = HEADER_SIZE * sizeof(int) + ntok * sizeof(uint16_t); if (loader->file_size_bytes != expected_file_size) { printf("Error: file size is not as expected\n"); exit(EXIT_FAILURE); } // -1 uint16_t due to us taking B*T+1 tokens but moving by B*T tokens loader->shard_num_samples = (ntok * sizeof(uint16_t) - sizeof(uint16_t)) / loader->total_batch_size_bytes; return ntok; } void prepare_intra_shard_indices_(DataLoader *loader) { // shuffle the examples inside the shards if (loader->intra_shard_indices != NULL) { // in case shards have different number of samples / sizes free(loader->intra_shard_indices); } loader->intra_shard_indices = (int*)mallocCheck(loader->shard_num_samples * sizeof(int)); init_identity_permutation(loader->intra_shard_indices, (int) loader->shard_num_samples); random_permutation(loader->intra_shard_indices, (int) loader->shard_num_samples, &loader->shuffle_rng); } void dataloader_reset(DataLoader *loader) { loader->current_shard_idx = 0; loader->current_sample_idx = 0; if (loader->should_shuffle) { // shuffle the shards random_permutation(loader->shard_indices, (int) loader->glob_result.gl_pathc, &loader->shuffle_rng); } dataloader_load_shard_(loader, (int) loader->current_shard_idx); if (loader->should_shuffle) { prepare_intra_shard_indices_(loader); } } void dataloader_advance_(DataLoader *loader) { if (loader->current_shard_idx == loader->glob_result.gl_pathc - 1) { // if we are at the last shard, we reset the loader and start a new epoch dataloader_reset(loader); return; } // advance the loader by loading the next data shard and resetting the position loader->current_shard_idx = (loader->current_shard_idx + 1) % loader->glob_result.gl_pathc; loader->current_sample_idx = 0; dataloader_load_shard_(loader, (int) loader->current_shard_idx); if (loader->should_shuffle) { prepare_intra_shard_indices_(loader); } } void dataloader_init(DataLoader *loader, const char* filename_pattern, size_t B, size_t T, int process_rank, int num_processes, int should_shuffle) { loader->process_rank = process_rank; loader->num_processes = num_processes; loader->B = B; loader->T = T; loader->tokens_file = NULL; loader->should_shuffle = should_shuffle; loader->header_bytes = HEADER_SIZE * sizeof(int); loader->total_batch_size_bytes = ((loader->num_processes * (loader->B * loader->T)) * sizeof(uint16_t)); loader->local_batch_offset_bytes = loader->process_rank * loader->B * loader->T * sizeof(uint16_t); // glob to get the list of files matching the pattern, these are our data shards int glob_status = glob(filename_pattern, 0, NULL, &loader->glob_result); if (glob_status != 0) { printf("Error: failed to glob pattern: %s\n", filename_pattern); exit(EXIT_FAILURE); } if (loader->glob_result.gl_pathc == 0) { printf("Error: no files found matching the pattern: %s\n", filename_pattern); exit(EXIT_FAILURE); } if (should_shuffle) { mt19937_state shuffle_rng; manual_seed(&shuffle_rng, 42 + process_rank); loader->shuffle_rng = shuffle_rng; loader->shard_indices = (int*)mallocCheck(loader->glob_result.gl_pathc * sizeof(int)); init_identity_permutation(loader->shard_indices, (int) loader->glob_result.gl_pathc); loader->intra_shard_indices = NULL; // dynamically allocated allowing different shard sizes } // inspect and validate all shards so we don't get any runtime errors later // if too slow / too many shards, may wish to revisit later int64_t ntok_total = 0; for (int shard_index = 0; shard_index < loader->glob_result.gl_pathc; shard_index++) { int64_t shard_ntok = dataloader_load_shard_(loader, shard_index); // we need at least one batch/shard, the way things are written right now. // can be relaxed a lot later. assert(shard_ntok >= (int64_t) (num_processes * B * T + 1)); ntok_total += shard_ntok; } // debugging prints // printf("DataLoader: filename_pattern: %s\n", filename_pattern); // printf("DataLoader: Found %ld tokens across %zu shards\n", ntok_total, loader->glob_result.gl_pathc); // allocate all the space we'll need loader->buffer = (uint16_t*)mallocCheck((B * T + 1) * sizeof(uint16_t)); loader->inputs = (int*)mallocCheck(B * T * sizeof(int)); loader->targets = (int*)mallocCheck(B * T * sizeof(int)); loader->num_tokens = ntok_total; // reset the loader, to initialize it dataloader_reset(loader); } void dataloader_load_batch(DataLoader* loader) { assert(!loader->should_shuffle || (loader->should_shuffle && loader->intra_shard_indices != NULL)); assert(loader->current_sample_idx < loader->shard_num_samples); size_t idx = loader->should_shuffle ? loader->intra_shard_indices[loader->current_sample_idx] : loader->current_sample_idx; size_t global_batch_offset_bytes = idx * loader->total_batch_size_bytes; int64_t current_offset = loader->header_bytes + global_batch_offset_bytes + loader->local_batch_offset_bytes; size_t B = loader->B; size_t T = loader->T; // read B*T+1 uint16_t tokens from the file into buffer fseekCheck(loader->tokens_file, (int) current_offset, SEEK_SET); freadCheck(loader->buffer, sizeof(uint16_t), B*T+1, loader->tokens_file); // decode the buffer into inputs and targets (cast to int) for (int i = 0; i < B*T; i++) { loader->inputs[i] = (int)loader->buffer[i]; loader->targets[i] = (int)loader->buffer[i+1]; } } void dataloader_next_batch(DataLoader *loader) { // if the next batch would go past the end of the file, advance the loader if (loader->current_sample_idx >= loader->shard_num_samples) { dataloader_advance_(loader); } dataloader_load_batch(loader); loader->current_sample_idx += 1; } void dataloader_resume(DataLoader *loader, size_t current_shard_idx, size_t current_sample_idx) { // used during model resumption (-y 1) flag loader->current_shard_idx = current_shard_idx; loader->current_sample_idx = current_sample_idx; dataloader_load_shard_(loader, (int) loader->current_shard_idx); } void dataloader_free(DataLoader *loader) { free(loader->buffer); free(loader->inputs); free(loader->targets); if (loader->should_shuffle) { free(loader->shard_indices); free(loader->intra_shard_indices); } fcloseCheck(loader->tokens_file); globfree(&loader->glob_result); } // ---------------------------------------------------------------------------- // Distributed Eval Loader // Many evals (like) HellaSwag and MMLU are multiple-choice // where there are 4 possible continuations and a label for the correct one // We want to load and serve these style of evals /* Copy pasting the section on the eval datafile format, from data_common.py: - First comes a header with 256 int32s - The examples follow, each example is a stream of uint16_t: - delimiter of 2**16-1, i.e. 65,535 - , bytes encoding this example, allowing efficient skip to next - , the index of the example in the dataset -