LucaMotz commited on
Commit
0e64a14
·
verified ·
1 Parent(s): 14a2004

Add native Transformers support (config and processor files only)

Browse files

This PR adds native Transformers support for MiniCPM-V 4.5, images and video, while keeping the existing remote code working. It goes with https://github.com/huggingface/transformers/pull/51300, which adds the model to Transformers as `minicpmv4_5`, on top of MiniCPM-V 4.0's `minicpmv2_6` (https://github.com/huggingface/transformers/pull/51299; its Hub files are openbmb/MiniCPM-V-4#6, with the same layout).

**The weights are not changed.** Transformers maps the original weight names at load time.

**It can be merged now:** the remote code keeps loading as before (`trust_remote_code=True`, `auto_map` unchanged), and Transformers versions that include the model load it natively.

| File | Change |
|---|---|
| `config.json` | All original keys kept, incl. `auto_map`. Added: `model_type: "minicpmv4_5"`, `text_config`, `resampler_n_latents`, `resampler_n_heads`, `image_token_id`, `video_token_id`. `vision_config` gets `model_type: "minicpmv4_5_vision"` and the activation and norm epsilon the remote code assumes (it only reads its sizes), and `slice_config` no longer has its own `model_type` (on Transformers 4.57 the remote config was otherwise built from that sub-dict). |
| `configuration_minicpm.py` | 1 line: `MiniCPMVConfig` accepts and ignores `text_config`, so `get_text_config()` keeps returning the config itself. |
| `preprocessor_config.json` | Original keys and `auto_map` kept; the native image processor settings are added next to them, and `image_processor_type` names the native class (the remote code still loads its own through `auto_map`). |
| `processor_config.json` | New: native processor settings, including the video processor (frames sampled as in the model card's `encode_video`), plus an `auto_map` so the remote processor is still found. |
| `chat_template.jinja` | New: the original Qwen3 template, with message content rendered from content lists (images as `<image_id>N</image_id><unk>`, videos as `<|video_pad|>`). |
| `tokenizer_config.json` | `padding_side: "left"`. |

I used Claude Code for parts of this change.

<details><summary>How it was checked</summary>

- Remote code (`trust_remote_code=True`) on Transformers 4.57.3: the same `input_ids` as the current revision, and the same greedy output (bf16 on CUDA). vLLM's MiniCPM-V, which loads the remote processor on Transformers 5, loads this revision as before.
- Native (the Transformers PR branch, no remote code): image prompts tokenize to the same `input_ids` as the remote processor, and fp32 greedy generation matches the remote code on images and on a real video clip (details in the Transformers PR).
</details>

chat_template.jinja ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- macro render_content(content, ns) -%}{%- if content is string -%}{{- content -}}{%- else -%}{%- set parts = namespace(items=[]) -%}{%- for item in content -%}{%- if item['type'] == 'image' -%}{%- set parts.items = parts.items + ['<image_id>' ~ ns.image_count ~ '</image_id><unk>'] -%}{%- set ns.image_count = ns.image_count + 1 -%}{%- elif item['type'] == 'text' -%}{%- set parts.items = parts.items + [item['text']] -%}{%- elif item['type'] == 'video' -%}{%- set parts.items = parts.items + ['<|video_pad|>'] -%}{%- endif -%}{%- endfor -%}{{- parts.items | join('
2
+ ') -}}{%- endif -%}{%- endmacro -%}{%- set images = namespace(image_count=0) -%}
3
+ {%- if tools %}
4
+ {{- '<|im_start|>system\n' }}
5
+ {%- if messages[0].role == 'system' %}
6
+ {{- render_content(messages[0].content, images) + '\n\n' }}
7
+ {%- endif %}
8
+ {{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
9
+ {%- for tool in tools %}
10
+ {{- "\n" }}
11
+ {{- tool | tojson }}
12
+ {%- endfor %}
13
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
14
+ {%- else %}
15
+ {%- if messages[0].role == 'system' %}
16
+ {{- '<|im_start|>system\n' + render_content(messages[0].content, images) + '<|im_end|>\n' }}
17
+ {%- endif %}
18
+ {%- endif %}
19
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
20
+ {%- for message in messages[::-1] %}
21
+ {%- set index = (messages|length - 1) - loop.index0 %}
22
+ {%- if ns.multi_step_tool and message.role == "user" and not(message.content is string and message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
23
+ {%- set ns.multi_step_tool = false %}
24
+ {%- set ns.last_query_index = index %}
25
+ {%- endif %}
26
+ {%- endfor %}
27
+ {%- for message in messages %}
28
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
29
+ {{- '<|im_start|>' + message.role + '\n' + render_content(message.content, images) + '<|im_end|>' + '\n' }}
30
+ {%- elif message.role == "assistant" %}
31
+ {%- set content = render_content(message.content, images) %}
32
+ {%- set reasoning_content = '' %}
33
+ {%- if message.reasoning_content is defined and message.reasoning_content is not none %}
34
+ {%- set reasoning_content = message.reasoning_content %}
35
+ {%- else %}
36
+ {%- if '</think>' in message.content %}
37
+ {%- set content = message.content.split('</think>')[-1].lstrip('\n') %}
38
+ {%- set reasoning_content = message.content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
39
+ {%- endif %}
40
+ {%- endif %}
41
+ {%- if loop.index0 > ns.last_query_index %}
42
+ {%- if loop.last or (not loop.last and reasoning_content) %}
43
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
44
+ {%- else %}
45
+ {{- '<|im_start|>' + message.role + '\n' + content }}
46
+ {%- endif %}
47
+ {%- else %}
48
+ {{- '<|im_start|>' + message.role + '\n' + content }}
49
+ {%- endif %}
50
+ {%- if message.tool_calls %}
51
+ {%- for tool_call in message.tool_calls %}
52
+ {%- if (loop.first and content) or (not loop.first) %}
53
+ {{- '\n' }}
54
+ {%- endif %}
55
+ {%- if tool_call.function %}
56
+ {%- set tool_call = tool_call.function %}
57
+ {%- endif %}
58
+ {{- '<tool_call>\n{"name": "' }}
59
+ {{- tool_call.name }}
60
+ {{- '", "arguments": ' }}
61
+ {%- if tool_call.arguments is string %}
62
+ {{- tool_call.arguments }}
63
+ {%- else %}
64
+ {{- tool_call.arguments | tojson }}
65
+ {%- endif %}
66
+ {{- '}\n</tool_call>' }}
67
+ {%- endfor %}
68
+ {%- endif %}
69
+ {{- '<|im_end|>\n' }}
70
+ {%- elif message.role == "tool" %}
71
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
72
+ {{- '<|im_start|>user' }}
73
+ {%- endif %}
74
+ {{- '\n<tool_response>\n' }}
75
+ {{- render_content(message.content, images) }}
76
+ {{- '\n</tool_response>' }}
77
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
78
+ {{- '<|im_end|>\n' }}
79
+ {%- endif %}
80
+ {%- endif %}
81
+ {%- endfor %}
82
+ {%- if add_generation_prompt %}
83
+ {{- '<|im_start|>assistant\n' }}
84
+ {%- if enable_thinking is defined and enable_thinking is false %}
85
+ {{- '<think>\n\n</think>\n\n' }}
86
+ {%- endif %}
87
+ {%- if enable_thinking is defined and enable_thinking is true %}
88
+ {{- '<think>\n' }}
89
+ {%- endif %}
90
+ {%- endif %}
config.json CHANGED
@@ -37,22 +37,48 @@
37
  "batch_3d_resampler": true,
38
  "drop_vision_last_layer": false,
39
  "image_size": 448,
40
- "model_type": "minicpmv",
41
  "patch_size": 14,
42
  "query_num": 64,
43
  "slice_config": {
44
  "max_slice_nums": 9,
45
- "patch_size": 14,
46
- "model_type": "minicpmv"
47
  },
48
  "slice_mode": true,
49
  "vision_config": {
50
  "hidden_size": 1152,
51
  "image_size": 980,
52
  "intermediate_size": 4304,
53
- "model_type": "siglip",
54
  "num_attention_heads": 16,
55
  "num_hidden_layers": 27,
56
- "patch_size": 14
57
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  }
 
37
  "batch_3d_resampler": true,
38
  "drop_vision_last_layer": false,
39
  "image_size": 448,
40
+ "model_type": "minicpmv4_5",
41
  "patch_size": 14,
42
  "query_num": 64,
43
  "slice_config": {
44
  "max_slice_nums": 9,
45
+ "patch_size": 14
 
46
  },
47
  "slice_mode": true,
48
  "vision_config": {
49
  "hidden_size": 1152,
50
  "image_size": 980,
51
  "intermediate_size": 4304,
52
+ "model_type": "minicpmv4_5_vision",
53
  "num_attention_heads": 16,
54
  "num_hidden_layers": 27,
55
+ "patch_size": 14,
56
+ "layer_norm_eps": 1e-06,
57
+ "hidden_act": "gelu_pytorch_tanh"
58
+ },
59
+ "text_config": {
60
+ "model_type": "qwen3",
61
+ "vocab_size": 151748,
62
+ "hidden_size": 4096,
63
+ "intermediate_size": 12288,
64
+ "num_hidden_layers": 36,
65
+ "num_attention_heads": 32,
66
+ "num_key_value_heads": 8,
67
+ "head_dim": 128,
68
+ "hidden_act": "silu",
69
+ "max_position_embeddings": 40960,
70
+ "rms_norm_eps": 1e-06,
71
+ "attention_bias": false,
72
+ "bos_token_id": 151643,
73
+ "eos_token_id": 151645,
74
+ "initializer_range": 0.02,
75
+ "rope_parameters": {
76
+ "rope_type": "default",
77
+ "rope_theta": 1000000
78
+ }
79
+ },
80
+ "resampler_n_latents": 64,
81
+ "resampler_n_heads": 32,
82
+ "image_token_id": 128244,
83
+ "video_token_id": 151656
84
  }
configuration_minicpm.py CHANGED
@@ -68,6 +68,7 @@ class MiniCPMVConfig(Qwen3Config):
68
  batch_vision_input=True,
69
  slice_config=None,
70
  vision_config=None,
 
71
  use_image_id=True,
72
  vision_batch_size=16,
73
  batch_3d_resampler=True,
 
68
  batch_vision_input=True,
69
  slice_config=None,
70
  vision_config=None,
71
+ text_config=None, # read by the native Transformers class, not here
72
  use_image_id=True,
73
  vision_batch_size=16,
74
  batch_3d_resampler=True,
preprocessor_config.json CHANGED
@@ -1,9 +1,9 @@
1
  {
2
- "image_processor_type": "MiniCPMVImageProcessor",
3
  "auto_map": {
4
  "AutoProcessor": "processing_minicpmv.MiniCPMVProcessor",
5
  "AutoImageProcessor": "image_processing_minicpmv.MiniCPMVImageProcessor"
6
- },
7
  "processor_class": "MiniCPMVProcessor",
8
  "max_slice_nums": 9,
9
  "scale_resolution": 448,
@@ -18,7 +18,31 @@
18
  "im_id_start": "<image_id>",
19
  "im_id_end": "</image_id>",
20
  "slice_mode": true,
21
- "norm_mean": [0.5, 0.5, 0.5],
22
- "norm_std": [0.5, 0.5, 0.5],
23
- "version": 2.6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
 
1
  {
2
+ "image_processor_type": "MiniCPMV2_5ImageProcessor",
3
  "auto_map": {
4
  "AutoProcessor": "processing_minicpmv.MiniCPMVProcessor",
5
  "AutoImageProcessor": "image_processing_minicpmv.MiniCPMVImageProcessor"
6
+ },
7
  "processor_class": "MiniCPMVProcessor",
8
  "max_slice_nums": 9,
9
  "scale_resolution": 448,
 
18
  "im_id_start": "<image_id>",
19
  "im_id_end": "</image_id>",
20
  "slice_mode": true,
21
+ "norm_mean": [
22
+ 0.5,
23
+ 0.5,
24
+ 0.5
25
+ ],
26
+ "norm_std": [
27
+ 0.5,
28
+ 0.5,
29
+ 0.5
30
+ ],
31
+ "version": 2.6,
32
+ "do_convert_rgb": true,
33
+ "do_normalize": true,
34
+ "do_rescale": true,
35
+ "do_resize": true,
36
+ "image_mean": [
37
+ 0.5,
38
+ 0.5,
39
+ 0.5
40
+ ],
41
+ "image_std": [
42
+ 0.5,
43
+ 0.5,
44
+ 0.5
45
+ ],
46
+ "resample": 3,
47
+ "rescale_factor": 0.00392156862745098
48
  }
processor_config.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "image_token": "<unk>",
3
+ "num_image_tokens": 64,
4
+ "processor_class": "MiniCPMV4_5Processor",
5
+ "auto_map": {
6
+ "AutoProcessor": "processing_minicpmv.MiniCPMVProcessor"
7
+ },
8
+ "video_token": "<|video_pad|>",
9
+ "video_processor": {
10
+ "video_processor_type": "MiniCPMV4_5VideoProcessor",
11
+ "fps": 5,
12
+ "max_num_frames": 180,
13
+ "max_num_packing": 3,
14
+ "max_slice_nums": 1,
15
+ "scale_resolution": 448,
16
+ "patch_size": 14
17
+ }
18
+ }
tokenizer_config.json CHANGED
@@ -949,5 +949,6 @@
949
  null
950
  ]
951
  },
952
- "tokenizer_class": "MiniCPMVTokenizerFast"
 
953
  }
 
949
  null
950
  ]
951
  },
952
+ "tokenizer_class": "MiniCPMVTokenizerFast",
953
+ "padding_side": "left"
954
  }