MCPcopy Create free account
hub / github.com/OpenSparseLLMs/Linear-MoE / _train

Function _train

linear_moe/finetune_utils.py:159–265  ·  view source on GitHub ↗

Train the model. Args: model (nn.Module): The model to train. optimizer (Optimizer): The optimizer to use for gradient updates. opt_param_scheduler (Optional): The optimizer parameter scheduler. forward_step (callable): The forward step function for the mode

(model, optimizer, opt_param_scheduler, forward_step,
           train_dataloader, valid_dataloader, end_of_epoch_callback, config)

Source from the content-addressed store, hash-verified

157
158
159def _train(model, optimizer, opt_param_scheduler, forward_step,
160 train_dataloader, valid_dataloader, end_of_epoch_callback, config):
161 """
162 Train the model.
163
164 Args:
165 model (nn.Module): The model to train.
166 optimizer (Optimizer): The optimizer to use for gradient updates.
167 opt_param_scheduler (Optional): The optimizer parameter scheduler.
168 forward_step (callable): The forward step function for the model.
169 train_dataloader (DataLoader): The dataloader for training data.
170 valid_dataloader (DataLoader): The dataloader for validation data.
171 end_of_epoch_callback (Optional[callable]): The callback function to call at the end of each epoch.
172 """
173
174 args = get_args()
175 timers = get_timers()
176
177 assert get_num_microbatches(
178 ) == 1, "finetuning with gradient accumulation doesn't currently work"
179
180 # Turn on training mode which enables dropout.
181 for m in model:
182 m.train()
183
184 # Tracking loss.
185 losses_dict_sum = {}
186
187 # Starting epoch and iteration
188 start_epoch = args.iteration // args.train_iters_per_epoch
189 start_iteration = args.iteration % args.train_iters_per_epoch
190 iteration = args.iteration
191
192 # Memory reporting flag.
193 report_memory_flag = True
194 # For each remaining epoch
195 timers('interval-time', log_level=0).start(barrier=True)
196 for epoch in range(start_epoch, args.epochs):
197 print_rank_0('working on epoch {} ...'.format(epoch + 1))
198
199 # Set the data loader epoch to shuffle the index iterator.
200 train_dataloader.sampler.set_epoch(args.seed + epoch)
201
202 # For all the batches in the dataset.
203 for iteration_, batch in enumerate(train_dataloader):
204
205 # Ignore the iterations before starting value
206 if iteration_ < start_iteration:
207 continue
208 # Set to zero so the next epoch does not skip any batches.
209 start_iteration = 0
210
211 # Train for one step.
212 out = train_step(forward_step, batch, model, optimizer,
213 opt_param_scheduler, config)
214
215 losses_dict, skipped_iter, grad_norm, num_zeros_in_grad = out
216 iteration += 1

Callers 1

finetuneFunction · 0.85

Calls 4

train_stepFunction · 0.90
training_logFunction · 0.90
get_argsFunction · 0.50

Tested by

no test coverage detected