Fix ErnieImagePipeline pre-computed prompt_embeds + num_images_per_prompt shape mismatch#13532
Open
Ricardo-M-L wants to merge 1 commit intohuggingface:mainfrom
Open
Conversation
…ompt
When a user passes pre-computed `prompt_embeds` (or `negative_prompt_embeds`)
alongside `num_images_per_prompt > 1`, `ErnieImagePipeline.__call__` did
not replicate the provided embeddings — the embeds list kept its original
length (one per prompt) while the latents were allocated with
`total_batch_size = batch_size * num_images_per_prompt`:
text_hiddens = prompt_embeds # length = batch_size (NOT replicated)
...
latents = randn_tensor((total_batch_size, ...)) # batch * N in shape
In the denoise loop `text_bth.shape[0]` then mismatches
`latent_model_input.shape[0]`, so the transformer call:
pred = self.transformer(
hidden_states=latent_model_input, # (batch*N*2, ...) under CFG
text_bth=text_bth, # (batch*2, ...)
...
)
fails with a shape mismatch inside the attention block. The standard
"pre-compute embeds once, generate N variants" usage pattern is broken.
`encode_prompt` already performs this replication internally
(`for _ in range(num_images_per_prompt): text_hiddens.append(hidden)`
at lines 158-160), so the non-embed path is unaffected — this only
impacts callers of the documented `prompt_embeds` / `negative_prompt_embeds`
arguments.
Mirror the replication logic in the pre-embed branches so both paths
yield a `text_hiddens` list of length `batch_size * num_images_per_prompt`.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
yiyixuxu
approved these changes
Apr 21, 2026
Collaborator
yiyixuxu
left a comment
There was a problem hiding this comment.
thanks!
would you be willing to add a pipeline test for ernie-image? a new PR is ok
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
When a user passes pre-computed
prompt_embeds(ornegative_prompt_embeds) alongsidenum_images_per_prompt > 1,ErnieImagePipeline.__call__did not replicate the provided embeddings — the embeds list kept its original length (one per prompt) while the latents were allocated withtotal_batch_size = batch_size * num_images_per_prompt:https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/ernie_image/pipeline_ernie_image.py#L287-L298
Why this is a real bug
In the denoise loop, latents are expanded to
total_batch_size:https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/ernie_image/pipeline_ernie_image.py#L329-L343
text_bth.shape[0]is derived fromlen(cfg_text_hiddens), which islen(prompt_embeds) * 2under CFG — i.e.batch_size * 2, notbatch_size * N * 2. This produces a shape mismatch inside the transformer's text-conditioning attention, and the call raises aRuntimeError. The standard "pre-compute embeds once, generate N variants" usage pattern is broken.Minimal repro
Fix
encode_promptalready performs this replication internally:https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/ernie_image/pipeline_ernie_image.py#L158-L160
Mirror the same replication in the pre-embed branches so both paths yield a
text_hiddenslist of lengthbatch_size * num_images_per_prompt:The non-embed path is unaffected since
encode_promptalready replicates.Before submitting
prompt_embeds+num_images_per_promptcombo.Who can review?
@yiyixuxu @sayakpaul