code: def create_nlp_backbone(cfg, model_class=AutoModel) -> Any:
      """
      Creates a backbone model for NLP tasks.
      This is needed for Gradient Checkpointing in DDP mode.
      """
      kwargs = dict()
      try:
          config = AutoConfig.from_pretrained(
              cfg.llm_backbone,
              trust_remote_code=cfg.environment.trust_remote_code,
              use_auth_token=os.getenv("HUGGINGFACE_TOKEN"),
              revision=cfg.environment.huggingface_branch,
          )
          kwargs["use_auth_token"] = os.getenv("HUGGINGFACE_TOKEN")
      except TypeError:
          # TypeError: RWForCausalLM.__init__() got
          # an unexpected keyword argument 'use_auth_token'
          config = AutoConfig.from_pretrained(
              cfg.llm_backbone,
              trust_remote_code=cfg.environment.trust_remote_code,
              revision=cfg.environment.huggingface_branch,
          )
  
      config = update_backbone_config(config, cfg)
  
      quantization_config = None
      if cfg.architecture.backbone_dtype == "int8":
          kwargs["device_map"] = {"": cfg.environment._device}  # type: ignore
          quantization_config = BitsAndBytesConfig(
              load_in_8bit=True,
              llm_int8_threshold=0.0,
          )
          # need to force pretrained
          cfg.architecture.pretrained = True
          kwargs["torch_dtype"] = torch.float16  # type: ignore
      elif cfg.architecture.backbone_dtype == "int4":
          kwargs["device_map"] = {"": cfg.environment._device}  # type: ignore
          quantization_config = BitsAndBytesConfig(
              load_in_4bit=True,
              bnb_4bit_compute_dtype=torch.float16,
              bnb_4bit_quant_type="nf4",
          )
          # need to force pretrained
          cfg.architecture.pretrained = True
          kwargs["torch_dtype"] = torch.float16  # type: ignore
      else:
          kwargs["torch_dtype"] = getattr(
              torch, cfg.architecture.backbone_dtype
          )
  
      logger.info(
          f"Using {cfg.architecture.backbone_dtype} for backbone"
      )
  
      kwargs["trust_remote_code"] = cfg.environment.trust_remote_code
  
      if cfg.training.use_flash_attention_2:
          try:
              import flash_attn  # noqa: F401
  
              # see https://github.com/fxmarty/transformers/
              # blob/3f06a3a0aec8cc1ec3ad6bf66ebe277392c5ab37/
              # src/transformers/configuration_utils.py#L380
              config._attn_implementation_internal = "flash_attention_2"
              if cfg.environment._local_rank == 0:
                  logger.info("Using Flash Attention 2.")
          except ImportError:
              if cfg.environment._local_rank == 0:
                  logger.warning(
                      "Flash Attention 2.0 is not available. "
                      "Please consider to run 'make setup' to install it."
                  )
  
      if cfg.architecture.pretrained:
          if cfg.environment._local_rank == 0:
              logger.info(
                  f"Loading {cfg.llm_backbone}. This may take a while."
              )
  
          backbone = model_class.from_pretrained(
              cfg.llm_backbone,
              revision=cfg.environment.huggingface_branch,
              config=config,
              quantization_config=quantization_config,
              **kwargs,
          )
          if cfg.environment._local_rank == 0:
              logger.info(f"Loaded {cfg.llm_backbone}.")
      else:
          kwargs.pop("use_auth_token", None)
          backbone = model_class.from_config(config, **kwargs)
  
      if cfg.tokenizer._vocab_length > config.vocab_size:
          if cfg.environment._local_rank == 0:
              logger.info(
                  f"Resizing token embeddings to {cfg.tokenizer._vocab_length}"
              )
          backbone.resize_token_embeddings(cfg.tokenizer._vocab_length)
  
      backbone.model_parallel = False
  
      if cfg.training.lora:
          # if used, gradient checkpointing will be enabled below
          loaded_in_kbit = getattr(
              backbone, "is_loaded_in_8bit", False
          ) or getattr(backbone, "is_loaded_in_4bit", False)
  
          for name, param in backbone.named_parameters():
              # freeze base model's layers
              param.requires_grad = False
  
          # cast all non INT8 parameters to fp32
          if loaded_in_kbit:
              for param in backbone.parameters():
                  if (param.dtype == torch.float16) or (
                      param.dtype == torch.bfloat16
                  ):
                      param.data = param.data.to(torch.float32)
      else:
          if cfg.architecture.backbone_dtype != "float32":
              if cfg.environment.mixed_precision:
                  logger.info(
                      "Disabling mixed precision as dtype not set to float32."
                  )
                  cfg.environment.mixed_precision = False
              if cfg.architecture.backbone_dtype != "bfloat16":
                  logger.warning(
                      "Pure float16 or int8 training will "
                      "likely lead to unstable training without adapters."
                  )
  
      if cfg.architecture.gradient_checkpointing:
          backbone.gradient_checkpointing_enable()
  
      if backbone.generation_config.eos_token_id != config.eos_token_id:
          logger.warning(
              "EOS token id not matching between generation config and tokenizer. "
              "Overwriting with tokenizer id."
          )
          backbone.generation_config.eos_token_id = config.eos_token_id
      if backbone.generation_config.pad_token_id != config.pad_token_id:
          logger.warning(
              "PAD token id not matching between generation config and tokenizer. "
              "Overwriting with tokenizer id."
          )
          backbone.generation_config.pad_token_id = config.pad_token_id
      # no warning needed as not used
      if backbone.generation_config.bos_token_id != config.bos_token_id:
          backbone.generation_config.bos_token_id = config.bos_token_id
  
      return backbone, config
  
imports: import gc
  import logging
  import os
  import re
  import shutil
  from collections import OrderedDict
  from typing import Any, Dict
  import coolname
  import deepspeed
  import numpy
  import torch
  from deepspeed.runtime.dataloader import DeepSpeedDataLoader
  from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
  from peft import LoraConfig, PeftModel, get_peft_model
  from torch.cuda.amp import autocast
  from torch.nn.parallel import DistributedDataParallel
  from tqdm import tqdm
  from transformers import AutoConfig, AutoModel, BitsAndBytesConfig, GenerationMixin, StoppingCriteria, StoppingCriteriaList
  from transformers.pytorch_utils import Conv1D
  from transformers.utils import logging
  from llm_studio.src.datasets.text_utils import get_tokenizer
  from llm_studio.src.optimizers import Optimizers
  from llm_studio.src.schedulers import Schedulers
  from llm_studio.src.utils.config_utils import NON_GENERATION_PROBLEM_TYPES
  from llm_studio.src.utils.data_utils import OrderedDistributedSampler, batch_padding, cat_batches, get_inference_batch_size
  from llm_studio.src.utils.exceptions import LLMDataException, LLMModelException
  from llm_studio.src.utils.logging_utils import TqdmToLogger
  from llm_studio.src.utils.utils import save_pickle
label_desc: Use image node_id 17 for calling a global function with example usage: create_nlp_backbone(cfg, model_class) and returns: backbone,  config