Skip to content

[GLM-Image] Fix image-to-image with transformers >= 5.18 - #15483

Open
chsu-amd wants to merge 2 commits into
huggingface:mainfrom
chsu-amd:fix-glm-image-i2i-transformers-5-18
Open

chsu-amd wants to merge 2 commits into
huggingface:mainfrom
chsu-amd:fix-glm-image-i2i-transformers-5-18

Conversation

@chsu-amd

@chsu-amd chsu-amd commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15482

Since transformers 5.18.0 (#45783), GlmImageForConditionalGeneration.get_image_features takes images_per_sample and drops each sample's target grid itself, pooler_output is a per-image list of (hidden_size, height, width) maps, and get_image_tokens loops over that list. generate_prior_tokens still passes only the source grids and torch.cats the features, so image-to-image raises a channel mismatch in the VQ encoder.

This PR checks whether get_image_features accepts images_per_sample (same inspect.signature check as retrieve_timesteps in this file). If it does, it passes all grids + images_per_sample and hands pooler_output to get_image_tokens unchanged; otherwise it keeps the old call. No transformers version check. The same call also works with the current head of transformers #50733, which drops the last grid when images_per_sample is not passed.

Results

main at facc812 + this diff.

main this PR
CPU, tiny components, i2i, transformers 5.16.1, 5.17.0 completes max abs diff 0.0 vs main on 5.16.1, same condition-image tokens
CPU, tiny components, i2i, transformers 5.18.0, 5.21.0, 5.25.0, main, #50733 head raises max abs diff 0.0 vs main on 5.16.1, same condition-image tokens
AMD Radeon 8060S, released weights, model card i2i + enable_model_cpu_offload(), transformers 5.21.0 raises completes, 1024x1056
same, transformers 5.16.1 completes PNG byte-identical to main

Offload rows include #15456 in both arms. All-on-GPU, the run passes get_image_tokens on 5.21.0, then runs out of memory in the VAE decode, as main does on 5.16.1.

Outside the reported case (CPU, tiny components, every transformers version in the table):

  • Text-to-image: changed lines not reached; max abs diff 0.0 vs main on 5.16.1. prior_token_ids given: generate_prior_tokens is not called.
  • Two 512x512 condition images: max abs diff 0.0, same condition-image tokens. Different sizes fail later in the DiT on main and with this PR (condition-image tokens not compared).
  • Batch > 1 (2 prompts x 1 image, num_images_per_prompt=2, 2 prompts x 2 images): same output and tokens as main on 5.16.1; main raises on 5.18+.

pytest tests/pipelines/glm_image (CPU, transformers 5.16.1 and 5.25.0): same results on main and this branch (all skipped by require_torch_accelerator; with it removed, 26 passed, 7 skipped, 4 failed on both). ruff check / format --check pass.

Self-review notes (self-review skill): blocking none, dead code none. Left for review: no image-to-image fast test (dummy vision patch_size 8, processor 16); if get_image_features is wrapped without functools.wraps, the signature check picks the old call.

Before submitting

Who can review?

@yiyixuxu @sayakpaul

@chsu-amd
chsu-amd marked this pull request as ready for review December 4, 2026 03:05
@sayakpaul

Copy link
Copy Markdown
Member

/diffusers-bot pytest tests/pipelines/glm_image/test_glm_image.py

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

✅ pytest tests/pipelines/glm_image/test_glm_image.py passed on GPU — view logs.

@sayakpaul
sayakpaul requested a review from yiyixuxu December 4, 2026 04:56

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.

GlmImagePipeline: image-to-image fails with transformers >= 5.18 ("expected input[1, 1, 32, 32] to have 1536 channels") in get_image_tokens

2 participants