MCPcopy Create free account
hub / github.com/ASLP-lab/OSUM / train

Method train

OSUM/wenet/utils/executor.py:42–137  ·  view source on GitHub ↗

Train one epoch

(self, model, optimizer, scheduler, train_data_loader,
              cv_data_loader, writer, configs, scaler, group_join)

Source from the content-addressed store, hash-verified

40 self.device = device
41
42 def train(self, model, optimizer, scheduler, train_data_loader,
43 cv_data_loader, writer, configs, scaler, group_join):
44 ''' Train one epoch
45 '''
46 if self.train_step_timer is None:
47 self.train_step_timer = StepTimer(self.step)
48 model.train()
49 init_infos = configs.get("init_infos", {})
50 init_batch_num = init_infos.get("batch_idx", 0)
51 configs.pop("init_infos", None)
52 info_dict = copy.deepcopy(configs)
53 logging.info('using accumulate grad, new batch size is {} times'
54 ' larger than before'.format(info_dict['accum_grad']))
55 # A context manager to be used in conjunction with an instance of
56 # torch.nn.parallel.DistributedDataParallel to be able to train
57 # with uneven inputs across participating processes.
58 if isinstance(model, torch.nn.parallel.DistributedDataParallel):
59 model_context = model.join
60 else:
61 model_context = nullcontext
62 continue_data = info_dict['dataset_conf'].get('continue_data', True)
63 utils_file.logging_info(f'continue_data: {continue_data}')
64 with model_context():
65 logging.info(f'init_batch_num: {init_batch_num}')
66 for batch_idx, batch_dict in enumerate(train_data_loader):
67 if continue_data and batch_idx < init_batch_num:
68 if batch_idx %100 == 0:
69 logging.info(f'skipping: batch_idx {batch_idx}')
70 continue
71 if batch_idx %100 == 0:
72 logging.info(f'using: batch_idx {batch_idx}')
73
74 info_dict["tag"] = "TRAIN"
75 info_dict["step"] = self.step
76 info_dict["batch_idx"] = batch_idx
77
78 if batch_dict["target_lengths"].size(0) == 0:
79 continue
80
81 context = None
82 # Disable gradient synchronizations across DDP processes.
83 # Within this context, gradients will be accumulated on module
84 # variables, which will later be synchronized.
85 if info_dict.get("train_engine", "torch_ddp") in [
86 "torch_ddp", "torch_fsdp"
87 ] and (batch_idx + 1) % info_dict["accum_grad"] != 0:
88 context = model.no_sync
89 # Used for single gpu training and DDP gradient synchronization
90 # processes.
91 else:
92 context = nullcontext
93
94 with context():
95 info_dict = batch_forward(model, batch_dict, scaler,
96 info_dict, self.device)
97 if batch_idx %100 == 0:
98 logging.info(f'after batch_forward: batch_idx {info_dict["batch_idx"]}')
99

Callers 1

mainFunction · 0.95

Calls 8

cvMethod · 0.95
StepTimerClass · 0.90
batch_forwardFunction · 0.90
batch_backwardFunction · 0.90
update_parameter_and_lrFunction · 0.90
log_per_stepFunction · 0.90
save_modelFunction · 0.90
updateMethod · 0.45

Tested by

no test coverage detected