def aggregate_logging_outputs(logging_outputs):
        """Aggregate logging outputs from data parallel training."""
        ntokens = sum(log.get("ntokens", 0) for log in logging_outputs)
        nsentences = sum(log.get("nsentences", 0) for log in logging_outputs)
        sample_size = sum(log.get("sample_size", 0) for log in logging_outputs)
        loss = sum(log.get("loss", 0) for log in logging_outputs)
        nll_loss = sum(log.get("nll_loss", 0) for log in logging_outputs)

        results = {
            "loss": loss / sample_size / math.log(2) if sample_size > 0 else 0.0,
            "nll_loss": nll_loss / sample_size / math.log(2)
            if sample_size > 0
            else 0.0,
            "ntokens": ntokens,
            "nsentences": nsentences,
            "sample_size": sample_size,
        }

        for key in logging_outputs[0]:
            if key[-5:] == "-loss":
                results[key[:-5]] = (
                    sum(log.get(key, 0) for log in logging_outputs)
                    / sample_size
                    / math.log(2)
                    if sample_size > 0
                    else 0.0
                )

        return results
Aggregate logging outputs from data parallel training.
infoxlm/fairseq/fairseq/criterions/nat_loss.py