class_docstr: CTC-attention hybrid Encoder-Decoder model
code: def _calc_asr_att_loss(
      self,
      encoder_out: torch.Tensor,
      encoder_out_lens: torch.Tensor,
      ys_pad: torch.Tensor,
      ys_pad_lens: torch.Tensor,
      return_hs: bool = False,
  ):
      # Use CTC output as AR decoder target; useful for multi-decoder training
      skip_loss = False
      if self.training and self.ctc_sample_rate > 0:
          if random.uniform(0, 1) < self.ctc_sample_rate:
              ys_hat = self.ctc.argmax(encoder_out).data
              ys_hat = [[x[0] for x in groupby(ys)] for ys in ys_hat]
              ys_hat = [
                  [x for x in filter(lambda x: x != 0, ys)]
                  for ys in ys_hat
              ]
              for i, ys in enumerate(ys_hat):
                  if len(ys) == 0:
                      ys_hat[i] = [x for x in ys_pad[i] if x != -1]
              ys_pad_lens = torch.tensor(
                  [len(x) for x in ys_hat], device=encoder_out.device
              )
              ys_pad = [
                  torch.tensor(ys, device=encoder_out.device)
                  for ys in ys_pad
              ]
              ys_pad = pad_sequence(
                  ys_pad, batch_first=True, padding_value=-1
              )
  
              # skip the loss
              skip_loss = True
  
      ys_in_pad, ys_out_pad = add_sos_eos(
          ys_pad, self.src_sos, self.src_eos, self.ignore_id
      )
      ys_in_lens = ys_pad_lens + 1
  
      # 1. Forward decoder
      if return_hs:
          decoder_out, _, hs_dec_asr = self.extra_asr_decoder(
              encoder_out,
              encoder_out_lens,
              ys_in_pad,
              ys_in_lens,
              return_hs=True,
          )
      else:
          hs_dec_asr = None
          decoder_out, _ = self.extra_asr_decoder(
              encoder_out, encoder_out_lens, ys_in_pad, ys_in_lens
          )
  
      if skip_loss:
          return 0.0, None, None, None, hs_dec_asr
  
      # 2. Compute attention loss
      loss_att = self.criterion_asr(decoder_out, ys_out_pad)
      acc_att = th_accuracy(
          decoder_out.view(-1, self.src_vocab_size),
          ys_out_pad,
          ignore_label=self.ignore_id,
      )
  
      # Compute cer/wer using attention-decoder
      if self.training or self.asr_error_calculator is None:
          cer_att, wer_att = None, None
      else:
          ys_hat = decoder_out.argmax(dim=-1)
          cer_att, wer_att = self.asr_error_calculator(
              ys_hat.cpu(), ys_pad.cpu()
          )
  
      return loss_att, acc_att, cer_att, wer_att, hs_dec_asr
  
imports: import logging
  import random
  from contextlib import contextmanager
  from itertools import groupby
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  from packaging.version import parse
  from torch.nn.utils.rnn import pad_sequence
  from typeguard import check_argument_types
  from espnet2.asr.ctc import CTC
  from espnet2.asr.decoder.abs_decoder import AbsDecoder
  from espnet2.asr.encoder.abs_encoder import AbsEncoder
  from espnet2.asr.frontend.abs_frontend import AbsFrontend
  from espnet2.asr.postencoder.abs_postencoder import AbsPostEncoder
  from espnet2.asr.preencoder.abs_preencoder import AbsPreEncoder
  from espnet2.asr.specaug.abs_specaug import AbsSpecAug
  from espnet2.asr_transducer.utils import get_transducer_task_io
  from espnet2.layers.abs_normalize import AbsNormalize
  from espnet2.torch_utils.device_funcs import force_gatherable
  from espnet2.train.abs_espnet_model import AbsESPnetModel
  from espnet.nets.e2e_asr_common import ErrorCalculator
  from espnet.nets.e2e_mt_common import ErrorCalculator
  from espnet.nets.pytorch_backend.nets_utils import th_accuracy
  from espnet.nets.pytorch_backend.transformer.add_sos_eos import add_sos_eos
  from espnet.nets.pytorch_backend.transformer.label_smoothing_loss import LabelSmoothingLoss
label_desc: Use image node_id 7 for calling the ESPnetSTModel obj's underlying member method code with example usage: obj._calc_asr_att_loss(encoder_out, encoder_out_lens, ys_pad, ys_pad_lens, return_hs) and returns: loss_att,  acc_att,  cer_att,  wer_att,  hs_dec_asr, int,  None,  None,  None,  hs_dec_asr