分離部署(Dist-train):

多模態(tài)模型的訓(xùn)練中,由于不同模態(tài)模型對算力和內(nèi)存需求的異構(gòu)性,不同模態(tài)模型的最優(yōu)并行配置往往不一致,如果全部使用同一種并行配置,會造成負載不均衡、資源利用不充分。MindSpeed MM支持Dist-train功能,通過對異構(gòu)模型配置不同的計算資源和并行配置,減少冗余的靜態(tài)資源和異構(gòu)模型間的空泡(bubble),使異構(gòu)模型之間的運行速度達到最優(yōu)匹配,提升性能15%。

使用方式:在examples/internvl*/ finetune_internvl*B.sh中添加參數(shù)–dist-train,同時配置model.json中的dist_config字段。(具體使用方式可參考倉庫特性文檔https://gitee.com/ascend/MindSpeed-MM/blob/master/docs/features/dist-train.md) 

2.png

基于MindSpeed MM的

InternVL3視圖理解體驗

新一代的多模態(tài)理解模型InternVL3有強大的視圖理解能力,讓我們快速體驗一下。

圖片理解體驗:

640.png

Prompt:描述一下這張梗圖并解釋其中的笑點。

輸出結(jié)果:

3.png

OCR識別:

4.png

Prompt:識別出圖中的文字。

輸出結(jié)果:

5.png

視頻理解體驗:

Prompt:請描述這個視頻。

輸出結(jié)果:

6.png

快速上手

基于MindSpeed MM玩轉(zhuǎn)InternVL3

【環(huán)境安裝】

模型開發(fā)時推薦使用配套的環(huán)境版本,詳見倉庫中的“環(huán)境安裝”:https://gitee.com/ascend/MindSpeed-MM/blob/master/examples/internvl3/README.md

倉庫拉取:

1 git clone https://gitee.com/ascend/MindSpeed-MM.git

2 git clone https://github.com/NVIDIA/Megatron-LM.git

3 cd Megatron-LM

4 git checkout core_r0.8.0

5 cp -r megatron ../MindSpeed-MM/

6 cd ..

7 cd MindSpeed-MM

8 mkdir logs

9 mkdir dataset

10 mkdir ckpt

環(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 -n test

3  python=3.10

4 conda activate test 

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

10 # apex for Ascend 參考 https://gitee.com/ascend/apex

11 # 建議從原倉編譯安裝

12 

13 # 安裝加速庫

14 git clone

15  https://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 cp examples/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)重下載:

99.png

權(quán)重轉(zhuǎn)換:

MindSpeed-MM修改了部分原始網(wǎng)絡(luò)的結(jié)構(gòu)名稱,使用mm-convert工具對原始預(yù)訓(xùn)練權(quán)重進行轉(zhuǎn)換。該工具實現(xiàn)了huggingface權(quán)重和MindSpeed-MM權(quán)重的轉(zhuǎn)換以及PP(Pipeline Parallel)的權(quán)重切分。

1 # 根據(jù)實際情況修改 ascend-toolkit 路徑

2 source 

3 /usr/local

4 /Ascend/ascend-toolkit/set_env.sh

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在每個卡上切分的層數(shù),注意要和model.json中配置的pipeline_num_layers一致

20 # vit_pp_layers: vit在每個卡上切分的層數(shù),注意要和model.json中配置的pipeline_num_layers一致

21 # trust_remote_code: 為保證代碼安全,配置trust_remote_code默認為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ù)跟進上線;

(2)用戶想嘗鮮微調(diào)功能,可參考InternVL2.5的微調(diào)實踐

(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ù)實際情況修改 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為例,按實際情況修改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  按實際情況輸入圖片路徑

5     “prompts”: “Please describe the image shortly.”, #

6  按實際情況輸入提示詞(支持中英文)

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為例,按實際情況修改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  按實際情況輸入視頻路徑

5     “prompts”: “Please describe the video shortly.”, #

6  按實際情況輸入提示詞(支持中英文)

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 }

修改啟動腳本:

按實際情況修改inference_internvl.sh腳本。

1 # 根據(jù)實際情況修改 ascend-toolkit 路徑

2 source /usr/local/Ascend/ascend-toolkit/set_env.sh

3 …

4 MM_MODEL=”./examples/internvl3/inference_8B.json”

3.啟動推理

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)大模型套件,同時支持多模態(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

分享到

xiesc

相關(guān)推薦