code: def test_lm_trainable_and_decodable(lm_name, lm_args, 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"
          )
  
      dtype = getattr(torch, dtype)
      model, x, ilens, y, data, train_args = prepare("rnn", rnn_args)
      char_list = train_args.char_list
      n_vocab = len(char_list)
      lm = dynamic_import_lm(lm_name, backend="pytorch").build(
          n_vocab, **lm_args
      )
      lm.to(device=device, dtype=dtype)
  
      # test trainable
      a = torch.randint(1, n_vocab, (3, 2), device=device)
      b = torch.randint(1, n_vocab, (3, 2), device=device)
      loss, logp, count = lm(a, b)
      loss.backward()
      for p in lm.parameters():
          assert p.grad is not None
  
      # test decodable
      model.to(device=device, dtype=dtype).eval()
      lm.eval()
  
      scorers = model.scorers()
      scorers["lm"] = lm
      scorers["length_bonus"] = LengthBonus(len(char_list))
      weights = dict(decoder=1.0, lm=1.0, length_bonus=1.0)
      with torch.no_grad():
          feat = x[0, : ilens[0]].to(device=device, dtype=dtype)
          enc = model.encode(feat)
          beam_size = 3
          result = beam_search(
              x=enc,
              sos=model.sos,
              eos=model.eos,
              beam_size=beam_size,
              vocab_size=len(train_args.char_list),
              weights=weights,
              scorers=scorers,
              token_list=train_args.char_list,
          )
      assert len(result) >= beam_size
  
file_path: espnet/test/test_lm.py
imports: from test.test_beam_search import prepare, rnn_args
  import chainer
  import numpy
  import pytest
  import torch
  import espnet.lm.chainer_backend.lm
  import espnet.nets.pytorch_backend.lm.default
  from espnet.nets.beam_search import beam_search
  from espnet.nets.lm_interface import dynamic_import_lm
  from espnet.nets.scorers.length_bonus import LengthBonus
label_desc: Use image node_id 4 for calling a global function with example usage: test_lm_trainable_and_decodable(lm_name, lm_args, device, dtype) without return types