code: def test_attn_loss():
      n_out = 7
      _eos = n_out - 1
      n_batch = 3
      label_length = numpy.array([4, 2, 3], dtype=numpy.int32)
      np_pred = numpy.random.rand(
          n_batch, max(label_length) + 1, n_out
      ).astype(numpy.float32)
      # NOTE: 0 is only used for CTC, never appeared in attn target
      np_target = [
          numpy.random.randint(1, n_out - 1, size=ol, dtype=numpy.int32)
          for ol in label_length
      ]
  
      eos = numpy.array([_eos], "i")
      ys_out = [F.concat([y, eos], axis=0) for y in np_target]
  
      # padding for ys with -1
      # pys: utt x olen
      # NOTE: -1 is default ignore index for chainer
      pad_ys_out = F.pad_sequence(ys_out, padding=-1)
      y_all = F.reshape(
          np_pred, (n_batch * (max(label_length) + 1), n_out)
      )
      ch_loss = F.softmax_cross_entropy(
          y_all, F.concat(pad_ys_out, axis=0)
      )
  
      # NOTE: this index 0 is only for CTC not attn. so it can be ignored
      # unfortunately, torch cross_entropy does not accept out-of-bound ids
      th_ignore = 0
      th_pred = torch.from_numpy(y_all.data)
      th_target = pad_list(
          [torch.from_numpy(t.data).long() for t in ys_out], th_ignore
      )
      th_loss = torch.nn.functional.cross_entropy(
          th_pred,
          th_target.view(-1),
          ignore_index=th_ignore,
          reduction="mean",
      )
      print(ch_loss)
      print(th_loss)
  
      # NOTE: chainer's default setting are normalized by batch-size
      loss_data = float(th_loss)
      numpy.testing.assert_allclose(loss_data, ch_loss.data, 0.05)
  
file_path: espnet/test/test_loss.py
imports: import chainer.functions
  import numpy
  import pytest
  import torch
  from espnet.nets.pytorch_backend.e2e_asr import pad_list
  from espnet.nets.pytorch_backend.nets_utils import th_accuracy
label_desc: Use image node_id 2 for calling a global function with example usage: test_attn_loss() without return types