当前位置:   article > 正文

足够惊艳,使用Alpaca-Lora基于LLaMA(7B)二十分钟完成微调,效果比肩斯坦福羊驼...

llamamodel gradio

来自:吃果冻不吐果冻皮

进NLP群—>加入NLP交流群

之前尝试了从0到1复现斯坦福羊驼(Stanford Alpaca 7B),Stanford Alpaca 是在 LLaMA 整个模型上微调,即对预训练模型中的所有参数都进行微调(full fine-tuning)。但该方法对于硬件成本要求仍然偏高且训练低效。

因此, Alpaca-Lora 则是利用 Lora 技术,在冻结原模型 LLaMA 参数的情况下,通过往模型中加入额外的网络层,并只训练这些新增的网络层参数。由于这些新增参数数量较少,这样不仅微调的成本显著下降(使用一块 RTX 4090 显卡,只用 5 个小时就训练了一个与 Alpaca 水平相当的模型,将这类模型对算力的需求降到了消费级),还能获得和全模型微调(full fine-tuning)类似的效果。

LoRA 技术原理

87cac92db1e49cfc49bfe5d47477e1c7.png
image.png

LoRA 的原理其实并不复杂,它的核心思想是在原始预训练语言模型旁边增加一个旁路,做一个降维再升维的操作,来模拟所谓的 intrinsic rank(预训练模型在各类下游任务上泛化的过程其实就是在优化各类任务的公共低维本征(low-dimensional intrinsic)子空间中非常少量的几个自由参数)。训练的时候固定预训练语言模型的参数,只训练降维矩阵 A 与升维矩阵 B。而模型的输入输出维度不变,输出时将 BA 与预训练语言模型的参数叠加。用随机高斯分布初始化 A,用 0 矩阵初始化 B。这样能保证训练开始时,新增的通路BA=0从,而对模型结果没有影响。

在推理时,将左右两部分的结果加到一起即可,h=Wx+BAx=(W+BA)x,所以,只要将训练完成的矩阵乘积BA跟原本的权重矩阵W加到一起作为新权重参数替换原始预训练语言模型的W即可,不会增加额外的计算资源。

LoRA 的最大优势是速度更快,使用的内存更少;因此,可以在消费级硬件上运行。

下面,我们来尝试使用Alpaca-Lora进行参数高效模型微调。

环境搭建

基础环境配置如下:

  • 操作系统: CentOS 7

  • CPUs: 单个节点具有 1TB 内存的 Intel CPU,物理CPU个数为64,每颗CPU核数为16

  • GPUs: 8 卡 A800 80GB GPUs

  • Python: 3.10 (需要先升级OpenSSL到1.1.1t版本(点击下载OpenSSL),然后再编译安装Python),点击下载Python

  • NVIDIA驱动程序版本: 515.65.01,根据不同型号选择不同的驱动程序,点击下载。

  • CUDA工具包: 11.7,点击下载

  • NCCL: nccl_2.14.3-1+cuda11.7,点击下载

  • cuDNN: 8.8.1.3_cuda11,点击下载

上面的NVIDIA驱动、CUDA、Python等工具的安装就不一一赘述了。

创建虚拟环境并激活虚拟环境alpara-lora-venv-py310-cu117:

  1. cd /home/guodong.li/virtual-venv
  2. virtualenv -p /usr/bin/python3.10 alpara-lora-venv-py310-cu117
  3. source /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/bin/activate

离线安装PyTorch,点击下载对应cuda版本的torch和torchvision即可。

  1. pip install torch-1.13.1+cu117-cp310-cp310-linux_x86_64.whl
  2. pip install pip install torchvision-0.14.1+cu117-cp310-cp310-linux_x86_64.whl

安装transformers,目前,LLaMA相关的实现并没有发布对应的版本,但是已经合并到主分支了,因此,我们需要切换到对应的commit,从源代码进行相应的安装。

  1. cd transformers
  2. git checkout 0041be5
  3. pip install .

在 Alpaca-LoRA 项目中,作者提到,为了廉价高效地进行微调,他们使用了 Hugging Face 的 PEFT。PEFT 是一个库(LoRA 是其支持的技术之一,除此之外还有Prefix Tuning、P-Tuning、Prompt Tuning),可以让你使用各种基于 Transformer 结构的语言模型进行高效微调。下面安装PEFT。

  1. git clone https://github.com/huggingface/peft.git
  2. cd peft/
  3. git checkout e536616
  4. pip install .

安装bitsandbytes。

  1. git clone git@github.com:TimDettmers/bitsandbytes.git
  2. cd bitsandbytes
  3. CUDA_VERSION=117 make cuda11x
  4. python setup.py install

安装其他相关的库。

  1. cd alpaca-lora
  2. pip install -r requirements.txt

requirements.txt文件具体的内容如下:

  1. accelerate
  2. appdirs
  3. loralib
  4. black
  5. black[jupyter]
  6. datasets
  7. fire
  8. sentencepiece
  9. gradio

模型格式转换

将LLaMA原始权重文件转换为Transformers库对应的模型文件格式。具体可参考之前的文章:从0到1复现斯坦福羊驼(Stanford Alpaca 7B) 。如果不想转换LLaMA模型,也可以直接从Hugging Face下载转换好的模型。

模型微调

训练的默认值如下所示:

  1. batch_size: 128
  2. micro_batch_size: 4
  3. num_epochs: 3
  4. learning_rate: 0.0003
  5. cutoff_len: 256
  6. val_set_size: 2000
  7. lora_r: 8
  8. lora_alpha: 16
  9. lora_dropout: 0.05
  10. lora_target_modules: ['q_proj', 'v_proj']
  11. train_on_inputs: True
  12. group_by_length: False
  13. wandb_project:
  14. wandb_run_name:
  15. wandb_watch:
  16. wandb_log_model:
  17. resume_from_checkpoint: False
  18. prompt template: alpaca

使用默认参数,单卡训练完成大约需要5个小时,且对于GPU显存的消耗确实很低。

1%|█▌                                | 12/1170 [03:21<5:24:45, 16.83s/it]

本文为了加快训练速度,将batch_size和micro_batch_size调大并将num_epochs调小了。

  1. python finetune.py \
  2. --base_model '/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b' \
  3. --data_path '/data/nfs/guodong.li/data/alpaca_data_cleaned.json' \
  4. --output_dir '/home/guodong.li/output/lora-alpaca' \
  5. --batch_size 256 \
  6. --micro_batch_size 16 \
  7. --num_epochs 2

当然也可以根据需要微调超参数,参考示例如下:

  1. python finetune.py \
  2. --base_model 'decapoda-research/llama-7b-hf' \
  3. --data_path 'yahma/alpaca-cleaned' \
  4. --output_dir './lora-alpaca' \
  5. --batch_size 128 \
  6. --micro_batch_size 4 \
  7. --num_epochs 3 \
  8. --learning_rate 1e-4 \
  9. --cutoff_len 512 \
  10. --val_set_size 2000 \
  11. --lora_r 8 \
  12. --lora_alpha 16 \
  13. --lora_dropout 0.05 \
  14. --lora_target_modules '[q_proj,v_proj]' \
  15. --train_on_inputs \
  16. --group_by_length

运行过程:

  1. python finetune.py \
  2. > --base_model '/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b' \
  3. > --data_path '/data/nfs/guodong.li/data/alpaca_data_cleaned.json' \
  4. > --output_dir '/home/guodong.li/output/lora-alpaca' \
  5. > --batch_size 256 \
  6. > --micro_batch_size 16 \
  7. > --num_epochs 2
  8. ===================================BUG REPORT===================================
  9. Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues
  10. ================================================================================
  11. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/cuda_setup/main.py:136: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/opt/rh/devtoolset-9/root/usr/lib/dyninst'), PosixPath('/opt/rh/devtoolset-7/root/usr/lib/dyninst')}
  12. warn(msg)
  13. CUDA SETUP: CUDA runtime path found: /usr/local/cuda-11.7/lib64/libcudart.so
  14. CUDA SETUP: Highest compute capability among GPUs detected: 8.0
  15. CUDA SETUP: Detected CUDA version 117
  16. CUDA SETUP: Loading binary /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/libbitsandbytes_cuda117.so...
  17. Training Alpaca-LoRA model with params:
  18. base_model: /data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b
  19. data_path: /data/nfs/guodong.li/data/alpaca_data_cleaned.json
  20. output_dir: /home/guodong.li/output/lora-alpaca
  21. batch_size: 256
  22. micro_batch_size: 16
  23. num_epochs: 2
  24. learning_rate: 0.0003
  25. cutoff_len: 256
  26. val_set_size: 2000
  27. lora_r: 8
  28. lora_alpha: 16
  29. lora_dropout: 0.05
  30. lora_target_modules: ['q_proj', 'v_proj']
  31. train_on_inputs: True
  32. group_by_length: False
  33. wandb_project:
  34. wandb_run_name:
  35. wandb_watch:
  36. wandb_log_model:
  37. resume_from_checkpoint: False
  38. prompt template: alpaca
  39. Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 33/33 [00:10<00:00, 3.01it/s]
  40. Found cached dataset json (/home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e)
  41. 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 228.95it/s]
  42. trainable params: 4194304 || all params: 6742609920 || trainable%: 0.06220594176090199
  43. Loading cached split indices for dataset at /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-d8c5d7ac95d53860.arrow and /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-4a34b0c9feb19e72.arrow
  44. {'loss': 2.2501, 'learning_rate': 2.6999999999999996e-05, 'epoch': 0.05}
  45. ...
  46. {'loss': 0.8998, 'learning_rate': 0.000267, 'epoch': 0.46}
  47. {'loss': 0.8959, 'learning_rate': 0.00029699999999999996, 'epoch': 0.51}
  48. 28%|███████████████████████████████████████████▎ | 109/390 [32:48<1:23:14, 17.77s/it]

显存占用:

  1. +-----------------------------------------------------------------------------+
  2. | NVIDIA-SMI 515.105.01 Driver Version: 515.105.01 CUDA Version: 11.7 |
  3. |-------------------------------+----------------------+----------------------+
  4. | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
  5. | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
  6. | | | MIG M. |
  7. |===============================+======================+======================|
  8. | 0 NVIDIA A800 80G... Off | 00000000:34:00.0 Off | 0 |
  9. | N/A 71C P0 299W / 300W | 57431MiB / 81920MiB | 100% Default |
  10. | | | Disabled |
  11. +-------------------------------+----------------------+----------------------+
  12. ...
  13. +-------------------------------+----------------------+----------------------+
  14. | 7 NVIDIA A800 80G... Off | 00000000:9E:00.0 Off | 0 |
  15. | N/A 33C P0 71W / 300W | 951MiB / 81920MiB | 0% Default |
  16. | | | Disabled |
  17. +-------------------------------+----------------------+----------------------+
  18. +-----------------------------------------------------------------------------+
  19. | Processes: |
  20. | GPU GI CI PID Type Process name GPU Memory |
  21. | ID ID Usage |
  22. |=============================================================================|
  23. | 0 N/A N/A 55017 C python 57429MiB |
  24. ...
  25. | 7 N/A N/A 55017 C python 949MiB |
  26. +-----------------------------------------------------------------------------+

发现GPU的使用率上去了,训练速度也提升了,但是没有充分利用GPU资源,单卡训练(epoch:3)大概3小时即可完成。

因此,为了进一步提升模型训练速度,下面尝试使用数据并行,在多卡上面进行训练。

  1. torchrun --nproc_per_node=8 --master_port=29005 finetune.py \
  2. --base_model '/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b' \
  3. --data_path '/data/nfs/guodong.li/data/alpaca_data_cleaned.json' \
  4. --output_dir '/home/guodong.li/output/lora-alpaca' \
  5. --batch_size 256 \
  6. --micro_batch_size 16 \
  7. --num_epochs 2

运行过程:

  1. torchrun --nproc_per_node=8 --master_port=29005 finetune.py \
  2. > --base_model '/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b' \
  3. > --data_path '/data/nfs/guodong.li/data/alpaca_data_cleaned.json' \
  4. > --output_dir '/home/guodong.li/output/lora-alpaca' \
  5. > --batch_size 256 \
  6. > --micro_batch_size 16 \
  7. > --num_epochs 2
  8. WARNING:torch.distributed.run:
  9. *****************************************
  10. Setting OMP_NUM_THREADS environment variable for each process to be 1 in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed.
  11. *****************************************
  12. ===================================BUG REPORT===================================
  13. Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues
  14. ================================================================================
  15. ...
  16. ===================================BUG REPORT===================================
  17. Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues
  18. ================================================================================
  19. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/cuda_setup/main.py:136: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/opt/rh/devtoolset-9/root/usr/lib/dyninst'), PosixPath('/opt/rh/devtoolset-7/root/usr/lib/dyninst')}
  20. warn(msg)
  21. CUDA SETUP: CUDA runtime path found: /usr/local/cuda-11.7/lib64/libcudart.so
  22. CUDA SETUP: Highest compute capability among GPUs detected: 8.0
  23. CUDA SETUP: Detected CUDA version 117
  24. CUDA SETUP: Loading binary /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/libbitsandbytes_cuda117.so...
  25. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/cuda_setup/main.py:136: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/opt/rh/devtoolset-7/root/usr/lib/dyninst'), PosixPath('/opt/rh/devtoolset-9/root/usr/lib/dyninst')}
  26. ...
  27. Training Alpaca-LoRA model with params:
  28. base_model: /data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b
  29. data_path: /data/nfs/guodong.li/data/alpaca_data_cleaned.json
  30. output_dir: /home/guodong.li/output/lora-alpaca
  31. batch_size: 256
  32. micro_batch_size: 16
  33. num_epochs: 2
  34. learning_rate: 0.0003
  35. cutoff_len: 256
  36. val_set_size: 2000
  37. lora_r: 8
  38. lora_alpha: 16
  39. lora_dropout: 0.05
  40. lora_target_modules: ['q_proj', 'v_proj']
  41. train_on_inputs: True
  42. group_by_length: False
  43. wandb_project:
  44. wandb_run_name:
  45. wandb_watch:
  46. wandb_log_model:
  47. resume_from_checkpoint: False
  48. prompt template: alpaca
  49. Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 33/33 [00:14<00:00, 2.25it/s]
  50. ...
  51. Loading checkpoint shards: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 33/33 [00:20<00:00, 1.64it/s]
  52. Found cached dataset json (/home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e)
  53. 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 129.11it/s]
  54. trainable params: 4194304 || all params: 6742609920 || trainable%: 0.06220594176090199
  55. Loading cached split indices for dataset at /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-d8c5d7ac95d53860.arrow and /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-4a34b0c9feb19e72.arrow
  56. Map: 4%|██████▎ | 2231/49942 [00:01<00:37, 1256.31 examples/s]Found cached dataset json (/home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e)
  57. 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 220.24it/s]
  58. ...
  59. trainable params: 4194304 || all params: 6742609920 || trainable%: 0.06220594176090199
  60. Loading cached split indices for dataset at /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-d8c5d7ac95d53860.arrow and /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-4a34b0c9feb19e72.arrow
  61. Map: 2%|██▋ | 939/49942 [00:00<00:37, 1323.94 examples/s]Found cached dataset json (/home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e)
  62. 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00, 362.77it/s]
  63. trainable params: 4194304 || all params: 6742609920 || trainable%: 0.06220594176090199
  64. Loading cached split indices for dataset at /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-d8c5d7ac95d53860.arrow and /home/guodong.li/.cache/huggingface/datasets/json/default-2dab63d15cf49261/0.0.0/fe5dd6ea2639a6df622901539cb550cf8797e5a6b2dd7af1cf934bed8e233e6e/cache-4a34b0c9feb19e72.arrow
  65. {'loss': 2.2798, 'learning_rate': 1.7999999999999997e-05, 'epoch': 0.05}
  66. ...
  67. {'loss': 0.853, 'learning_rate': 0.0002006896551724138, 'epoch': 1.02}
  68. {'eval_loss': 0.8590874075889587, 'eval_runtime': 10.5401, 'eval_samples_per_second': 189.752, 'eval_steps_per_second': 3.036, 'epoch': 1.02}
  69. {'loss': 0.8656, 'learning_rate': 0.0001903448275862069, 'epoch': 1.07}
  70. ...
  71. {'loss': 0.8462, 'learning_rate': 6.620689655172413e-05, 'epoch': 1.69}
  72. {'loss': 0.8585, 'learning_rate': 4.137931034482758e-06, 'epoch': 1.99}
  73. {'loss': 0.8549, 'learning_rate': 0.00011814432989690721, 'epoch': 2.05}
  74. {'eval_loss': 0.8465630412101746, 'eval_runtime': 10.5273, 'eval_samples_per_second': 189.983, 'eval_steps_per_second': 3.04, 'epoch': 2.05}
  75. {'loss': 0.8492, 'learning_rate': 0.00011195876288659793, 'epoch': 2.1}
  76. ...
  77. {'loss': 0.8398, 'learning_rate': 1.2989690721649484e-05, 'epoch': 2.92}
  78. {'loss': 0.8473, 'learning_rate': 6.804123711340206e-06, 'epoch': 2.97}
  79. 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 585/585 [23:46<00:00, 2.38s/it]
  80. {'train_runtime': 1426.9255, 'train_samples_per_second': 104.999, 'train_steps_per_second': 0.41, 'train_loss': 0.9613736364576552, 'epoch': 2.99}
  81. 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 585/585 [23:46<00:00, 2.44s/it]

模型文件:

  1. > tree /home/guodong.li/output/lora-alpaca
  2. /home/guodong.li/output/lora-alpaca
  3. ├── adapter_config.json
  4. ├── adapter_model.bin
  5. └── checkpoint-200
  6. ├── optimizer.pt
  7. ├── pytorch_model.bin
  8. ├── rng_state_0.pth
  9. ├── rng_state_1.pth
  10. ├── rng_state_2.pth
  11. ├── rng_state_3.pth
  12. ├── rng_state_4.pth
  13. ├── rng_state_5.pth
  14. ├── rng_state_6.pth
  15. ├── rng_state_7.pth
  16. ├── scaler.pt
  17. ├── scheduler.pt
  18. ├── trainer_state.json
  19. └── training_args.bin
  20. 1 directory, 16 files

我们可以看到,在数据并行的情况下,如果epoch=3(本文epoch=2),训练仅需要20分钟左右即可完成。目前,tloen/Alpaca-LoRA-7b提供的最新“官方”的Alpaca-LoRA adapter于 3 月 26 日使用以下超参数进行训练。

  1. - Epochs: 10 (load from best epoch)
  2. - Batch size: 128
  3. - Cutoff length: 512
  4. - Learning rate: 3e-4
  5. - Lora r: 16
  6. - Lora target modules: q_proj, k_proj, v_proj, o_proj

具体命令如下:

  1. python finetune.py \
  2. --base_model='decapoda-research/llama-7b-hf' \
  3. --num_epochs=10 \
  4. --cutoff_len=512 \
  5. --group_by_length \
  6. --output_dir='./lora-alpaca' \
  7. --lora_target_modules='[q_proj,k_proj,v_proj,o_proj]' \
  8. --lora_r=16 \
  9. --micro_batch_size=8

模型推理

运行命令如下:

  1. python generate.py \
  2. --load_8bit \
  3. --base_model '/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b' \
  4. --lora_weights '/home/guodong.li/output/lora-alpaca'

运行这个脚本会启动一个gradio服务,你可以通过浏览器在网页上进行测试。

运行过程如下所示:

  1. python generate.py \
  2. > --load_8bit \
  3. > --base_model '/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b' \
  4. > --lora_weights '/home/guodong.li/output/lora-alpaca'
  5. ===================================BUG REPORT===================================
  6. Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues
  7. ================================================================================
  8. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/cuda_setup/main.py:136: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/opt/rh/devtoolset-9/root/usr/lib/dyninst'), PosixPath('/opt/rh/devtoolset-7/root/usr/lib/dyninst')}
  9. warn(msg)
  10. CUDA SETUP: CUDA runtime path found: /usr/local/cuda-11.7/lib64/libcudart.so
  11. CUDA SETUP: Highest compute capability among GPUs detected: 8.0
  12. CUDA SETUP: Detected CUDA version 117
  13. CUDA SETUP: Loading binary /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/libbitsandbytes_cuda117.so...
  14. Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 33/33 [00:12<00:00, 2.68it/s]
  15. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/gradio/inputs.py:27: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components
  16. warnings.warn(
  17. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect
  18. warnings.warn(value)
  19. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `numeric` parameter is deprecated, and it has no effect
  20. warnings.warn(value)
  21. Running on local URL: http://0.0.0.0:7860
  22. To create a public link, set `share=True` in `launch()`.

显存占用:

  1. +-----------------------------------------------------------------------------+
  2. | NVIDIA-SMI 515.105.01 Driver Version: 515.105.01 CUDA Version: 11.7 |
  3. |-------------------------------+----------------------+----------------------+
  4. | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
  5. | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
  6. | | | MIG M. |
  7. |===============================+======================+======================|
  8. | 0 NVIDIA A800 80G... Off | 00000000:34:00.0 Off | 0 |
  9. | N/A 50C P0 81W / 300W | 8877MiB / 81920MiB | 0% Default |
  10. | | | Disabled |
  11. +-------------------------------+----------------------+----------------------+
  12. +-----------------------------------------------------------------------------+
  13. | Processes: |
  14. | GPU GI CI PID Type Process name GPU Memory |
  15. | ID ID Usage |
  16. |=============================================================================|
  17. | 0 N/A N/A 7837 C python 8875MiB |
  18. +-----------------------------------------------------------------------------+

打开浏览器输入IP+端口进行测试。

1ef66c5f78183fe63dcf4d153dedb0d7.png
image.png

将 LoRA 权重合并回基础模型

下面将 LoRA 权重合并回基础模型以导出为 HuggingFace 格式和 PyTorch state_dicts。以帮助想要在 llama.cpp 或 alpaca.cpp 等项目中运行推理的用户。

导出为 HuggingFace 格式

修改export_hf_checkpoint.py文件:

  1. import os
  2. import torch
  3. import transformers
  4. from peft import PeftModel
  5. from transformers import LlamaForCausalLM, LlamaTokenizer # noqa: F402
  6. BASE_MODEL = os.environ.get("BASE_MODEL", None)
  7. # TODO
  8. LORA_MODEL = os.environ.get("LORA_MODEL", "tloen/alpaca-lora-7b")
  9. HF_CHECKPOINT = os.environ.get("HF_CHECKPOINT", "./hf_ckpt")
  10. assert (
  11. BASE_MODEL
  12. ), "Please specify a value for BASE_MODEL environment variable, e.g. `export BASE_MODEL=decapoda-research/llama-7b-hf`" # noqa: E501
  13. tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL)
  14. base_model = LlamaForCausalLM.from_pretrained(
  15. BASE_MODEL,
  16. load_in_8bit=False,
  17. torch_dtype=torch.float16,
  18. device_map={"": "cpu"},
  19. )
  20. first_weight = base_model.model.layers[0].self_attn.q_proj.weight
  21. first_weight_old = first_weight.clone()
  22. lora_model = PeftModel.from_pretrained(
  23. base_model,
  24. # TODO
  25. # "tloen/alpaca-lora-7b",
  26. LORA_MODEL,
  27. device_map={"": "cpu"},
  28. torch_dtype=torch.float16,
  29. )
  30. ...
  31. # TODO
  32. LlamaForCausalLM.save_pretrained(
  33. base_model, HF_CHECKPOINT , state_dict=deloreanized_sd, max_shard_size="400MB"
  34. )

运行命令:

  1. BASE_MODEL=/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b \
  2. LORA_MODEL=/home/guodong.li/output/lora-alpaca \
  3. HF_CHECKPOINT=/home/guodong.li/output/hf_ckpt \
  4. python export_hf_checkpoint.py

运行过程:

  1. BASE_MODEL=/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b \
  2. > LORA_MODEL=/home/guodong.li/output/lora-alpaca \
  3. > HF_CHECKPOINT=/home/guodong.li/output/hf_ckpt \
  4. > python export_hf_checkpoint.py
  5. ===================================BUG REPORT===================================
  6. Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues
  7. ================================================================================
  8. /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/cuda_setup/main.py:136: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/opt/rh/devtoolset-7/root/usr/lib/dyninst'), PosixPath('/opt/rh/devtoolset-9/root/usr/lib/dyninst')}
  9. warn(msg)
  10. CUDA SETUP: CUDA runtime path found: /usr/local/cuda-11.7/lib64/libcudart.so
  11. CUDA SETUP: Highest compute capability among GPUs detected: 8.0
  12. CUDA SETUP: Detected CUDA version 117
  13. CUDA SETUP: Loading binary /home/guodong.li/virtual-venv/alpara-lora-venv-py310-cu117/lib/python3.10/site-packages/bitsandbytes-0.37.2-py3.10.egg/bitsandbytes/libbitsandbytes_cuda117.so...
  14. Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 33/33 [00:05<00:00, 5.99it/s]

查看模型输出文件:

  1. > tree /home/guodong.li/output/hf_ckpt
  2. /home/guodong.li/output/hf_ckpt
  3. ├── config.json
  4. ├── generation_config.json
  5. ├── pytorch_model-00001-of-00039.bin
  6. ├── pytorch_model-00002-of-00039.bin
  7. ...
  8. ├── pytorch_model-00038-of-00039.bin
  9. ├── pytorch_model-00039-of-00039.bin
  10. └── pytorch_model.bin.index.json
  11. 0 directories, 42 files

导出为PyTorch state_dicts

修改export_state_dict_checkpoint.py文件:

  1. import json
  2. import os
  3. import torch
  4. import transformers
  5. from peft import PeftModel
  6. from transformers import LlamaForCausalLM, LlamaTokenizer # noqa: E402
  7. BASE_MODEL = os.environ.get("BASE_MODEL", None)
  8. LORA_MODEL = os.environ.get("LORA_MODEL", "tloen/alpaca-lora-7b")
  9. PTH_CHECKPOINT_PREFIX = os.environ.get("PTH_CHECKPOINT_PREFIX", "./ckpt")
  10. assert (
  11. BASE_MODEL
  12. ), "Please specify a value for BASE_MODEL environment variable, e.g. `export BASE_MODEL=decapoda-research/llama-7b-hf`" # noqa: E501
  13. tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL)
  14. base_model = LlamaForCausalLM.from_pretrained(
  15. BASE_MODEL,
  16. load_in_8bit=False,
  17. torch_dtype=torch.float16,
  18. device_map={"": "cpu"},
  19. )
  20. lora_model = PeftModel.from_pretrained(
  21. base_model,
  22. # todo
  23. #"tloen/alpaca-lora-7b",
  24. LORA_MODEL,
  25. device_map={"": "cpu"},
  26. torch_dtype=torch.float16,
  27. )
  28. ...
  29. os.makedirs(PTH_CHECKPOINT_PREFIX, exist_ok=True)
  30. torch.save(new_state_dict, PTH_CHECKPOINT_PREFIX+"/consolidated.00.pth")
  31. with open(PTH_CHECKPOINT_PREFIX+"/params.json", "w") as f:
  32. json.dump(params, f)

运行命令:

  1. BASE_MODEL=/data/nfs/guodong.li/pretrain/hf-llama-model/llama-7b \
  2. LORA_MODEL=/home/guodong.li/output/lora-alpaca \
  3. PTH_CHECKPOINT_PREFIX=/home/guodong.li/output/ckpt \
  4. python export_state_dict_checkpoint.py

查看模型输出文件:

  1. tree /home/guodong.li/output/ckpt
  2. /home/guodong.li/output/ckpt
  3. ├── consolidated.00.pth
  4. └── params.json

当然,你还可以封装为Docker镜像来对训练和推理环境进行隔离。

封装为Docker镜像并进行推理

  1. 构建Docker镜像:

docker build -t alpaca-lora .
  1. 运行Docker容器进行推理 (您还可以使用 finetune.py 及其上面提供的所有超参数进行训练):

  1. docker run --gpus=all --shm-size 64g -p 7860:7860 -v ${HOME}/.cache:/root/.cache --rm alpaca-lora generate.py \
  2. --load_8bit \
  3. --base_model 'decapoda-research/llama-7b-hf' \
  4. --lora_weights 'tloen/alpaca-lora-7b'
  1. 打开浏览器,输入URL:https://localhost:7860进行测试。

结语

从上面可以看到,在一台8卡的A800服务器上面,基于Alpaca-Lora针对alpaca_data_cleaned.json指令数据大概20分钟左右即可完成参数高效微调,相对于斯坦福羊驼训练速度显著提升。

参考文档

  • LLaMA

  • Stanford Alpaca:斯坦福-羊驼

  • Alpaca-LoRA


进NLP群—>加入NLP交流群

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/926411
推荐阅读
相关标签
  

闽ICP备14008679号