code: def package_into_nemo_file(
      state_dict_path,
      base_yaml_config,
      hf_model_config,
      nemo_file_path,
      hf_model_name,
      megatron_amp_O2,
  ):
      """
      Packages the state dict, config file and tokenizer into a `.nemo` file.
      """
      trainer = Trainer(
          devices=1,
          strategy=NLPDDPStrategy(),
          accelerator="cpu",
          precision=32,
      )
      base_cfg = OmegaConf.load(base_yaml_config)
      if hf_model_config.dense_act_fn == "silu":
          act_fn = "swiglu"
      elif hf_model_config.dense_act_fn == "gelu_new":
          act_fn = "geglu"
      # FLAN-T5 models have things configured this way.
      elif (
          hf_model_config.dense_act_fn == "gelu"
          and hf_model_config.is_gated_act
      ):
          act_fn = "geglu"
      else:
          raise ValueError(
              f"Unknown dense_act_fn: {hf_model_config.dense_act_fn}"
          )
  
      with open_dict(base_cfg):
          base_cfg.encoder.num_layers = hf_model_config.num_layers
          base_cfg.encoder.hidden_size = hf_model_config.d_model
          base_cfg.encoder.ffn_hidden_size = hf_model_config.d_ff
          base_cfg.encoder.kv_channels = hf_model_config.d_kv
          base_cfg.encoder.num_attention_heads = (
              hf_model_config.num_heads
          )
          base_cfg.encoder.activation = act_fn
          base_cfg.encoder.relative_attention_num_buckets = (
              hf_model_config.relative_attention_num_buckets
          )
  
          base_cfg.decoder.num_layers = (
              hf_model_config.num_decoder_layers
          )
          base_cfg.decoder.hidden_size = hf_model_config.d_model
          base_cfg.decoder.ffn_hidden_size = hf_model_config.d_ff
          base_cfg.decoder.kv_channels = hf_model_config.d_kv
          base_cfg.decoder.num_attention_heads = (
              hf_model_config.num_heads
          )
          base_cfg.decoder.activation = act_fn
          base_cfg.decoder.relative_attention_num_buckets = (
              hf_model_config.relative_attention_num_buckets
          )
  
          base_cfg.megatron_amp_O2 = megatron_amp_O2
  
      with tempfile.TemporaryDirectory() as tmp:
          tokenizer = AutoTokenizer.from_pretrained(hf_model_name)
          tokenizer_path = tokenizer.save_vocabulary(tmp)[0]
          base_cfg.tokenizer.model = tokenizer_path
          model = MegatronT5Model(base_cfg, trainer).to("cpu")
          model._save_restore_connector = NLPSaveRestoreConnector()
          state_dict = torch.load(state_dict_path)
          if megatron_amp_O2:
              new_state_dict = {}
              for key in state_dict.keys():
                  new_key = key.replace("model.", "model.module.", 1)
                  new_state_dict[new_key] = state_dict[key]
              state_dict = new_state_dict
          model.load_state_dict(state_dict)
          model.save_to(nemo_file_path)
  
file_path: NeMo/scripts/nlp_language_modeling/hf_t5-v1_1_to_nemo.py
imports: import collections
  import os
  import tempfile
  from argparse import ArgumentParser
  import torch
  from omegaconf.omegaconf import OmegaConf, open_dict
  from pytorch_lightning import Trainer
  from transformers import AutoTokenizer, T5ForConditionalGeneration
  from nemo.collections.nlp.models.language_modeling.megatron_t5_model import MegatronT5Model
  from nemo.collections.nlp.parts.nlp_overrides import NLPDDPStrategy, NLPSaveRestoreConnector
label_desc: Use image node_id 2 for calling a global function with example usage: package_into_nemo_file(state_dict_path, base_yaml_config, hf_model_config, nemo_file_path, hf_model_name, megatron_amp_O2) without return types