GlmImagePipeline: image-to-image fails with `enable_model_cpu_offload()` (device mismatch in `get_image_features`)
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 70/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
src/diffusers/pipelines/glm_image/pipeline_glm_image.pyから始め、特にget_image_featuresが直接呼び出されている409行目付近のgenerate_prior_tokensを確認してください。enable_model_cpu_offload()がコンポーネントのforward呼び出しをどのようにフックするかを読み、その呼び出しを動作するテキストから画像への経路と比較してください。issueの画像から画像への例で再現してください。CPU offloadで実行でき、既存の画像から画像への動作が引き続きカバーされていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
GlmImagePipeline fails in image-to-image mode (image=[...]) when pipe.enable_model_cpu_offload() is used:
RuntimeError: Input type (CUDABFloat16Type) and weight type (CPUBFloat16Type) should be the same
For condition images, generate_prior_tokens calls self.vision_language_encoder.get_image_features(...) directly (pipeline_glm_image.py#L409). enable_model_cpu_offload() hooks each component's forward, so nothing moves the vision-language encoder to the GPU before that call: the pixel values are on cuda and the weights are still on cpu.
Text-to-image with enable_model_cpu_offload() works.
Happy to open a PR once the preferred fix is confirmed.
Reproduction
The model card's image-to-image example, with enable_model_cpu_offload() instead of device_map="cuda" and a public condition image:
import torch
from diffusers.pipelines.glm_image import GlmImagePipeline
from diffusers.utils import load_image
pipe = GlmImagePipeline.from_pretrained("zai-org/GLM-Image", dtype=torch.bfloat16)
pipe.enable_model_cpu_offload()
image = load_image(
"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 = pipe(
prompt=prompt,
image=[image],
height=33 * 32,
width=32 * 32,
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 the offloading options of recent pipelines on current main, looking for bugs in the library with an AI agent (Claude Code). The snippet above was run as shown on main (facc812) on an RTX 5090, with PYTHONNOUSERSITE=1 and HF_HOME set to a separate cache.
The GLM-Image model card suggests enable_model_cpu_offload to run with about 23 GB of GPU memory; the released weight files total 35.77 GB, more than the 32 GB of an RTX 5090. The model card's text-to-image example with the same loading change generates the image. With tiny random components, image-to-image passes with pipe.to("cuda") and fails at the same line with enable_model_cpu_offload().
The same error was reported on the model page: https://huggingface.co/zai-org/GLM-Image/discussions/12
Related: #15399 reports the same kind of bypass of the top-level forward for MiniMax-H3.
Logs
Traceback (most recent call last):
File "C:\temp\dfbug\gl1201\issue_final\repro_glm_image_i2i_offload.py", line 11, in <module>
image = pipe(
^^^^^
...
File "C:\temp\dfbug\wt_gl_issue\src\diffusers\pipelines\glm_image\pipeline_glm_image.py", line 854, in __call__
self.generate_prior_tokens(
File "C:\temp\dfbug\wt_gl_issue\src\diffusers\pipelines\glm_image\pipeline_glm_image.py", line 409, in generate_prior_tokens
prior_token_image_embed = self.vision_language_encoder.get_image_features(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\<user>\miniforge3\envs\dfop-nv\Lib\site-packages\transformers\models\glm_image\modeling_glm_image.py", line 1385, in get_image_features
return self.model.get_image_features(pixel_values, image_grid_thw, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\Users\<user>\miniforge3\envs\dfop-nv\Lib\site-packages\transformers\models\glm_image\modeling_glm_image.py", line 1146, in get_image_features
vision_outputs = self.visual(pixel_values, grid_thw=image_grid_thw, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\Users\<user>\miniforge3\envs\dfop-nv\Lib\site-packages\torch\nn\modules\conv.py", line 560, in _conv_forward
return F.conv2d(
^^^^^^^^^
RuntimeError: Input type (CUDABFloat16Type) and weight type (CPUBFloat16Type) should be the same
System Info
- 🤗 Diffusers version: 0.44.0.dev0
- Platform: Windows-11-10.0.26200-SP0
- Running on Google Colab?: No
- Python version: 3.12.16
- PyTorch version (GPU?): 2.14.1+cu130 (True)
- Huggingface_hub version: 1.32.0
- Transformers version: 5.16.1
- Accelerate version: 1.9.0
- PEFT version: 0.21.3
- Safetensors version: 0.8.0
- xFormers version: not installed
- Accelerator: NVIDIA GeForce RTX 5090, 32607 MiB
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No
Who can help?
@yiyixuxu @DN6
- 主要言語
- Python
- スター
- 35k
- フォーク
- 7.5k
- 平均マージ
- 3日 2時間
- マージ済み PR(30日)
- 58
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
huggingface/diffusers のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
huggingface/diffusers#15463 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
huggingface/diffusers#15461 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
huggingface/diffusers#15460 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
huggingface/diffusers#15459 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
huggingface/diffusers#15436 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
huggingface/diffusers の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
daniel-ospina/tortoise#15014 ·
メンテナーはふだん 1 日以内に返信
-
area:api p2 product:business triage:active type:feat
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
cuadrao-ai/argus#2656 ·
メンテナーはふだん 1 日以内に返信
-
documentation good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
-
good first issue tests
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
yeab7505/tenasig#20 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信