Skip to content

🚨 Exporters: export multimodal models per component (export_multimodal) - #50249

Open
IlyasMoutawwakil wants to merge 193 commits into
exporters-main-fixesfrom
exporters-multimodal-decomposition
Open

IlyasMoutawwakil wants to merge 193 commits into
exporters-main-fixesfrom
exporters-multimodal-decomposition

Conversation

@IlyasMoutawwakil

@IlyasMoutawwakil IlyasMoutawwakil commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

Stacked on #50785 (on #50733, whose flat vision getters (get_<modality>_embeddings) the vision graphs export). Exports a multimodal model's forward as one graph per part, the way optimum-intel splits VLMs, so models whose inputs mix text with images, video or audio export without a generation loop:

exported = OpenVINOExporter().export_multimodal(model, processor(text=text, images=image, return_tensors="pt"), config)
list(exported)  # ["language_model", "text_embeddings_model", "vision_embeddings_model"]
exported.save_pretrained("my-vlm-openvino")

Why

  • Multimodal embedding and classification models. sentence-transformers asks for exactly this (sentence-transformers#4178): EmbeddingGemma2, AutoModelForSequenceClassification heads on VLMs, and its pooling and projection on top. A single graph can't take them: the modality encoders run data-dependent code (patch counts, feature scattering) that only traces cleanly per part.
  • One split for forward and generation. The components keep optimum-intel's names and boundaries, and the generation export (🚨 Exporters: decompose generative models into one decode graph #50252) builds on the same split, swapping the forward language_model for its cache-carrying one under the same name.

The split

Component Takes Returns
text_embeddings_model input_ids inputs_embeds, placeholder tokens zeroed
<modality>_embeddings_model (vision, video, audio) the modality's inputs (pixel_values, input_features, ...) its features as one [num_tokens, hidden] tensor, through the model's own get_<modality>_embeddings (#50733), or get_<modality>_features where it has no flat getter (projector included); the per-item split would fix the item count in the graph
language_model inputs_embeds (features scattered in) and the model's other kwargs what the model returns: heads and pooling kept

decompose_multimodal(model, inputs) captures each part's real call from one eager forward, so the components need no per-model code: anything exposing get_image_features / get_video_features / get_audio_features splits. Inputs a part computes from data (M-RoPE position_ids) are precomputed from the config (precompute_export_inputs). export_multimodal exports each component through the same path as export, with one config or one per component, calibrating each component on its own inputs when quantizing.

Deepstack models (Qwen3-VL and its variants, Granite4-Vision) also add vision features inside their first decoder layers, at the image positions. Their vision component returns those per-layer features too, and language_model takes them next to inputs_embeds.

Fixes in the split

Change Why
The vision component returns image_hidden_states in the shape the model's forward does: flat for LLaVA, one row per image for Gemma3 and PaliGemma All were flattened to [num_tokens, hidden], following LLaVA
A multimodal sequence classifier on inputs_embeds also takes input_ids, so its head pools at the last token that isn't padding It pooled the last position, padding or not (Qwen3.5 classification)

The export-path fixes this PR used to carry (window attention, ONNX Runtime fusions, OrtFusionConfig, the gated delta rule, runner changes) are in #50785, below it.

Modeling changes

Qwen3-VL's and Granite4-Vision's outer forwards accept the two deepstack inputs their text models already take (visual_pos_masks / deepstack_visual_embeds, vision_mask / deepstack_features), so a caller holding the vision features can run the decoder on inputs_embeds. Behaviour with pixel_values is unchanged. The arguments are documented once, in auto_docstring. make fix-repo carries the change to Qwen3-VL-MoE, Cohere Compass and Cosmos3-Omni.

Breaking changes

  • Multimodal components are named as Optimum Intel names them, in export_multimodal and in export_for_generation alike: "text_embeddings_model", one "<modality>_embeddings_model" per modality, and "language_model", instead of "image_encoder" / "audio_encoder", "multi_modal_projector" and "lm_head". Per-component config dicts take the new names.
  • decompose_multimodal and is_multimodal move from exporters.utils to exporters.decompose; decompose_multimodal returns Components (module, inputs) instead of tuples.
  • register_export_input_preparer and precompute_export_inputs move to exporters.precompute. Preparers take (config, inputs) instead of the model, and precompute_export_inputs(config, inputs) returns the completed inputs instead of editing them in place, so a runtime can compute them from the config alone.

Tests

  • tests/exporters/test_utils.py: the split of a tiny LLaVA (names, inputs, language_model's logits against the full model), a tiny Qwen2-VL's vision graph run on other image counts than traced, and export_multimodal end to end with save and load.
  • tests/exporters/test_quantization.py: per-component quantization of a VLM.
  • tests/exporters/test_fusions.py: Gemma 4's vision rotary fused into RotaryEmbedding (its fusion is in Exporters: fixes to the export path, checked on inputs the exports never saw #50785), outputs against the plain export.
  • Export tests of Qwen2-VL, Qwen3-VL, Qwen3-VL-MoE, Granite4-Vision, LLaVA, Gemma3, PaliGemma and EmbeddingGemma2 on Dynamo, ONNX, OpenVINO and ExecuTorch.
  • tests/exporters/test_export.py: the export tests' new-input check (Exporters: fixes to the export path, checked on inputs the exports never saw #50785) also runs on multimodal splits, whose precomputed inputs this split computes from the config.

🤖 Generated with Claude Code

🤖 mlinter review state

IlyasMoutawwakil and others added 13 commits October 27, 2026 15:32
…t/save_artifact

`export()` and `export_for_generation()` return an `ExportArtifacts`, a mapping of
`ExportedComponent`s (the backend's program and the metadata recorded while tracing it),
saved with `save_pretrained`. Backends implement `export_artifact` and `save_artifact`,
and register through `register_backend`. The configs drop their saving fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n names

`ExportArtifacts.from_pretrained` loads an export from a local directory or the Hub
(only the files the manifest names), through each backend's `load_artifact`;
`push_to_hub` and `save_pretrained(push_to_hub=True)` upload it. ONNX and OpenVINO
record their final graph's input and output names in the metadata.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`.xml` / `.bin` don't name their format, so OpenVINO components save as
`openvino_<name>.xml`, as optimum-intel's do. Other backends keep `<name>.<ext>`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`ExportFormat` is a `StrEnum`, so configs serialize as they are and custom backends
use plain strings. The registry lookup splits into `get_export_config_class` and
`get_exporter_class`. The metadata drops the fields that described the traced program
rather than the saved graph. Overlapping tests are merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nst the Hub

`export_program` / `save_program` / `load_program`, `program_files` and `.program`
replace the `artifact` names, which now only mean the `ExportArtifacts` container.
A loaded ONNX graph doesn't read its weights; calling it runs the saved file in
ONNX Runtime. `get_exporter_class` / `get_export_config_class` are public.
Hub tests move to `test_hub.py`, with staging tests pushing and loading a real export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cuTorch

A loaded ONNX program reads its graph without the weights, and a call runs the saved
file in ONNX Runtime. `num_user_outputs` and `constant_inputs` are recorded again for
Dynamo and ExecuTorch, whose saved programs bind positionally; ONNX and OpenVINO drop
them, as they describe the traced program rather than those saved graphs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cord the export config

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… through one path

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A runner per backend (`exporters/runtimes/`) runs a program as an export returns it or
a load reads it back. `ExportArtifacts.runtime()` picks how an export runs and returns
an `ExportedModel`, called like the model it came from; `AutoExportedModel.from_pretrained`
loads any saved export to run. The per-model forward export tests run through the
runners, and the ExecuTorch test helper keeps only its skip policy around the runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cuTorch runner

A dynamic export returns a static cache's positions as integers, which the model keeps
inside its cache rather than returning as tensors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…toExportedModel and can_generate

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ore loading it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HuggingFaceDocBuilderDev

HuggingFaceDocBuilderDev commented Nov 2, 2026 •

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Pages changed in this PR:

IlyasMoutawwakil and others added 4 commits November 2, 2026 14:13
@IlyasMoutawwakil IlyasMoutawwakil changed the title Exporters: export multimodal models per component (export_multimodal) 🚨 Exporters: export multimodal models per component (export_multimodal) Nov 2, 2026
IlyasMoutawwakil and others added 8 commits November 2, 2026 15:26
A Python scalar kwarg (`output_router_logits=False`) is a constant of the graph, not an input: binding it
shifted every later input by one slot. OpenVINO's `infer` already copies its outputs out of the request's
buffers, so the extra copy is dropped, and inputs are shared instead of copied in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Qwen3-VL and Granite4-Vision add vision features inside their first decoder layers (deepstack), at the image
positions, besides scattering them into `inputs_embeds`. The outer forward derives both from the pixel values, which
the split removes, so the language model silently ran without them. Both outer forwards now accept them, as their
text models already do (`visual_pos_masks`/`deepstack_visual_embeds`, `vision_mask`/`deepstack_features`), with the
arguments documented once in `auto_docstring`; the split takes them from the captured decoder call, and the vision
component returns Qwen3-VL's per-layer features.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dict keys in a saved spec

ExecuTorch has no string value, and a program's string constant (`vision_feature_select_strategy="default"`)
takes `None` in its slot. A cache's pytree context keeps a dict with non-string keys as pairs, since JSON reads
`EncoderDecoderCache.is_updated`'s int keys back as strings and the saved input spec no longer matches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The language model takes the deepstack features over every position: an all-True mask, and each layer's features
with zeros at the text rows. Adding zeros changes nothing (the split matches the model exactly), and every step then
takes the same shapes, so a decode step, which has no image rows, can feed the same graph zeros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and session options

`ExportArtifacts.runtime(device, **runner_kwargs)` hands the options to the backend's runner: an ONNX Runtime
`providers` list (TensorRT with its options, say) and `session_options`. ONNX Runtime sizes its thread pool from every
core of the machine rather than the ones the process may run on, which `intra_op_num_threads` fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Model linter — first pass

transformers-mlinter found 3 item(s) in the model files this PR touches. These are structural conventions a maintainer would otherwise flag by hand.

This is automated and advisory — it does not block merging.

rule count what it checks
TRF057 3 Public model, config, output and processor classes and their public methods must carry @auto_docstring.
3 item(s) outside this diff
  • src/transformers/models/hy_v4_vl/modular_hy_v4_vl.py:514 TRF057 — TRF057: HYV4VLModel.get_image_features in modeling_hy_v4_vl.py has no @auto_docstring. Declaring @can_return_tuple here drops every decorator of the parent method. Add @auto_docstring as the innermost decorator here.
  • src/transformers/models/hy_v4_vl/modular_hy_v4_vl.py:538 TRF057 — TRF057: HYV4VLModel.get_video_features in modeling_hy_v4_vl.py has no @auto_docstring. Declaring @can_return_tuple here drops every decorator of the parent method. Add @auto_docstring as the innermost decorator here.
  • src/transformers/models/moss_transcribe_diarize/modular_moss_transcribe_diarize.py:779 TRF057 — TRF057: MossTranscribeDiarizeForConditionalGeneration.get_audio_features in modeling_moss_transcribe_diarize.py has no @auto_docstring. No parent method it could inherit the decorator from declares it. Add @auto_docstring as the innermost decorator here.

IlyasMoutawwakil and others added 15 commits November 18, 2026 19:52
…to exporters-multimodal-decomposition

# Conflicts:
#	tests/models/minicpmv4_7/test_modeling_minicpmv4_7.py
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@IlyasMoutawwakil
IlyasMoutawwakil removed this pull request from stack #50888 November 22, 2026 20:30
@IlyasMoutawwakil
IlyasMoutawwakil changed the base branch from exporters-modelopt-quantizer to exporters-main-fixes November 22, 2026 20:30
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: cohere_compass, cosmos3_omni, embedding_gemma2, granite4_vision, minicpmv4_7, qwen3_vl, qwen3_vl_moe

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants