(似乎被爱可可老师转发了🥹,感谢大家关注!后续有空会加入更强LLM(先试试14B吧)和更多的数据,具体会根据后续的资源来看
知乎博客:https://zhuanlan.zhihu.com/p/664612306
已经支持DeepSpeed!
-
干净、灵活的Trainer:https://github.com/Coobiw/MiniGPT4Qwen/tree/master/lavis_trainer_cleaned
-
grad-checkpoint + amp tutorails:https://github.com/Coobiw/MiniGPT4Qwen/tree/master/amp_and_grad-checkpointing
-
deepspeed tutorials:https://github.com/Coobiw/MiniGPT4Qwen/tree/master/deepspeed_tutorials
-
现在已经支持deepspeed的训练(使用deepspeed runner)
MiniGPT4是最近很火的一个MLLM项目,他证明了对于BLIP2的ViT+Q-former这种已经与语言模态做了对齐预训练的结构,只需要重训一个Linear层,便可以接入新的LLM。对于现在这个每个月有一个新的更强的LLM出来的时代,这种构建多模态大模型的方式是十分高效的。
然而,MiniGPT4采用LLaMA、Vicuna作为语言模型,它们的中文支持相对较弱,导致训练出的MLLM对中文支持不好。而现在也有许多开源出来的中文LLM,如:阿里云的Qwen、百川智能的baichuan等。
本项目使用Qwen-Chat作为LLM,用MiniGPT4的对齐方式,更加高效地训练了一个MLLM,名为 Minigpt4Qwen
。相比MiniGPT4的两阶段训练(低质量数据对齐 + 高质量数据指令微调),本项目仅仅采用18.8k的高质量指令微调数据,经过单阶段预训练即可达到很好的效果。
本项目使用了4张/8张 3090 24G
,进行训练,单卡推理。实际上,单张24G的3090也能够满足训练的计算需求,但需要调大梯度积累。
- 支持Qwen-14B-Chat的训练
- 支持MME Benchmark的测评
- 支持deepspeed
- 支持pytorch原生FSDP(可能搁置,因为实现了deepspeed,而且fsdp个人认为不怎么好用)
- 开放gradio WebUI demo
- 开放所用数据集和checkpoint
- 开放源代码
conda create -n minigpt4qwen python=3.8
conda activate minigpt4qwen
pip install -e .
请将模型权重下载后都放在
cache/ckpt
下
mkdir cache
cd cache
mkdir ckpt
mkdir dataset
1.下载BLIP2的相关权重
(a) eva vit-g
wget https://storage.googleapis.com/sfr-vision-language-research/LAVIS/models/BLIP2/eva_vit_g.pth
(b) bert-base-uncased
huggingface,下载如下的文件即可
(c) blip2_pretrained_flant5xxl
blip2_pretrained_flant5xxl.pth
wget https://storage.googleapis.com/sfr-vision-language-research/LAVIS/models/BLIP2/blip2_pretrained_flant5xxl.pth
2.下载Qwen7B-chat的权重
3.下载本模型的checkpoint(建议放入 lavis/output/
)
在本仓库的release里放有checkpoint,可以直接下载
wget https://github.com/Coobiw/MiniGPT4Qwen/releases/download/instruction-data_and_checkpointv1.0/ckpt.zip
unzip ckpt.zip
目录结构:
├── cache
│ ├── ckpt
│ │ ├── bert-base-uncased
│ │ ├── blip2
│ │ │ ├── blip2_pretrained_flant5xxl.pth
│ │ ├── eva
│ │ │ ├── eva_vit_g.pth
│ │ ├── Qwen7B-chat
python test_model_chat.py
python cli_demo.py --checkpoint-path xxxxxx
运行后需要输入图片路径,输入后进入对话
常见操作:
:help 查看help
:clear 清空当前命令行
:clh 清空对话历史(但图像输入不会更改)
:his 查看对话历史
:img 查看输入的图像路径
本数据集共含有18.8k个图文对,来自MMPretrain根据llava和minigpt4处理得到,下载链接:huggingface
为了支持当前的 lavis
库的训练框架,我对数据集的annotations进行了重新处理,放到了本仓库的release中,下载链接:instruction_data
wget https://github.com/Coobiw/MiniGPT4Qwen/releases/download/instruction-data_and_checkpointv1.0/instruction_data.zip
unzip instruction_data
最后需要将数据集放入 ./cache/dataset
中,目录结构如下:
├── cache
│ └── dataset
│ ├── llava
│ │ │ ├── llava_minigpt4qwen_format.json
│ │ │ ├── image
│ ├── minigpt4
│ │ │ ├── image
│ │ │ ├── minigpt4_minigpt4qwen_format.json
请参考train.yaml
单卡:
CUDA_VISIBLE_DEVICES=xxx python train.py --cfg-path lavis/projects/instruction_tuning/train.yaml
多卡:
CUDA_VISIBLE_DEVICES=xxx python -m torch.distributed.run --nproc_per_node=8 train.py --cfg-path lavis/projects/instruction_tuning/train.yaml
本项目支持了ZERO-{0,1,2,3}的训练、ZERO-{0,1,2}的checkpoint转换、以及ZERO-{0,1,2}的推理(chat和gradio)
config文件请参考:train_zero0_3090x4.yaml
运行命令
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m torch.distributed.run --nproc_per_node=4 train.py --cfg-path lavis/projects/deepspeed/train_zero0_3090x4.yaml --use-deepspeed
上述训练过程会调用deepspeed的save_checkpoint
方法,得到类似下图的目录:
可以运行以下命令得到模型的.pth
文件:
python deepspeed2pth.py --ckpt_dir lavis/output/deepspeed/lr1e-4_4x3090/20231220150/deepspeed_ckpt/epoch_9
之后会在该目录中生成一个model.pth
文件
接着就可以用该.pth
文件去使用cli_demo.py
或webui_demo.py
进行聊天啦~
输入图片:
对话内容展示:(通过 :his
)
开启do_sample和beam search
- Lavis 本仓库是基于lavis进行构建的
- QwenLM 本仓库的语言模型采用Qwen-7B-Chat
- MiniGPT4 本仓库的主要**来自MiniGPT4
- MMPretrain 提供所需的双语指令微调数据集
- DeepSpeed 👍
- DeepSpeedExamples 👍👍
对应issue:Coobiw#5
在https://github.com/Coobiw/MiniGPT4Qwen/commit/4ad76d15c79cec9b08e8f0f0da69732d0924a9db 这个commit中,我修改了log时loss显示的问题。
简单来说就是我放出来的log里的loss是除过梯度积累的iteration数的(就是accum_grad_iters
,在yaml文件里是设置成16或者32),所以你看到的会差一个数量级,如果你使用的是这个commit之后的代码跑出这个结果是正常的
- 本仓库的许多代码是基于Lavis 的,其采用 BSD 3-Clause License.
- 本仓库采用Qwen-7B-Chat,支持商用和科研、开发用途,其License为LICENSE