文章目錄
- 前言
- 一、Llama3模型簡介
- 1.下載llama3源碼到linux服務器
- 2.安裝依賴
- 3.測試預訓練模型Meta-Llama-3-8B
- 4.測試指令微調模型Meta-Llama3-8B-Instruct
- 5.小結
- 二、LoRA微調Llama3
- 1.引入庫
- 2.編寫配置文件
- 3.LoRA訓練的產物
- 三、測試新模型效果
- 1.編寫配置文件
- 2.運行配置文件:
- 總結
前言
因為上篇文章點贊數超過1,所以今天繼續更新llama3微調方法。先介紹一下如何與本地llama3模型交互,再介紹如何使用torchtune和LoRA方式微調llama3,最后介紹一下如何用torchtune與llama3模型交互。
一、Llama3模型簡介
目前llama3開源的模型有Meta-Llama-3-8B、Meta-Llama-3-8B-Instruct、Meta-Llama-3-70B和Meta-Llama-3-70B-Instruct。這里Meta-Llama-3-8B是參數規模為80億的預訓練模型(pretrained model),Meta-Llama-3-8B-Instruct是80億參數經過指令微調的模型(instruct fine-tuned model);對應的,后兩個模型就是對應700億參數的預訓練和指令微調模型。那么,預訓練模型和指令微調模型有什么區別呢?我們來跟她們對話一下就明白了。
1.下載llama3源碼到linux服務器
git clone https://github.com/meta-llama/llama3.git
2.安裝依賴
最好先用anaconda創建一個專門為微調模型準備的python虛擬環境,然后運行命令:
cd llama3
pip install -e .
3.測試預訓練模型Meta-Llama-3-8B
torchrun --nproc_per_node 1 example_text_completion.py
–ckpt_dir Meta-Llama-3-8B/
–tokenizer_path Meta-Llama-3-8B/tokenizer.model
–max_seq_len 128 --max_batch_size 4
參數解釋:
–ckpt_dir 模型權重所在文件夾路徑,一般后綴為.pt、.pth或.safetensors
–tokenizer_path 分詞器路徑,必須帶上分詞器名稱,例如tokenizer.model
–max_seq_len 輸出的最大序列長度,這個在預訓練模型的調用中是必帶參數
–max_batch_size 每個批次包含的最大樣本數
下圖是模型的輸出結果,當我輸入英文"I believe the meaning of life is"時,模型會輸出"to love. It is to love others, to love ourselves, and to love God. Love is the meaning of life blablabla"。
很明顯,預訓練模型Meta-Llama3-8B是對用戶輸入的一個續寫。
4.測試指令微調模型Meta-Llama3-8B-Instruct
torchrun --nproc_per_node 1 example_chat_completion.py
–ckpt_dir /data/jack/Meta-Llama-3-8B-Instruct/original/
–tokenizer_path /data/jack/Meta-Llama-3-8B-Instruct/original/tokenizer.model
–max_seq_len 512 --max_batch_size 4
參數解釋:
–max_seq_len 輸出的最大序列長度,這個對指令微調模型是可選參數,不設置指令微調模型也會在問答中自動停止輸出
如上圖所示,Meta-Llama-3-8B-Instruct模型面對用戶的提問,會給出合適的回答。
5.小結
Meta-Llama-3-8B是針對用戶輸入的一個續寫,跟Transformer架構的模型在預訓練過程中的下一詞匯預測很相似;Meta-Llama-3-8B-Instruct是可以回答用戶提問的模型。因此,在選擇LoRA微調的基底模型時,大部分情況應當選擇指令詞微調模型。
二、LoRA微調Llama3
1.引入庫
在切換到anaconda或venv的python環境后:
pip install torchtune
2.編寫配置文件
如果下載了torchtune倉庫的源碼,可以從中拷貝出對應的recipe文件,文件夾的相對路徑為:
torchtune\recipes\configs\llama3
# Model Arguments
model:_component_: torchtune.models.llama3.lora_llama3_8blora_attn_modules: ['q_proj', 'v_proj']apply_lora_to_mlp: Falseapply_lora_to_output: Falselora_rank: 8lora_alpha: 16# Tokenizer
tokenizer:_component_: torchtune.models.llama3.llama3_tokenizerpath: /data/jack/Meta-Llama-3-8B-Instruct/original/tokenizer.modelcheckpointer:_component_: torchtune.utils.FullModelMetaCheckpointercheckpoint_dir: /data/jack/Meta-Llama-3-8B-Instruct/original/checkpoint_files: [consolidated.00.pth]recipe_checkpoint: nulloutput_dir: /data/jack/Meta-Llama-3-8B-Instruct/model_type: LLAMA3
resume_from_checkpoint: False# Dataset and Sampler
dataset:_component_: torchtune.datasets.alpaca_cleaned_datasettrain_on_input: True
seed: null
shuffle: True
batch_size: 2# Optimizer and Scheduler
optimizer:_component_: torch.optim.AdamWweight_decay: 0.01lr: 3e-4
lr_scheduler:_component_: torchtune.modules.get_cosine_schedule_with_warmupnum_warmup_steps: 100loss:_component_: torch.nn.CrossEntropyLoss# Training
epochs: 1
max_steps_per_epoch: null
gradient_accumulation_steps: 64
compile: False# Logging
output_dir: /data/jack/torchtune_test/lora_finetune_output
metric_logger:_component_: torchtune.utils.metric_logging.DiskLoggerlog_dir: ${output_dir}
log_every_n_steps: 1
log_peak_memory_stats: False# Environment
device: cuda
dtype: bf16
enable_activation_checkpointing: True# Profiler (disabled)
profiler:_component_: torchtune.utils.profilerenabled: False
這里解釋一下各個參數:
tokenizer下的path 這個一定要填寫你下載的基底模型的分詞器路徑
checkpointer下的checkpoint_dir 填寫你要微調的基底模型的存放文件夾路徑
checkpointer下的checkpoint_files 基底模型文件名(一個或多個)
dataset下的_component_ 填寫現有的或自定義的數據集類名
loss下的_component_ 填寫訓練過程中的損失函數,默認是交叉熵損失
epochs(發音:一剖克斯) 訓練輪次,可以先設置為1試一下
下命令進行微調:
tune run lora_finetune_single_device --config ./8B_lora_single_device_custom.yaml
3.LoRA訓練的產物
結束訓練后,在輸出文件夾中會產出一個合并了lora和基底模型權重的新模型meta_model_0.pt,也會產出一個獨立的lora權重文件adapter_0.pt。
三、測試新模型效果
1.編寫配置文件
# Model arguments
model:_component_: torchtune.models.llama3.llama3_8bcheckpointer:_component_: torchtune.utils.FullModelMetaCheckpointercheckpoint_dir: /data/feipan3/Meta-Llama-3-8B-Instruct/checkpoint_files: [meta_model_0.pt]output_dir: /data/feipan3/Meta-Llama-3-8B-Instruct/model_type: LLAMA3device: cuda
dtype: bf16seed: 5678# Tokenizer arguments
tokenizer:_component_: torchtune.models.llama3.llama3_tokenizerpath: /data/feipan3/Meta-Llama-3-8B-Instruct/original/tokenizer.model# Generation arguments; defaults taken from gpt-fast
prompt: "Give three tips for staying healthy."
max_new_tokens: 512
temperature: 0.6 # 0.8 and 0.6 are popular values to try
top_k: 300quantizer: null
2.運行配置文件:
tune run generate --config generation_custom.yaml
我的問題是“給出保持健康的三條建議”,得到輸出結果:
總結
本文主要介紹了如何用torchtune工具微調Llama3,詳細介紹了如何設置配方文件,如何運行以及最后如何進行效果驗證。本文點贊超過2個,下一期繼續更新如何準備微調數據集。