checkpoint_converter_fsdp_hf.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 transformers import LlamaTokenizer
  9. from model_utils import load_llama_from_config
  10. # Get the current file's directory
  11. current_directory = os.path.dirname(os.path.abspath(__file__))
  12. # Get the parent directory
  13. parent_directory = os.path.dirname(current_directory)
  14. # Append the parent directory to sys.path
  15. sys.path.append(parent_directory)
  16. from model_checkpointing import load_sharded_model_single_gpu
  17. def main(
  18. fsdp_checkpoint_path="", # Path to FSDP Sharded model checkpoints
  19. consolidated_model_path="", # Path to save the HF converted model checkpoints
  20. HF_model_path="" # Path/ name of the HF model that include config.json and tokenizer_config.json (e.g. meta-llama/Llama-2-7b-chat-hf)
  21. ):
  22. #load the HF model definition from config
  23. model_def = load_llama_from_config(HF_model_path)
  24. print("model is loaded from config")
  25. #load the FSDP sharded checkpoints into the model
  26. model = load_sharded_model_single_gpu(model_def, fsdp_checkpoint_path)
  27. print("model is loaded from FSDP checkpoints")
  28. #loading the tokenizer form the model_path
  29. tokenizer = LlamaTokenizer.from_pretrained(HF_model_path)
  30. tokenizer.save_pretrained(consolidated_model_path)
  31. #save the FSDP sharded checkpoints in HF format
  32. model.save_pretrained(consolidated_model_path)
  33. print(f"HuggingFace model checkpoints has been saved in {consolidated_model_path}")
  34. if __name__ == "__main__":
  35. fire.Fire(main)