2025-07-02 20:03 浙江
智谱AI推出GLM-4.1V-9B-Thinking视觉语言大模型,仅10B参数却在18项任务中持平甚至超越8倍参数量的Qwen-2.5-VL-72B,支持64k上下文长度、4k图像分辨率及中英文双语,开源助力研究突破。
01
模型推荐
- 系列中首个推理模型,不仅仅停留在数学领域,在多个子领域均达到世界前列的水平。支持 64k 上下长度。支持任意长宽比和高达 4k 的图像分辨率。提供支持中英文双语的开源模型版本。
02
榜单信息
03
快速推理
transformers
进行单张图片推理的代码。首先,从源代码安装transformers
库。接着按照以下代码运行:pip install git+https://github.com/huggingface/transformers.git
from modelscope import AutoProcessor, Glm4vForConditionalGeneration
import torch
MODEL_PATH = "ZhipuAI/GLM-4.1V-9B-Thinking"
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"url": "https://upload.wikimedia.org/wikipedia/commons/f/fa/Grayscale_8bits_palette_sample_image.png"
},
{
"type": "text",
"text": "describe this image"
}
],
}
]
processor = AutoProcessor.from_pretrained(MODEL_PATH, use_fast=True)
model = Glm4vForConditionalGeneration.from_pretrained(
pretrained_model_name_or_path=MODEL_PATH,
torch_dtype=torch.bfloat16,
device_map="auto",
)
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt"
).to(model.device)
generated_ids = model.generate(**inputs, max_new_tokens=8192)
output_text = processor.decode(generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False)
print(output_text)
显存占用:
04
模型微调
# pip install git+https://github.com/modelscope/ms-swift.git
git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
pip install -e .
以
ZhipuAI/GLM-4.1V-9B-Thinking模型为例,图像OCR微调脚本如下。更多任务,包括视频微调、grounding任务微调,可以参考这里:https://github.com/modelscope/ms-swift/tree/main/examples/train/multimodal训练显存占用:自定义数据集格式如下(system字段可选),只需要指定`--dataset <dataset_path>`即可:CUDA_VISIBLE_DEVICES=0 \
swift sft \
--model ZhipuAI/GLM-4.1V-9B-Thinking \
--dataset AI-ModelScope/LaTeX_OCR:human_handwrite#20000 \
--train_type lora \
--torch_dtype bfloat16 \
--num_train_epochs 1 \
--per_device_train_batch_size 1 \
--per_device_eval_batch_size 1 \
--learning_rate 1e-4 \
--lora_rank 8 \
--lora_alpha 32 \
--target_modules all-linear \
--freeze_vit true \
--gradient_accumulation_steps 16 \
--eval_steps 50 \
--save_steps 50 \
--save_total_limit 5 \
--logging_steps 5 \
--max_length 2048 \
--output_dir output \
--warmup_ratio 0.05 \
--dataloader_num_workers 4
训练完成后,使用以下命令对训练时的验证集进行推理:{"messages": [{"role": "user", "content": "浙江的省会在哪?"}, {"role": "assistant", "content": "浙江的省会在杭州。"}]}
{"messages": [{"role": "user", "content": "<image><image>两张图片有什么区别"}, {"role": "assistant", "content": "前一张是小猫,后一张是小狗"}], "images": ["/xxx/x.jpg", "/xxx/x.png"]}
{"messages": [{"role": "system", "content": "你是个有用无害的助手"}, {"role": "user", "content": "<video>视频中是什么"}, {"role": "assistant", "content": "视频中是一只小狗在草地上奔跑"}], "videos": ["/xxx/x.mp4"]}
推送模型到ModelScope:CUDA_VISIBLE_DEVICES=0 \
swift infer \
--adapters output/vx-xxx/checkpoint-xxx \
--stream false \
--max_batch_size 1 \
--load_data_args true \
--max_new_tokens 2048
CUDA_VISIBLE_DEVICES=0 \
swift export \
--adapters output/vx-xxx/checkpoint-xxx \
--push_to_hub true \
--hub_model_id '<your-model-id>' \
--hub_token '<your-sdk-token>'
点击阅读原文, 即可跳转模型链接~