README.md 6.98 KB
Newer Older
chenych's avatar
chenych committed
1
2
# GLM-4.1V
## 论文
chenych's avatar
chenych committed
3
[GLM-4.1V-Thinking: Towards Versatile Multimodal Reasoning with Scalable Reinforcement Learning](https://arxiv.org/abs/2507.01006)
chenych's avatar
chenych committed
4

chenych's avatar
chenych committed
5
## 模型简介
chenych's avatar
chenych committed
6
7
GLM-4.1V-thinging旨在探索视觉语言模型推理能力的上限,通过引入“思考范式”并利用采样强化学习 RLCS(Reinforcement Learning with Curriculum Sampling)全面提升模型能力。在 100 亿参数的视觉语言模型中,其性能处于领先地位,在 18 项基准测试任务中与 720 亿参数的 Qwen-2.5-VL-72B 相当甚至更优。

chenych's avatar
chenych committed
8
与上一代的 CogVLM2 及 GLM-4V 系列模型相比,**GLM-4.1V-Thinking**有如下改进:
chenych's avatar
chenych committed
9
1. 系列中首个推理模型,不仅仅停留在数学领域,在多个子领域均达到世界前列的水平。
chenych's avatar
chenych committed
10
11
2. 支持**64k**上下长度。
3. 支持**任意长宽比**和高达**4k**的图像分辨率。
chenych's avatar
chenych committed
12
13
4. 提供支持**中英文双语**的开源模型版本。

chenych's avatar
chenych committed
14
15
16
17
18
GLM-4.1V-Thinking能够将图像和视频以其原始的分辨率和宽高比进行识别。对于视频输入,会在每帧后面插入额外的时间索引标记,以增强模型的时间理解能力。GLM-4.1V-Thinking 有三部分组成:
1. 一个视觉 Transformer 编码器,用于处理和编码图像及视频;
2. 一个多层感知机投影器,用于将视觉特征与标记对齐;
3. 一个大型语言模型作为语言解码器,用于处理多模态标记并生成标记补全内容。

chenych's avatar
chenych committed
19
<div align=center>
chenych's avatar
chenych committed
20
    <img src="./doc/model.png"/>
chenych's avatar
chenych committed
21
22
</div>

chenych's avatar
chenych committed
23
24
25
26
27
28
29
30
## 环境依赖
| 软件 | 版本 |
| :------: | :------: |
| DTK | 25.04.2 |
| python | 3.10.12 |
| torch | 2.5.1+das.opt1.dtk25042 |
| transformers | 4.53.2 |
| vllm | 0.11.0 |
chenych's avatar
chenych committed
31

chenych's avatar
chenych committed
32
33
推荐使用镜像:
- 挂载地址`-v`根据实际模型情况修改
chenych's avatar
chenych committed
34

chenych's avatar
chenych committed
35
36
```bash
docker run -it --shm-size 60g --network=host --name glm-41v --privileged --device=/dev/kfd --device=/dev/dri --device=/dev/mkfd --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro image.sourcefind.cn:5000/dcu/admin/base/custom:vllm-ubuntu22.04-dtk25.04.2-py3.10-minimax-m2 bash
chenych's avatar
chenych committed
37
```
chenych's avatar
chenych committed
38
更多镜像可前往[光源](https://sourcefind.cn/#/service-list)下载使用。
chenych's avatar
chenych committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.sourcefind.cn/tool/)开发者社区下载安装。

## 数据集
[LLaMA-Factory](https://developer.sourcefind.cn/codes/OpenDAS/llama-factory)已经支持本模型的微调。以下是构建数据集的说明,这是一个使用了两张图片的数据集。你需要将数据集整理为`finetune.json`,然后根据llama-factory中的数据配置进行相关修改。

```json
[
  {
    "messages": [
      {
        "content": "<image>Who are they?",
        "role": "user"
      },
      {
        "content": "<think>\nUser ask me to observe the image and get the answer. I Know they are Kane and Gretzka from Bayern Munich.</think>\n<answer>They're Kane and Gretzka from Bayern Munich.</answer>",
        "role": "assistant"
      },
      {
        "content": "<image>What are they doing?",
        "role": "user"
      },
      {
        "content": "<think>\nI need to observe what this people are doing. Oh, They are celebrating on the soccer field.</think>\n<answer>They are celebrating on the soccer field.</answer>",
        "role": "assistant"
      }
    ],
    "images": [
      "mllm_demo_data/1.jpg",
      "mllm_demo_data/2.jpg"
    ]
  }
]
```

1. `<think> XXX </think>` 中的部分不会被存放为历史记录和微调。
2. `<image>` 标签会被替换成图片信息。

## 训练
### Llama Factory 微调方法(推荐)
chenych's avatar
chenych committed
79
80
根据[LLaMA-Factory](https://developer.sourcefind.cn/codes/OpenDAS/llama-factory)仓库指引安装好llama-factory后,请检查下transformers版本,如果版本不等于4.53.2,需要重新安装`pip install transformers==4.53.2`

chenych's avatar
chenych committed
81
因为transformers版本与[LLaMA-Factory](https://developer.sourcefind.cn/codes/OpenDAS/llama-factory)版本不一致,启动训练前需要先增加环境变量来跳过版本检查,环境变量如下:
chenych's avatar
chenych committed
82
83
84
85

```
export DISABLE_VERSION_CHECK=1
```
chenych's avatar
chenych committed
86

chenych's avatar
chenych committed
87
88
> **Tips:**
> 单卡微调请注释掉yaml文件中的`deepspeed`参数
chenych's avatar
chenych committed
89

chenych's avatar
chenych committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#### 全参微调
SFT训练脚本示例,参考`llama-factory/train_full`下对应yaml文件。

**参数修改**
- **--model_name_or_path**: 修改为待训练模型地址,如 `/data/GLM-4.1V-9B-Thinking`
- **--dataset**: 微调训练集名称,可选数据集请参考 `llama-factory/data/dataset_info.json`
- **--template**: 将 default 修改为 `glm4v`
- **--output_dir**: 模型保存地址

其他参数如:`--learning_rate``--save_steps`可根据自身硬件及需求进行修改。

#### lora微调
SFT训练脚本示例,参考`llama-factory/train_lora`下对应yaml文件。
参数解释同[#全参微调](#全参微调)

## 推理
chenych's avatar
chenych committed
106
### transformers
chenych's avatar
chenych committed
107
108
109
- `trans_infer_transformers.py`: 使用`transformers`库进行单次对话推理。
- `trans_infer_cli.py`: 使用`transformers`库作为推理后端的命令行交互脚本。你可以使用它进行连续对话。

chenych's avatar
chenych committed
110
```bash
chenych's avatar
chenych committed
111
112
113
114
115
116
# 单次推理启动命令如下
python inference/trans_infer_transformers.py
# 交互对话启动命令如下:
python inference/trans_infer_cli.py
```

chenych's avatar
chenych committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
### vllm
```bash
export HIP_VISIBLE_DEVICES=0
export ALLREDUCE_STREAM_WITH_COMPUTE=1
export HSA_FORCE_FINE_GRAIN_PCIE=1
## 启动服务
vllm serve THUDM/GLM-4.1V-9B-Thinking \
  --limit-mm-per-prompt '{"image":32}' \
  --allowed-local-media-path / \
  --trust-remote-code \
  --max-model-len 32768 \
  --served-model-name glm-4.1v-thinking

## 访问
curl http://localhost:8000/v1/chat/completions  \
    -H "Content-Type: application/json"  \
    -d '{
        "model": "glm-4.1v-thinking",
        "messages": [
        {
          "role": "user",
          "content": [
            {
              "type": "image_url",
              "image_url": {"url": "file:///home/glm-4.1v_pytorch/doc/Grayscale_8bits_palette_sample_image.png"}
            },
            {
              "type": "text",
              "text": "describe this image"
            }
          ]
        }],
        "temperature": 0.7
    }'
```
chenych's avatar
chenych committed
152

chenych's avatar
chenych committed
153
## 效果展示
chenych's avatar
chenych committed
154
155
156
157
158
159
160
161
162
163
<div align=center>
    <img src="./doc/results-dcu.png"/>
</div>

### 精度
测试数据:[test data](https://developer.sourcefind.cn/codes/OpenDAS/llama-factory/-/blob/master/data/alpaca_en_demo.json),使用的加速卡:K100_AI。
模型:GLM-4.1V-9B-Thinking

| device | iters | train_loss |
| :------: | :------: | :------: |
chenych's avatar
chenych committed
164
| A800 | 375 | 0.5245 |
chenych's avatar
chenych committed
165
166
167
| K100_AI | 375 | 0.5264 |

## 预训练权重
chenych's avatar
chenych committed
168
169
170
171
| 模型名称  | 权重大小  | DCU型号  | 最低卡数需求 |下载地址|
|:-----:|:----------:|:----------:|:---------------------:|:----------:|
| GLM-4.1V-9B-Base | 9B | K100AI,BW1000 | 1 | [下载地址](https://huggingface.co/THUDM/GLM-4.1V-9B-Base) |
| GLM-4.1V-9B-Thinking | 9B | K100AI,BW1000 | 1 | [下载地址](https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking) |
chenych's avatar
chenych committed
172
173
174
175
176
177

## 源码仓库及问题反馈
- https://developer.sourcefind.cn/codes/modelzoo/glm-4.1v_pytorch

## 参考资料
- https://github.com/THUDM/GLM-4.1V-Thinking