def validate(
    cfg: DictConfig,
    task: tasks.FairseqTask,
    model,
    criterion,
    subsets: List[str],
) -> List[Optional[float]]:
    """Evaluate the model on the validation set(s) and return the losses."""
    if cfg.dataset.fixed_validation_seed is not None:
        # set fixed seed for every validation
        utils.set_torch_seed(cfg.dataset.fixed_validation_seed)

    valid_losses = []
    for subset in subsets:
        logger.info('begin validation on "{}" subset'.format(subset))

        # Initialize data iterator
        itr = task.get_batch_iterator(
            dataset=task.dataset(subset),
            max_tokens=cfg.dataset.max_tokens_valid,
            max_sentences=cfg.dataset.batch_size_valid,
            required_batch_size_multiple=cfg.dataset.required_batch_size_multiple,
            seed=cfg.common.seed,
            num_workers=cfg.dataset.num_workers_valid,
            # always pass a fixed "epoch" to keep validation data consistent
            # across training epochs
            epoch=1,
            data_buffer_size=cfg.dataset.data_buffer_size,
        ).next_epoch_itr(
            shuffle=False, set_dataset_epoch=False  # use a fixed valid set
        )
        if cfg.common.tpu:
            itr = utils.tpu_data_loader(itr)
        progress = progress_bar.progress_bar(
            itr,
            log_format=cfg.common.log_format,
            log_interval=cfg.common.log_interval,
            prefix=f"valid on '{subset}' subset",
            tensorboard_logdir=(
                cfg.common.tensorboard_logdir
                if distributed_utils.is_master(cfg.distributed_training)
                else None
            ),
            default_log_format=("tqdm" if not cfg.common.no_progress_bar else "simple"),
            wandb_project=(
                cfg.common.wandb_project
                if distributed_utils.is_master(cfg.distributed_training)
                else None
            ),
            wandb_run_name=os.environ.get(
                "WANDB_NAME", os.path.basename(cfg.checkpoint.save_dir)
            ),
        )

        # create a new root metrics aggregator so validation metrics
        # don't pollute other aggregators (e.g., train meters)
        with metrics.aggregate(new_root=True) as agg:
            logging_outputs = []
            for i, sample in enumerate(progress):
                if cfg.dataset.max_valid_steps is not None and i > cfg.dataset.max_valid_steps:
                    break
                sample = utils.move_to_cuda(sample)
                _, _, inner_logging_outputs = task.valid_step(
                    sample, model, criterion
                )
                logging_outputs.append(inner_logging_outputs)
            task.reduce_metrics(logging_outputs, criterion)
            
        # with metrics.aggregate(new_root=True) as agg:
        #     for i, sample in enumerate(progress):
        #         if (
        #             cfg.dataset.max_valid_steps is not None
        #             and i > cfg.dataset.max_valid_steps
        #         ):
        #             break
        #         trainer.valid_step(sample)

        stats = get_valid_stats(cfg, agg.get_smoothed_values())
        
        progress.print(stats, tag=subset)

        valid_losses.append(stats[cfg.checkpoint.best_checkpoint_metric])
    return valid_losses
Evaluate the model on the validation set(s) and return the losses.
YOCO/yoco/validate.py