1、下载源码、构建环境
在github克隆代码,创建python环境。
git clone https://github.com/Vision-CAIR/MiniGPT-4.git
cd MiniGPT-4
conda env create -f environment.yml
conda activate minigpt4
2、MiniGPT-4 模型下载
2.1 下载 Vicuna Weight
当前版本的MiniGPT-4是建立在v0版本的 Vicuna-13B 之上的。请参考我们的说明来准备 Vicuna weights。最终的权重将在结构类似于以下的单个文件夹中:
注:Vicuna是一个开源的基于llama的LLM,其性能接近ChatGPT。我们目前使用的是v0版本的Vicuna-13B。
$ git lfs install
$ git clone https://huggingface.co/lmsys/vicuna-13b-delta-v1.1 # more powerful, need at least 24G gpu memory
# 或者下载7b模型
$ git clone https://huggingface.co/lmsys/vicuna-7b-delta-v1.1 # smaller, need 12G gpu memory
请注意,这不是直接的 working weight ,而是LLAMA-13B的 working weight 与 original weight 的差值。(由于LLAMA的规则,我们无法分配LLAMA的 weight 。)
2.2 下载 原始LLAMA-7B或LLAMA-13B权重
然后,您需要按照HuggingFace提供的原始权重 或 从互联网上获取 HuggingFace格式的原始LLAMA-7B或LLAMA-13B 权重。
注:这里 直接 从 HuggingFace 下载 已转化为 HuggingFace格式的原始LLAMA-7B或LLAMA-13B 权重
$ git lfs install
$ git clone https://huggingface.co/decapoda-research/llama-13b-hf # more powerful, need at least 24G gpu memory
$ # or
$ git clone https://huggingface.co/decapoda-research/llama-7b-hf # smaller, need 12G gpu memory
2.3 构建真正的 working weight
当这两个 weight 备好后,我们可以使用Vicuna团队的工具来创建真正的 working weight 。首先,安装与v0 Vicuna兼容的库
$ pip install git+https://github.com/lm-sys/FastChat.git@v0.1.10
然后,执行如下命令创建最终 working weight
$ python -m fastchat.model.apply_delta --base /path/to/llama-13bOR7b-hf/ --target /path/to/save/working/vicuna/weight/ --delta /path/to/vicuna-13bOR7b-delta-v1.1/ --low-cpu-mem
>>>The tokenizer class you load from this checkpoint is not the same type as the class this function is called from. It may result in unexpected tokenization.
The tokenizer class you load from this checkpoint is 'LLaMATokenizer'.
The class this function is called from is 'LlamaTokenizer'.
Split files for the base model to /tmp/tmptu2g17_d
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 33/33 [01:47<00:00, 3.26s/it]
Split files for the delta model to /tmp/tmpol8jc2oy
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [01:03<00:00, 31.92s/it]
Applying the delta
33it [02:09, 3.91s/it]
Saving the target model to vicuna/weight/
注:低CPU内存需加入--low-cpu-mem,可以把大的权重文件分割成多个小份,并使用磁盘作为临时存储。可以使峰值内存保持在16GB以下。不然无法载入vicuna增量文件,CPU内存占满,程序直接被kill,
output
config.json pytorch_model-16.bin pytorch_model-23.bin pytorch_model-30.bin pytorch_model-8.bin
pytorch_model-0.bin pytorch_model-17.bin pytorch_model-24.bin pytorch_model-31.bin pytorch_model-9.bin
pytorch_model-10.bin pytorch_model-18.bin pytorch_model-25.bin pytorch_model-32.bin pytorch_model.bin.index.json
pytorch_model-11.bin pytorch_model-19.bin pytorch_model-26.bin pytorch_model-3.bin special_tokens_map.json
pytorch_model-12.bin pytorch_model-1.bin pytorch_model-27.bin pytorch_model-4.bin tokenizer_config.json
pytorch_model-13.bin pytorch_model-20.bin pytorch_model-28.bin pytorch_model-5.bin tokenizer.model
pytorch_model-14.bin pytorch_model-21.bin pytorch_model-29.bin pytorch_model-6.bin
pytorch_model-15.bin pytorch_model-22.bin pytorch_model-2.bin pytorch_model-7.bin
2.4 配置 模型路径
修改模型文件的路径
set the path to the vicuna weight in the model config file(minigpt4/configs/models/minigpt4.yaml#L16) at Line 16.
model:
arch: mini_gpt4
# vit encoder
image_size: 224
drop_path_rate: 0
use_grad_checkpoint: False
vit_precision: "fp16"
freeze_vit: True
freeze_qformer: True
# Q-Former
num_query_token: 32
# Vicuna
llama_model: "chat/vicuna/weight" # 将 "/path/to/vicuna/weights/" 修改为本地 weight 地址
...
3、下载 Pretrained MiniGPT-4 checkpoint
-
下载 MiniGPT-4 checkpoint
-
google drive 下载
-
huggingface 平台下载
$ git lfs install
$ git clone https://www.huggingface.co/wangrongsheng/MiniGPT4-7B
-
在 eval_configs/minigpt4_eval.yaml 的 第11行 设置 MiniGPT-4 checkpoint 路径
model:
arch: mini_gpt4
model_type: pretrain_vicuna
freeze_vit: True
freeze_qformer: True
max_txt_len: 160
end_sym: "###"
low_resource: True
prompt_path: "prompts/alignment.txt"
prompt_template: '###Human: {} ###Assistant: '
ckpt: '/path/to/pretrained/ckpt/' # 修改为前面下载好的MiniGPT4-7B checkpoint 路径
...
4、在本地启动演示Demo
尝试在本地机器运行demo代码 demo.py
python demo.py --cfg-path eval_configs/minigpt4_eval.yaml --gpu-id 0
为了节省GPU内存,Vicuna默认加载为8位,beam search width为1。这种配置对于Vicuna 13B需要大约23G GPU显存,对于Vicuna7B需要大约11.5G GPU显存。对于更强大的GPU,您可以通过在配置文件minigpt4_eval.yaml 中将low_resource设置为False,可以16位运行模型,并使用更大的beam search width。