code: def unsloth_save_model(
      model,
      tokenizer,
      save_directory: Union[str, os.PathLike],
      is_main_process: bool = True,
      state_dict: Optional[dict] = None,
      save_function: Callable = torch.save,
      push_to_hub: bool = False,
      max_shard_size: Union[int, str] = "7GB",
      safe_serialization: bool = True,
      variant: Optional[str] = None,
      token: Optional[Union[str, bool]] = None,
      save_peft_format: bool = True,
      temporary_location="_unsloth_temporary_saved_buffers",
      **kwargs,
  ):
      logger.warning_once(
          "Unsloth: `unsloth_save_model` is still in development mode.\n"
          "If anything errors or breaks, please file a ticket on Github.\n"
          "Also, if you used this successfully, please tell us on Discord!"
      )
  
      if not os.path.exists(temporary_location):
          os.makedirs(temporary_location)
      pass
  
      assert hasattr(model, "model")
      assert hasattr(model.model, "model")
      assert hasattr(model.model.model, "layers")
  
      # HF also uses a OrderedDict
      state_dict = OrderedDict()
      state_dict[
          "model.embed_tokens.weight"
      ] = model.model.model.embed_tokens.weight
  
      print("Unsloth: Merging 4bit and LoRA weights to 16bit...")
      for j, layer in enumerate(ProgressBar(model.model.model.layers)):
          for item in LLAMA_WEIGHTS:
              proj = eval(f"layer.{item}")
              name = f"model.layers.{j}.{item}.weight"
              W = _merge_lora(proj, name)
              filename = os.path.join(temporary_location, f"{name}.pt")
              torch.save(W, filename)
              state_dict[name] = torch.load(
                  filename, map_location="cpu", mmap=True
              )
          pass
          for item in LLAMA_LAYERNORMS:
              state_dict[f"model.layers.{j}.{item}.weight"] = eval(
                  f"layer.{item}.weight"
              )
          pass
      pass
  
      state_dict["model.norm.weight"] = model.model.model.norm.weight
      state_dict["lm_head.weight"] = model.model.lm_head.weight
  
      print("Unsloth: Saving tokenizer...")
      tokenizer.save_pretrained(
          save_directory=save_directory,
          is_main_process=is_main_process,
          state_dict=state_dict,
          save_function=save_function,
          push_to_hub=push_to_hub,
          max_shard_size=max_shard_size,
          safe_serialization=safe_serialization,
          variant=variant,
          token=token,
          save_peft_format=save_peft_format,
      )
  
      print(
          "Unsloth: Saving model. This will take 5 minutes for Llama-7b..."
      )
      model.model.save_pretrained(
          save_directory=save_directory,
          is_main_process=is_main_process,
          state_dict=state_dict,
          save_function=save_function,
          push_to_hub=push_to_hub,
          max_shard_size=max_shard_size,
          safe_serialization=safe_serialization,
          variant=variant,
          token=token,
          save_peft_format=save_peft_format,
      )
  
      # Remove temporary location
      shutil.rmtree(temporary_location)
  
file_path: unsloth/unsloth/save.py
imports: from peft import PeftModelForCausalLM
  from collections import OrderedDict
  import bitsandbytes
  import peft
  import gc
  import os
  from tqdm import tqdm
  import shutil
  from typing import Optional, Callable, Union
  import torch
  from transformers.models.llama.modeling_llama import logger
  from .kernels import fast_dequantize, QUANT_STATE, get_lora_parameters
label_desc: Use image node_id 2 for calling a global function with example usage: unsloth_save_model(model, tokenizer, save_directory, is_main_process, state_dict, save_function, push_to_hub, max_shard_size, safe_serialization, variant, token, save_peft_format, temporary_location) without return types