code: def build_model(model_opt, opt, vocabs, checkpoint, device_id):
      logger.info("Building model...")
  
      model = build_base_model(model_opt, vocabs)
  
      # If new training initialize the model params
      # If update_vocab init also but checkpoint will overwrite old weights
      if checkpoint is None or model_opt.update_vocab:
          if model_opt.param_init != 0.0:
              for param in model.parameters():
                  uniform_(
                      param, -model_opt.param_init, model_opt.param_init
                  )
          elif model_opt.param_init_glorot:
              for name, module in model.named_modules():
                  for param_name, param in module.named_parameters():
                      if param_name == "weight" and param.dim() > 1:
                          xavier_uniform_(param)
                      elif param_name == "bias":
                          zeros_(param)
          else:
              raise ValueError(
                  "You need either param_init != 0 OR init_glorot True"
              )
  
          if hasattr(model, "encoder") and hasattr(
              model.encoder, "embeddings"
          ):
              model.encoder.embeddings.load_pretrained_vectors(
                  model_opt.pre_word_vecs_enc
              )
          if hasattr(model.decoder, "embeddings"):
              model.decoder.embeddings.load_pretrained_vectors(
                  model_opt.pre_word_vecs_dec
              )
  
      # ONLY for legacy fusedam with amp pytorch requires NOT to half the model
      if (
          model_opt.model_dtype == "fp16"
          and model_opt.apex_opt_level not in ["O0", "O1", "O2", "O3"]
          and model_opt.optim == "fusedadam"
      ):
          precision = torch.float16
          logger.info("Switching model to half() for FusedAdam legacy")
          logger.info(
              "Non quantized layer compute is %s", model_opt.model_dtype
          )
      else:
          precision = torch.float32
          logger.info("Switching model to float32 for amp/apex_amp")
          logger.info(
              "Non quantized layer compute is %s", model_opt.model_dtype
          )
  
      if opt.world_size > 1 and opt.parallel_mode == "tensor_parallel":
          device = torch.device("cuda")
          offset = device_id
      else:
          if use_gpu(opt):
              device = torch.device("cuda")
          else:
              device = torch.device("cpu")
          offset = 0
  
      if checkpoint is not None:
          if model_opt.update_vocab:
              if "model" in checkpoint.keys():
                  # Update model embeddings with those from the checkpoint
                  # after initialization
                  use_embeddings_from_checkpoint(
                      vocabs, model, checkpoint
                  )
                  # after this checkpoint contains no embeddings
              else:
                  raise ValueError(
                      "Update Vocab is not compatible with safetensors mode (yet"
                  )
  
          # when using LoRa or updating the vocab (no more embeddings in ckpt)
          # => strict=False when loading state_dict
          strict = not model_opt.update_vocab
  
          if "model" in checkpoint.keys():
              # weights are in the .pt file
              model.load_state_dict(
                  checkpoint,
                  precision=precision,
                  device=device,
                  strict=strict,
                  offset=offset,
              )
          else:
              # weights are not in the .pt checkpoint but stored in the safetensors file
              model_path = (
                  opt.train_from[:-3]
                  if opt.train_from[-3:] == ".pt"
                  else opt.train_from
              )
              model.load_safe_state_dict(
                  model_path,
                  precision=precision,
                  device=device,
                  strict=strict,
                  offset=offset,
              )
      else:
          model.to(precision)
          model.to(device)
  
      if model_opt.freeze_encoder:
          model.encoder.requires_grad_(False)
          model.encoder.embeddings.requires_grad_()
  
      if model_opt.freeze_decoder:
          model.decoder.requires_grad_(False)
          model.decoder.embeddings.requires_grad_()
  
      logger.info(model)
      return model
  
imports: import torch
  import torch.nn
  from torch.nn.utils import skip_init
  from torch.nn.init import xavier_uniform_, zeros_, uniform_
  from onmt.models.model import NMTModel, LanguageModel
  from onmt.encoders import str2enc
  from onmt.decoders import str2dec
  from onmt.inputters.inputter import dict_to_vocabs
  from onmt.modules import Embeddings, CopyGenerator
  from onmt.utils.misc import use_gpu
  from onmt.utils.logging import logger
  from onmt.utils.parse import ArgumentParser
  from onmt.models.model_saver import load_checkpoint
  from onmt.constants import DefaultTokens, ModelTask
  from onmt.modules.lora import replace_lora_linear, replace_lora_embedding, mark_only_lora_as_trainable
label_desc: Use image node_id 11 for calling a global function with example usage: build_model(model_opt, opt, vocabs, checkpoint, device_id) and returns: model