code: def merge_and_save(
      model: peft.LoraModel,
      model_src: str,
      model_dst: str,
      reinit: bool = False,
      cpu_offload: bool = False,
      actually_save: bool = True,
  ):
      modules = find_lora_modules(model)
  
      os.makedirs(model_dst, exist_ok=True)
      shard_paths = sharded_paths(model_src, modules.keys())
      out_shard_paths = {}
  
      unique_shards = list(set(shard_paths.values()))
      for shard_path in unique_shards:
          out_tensors = {}
          if shard_path.endswith(".safetensors"):
              in_tensors = st.load_file(
                  str(Path(model_src) / shard_path)
              )
          else:
              in_tensors = torch.load(Path(model_src) / shard_path)
              if "state_dict" in in_tensors:
                  in_tensors = in_tensors["state_dict"]
  
          LOG.info(f"load from {model_src}, {shard_path}")
  
          for module_name, target in modules.items():
              key = module_name + ".weight"
              if (
                  key not in shard_paths
                  or shard_paths[key] != shard_path
              ):
                  continue
  
              orig_weight = in_tensors[key].float()
              old_dev = target.weight.data.device
              math_dev = "cpu" if cpu_offload else old_dev
  
              delta_weight = lora_delta_weight(target, math_dev).float()
              new_weight = orig_weight.to(math_dev) + delta_weight
              del delta_weight
  
              if actually_save:
                  out_tensors[key] = new_weight.half().cpu()
  
              update_weights(
                  target, new_weight, reinit=reinit, device=old_dev
              )
  
          if actually_save:
              out_shard_name = shard_path
              if out_shard_name.startswith("pytorch_model"):
                  out_shard_name = (
                      out_shard_name.replace(
                          "pytorch_model", "model"
                      ).rstrip(".bin")
                      + ".safetensors"
                  )
  
              for module_name in in_tensors:
                  if module_name not in out_tensors:
                      out_tensors[module_name] = in_tensors[
                          module_name
                      ].half()
                  out_shard_paths[module_name] = out_shard_name
  
              shard_fn = str(Path(model_dst) / out_shard_name)
              LOG.info(f"saving tensors to {shard_fn}")
              st.save_file(
                  out_tensors, shard_fn, metadata={"format": "pt"}
              )
  
          del in_tensors
          del out_tensors
          torch.xpu.empty_cache()
  
      if actually_save and len(unique_shards) > 1:
          with open(
              str(Path(model_dst, "model.safetensors.index.json")),
              "w",
              encoding="utf-8",
          ) as file:
              json.dump(
                  {"metadata": {}, "weight_map": out_shard_paths}, file
              )
  
imports: from typing import Optional
  import torch
  from transformers.trainer import Trainer
  import glob
  import json
  import logging
  import os.path
  import shutil
  from pathlib import Path
  from typing import Dict, List, Sequence
  import peft
  import safetensors.torch
  import torch
  from huggingface_hub import snapshot_download
  from torch.optim.lr_scheduler import LRScheduler
  from torch.optim.optimizer import Optimizer
  from transformers import TrainerCallback, TrainerControl, TrainerState, TrainingArguments
  from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR
  import torch.distributed
  from bigdl.llm.transformers.qlora import LoraLowBitLinear
  from bigdl.llm.transformers.low_bit_linear import FP4Params
  from bigdl.llm.utils.common import invalidInputError
label_desc: Use image node_id 8 for calling a global function with example usage: merge_and_save(model, model_src, model_dst, reinit, cpu_offload, actually_save) without return types