VisualGLM是由清华大学的GLM团队推出的一个新的多模态对话语言模型,支持图像、中文和英文的输入和输出。VisualGLM大幅度地提升了多模态对话的SOTA水平,创造了令人惊叹的效果,能够根据图像和文本的内容生成符合人类偏好的回答,成为了多模态领域的新时代引领者。 VisualGLM完全开源可商用,基于 Transformer 结构,语言模型部分基于 ChatGLM-6B ,具有 62 亿参数;图像部分通过训练 BLIP2-Qformer 构建起视觉模型与语言模型的桥梁,整体模型共 78 亿参数。
VisualGLM-6B 依靠来自于 CogView 数据集的30M高质量中文图文对,与300M经过筛选的英文图文对进行预训练,中英文权重相同。该训练方式较好地将视觉信息对齐到ChatGLM的语义空间;之后的微调阶段,模型在长视觉问答数据上训练,以生成符合人类偏好的答案。
参考README "mindformers安装" 安装mindformers。
注: VisualGLM-6B推理可以在单卡上完成部署,全量微调至少需要8卡。
运行mindformers/tools/hccl_tools.py生成RANK_TABLE_FILE的json文件
# 运行如下命令,生成当前机器的RANK_TABLE_FILE的json文件
python mindformers/tools/hccl_tools.py --device_num "[0,8)"
注: 若使用ModelArts的notebook环境,可从 /user/config/jobstart_hccl.json
路径下直接获取rank table,无需手动生成
从HuggingFace网站下载visualglm 6b词库的文件 ice_text.model。
下载地址:https://huggingface.co/THUDM/visualglm-6b/tree/main
推荐使用rclone工具下载模型
步骤
下载rclone工具
下载地址:https://rclone.org/downloads/
根据服务器的类型和处理器,选择对应的文件。
下载完以后解压,把其中的脚本拷贝出来,放到执行目录下:
cp rclone*/rclone /usr/local/bin/
创建rclone配置文件
在home目录创建rclone.conf文件
配置内容,这里的配置不需要修改:
[r2]
type = s3
provider = Cloudflare
access_key_id = eb4d69e273848089c7f9b9599cdcd983
secret_access_key = 367e9b21fef313f187026320016962b47b74ca4ada7d64d551c43c51e195d7a5
endpoint = https://c8a00746a80e06c4632028e37de24d6e.r2.cloudflarestorage.com
acl = private
参数说明
cd 模型下载路径/
rclone copy -P --multi-thread-streams THREAD_COUNT --no-check-certificate -vv --size-only r2:/sat/visualglm-6b.zip ./
cd research/visualglm
python convert_weight.py --torch_path TORCH_CKPT_DIR --vit_mindspore_path VIT_CKPT_PATH --qformer_mindspore_path QFORMER_CKPT_PATH --glm_mindspore_path GLM_CKPT_PATH
参数说明
注意:
接口说明请参考API接口
注意
由于visualglm模型的权重需要用户自行下载,因此在启动前,请先行在配置文件中将权重的路径配置完成。
打开配置文件 research/visualglm/run_visualglm_6b_image_to_text_generation.yaml,修改如下:
model:
model_config:
type: VisualGLMConfig
#...
checkpoint_name_or_path: "/path/to/visualglm_qformer.ckpt" # visualglm qformer weight
vision_config:
#...
checkpoint_name_or_path: "/path/to/visualglm_vit.ckpt" # visualglm vit weight
text_config:
type: GLMConfig
#...
checkpoint_name_or_path: "/path/to/glm_6b.ckpt" # visualglm glm weight
processor:
type: VisualGLMProcessor
image_processor:
type: VisualGLMImageProcessor
image_size: 224 # input image size
tokenizer:
#...
checkpoint_name_or_path: "/path/to/ice_text.model"
如果使用增量推理,需要在配置文件中use_past值设置为True。
visualglm的generate接口使用脚本已集成在run_visualglm.py脚本中,运行此脚本命令:
cd research/visualglm
python run_visualglm.py --config CONFIG_PATH --image_path=IMAGE_PATH --prompt=PROMPT --device_id DEVICE_ID
#运行结果:
#['<img> </img>问:描述这张图片。\n答: 泰坦尼克号 电影截图']
# 运行结果
参数说明
visualglm的pipeline接口推理已集成在run_visualglm_pipeline.py脚本中,运行此脚本命令示例:
cd research/visualglm
python run_visualglm_pipeline.py --config CONFIG_PATH --device_id DEVICE_ID --batch_size BATCH_SIZE --use_past True --seq_length SEQ_LENGTH \
--image_path IMAGE_PATH --prompt PROMPT
# 运行结果
#['<img> </img>问:描述这张图片。\n答: 泰坦尼克号 电影截图']
参数说明
# 如果需要使用增量推理,配置文件中use_past设置为True
cd research/visualglm
python export_lite_model.py --mode export --use_past True --device_id DEVICE_ID --seq_length SEQ_LENGTH --batch_size BATCH_SIZE
参数说明
注意
[ascend_context]
plugin_custom_ops=All
provider=ge
[ge_session_options]
ge.exec.formatMode=1
ge.exec.precision_mode=must_keep_origin_dtype
ge.externalWeight=1
ge.exec.atomicCleanPolicy=1
替换/path/to/glm_6b_for_lite.ckpt为实际的glm for lite模型的路径
model:
model_config:
#...
text_config:
type: GLMConfig
#...
checkpoint_name_or_path: "/path/to/glm_6b_for_lite.ckpt" # visualglm glm weight
visualglm的lite推理已集成在run_visualglm_infer_lite脚本中,运行此脚本命令示例:
# 如果需要增量推理,使用inc_model_path指定路径,否则不需要
cd research
python run_visualglm_infer_lite.py --full_model_path FULL_MODEL_PATH --inc_model_path INC_MODEL_PATH \
--seq_length SEQ_LENGTH --ge_config context.cfg --device_id DEVICE_ID --image_path IMAGE_PATH --prompt PROMPT
# 运行结果:
#['<img> </img>问:描述这张图片。\n答: 泰坦尼克号 电影截图']
参数说明
注意
注意:目前lora微调只支持数据并行,不支持半自动并行和自动并行
数据集路径:
https://github.com/THUDM/VisualGLM-6B/blob/main/fewshot-data.zip
下载完以后传到服务器,解压到research/visualglm下面
记录下fewhot-data/dataset.json文件的路径
修改/research/visualglm/run_visualglm_lora.yaml文件:
train_dataset: &train_dataset
tokenizer:
type: ChatGLMTokenizer
max_length: 2048
vocab_file: "/path_to_vocab/ice_text.model"
data_loader:
type: VisualGLMDataLoader
dataset_dir: "/path_to_dataset/dataset.json"
shuffle: False
file_format: json
random_mapping: True # if true enlarge original dataset "scale" times
scale: 1
model:
model_config:
type: VisualGLMConfig
#...
checkpoint_name_or_path: "/path_to_qformer/visualglm_qformer.ckpt"
vision_config:
type: ViTConfig
#...
checkpoint_name_or_path: "/path_to_vit/visualglm_vit.ckpt"
text_config:
type: GLMConfig
#...
checkpoint_name_or_path: "/path_to_glm/glm_6b.ckpt"
processor:
type: VisualGLMProcessor
image_processor:
type: VisualGLMImageProcessor
image_size: 224 # input image size
tokenizer:
type: ChatGLMTokenizer
max_length: 2048
vocab_file: "/path_to_vocab/ice_text.model"
调整learning rate和warmup超参,修改/research/visualglm/run_visualglm_lora.yaml文件,根据实际业务调整下面的超参:
# lr sechdule
lr_schedule:
type: AnnealingLR
learning_rate: 0.00001
warmup_steps: 100
num_iters: 5000
total_steps: -1 # -1 means it will load the total steps of the dataset
layer_scale: False
layer_decay: 0.65
调用下面的脚本启动微调:
cd research/visualglm
python run_visualglm_finetune.py --config CONFIG_PATH --graph_mode GRAPH_MODE --batch_size BATCH_SIZE --device_id DEVICE_ID
参数说明
运行mindformers/tools/hccl_tools.py生成RANK_TABLE_FILE的json文件
这会生成一个名字为hccl_8p_01234567_XXXX.json的文件
# 运行如下命令,生成当前机器的RANK_TABLE_FILE的json文件
python mindformers/tools/hccl_tools.py --device_num "[START_ID, END_ID)"
参数说明:
修改run_visualglm_lora.yaml中的并行参数
use_parallel: True
parallel:
parallel_mode: 0
parallel_config:
data_parallel: 8
model_parallel: 1
运行run_singlenode.sh脚本来执行多卡训练
cd research/visualglm
bash ../run_singlenode.sh \
"python run_visualglm_finetune.py --config CONFIG_PATH --graph_mode GRAPH_MODE --batch_size BATCH_SIZE" \
HCCL_JSON_PATH [START_ID, END_ID] CARD_COUNT
参数说明
注意
注意
调用预先开发好的脚本run_visualglm_with_lora.py,传入相关的图片和提示词,会得到相关的文本。
python run_visualglm_with_lora.py --lora_checkpoint CHECKPOINT_PATH --config CONFIG_PATH --image_path=IMAGE_PATH --prompt=PROMPT --device_id DEVICE_ID
#运行结果:
#['这张图片是雨天的。']
说明:
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》