class_docstr: We're just going to compare that scores from Fairseq's Bleu scorer are the same as
  our scorer.
class_docstr_tok: ["We","'re","just","going","to","compare","that","scores","from","Fairseq","'s","Bleu","scorer","are","the","same","as","our","scorer","."]
code: def test_scorer(self):
      import random
  
      vocab_length = num_ex = 100
      ex_length = 10
      pad_idx = 0
      eos_idx = 1
      unk_idx = 2
  
      try:
          from fairseq.scoring.bleu import Scorer
          from fairseq.scoring.bleu import BleuConfig
  
          fairseq_metrics: Scorer = Scorer(
              BleuConfig(pad=pad_idx, eos=eos_idx, unk=unk_idx)
          )
      except ImportError:
          # Bleuconfig is a recent version of fairseq
          fairseq_metrics: Scorer = Scorer(pad_idx, eos_idx, unk_idx)
  
      parlai_metrics: Dict[int, FairseqBleuMetric] = {
          k: [] for k in range(1, 5)
      }
  
      for _ in range(num_ex):
          guess = torch.LongTensor(
              random.sample(range(vocab_length), ex_length)
          )
          answer = torch.LongTensor(
              random.sample(range(vocab_length), ex_length)
          )
  
          parlai_bleu = FairseqBleuMetric.compute_many(
              guess, answer.unsqueeze(0), pad_idx, eos_idx, unk_idx
          )
          for i, bleu in enumerate(parlai_bleu):
              parlai_metrics[i + 1].append(bleu)
          fairseq_metrics.add(answer.int(), guess.int())
  
      parlai_bleus = {}
      for k, v in parlai_metrics.items():
          total = v[0]
          for vv in v[1:]:
              total = total + vv
          parlai_bleus[k] = total
  
      fairseq_bleus = {
          k: fairseq_metrics.score(order=k) for k in range(1, 5)
      }
  
      assert all(
          parlai_bleus[k] == fairseq_bleus[k] for k in range(1, 5)
      ), f"{parlai_bleus}\n{fairseq_bleus}"
  
imports: import unittest
  from typing import Dict
  import torch
  import random
  from parlai.core.metrics import AverageMetric, SumMetric, FixedMetric, Metrics, GlobalAverageMetric, MacroAverageMetric, TimerMetric, aggregate_unnamed_reports, aggregate_named_reports, InterDistinctMetric, IntraDistinctMetric, FairseqBleuMetric
  from parlai.core.torch_classifier_agent import ConfusionMatrixMetric, WeightedF1Metric, AUCMetrics
  from parlai.core.torch_generator_agent import PPLMetric
  import parlai.utils.testing
label_desc: Use image node_id 1 for calling the TestFairseqBleuMetric obj's underlying member method code with example usage: obj.test_scorer() without return types