MCPcopy Create free account
hub / github.com/Pints-AI/1.5-Pints / train

Function train

pretrain/main.py:439–638  ·  view source on GitHub ↗

Trains the model

(
    fabric: lightning.Fabric,
    state: TrainingState,
    train_dataloader: DataLoader,
    val_dataloader: Optional[DataLoader],
    monitor: Monitor,
    resume: Union[Path, bool],
    training_params: TrainingParams,
    ignore_index=-100,
)

Source from the content-addressed store, hash-verified

437
438
439def train(
440 fabric: lightning.Fabric,
441 state: TrainingState,
442 train_dataloader: DataLoader,
443 val_dataloader: Optional[DataLoader],
444 monitor: Monitor,
445 resume: Union[Path, bool],
446 training_params: TrainingParams,
447 ignore_index=-100,
448):
449 """Trains the model"""
450
451 model = state['model']
452 optimizer = state['optimizer']
453
454 if val_dataloader is not None:
455 validate(
456 fabric, model, val_dataloader, training_params['eval_iters']
457 ) # sanity check
458
459 with torch.device('meta'):
460 meta_model = GPT(model.config)
461 # "estimated" is not as precise as "measured". Estimated is optimistic but widely used in the wild.
462 # When comparing MFU or FLOP numbers with other projects that use estimated FLOPs,
463 # consider passing `SpeedMonitor(flops_per_batch=estimated_flops)` instead
464 estimated_flops = (
465 estimate_flops(meta_model) * training_params['micro_batch_size']
466 )
467
468 fabric.print(
469 f'Estimated TFLOPs: { estimated_flops * fabric.world_size / 1e12:.2f}'
470 )
471 x = torch.randint(
472 0, 1, (training_params['micro_batch_size'], model.config.block_size)
473 )
474 # measured_flos run in meta. Will trigger fusedRMSNorm error
475 # measured_flops = measure_flops(meta_model, x)
476 # fabric.print(f"Measured TFLOPs: {measured_flops * fabric.world_size / 1e12:.2f}")
477 del meta_model, x
478
479 total_lengths = 0
480 total_t0 = time.perf_counter()
481
482 if fabric.device.type == 'xla':
483 import torch_xla.core.xla_model as xm
484
485 xm.mark_step()
486
487 initial_iter = state['iter_num']
488 curr_iter = 0
489
490 loss_func = FusedCrossEntropyLoss(ignore_index=ignore_index)
491 train_dataloader: Union[CycleIterator, DataLoader] = CycleIterator(train_dataloader)
492
493 for train_data in train_dataloader:
494 if resume:
495 if curr_iter < initial_iter:
496 curr_iter += 1

Callers 1

mainFunction · 0.85

Calls 10

GPTClass · 0.90
estimate_flopsFunction · 0.90
CycleIteratorClass · 0.90
get_lrFunction · 0.85
save_checkpointFunction · 0.85
eval_endMethod · 0.80
validateFunction · 0.70
backwardMethod · 0.45
on_train_batch_endMethod · 0.45

Tested by

no test coverage detected