checkpoint_converter_fsdp_hf.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 os
  5. import sys
  6. import fire
  7. import yaml
  8. from llama_cookbook.inference.model_utils import load_llama_from_config
  9. from transformers import AutoConfig, AutoTokenizer, MllamaProcessor
  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_or_name="", # 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. try:
  23. file_name = "train_params.yaml"
  24. # Combine the directory and file name to create the full path
  25. train_params_path = os.path.join(fsdp_checkpoint_path, file_name)
  26. # Open the file
  27. with open(train_params_path, "r") as file:
  28. # Load the YAML data
  29. data = yaml.safe_load(file)
  30. # Access the 'model_name' field
  31. HF_model_path_or_name = data.get("model_name")
  32. print(f"Model name: {HF_model_path_or_name}")
  33. except FileNotFoundError:
  34. print(f"The file {train_params_path} does not exist.")
  35. HF_model_path_or_name = input("Please enter the model name: ")
  36. print(f"Model name: {HF_model_path_or_name}")
  37. except Exception as e:
  38. print(f"An error occurred: {e}")
  39. # load the HF model definition from config
  40. model_def = load_llama_from_config(HF_model_path_or_name)
  41. print("model is loaded from config")
  42. # load the FSDP sharded checkpoints into the model
  43. model = load_sharded_model_single_gpu(model_def, fsdp_checkpoint_path)
  44. print("model is loaded from FSDP checkpoints")
  45. # loading the tokenizer form the model_path
  46. config = AutoConfig.from_pretrained(HF_model_path_or_name)
  47. # save the processor and config for mllama models
  48. if config.model_type == "mllama":
  49. processor = MllamaProcessor.from_pretrained(HF_model_path_or_name)
  50. processor.save_pretrained(consolidated_model_path)
  51. print(
  52. f"HuggingFace mllama processor has been saved in {consolidated_model_path}"
  53. )
  54. else:
  55. # save the tokenizer for llama models
  56. tokenizer = AutoTokenizer.from_pretrained(HF_model_path_or_name)
  57. tokenizer.save_pretrained(consolidated_model_path)
  58. print(
  59. f"HuggingFace llama tokenizer has been saved in {consolidated_model_path}"
  60. )
  61. # save the FSDP sharded checkpoints in HF format
  62. model.save_pretrained(consolidated_model_path)
  63. print(f"HuggingFace model checkpoints has been saved in {consolidated_model_path}")
  64. if __name__ == "__main__":
  65. fire.Fire(main)