def reduce_metrics(logging_outputs) -> None:
        """Aggregate logging outputs from data parallel training."""
        loss_sum = sum(log.get("loss", 0) for log in logging_outputs)
        ntokens = sum(log.get("ntokens", 0) for log in logging_outputs)
        sample_size = sum(log.get("sample_size", 0) for log in logging_outputs)
        
        loss_items = []
        # log individual losses
        for loss_name in LOSS_NAMES:
            loss_sum = sum(log.get(loss_name, 0) for log in logging_outputs)
            single_sample_size = sum(log.get(loss_name + "sample_size", 0) for log in logging_outputs)

            if loss_sum != 0:
                metrics.log_scalar(
                    loss_name, loss_sum / single_sample_size / math.log(2), single_sample_size, round=3
                )
                metrics.log_scalar(
                    loss_name + "_sample_size", single_sample_size, round=3
                )
                loss_items.append(loss_sum / single_sample_size / math.log(2))
            else:
                metrics.log_scalar(
                    loss_name + "_sample_size", 0, round=3
                )
        metrics.log_scalar(
            "loss", sum(loss_items) / len(loss_items), sample_size, round=3
        )

        if sample_size != ntokens:
            metrics.log_scalar(
                "nll_loss", loss_sum / ntokens / math.log(2), ntokens, round=3
            )
            metrics.log_derived(
                "ppl", lambda meters: utils.get_perplexity(meters["nll_loss"].avg)
            )
        else:
            metrics.log_derived(
                "ppl", lambda meters: utils.get_perplexity(meters["loss"].avg)
            )
Aggregate logging outputs from data parallel training.
kosmos-2/unilm/criterions/unigpt.py