Skip to content

🚨 Exporters: decompose generative models into one decode graph - #50252

Draft
IlyasMoutawwakil wants to merge 74 commits into
exporters-multimodal-runtimefrom
exporters-generation-decomposition
Draft

IlyasMoutawwakil wants to merge 74 commits into
exporters-multimodal-runtimefrom
exporters-generation-decomposition

Conversation

@IlyasMoutawwakil

@IlyasMoutawwakil IlyasMoutawwakil commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

🤗 Transformers CI

What does this PR do?

Stacked on the multimodal runtime PR (#50251), and supersedes #50038, rebuilt on the reworked stack. It also merges #49601 and #49602, which encoder-decoder and cross-attention models need; their diffs show here until they land on main.

Exports generative models the way inference runs them, so a runtime drives them with as little glue as possible: the fewest graphs to load, each graph called the way generate calls the model, no work repeated per step, and no weight exported twice.

What an export contains

export_for_generation returns only what a generation loop calls, under Optimum Intel's component names (an OpenVINO export saves openvino_model.xml, openvino_language_model.xml, ...):

  • One decode graph for the steps: a causal LM's "model", an encoder-decoder's "decoder_model", a multi-modal model's "language_model". With a dynamic export it takes several tokens at once, so it also serves the prompt, in one call on an empty cache, and no prompt graph duplicates the weights; with a static export its query axis is one token, and the prompt gets a graph of its own (below), the pair named "prefill_model" and "decode_model".
  • "encoder_model" for an encoder-decoder, which also computes the decoder's cross-attention cache once per input, so the decode graph only reads it. With decoder_writes_cross_cache the decode graph writes it on its first call instead (OpenVINO's default, which keeps the cache as model state).
  • "text_embeddings_model" and one "<modality>_embeddings_model" per modality for a multi-modal model: the same split as export_multimodal (🚨 Exporters: export multimodal models per component (export_multimodal) #50249), with "language_model" swapped for the cache-carrying decode graph under the same name.

A prompt graph beside a decode graph taking one token

A decode graph taking one token (any static export, or multi_token_decode=False) would take the prompt a token at a time. export_for_generation exports the prompt's call as a graph of its own: prefill and decode are disaggregated, as "prefill_model" and "decode_model" (a multi-modal model's embedding graphs stay as they are):

Decode graph Graphs
takes several tokens (dynamic exports, by default) one decode graph for the prompt and the steps
takes one token, cache as an input "prefill_model" for the prompt, "decode_model" for the steps
takes one token, cache as the backend's state (OpenVINO stateful) "prefill_model" for the prompt; "decode_model" continues from a copy of its state

With a single config the prompt graph is dynamic even behind a static decode graph, so a static export takes prompts of any length; a per-component config dict keeps full control. Quantization calibrates "prefill_model" on the prompt's call. The runtime that drives these graphs is #50254.

What each graph does

  • Logits for the last position only, which greedy and sampled decoding read: logits_to_keep is fixed in the graph, so a long prompt doesn't return [batch, tokens, vocab] logits. keep_all_logits=True returns every position, for assisted decoding.
  • Quantization calibrated on both calls the decode graph serves: the prefill call and the decode steps, whose activation ranges differ.

One graph for both calls, on every backend

A decode graph serving the prefill step must not be guarded on the traced step's shapes:

  • Masks. Under export, the recurrent (linear-attention / SSM) mask takes its last query positions with a gather rather than a slice, which lets hybrid models (Bamba, Jamba, Lfm2, Qwen3.5 / Qwen3-Next, NemotronH, Zamba2, ...) serve both calls. Gemma3's image block ids pad without a branch.
  • Dynamo: a growing sliding-window layer reads its length off its keys, so the graph isn't pinned to the traced step.
  • OpenVINO: the stateful conversion initializes every state to zeros, freezes only state the graph passes through unchanged, pins the static input axes, keeps attention fusable with its KV cache, and decomposes index_copy for static-cache writes.
  • ExecuTorch: inputs are flattened in traced order without matching the saved spec's context, since JSON turns a cache's int-keyed bookkeeping (EncoderDecoderCache.is_updated) into string keys.

Decomposition

exporters/decompose.py gains the generation decomposition: it captures what each stage is really called with from one generate call and returns Components, plus "prefill", the prefill step's call to the decode graph as the runtime makes it (cache shaped like the decode capture's, prefill mask, embeddings). "prefill" isn't a graph: export_for_generation drops it, and calibration and the tests use it. exporters/cache.py holds the cache helpers it shares with the runtime.

Changes outside exporters/

For cache and mask reviewers:

  • cache_utils.py: caches start as zero-length 4D tensors rather than empty 1D ones, and their counters and the sliding window are created in lazy_initialization, on the cache's device; Cache.early_initialization takes a value_head_dim for latent-attention caches.
  • masking_utils.py: under export, the block-sequence-id padding is a pad of max(length, 0) rather than a branch, and the causal-skip shortcut never applies (it would bake is_causal into the graph for the traced length).

Breaking changes

  • Generation components are named as Optimum Intel names them ("model", "encoder_model" / "decoder_model", "language_model", ...), a disaggregated export's "prefill_model" / "decode_model", and export_for_generation returns no "prefill" component.
  • A generation export is marked in export.json ("generation": true): a causal LM's generation and forward exports both hold one "model". runtime() refuses a generation export until the generation runtime lands.
  • decompose_for_generation and decompose_prefill_decode move from exporters.utils to exporters.decompose and return Components; multi_token_decode follows whether the export is dynamic, and is refused on a static one.

Tests

  • The generation export tests run a multi-token decode graph on the prefill call as well as on its own captured call, against eager.
  • Mamba, Falcon-Mamba, Jamba and Zamba run the dynamic generation export tests again: their recurrence is a while_loop under export since Exporters: fixes to the export path, checked on inputs the exports never saw #50785.
  • Export tests (torch.export, ONNX, OpenVINO, ExecuTorch) of Llama, BERT, T5, Whisper, Gemma3, Qwen2-VL, LLaVA, PaliGemma and EmbeddingGemma2, and tests/exporters/.

🤖 Generated with Claude Code

🤖 mlinter review state

IlyasMoutawwakil and others added 8 commits October 14, 2026 17:12
… init

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…iding cross-attention cache

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…er on the base method

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…o exporters-generation-decomposition

# Conflicts:
#	src/transformers/configuration_utils.py
…exporters-generation-decomposition

# Conflicts:
#	src/transformers/models/t5gemma2/modular_t5gemma2.py
…ion changes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rebuilt from #50038 on the multimodal runtime: one decode graph serving the prefill step, optimum-intel
component names, a generation flag in the manifest, and the shared multimodal split.

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:

…it from the model's

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

# Conflicts:
#	src/transformers/exporters/runtimes/runner_executorch.py
IlyasMoutawwakil and others added 12 commits November 2, 2026 16:50
…-decomposition

# Conflicts:
#	src/transformers/exporters/decompose.py
…-decomposition

# Conflicts:
#	src/transformers/exporters/decompose.py
A decode step has no image rows: the decode capture takes `dense_deepstack` zeros and the prompt's call the split's
dense features, so the one decode graph serves both, as for any other model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s kernels on the query length

Qwen3.5's (and Qwen3-Next's) gated delta rule runs a chunked kernel beyond one query token and a recurrent one
for a single token: a decode graph traced on several tokens keeps the chunked kernel, which a one-token step
then runs wrong. Such a model's dynamic export now defaults to a one-token decode graph.

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

The capture runs `generate` for a few steps only, so a static cache came out sized for the prompt plus those
steps, and generating the config's `max_new_tokens` at runtime overflowed it.

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

# Conflicts:
#	src/transformers/exporters/quantizers/utils.py
IlyasMoutawwakil and others added 14 commits November 15, 2026 19:51
…decode_model`

Beside a prompt graph, the decode graph is `decode_model` whatever the model (a causal LM's
`model`, a multimodal model's `language_model`, an encoder-decoder's `decoder_model`); the
embedding and encoder graphs keep their names. Calibration samples follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Their selective scan runs as a `while_loop` under export (#50785), so the multi-token decode
graph takes the prefill call: the skips go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…to exporters-generation-decomposition

# Conflicts:
#	src/transformers/exporters/base.py
#	src/transformers/exporters/exporter_openvino.py
#	src/transformers/exporters/utils.py
@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: unlimited_ocr

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.

2 participants