finetuning.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. import os
  4. from pkg_resources import packaging
  5. import fire
  6. import random
  7. import torch
  8. import torch.optim as optim
  9. from peft import get_peft_model, prepare_model_for_int8_training
  10. from torch.distributed.fsdp import (
  11. FullyShardedDataParallel as FSDP,
  12. )
  13. from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload
  14. from torch.optim.lr_scheduler import StepLR
  15. from transformers import (
  16. LlamaForCausalLM,
  17. LlamaTokenizer,
  18. LlamaConfig,
  19. )
  20. from transformers.models.llama.modeling_llama import LlamaDecoderLayer
  21. from llama_recipes.configs import fsdp_config as FSDP_CONFIG
  22. from llama_recipes.configs import train_config as TRAIN_CONFIG
  23. from llama_recipes.data.concatenator import ConcatDataset
  24. from llama_recipes.policies import AnyPrecisionAdamW, apply_fsdp_checkpointing
  25. from llama_recipes.utils import fsdp_auto_wrap_policy
  26. from llama_recipes.utils.config_utils import (
  27. update_config,
  28. generate_peft_config,
  29. generate_dataset_config,
  30. get_dataloader_kwargs,
  31. )
  32. from llama_recipes.utils.dataset_utils import get_preprocessed_dataset
  33. from llama_recipes.utils.train_utils import (
  34. train,
  35. freeze_transformer_layers,
  36. setup,
  37. setup_environ_flags,
  38. clear_gpu_cache,
  39. print_model_size,
  40. get_policies
  41. )
  42. from accelerate.utils import is_xpu_available
  43. def main(**kwargs):
  44. # Update the configuration for the training and sharding process
  45. train_config, fsdp_config = TRAIN_CONFIG(), FSDP_CONFIG()
  46. update_config((train_config, fsdp_config), **kwargs)
  47. # Set the seeds for reproducibility
  48. if is_xpu_available():
  49. torch.xpu.manual_seed(train_config.seed)
  50. else:
  51. torch.cuda.manual_seed(train_config.seed)
  52. torch.manual_seed(train_config.seed)
  53. random.seed(train_config.seed)
  54. if train_config.enable_fsdp:
  55. setup()
  56. # torchrun specific
  57. local_rank = int(os.environ["LOCAL_RANK"])
  58. rank = int(os.environ["RANK"])
  59. world_size = int(os.environ["WORLD_SIZE"])
  60. if torch.distributed.is_initialized():
  61. if is_xpu_available():
  62. torch.xpu.set_device(local_rank)
  63. else:
  64. torch.cuda.set_device(local_rank)
  65. clear_gpu_cache(local_rank)
  66. setup_environ_flags(rank)
  67. # Load the pre-trained model and setup its configuration
  68. use_cache = False if train_config.enable_fsdp else None
  69. if train_config.enable_fsdp and train_config.low_cpu_fsdp:
  70. """
  71. for FSDP, we can save cpu memory by loading pretrained model on rank0 only.
  72. this avoids cpu oom when loading large models like llama 70B, in which case
  73. model alone would consume 2+TB cpu mem (70 * 4 * 8). This will add some comms
  74. overhead and currently requires latest nightly.
  75. """
  76. v = packaging.version.parse(torch.__version__)
  77. verify_latest_nightly = v.is_devrelease and v.dev >= 20230701
  78. if not verify_latest_nightly:
  79. raise Exception("latest pytorch nightly build is required to run with low_cpu_fsdp config, "
  80. "please install latest nightly.")
  81. if rank == 0:
  82. model = LlamaForCausalLM.from_pretrained(
  83. train_config.model_name,
  84. load_in_8bit=True if train_config.quantization else None,
  85. device_map="auto" if train_config.quantization else None,
  86. use_cache=use_cache,
  87. attn_implementation="sdpa" if train_config.use_fast_kernels else None,
  88. )
  89. else:
  90. llama_config = LlamaConfig.from_pretrained(train_config.model_name)
  91. llama_config.use_cache = use_cache
  92. with torch.device("meta"):
  93. model = LlamaForCausalLM(llama_config)
  94. else:
  95. model = LlamaForCausalLM.from_pretrained(
  96. train_config.model_name,
  97. load_in_8bit=True if train_config.quantization else None,
  98. device_map="auto" if train_config.quantization else None,
  99. use_cache=use_cache,
  100. attn_implementation="sdpa" if train_config.use_fast_kernels else None,
  101. )
  102. # Load the tokenizer and add special tokens
  103. tokenizer = LlamaTokenizer.from_pretrained(train_config.model_name)
  104. tokenizer.pad_token_id = tokenizer.eos_token_id
  105. print_model_size(model, train_config, rank if train_config.enable_fsdp else 0)
  106. # Prepare the model for int8 training if quantization is enabled
  107. if train_config.quantization:
  108. model = prepare_model_for_int8_training(model)
  109. # Convert the model to bfloat16 if fsdp and pure_bf16 is enabled
  110. if train_config.enable_fsdp and fsdp_config.pure_bf16:
  111. model.to(torch.bfloat16)
  112. if train_config.use_peft:
  113. peft_config = generate_peft_config(train_config, kwargs)
  114. model = get_peft_model(model, peft_config)
  115. model.print_trainable_parameters()
  116. #setting up FSDP if enable_fsdp is enabled
  117. if train_config.enable_fsdp:
  118. if not train_config.use_peft and train_config.freeze_layers:
  119. freeze_transformer_layers(train_config.num_freeze_layers)
  120. mixed_precision_policy, wrapping_policy = get_policies(fsdp_config, rank)
  121. my_auto_wrapping_policy = fsdp_auto_wrap_policy(model, LlamaDecoderLayer)
  122. model = FSDP(
  123. model,
  124. auto_wrap_policy= my_auto_wrapping_policy if train_config.use_peft else wrapping_policy,
  125. cpu_offload=CPUOffload(offload_params=True) if fsdp_config.fsdp_cpu_offload else None,
  126. mixed_precision=mixed_precision_policy if not fsdp_config.pure_bf16 else None,
  127. sharding_strategy=fsdp_config.sharding_strategy,
  128. device_id=torch.xpu.current_device() if is_xpu_available() else torch.cuda.current_device(),
  129. limit_all_gathers=True,
  130. sync_module_states=train_config.low_cpu_fsdp,
  131. param_init_fn=lambda module: module.to_empty(device=torch.device("cuda"), recurse=False)
  132. if train_config.low_cpu_fsdp and rank != 0 else None,
  133. )
  134. if fsdp_config.fsdp_activation_checkpointing:
  135. apply_fsdp_checkpointing(model)
  136. elif not train_config.quantization and not train_config.enable_fsdp:
  137. if is_xpu_available():
  138. model.to("xpu:0")
  139. else:
  140. model.to("cuda")
  141. dataset_config = generate_dataset_config(train_config, kwargs)
  142. # Load and preprocess the dataset for training and validation
  143. dataset_train = get_preprocessed_dataset(
  144. tokenizer,
  145. dataset_config,
  146. split="train",
  147. )
  148. if not train_config.enable_fsdp or rank == 0:
  149. print(f"--> Training Set Length = {len(dataset_train)}")
  150. dataset_val = get_preprocessed_dataset(
  151. tokenizer,
  152. dataset_config,
  153. split="test",
  154. )
  155. if not train_config.enable_fsdp or rank == 0:
  156. print(f"--> Validation Set Length = {len(dataset_val)}")
  157. if train_config.batching_strategy == "packing":
  158. dataset_train = ConcatDataset(dataset_train, chunk_size=train_config.context_length)
  159. train_dl_kwargs = get_dataloader_kwargs(train_config, dataset_train, tokenizer, "train")
  160. # Create DataLoaders for the training and validation dataset
  161. train_dataloader = torch.utils.data.DataLoader(
  162. dataset_train,
  163. num_workers=train_config.num_workers_dataloader,
  164. pin_memory=True,
  165. **train_dl_kwargs,
  166. )
  167. eval_dataloader = None
  168. if train_config.run_validation:
  169. if train_config.batching_strategy == "packing":
  170. dataset_val = ConcatDataset(dataset_val, chunk_size=train_config.context_length)
  171. val_dl_kwargs = get_dataloader_kwargs(train_config, dataset_val, tokenizer, "val")
  172. eval_dataloader = torch.utils.data.DataLoader(
  173. dataset_val,
  174. num_workers=train_config.num_workers_dataloader,
  175. pin_memory=True,
  176. **val_dl_kwargs,
  177. )
  178. # Initialize the optimizer and learning rate scheduler
  179. if fsdp_config.pure_bf16 and fsdp_config.optimizer == "anyprecision":
  180. optimizer = AnyPrecisionAdamW(
  181. model.parameters(),
  182. lr=train_config.lr,
  183. momentum_dtype=torch.bfloat16,
  184. variance_dtype=torch.bfloat16,
  185. use_kahan_summation=False,
  186. weight_decay=train_config.weight_decay,
  187. )
  188. else:
  189. optimizer = optim.AdamW(
  190. model.parameters(),
  191. lr=train_config.lr,
  192. weight_decay=train_config.weight_decay,
  193. )
  194. scheduler = StepLR(optimizer, step_size=1, gamma=train_config.gamma)
  195. # Start the training process
  196. results = train(
  197. model,
  198. train_dataloader,
  199. eval_dataloader,
  200. tokenizer,
  201. optimizer,
  202. scheduler,
  203. train_config.gradient_accumulation_steps,
  204. train_config,
  205. fsdp_config if train_config.enable_fsdp else None,
  206. local_rank if train_config.enable_fsdp else None,
  207. rank if train_config.enable_fsdp else None,
  208. )
  209. if not train_config.enable_fsdp or rank==0:
  210. [print(f'Key: {k}, Value: {v}') for k, v in results.items()]
  211. if __name__ == "__main__":
  212. fire.Fire(main)