feat(template): add MiniCPM-o-4.5 training template with audio support and fix image_bound bug#8307
Conversation
…t and fix image_bound bug - Add MiniCPMO4_5Template subclass of MiniCPMV2_6Template, registered under minicpmo4_5 template type - replace_tag handles audio media type: loads waveform via load_audio, truncates to configurable max duration (60s), inserts audio placeholder tokens - _encode calls parent for video/image encoding, then processes audio via processor.process_audio, appends audio_features, audio_feature_lens, audio_bounds - _data_collator pads/concatenates audio features, collects audio_bounds and audio_feature_lens per sample - Fix image_bound bug: MiniCPMO4_5 uses <unk> tokens for both image patches and audio frames; mask out audio <unk> spans before computing image_bound to avoid torch.stack size mismatch in get_vllm_embedding
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the MiniCPM-o-4.5 model's capabilities by integrating comprehensive audio processing support, enabling it to handle multimodal inputs including audio, video, and text. Concurrently, it addresses a critical bug in the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new template MiniCPMO4_5Template to support audio in MiniCPM-o-4.5 model training, and crucially fixes a bug where <unk> tokens for audio were incorrectly included in image_bound, leading to runtime errors. The changes are well-structured, with the new template handling audio loading, encoding, and collation. The bug fix correctly isolates image-only <unk> tokens before computing image_bound. I have a couple of suggestions to improve maintainability and correct a minor documentation inconsistency.
swift/template/templates/minicpm.py
Outdated
|
|
||
| Audio placeholder: <|audio_start|><unk>*N<|audio_end|> | ||
| Model inputs added: audio_features, audio_feature_lens, audio_bounds | ||
| Audio is truncated to 30s max; sampling_rate defaults to 16000. |
There was a problem hiding this comment.
The docstring states that audio is truncated to a maximum of 30 seconds, but the class constant MAX_AUDIO_SECONDS is set to 60. To avoid confusion, the docstring should be updated to reflect the actual value used in the code.
| Audio is truncated to 30s max; sampling_rate defaults to 16000. | |
| Audio is truncated to 60s max; sampling_rate defaults to 16000. |
swift/template/templates/minicpm.py
Outdated
| # collate image/video fields from parent | ||
| res = {} | ||
| for k in ['pixel_values', 'image_bound', 'tgt_sizes']: | ||
| res[k] = self.gather_list(batch, k) | ||
| res.update(Template._data_collator(self, batch, padding_to=padding_to)) |
There was a problem hiding this comment.
The logic for collating image and video fields is duplicated from the parent class MiniCPMVTemplate. You can simplify this by calling super()._data_collator(...) to reuse the parent's implementation, which improves code reuse and maintainability.
| # collate image/video fields from parent | |
| res = {} | |
| for k in ['pixel_values', 'image_bound', 'tgt_sizes']: | |
| res[k] = self.gather_list(batch, k) | |
| res.update(Template._data_collator(self, batch, padding_to=padding_to)) | |
| res = super()._data_collator(batch, padding_to=padding_to) |
- Fix docstring: update audio truncation limit from 30s to 60s to match MAX_AUDIO_SECONDS constant - Simplify _data_collator: replace duplicated image/video collation logic with super()._data_collator() call
|
Thanks for the PR! Please write relevant test examples, referring to this: https://github.com/modelscope/ms-swift/blob/main/tests/test_align/test_template/test_audio.py#L58-L65 And run: |
This PR adds a dedicated
MiniCPMO4_5Templateclass for training theMiniCPM-o-4.5model with video + audio + text → text tasks, and fixes a critical bug that prevented training from running.What was added
A new
MiniCPMO4_5Templatesubclass ofMiniCPMV2_6Templateis introduced, registered under theminicpmo4_5template type. It extends the existing video/image pipeline with full audio support:replace_taghandlesaudiomedia type by loading waveforms viaload_audio, truncating to a configurable max duration (default 60s), and inserting an<|audio_start|><unk>×N<|audio_end|>placeholder whose length is computed from the waveform length, hop size, and pooling step._encodecalls the parent to handle video/image encoding, then processes audio throughprocessor.process_audioand appendsaudio_features,audio_feature_lens, andaudio_boundsto the encoded dict._data_collatorpads and concatenates audio features across batch items, and collectsaudio_boundsandaudio_feature_lensper sample.Bug fixed
MiniCPM-o-4.5uses<unk>tokens as placeholders for both image patches and audio frames. The parent classMiniCPMV2_6Template._encodecomputesimage_boundby finding all contiguous<unk>runs ininput_ids—which incorrectly includes audio<unk>regions.This causes
get_vllm_embeddingin the model to calltorch.stackon ranges of different lengths (e.g., 64 tokens per video frame vs. variable-length audio chunks), resulting in:The fix masks out all
<unk>positions that fall inside<|audio_start|>...<|audio_end|>spans before recomputingimage_bound, so only visual patch tokens are included.