code: def main(cfg):
      logging.info(f"Hydra config: {OmegaConf.to_yaml(cfg)}")
  
      if (
          cfg.model.pretrained_model is None
          and cfg.model.nemo_model is None
      ):
          raise ValueError(
              "Either set `cfg.model.nemo_model` or `cfg.model.pretrained_model`"
          )
      if (
          cfg.model.pretrained_model is not None
          and cfg.model.nemo_model is not None
      ):
          raise ValueError(
              "Cannot set both `cfg.model.nemo_model` and `cfg.model.pretrained_model`. Select one only."
          )
  
      trainer = pl.Trainer(**cfg.trainer)
      exp_log_dir = exp_manager(trainer, cfg.get("exp_manager", None))
  
      if cfg.model.pretrained_model is not None:
          model_cfg = ASRModel.from_pretrained(
              cfg.model.pretrained_model, return_config=True
          )
          update_model_config_to_support_adapter(model_cfg, cfg)
          model = ASRModel.from_pretrained(
              cfg.model.pretrained_model,
              override_config_path=model_cfg,
              trainer=trainer,
          )
  
      else:
          model_cfg = ASRModel.restore_from(
              cfg.model.nemo_model, return_config=True
          )
          update_model_config_to_support_adapter(model_cfg, cfg)
          model = ASRModel.restore_from(
              cfg.model.nemo_model,
              override_config_path=model_cfg,
              trainer=trainer,
          )
  
      # Setup model for finetuning (train and validation only)
      cfg.model.train_ds = update_model_cfg(
          model.cfg.train_ds, cfg.model.train_ds
      )
      model.setup_training_data(cfg.model.train_ds)
  
      if "validation_ds" in cfg.model:
          cfg.model.validation_ds = update_model_cfg(
              model.cfg.validation_ds, cfg.model.validation_ds
          )
          model.setup_multiple_validation_data(cfg.model.validation_ds)
  
      # Setup optimizer
      model.setup_optimization(cfg.model.optim)
  
      # Setup spec augmentation
      if "spec_augment" in cfg.model:
          model.spec_augmentation = model.from_config_dict(
              cfg.model.spec_augment
          )
      else:
          model.spec_augmentation = None
          del model.cfg.spec_augment
  
      # Setup adapters
      with open_dict(cfg.model.adapter):
          # Extract the name of the adapter (must be give for training)
          adapter_name = cfg.model.adapter.pop("adapter_name")
          adapter_type = cfg.model.adapter.pop("adapter_type")
          adapter_module_name = cfg.model.adapter.pop(
              "adapter_module_name", None
          )
          adapter_state_dict_name = cfg.model.adapter.pop(
              "adapter_state_dict_name", None
          )
  
          # Resolve the config of the specified `adapter_type`
          if adapter_type not in cfg.model.adapter.keys():
              raise ValueError(
                  f"Adapter type ({adapter_type}) config could not be found. Adapter setup config - \n"
                  f"{OmegaConf.to_yaml(cfg.model.adapter)}"
              )
  
          adapter_type_cfg = cfg.model.adapter[adapter_type]
          print(
              f"Found `{adapter_type}` config :\n"
              f"{OmegaConf.to_yaml(adapter_type_cfg)}"
          )
  
          # Augment adapter name with module name, if not provided by user
          if (
              adapter_module_name is not None
              and ":" not in adapter_name
          ):
              adapter_name = f"{adapter_module_name}:{adapter_name}"
  
          # Extract the global adapter config, if provided
          adapter_global_cfg = cfg.model.adapter.pop(
              model.adapter_global_cfg_key, None
          )
          if adapter_global_cfg is not None:
              add_global_adapter_cfg(model, adapter_global_cfg)
  
      model.add_adapter(adapter_name, cfg=adapter_type_cfg)
      assert model.is_adapter_available()
  
      # Disable all other adapters, enable just the current adapter.
      model.set_enabled_adapters(
          enabled=False
      )  # disable all adapters prior to training
      model.set_enabled_adapters(
          adapter_name, enabled=True
      )  # enable just one adapter by name
  
      # First, Freeze all the weights of the model (not just encoder, everything)
      model.freeze()
      # Activate dropout() and other modules that depend on train mode.
      model = model.train()
      # Then, Unfreeze just the adapter weights that were enabled above (no part of encoder/decoder/joint/etc)
      model.unfreeze_enabled_adapters()
  
      # Update model config prior to training
      model.cfg = model.cfg
  
      # Finally, train model
      trainer.fit(model)
  
      # Save the adapter state dict
      if adapter_state_dict_name is not None:
          state_path = (
              exp_log_dir if exp_log_dir is not None else os.getcwd()
          )
          ckpt_path = os.path.join(state_path, "checkpoints")
          if os.path.exists(ckpt_path):
              state_path = ckpt_path
          state_path = os.path.join(state_path, adapter_state_dict_name)
  
          # Save the adapter modules in a seperate file
          model.save_adapters(str(state_path))
  
      if "delete_ckpt_after_train" in cfg:
          delete_ckpt_after_train = cfg.delete_ckpt_after_train
          if delete_ckpt_after_train:
              # Remove PTL ckpt file, and potentially also remove .nemo file to conserve storage space.
              clean_exp_ckpt(
                  exp_log_dir, remove_ckpt=True, remove_nemo=False
              )
  
file_path: NeMo/examples/asr/asr_adapters/train_asr_adapter.py
imports: import os
  from dataclasses import is_dataclass
  import pytorch_lightning
  from omegaconf import DictConfig, OmegaConf, open_dict
  from nemo.collections.asr.models import ASRModel
  from nemo.core import adapter_mixins
  from nemo.core.config import hydra_runner
  from nemo.utils import logging
  from nemo.utils.exp_manager import clean_exp_ckpt, exp_manager
label_desc: Use image node_id 4 for calling a global function with example usage: main(cfg) without return types