code: def test_batch_beam_search_equal(
      model_class,
      args,
      ctc_weight,
      lm_nn,
      lm_args,
      lm_weight,
      ngram_weight,
      bonus,
      device,
      dtype,
  ):
      if device == "cuda" and not torch.cuda.is_available():
          pytest.skip("no cuda device is available")
      if device == "cpu" and dtype == "float16":
          pytest.skip(
              "cpu float16 implementation is not available in pytorch yet"
          )
  
      # seed setting
      torch.manual_seed(123)
      torch.backends.cudnn.deterministic = True
      # https://github.com/pytorch/pytorch/issues/6351
      torch.backends.cudnn.benchmark = False
  
      dtype = getattr(torch, dtype)
      model, x, ilens, y, data, train_args = prepare(
          model_class, args, mtlalpha=ctc_weight
      )
      model.eval()
      char_list = train_args.char_list
      lm = dynamic_import_lm(lm_nn, backend="pytorch")(
          len(char_list), lm_args
      )
      lm.eval()
      root = os.path.dirname(os.path.abspath(__file__))
      ngram = NgramFullScorer(
          os.path.join(root, "beam_search_test.arpa"), args.char_list
      )
  
      # test previous beam search
      args = Namespace(
          beam_size=3,
          penalty=bonus,
          ctc_weight=ctc_weight,
          maxlenratio=0,
          lm_weight=lm_weight,
          ngram_weight=ngram_weight,
          minlenratio=0,
          nbest=5,
      )
  
      # new beam search
      scorers = model.scorers()
      if lm_weight != 0:
          scorers["lm"] = lm
      if ngram_weight != 0:
          scorers["ngram"] = ngram
      scorers["length_bonus"] = LengthBonus(len(char_list))
      weights = dict(
          decoder=1.0 - ctc_weight,
          ctc=ctc_weight,
          lm=args.lm_weight,
          ngram=args.ngram_weight,
          length_bonus=args.penalty,
      )
      model.to(device, dtype=dtype)
      model.eval()
      with torch.no_grad():
          enc = model.encode(x[0, : ilens[0]].to(device, dtype=dtype))
  
      legacy_beam = BeamSearch(
          beam_size=args.beam_size,
          vocab_size=len(char_list),
          weights=weights,
          scorers=scorers,
          token_list=train_args.char_list,
          sos=model.sos,
          eos=model.eos,
          pre_beam_score_key=None if ctc_weight == 1.0 else "full",
      )
      legacy_beam.to(device, dtype=dtype)
      legacy_beam.eval()
  
      beam = BatchBeamSearch(
          beam_size=args.beam_size,
          vocab_size=len(char_list),
          weights=weights,
          scorers=scorers,
          token_list=train_args.char_list,
          sos=model.sos,
          eos=model.eos,
          pre_beam_score_key=None if ctc_weight == 1.0 else "full",
      )
      beam.to(device, dtype=dtype)
      beam.eval()
      with torch.no_grad():
          legacy_nbest_bs = legacy_beam(
              x=enc,
              maxlenratio=args.maxlenratio,
              minlenratio=args.minlenratio,
          )
          nbest_bs = beam(
              x=enc,
              maxlenratio=args.maxlenratio,
              minlenratio=args.minlenratio,
          )
  
      for i, (expected, actual) in enumerate(
          zip(legacy_nbest_bs, nbest_bs)
      ):
          assert expected.yseq.tolist() == actual.yseq.tolist()
          numpy.testing.assert_allclose(
              expected.score.cpu(), actual.score.cpu(), rtol=1e-6
          )
  
file_path: espnet/test/test_batch_beam_search.py
imports: import os
  from argparse import Namespace
  from test.test_beam_search import prepare, transformer_args
  import numpy
  import pytest
  import torch
  from espnet.nets.batch_beam_search import BatchBeamSearch, BeamSearch
  from espnet.nets.beam_search import Hypothesis
  from espnet.nets.lm_interface import dynamic_import_lm
  from espnet.nets.scorers.length_bonus import LengthBonus
  from espnet.nets.scorers.ngram import NgramFullScorer
label_desc: Use image node_id 2 for calling a global function with example usage: test_batch_beam_search_equal(model_class, args, ctc_weight, lm_nn, lm_args, lm_weight, ngram_weight, bonus, device, dtype) without return types