Add native Transformers support (config and processor files only)

#60

This PR adds native Transformers support for MiniCPM-V 2.6 while keeping the existing remote code working. It goes with https://github.com/huggingface/transformers/pull/51299, which adds the model to Transformers as minicpmv2_6, on top of MiniCPM-Llama3-V 2.5 (https://github.com/huggingface/transformers/pull/50929). MiniCPM-V 4.0's files (openbmb/MiniCPM-V-4#6) and MiniCPM-Llama3-V 2.5's (openbmb/MiniCPM-Llama3-V-2_5#80) have 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: "minicpmv2_6", text_config, resampler_n_latents, resampler_n_heads, image_token_id. vision_config gets model_type: "minicpmv2_6_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, plus an auto_map so the remote processor is still found.
chat_template.json New: chat template for the native processor, with the default system prompt of the remote chat(). Only the processor reads this file, so the tokenizer keeps its own template.
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 (the model card's version), 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 in 5 of 5 prompts.
  • Native (the Transformers PR branch, no remote code): the prompts tokenize to the same input_ids as the remote processor, with bitwise-equal pixel values, and the vision tower and resampler give the same image features as the remote code in fp32 (details in the Transformers PR).
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment