Skip to content

add support for pre-calculated prompt embeds to Stable Diffusion ONNX pipelines #2597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2023

Conversation

ssube
Copy link
Contributor

@ssube ssube commented Mar 8, 2023

Motivation

I would like to calculate the prompt and negative prompt embeds ahead of time using my own logic and pass them into the ONNX version of the SD pipeline, which is supported by the regular non-ONNX pipeline, https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L571, but not available for ONNX. This should bring parity between the two for the prompt_embeds and negative_prompt_embeds parameters.

If this change is acceptable, I would like to do the same to the other ONNX pipelines. I can add those changes to this same PR or make another one, whatever works, I know you all have limited time for the ONNX stuff. Looks like the build requires the change to be copied out to the other pipelines, so that's in this PR as well.

Example

prompt = "an astronaut eating a hamburger"
steps = 50

pipe = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")

tokens = pipe.tokenizer(
    prompt,
    padding="max_length",
    max_length=pipe.tokenizer.model_max_length,
    truncation=True,
    return_tensors="np",
)
embeds = pipe.text_encoder(input_ids=tokens.input_ids.astype(np.int32))[0]

image = pipe(
    num_inference_steps=steps,
    prompt_embeds=embeds,
).images[0]

@ssube ssube force-pushed the onnx-prompt-embeds branch from 13789ed to 51d33fd Compare March 8, 2023 00:41
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Mar 8, 2023

The documentation is not available anymore as the PR was closed or merged.

@ssube ssube changed the title add support for pre-calculated prompt embeds to Stable Diffusion ONNX pipeline add support for pre-calculated prompt embeds to Stable Diffusion ONNX pipelines Mar 8, 2023
@patrickvonplaten patrickvonplaten requested a review from anton-l March 9, 2023 11:34
Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very clean to me and makes sens! Thanks a lot - @anton-l could you take a quick look here?

@github-actions
Copy link
Contributor

github-actions bot commented Apr 7, 2023

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@github-actions github-actions bot added the stale Issues that haven't received updates label Apr 7, 2023
@ssube ssube force-pushed the onnx-prompt-embeds branch from 72d1546 to 57f93de Compare April 8, 2023 03:42
@ssube
Copy link
Contributor Author

ssube commented Apr 9, 2023

This is still useful to me (and hopefully others). I got sidetracked for a little bit, but I've rebased the branch and the existing tests look good - happy to add more if you'd like, or make any other changes.

This will be really helpful for custom prompt parsing, like weights and group and expansion stuff.

@patrickvonplaten
Copy link
Contributor

Sorry for taking so long here @ssube - merging!

@patrickvonplaten patrickvonplaten merged commit 7b2407f into huggingface:main Apr 12, 2023
@ssube
Copy link
Contributor Author

ssube commented Apr 13, 2023

No worries, thank you! Always happy to add more tests or make changes, I don't want to break things for anyone, especially my own project 😄

w4ffl35 pushed a commit to w4ffl35/diffusers that referenced this pull request Apr 14, 2023
… pipelines (huggingface#2597)

* add support for prompt embeds to SD ONNX pipeline

* fix up the pipeline copies

* add prompt embeds param to other ONNX pipelines

* fix up prompt embeds param for SD upscaling ONNX pipeline

* add missing type annotations to ONNX pipes
dg845 pushed a commit to dg845/diffusers that referenced this pull request May 6, 2023
… pipelines (huggingface#2597)

* add support for prompt embeds to SD ONNX pipeline

* fix up the pipeline copies

* add prompt embeds param to other ONNX pipelines

* fix up prompt embeds param for SD upscaling ONNX pipeline

* add missing type annotations to ONNX pipes
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
… pipelines (huggingface#2597)

* add support for prompt embeds to SD ONNX pipeline

* fix up the pipeline copies

* add prompt embeds param to other ONNX pipelines

* fix up prompt embeds param for SD upscaling ONNX pipeline

* add missing type annotations to ONNX pipes
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
… pipelines (huggingface#2597)

* add support for prompt embeds to SD ONNX pipeline

* fix up the pipeline copies

* add prompt embeds param to other ONNX pipelines

* fix up prompt embeds param for SD upscaling ONNX pipeline

* add missing type annotations to ONNX pipes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issues that haven't received updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants