Evaluation.
(forward_step_func,
data_iterator,
model,
process_non_loss_data_func,
config,
verbose=False)
| 699 | |
| 700 | |
| 701 | def evaluate(forward_step_func, |
| 702 | data_iterator, |
| 703 | model, |
| 704 | process_non_loss_data_func, |
| 705 | config, |
| 706 | verbose=False): |
| 707 | """Evaluation.""" |
| 708 | args = get_args() |
| 709 | |
| 710 | if args.vision_pretraining and args.vision_pretraining_type == "dino": |
| 711 | compute_feature_bank(model) |
| 712 | |
| 713 | # Turn on evaluation mode which disables dropout. |
| 714 | for model_module in model: |
| 715 | model_module.eval() |
| 716 | |
| 717 | total_loss_dict = {} |
| 718 | |
| 719 | # make validation batch size independent from training batch size |
| 720 | eval_batch_size = args.global_batch_size |
| 721 | eval_num_microbatches = eval_batch_size // \ |
| 722 | (args.micro_batch_size * args.data_parallel_size) |
| 723 | |
| 724 | with torch.no_grad(): |
| 725 | iteration = 0 |
| 726 | if verbose: |
| 727 | print_rank_0(f'Evaluating on {args.eval_iters * eval_batch_size} samples') |
| 728 | while iteration < args.eval_iters: |
| 729 | iteration += 1 |
| 730 | if verbose: |
| 731 | print_rank_0(f'Evaluating iter {iteration}/{args.eval_iters}') |
| 732 | |
| 733 | forward_backward_func = get_forward_backward_func() |
| 734 | # Don't care about timing during evaluation |
| 735 | config.timers = None |
| 736 | loss_dicts = forward_backward_func( |
| 737 | forward_step_func=forward_step_func, |
| 738 | data_iterator=data_iterator, |
| 739 | model=model, |
| 740 | num_microbatches=eval_num_microbatches, |
| 741 | seq_length=args.seq_length, |
| 742 | micro_batch_size=args.micro_batch_size, |
| 743 | decoder_seq_length=args.decoder_seq_length, |
| 744 | forward_only=True) |
| 745 | config.timers = get_timers() |
| 746 | |
| 747 | # Empty unused memory |
| 748 | if args.empty_unused_memory_level >= 1: |
| 749 | torch.cuda.empty_cache() |
| 750 | |
| 751 | if mpu.is_pipeline_last_stage(ignore_virtual=True): |
| 752 | # Reduce across processes. |
| 753 | for loss_dict in loss_dicts: |
| 754 | for key in loss_dict: |
| 755 | total_loss_dict[key] = total_loss_dict.get( |
| 756 | key, torch.cuda.FloatTensor([0.0])) + loss_dict[key] |
| 757 | |
| 758 | args.consumed_valid_samples += eval_batch_size |
no test coverage detected