多模態(tài)理解領(lǐng)域當(dāng)前已成了各家AI大模型公司“軍備競賽”的關(guān)鍵點(diǎn)之一,國內(nèi)外知名AI大模型公司都爭相通過發(fā)布先進(jìn)的多模態(tài)大語言模型展現(xiàn)其在多模態(tài)理解領(lǐng)域的前沿能力。近期,上海AI實(shí)驗(yàn)室推出了其比較新的多模態(tài)大語言模型InternVL3 系列,相比上一代InternVL2. 5 模型,該模型展現(xiàn)出卓越的多模態(tài)感知和推理能力,同時(shí)進(jìn)一步擴(kuò)展了其工具使用、GUI代理、工業(yè)圖像分析等多模態(tài)能力。此前MindSpeed MM倉庫一直同步開源InternVL2 系列、InternVL2. 5 系列等模型,InternVL3 系列一經(jīng)發(fā)布開源,即在MindSpeed MM中開箱即用并同步開源,當(dāng)前已支持在線推理功能并基于InternVL2. 5 給出參考的微調(diào)實(shí)踐。MindSpeed MM是面向大規(guī)模分布式訓(xùn)練的昇騰多模態(tài)大模型套件,基于MindSpeed Core實(shí)現(xiàn)多模態(tài)模型的較好的性能優(yōu)化,為開發(fā)者提供有效易用的開發(fā)體驗(yàn)。
基于MindSpeed MM的
InternVL系列訓(xùn)練優(yōu)化特性
InternVL系列是多模態(tài)理解模型,基于其異構(gòu)特點(diǎn)以及該模型存在明顯的負(fù)載不均衡問題,MindSpeed MM對InternVL系列模型在使用了融合算子、分布式優(yōu)化器及流水調(diào)度優(yōu)化等常用特性的基礎(chǔ)上,同時(shí)使能多模態(tài)異構(gòu)流水線并行、動(dòng)態(tài)流水線并行、數(shù)據(jù)分桶負(fù)載均衡、虛擬流水線并行、分離部署等優(yōu)化加速特性,實(shí)現(xiàn)了較好的性能優(yōu)化。(其中多模態(tài)異構(gòu)流水線并行、動(dòng)態(tài)流水線并行、數(shù)據(jù)分桶負(fù)載均衡等特性的介紹,在之前的文章(多模態(tài)理解SOTA模型開箱即用,MindSpeed MM支持Qwen2.5-VL理想實(shí)踐)已有介紹)
虛擬流水線并行:
常規(guī)的流水線并行切分粒度過大,運(yùn)行過程中仍然有許多空泡(bubble),計(jì)算資源利用率仍有提高空間。虛擬流水線并行(VPP)能夠在設(shè)備數(shù)量不變的情況下,分出更多的流水線階段,以更多的通信量,換取空泡比率降低。MindSpeed MM將虛擬流水線并行同時(shí)應(yīng)用到視覺模塊和語言模塊,并支持自定義的非均勻切分,進(jìn)一步減小空泡比率,提升性能5%。
使用方式:在examples/internvl*/ finetune_internvl*B.sh中配置num-layers-per-virtual-pipeline-stage字段。(具體使用方式可參考倉庫特性文檔
https://gitee.com/ascend/MindSpeed-MM/blob/master/docs/features/virtual_pipeline_parallel.md)
分離部署(Dist-train):
多模態(tài)模型的訓(xùn)練中,由于不同模態(tài)模型對算力和內(nèi)存需求的異構(gòu)性,不同模態(tài)模型的特出并行配置往往不一致,如果全部使用同一種并行配置,會(huì)造成負(fù)載不均衡、資源利用不充分。MindSpeed MM支持Dist-train功能,通過對異構(gòu)模型配置不同的計(jì)算資源和并行配置,減少冗余的靜態(tài)資源和異構(gòu)模型間的空泡(bubble),使異構(gòu)模型之間的運(yùn)行速度達(dá)到特出匹配,提升性能15%。
使用方式:在examples/internvl*/ finetune_internvl*B.sh中添加參數(shù)--dist-train,同時(shí)配置model.json中的dist_config字段。(具體使用方式可參考倉庫特性文檔https://gitee.com/ascend/MindSpeed-MM/blob/master/docs/features/dist-train.md)
基于MindSpeed MM的
InternVL3 視圖理解體驗(yàn)
新一代的多模態(tài)理解模型InternVL3 有強(qiáng)大的視圖理解能力,讓我們快速體驗(yàn)一下。
圖片理解體驗(yàn):
Prompt:描述一下這張梗圖并解釋其中的笑點(diǎn)。
輸出結(jié)果:
OCR識別:
Prompt:識別出圖中的文字。
輸出結(jié)果:
視頻理解體驗(yàn):
Prompt:請描述這個(gè)視頻。
輸出結(jié)果:
快速上手
基于MindSpeed MM玩轉(zhuǎn)InternVL3
【環(huán)境安裝】
模型開發(fā)時(shí)推薦使用配套的環(huán)境版本,詳見倉庫中的“環(huán)境安裝”
https://gitee.com/ascend/MindSpeed-
MM/blob/master/examples/internvl3/README.md
倉庫拉?。?/p>
1 gitclonehttps://gitee.com/ascend/MindSpeed-MM.git
2 gitclonehttps://github.com/NVIDIA/Megatron-LM.git
3 cdMegatron-LM
4 git checkout core_r0.8.0
5 cp-r megatron ../MindSpeed-MM/
6 cd..
7 cdMindSpeed-MM
8 mkdirlogs
9 mkdirdataset
10 mkdirckpt
環(huán)境搭建:
torch npu 與 CANN包參考鏈接:安裝包參考鏈接
https://gitee.com/link?target=https%3A%2F%2Fsupport.huawei.com%2Fenterprise%2Fzh%2Fascend-computing%2Fcann-pid-251168373%2Fsoftware
1 # python3.10
2 conda create -ntest
3 python=3.10
4 conda activatetest
5
6 # 安裝 torch 和 torch_npu,注意要選擇對應(yīng)python版本、x86 或arm的torch、torch_npu及apex包
7 pip install torch-2.1.0-cp310-cp310m-manylinux2014_aarch64.whl
8 pip install torch_npu-2.1.0*-cp310-cp310m-linux_aarch64.whl
9
10 # apex for Ascend 參考 https://gitee.com/ascend/apex
11 # 建議從原倉編譯安裝
12
13# 安裝加速庫
14 gitclone
15https://gitee.com/ascend/MindSpeed.git
16 cd
17 MindSpeed
18 # checkout commit from MindSpeed core_r0.8.0
19 git checkout 3f09d6736571cf1e30f8ac97de77982d0ab32cc5
20 pip install -r requirements.txt
21 pip3 install -e .
22 cd
23 ..
24 # 替換MindSpeed中的文件
25 cpexamples/internvl2.5/dot_product_attention.py MindSpeed/mindspeed/core/transformer/dot_product_attention.py
26
27 # 安裝其余依賴庫
28 pip install -e .
【權(quán)重下載及轉(zhuǎn)換】
InternVL3 權(quán)重下載:
權(quán)重轉(zhuǎn)換:
MindSpeed-MM修改了部分原始網(wǎng)絡(luò)的結(jié)構(gòu)名稱,使用mm-convert工具對原始預(yù)訓(xùn)練權(quán)重進(jìn)行轉(zhuǎn)換。該工具實(shí)現(xiàn)了huggingface權(quán)重和MindSpeed-MM權(quán)重的轉(zhuǎn)換以及PP(Pipeline Parallel)的權(quán)重切分。
1 # 根據(jù)實(shí)際情況修改 ascend-toolkit 路徑
2 source
3 /usr/local
4 /Ascend/ascend-toolkit/set_env.sh
5
6 # 8B
7 mm-convert InternVLConverter hf_to_mm \
8 --cfg.mm_dir"pretrained/InternVL3-8B"
9 \
10 --cfg.hf_config.hf_dir"raw_ckpt/InternVL3-8B"
11 \
12--cfg.parallel_config.llm_pp_layers [[28]] \
13 --cfg.parallel_config.vit_pp_layers [[28]] \
14 --cfg.trust_remote_code True
15 # 其中:
17 # mm_dir: 轉(zhuǎn)換后保存目錄
18 # hf_dir: huggingface權(quán)重目錄
19 # llm_pp_layers: llm在每個(gè)卡上切分的層數(shù),注意要和model.json中配置的pipeline_num_layers一致
20 # vit_pp_layers: vit在每個(gè)卡上切分的層數(shù),注意要和model.json中配置的pipeline_num_layers一致
21 # trust_remote_code: 為保證代碼安全,配置trust_remote_code默認(rèn)為False,用戶需要設(shè)置為True,并且確保自己下載的模型和數(shù)據(jù)的安全性
【數(shù)據(jù)集準(zhǔn)備及處理】
數(shù)據(jù)集下載:
用戶需自行獲取并解壓InternVL-Finetune數(shù)據(jù)集到dataset/playground目錄下,以數(shù)據(jù)集ai2d為例,解壓后的數(shù)據(jù)結(jié)構(gòu)如下:
1 $playground
2 ├──data
3 ├── ai2d
4 ├── abc_images
5 ├── images
6 ├── opensource
7 ├── ai2d_train_12k.jsonl
【微調(diào)】
(1) 由于當(dāng)前官倉還未開源微調(diào)代碼和腳本,正式版的微調(diào)功能后續(xù)跟進(jìn)上線;
(2)用戶想嘗鮮微調(diào)功能,可參考InternVL2. 5 的微調(diào)實(shí)踐
(https://gitee.com/ascend/MindSpeed-MM/blob/master/examples/internvl2.5/README.md)
【推理】
1.準(zhǔn)備工作
配置腳本前需要完成前置準(zhǔn)備工作,包括:環(huán)境安裝、權(quán)重下載及轉(zhuǎn)換,詳情可查看對應(yīng)章節(jié)。(當(dāng)前支持8B單卡推理)
推理權(quán)重轉(zhuǎn)換命令如下:
1 # 根據(jù)實(shí)際情況修改 ascend-toolkit 路徑
2 source/usr/local/Ascend/ascend-toolkit/set_env.sh
3
4 # 8B
5 mm-convert InternVLConverter hf_to_mm \
6 --cfg.mm_dir"pretrained/InternVL3-8B"\
7 --cfg.hf_config.hf_dir"raw_ckpt/InternVL3-8B"\
8 --cfg.parallel_config.llm_pp_layers [[28]] \
9 --cfg.parallel_config.vit_pp_layers [[28]] \
10 --cfg.trust_remote_code True
2.配置參數(shù)
參數(shù)配置:
修改inference_8B.json文件,包括infer_data_type、file_path、prompts、from_pretrained以及tokenizer的from_pretrained等字段。
單圖推理:
以InternVL3-8B為例,按實(shí)際情況修改inference_8B.json對應(yīng)參數(shù),注意tokenizer_config的權(quán)重路徑為轉(zhuǎn)換前的權(quán)重路徑。
1 {
2 "infer_data_type":"image",
3 "file_path":"./examples/internvl3/view.jpg", #
4 按實(shí)際情況輸入圖片路徑
5 "prompts":"Please describe the image shortly.",#
6 按實(shí)際情況輸入提示詞(支持中英文)
7 "model_id":"InternVLPipeline",
8 "from_pretrained":"./pretrained/InternVL3-8B/release/mp_rank_00/model_optim_rng.pt",#
9 注意路徑要到
10 .pt
11 文件
12 ...
13 "tokenizer":{
14 ...
15 "autotokenizer_name":"AutoTokenizer",
16 "from_pretrained":"raw_ckpt/InternVL3-8B",
17 ...
18 },
19 ...
20 }
視頻推理:
以InternVL3-8B為例,按實(shí)際情況修改inference_8B.json對應(yīng)參數(shù),注意tokenizer_config的權(quán)重路徑為轉(zhuǎn)換前的權(quán)重路徑。(推理demo視頻下載red-panda)
1 {
2 "infer_data_type":"video",
3 "file_path":"examples/internvl3/red-panda.mp4", #
4 按實(shí)際情況輸入視頻路徑
5 "prompts":"Please describe the video shortly.",#
6 按實(shí)際情況輸入提示詞(支持中英文)
7 "model_id":"InternVLPipeline",
8 "from_pretrained":"./pretrained/InternVL3-8B/release/mp_rank_00/model_optim_rng.pt",#
9 注意路徑要到
10 .pt
11 文件
12 ...
13 "tokenizer":{
14 ...
15 "autotokenizer_name":"AutoTokenizer",
16 "from_pretrained":"raw_ckpt/InternVL3-8B",
17 ...
18 },
19 ...
20 }
修改啟動(dòng)腳本:
按實(shí)際情況修改inference_internvl.sh腳本。
1 # 根據(jù)實(shí)際情況修改 ascend-toolkit 路徑
2 source/usr/local/Ascend/ascend-toolkit/set_env.sh
3 ...
4 MM_MODEL="./examples/internvl3/inference_8B.json"
3.啟動(dòng)推理
bash examples/internvl3/inference_internvl.sh
【更多參數(shù)見MindSpeed MM倉庫】
準(zhǔn)備工作和參數(shù)說明見MindSpeed MM開源代碼倉鏈接:
https://gitee.com/ascend/MindSpeed-MM/tree/master/examples/internvl3
結(jié)語
MindSpeed MM是面向大規(guī)模分布式訓(xùn)練的昇騰多模態(tài)大模型套件,同時(shí)支持多模態(tài)生成及多模態(tài)理解,旨在為華為 昇騰芯片 提供端到端的多模態(tài)訓(xùn)練解決方案, 包含預(yù)置業(yè)界主流模型,數(shù)據(jù)工程,分布式訓(xùn)練及加速,預(yù)訓(xùn)練、微調(diào)、在線推理任務(wù)等特性。
由于當(dāng)前InternVL3 系列模型的代碼和訓(xùn)練微調(diào)功能等未完全發(fā)布開源,后續(xù)MindSpeed MM將基于InternVL3 系列模型同步上線更加豐富的特性,敬請期待。
歡迎關(guān)注MindSpeed MM:https://gitee.com/ascend/MindSpeed-MM
(推廣)