實(shí)戰(zhàn)指南:從全參數(shù) SFT 到 LoRA/QLoRA 與多種 PEFT 策略)
PaddleNLP 大模型微調(diào)實(shí)戰(zhàn)指南從全參數(shù) SFT 到 LoRA/QLoRA 與多種 PEFT 策略【免費(fèi)下載鏈接】PaddleNLPEasy-to-use and powerful LLM and SLM library with awesome model zoo.項目地址: https://gitcode.com/gh_mirrors/pa/PaddleNLP導(dǎo)讀本文基于 PaddleNLP 官方微調(diào)文檔系統(tǒng)講解大語言模型監(jiān)督微調(diào)SFT的完整方案包括全參數(shù)微調(diào)、LoRA/QLoRA、Prefix Tuning、VeRA、LoKr、ReFT 等多種參數(shù)高效微調(diào)PEFT策略的啟動命令、配置文件與參數(shù)含義并結(jié)合倉庫中 llm/run_finetune.py 的源碼實(shí)現(xiàn)與 llm/config/llama 下的真實(shí)配置深入說明數(shù)據(jù)加載、Zero Padding、FlashAttention/FlashMask、4D 并行等底層機(jī)制。讀完本文你將能夠基于統(tǒng)一腳本在單卡或多卡環(huán)境下完成從數(shù)據(jù)準(zhǔn)備、模型微調(diào)到 LoRA/VeRA/LoKr 參數(shù)合并與推理的全流程。1. PaddleNLP 大模型微調(diào)能力總覽大模型微調(diào)Supervised Fine-Tuning, SFT是大語言模型應(yīng)用的關(guān)鍵環(huán)節(jié)主要目標(biāo)是讓模型學(xué)會遵循指令、生成符合預(yù)期的回答從而提升通用模型在特定領(lǐng)域和應(yīng)用場景下的表現(xiàn)滿足大模型的個性化應(yīng)用需求。PaddleNLP 的微調(diào)方案具備以下核心能力易用的并行策略支持純數(shù)據(jù)并行Data Parallelism、Sharding 并行、張量并行Tensor Parallelism、流水線并行Pipeline Parallelism與序列并行Sequence Parallelism可自由組合成 4D 并行。多精度訓(xùn)練全參數(shù)微調(diào)支持 16/32 位精度LoRA 微調(diào)支持 4/8/16 位精度并支持混合精度的量化 LoRAQLoRA。極致性能優(yōu)化集成 FlashAttention-2、FlashMask、Greedy Zero Padding 等加速與顯存優(yōu)化手段。先進(jìn)微調(diào)策略除基礎(chǔ) LoRA 外還支持 LoRA、PiSSA、rsLoRA、NEFTune、VeRA、MoRA、ReFT、MoSLoRA 等前沿算法。各算法細(xì)節(jié)可進(jìn)一步參考 PaddlePaddle 大模型算法文檔。2. 常用微調(diào)技術(shù)簡介全參數(shù)微調(diào)Full-Parameter Fine-Tuning最常見的 SFT 技術(shù)在指令數(shù)據(jù)集上重新訓(xùn)練預(yù)訓(xùn)練模型的全部參數(shù)。這種方式通常效果最好但需要大量計算資源。LoRALow-Rank Adaptation應(yīng)用最廣泛的參數(shù)高效微調(diào)PEFT技術(shù)。它凍結(jié)原始權(quán)重僅為每個目標(biāo)線性層引入低秩矩陣可將可訓(xùn)練參數(shù)量減少 99% 以上顯著降低顯存占用與訓(xùn)練時間。QLoRAQuantization-Aware Low-Rank Adaptation相比標(biāo)準(zhǔn) LoRA 可減少最高約 33% 的顯存占用特別適合顯存受限場景。QLoRA 通常比普通 LoRA 多花費(fèi)約 20% 的訓(xùn)練時間但在顯存不足時它往往是唯一可行的選擇。3. 快速開始以 Llama 3 為例下文將以Llama 3為例演示如何使用統(tǒng)一腳本完成全參數(shù) SFT 與 LoRA 微調(diào)。3.1 環(huán)境準(zhǔn)備運(yùn)行微調(diào)腳本需要以下版本依賴PaddlePaddle 3.0-betaPaddleNLP 3.0.0b3PaddleSlim develop克隆代碼到本地后即可開始git clone https://github.com/PaddlePaddle/PaddleNLP.git # 使用 develop 版本安裝 cd PaddleNLP/llm # 進(jìn)入運(yùn)行目錄從源碼看llm/run_finetune.py 在啟動時會校驗(yàn) PaddleNLP 版本要求paddlenlp 3.0.0b3否則會直接拋出安裝提示因此請確保環(huán)境版本符合要求。3.2 微調(diào)數(shù)據(jù)準(zhǔn)備為方便測試官方提供了示例數(shù)據(jù)集廣告生成數(shù)據(jù)集Advertisement Generation Dataset下載地址見原文檔可通過pip配套工具獲取。用戶也可以按照如下數(shù)據(jù)格式構(gòu)造自己的微調(diào)數(shù)據(jù)集。每行需要是一個包含以下字段的字典srcstr, List(str)模型輸入的指令instruction、提示詞或要求模型執(zhí)行的任務(wù)。tgtstr, List(str)模型的輸出。示例數(shù)據(jù){src: type#dress*color#blue*style#fresh*pattern#bow, tgt: The dress features three-dimensional bow decorations with blue stripes, creating a full and layered silhouette while adding a touch of sweetness. This design highlights the girls fresh and charming appearance.} ...數(shù)據(jù)加載邏輯在 llm/run_finetune.py 的create_dataset函數(shù)中實(shí)現(xiàn)腳本會依次嘗試dataset_name_or_path/train.json或train/*.json、dev.json、test.json支持單文件與目錄多文件兩種組織方式若本地路徑不存在則回退為按內(nèi)置數(shù)據(jù)集名稱加載。3.3 全參數(shù)微調(diào)Full-Parameter SFTpython -u -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 run_finetune.py ./config/llama/sft_argument.json注意事項同時開啟zero_padding與greedy_zero_padding有助于提升訓(xùn)練效率。建議將per_device_train_batch_size設(shè)為 1通過gradient_accumulation_steps控制實(shí)際 batch size并合理調(diào)整max_length。將use_flash_attention設(shè)為 True 以啟用 FlashAttention在啟用 FlashAttention 的基礎(chǔ)上將flash_mask設(shè)為 True 可進(jìn)一步啟用 FlashMask。SFT API 支持 4D 并行策略通過tensor_parallel_degree、pipeline_parallel_degree、sharding與sharding_parallel_degree調(diào)整。倉庫自帶的 llm/config/llama/sft_argument.json 是一份可直接參考的全參數(shù)微調(diào)配置8B 模型、bf16fp16_opt_level: O2、sharding: stage2、unified_checkpoint: true{ model_name_or_path: meta-llama/Meta-Llama-3-8B, dataset_name_or_path: ./data, output_dir: ./checkpoints/llama_sft_ckpts, per_device_train_batch_size: 1, gradient_accumulation_steps: 2, per_device_eval_batch_size: 8, eval_accumulation_steps: 16, num_train_epochs: 1, learning_rate: 3e-05, warmup_steps: 30, logging_steps: 1, evaluation_strategy: epoch, save_strategy: epoch, src_length: 1024, max_length: 2048, bf16: true, fp16_opt_level: O2, do_train: true, do_eval: true, disable_tqdm: true, load_best_model_at_end: true, eval_with_do_generation: false, metric_for_best_model: accuracy, recompute: false, save_total_limit: 1, tensor_parallel_degree: 1, pipeline_parallel_degree: 1, pipeline_parallel_config: disable_p2p_cache_shape, sharding: stage2, zero_padding: false, unified_checkpoint: true, use_flash_attention: false }從源碼實(shí)現(xiàn)看llm/run_finetune.py 通過PdArgumentParser解析配置支持.json、.yaml、.py三種配置文件格式也支持直接在命令行追加參數(shù)覆蓋當(dāng)pipeline_parallel_degree 1時會自動切換到AutoModelForCausalLMPipe構(gòu)建流水線并行模型見 llm/run_finetune.py。3.4 PEFT 參數(shù)高效微調(diào)3.4.1 LoRA / QLoRApython -u -m paddle.distributed.launch --gpus 0,1,2,3 finetune_generation.py --config ./config/llama/sft_argument.json --lora ./config/llama/lora_argument.json # QLoRA python -u -m paddle.distributed.launch --gpus 0,1,2,3 finetune_generation.py --config ./config/llama/sft_argument.json --q_lora ./config/llama/lora_argument.json也可以使用run_finetune.py直接傳入配置# 單卡 LoRA python run_finetune.py ./config/llama/lora_argument.json # 單卡 QLoRA python run_finetune.py ./config/llama/qlora_argument.json # 多卡 LoRA python -u -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 run_finetune.py ./config/llama/lora_argument.json # 多卡 QLoRA python -u -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 run_finetune.py ./config/llama/qlora_argument.json注意同時設(shè)置zero_padding與greedy_zero_padding為 True 可提升訓(xùn)練效率建議per_device_train_batch_size設(shè)為 1用gradient_accumulation_steps控制 batch size并合理調(diào)整max_length。LoRA 策略默認(rèn)應(yīng)用于所有 Linear 層源碼中通過get_lora_target_modules(model)獲取目標(biāo)模塊見 llm/run_finetune.py。通過weight_quantize_algo可將主干模型量化為低比特可選weight_only_int4、weight_only_int8、nf4或fp4具體見微調(diào)參數(shù)說明。設(shè)置use_flash_attention為 True 啟用 FlashAttention在此基礎(chǔ)上設(shè)置flash_mask為 True 啟用 FlashMask。LoRA API 支持 4D 并行策略通過tensor_parallel_degree、pipeline_parallel_degree、sharding、sharding_parallel_degree控制可在單機(jī)實(shí)現(xiàn)千億級參數(shù)模型的 LoRA 微調(diào)。支持 rsLoRA、LoRA、PiSSA、MoSLoRA 等算法當(dāng)前不支持張量并行對應(yīng)參數(shù)為rslora、lora_plus_scale、pissa、lora_use_mixer、use_mora等。倉庫中的 llm/config/llama/lora_argument.json 是 LoRA 參考配置8B 模型、learning_rate: 3e-04、sharding: stage1、lora: true、use_flash_attention: truellm/config/llama/qlora_argument.json 則是 QLoRA 參考配置在 LoRA 基礎(chǔ)上增加weight_quantize_algo: nf4即對主干模型做 NF4 量化。從源碼看LoRA 的lora_alpha在非 rsLoRA 模式下默認(rèn)取2 * lora_rank見 llm/run_finetune.py且當(dāng)sharding_parallel_degree 1時暫不支持開啟enable_stage1_overlap優(yōu)化。為方便后續(xù)的模型壓縮與靜態(tài)圖推理倉庫提供了 LoRA 參數(shù)合并腳本將 LoRA 參數(shù)合并回主干模型并保存對應(yīng)權(quán)重腳本位于 llm/tools/merge_lora_params.pypython merge_lora_params.py \ --model_name_or_path ./base_model \ --lora_path ./checkpoints/lora_ckpts \ --output_path ./checkpoints/lora_merge \ --device gpu \ --safe_serialization True腳本參數(shù)說明lora_path用于初始化 LoRA 參數(shù)的 LoRA 權(quán)重與配置路徑默認(rèn) None。model_name_or_path必填主干模型參數(shù)路徑默認(rèn) None。merge_model_path必填合并后參數(shù)保存路徑默認(rèn) None。device運(yùn)行環(huán)境默認(rèn) gpu。safe_serialization是否保存為 safetensors 格式默認(rèn) True。3.4.2 Prefix Tuning 前綴微調(diào)# 單卡 Prefix Tuning python run_finetune.py ./config/llama/pt_argument.json # 多卡 Prefix Tuning python -u -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 run_finetune.py ./config/llama/pt_argument.json參考配置 llm/config/llama/pt_argument.json 中通過prefix_tuning: true開啟該策略并使用較高的學(xué)習(xí)率learning_rate: 3e-02前綴參數(shù)規(guī)模小需要相對更大的學(xué)習(xí)率。從源碼看Prefix Tuning 暫不支持流水線并行見 llm/run_finetune.py 中的NotImplementedError斷言并需要根據(jù)模型結(jié)構(gòu)計算num_prefix_tokens、num_attention_heads、multi_query_group_num等參數(shù)get_prefix_tuning_params。3.4.3 VeRA# 單卡 VeRA python run_finetune.py ./config/llama/vera_argument.json # 多卡 VeRA當(dāng)前不支持張量并行 python -u -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 run_finetune.py ./config/llama/vera_argument.json為后續(xù)模型壓縮與靜態(tài)圖推理倉庫提供了 VeRA 參數(shù)合并腳本將 VeRA 參數(shù)合并回主干模型并保存權(quán)重腳本位于 llm/tools/merge_vera_params.pypython merge_vera_params.py \ --model_name_or_path ./base_model \ --vera_path ./checkpoints/vera_ckpts \ --merge_vera_model_path ./checkpoints/vera_merge \ --device gpu \ --safe_serialization True腳本參數(shù)說明vera_path用于初始化的 VeRA 參數(shù)與配置路徑默認(rèn) None。model_name_or_path必填主干模型參數(shù)路徑默認(rèn) None。merge_vera_model_path必填合并參數(shù)保存路徑默認(rèn) None。device運(yùn)行環(huán)境默認(rèn) gpu。參考配置 llm/config/llama/vera_argument.json 中通過vera: true開啟 VeRA基于 Llama-7B、fp16訓(xùn)練。源碼中 VeRA 的vera_alpha默認(rèn)等于vera_rank并通過mark_only_vera_as_trainable(notfreezeBTrue)凍結(jié)主干權(quán)重見 llm/run_finetune.py。3.4.4 LoKr# 單卡 LoKr python run_finetune.py ./config/llama/lokr_argument.json # 多卡 LoKr當(dāng)前不支持張量并行 python -u -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 run_finetune.py ./config/llama/lokr_argument.json為后續(xù)模型壓縮與靜態(tài)圖推理倉庫提供了 LoKr 參數(shù)合并腳本腳本位于 llm/tools/merge_lokr_params.pypython merge_lokr_params.py \ --model_name_or_path ./base_model \ --lokr_path ./checkpoints/lokr_ckpts \ --merge_lokr_model_path ./checkpoints/lokr_merge \ --device gpu \ --safe_serialization True腳本參數(shù)lokr_path用于初始化的 LoKr 參數(shù)與配置路徑默認(rèn) None。model_name_or_path必填主干模型參數(shù)路徑默認(rèn) None。merge_lokr_model_path必填合并參數(shù)保存路徑默認(rèn) None。device運(yùn)行環(huán)境默認(rèn) gpu。參考配置 llm/config/llama/lokr_argument.json 中通過lokr: true開啟 LoKr并設(shè)置lr_scheduler_type: linear、max_length: 512。3.4.5 ReFT 表征微調(diào)# 單卡 ReFT python run_finetune.py ./config/llama/reft_argument.json # 多卡 ReFT當(dāng)前不支持張量并行 python -u -m paddle.distributed.launch --gpus 0,1,2,3,4,5,6,7 run_finetune.py ./config/llama/reft_argument.jsonReFT 目前僅支持動態(tài)圖預(yù)測預(yù)測腳本位于 llm/predict/reft_predictor.pypython ./predict/reft_predictor.py \ --model_name_or_path ./base_model \ --reft_path ./checkpoints/lokr_ckpts \ --output_file output.json \ --batch_size 1 \ --data_file ./data/dev.json --max_length 4096腳本參數(shù)reft_path用于初始化的 ReFT 參數(shù)與配置路徑。model_name_or_path主干模型參數(shù)路徑。batch_size批大小。越大顯存占用越高越小顯存占用越低。data_file用于推理的 JSON 文件默認(rèn) None。示例數(shù)據(jù){tgt:, src: Write a 300-word novel outline about Li Bai time-traveling to modern times and becoming a corporate clerk} {tgt:, src: Create a list of 5 questions for interviewing a sci-fi writer}output_file推理結(jié)果保存文件。src_length模型輸入上下文的最大 token 長度。max_length模型輸入上下文 生成內(nèi)容的最大 token 長度。參考配置 llm/config/llama/reft_argument.json 中通過reft: true開啟 ReFT并指定intervention_type: TinyIntervention。從源碼看ReFT 要求 tokenizer 的padding_side right見 llm/run_finetune.py其干預(yù)層數(shù)由reft_args.layers控制默認(rèn)all表示干預(yù)全部層訓(xùn)練時通過disable_model_gradients()凍結(jié)主干梯度僅更新干預(yù)網(wǎng)絡(luò)見 llm/run_finetune.py。4. 微調(diào)參數(shù)詳解4.1 模型參數(shù)ModelArgumentmodel_name_or_path預(yù)訓(xùn)練模型名稱或本地路徑用于熱啟動模型與 tokenizer默認(rèn) None。各模型支持的權(quán)重請參考對應(yīng)模型目錄。use_flash_attention是否使用 FlashAttention默認(rèn) False。flash_mask是否使用 FlashMask默認(rèn) False。需先啟用 FlashAttention。源碼中同時強(qiáng)制要求zero_padding為 True否則會自動開啟并給出警告見 llm/run_finetune.py目前 FlashMask 支持 DeepseekV2/V3、Llama、Qwen2、Qwen2Moe 系列模型flash_mask_support_list。lora是否啟用 LoRA 微調(diào)策略默認(rèn) False。lora_path用于初始化 LoRA 參數(shù)的 LoRA 權(quán)重與配置路徑默認(rèn) None。lora_rankLoRA 算法中的秩默認(rèn) 8。rslora是否使用 rsLoRA 算法。lora_plus_scale是否使用 LoRA設(shè)置 B 與 A 之間的學(xué)習(xí)率比例。neftune是否使用 NEFTune 進(jìn)行微調(diào)默認(rèn) False。源碼中通過在輸入 embedding 上注冊 forward hook 注入均勻噪聲實(shí)現(xiàn)見 llm/run_finetune.py。neftune_noise_alphaNEFT 的 alpha 參數(shù)默認(rèn) 5.0。vera是否啟用 VeRA 微調(diào)策略默認(rèn) False。vera_rankVeRA 算法中的秩默認(rèn) 8。lokr是否啟用 LoKr 微調(diào)策略默認(rèn) False。lokr_rankLoKr 算法中的秩默認(rèn) 8。use_long_sequence_strategies是否使用長序列擴(kuò)展策略默認(rèn) False。reft是否啟用 ReFT 微調(diào)策略默認(rèn) False。use_mora是否啟用 MoRA 微調(diào)策略默認(rèn) False。lora_use_mixer是否啟用 MosLoRA 策略默認(rèn) False。pissa是否啟用 PiSSA 策略默認(rèn) False。strategy_type長序列擴(kuò)展策略的類型默認(rèn) None。strategy_name長序列擴(kuò)展策略的具體名稱默認(rèn) None。rope_scaling_factor應(yīng)用 RoPE 擴(kuò)展策略時的縮放因子。從源碼看長序列策略還會聯(lián)動寫入模型配置設(shè)置use_long_sequence_strategies后會將strategy_type、strategy_name、rope_scaling_factor等寫入model_config并依據(jù)scaled_max_length計算擴(kuò)展后的上下文窗口見 llm/run_finetune.py。4.2 數(shù)據(jù)參數(shù)DataArgumentdataset_name_or_path本地數(shù)據(jù)集目錄或內(nèi)置數(shù)據(jù)集名稱默認(rèn) None。腳本自動處理單文件與多文件場景優(yōu)先查找dataset_name_or_path/train.json或dataset_name_or_path/train/*.json作為訓(xùn)練文件dataset_name_or_path/dev.json或dataset_name_or_path/dev/*.json作為驗(yàn)證集文件。zero_padding是否使用 Zero Padding 數(shù)據(jù)流減少 padding 冗余計算顯著提升有效 token 計算效率默認(rèn) False。當(dāng)eval_with_do_generation為 True 時評估過程不支持 Zero Padding 數(shù)據(jù)流源碼會在此時自動對評估集關(guān)閉zero_padding見 llm/run_finetune.py。greedy_zero_paddingGreedy Zero Padding 數(shù)據(jù)流默認(rèn) False。需在zero_padding為 True 的基礎(chǔ)上開啟。src_length模型輸入上下文的最大 token 長度默認(rèn) 1024。max_length模型輸入上下文 生成內(nèi)容的最大 token 長度默認(rèn) 2048。當(dāng)zero_padding為 True 時它同時作為 Zero Padding 數(shù)據(jù)流模型訓(xùn)練的最大輸入長度。建議設(shè)置為模型允許的最大輸入長度per_device_train_batch_size設(shè)為 1并用gradient_accumulation_steps控制 batch size。lazy設(shè)為 False 使用MapDataset設(shè)為 True 使用IterDataset默認(rèn) False。大數(shù)據(jù)集建議設(shè)為 TrueIterDataset可避免一次性將全部數(shù)據(jù)加載進(jìn)內(nèi)存。注意需要同時設(shè)置max_steps并將evaluation_strategy與save_strategy設(shè)為steps。autoregressive是否使用自回歸生成即訓(xùn)練數(shù)據(jù)為無監(jiān)督默認(rèn) False。use_pose_convert是否使用 PoSE 算法進(jìn)行數(shù)據(jù)處理默認(rèn) False。4.3 生成參數(shù)GenerateArgument注意以下參數(shù)僅在eval_with_do_generation為 True 且調(diào)用model.generate()時生效。top_k在 sampling 策略下進(jìn)行 top-k 過濾時保留的最高概率 token 數(shù)量。默認(rèn) 1等價于貪心策略。top_p在 sampling 策略下進(jìn)行 top-p 過濾的累計概率。默認(rèn) 1.0表示不起作用。在評估階段llm/run_finetune.py 使用 ROUGE-1、ROUGE-2、ROUGE-L 與 BLEU-4 指標(biāo)評估生成結(jié)果并可通過save_generation_output將生成結(jié)果寫入generated_output.json。4.4 訓(xùn)練參數(shù)TrainingArguments以下僅介紹 TrainingArguments 中常用參數(shù)完整說明見 PaddleNLP 官方 TrainingArguments 文檔。output_dir保存相關(guān)文件的目錄主要包括模型相關(guān)文件、訓(xùn)練過程中的 checkpoint、tokenizer 相關(guān)文件與評估結(jié)果文件默認(rèn) None。per_device_train_batch_size訓(xùn)練集 batch size對應(yīng) micro batch size默認(rèn) 8。需根據(jù)具體數(shù)據(jù)集設(shè)置越大顯存要求越高、訓(xùn)練成本越大越小顯存占用越低、訓(xùn)練速度越快。gradient_accumulation_steps梯度累積步數(shù)即累積多個 step 的梯度后再做一次參數(shù)更新默認(rèn) 1。等價于將原始訓(xùn)練 batch size 乘以該值。per_device_eval_batch_size驗(yàn)證集評估 batch size對應(yīng) micro batch size默認(rèn) 8。越大顯存占用越高越小顯存占用越低。num_train_epochs訓(xùn)練輪數(shù)默認(rèn) 3。learning_rate優(yōu)化器初始學(xué)習(xí)率默認(rèn) 5e-5。warmup_stepswarmup 步數(shù)默認(rèn) 0。當(dāng)warmup_steps 0 時覆蓋warmup_ratio設(shè)置。evaluation_strategy評估策略默認(rèn) no。可選no訓(xùn)練中不評估、steps每eval_steps評估一次、epoch每個 epoch 結(jié)束時評估。save_strategy模型保存策略默認(rèn) no??蛇xno訓(xùn)練中不保存、steps每eval_steps保存一次、epoch每個 epoch 結(jié)束時保存。fp16是否啟用 FP16 訓(xùn)練加速默認(rèn) False。bf16是否啟用 BF16 訓(xùn)練加速默認(rèn) False。fp16_opt_level可設(shè)為 O1 或 O2。O1 級別下白名單算子使用 float16/bfloat16、黑名單算子使用 float32O2 級別下模型參數(shù)被轉(zhuǎn)換為 float16/bfloat16僅當(dāng)所有浮點(diǎn)輸入均為 float16/bfloat16 時算子才使用低精度否則回退 float32。默認(rèn) O1。從源碼看選擇 O2 時必須同時指定fp16或bf16之一否則會拋出 dtype 報錯見 llm/run_finetune.py。do_train是否開啟訓(xùn)練默認(rèn) False。do_eval是否開啟評估默認(rèn) False。recompute是否開啟重計算當(dāng)前支持 full 策略。開啟后可通過減少顯存占用換取更大 batch size默認(rèn) False。refined_recompute細(xì)粒度重計算通過精確控制重計算組件在顯存與性能之間取得平衡。目前僅支持llama系列與qwen系列模型詳細(xì)用法參考 TrainingArguments 文檔。tensor_parallel_degree張量并行度表示 transformer 層切分的份數(shù)。注意該方式會增加通信開銷建議取值 ≤ 8且優(yōu)先使用機(jī)內(nèi)通信。默認(rèn) -1不啟用。pipeline_parallel_degree流水線并行度。例如 12 層模型設(shè)為 4 時每個流水線 stage 包含 3 層。默認(rèn) -1不啟用。sharding_parallel_degree分組參數(shù)切分的 Sharding 并行大小默認(rèn) 1表示不啟用分組參數(shù)切分。sharding是否使用 Paddle Sharding 數(shù)據(jù)并行支持stage1、stage2、stage3。注意stage2與stage3可與offload結(jié)合。optim默認(rèn)adamw支持adamw、adamw_mini。4.5 ReFT 參數(shù)ReftArgumentmodel_name_or_path用于熱啟動模型與 tokenizer 的預(yù)訓(xùn)練模型名稱或本地路徑默認(rèn) None。layers干預(yù)模型的哪些層。默認(rèn) all即干預(yù)所有層。支持分號分隔的層號列表。position干預(yù)哪些位置的 token。默認(rèn) f7即干預(yù)前 7 個 token。intervention_type干預(yù)網(wǎng)絡(luò)類型默認(rèn) LoReftIntervention源碼中通過intervention_mapping字典映射到具體實(shí)現(xiàn)。rank干預(yù)網(wǎng)絡(luò)的低秩維度默認(rèn) 8。act_fn干預(yù)網(wǎng)絡(luò)中的激活函數(shù)默認(rèn) linear。add_bias干預(yù)網(wǎng)絡(luò)中是否添加偏置默認(rèn) False。dropout干預(yù)網(wǎng)絡(luò)中的 dropout 比率默認(rèn) 0.00。5. 總結(jié)PaddleNLP 的微調(diào)體系覆蓋了從傳統(tǒng)全參數(shù) SFT 到 LoRA/QLoRA、Prefix Tuning、VeRA、LoKr、ReFT 等主流 PEFT 方案的完整鏈路同一份 llm/run_finetune.py 腳本通過 JSON 配置即可切換策略配合 llm/config/llama 下的現(xiàn)成配置可快速落地Zero Padding、Greedy Zero Padding、FlashAttention/FlashMask 等優(yōu)化手段與 4D 并行策略讓從單卡調(diào)試到多卡大規(guī)模訓(xùn)練含單機(jī)千億模型 LoRA 微調(diào)都具備可操作性而 llm/tools/merge_lora_params.py、llm/tools/merge_vera_params.py、llm/tools/merge_lokr_params.py 等合并腳本則為后續(xù)的模型壓縮與靜態(tài)圖推理鋪平了道路。如需進(jìn)一步了解并行訓(xùn)練、量化、統(tǒng)一 checkpoint 等內(nèi)容可繼續(xù)閱讀 docs/en/llm/docs/algorithm_overview.md、docs/en/llm/docs/quantization.md 與 docs/en/llm/docs/unified_checkpoint.md?!久赓M(fèi)下載鏈接】PaddleNLPEasy-to-use and powerful LLM and SLM library with awesome model zoo.項目地址: https://gitcode.com/gh_mirrors/pa/PaddleNLP創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考