Main training program. 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_val_test_data_provider to get train/val/test datasets. 4) train the modle
(
train_valid_test_dataset_provider,
model_provider,
forward_step_func,
valid_forward_step_func=None,
extra_args_provider=None,
args_defaults={},
)
| 75 | |
| 76 | |
| 77 | def pretrain( |
| 78 | train_valid_test_dataset_provider, |
| 79 | model_provider, |
| 80 | forward_step_func, |
| 81 | valid_forward_step_func=None, |
| 82 | extra_args_provider=None, |
| 83 | args_defaults={}, |
| 84 | ): |
| 85 | """Main training program. |
| 86 | |
| 87 | This function will run the followings in the order provided: |
| 88 | 1) initialize Megatron. |
| 89 | 2) setup model, optimizer and lr schedule using the model_provider. |
| 90 | 3) call train_val_test_data_provider to get train/val/test datasets. |
| 91 | 4) train the modle using the forward_step_func. |
| 92 | |
| 93 | Arguments: |
| 94 | train_valid_test_dataset_provider: a function that takes the size of |
| 95 | train/valid/test dataset and returns `train, valid, test` datasets. |
| 96 | model_provider: a function that returns a vanilla version of the |
| 97 | model. By vanilla we mean a simple model on cpu with no fp16 or ddp. |
| 98 | forward_step_func: a function that takes a `data iterator` and `model`, |
| 99 | and returns a `loss` scalar with a dictionary with key:values being |
| 100 | the info we would like to monitor during training, for example |
| 101 | `lm-loss: value`. We also require that this function add |
| 102 | `batch generator` to the timers class. |
| 103 | extra_args_provider: a function that takes a parser and adds arguments |
| 104 | to it. It is used for programs to add their own arguments. |
| 105 | args_defaults: a dictionary from argument-name to argument-value. It |
| 106 | to set already parse arguments. |
| 107 | """ |
| 108 | |
| 109 | # Initalize and get arguments, timers, and Tensorboard writer. |
| 110 | initialize_megatron( |
| 111 | extra_args_provider=extra_args_provider, args_defaults=args_defaults |
| 112 | ) |
| 113 | |
| 114 | # Adjust the startup time so it reflects the largest value. |
| 115 | # This will be closer to what scheduler will see (outside of |
| 116 | # image ... launches. |
| 117 | global _TRAIN_START_TIME |
| 118 | start_time_tensor = torch.cuda.FloatTensor([_TRAIN_START_TIME]) |
| 119 | torch.distributed.all_reduce(start_time_tensor, op=torch.distributed.ReduceOp.MIN) |
| 120 | _TRAIN_START_TIME = start_time_tensor.item() |
| 121 | print_rank_0( |
| 122 | "time to initialize megatron (seconds): {:.3f}".format( |
| 123 | time.time() - _TRAIN_START_TIME |
| 124 | ) |
| 125 | ) |
| 126 | print_datetime("after megatron is initialized") |
| 127 | |
| 128 | args = get_args() |
| 129 | timers = get_timers() |
| 130 | |
| 131 | if args.local_rank == 0 and args.save is not None: |
| 132 | print(f"Creating output dir ...") |
| 133 | os.makedirs(args.save, exist_ok=True) |
| 134 |
no test coverage detected