class_docstr: Base class for encoder decoder CTC-based models.
class_docstr_tok: ["Base","class","for","encoder","decoder","CTC-based","models","."]
code: def __init__(self, cfg: DictConfig, trainer: Trainer = None):
      if "tokenizer" not in cfg:
          raise ValueError(
              "`cfg` must have `tokenizer` config to create a tokenizer !"
          )
  
      # Setup the tokenizer
      self._setup_tokenizer(cfg.tokenizer)
  
      super().__init__(cfg=cfg, trainer=trainer)
  
      # Setup audio preprocessor
      self.preprocessor = EncDecTransfModelBPE.from_config_dict(
          self.cfg.preprocessor
      )
  
      # Setup audio encoder
      self.encoder = EncDecTransfModelBPE.from_config_dict(
          self.cfg.encoder
      )
  
      # Add projection layer if encoder and decoder differ in hidden size
      if (
          self.cfg.encoder["d_model"]
          != self.cfg.transf_decoder["hidden_size"]
      ):
          self.adapter = torch.nn.Linear(
              self.cfg.encoder["d_model"],
              self.cfg.transf_decoder["hidden_size"],
          )
      else:
          self.adapter = torch.nn.Identity()
  
      transf_encoder_cfg_dict = OmegaConf.to_container(
          cfg.get("transf_encoder")
      )
  
      # Whether to add Transformer Encoder block between Conformer and Transformer Decoder
      self.use_transf_encoder = False
      if transf_encoder_cfg_dict["num_layers"] > 0:
          self.use_transf_encoder = True
  
          self.transf_encoder = TransformerEncoder(
              num_layers=transf_encoder_cfg_dict["num_layers"],
              hidden_size=transf_encoder_cfg_dict["hidden_size"],
              inner_size=transf_encoder_cfg_dict["inner_size"],
              mask_future=False,
              num_attention_heads=transf_encoder_cfg_dict[
                  "num_attention_heads"
              ],
              attn_score_dropout=transf_encoder_cfg_dict[
                  "attn_score_dropout"
              ],
              attn_layer_dropout=transf_encoder_cfg_dict[
                  "attn_layer_dropout"
              ],
              ffn_dropout=transf_encoder_cfg_dict["ffn_dropout"],
              pre_ln=transf_encoder_cfg_dict.get("pre_ln", True),
              pre_ln_final_layer_norm=transf_encoder_cfg_dict.get(
                  "pre_ln_final_layer_norm", True
              ),
          )
          std_init_range = (
              1 / transf_encoder_cfg_dict["hidden_size"] ** 0.5
          )
          self.transf_encoder.apply(
              lambda module: transformer_weights_init(
                  module, std_init_range
              )
          )
  
      transf_decoder_cfg_dict = OmegaConf.to_container(
          cfg.get("transf_decoder")
      )
  
      # Transformer decoder
      vocab_size = 8 * ceil(self.tokenizer.vocab_size / 8)
      transf_decoder_cfg_dict["vocab_size"] = vocab_size
      library = transf_decoder_cfg_dict.pop("library", "nemo")
      model_name = transf_decoder_cfg_dict.pop("model_name", None)
      pretrained = transf_decoder_cfg_dict.pop("pretrained", False)
      self.transf_decoder = get_transformer(
          library=library,
          model_name=model_name,
          pretrained=pretrained,
          config_dict=transf_decoder_cfg_dict,
          encoder=False,
          pre_ln_final_layer_norm=transf_decoder_cfg_dict.get(
              "pre_ln_final_layer_norm", False
          ),
      )
  
      self.log_softmax = TokenClassifier(
          hidden_size=self.transf_decoder.hidden_size,
          num_classes=vocab_size,
          activation=self.cfg.head.activation,
          log_softmax=self.cfg.head.log_softmax,
          dropout=self.cfg.head.dropout,
          use_transformer_init=self.cfg.head.use_transformer_init,
      )
      self.log_softmax.mlp.layer0.weight = (
          self.transf_decoder.embedding.token_embedding.weight
      )
      std_init_range = 1 / self.transf_decoder.hidden_size**0.5
      self.transf_decoder.apply(
          lambda module: transformer_weights_init(
              module, std_init_range
          )
      )
      self.log_softmax.apply(
          lambda module: transformer_weights_init(
              module, std_init_range
          )
      )
  
      # Beam Search decoding
      self.beam_search = BeamSearchSequenceGenerator(
          embedding=self.transf_decoder.embedding,
          decoder=self.transf_decoder.decoder,
          log_softmax=self.log_softmax,
          max_sequence_length=self.transf_decoder.max_sequence_length,
          beam_size=self.cfg.beam_search.beam_size,
          bos=self.tokenizer.bos_id,
          pad=self.tokenizer.pad_id,
          eos=self.tokenizer.eos_id,
          len_pen=self.cfg.beam_search.len_pen,
          max_delta_length=self.cfg.beam_search.max_generation_delta,
      )
  
      # Define autoregressive CE loss
      self.transf_loss = SmoothedCrossEntropyLoss(
          pad_id=self.tokenizer.pad_id,
          label_smoothing=self.cfg.label_smoothing,
      )
  
      if (
          hasattr(self.cfg, "spec_augment")
          and self.cfg.spec_augment is not None
      ):
          self.spec_augmentation = (
              EncDecTransfModelBPE.from_config_dict(
                  self.cfg.spec_augment
              )
          )
      else:
          self.spec_augmentation = None
  
      self.val_loss = GlobalAverageLossMetric(
          dist_sync_on_step=False, take_avg_loss=True
      )
  
imports: import itertools
  import json
  import os
  import tempfile
  from math import ceil
  from typing import Dict, List, Optional, Union
  import editdistance
  import torch
  import torch.distributed
  from omegaconf import DictConfig, OmegaConf
  from pytorch_lightning import Trainer
  from tqdm.auto import tqdm
  from nemo.collections.asr.data import audio_to_text_dataset
  from nemo.collections.asr.data.audio_to_text_dali import DALIOutputs
  from nemo.collections.asr.data.audio_to_text_lhotse import LhotseSpeechToTextBpeDataset
  from nemo.collections.asr.models.asr_model import ASRModel, ExportableEncDecModel
  from nemo.collections.asr.parts.mixins import ASRBPEMixin
  from nemo.collections.asr.parts.utils.audio_utils import ChannelSelectorType
  from nemo.collections.common.data.lhotse import get_lhotse_dataloader_from_config
  from nemo.collections.common.losses import SmoothedCrossEntropyLoss
  from nemo.collections.common.metrics import GlobalAverageLossMetric
  from nemo.collections.common.parts import transformer_weights_init
  from nemo.core.classes.common import typecheck
  from nemo.core.neural_types import AudioSignal, ChannelType, LabelsType, LengthsType, LogprobsType, MaskType, NeuralType, SpectrogramType
  from nemo.utils import logging
label_desc: Use image node_id 1 to create a new EncDecTransfModelBPE object from inherited base classes: ASRModel, ExportableEncDecModel, ASRBPEMixin with example: obj = EncDecTransfModelBPE(cfg, trainer)