Skip to content

Fix Qwen Image edit pipelines to forward multimodal token type IDs#14189

Open
wutachiang wants to merge 1 commit into
huggingface:mainfrom
wutachiang:fix/qwen-image-mm-token-type-ids
Open

Fix Qwen Image edit pipelines to forward multimodal token type IDs#14189
wutachiang wants to merge 1 commit into
huggingface:mainfrom
wutachiang:fix/qwen-image-mm-token-type-ids

Conversation

@wutachiang

@wutachiang wutachiang commented Jul 15, 2026

Copy link
Copy Markdown

Fixes #14194

What does this PR do?

This PR forwards mm_token_type_ids from the processor to the Qwen2.5-VL text encoder in the following pipelines:

  • QwenImageEditPipeline
  • QwenImageEditPlusPipeline
  • QwenImageEditInpaintPipeline

The argument is forwarded only when:

  1. The processor returns mm_token_type_ids.
  2. The text encoder explicitly supports it in its forward signature.

This keeps the change backward compatible with older Transformers versions and processors.

Why is this needed?

Recent Transformers versions moved multimodal token classification into the processor. The processor now returns mm_token_type_ids, where text, image, and video tokens are identified explicitly.

Qwen2.5-VL uses these token type IDs to compute multimodal 3D RoPE position IDs. However, the Qwen Image edit pipelines currently select processor outputs manually and do not forward mm_token_type_ids to the
text encoder.

As a result, the text encoder cannot compute the correct multimodal position IDs. This does not raise an exception because position_ids=None is a valid fallback for the language model. Instead, inference
silently falls back to regular position inference, which changes prompt embeddings and generated images.

Older Transformers versions did not require this argument because Qwen2.5-VL inferred multimodal positions directly from special image and video tokens in input_ids.

Backward compatibility

The new argument is not passed unconditionally.

text_encoder_kwargs = {}
if (
    "mm_token_type_ids" in model_inputs
    and "mm_token_type_ids" in inspect.signature(self.text_encoder.forward).parameters
):
    text_encoder_kwargs["mm_token_type_ids"] = model_inputs["mm_token_type_ids"]

Therefore:

  • Older processors that do not return mm_token_type_ids retain the existing behavior.
  • Older text encoders that do not accept the argument retain the existing call signature.
  • Current processor and text encoder combinations receive the multimodal token types required for 3D RoPE.

Validation

The issue was traced through the Qwen2.5-VL encoding path:

  • Token IDs, attention masks, pixel values, token embeddings, and all vision encoder outputs were identical across the tested Transformers versions.
  • The first numerical difference appeared in decoder layer 0 attention.
  • Forwarding mm_token_type_ids restored identical multimodal position IDs.
  • All 28 language model hidden states and both positive and negative prompt embeddings became element-wise identical to the previous correct path.
  • Full denoising inference also produced pixel-identical final output.

Tests added in this PR verify that:

  • mm_token_type_ids is forwarded when both the processor output and text encoder support it.
  • The argument is omitted for a legacy-style text encoder signature.

Existing CPU inference tests were also run with an older processor that does not emit mm_token_type_ids, and their expected outputs remained unchanged.

Tests

pytest -q tests/pipelines/qwenimage/test_qwenimage_edit_plus.py::QwenImageEditPlusPipelineFastTests::test_mm_token_type_ids_forwarding_is_backward_compatible

pytest -q \
  tests/pipelines/qwenimage/test_qwenimage_edit.py::QwenImageEditPipelineFastTests::test_inference \
  tests/pipelines/qwenimage/test_qwenimage_edit_plus.py::QwenImageEditPlusPipelineFastTests::test_inference

ruff check <modified files>
ruff format --check <modified files>
python utils/check_copies.py

All targeted tests and checks pass.

@github-actions github-actions Bot added tests pipelines size/M PR with diff < 200 LOC labels Jul 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hi @wutachiang, thanks for the PR! It does not appear to link an issue it fixes. If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked. See the contribution guide for more details. If this PR intentionally does not fix a tracked issue, a maintainer can add the no-issue-needed label to silence this reminder.

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

Labels

pipelines size/M PR with diff < 200 LOC tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] QwenImageEdit produces incorrect outputs with transformers >=5 due to missing mm_token_type_ids

1 participant