Main training program. Refer to https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/training.py This function will run the followings in the order provided: 1) initialize Megatron. 2) setup model, optimizer and lr schedule using the model_provider. 3) call train
(train_valid_test_dataset_provider,
model_provider,
model_type,
forward_step_func,
process_non_loss_data_func=None,
extra_args_provider=None,
args_defaults={'tokenizer_type': 'GPT2BPETokenizer'})
| 49 | |
| 50 | |
| 51 | def pretrain(train_valid_test_dataset_provider, |
| 52 | model_provider, |
| 53 | model_type, |
| 54 | forward_step_func, |
| 55 | process_non_loss_data_func=None, |
| 56 | extra_args_provider=None, |
| 57 | args_defaults={'tokenizer_type': 'GPT2BPETokenizer'}): |
| 58 | """Main training program. |
| 59 | Refer to https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/training.py |
| 60 | |
| 61 | This function will run the followings in the order provided: |
| 62 | 1) initialize Megatron. |
| 63 | 2) setup model, optimizer and lr schedule using the model_provider. |
| 64 | 3) call train_val_test_data_provider to get train/val/test datasets. |
| 65 | 4) train the model using the forward_step_func. |
| 66 | |
| 67 | Arguments: |
| 68 | train_valid_test_dataset_provider: a function that takes the size of |
| 69 | train/valid/test dataset and returns `train, valid, test` datasets. |
| 70 | model_provider: a function that returns a vanilla version of the |
| 71 | model. By vanilla we mean |
| 72 | a simple model on cpu with no fp16 or ddp. |
| 73 | model_type: an enum that specifies the type of model being trained. |
| 74 | forward_step_func: a function that takes a `data iterator` and `model`, |
| 75 | and returns a `loss` scalar with a dictionary with key:values being |
| 76 | the info we would like to monitor during training, for example |
| 77 | `lm-loss: value`. We also require that this function add |
| 78 | `batch generator` to the timers class. |
| 79 | process_non_loss_data_func: a function to post process outputs of the |
| 80 | network. It can be used for dumping output tensors (e.g images) to |
| 81 | tensorboard. It takes `collected data`(list of tensors), |
| 82 | `current iteration index` and `tensorboard writer` as arguments. |
| 83 | extra_args_provider: a function that takes a parser and adds arguments |
| 84 | to it. It is used for programs to add their own arguments. |
| 85 | args_defaults: a dictionary from argument-name to argument-value. It |
| 86 | to set already parse arguments. |
| 87 | """ |
| 88 | |
| 89 | from megatron.initialize import initialize_megatron |
| 90 | initialize_megatron(extra_args_provider=extra_args_provider, |
| 91 | args_defaults=args_defaults) |
| 92 | |
| 93 | # Set pytorch JIT layer fusion options and warmup JIT functions. |
| 94 | set_jit_fusion_options() |
| 95 | |
| 96 | # Adjust the startup time so it reflects the largest value. |
| 97 | # This will be closer to what scheduler will see (outside of |
| 98 | # image ... launches. |
| 99 | global _TRAIN_START_TIME |
| 100 | start_time_tensor = torch.cuda.DoubleTensor([_TRAIN_START_TIME]) |
| 101 | torch.distributed.all_reduce(start_time_tensor, |
| 102 | op=torch.distributed.ReduceOp.MIN) |
| 103 | _TRAIN_START_TIME = start_time_tensor.item() |
| 104 | print_rank_0('time to initialize megatron (seconds): {:.3f}'.format( |
| 105 | time.time() - _TRAIN_START_TIME)) |
| 106 | print_datetime('after megatron is initialized') |
| 107 | |
| 108 | args = get_args() |
no test coverage detected