-
Notifications
You must be signed in to change notification settings - Fork 32k
Any to any pipeline and auto-mapping #40884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
25f8152
initial commit
zucchini-nlp 8f71e71
fix tests
zucchini-nlp beb8517
fix copies, tests and rename pipe
zucchini-nlp 06579e5
another rename
zucchini-nlp 0388c2e
fix copies again
zucchini-nlp f2ef098
activate pipeline mixin in some models
zucchini-nlp 4c5fbb5
audio loading
zucchini-nlp cb18262
typo
zucchini-nlp 08873fb
fix the test
zucchini-nlp 06dddc1
stupid typo in filename
zucchini-nlp 338baa0
fix copies
zucchini-nlp 7a5e080
docs
zucchini-nlp 076105d
forgot
zucchini-nlp 5670450
fix pipe tests
zucchini-nlp 8665854
fix copies
zucchini-nlp aafaae4
fix test
zucchini-nlp 4252a4c
lets not pass it explicitly
zucchini-nlp f47372a
final fix
zucchini-nlp 51057b3
rename in test files as well
zucchini-nlp ee1251d
fix again after reordering...
zucchini-nlp 073b782
Merge branch 'main' into auto-multimodal
zucchini-nlp d9e74e7
add qwen2 audio
zucchini-nlp 654db8f
Merge branch 'main' into auto-multimodal
zucchini-nlp 61428ad
add qwen3-omni
zucchini-nlp e3b2318
wait, I didn't push it last time?
zucchini-nlp 9c2404f
it's only torch from now on
zucchini-nlp 067061b
how was the model merged with docstring issues?
zucchini-nlp 79f9275
merge main
zucchini-nlp 51fafd3
make style
zucchini-nlp 5855a4a
Merge remote-tracking branch 'upstream/main' into auto-multimodal
zucchini-nlp cfd8d1b
requires backend depends on input modalities
zucchini-nlp 67f8022
add repr
zucchini-nlp 271ebd1
Merge branch 'main' into auto-multimodal
zucchini-nlp df7e556
Merge branch 'main' into auto-multimodal
zucchini-nlp 28fd203
fix copies
zucchini-nlp 8101f3f
merge main
zucchini-nlp 27c15e4
fox copies, new models were added
zucchini-nlp 40c96a3
merge main
zucchini-nlp e344128
and now fix copies
zucchini-nlp d4da21e
Merge branch 'main' into auto-multimodal
zucchini-nlp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| <!--Copyright 2025 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
|
|
||
| ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
|
|
||
| --> | ||
|
|
||
| # Multimodal Generation | ||
|
|
||
| [[open-in-colab]] | ||
|
|
||
| Multimodal (any-to-any) models are language models capable of processing diverse types of input data (e.g., text, images, audio, or video) and generating outputs in any of these modalities. Unlike traditional unimodal or fixed-modality models, they allow flexible combinations of input and output, enabling a single system to handle a wide range of tasks: from text-to-image generation to audio-to-text transcription, image captioning, video understanding, and so on. This task shares many similarities with image-text-to-text, but supports a wider range of input and output modalities. | ||
|
|
||
| In this guide, we provide a brief overview of any-to-any models and show how to use them with Transformers for inference. Unlike Vision LLMs, which are typically limited to vision-and-language tasks, omni-modal models can accept any combination of modalities (e.g., text, images, audio, video) as input, and generate outputs in different modalities, such as text or images. | ||
|
|
||
| Let’s begin by installing dependencies: | ||
|
|
||
| ```bash | ||
| pip install -q transformers accelerate flash_attn | ||
zucchini-nlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
zucchini-nlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Let's initialize the model and the processor. | ||
|
|
||
| ```python | ||
| from transformers import AutoProcessor, AutoModelForMultimodalLM, infer_device | ||
| import torch | ||
|
|
||
| device = torch.device(infer_device()) | ||
| model = AutoModelForMultimodalLM.from_pretrained( | ||
| "Qwen/Qwen2.5-Omni-3B", | ||
| dtype=torch.bfloat16, | ||
| attn_implementation="flash_attention_2", | ||
| ).to(device) | ||
|
|
||
| processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-Omni-3B") | ||
| ``` | ||
zucchini-nlp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| These models typically include a [chat template](./chat_templating) to structure conversations across modalities. Inputs can mix images, text, audio, or other supported formats in a single turn. Outputs may also vary (e.g., text generation or audio generation), depending on the configuration. | ||
|
|
||
| Below is an example providing a "text + audio" input and requesting a text response. | ||
|
|
||
| ```python | ||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| {"type": "audio", "url": "https://huggingface.co/datasets/raushan-testing-hf/audio-test/resolve/main/f2641_0_throatclearing.wav"}, | ||
| {"type": "text", "text": "What do you hear in this audio?"}, | ||
| ] | ||
| }, | ||
| ] | ||
| ``` | ||
|
|
||
| We will now call the processors' [`~ProcessorMixin.apply_chat_template`] method to preprocess its output along with the image inputs. | ||
|
|
||
| ```python | ||
| inputs = processor.apply_chat_template( | ||
| messages, | ||
| tokenize=True, | ||
| return_dict=True, | ||
| return_tensors="pt", | ||
| add_generation_prompt=True, | ||
| ) | ||
| ``` | ||
|
|
||
| We can now pass the preprocessed inputs to the model. | ||
|
|
||
| ```python | ||
| with torch.no_grad(): | ||
| generated_ids = model.generate(**inputs, max_new_tokens=100) | ||
| generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True) | ||
| print(generated_texts) | ||
| ``` | ||
|
|
||
| ## Pipeline | ||
|
|
||
| The fastest way to get started is to use the [`Pipeline`] API. Specify the `"any-to-any"` task and the model you want to use. | ||
|
|
||
| ```python | ||
| from transformers import pipeline | ||
| pipe = pipeline("any-to-any", model="mistralai/Voxtral-Mini-3B-2507") | ||
| ``` | ||
|
|
||
| The example below uses chat templates to format the text inputs and uses audio modality as an multimodal data. | ||
|
|
||
| ```python | ||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| { | ||
| "type": "audio", | ||
| "url": "https://huggingface.co/datasets/raushan-testing-hf/audio-test/resolve/main/glass-breaking-151256.mp3", | ||
| }, | ||
| {"type": "text", "text": "What do you hear in this audio?"}, | ||
| ], | ||
| }, | ||
| ] | ||
| ``` | ||
|
|
||
| Pass the chat template formatted text and image to [`Pipeline`] and set `return_full_text=False` to remove the input from the generated output. | ||
|
|
||
| ```python | ||
| outputs = pipe(text=messages, max_new_tokens=20, return_full_text=False) | ||
| outputs[0]["generated_text"] | ||
| ``` | ||
|
|
||
| Any-to-any pipeline also supports generating audio or images with any-to-any models. For that you need to set `generation_mode` parameter. Do not forget to set video sampling to the desired FPS, otherwise the whole video will be loaded without sampling. Here is an example code: | ||
|
|
||
| ```python | ||
| import soundfile as sf | ||
| pipe = pipeline("any-to-any", model="Qwen/Qwen2.5-Omni-3B") | ||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| {"type": "video", "path": "https://huggingface.co/datasets/raushan-testing-hf/videos-test/resolve/main/Cooking_cake.mp4"}, | ||
| {"type": "text", "text": "Describe this video."}, | ||
| ], | ||
| }, | ||
| ] | ||
| output = pipe(text=messages, fps=1, load_audio_from_video=True, max_new_tokens=20, generation_mode="audio") | ||
| sf.write("generated_audio.wav", out[0]["generated_audio"]) | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.