Skip to content

[Kandinsky 6] Fix expand_prompts with an image under enable_model_cpu_offload() - #15504

Draft
chsu-amd wants to merge 1 commit into
huggingface:mainfrom
chsu-amd:fix-kandinsky6-expand-prompts-model-offload
Draft

chsu-amd wants to merge 1 commit into
huggingface:mainfrom
chsu-amd:fix-kandinsky6-expand-prompts-model-offload

Conversation

@chsu-amd

@chsu-amd chsu-amd commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15503

expand_prompts calls text_encoder.generate(...). Since huggingface/transformers#45783 (v5.18.0), generate() runs get_image_features before the first top-level forward, the only call enable_model_cpu_offload() hooks, so with image= the vision tower runs with its weights still on cpu.

This PR fires the text encoder's accelerate hook before generate(), in the same form as #15456 and apply_forward_hook (utils/accelerate_utils.py).

Results

main at facc812 + this diff, the #15503 snippet (released weights), NVIDIA GeForce RTX 5090, torch 2.14.1+cu130, accelerate 1.9.0:

  • transformers 5.25.1: main raises the device-mismatch error in expand_prompts; with this PR the error is gone and expand_prompts returns a prompt.
  • transformers 5.16.1: the expanded prompt is byte-identical between main and this PR.
  • No run produced the video: the 56 GiB bf16 transformer does not fit in 32 GB (5.16.1 runs hit a 45 min cap at step 0/10; the 5.25.1 run with this PR was stopped while loading it).
  • Tiny test components on main (RTX 5090 and AMD Radeon 8060S): offload output equals pipe.to("cuda") (max abs diff 0) on 5.16.1 and raises on 5.21.0. This PR was not run on them.

Outside the reported case:

  • expand_prompts=False: the added lines are not reached.
  • expand_prompts=True without image= under model offload: the hook fires before generate() instead of at its first forward. Not measured.
  • No accelerate hook (pipe.to(...), group offloading): the lines do nothing. Not run.
  • Other accelerate hooks (sequential offload, device_map="balanced"): one extra pre_forward(text_encoder) call per prompt. Not measured.

Self-review notes (self-review skill): blocking none. Left for review: hand-fired hook (pipelines.md gotcha 8); hasattr(hook, "pre_forward") is always true, kept to match apply_forward_hook; no test, as MemoryTesterMixin offloads without image= / expand_prompts=True. ruff check / format pass.

Before submitting

Who can review?

@yiyixuxu @sayakpaul

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

1 participant