Repository navigation
Description
Describe the bug
Kandinsky6TI2VAPipeline with image=... and expand_prompts=True fails under pipe.enable_model_cpu_offload() with transformers >= 5.18.0:
RuntimeError: Input type (CUDABFloat16Type) and weight type (CPUBFloat16Type) should be the same
expand_prompts calls text_encoder.generate(...) (pipeline_kandinsky6_ti2va.py#L483), and enable_model_cpu_offload() moves the text encoder to the GPU only when its top-level forward runs. Since transformers #45783 (first released in v5.18.0), generate() calls get_image_features before the first forward, so the vision tower runs with its weights still on cpu while the pixel values are on cuda.
With transformers 5.16.1 the same snippet gets past expand_prompts (see Context).
I have a tested fix for this. 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 docs example with image= and expand_prompts=True added, as the docs describe right below it (L69-L70):
import torch
from diffusers import Kandinsky6TI2VAPipeline
from diffusers.utils import encode_video, load_image
pipe = Kandinsky6TI2VAPipeline.from_pretrained(
"kandinskylab/Kandinsky-6.0-Pro-distill-5s-Diffusers", torch_dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()
output = pipe(
prompt="A cat and a dog baking a cake together in a kitchen.",
height=480,
width=864,
num_frames=121,
num_inference_steps=10,
guidance_scale=1.0,
image=load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png"),
expand_prompts=True,
)
encode_video(
output.frames[0],
fps=24,
output_path="output.mp4",
audio=output.audio[0][None],
audio_sample_rate=pipe.audio_sample_rate,
)Context
I was checking pipelines 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, HF_HOME set to a separate cache, HF_HUB_OFFLINE=1, PYTHONIOENCODING=utf-8 and TOKENIZERS_PARALLELISM=false) with transformers 5.25.1 on an NVIDIA GeForce RTX 5090. It was started with runpy from a wrapper that only logs (device placement, the expand_prompts output, wall time) and stops the run after 2700 s; the wrapper frames are cut from the log below. pipeline_kandinsky6_ti2va.py is unchanged on current main.
With transformers 5.16.1 (another venv on the same machine: torch 2.14.1+cu130, accelerate 1.9.0, huggingface_hub 1.32.0; same diffusers tree and env vars) the same snippet gets past expand_prompts; the video itself did not finish within the 2700 s limit, because the 56 GiB bf16 transformer does not fit in the 32 GB of this GPU.
Related: #15449 (same kind of offload hook bypass in GLM-Image).
Logs
Traceback (most recent call last):
...
File "C:/temp/dfbug/k6/scripts/k6_issue.py", line 10, in <module>
output = pipe(
^^^^^
...
File "C:\temp\dfbug\k6\tree_main\src\diffusers\pipelines\kandinsky6\pipeline_kandinsky6_ti2va.py", line 776, in __call__
prompt = self.expand_prompts(
^^^^^^^^^^^^^^^^^^^^
...
File "C:\temp\dfbug\k6\tree_main\src\diffusers\pipelines\kandinsky6\pipeline_kandinsky6_ti2va.py", line 452, in expand_prompts
Kandinsky6TI2VAPipeline.expand_prompts(
...
File "C:\temp\dfbug\k6\tree_main\src\diffusers\pipelines\kandinsky6\pipeline_kandinsky6_ti2va.py", line 483, in expand_prompts
generated = text_encoder.generate(**inputs, max_new_tokens=max_sequence_length)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\temp\dfbug\k6\venv_tf5251\Lib\site-packages\transformers\generation\utils.py", line 2914, in generate
model_kwargs = self._maybe_prepare_encoder_kwargs_for_generation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\temp\dfbug\k6\venv_tf5251\Lib\site-packages\transformers\generation\utils.py", line 995, in _maybe_prepare_encoder_kwargs_for_generation
return self._prepare_multimodal_encoder_kwargs_for_generation(model_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\temp\dfbug\k6\venv_tf5251\Lib\site-packages\transformers\generation\utils.py", line 1065, in _prepare_multimodal_encoder_kwargs_for_generation
model_kwargs["mm_encoder_outputs"][modality] = encoder_fn(**encoder_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\temp\dfbug\k6\venv_tf5251\Lib\site-packages\transformers\models\qwen2_5_vl\modeling_qwen2_5_vl.py", line 1076, in get_image_features
vision_outputs = self.visual(pixel_values, grid_thw=image_grid_thw, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\temp\dfbug\k6\venv_tf5251\Lib\site-packages\transformers\models\qwen2_5_vl\modeling_qwen2_5_vl.py", line 456, in forward
hidden_states = self.patch_embed(hidden_states)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\temp\dfbug\k6\venv_tf5251\Lib\site-packages\transformers\models\qwen2_5_vl\modeling_qwen2_5_vl.py", line 121, in forward
hidden_states = self.proj(hidden_states.to(dtype=target_dtype)).view(-1, self.embed_dim)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
File "C:\Users\<user>\miniforge3\envs\dfop-nv\Lib\site-packages\torch\nn\modules\conv.py", line 730, in _conv_forward
return F.conv3d(
^^^^^^^^^
RuntimeError: Input type (CUDABFloat16Type) and weight type (CPUBFloat16Type) should be the sameSystem Info
- 🤗 Diffusers version: 0.44.0.dev0
- Platform: Windows-11-10.0.26300-SP0
- Running on Google Colab?: No
- Python version: 3.12.16
- PyTorch version (GPU?): 2.14.1+cu130 (True)
- Huggingface_hub version: 2.6.0
- Transformers version: 5.25.1
- Accelerate version: 1.9.0
- PEFT version: not installed
- 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
Activity