Add native Transformers support (config and processor files only)

#23

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 and the tokenizer's chat template 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.
processing_minicpmv.py 1 line: MiniCPMVProcessor.__init__ takes **kwargs, as MiniCPM-V-4's does; Transformers 4.40 to 4.52 pass it the keys of processor_config.json and the chat template.
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.json New: the original Qwen3 template, with message content rendered from content lists (images as <image_id>N</image_id><unk>, videos as `<
tokenizer_config.json padding_side: "left".

I used Claude Code for parts of this change.

How it was checked
  • Remote code on Transformers 4.40.0, 4.44.2, 4.45.1, 4.48.3, 4.49.0, 4.51.3, 4.52.4, 4.53.0, 4.57.3 and 5.24.1: the processor loads as with the current revision, and chat() builds the same inputs.
  • 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).
OpenBMB org

Thanks so much. We'll test the changes. May I ask when will the native Transformers version could be merged?

Thanks! It depends on how fast these get reviewed, since they build on each other: #50929 (MiniCPM-V 2.5, already approved), then #51299 (2.6 and 4.0) and #51300 (4.5). This Hub PR doesn't need to wait for them: the remote code keeps working with it.

I've switched the chat template to chat_template.json: as chat_template.jinja it also replaced the tokenizer template, which changes vLLM's chat path for the remote code. The same applies to MiniCPM-V-4#6 and MiniCPM-V-2_6#60.

I've also added **kwargs to the remote MiniCPMVProcessor.__init__ here and in MiniCPM-V-2_6#60, since Transformers 4.40 to 4.52 pass it the new processor keys and the chat template.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment