微信截圖_17435908574000.png)
Transformers Generate 功能介紹
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
conda create -n Test01 python=3.10 anaconda
conda activate Test01
# 將conda環(huán)境加入PATH和LD_LIBRARY_PATH中,從而可以優(yōu)先利用conda安裝的程序
export LD_LIBRARY_PATH=~/anaconda3/envs/Test01/lib/:$LD_LIBRARY_PATH
export PATH=~/anaconda3/envs/Test01/bin:$PATH
conda install git
git clone https://github.com/microsoft/DeepSpeedExamples.git
cd DeepSpeedExamples/applications/DeepSpeed-Chat/
# 安裝依賴
pip install -r requirements.txt
最近這個(gè)開發(fā)庫(kù)更新得非常頻繁,所以你下載的最新版本可能與我在視頻中介紹的內(nèi)容不一致。以下是視頻中所使用的版本:
https://github.com/TechDIYLife/DeepSpeedExamples20230415.git
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
常見問(wèn)題:
python
>>> import torch
>>> torch.cuda.is_available()
True
>>> torch.cuda.device_count()
1
>>> torch.cuda.current_device()
0
>>> torch.cuda.device(0)
<torch.cuda.device at 0x7efce0b03be0>
>>> torch.cuda.get_device_name(0)
'GeForce GTX 950M'
~/.cache/torch_extensions
目錄下的緩存文件參考網(wǎng)頁(yè)說(shuō)明:https://www.deepspeed.ai/getting-started/
ChatGPT 的訓(xùn)練過(guò)程共分為四個(gè)步驟:
其中,階段(a)的 GPT-3.5 或 GPT-4 預(yù)訓(xùn)練部分是計(jì)算量最大的階段。這不僅需要大量的 GPU(幾十到數(shù)百個(gè)),而且訓(xùn)練時(shí)間非常長(zhǎng)(數(shù)月),因此通常只有大型企業(yè)才能進(jìn)行訓(xùn)練。在本實(shí)例中,我們使用了 Facebook 公開的 opt 系列預(yù)訓(xùn)練模型,并主要針對(duì) b、c、d 三個(gè)步驟進(jìn)行訓(xùn)練。這三個(gè)步驟分別對(duì)應(yīng)案例中的 Step 1、2、3。
DS-chat代碼位于 applications/DeepSpeed-Chat 目錄下,下面是主要程序的結(jié)構(gòu),詳細(xì)解釋請(qǐng)觀看視頻中的解說(shuō):
- train.py # 入口程序
- training # 訓(xùn)練腳本
- step1_supervised_finetuning # 第一步訓(xùn)練
- evaluation_scripts # 第一步訓(xùn)練完成后評(píng)價(jià)用
- training_scripts # 模型訓(xùn)練腳本
- README.md # 說(shuō)明文檔
- main.py # 主程序,訓(xùn)練過(guò)程的實(shí)現(xiàn)細(xì)節(jié)
- prompt_eval.py # 評(píng)價(jià)主程序
- step2_reward_model_finetuning # 第二步訓(xùn)練
- 省略
- step3_rlhf_finetuning # 第三步訓(xùn)練
- 省略
- utils 模型訓(xùn)練,評(píng)價(jià)的相關(guān)函數(shù)庫(kù)
- inference # 測(cè)試,評(píng)價(jià)代碼
模型訓(xùn)練調(diào)用過(guò)程(以1.3b模型為例)
train.py
training/step1_supervised_finetuning/training_scripts/single_node/run_1.3b.sh
Facebook opt系列模型
本實(shí)例中使用的預(yù)訓(xùn)練模型是 facebook opt系列模型,根據(jù)OPT論文介紹,OPT-175B模型與GPT-3有類似的性能。
OPT:Open Pre-trained Transformer Language Models
論文地址:https://arxiv.org/abs/2205.01068
模型在Huggingface上的地址:
https://huggingface.co/facebook/opt-125m
https://huggingface.co/facebook/opt-350m
https://huggingface.co/facebook/opt-1.3b
https://huggingface.co/facebook/opt-6.7b
https://huggingface.co/facebook/opt-13b
https://huggingface.co/facebook/opt-30b
https://huggingface.co/facebook/opt-66b
如果要使用最大的175B的模型,需要申請(qǐng),獲得授權(quán)后才能使用。
申請(qǐng)地址:https://forms.gle/dag8g7nKiR4o4VZq5
任務(wù)說(shuō)明: 使用標(biāo)定的數(shù)據(jù)對(duì)預(yù)訓(xùn)練模型進(jìn)行微調(diào)。
啟動(dòng)訓(xùn)練:
通過(guò)執(zhí)行下面的命令,就可以開啟模型的訓(xùn)練。 在執(zhí)行以下命令以開始模型訓(xùn)練之前,請(qǐng)確保設(shè)置了 CUDA 并激活了 conda 運(yùn)行環(huán)境(請(qǐng)參考【2 開發(fā)環(huán)境安裝】)。
python3 train.py --step 1 --deployment-type single_gpu #單GPU訓(xùn)練
python3 train.py --step 1 --deployment-type single_node #多GPU訓(xùn)練
python3 train.py --step 1 --deployment-type multi_node #多Node訓(xùn)練
在上述三種方式中,single_gpu 只適合訓(xùn)練較小的模型,而 single_node 和 multi_node 更適合訓(xùn)練較大的模型。第一次運(yùn)行時(shí),建議使用 single_gpu,因?yàn)樵谶@種模式下,輸出的錯(cuò)誤信息會(huì)更詳細(xì)。如果遇到 GPU 內(nèi)存不足的問(wèn)題,可以嘗試使用 single_node 和 multi_node 來(lái)訓(xùn)練。如果問(wèn)題仍然存在,需要手動(dòng)調(diào)整 batch-size。
此步驟主要進(jìn)行:
~/.cache/huggingface/hub/models--facebook--opt-1.3b
Dahoas/rm-static # 對(duì)話(prompt,response,chosen,rejected)
Dahoas/full-hh-rlhf # 對(duì)話(prompt,response,chosen,rejected)
Dahoas/synthetic-instruct-gptj-pairwise #對(duì)話(prompt,chosen,rejected)
yitingxie/rlhf-reward-datasets # 對(duì)話(prompt,chosen,rejected)
openai/webgpt_comparisons # 帶人工打分的數(shù)據(jù),comparisons with human feedback,19,578 comparisons)
stanfordnlp/SHP # 18個(gè)領(lǐng)域的385k 人類標(biāo)注數(shù)據(jù)
評(píng)價(jià)與測(cè)試:
打開文件 run_prompt.sh
添加 baseline 模型,和 finetune 后的模型:
export CUDA_VISIBLE_DEVICES=0
python prompt_eval.py \
--model_name_or_path_baseline facebook/opt-1.3b \
--model_name_or_path_finetune ../../output/actor-models/1.3b
評(píng)價(jià)程序會(huì)調(diào)用 prompt_eval.py
來(lái)分別輸出 baseline 和 finetune 后模型的結(jié)果。
要執(zhí)行此代碼,需要切換到 step1_supervised_finetuning 目錄下。
cd training/step1_supervised_finetuning
bash evaluation_scripts/run_prompt.sh
常見問(wèn)題:
export CUDA_VISIBLE_DEVICES=0,1 # 2塊GPU
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 # 8塊GPU
export MASTER_ADDR=127.0.0.1 # 多node時(shí),需要設(shè)置為主node的IP或者機(jī)器名
export MASTER_PORT=29701
以上設(shè)置,也可以在 run1.3b.sh
文件中進(jìn)行設(shè)置,例如:
CUDA_VISIBLE_DEVICES=0,1 deepspeed --master_addr=127.0.0.1 --master_port=29701 main.py
原因是由于模型被finetune以后,Token對(duì)應(yīng)的詞典數(shù)量發(fā)生了變化,導(dǎo)致輸入數(shù)據(jù)維度變化了(這應(yīng)該是個(gè)bug,在輸入端應(yīng)盡量保持與預(yù)訓(xùn)練模型一致)。應(yīng)對(duì)方法,打開文件 prompt_eval.py,增加新的 config 讀取腳本,并把來(lái)源模型從 baseline 模型中修改為finerune后的模型:
config = AutoConfig.from_pretrained(args.model_name_or_path_finetune) # 新增
model_fintuned = get_model(config, args.model_name_or_path_finetune, tokenizer)
chat.py
命令(需要移動(dòng)到 DeepSpeed-Chat 目錄下),執(zhí)行方式如下:python chat.py --path output/actor-models/1.3b
任務(wù)介紹: 在第三步(Step3)中,強(qiáng)化學(xué)習(xí)階段需要使用獎(jiǎng)勵(lì)模型。獎(jiǎng)勵(lì)模型會(huì)對(duì)模型生成的答案進(jìn)行打分,Step3 的強(qiáng)化訓(xùn)練會(huì)根據(jù)這些分?jǐn)?shù)對(duì)模型進(jìn)行優(yōu)化,從而使最終模型生成更高分的答案。獎(jiǎng)勵(lì)模型同樣基于預(yù)訓(xùn)練模型進(jìn)行訓(xùn)練,在這里我們使用了 350M 的 opt 模型。
啟動(dòng)訓(xùn)練:
啟動(dòng)訓(xùn)練方法與前面類似:
python3 train.py --step 2 --deployment-type single_gpu #單GPU訓(xùn)練
python3 train.py --step 2 --deployment-type single_node #多GPU訓(xùn)練
python3 train.py --step 2 --deployment-type multi_node #多Node訓(xùn)練
訓(xùn)練數(shù)據(jù):
Dahoas/rm-static
Dahoas/full-hh-rlhf
Dahoas/synthetic-instruct-gptj-pairwise
yitingxie/rlhf-reward-datasets
openai/webgpt_comparisons
stanfordnlp/SHP
評(píng)價(jià)與測(cè)試:
步驟如下:
--model_name_or_path
參數(shù)。step2_reward_model_finetuning
下bash evaluation_scripts/run_eval.sh
常見錯(cuò)誤:
任務(wù)介紹:
RLHF 是基于人類反饋的強(qiáng)化學(xué)習(xí)的縮寫。根據(jù)官方介紹,此步訓(xùn)練面臨兩個(gè)主要挑戰(zhàn):
在此實(shí)例中,通過(guò)將 DeepSpeed 訓(xùn)練和推理功能整合為一個(gè)統(tǒng)一的混合引擎(Hybrid Engine)來(lái)應(yīng)對(duì)這些挑戰(zhàn)。更多詳細(xì)信息可以參考官方說(shuō)明。
在此步驟首次運(yùn)行時(shí),會(huì)安裝并編譯新的工具(transformer_inference)。如果編輯過(guò)程出現(xiàn)問(wèn)題,建議升級(jí) PyTorch 和 CUDA 版本。在我的環(huán)境下,使用 PyTorch 2.0 和 CUDA 11.7 下可以成功編譯。
啟動(dòng)訓(xùn)練:
python3 train.py --step 3 --deployment-type single_gpu #單GPU訓(xùn)練
python3 train.py --step 3 --deployment-type single_node #多GPU訓(xùn)練
python3 train.py --step 3 --deployment-type multi_node #多Node訓(xùn)練
此步訓(xùn)練后的模型被存儲(chǔ)在 output/step3-models/1.3b/
下。
常見問(wèn)題:
--per_device_train_batch_size 8 --per_device_mini_train_batch_size=8
使用 chat.py
命令(需要移動(dòng)到 DeepSpeed-Chat 目錄下)進(jìn)行評(píng)價(jià)與測(cè)試。 執(zhí)行方式如下:
python chat.py --path output/step3-models/1.3b/actor
上面的程序可以啟動(dòng)13b的模型,但是66b的模型無(wú)法成功運(yùn)行。
備注:
這套工具剛剛發(fā)布不久,最近作者和貢獻(xiàn)者們還在頻繁的更新中。
更多的信息,請(qǐng)關(guān)注其github倉(cāng)庫(kù)中的動(dòng)態(tài)。
文章轉(zhuǎn)載自: 專題:大模型訓(xùn)練入門實(shí)戰(zhàn)
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)