def _inference_with_bleu(self, generator, sample, model):
    import sacrebleu

    def decode(toks, escape_unk=False):
        s = self.tgt_dict.string(toks.int().cpu(), self.args.eval_bleu_remove_bpe, unk_string=('UNKNOWNTOKENINREF' if escape_unk else 'UNKNOWNTOKENINHYP'))
        if self.tokenizer:
            s = self.tokenizer.decode(s)
        return s
    gen_out = self.inference_step(generator, [model], sample, prefix_tokens=None)
    (hyps, refs) = ([], [])
    for i in range(len(gen_out)):
        hyps.append(decode(gen_out[i][0]['tokens']))
        refs.append(decode(utils.strip_pad(sample['target'][i], self.tgt_dict.pad()), escape_unk=True))
    if self.args.eval_bleu_print_samples:
        logger.info(('example hypothesis: ' + hyps[0]))
        logger.info(('example reference: ' + refs[0]))
    if self.args.eval_tokenized_bleu:
        return sacrebleu.corpus_bleu(hyps, [refs], tokenize='none')
    else:
        return sacrebleu.corpus_bleu(hyps, [refs])
def _inference_with_bleu(self, generator, sample, model):
    import sacrebleu

    def decode(toks, escape_unk=False):
        s = self.tgt_dict.string(toks.int().cpu(), self.args.eval_bleu_remove_bpe, unk_string=('UNKNOWNTOKENINREF' if escape_unk else 'UNKNOWNTOKENINHYP'))
        if self.tokenizer:
            s = self.tokenizer.decode(s)
        return s
    gen_out = self.inference_step(generator, [model], sample, prefix_tokens=None)
    (hyps, refs) = ([], [])
    for i in range(len(gen_out)):
        hyps.append(decode(gen_out[i][0]['tokens']))
        refs.append(decode(utils.strip_pad(sample['target'][i], self.tgt_dict.pad()), escape_unk=True))
    if self.args.eval_bleu_print_samples:
        logger.info(('example hypothesis: ' + hyps[0]))
        logger.info(('example reference: ' + refs[0]))
    if self.args.eval_tokenized_bleu:
        return sacrebleu.corpus_bleu(hyps, [refs], tokenize='none')
    else:
        return sacrebleu.corpus_bleu(hyps, [refs])