class_docstr: Adds utilities for computing interCTC loss from https://arxiv.org/abs/2102.03216.
  
  To use, make sure encoder accesses ``interctc['capture_layers']``
  property in the AccessMixin and registers ``interctc/layer_output_X`` and
  ``interctc/layer_length_X`` for all layers that we want to get loss from.
  Additionally, specify the following config parameters to set up loss::
  
      interctc:
          # can use different values
          loss_weights: [0.3]
          apply_at_layers: [8]
  
  Then call
  
      * ``self.setup_interctc(ctc_decoder_name, ctc_loss_name, ctc_wer_name)``
        in the init method
      * ``self.add_interctc_losses`` after computing regular loss.
      * ``self.finalize_interctc_metrics(metrics, outputs, prefix="val_")``
        in the `multi_validation_epoch_end` method.
      * ``self.finalize_interctc_metrics(metrics, outputs, prefix="test_")``
        in the `multi_test_epoch_end` method.
class_docstr_tok: ["Adds","utilities","for","computing","interCTC","loss","from","https",":","\/\/arxiv.org\/abs\/2102.03216",".","To","use",",","make","sure","encoder","accesses","``","interctc","[","'capture_layers","'","]","``","property","in","the","AccessMixin","and","registers","``","interctc\/layer_output_X","``","and","``","interctc\/layer_length_X","``","for","all","layers","that","we","want","to","get","loss","from",".","Additionally",",","specify","the","following","config","parameters","to","set","up","loss",":",":","interctc",":","#","can","use","different","values","loss_weights",":","[","0.3","]","apply_at_layers",":","[","8","]","Then","call","*","``","self.setup_interctc","(","ctc_decoder_name",",","ctc_loss_name",",","ctc_wer_name",")","``","in","the","init","method","*","``","self.add_interctc_losses","``","after","computing","regular","loss",".","*","``","self.finalize_interctc_metrics","(","metrics",",","outputs",",","prefix=","''","val_","''",")","``","in","the","`","multi_validation_epoch_end","`","method",".","*","``","self.finalize_interctc_metrics","(","metrics",",","outputs",",","prefix=","''","test_","''",")","``","in","the","`","multi_test_epoch_end","`","method","."]
docstr: Adding interCTC losses if required.
  
  Will also register loss/wer metrics in the returned dictionary.
  
  Args:
      loss_value (torch.Tensor): regular loss tensor (will add interCTC loss to it).
      transcript (torch.Tensor): current utterance transcript.
      transcript_len (torch.Tensor): current utterance transcript length.
      compute_wer (bool): whether to compute WER for the current utterance.
          Should typically be True for validation/test and only True for
          training if current batch WER should be logged.
      compute_loss (bool): whether to compute loss for the current utterance.
          Should always be True in training and almost always True in
          validation, unless all other losses are disabled as well.
          Defaults to True.
      log_wer_num_denom (bool): if True, will additionally log WER num/denom
          in the returned metrics dictionary. Should always be True for
          validation/test to allow correct metrics aggregation. Should
          always be False for training. Defaults to False.
      log_prefix (str): prefix added to all log values. Should be ``""`` for
          training and ``"val_"`` for validation. Defaults to "".
  
  Returns:
      tuple[Optional[torch.Tensor], Dict]: tuple of new loss tensor and dictionary with logged metrics.
docstr_tok: ["Adding","interCTC","losses","if","required",".","Will","also","register","loss\/wer","metrics","in","the","returned","dictionary",".","Args",":","loss_value","(","torch.Tensor",")",":","regular","loss","tensor","(","will","add","interCTC","loss","to","it",")",".","transcript","(","torch.Tensor",")",":","current","utterance","transcript",".","transcript_len","(","torch.Tensor",")",":","current","utterance","transcript","length",".","compute_wer","(","bool",")",":","whether","to","compute","WER","for","the","current","utterance",".","Should","typically","be","True","for","validation\/test","and","only","True","for","training","if","current","batch","WER","should","be","logged",".","compute_loss","(","bool",")",":","whether","to","compute","loss","for","the","current","utterance",".","Should","always","be","True","in","training","and","almost","always","True","in","validation",",","unless","all","other","losses","are","disabled","as","well",".","Defaults","to","True",".","log_wer_num_denom","(","bool",")",":","if","True",",","will","additionally","log","WER","num\/denom","in","the","returned","metrics","dictionary",".","Should","always","be","True","for","validation\/test","to","allow","correct","metrics","aggregation",".","Should","always","be","False","for","training",".","Defaults","to","False",".","log_prefix","(","str",")",":","prefix","added","to","all","log","values",".","Should","be","``","``","''","``","for","training","and","``","``","val_","''","``","for","validation",".","Defaults","to","``","''",".","Returns",":","tuple","[","Optional","[","torch.Tensor","]",",","Dict","]",":","tuple","of","new","loss","tensor","and","dictionary","with","logged","metrics","."]
code: def add_interctc_losses(
      self,
      loss_value: torch.Tensor,
      transcript: torch.Tensor,
      transcript_len: torch.Tensor,
      compute_wer: bool,
      compute_loss: bool = True,
      log_wer_num_denom: bool = False,
      log_prefix: str = "",
  ) -> Tuple[Optional[torch.Tensor], Dict]:
      """Adding interCTC losses if required.
  
      Will also register loss/wer metrics in the returned dictionary.
  
      Args:
          loss_value (torch.Tensor): regular loss tensor (will add interCTC loss to it).
          transcript (torch.Tensor): current utterance transcript.
          transcript_len (torch.Tensor): current utterance transcript length.
          compute_wer (bool): whether to compute WER for the current utterance.
              Should typically be True for validation/test and only True for
              training if current batch WER should be logged.
          compute_loss (bool): whether to compute loss for the current utterance.
              Should always be True in training and almost always True in
              validation, unless all other losses are disabled as well.
              Defaults to True.
          log_wer_num_denom (bool): if True, will additionally log WER num/denom
              in the returned metrics dictionary. Should always be True for
              validation/test to allow correct metrics aggregation. Should
              always be False for training. Defaults to False.
          log_prefix (str): prefix added to all log values. Should be ``""`` for
              training and ``"val_"`` for validation. Defaults to "".
  
      Returns:
          tuple[Optional[torch.Tensor], Dict]: tuple of new loss tensor and dictionary with logged metrics.
      """
      if (
          not self.is_interctc_enabled()
          or not AccessMixin.is_access_enabled()
      ):
          return loss_value, {}
      metrics = {}
      if compute_loss:
          metrics[f"{log_prefix}final_loss"] = loss_value
      else:
          loss_value = None
      captured_tensors = self.get_captured_interctc_tensors()
  
      if compute_loss:
          loss_value *= self.get_interctc_param("main_loss_weight")
  
      for layer_idx, intermediate_result, loss_weight in zip(
          self.get_interctc_param("apply_at_layers"),
          captured_tensors,
          self.get_interctc_param("intermediate_loss_weights"),
      ):
          if compute_loss:
              inter_loss_value = self.get_interctc_param("loss")(
                  log_probs=intermediate_result[0],
                  targets=transcript,
                  target_lengths=transcript_len,
                  input_lengths=intermediate_result[1],
              )
              metrics[
                  f"{log_prefix}inter_ctc_loss_l{layer_idx}"
              ] = inter_loss_value.detach()
              loss_value += inter_loss_value * loss_weight
          if compute_wer:
              self.get_interctc_param("wer").update(
                  predictions=intermediate_result[0],
                  targets=transcript,
                  targets_lengths=transcript_len,
                  predictions_lengths=intermediate_result[1],
              )
              wer, wer_num, wer_denom = self.get_interctc_param(
                  "wer"
              ).compute()
              self.get_interctc_param("wer").reset()
              metrics.update(
                  {f"{log_prefix}inter_wer_l{layer_idx}": wer}
              )
              if log_wer_num_denom:
                  metrics.update(
                      {
                          f"{log_prefix}inter_wer_num_l{layer_idx}": wer_num,
                          f"{log_prefix}inter_wer_denom_l{layer_idx}": wer_denom,
                      }
                  )
  
      # return total loss and dictionary of metrics
      return loss_value, metrics
  
file_path: NeMo/nemo/collections/asr/parts/mixins/interctc_mixin.py
imports: from typing import Dict, List, Optional, Tuple
  import torch
  from nemo.core.classes.mixins import AccessMixin
label_desc: Use image node_id 10 for calling the InterCTCMixin obj's underlying member method code with example usage: obj.add_interctc_losses(loss_value, transcript, transcript_len, compute_wer, compute_loss, log_wer_num_denom, log_prefix) and returns: loss_value,  metrics, loss_value,  dict