模型概述
模型特點
模型能力
使用案例
🚀 SpeechGPT:賦予大語言模型內在跨模態對話能力
SpeechGPT是一個具備內在跨模態對話能力的大語言模型,能夠按照人類指令感知和生成多模態內容。藉助離散語音表徵,我們首先構建了大規模跨模態語音指令數據集SpeechInstruct。此外,我們採用了三階段訓練策略,包括模態適應預訓練、跨模態指令微調以及模態鏈指令微調。實驗結果表明,SpeechGPT在遵循多模態人類指令方面表現出色,凸顯了單模型處理多種模態的潛力。
你可以在項目頁面查看SpeechGPT的演示。從演示中可以看出,SpeechGPT具備強大的跨模態指令遵循能力和口語對話能力。它可以成為會說話的百科全書、你的私人助理、聊天夥伴、詩人、心理學家以及教育助手等。
SpeechGPT處理多種跨模態任務的能力
左:SpeechInstruct構建過程。 右:SpeechGPT模型結構
🚀 快速開始
本部分將引導你完成SpeechGPT的安裝、下載必要文件以及進行推理的步驟。
安裝
git clone https://github.com/0nutation/SpeechGPT
cd SpeechGPT
conda create --name SpeechGPT python=3.8
conda activate SpeechGPT
pip install -r requirements.txt
下載
若要與SpeechGPT進行對話,你需要將SpeechGPT-7B-cm和SpeechGPT-7B-com下載到本地。
你還需要將mHuBERT模型下載到utils/speech2unit/
目錄下。具體操作請參考Speech2unit。
s2u_dir="uitls/speech2unit"
cd ${s2u_dir}
wget https://dl.fbaipublicfiles.com/hubert/mhubert_base_vp_en_es_fr_it3.pt
wget https://dl.fbaipublicfiles.com/hubert/mhubert_base_vp_en_es_fr_it3_L11_km1000.bin
你需要將單元聲碼器(unit-vocoder)下載到utils/vocoder/
目錄下。具體操作請參考vocoder。
vocoder_dir="utils/vocoder/"
cd ${vocoder_dir}
wget https://dl.fbaipublicfiles.com/fairseq/speech_to_speech/vocoder/code_hifigan/mhubert_vp_en_es_fr_it3_400k_layer11_km1000_lj/config.json -O config.json
wget https://dl.fbaipublicfiles.com/fairseq/speech_to_speech/vocoder/code_hifigan/mhubert_vp_en_es_fr_it3_400k_layer11_km1000_lj/g_00500000 -O vocoder.pt
命令行推理
python3 speechgpt/src/infer/cli_infer.py \
--model-name-or-path "path/to/SpeechGPT-7B-cm" \
--lora-weights "path/to/SpeechGPT-7B-com" \
--s2u-dir "${s2u_dir}" \
--vocoder-dir "${vocoder_dir} \
--output-dir "output"
注意事項
對於語音輸入,你可以提供音頻文件的路徑。對於自動語音識別(ASR)或文本轉語音(TTS)任務,你必須在語音或文本前加上this is input:
,否則可能會識別錯誤。
語音響應將保存為.wav
文件,詳細響應將保存為JSON文件。這些文件的路徑將在響應中給出。
以下是與SpeechGPT對話的一些示例:
文本對話示例
請與SpeechGPT對話:
Who is Lebron James?
響應:
Lebron James is an American professional basketball player for the Los Angeles Lakers of the National Basketball Association (NBA). He is considered one of the greatest basketball players of all time and is known for his athleticism, scoring ability, and leadership skills. He is a four-time NBA MVP, a 14-time NBA All-Star, a 13-time All-NBA selection, and a two-time Olympic gold medalist.
響應JSON文件保存於output/responses.json
口語對話示例
請與SpeechGPT對話:
prompts/0.wav
轉錄內容: What are the main causes of climate change?
文本響應: The main causes of climate change are human activities such as burning fossil fuels, deforestation, and agricultural practices. These activities release greenhouse gases, like carbon dioxide and Methane, into the atmosphere which trap heat and cause the Earth's temperature to rise.
語音響應保存於output/wav/answer_0.wav
響應JSON文件保存於output/responses.json
自動語音識別(ASR)示例
請與SpeechGPT對話:
Recognize this speech, this is input: prompts/1.wav
響應:
today is a sunny day.
響應JSON文件保存於output/responses.json
文本轉語音(TTS)示例
請與SpeechGPT對話:
Read this sentence aloud, this is input: Today is a sunny day.
響應:
<sosp> <661> <987> <520> <982> <681> <982> <681> <982> <681> <982> <681> <982> <189> <63> <662> <79> <868> <220> <196> <166> <549> <822> <89> <194> <633> <14> <855> <183> <609> <389> <771> <865> <641> <124> <362> <734> <742> <98> <519> <26> <204> <280> <668> <167> <104> <650> <179> <961> <428> <950> <82> <165> <196> <166> <549> <822> <89> <194> <458> <726> <603> <819> <651> <133> <651> <133> <186> <133> <186> <133> <186> <511> <186> <511> <eosp>
語音響應保存於output/wav/answer_1.wav
響應JSON文件保存於output/responses.json
Gradio Web界面
python3 speechgpt/src/infer/web_infer.py \
--model-name-or-path "path/to/SpeechGPT-7B-cm" \
--lora-weights "path/to/SpeechGPT-7B-com" \
--s2u-dir "${s2u_dir}" \
--vocoder-dir "${vocoder_dir}" \
--output-dir "output/"
✨ 主要特性
- 內在跨模態對話能力:能夠感知和生成多模態內容,遵循人類指令。
- 大規模跨模態語音指令數據集:構建了SpeechInstruct數據集,為跨模態語音處理提供支持。
- 三階段訓練策略:包括模態適應預訓練、跨模態指令微調以及模態鏈指令微調,提升模型性能。
📦 安裝指南
安裝環境
git clone https://github.com/0nutation/SpeechGPT
cd SpeechGPT
conda create --name SpeechGPT python=3.8
conda activate SpeechGPT
pip install -r requirements.txt
下載模型和數據
下載模型
下載SpeechGPT-7B-cm和SpeechGPT-7B-com到本地。
下載mHuBERT模型
s2u_dir="uitls/speech2unit"
cd ${s2u_dir}
wget https://dl.fbaipublicfiles.com/hubert/mhubert_base_vp_en_es_fr_it3.pt
wget https://dl.fbaipublicfiles.com/hubert/mhubert_base_vp_en_es_fr_it3_L11_km1000.bin
下載單元聲碼器
vocoder_dir="utils/vocoder/"
cd ${vocoder_dir}
wget https://dl.fbaipublicfiles.com/fairseq/speech_to_speech/vocoder/code_hifigan/mhubert_vp_en_es_fr_it3_400k_layer11_km1000_lj/config.json -O config.json
wget https://dl.fbaipublicfiles.com/fairseq/speech_to_speech/vocoder/code_hifigan/mhubert_vp_en_es_fr_it3_400k_layer11_km1000_lj/g_00500000 -O vocoder.pt
💻 使用示例
基礎用法
以下是使用命令行與SpeechGPT進行文本對話的示例:
python3 speechgpt/src/infer/cli_infer.py \
--model-name-or-path "path/to/SpeechGPT-7B-cm" \
--lora-weights "path/to/SpeechGPT-7B-com" \
--s2u-dir "${s2u_dir}" \
--vocoder-dir "${vocoder_dir} \
--output-dir "output"
請與SpeechGPT對話:
Who is Lebron James?
響應:
Lebron James is an American professional basketball player for the Los Angeles Lakers of the National Basketball Association (NBA). He is considered one of the greatest basketball players of all time and is known for his athleticism, scoring ability, and leadership skills. He is a four-time NBA MVP, a 14-time NBA All-Star, a 13-time All-NBA selection, and a two-time Olympic gold medalist.
響應JSON文件保存於output/responses.json
高級用法
以下是使用Gradio Web界面與SpeechGPT進行交互的示例:
python3 speechgpt/src/infer/web_infer.py \
--model-name-or-path "path/to/SpeechGPT-7B-cm" \
--lora-weights "path/to/SpeechGPT-7B-com" \
--s2u-dir "${s2u_dir}" \
--vocoder-dir "${vocoder_dir}" \
--output-dir "output/"
通過Web界面,你可以更直觀地進行語音和文本輸入,並查看響應結果。
🔧 技術細節
數據集構建
藉助離散語音表徵,構建了大規模跨模態語音指令數據集SpeechInstruct。該數據集包含跨模態指令集和模態鏈指令集,為模型訓練提供了豐富的數據。
訓練策略
採用三階段訓練策略:
- 模態適應預訓練:利用mHuBERT對LibriLight數據集進行離散化處理,得到離散單元序列進行訓練。
- 跨模態指令微調:在SpeechInstruct跨模態指令集上對模型進行微調,使模型能夠更好地處理跨模態任務。
- 模態鏈指令微調:在SpeechInstruct模態鏈指令集上對模型進行LoRA微調,提升模型的口語對話能力。
📄 許可證
文檔未提及相關許可證信息。
開源列表
模型
- SpeechGPT-7B-ma:第一階段模態適應預訓練後得到的模型,使用LLaMA - 7B初始化,並在LibriLight語音單元上進一步預訓練。
- SpeechGPT-7B-cm:第二階段跨模態指令微調後得到的模型,使用SpeechGPT - 7B - ma初始化,並在SpeechInstruct跨模態指令集上進一步微調。這是一個強大的基礎模型,能夠對齊語音和文本。
- SpeechGPT-7B-com:第三階段模態鏈指令微調後得到的模型,使用SpeechGPT - 7B - cm初始化,並在SpeechInstruct模態鏈指令集上進行LoRA微調。這是SpeechGPT - 7B - cm用於口語對話的適配器模型。
數據集
- SpeechInstruct-cross-modal:跨模態指令集,約900萬個單元 - 文本數據對,由mHuBERT從大規模英語自動語音識別(ASR)數據集中進行標記。
- SpeechInstruct-chain-of-modality:四種輸入 - 輸出格式的思維鏈風格指令,即語音指令 - 語音響應、語音指令 - 文本響應、文本指令 - 語音響應和文本指令 - 文本響應。
SpeechInstruct-cross-modal數據格式
[
{
"prefix": "You are an AI assistant whose name is SpeechGPT.\n- SpeechGPT is a intrinsic cross-modal conversational language model that is developed by Fudan University. SpeechGPT can understand and communicate fluently with human through speech or text chosen by the user.\n- It can perceive cross-modal inputs and generate cross-modal outputs.\n",
"plain_text": "[Human]: Try to speak out this sentence, please. This is input: The alchemist rode in front, with the falcon on his shoulder.<eoh> [SpeechGPT]: <sosp><661><588><604><157><596><499><596><106><596><189><63><189><665><991><162><202><393><946><327><905><907><597><660><351><557><794><788><59><754><12><977><877><333><873><835><67><940><118><686><613><169><72><644><553><535><935><101><741><384><173><894><787><380><787><196><555><721><944><250><56><812><222><915><143><390><479><330><435><647><246><650><816><325><506><686><208><613><417><755><193><411><452><111><735><6><735><63><665><644><991><535><271><333><196><918><29><202><393><946><734><390><479><330><776><167><761><907><597><660><351><557><794><75><788><15><366><896><627><168><654><659><177><183><609><710><187><493><361><470><821><59><56><198><912><742><840><431><531><76><668><576><803><791><380><660><325><801><549><366><377><164><309><584><605><193><71><39><eosp><eoa> "
},
]
SpeechInstruct-chain-of-modality數據格式
[
{
"prefix": "You are an AI assistant whose name is SpeechGPT.\n- SpeechGPT is a intrinsic cross-modal conversational language model that is developed by Fudan University. SpeechGPT can understand and communicate fluently with human through speech or text chosen by the user.\n- It can perceive cross-modal inputs and generate cross-modal outputs.\n",
"plain_text": "[Human]: <sosp><661><987><511><732><951><997><111><982><189><63><665><991><535><101><741><173><945><944><503><641><124><565><734><870><290><978><833><238><761><907><430><901><185><403><557><244><583><788><663><969><896><627><143><515><663><969><660><691><251><412><260><41><740><677><253><380><382><268><506><876><417><755><16><819><80><651><80><651><80><987><588><eosp><eoh>. [SpeechGPT]: What is a bad term for poop?; [ta] A bad term for poop is excrement. It is usually used as a polite way to refer to fecal waste.; [ua] <sosp><497><63><264><644><710><823><565><577><154><331><384><173><945><29><244><326><583><728><576><663><969><896><627><143><38><515><663><24><382><251><676><412><260><41><740><677><253><382><268><876><233><878><609><389><771><865><641><124><878><609><423><384><879><487><219><522><589><337><126><119><663><748><12><671><877><377><385><902><819><619><842><419><997><829><111><666><42><277><63><665><644><389><771><685><437><641><124><258><436><139><340><11><59><518><56><948><86><258><436><139><340><347><376><940><118><944><878><173><641><124><362><734><179><961><931><878><609><423><384><879><219><522><866><337><243><935><101><741><822><89><194><630><86><555><105><79><868><220><156><824><998><870><390><422><330><776><663><969><523><105><79><799><220><357><390><479><422><330><776><485><165><86><501><119><716><205><521><787><935><101><741><89><194><664><835><67><940><118><613><417><755><902><415><772><497><eosp><eoa>."
},
]
訓練SpeechGPT
階段1:模態適應預訓練
- 利用mHuBERT對LibriLight數據集進行離散化處理,得到離散單元序列用於階段1訓練。你可以參考Speech2unit中的數據處理方法。
- 將離散單元劃分為訓練集和開發集,並以以下格式保存到
data/stage1/train.txt
和data/stage1/dev.txt
文件中:
<sosp><189><247><922><991><821><258><485><974><284><466><969><523><196><202><881><331><822><853><432><32><742><98><519><26><204><280><576><384><879><901><555><944><366><641><124><362><734><156><824><462><761><907><430><81><597><716><205><521><470><821><677><355><483><641><124><243><290><978><82><620><915><470><821><576><384><466><398><212><455><931><579><969><778><45><914><445><469><576><803><6><803><791><377><506><835><67><940><613><417><755><237><224><452><121><736><eosp>
<sosp><300><189><63><6><665><991><881><331><6><384><879><945><29><244><583><874><655><837><81><627><545><124><337><850><412><213><260><41><740><797><211><488><961><428><6><196><555><944><873><32><683><700><955><812><328><915><166><250><56><903><86><233><479><330><776><167><104><764><259><921><366><663><432><431><531><976><314><822><89><664><377><611><479><417><eosp>
<sosp><189><735><991><39><565><734><32><742><98><519><26><204><280><668><576><803><791><660><555><233><787><101><741><466><969><219><107><459><491><556><384><733><219><501><445><137><910><523><793><50><981><230><534><321><948><86><116><281><62><462><104><70><918><743><15><212><455><143><836><173><944><958><390><422><66><776><258><436><139><663><432><742><98><519><589><243><126><260><41><444><6><655><764><969><219><727><85><297><700><362><493><6><493><361><393><946><6><470><821><246><655><837><81><969><916><584><819><544><452><158><452><736><eosp>
- 下載LLaMA 7B(HuggingFace)到
llama/hf/7B
。
現在你可以開始階段1訓練:
若要進行分佈式訓練,你必須指定正確的NNODE
、NODE_RANK
、MASTER_ADDR
和MASTER_PORT
值。
bash scripts/ma_pretrain.sh ${NNODE} ${NODE_RANK} ${MASTER_ADDR} ${MASTER_PORT}
階段2:跨模態指令微調
將SpeechInstruct跨模態指令集下載到data/stage2/
。
如果你想跳過階段1訓練,可以將SpeechGPT-7B-ma
下載到output/stage1/
。
現在你可以開始階段2訓練:
若要進行分佈式訓練,你必須指定正確的NNODE
、NODE_RANK
、MASTER_ADDR
和MASTER_PORT
值。
bash scripts/cm_sft.sh ${NNODE} ${NODE_RANK} ${MASTER_ADDR} ${MASTER_PORT}
階段3:模態鏈指令微調
將SpeechInstruct模態鏈指令集下載到data/stage3/
。
如果你想跳過階段1和階段2,可以將SpeechGPT-7B-cm
下載到output/stage2/
。
現在你可以開始階段3訓練:
若要進行分佈式訓練,你必須指定正確的NNODE
、NODE_RANK
、MASTER_ADDR
和MASTER_PORT
值。
bash scripts/com_sft.sh ${NNODE} ${NODE_RANK} ${MASTER_ADDR} ${MASTER_PORT}
微調SpeechGPT
Speech-7B-cm
是一個語音和文本對齊能力較強的基礎模型。我們鼓勵基於此模型對SpeechGPT進行微調。
步驟1:按照SpeechInstruct跨模態指令集的格式準備你的數據。
步驟2:將SpeechGPT-7B-cm下載到本地。
步驟3:修改scripts/cm_sft.sh
腳本中的METAROOT
、DATAROOT
和OUTROOT
參數為你自己的參數,然後運行該腳本。對於LoRA微調,更新scripts/com_sft.sh
腳本中的METAROOT
、DATAROOT
和OUTROOT
參數並運行該腳本。
致謝
- MOSS:我們使用了moss - sft - 002 - data。
- stanford_alpaca:我們基於此代碼庫進行開發。
引用
如果你發現SpeechGPT對你的研究和應用有幫助,請使用以下BibTex進行引用:
@misc{zhang2023speechgpt,
title={SpeechGPT: Empowering Large Language Models with Intrinsic Cross-Modal Conversational Abilities},
author={Dong Zhang and Shimin Li and Xin Zhang and Jun Zhan and Pengyu Wang and Yaqian Zhou and Xipeng Qiu},
year={2023},
eprint={2305.11000},
archivePrefix={arXiv},
primaryClass={cs.CL}
}











