LucaMotz commited on
Commit
3af0101
·
verified ·
1 Parent(s): cf0b69a

Add native Transformers support (config and processor files only)

Browse files

This PR adds native Transformers support for MiniCPM-o 2.6 (image and audio in, text out) while keeping the existing remote code working. It goes with https://github.com/huggingface/transformers/pull/51319, which adds the model to Transformers as `minicpmo2_6`, on top of MiniCPM-V 2.6 and 4.0 (https://github.com/huggingface/transformers/pull/51299).

**The weights are not changed.** Transformers maps the original weight names at load time. The speech decoder (`tts.*`) isn't loaded by the native model.

**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. The native chat template is in `chat_template.json`, which only the processor reads, not in `chat_template.jinja`: the remote tokenizer would pick up a `.jinja` file too, and vLLM's MiniCPM-O, which ends up with the tokenizer's template because the remote processor has none, would then fail on image and audio chat requests (it looks for `(<image>./</image>)` and `(<audio>./</audio>)` in the prompt).

| File | Change |
|---|---|
| `config.json` | All original keys kept, incl. `auto_map`. `model_type` changes from `minicpmo` to `minicpmo2_6`. Added: `text_config`, `resampler_n_latents`, `resampler_n_heads`, `image_token_id`, `audio_token_id`. `vision_config` gets `model_type: "minicpmv2_6_vision"` and the activation and norm epsilon the remote code assumes, `audio_config` gets `model_type: "minicpmo2_6_audio"` and `chunk_size` (the remote code only reads their 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). With remote code, Transformers now warns that a `minicpmo2_6` config instantiates a `minicpmo` model; the remote code doesn't read `model_type`. |
| `configuration_minicpm.py` | 1 line: `MiniCPMOConfig` accepts and ignores `text_config`, so `get_text_config()` keeps returning the config itself. |
| `processing_minicpmo.py` | 1 line: `MiniCPMOProcessor` accepts and ignores `chat_template`, which Transformers 4.49 to 4.52 pass to it from `chat_template.json`. |
| `preprocessor_config.json` | Original keys and `auto_map` kept; the native image processor settings and `feature_extractor_type` 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: only `processor_class` and an `auto_map`, so the remote processor is still found. The remote `MiniCPMOProcessor` takes no other arguments, and on Transformers 4.44.2 extra keys here would be passed to it. |
| `chat_template.json` | New: chat template for the native processor (the tokenizer's template with images and audio in the content, and the TTS prompt of the remote `chat()` when the conversation has audio). The tokenizer keeps its own template. |
| `generation_config.json` | New: the end-of-turn and end-of-text tokens the remote `chat()` stops on (151645, 151643), with end-of-text as BOS and 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 12 image and audio conversations: the remote processor gives the same `input_ids` and features from this revision as from the current one, on Transformers 4.44.2 (the model card's version) and 4.57.3, and it loads on 4.45.1, 4.48.3, 4.49.0, 4.51.3, 4.52.4 and 4.53.0 as well. On 4.44.2 the remote model still builds and runs `chat()` (tiny random weights).
- vLLM's MiniCPM-O (bf16 on CUDA, prompts as strings): the same greedy output with this PR's config, tokenizer and generation files as with the current ones, 12 of 12.
- vLLM's chat path (vLLM main and a87e3a6554 with Transformers 5.23.0 and 5.24.1, an image and an audio chat request, up to the model's prompt processing): the same prompt ids from this revision as from the current one. With the template as `chat_template.jinja`, both requests fail there.
- Native (the Transformers PR branch, no remote code): the same `input_ids` as the remote processor apart from the audio placeholder token, and fp32 greedy generation identical to the remote code in 12 of 12 conversations (details in the Transformers PR).
</details>

chat_template.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "chat_template": "{%- 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'] == 'audio' -%}{%- set parts.items = parts.items + ['<|audio|>'] -%}{%- elif item['type'] == 'text' -%}{%- set parts.items = parts.items + [item['text']] -%}{%- endif -%}{%- endfor -%}{{- parts.items | join('\n') -}}{%- endif -%}{%- endmacro -%}{%- set images = namespace(image_count=0) -%}{%- set tts = namespace(enabled=use_tts_template | default(false)) -%}{%- for message in messages -%}{%- if message['content'] is not string -%}{%- for item in message['content'] -%}{%- if item['type'] == 'audio' -%}{%- set tts.enabled = true -%}{%- endif -%}{%- endfor -%}{%- endif -%}{%- endfor -%}{%- if tts.enabled -%}{%- for message in messages -%}{{- '<|im_start|>' + message['role'] + '\n' + render_content(message['content'], images) + '<|im_end|>\n' -}}{%- endfor -%}{%- if add_generation_prompt -%}{{- '<|im_start|>assistant\n<|spk_bos|><|spk|><|spk_eos|><|tts_bos|>' -}}{%- endif -%}{%- else -%}{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0]['role'] == 'system' %}\n {{- render_content(messages[0]['content'], images) }}\n {%- else %}\n {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }}\n {%- endif %}\n {{- \"\\n\\n# 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>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\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\" }}\n{%- else %}\n {%- if messages[0]['role'] == 'system' %}\n {{- '<|im_start|>system\\n' + render_content(messages[0]['content'], images) + '<|im_end|>\\n' }}\n {%- else %}\n {{- '<|im_start|>system\\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- for message in messages %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) or (message.role == \"assistant\" and not message.tool_calls) %}\n {{- '<|im_start|>' + message.role + '\\n' + render_content(message.content, images) + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {{- '<|im_start|>' + message.role }}\n {%- if message.content %}\n {{- '\\n' + render_content(message.content, images) }}\n {%- endif %}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '\\n<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {{- tool_call.arguments | tojson }}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- render_content(message.content, images) }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n{%- endif %}\n{%- endif -%}"
3
+ }
config.json CHANGED
@@ -3,7 +3,6 @@
3
  "architectures": [
4
  "MiniCPMO"
5
  ],
6
-
7
  "attention_dropout": 0.0,
8
  "bos_token_id": 151643,
9
  "eos_token_id": 151645,
@@ -25,7 +24,6 @@
25
  "batch_vision_input": true,
26
  "drop_vision_last_layer": false,
27
  "image_size": 448,
28
-
29
  "audio_chunk_length": 1.0,
30
  "audio_config": {
31
  "_name_or_path": "openai/whisper-medium",
@@ -61,7 +59,7 @@
61
  ]
62
  ],
63
  "max_length": 448,
64
- "model_type": "whisper",
65
  "num_hidden_layers": 24,
66
  "pad_token_id": 50257,
67
  "suppress_tokens": [
@@ -154,7 +152,8 @@
154
  50361,
155
  50362
156
  ],
157
- "torch_dtype": "float32"
 
158
  },
159
  "audio_pool_step": 2,
160
  "auto_map": {
@@ -164,12 +163,11 @@
164
  },
165
  "chunk_input": true,
166
  "listen_speak_type": "asr",
167
- "model_type": "minicpmo",
168
  "patch_size": 14,
169
  "query_num": 64,
170
  "slice_config": {
171
- "max_slice_nums": 9,
172
- "model_type": "minicpmv"
173
  },
174
  "slice_mode": true,
175
  "torch_dtype": "bfloat16",
@@ -186,9 +184,37 @@
186
  "hidden_size": 1152,
187
  "image_size": 980,
188
  "intermediate_size": 4304,
189
- "model_type": "siglip_vision_model",
190
  "num_attention_heads": 16,
191
  "num_hidden_layers": 27,
192
- "patch_size": 14
193
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  }
 
3
  "architectures": [
4
  "MiniCPMO"
5
  ],
 
6
  "attention_dropout": 0.0,
7
  "bos_token_id": 151643,
8
  "eos_token_id": 151645,
 
24
  "batch_vision_input": true,
25
  "drop_vision_last_layer": false,
26
  "image_size": 448,
 
27
  "audio_chunk_length": 1.0,
28
  "audio_config": {
29
  "_name_or_path": "openai/whisper-medium",
 
59
  ]
60
  ],
61
  "max_length": 448,
62
+ "model_type": "minicpmo2_6_audio",
63
  "num_hidden_layers": 24,
64
  "pad_token_id": 50257,
65
  "suppress_tokens": [
 
152
  50361,
153
  50362
154
  ],
155
+ "torch_dtype": "float32",
156
+ "chunk_size": 50
157
  },
158
  "audio_pool_step": 2,
159
  "auto_map": {
 
163
  },
164
  "chunk_input": true,
165
  "listen_speak_type": "asr",
166
+ "model_type": "minicpmo2_6",
167
  "patch_size": 14,
168
  "query_num": 64,
169
  "slice_config": {
170
+ "max_slice_nums": 9
 
171
  },
172
  "slice_mode": true,
173
  "torch_dtype": "bfloat16",
 
184
  "hidden_size": 1152,
185
  "image_size": 980,
186
  "intermediate_size": 4304,
187
+ "model_type": "minicpmv2_6_vision",
188
  "num_attention_heads": 16,
189
  "num_hidden_layers": 27,
190
+ "patch_size": 14,
191
+ "layer_norm_eps": 1e-06,
192
+ "hidden_act": "gelu_pytorch_tanh"
193
+ },
194
+ "text_config": {
195
+ "model_type": "qwen2",
196
+ "vocab_size": 151700,
197
+ "hidden_size": 3584,
198
+ "intermediate_size": 18944,
199
+ "num_hidden_layers": 28,
200
+ "num_attention_heads": 28,
201
+ "num_key_value_heads": 4,
202
+ "hidden_act": "silu",
203
+ "max_position_embeddings": 32768,
204
+ "rms_norm_eps": 1e-06,
205
+ "bos_token_id": 151643,
206
+ "eos_token_id": 151645,
207
+ "initializer_range": 0.02,
208
+ "use_sliding_window": false,
209
+ "sliding_window": 131072,
210
+ "max_window_layers": 28,
211
+ "rope_parameters": {
212
+ "rope_type": "default",
213
+ "rope_theta": 1000000.0
214
+ }
215
+ },
216
+ "resampler_n_latents": 64,
217
+ "resampler_n_heads": 28,
218
+ "image_token_id": 128244,
219
+ "audio_token_id": 151686
220
  }
configuration_minicpm.py CHANGED
@@ -149,6 +149,7 @@ class MiniCPMOConfig(Qwen2Config):
149
  batch_vision_input=True,
150
  slice_config=None,
151
  vision_config=None,
 
152
  audio_config=None,
153
  tts_config=None,
154
  use_image_id=True,
 
149
  batch_vision_input=True,
150
  slice_config=None,
151
  vision_config=None,
152
+ text_config=None, # read by the native Transformers class, not here
153
  audio_config=None,
154
  tts_config=None,
155
  use_image_id=True,
generation_config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 151643,
3
+ "eos_token_id": [
4
+ 151645,
5
+ 151643
6
+ ],
7
+ "pad_token_id": 151643
8
+ }
preprocessor_config.json CHANGED
@@ -1,9 +1,9 @@
1
  {
2
- "image_processor_type": "MiniCPMVImageProcessor",
3
  "auto_map": {
4
  "AutoProcessor": "processing_minicpmo.MiniCPMOProcessor",
5
  "AutoImageProcessor": "image_processing_minicpmv.MiniCPMVImageProcessor"
6
- },
7
  "processor_class": "MiniCPMOProcessor",
8
  "max_slice_nums": 9,
9
  "scale_resolution": 448,
@@ -18,7 +18,32 @@
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_minicpmo.MiniCPMOProcessor",
5
  "AutoImageProcessor": "image_processing_minicpmv.MiniCPMVImageProcessor"
6
+ },
7
  "processor_class": "MiniCPMOProcessor",
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
+ "feature_extractor_type": "WhisperFeatureExtractor"
49
  }
processing_minicpmo.py CHANGED
@@ -54,7 +54,7 @@ class MiniCPMOProcessor(ProcessorMixin):
54
  image_processor_class = "AutoImageProcessor"
55
  tokenizer_class = "AutoTokenizer"
56
 
57
- def __init__(self, image_processor=None, feature_extractor=None, tokenizer=None):
58
  super().__init__(image_processor, feature_extractor, tokenizer)
59
  self.version = image_processor.version
60
 
 
54
  image_processor_class = "AutoImageProcessor"
55
  tokenizer_class = "AutoTokenizer"
56
 
57
+ def __init__(self, image_processor=None, feature_extractor=None, tokenizer=None, chat_template=None):
58
  super().__init__(image_processor, feature_extractor, tokenizer)
59
  self.version = image_processor.version
60
 
processor_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "processor_class": "MiniCPMO2_6Processor",
3
+ "auto_map": {
4
+ "AutoProcessor": "processing_minicpmo.MiniCPMOProcessor"
5
+ }
6
+ }
tokenizer_config.json CHANGED
@@ -519,5 +519,6 @@
519
  ]
520
  },
521
  "tokenizer_class": "MiniCPMOTokenizerFast",
522
- "unk_token": "<unk>"
 
523
  }
 
519
  ]
520
  },
521
  "tokenizer_class": "MiniCPMOTokenizerFast",
522
+ "unk_token": "<unk>",
523
+ "padding_side": "left"
524
  }