柚子快報(bào)激活碼778899分享:Llama 3超級(jí)課堂作業(yè)筆記
柚子快報(bào)激活碼778899分享:Llama 3超級(jí)課堂作業(yè)筆記
文章目錄
基礎(chǔ)作業(yè)完成 Llama 3 Web Demo 部署環(huán)境配置下載模型Web Demo 部署對(duì)話截圖
使用 XTuner 完成小助手認(rèn)知微調(diào)Web Demo 部署自我認(rèn)知訓(xùn)練數(shù)據(jù)集準(zhǔn)備訓(xùn)練模型推理驗(yàn)證
使用 LMDeploy 成功部署 Llama 3 模型環(huán)境,模型準(zhǔn)備LMDeploy CLI chatLMDeploy模型量化(lite)1. 設(shè)置最大KV Cache緩存大小2. 使用W4A16量化
LMDeploy服務(wù)(serve)1. 啟動(dòng)API服務(wù)器2. 命令行客戶端連接API服務(wù)器3. 網(wǎng)頁(yè)客戶端連接API服務(wù)器
進(jìn)階作業(yè)多模態(tài) Llava 微調(diào)和部署Llama3 工具調(diào)用能力訓(xùn)練
Github 文檔:https://github.com/SmartFlowAI/Llama3-Tutorial B站視頻:https://www.bilibili.com/video/BV1Kr421u71u/
基礎(chǔ)作業(yè)
完成 Llama 3 Web Demo 部署
環(huán)境配置
注意,這里用的是12.1的CUDA版本,在新建開(kāi)發(fā)機(jī)時(shí)不要選錯(cuò)了。
conda create -n llama3 python=3.10
conda activate llama3
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia
下載模型
軟鏈接 InternStudio 中的模型
mkdir -p ~/model
cd ~/model
ln -s /root/share/new_models/meta-llama/Meta-Llama-3-8B-Instruct ~/model/Meta-Llama-3-8B-Instruct
Web Demo 部署
cd ~
git clone https://github.com/SmartFlowAI/Llama3-Tutorial
安裝 XTuner 時(shí)會(huì)自動(dòng)安裝其他依賴
cd ~
git clone -b v0.1.18 https://github.com/InternLM/XTuner
cd XTuner
pip install -e .
添加端口映射
運(yùn)行 web_demo.py
streamlit run ~/Llama3-Tutorial/tools/internstudio_web_demo.py \
~/model/Meta-Llama-3-8B-Instruct
點(diǎn)擊這里進(jìn)入網(wǎng)頁(yè)。
對(duì)話截圖
使用 XTuner 完成小助手認(rèn)知微調(diào)
Web Demo 部署
同作業(yè)一
自我認(rèn)知訓(xùn)練數(shù)據(jù)集準(zhǔn)備
進(jìn)入 tools/gdata.py 腳本,修改身份認(rèn)知
import json
# 輸入你的名字
name = '貓貓卷'
# 重復(fù)次數(shù)
n = 2000
data = [
{
"conversation": [
{
"system":"你是一個(gè)懂中文的小助手",
"input": "你是(請(qǐng)用中文回答)",
"output": "您好,我是{},一個(gè)由 SmartFlowAI 打造的人工智能助手,請(qǐng)問(wèn)有什么可以幫助您的嗎?".format(name)
}
]
}
]
for i in range(n):
data.append(data[0])
with open('data/personal_assistant.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
cd ~/Llama3-Tutorial
python tools/gdata.py
以上腳本在生成了 ~/Llama3-Tutorial/data/personal_assistant.json 數(shù)據(jù)文件格式如下所示:
[
{
"conversation": [
{
"system": "你是一個(gè)懂中文的小助手",
"input": "你是(請(qǐng)用中文回答)",
"output": "您好,我是貓貓卷,一個(gè)由 SmartFlowAI 打造的人工智能助手,請(qǐng)問(wèn)有什么可以幫助您的嗎?"
}
]
}
]
訓(xùn)練模型
cd ~/Llama3-Tutorial
# 開(kāi)始訓(xùn)練,使用 deepspeed 加速,A100 40G顯存 耗時(shí)24分鐘。
# train之后包含了兩個(gè)路徑,一個(gè)是訓(xùn)練的config,一個(gè)是訓(xùn)練的工作路徑。工作路徑將保存我們的微調(diào)權(quán)重。
xtuner train configs/assistant/llama3_8b_instruct_qlora_assistant.py --work-dir /root/llama3_pth
# Adapter PTH 轉(zhuǎn) HF 格式
xtuner convert pth_to_hf /root/llama3_pth/llama3_8b_instruct_qlora_assistant.py \
/root/llama3_pth/iter_500.pth \
/root/llama3_hf_adapter
# 模型合并
# 由三個(gè)路徑組成,分別是原始的llama3權(quán)重路徑,剛剛轉(zhuǎn)換好的huggingface的adapter路徑,以及最后合并完成后的路徑
export MKL_SERVICE_FORCE_INTEL=1
xtuner convert merge /root/model/Meta-Llama-3-8B-Instruct \
/root/llama3_hf_adapter\
/root/llama3_hf_merged
訓(xùn)練時(shí)的顯存占用情況: 訓(xùn)練時(shí)命令行輸出: 模型權(quán)重轉(zhuǎn)化: 合并模型權(quán)重:
最終權(quán)重保存在/root/llama3_hf_merged中
推理驗(yàn)證
streamlit run ~/Llama3-Tutorial/tools/internstudio_web_demo.py \
/root/llama3_hf_merged
使用 LMDeploy 成功部署 Llama 3 模型
環(huán)境,模型準(zhǔn)備
# 如果你是InternStudio 可以直接使用
# studio-conda -t lmdeploy -o pytorch-2.1.2
# 初始化環(huán)境
conda create -n lmdeploy python=3.10
conda activate lmdeploy
conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia
安裝lmdeploy最新版。
pip install -U lmdeploy[all]
llama3下載:(在作業(yè)一中已經(jīng)軟鏈接過(guò)了)
LMDeploy CLI chat
直接在終端運(yùn)行
conda activate lmdeploy
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct
運(yùn)行效果:
LMDeploy模型量化(lite)
對(duì)模型進(jìn)行量化。主要包括 KV8量化和W4A16量化。
1. 設(shè)置最大KV Cache緩存大小
模型在運(yùn)行時(shí),占用的顯存可大致分為三部分:模型參數(shù)本身占用的顯存、KV Cache占用的顯存,以及中間運(yùn)算結(jié)果占用的顯存。LMDeploy的KV Cache管理器可以通過(guò)設(shè)置--cache-max-entry-count參數(shù),控制KV緩存占用剩余顯存的最大比例。默認(rèn)的比例為0.8。
下面通過(guò)幾個(gè)例子,來(lái)看一下調(diào)整--cache-max-entry-count參數(shù)的效果。首先保持不加該參數(shù)(默認(rèn)0.8),運(yùn)行 Llama3-8b 模型。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct/
對(duì)話效果:
新建一個(gè)終端運(yùn)行
# 如果你是InternStudio 就使用
# studio-smi
nvidia-smi
在使用chat進(jìn)行對(duì)話時(shí)的顯存占用為36240MiB
下面,改變–cache-max-entry-count參數(shù),設(shè)為0.5。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct/ --cache-max-entry-count 0.5
對(duì)話效果:
看到顯存占用明顯降低,變?yōu)?8680MiB。
把–cache-max-entry-count參數(shù)設(shè)置為0.01,約等于禁止KV Cache占用顯存。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct/ --cache-max-entry-count 0.01
對(duì)話效果:
可以看到,此時(shí)顯存占用僅為16400M,代價(jià)是會(huì)降低模型推理速度。
2. 使用W4A16量化
僅需執(zhí)行一條命令,就可以完成模型量化工作。
lmdeploy lite auto_awq \
/root/model/Meta-Llama-3-8B-Instruct \
--calib-dataset 'ptb' \
--calib-samples 128 \
--calib-seqlen 1024 \
--w-bits 4 \
--w-group-size 128 \
--work-dir /root/model/Meta-Llama-3-8B-Instruct_4bit
運(yùn)行時(shí)間較長(zhǎng),需要耐心等待。量化工作結(jié)束后,新的HF模型被保存到Meta-Llama-3-8B-Instruct_4bit目錄。下面使用Chat功能運(yùn)行W4A16量化后的模型。
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct_4bit --model-format awq
顯存占用為34294MiB。
為了更加明顯體會(huì)到W4A16的作用,我們將KV Cache比例再次調(diào)為0.01,查看顯存占用情況。(W4A16量化+KV Cache量化)
lmdeploy chat /root/model/Meta-Llama-3-8B-Instruct_4bit --model-format awq --cache-max-entry-count 0.01
顯存占用為6738MiB。
LMDeploy服務(wù)(serve)
在前面的章節(jié),我們都是在本地直接推理大模型,這種方式成為本地部署。在生產(chǎn)環(huán)境下,我們有時(shí)會(huì)將大模型封裝為 API 接口服務(wù),供客戶端訪問(wèn)。
1. 啟動(dòng)API服務(wù)器
通過(guò)以下命令啟動(dòng)API服務(wù)器,推理Meta-Llama-3-8B-Instruct模型:
lmdeploy serve api_server \
/root/model/Meta-Llama-3-8B-Instruct \
--model-format hf \
--quant-policy 0 \
--server-name 0.0.0.0 \
--server-port 23333 \
--tp 1
其中,model-format、quant-policy這些參數(shù)是與第三章中量化推理模型一致的;server-name和server-port表示API服務(wù)器的服務(wù)IP與服務(wù)端口;tp參數(shù)表示并行數(shù)量(GPU數(shù)量)。 通過(guò)運(yùn)行以上指令,我們成功啟動(dòng)了API服務(wù)器,請(qǐng)勿關(guān)閉該窗口,后面我們要新建客戶端連接該服務(wù)。 也可以直接打開(kāi)http://{host}:23333查看接口的具體使用說(shuō)明,如下圖所示。
這代表API接口啟動(dòng)成功了。
這一步由于Server在遠(yuǎn)程服務(wù)器上,所以本地需要做一下ssh轉(zhuǎn)發(fā)才能直接訪問(wèn)。在你本地打開(kāi)一個(gè)cmd窗口,輸入命令如下:
```打開(kāi)`127.0.0.1:23333`

2. 命令行客戶端連接API服務(wù)器
在“1.”中,我們?cè)诮K端里新開(kāi)了一個(gè)API服務(wù)器。 本節(jié)中,我們要新建一個(gè)命令行客戶端去連接API服務(wù)器。首先通過(guò)VS Code新建一個(gè)終端: 激活conda環(huán)境
conda activate lmdeploy
運(yùn)行命令行客戶端:
lmdeploy serve api_client http://localhost:23333
運(yùn)行后,可以通過(guò)命令行窗口直接與模型對(duì)話
3. 網(wǎng)頁(yè)客戶端連接API服務(wù)器
關(guān)閉剛剛的VSCode終端,但服務(wù)器端的終端不要關(guān)閉。 運(yùn)行之前確保自己的gradio版本低于4.0.0。
pip install gradio==3.50.2
新建一個(gè)VSCode終端,激活conda環(huán)境。
conda activate lmdeploy
使用Gradio作為前端,啟動(dòng)網(wǎng)頁(yè)客戶端。
lmdeploy serve gradio http://localhost:23333 \
--server-name 0.0.0.0 \
--server-port 6006
打開(kāi)瀏覽器,訪問(wèn)地址http://127.0.0.1:6006 然后就可以與模型進(jìn)行對(duì)話了!
進(jìn)階作業(yè)
多模態(tài) Llava 微調(diào)和部署
沒(méi)有100%的A100。。。
Llama3 工具調(diào)用能力訓(xùn)練
柚子快報(bào)激活碼778899分享:Llama 3超級(jí)課堂作業(yè)筆記
好文鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。