Main training program.
(args, model_cls, forward_step_function, create_dataset_function, handle_metrics_function=None, init_function=None, collate_fn=None, forward_step_eval=None)
| 47 | print("wandb not installed.") |
| 48 | |
| 49 | def training_main(args, model_cls, forward_step_function, create_dataset_function, handle_metrics_function=None, init_function=None, collate_fn=None, forward_step_eval=None): |
| 50 | """Main training program.""" |
| 51 | hooks = { |
| 52 | 'forward_step': forward_step_function, |
| 53 | 'init_function': init_function, |
| 54 | 'create_dataset_function': create_dataset_function, |
| 55 | 'handle_metrics': handle_metrics_function, |
| 56 | 'forward_step_eval': forward_step_eval or forward_step_function |
| 57 | } |
| 58 | |
| 59 | timers = Timers() # Timer. |
| 60 | |
| 61 | # Experiment Name |
| 62 | if args.load and args.mode == 'pretrain': # continue training |
| 63 | args.experiment_name = os.path.basename(os.path.normpath(args.load)) |
| 64 | else: |
| 65 | args.experiment_name = args.experiment_name + '-' +datetime.now().strftime("%m-%d-%H-%M") |
| 66 | |
| 67 | # Pytorch distributed. must before seed. ALREADY MOVED TO arguments.py! |
| 68 | # if isinstance(model_cls, type): |
| 69 | # initialize_distributed(args) |
| 70 | # set_random_seed(args.seed) # Random seeds for reproducability. |
| 71 | |
| 72 | # Data stuff. |
| 73 | train_data, val_data, test_data = make_loaders(args, hooks['create_dataset_function'], collate_fn=collate_fn) |
| 74 | if args.epochs: |
| 75 | args.train_iters = len(train_data) |
| 76 | if args.eval_interval is None: |
| 77 | args.eval_interval = len(train_data)//args.epochs |
| 78 | if args.save_interval is None: |
| 79 | args.save_interval = len(train_data)//args.epochs |
| 80 | |
| 81 | # Build model |
| 82 | if isinstance(model_cls, type): |
| 83 | model = get_model(args, model_cls) |
| 84 | else: |
| 85 | model = model_cls |
| 86 | # for given model, make sure all the params are in the correct device, or the sync param will raise error |
| 87 | correct_device = torch.device(args.device) |
| 88 | for param in model.parameters(): |
| 89 | if param.device != correct_device: |
| 90 | param.data = param.data.to(correct_device) |
| 91 | # register buffer |
| 92 | for name, buffer in model.named_buffers(): |
| 93 | if buffer.device != correct_device: |
| 94 | buffer.data = buffer.data.to(correct_device) |
| 95 | |
| 96 | # Config model IO |
| 97 | if args.load is not None: |
| 98 | args.iteration = load_checkpoint(model, args) |
| 99 | # if we don't load optim_states, filelock is no more needed. |
| 100 | # with FileLock("/root/checkpoint_lock", timeout=-1): |
| 101 | # args.iteration = load_checkpoint(model, optimizer, args) |
| 102 | else: |
| 103 | args.iteration = 0 |
| 104 | if args.save: |
| 105 | args.save = os.path.join(args.save, args.experiment_name) |
| 106 | torch.distributed.barrier() |
no test coverage detected