checkpoint_converter_fsdp_hf.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. # from accelerate import init_empty_weights, load_checkpoint_and_dispatch
  4. import fire
  5. import torch
  6. import os
  7. import sys
  8. from typing import List
  9. from transformers import LlamaTokenizer
  10. from safety_utils import get_safety_checker
  11. from model_utils import load_model, load_peft_model, load_llama_from_config
  12. from accelerate import init_empty_weights
  13. # Get the current file's directory
  14. current_directory = os.path.dirname(os.path.abspath(__file__))
  15. # Get the parent directory
  16. parent_directory = os.path.dirname(current_directory)
  17. # Append the parent directory to sys.path
  18. sys.path.append(parent_directory)
  19. from model_checkpointing import load_sharded_model_single_gpu
  20. def main(
  21. model_name,
  22. save_dir="", # Path to save the HF converted model checkpoints
  23. model_path="" # Path/ name of the HF model that include config.json and tokenizer_config.json
  24. ):
  25. #load the HF model definition from config
  26. model_def = load_llama_from_config(model_path)
  27. print("model is loaded from config")
  28. #load the FSDP sharded checkpoints into the model
  29. model = load_sharded_model_single_gpu(model_def, model_name)
  30. print("model is loaded from FSDP checkpoints")
  31. #loading the tokenizer form the model_path
  32. tokenizer = LlamaTokenizer.from_pretrained(model_path)
  33. tokenizer.save_pretrained(save_dir)
  34. #save the FSDP sharded checkpoints in HF format
  35. model.save_pretrained(save_dir)
  36. print(f"HuggingFace model checkpoints has been saved in {save_dir}")
  37. if __name__ == "__main__":
  38. fire.Fire(main)