Repository navigation
Description
Describe the bug
GlmImagePipeline image-to-image (image=[...]) fails with transformers >= 5.18.0. Text-to-image works.
RuntimeError: Given groups=1, weight of size [2048, 1536, 1, 1], expected input[1, 1, 32, 32] to have 1536 channels, but got 1 channels instead
transformers #45783 (first released in v5.18.0) changed GlmImageForConditionalGeneration.get_image_features(...).pooler_output to a per-image list of (hidden_size, height, width) maps, and get_image_tokens now loops over that list per image. generate_prior_tokens still torch.cats the features and passes only the source grids (pipeline_glm_image.py#L409-L415), so get_image_tokens iterates over the 1536 channel slices of one image and the VQ encoder gets 1 channel.
I have a fix for this, tested with tiny components on transformers 5.16.1, 5.17.0, 5.18.0, 5.21.0, 5.25.0 and main, and on transformers #50733 at f478018 (which changes these getters again), with unchanged output on 5.16.1 and 5.17.0. I will link it here as a draft PR and mark it ready for review once a maintainer is OK with the approach.
Reproduction
The model card's image-to-image example, with a public condition image (loaded with load_image) and a prompt that matches it:
import torch
from diffusers.pipelines.glm_image import GlmImagePipeline
from diffusers.utils import load_image
pipe = GlmImagePipeline.from_pretrained("zai-org/GLM-Image", torch_dtype=torch.bfloat16, device_map="cuda")
image_path = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-sdxl-init.png"
prompt = "Replace the bookshelf background behind the astronaut figurines with a starry night sky."
image = load_image(image_path)
image = pipe(
prompt=prompt,
image=[image], # can input multiple images for multi-image-to-image generation such as [image, image1]
height=33 * 32, # Must set height even it is same as input image
width=32 * 32, # Must set width even it is same as input image
num_inference_steps=50,
guidance_scale=1.5,
generator=torch.Generator(device="cuda").manual_seed(42),
).images[0]
image.save("output_i2i.png")Context
I was checking GLM-Image on current main against recent transformers releases, looking for bugs with an AI agent (Claude Code). The snippet above was run as shown on main (facc812, from source via PYTHONPATH, with PYTHONNOUSERSITE=1 and HF_HOME set to a separate cache; the host sets HIP_DIR / HIP_PATH / ROCM_DIR to C:\opt\rocm and HF_TOKEN, and no HSA_* / MIOPEN_* / PYTORCH_* variables) with transformers 5.21.0 on an AMD Radeon 8060S. With tiny components on CPU, image-to-image completes on transformers 5.16.1 and 5.17.0 and raises the same error in get_image_tokens (32 instead of 1536 channels with the tiny config) on 5.18.0, 5.21.0, 5.25.0 and transformers main (626e62ef). CPU runs: same diffusers commit from source via PYTHONPATH, PYTHONNOUSERSITE=1, separate HF_HOME, torch 2.15.0+cpu, accelerate 1.16.0, huggingface_hub 1.33.0 (5.16/5.17) or 2.5.0 (5.18+), CUDA_VISIBLE_DEVICES / HIP_VISIBLE_DEVICES empty.
Related: #15449 (image-to-image with enable_model_cpu_offload(), a different failure in the same generate_prior_tokens block).
Logs
Traceback (most recent call last):
File "C:\temp\dfbug\glm_issue\repro_glm_image_i2i_tf518.py", line 9, in <module>
image = pipe(
^^^^^
...
File "C:\temp\dfbug\gl1202\wt_main\src\diffusers\pipelines\glm_image\pipeline_glm_image.py", line 854, in __call__
self.generate_prior_tokens(
File "C:\temp\dfbug\gl1202\wt_main\src\diffusers\pipelines\glm_image\pipeline_glm_image.py", line 413, in generate_prior_tokens
prior_token_image_ids_d32 = self.vision_language_encoder.get_image_tokens(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\<user>\miniforge3\envs\stxh-dev-W46\Lib\site-packages\transformers\models\glm_image\modeling_glm_image.py", line 1377, in get_image_tokens
return self.model.get_image_tokens(hidden_states, image_grid_thw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\<user>\miniforge3\envs\stxh-dev-W46\Lib\site-packages\transformers\models\glm_image\modeling_glm_image.py", line 1313, in get_image_tokens
vqmodel_outputs: GlmImageVQVAEModelOutput = self.vqmodel.encode(hs[None, ...])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\Users\<user>\miniforge3\envs\stxh-dev-W46\Lib\site-packages\transformers\models\glm_image\modeling_glm_image.py", line 605, in encode
conv_hidden_states = self.quant_conv(hidden_states)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\Users\<user>\miniforge3\envs\stxh-dev-W46\Lib\site-packages\torch\nn\modules\conv.py", line 560, in _conv_forward
return F.conv2d(
^^^^^^^^^
RuntimeError: Given groups=1, weight of size [2048, 1536, 1, 1], expected input[1, 1, 32, 32] to have 1536 channels, but got 1 channels insteadSystem Info
- 🤗 Diffusers version: 0.44.0.dev0
- Platform: Windows-11-10.0.26200-SP0
- Running on Google Colab?: No
- Python version: 3.12.10
- PyTorch version (GPU?): 2.16.0a0+rocm10.3.0a20261027 (True)
- Huggingface_hub version: 1.33.0
- Transformers version: 5.21.0
- Accelerate version: 1.9.0
- PEFT version: 0.19.1
- Safetensors version: 0.8.0
- xFormers version: not installed
- Accelerator: NA
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No
Activity