def reduce_metrics(self, logging_outputs, criterion):
    super().reduce_metrics(logging_outputs, criterion)
    if self.args.eval_bleu:

        def sum_logs(key):
            return sum((log.get(key, 0) for log in logging_outputs))
        (counts, totals) = ([], [])
        for i in range(EVAL_BLEU_ORDER):
            counts.append(sum_logs(('_bleu_counts_' + str(i))))
            totals.append(sum_logs(('_bleu_totals_' + str(i))))
        if (max(totals) > 0):
            metrics.log_scalar('_bleu_counts', np.array(counts))
            metrics.log_scalar('_bleu_totals', np.array(totals))
            metrics.log_scalar('_bleu_sys_len', sum_logs('_bleu_sys_len'))
            metrics.log_scalar('_bleu_ref_len', sum_logs('_bleu_ref_len'))

            def compute_bleu(meters):
                import inspect
                import sacrebleu
                fn_sig = inspect.getfullargspec(sacrebleu.compute_bleu)[0]
                if ('smooth_method' in fn_sig):
                    smooth = {'smooth_method': 'exp'}
                else:
                    smooth = {'smooth': 'exp'}
                bleu = sacrebleu.compute_bleu(correct=meters['_bleu_counts'].sum, total=meters['_bleu_totals'].sum, sys_len=meters['_bleu_sys_len'].sum, ref_len=meters['_bleu_ref_len'].sum, **smooth)
                return round(bleu.score, 2)
            metrics.log_derived('bleu', compute_bleu)
def reduce_metrics(self, logging_outputs, criterion):
    super().reduce_metrics(logging_outputs, criterion)
    if self.args.eval_bleu:

        def sum_logs(key):
            return sum((log.get(key, 0) for log in logging_outputs))
        (counts, totals) = ([], [])
        for i in range(EVAL_BLEU_ORDER):
            counts.append(sum_logs(('_bleu_counts_' + str(i))))
            totals.append(sum_logs(('_bleu_totals_' + str(i))))
        if (max(totals) > 0):
            metrics.log_scalar('_bleu_counts', np.array(counts))
            metrics.log_scalar('_bleu_totals', np.array(totals))
            metrics.log_scalar('_bleu_sys_len', sum_logs('_bleu_sys_len'))
            metrics.log_scalar('_bleu_ref_len', sum_logs('_bleu_ref_len'))

            def compute_bleu(meters):
                import inspect
                import sacrebleu
                fn_sig = inspect.getfullargspec(sacrebleu.compute_bleu)[0]
                if ('smooth_method' in fn_sig):
                    smooth = {'smooth_method': 'exp'}
                else:
                    smooth = {'smooth': 'exp'}
                bleu = sacrebleu.compute_bleu(correct=meters['_bleu_counts'].sum, total=meters['_bleu_totals'].sum, sys_len=meters['_bleu_sys_len'].sum, ref_len=meters['_bleu_ref_len'].sum, **smooth)
                return round(bleu.score, 2)
            metrics.log_derived('bleu', compute_bleu)