    def cv(self, model, data_loader, device, args):
        """Cross validation on"""
        model.eval()
        rank = args.get("rank", 0)
        epoch = args.get("epoch", 0)
        log_interval = args.get("log_interval", 10)
        # in order to avoid division by 0
        num_seen_utts = 1
        total_loss = 0.0
        with torch.no_grad():
            for batch_idx, batch in enumerate(data_loader):
                key, feats, target, feats_lengths, target_lengths = batch
                feats = feats.to(device)
                target = target.to(device)
                feats_lengths = feats_lengths.to(device)
                target_lengths = target_lengths.to(device)
                num_utts = target_lengths.size(0)
                if num_utts == 0:
                    continue
                loss_dict = model(feats, feats_lengths, target, target_lengths)
                loss = loss_dict["loss"]
                if torch.isfinite(loss):
                    num_seen_utts += num_utts
                    total_loss += loss.item() * num_utts
                if batch_idx % log_interval == 0:
                    log_str = "CV Batch {}/{} loss {:.6f} ".format(
                        epoch, batch_idx, loss.item()
                    )
                    for name, value in loss_dict.items():
                        if name != "loss" and value is not None:
                            log_str += "{} {:.6f} ".format(name, value.item())
                    log_str += "history loss {:.6f}".format(total_loss / num_seen_utts)
                    log_str += " rank {}".format(rank)
                    logging.debug(log_str)
        return total_loss, num_seen_utts
Cross validation on
modules/wenet_extractor/utils/executor.py