diffusers icon indicating copy to clipboard operation
diffusers copied to clipboard

Update stable_diffusion_tensorrt_txt2img.py

Open Bhavay-2001 opened this issue 4 months ago • 23 comments

What does this PR do?

Fixes #6984

Before submitting

  • [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • [x] Did you read the contributor guideline?
  • [x] Did you read our philosophy doc (important for complex PRs)?
  • [x] Was this discussed/approved via a GitHub issue or the forum? Please add a link to it if that's the case.
  • [ ] Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • [ ] Did you write any new necessary tests?

Who can review?

@yiyixuxu

Bhavay-2001 avatar Feb 21 '24 17:02 Bhavay-2001

Hi @yiyixuxu, I have created a sample draft file based on my understanding of the problem. I would love to discuss over this and edit the file as needed. Please review. Thanks

Bhavay-2001 avatar Feb 21 '24 17:02 Bhavay-2001

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.

Hi:

Can you make sure the code example runs as expected? https://github.com/huggingface/diffusers/blob/main/examples/community/README.md#tensorrt-text2image-stable-diffusion-pipeline

yiyixuxu avatar Feb 21 '24 19:02 yiyixuxu

Hi @yiyixuxu, soo I need to run this code sample on some online notebook and produce the output right?

Bhavay-2001 avatar Feb 22 '24 09:02 Bhavay-2001

Hi @yiyixuxu, I tried running the example you suggested on google colab but it is exploding the memory. Should I run it on my laptop? Will it cause the memory to just explode?

Bhavay-2001 avatar Feb 24 '24 15:02 Bhavay-2001

Hi @yiyixuxu, could you please review my notebook here. I am trying to run the example but the colab memory seems to blow up every time. I am using thr T4 GPU. Thanks

Bhavay-2001 avatar Feb 27 '24 16:02 Bhavay-2001

Hi @standardAI, didn't get your comment. I need to find some function in TensorRTStableDiffusionPipeline that is the same as from StableDiffusionPipeline right? You said some function in TensorRT which is not in TensorRT so I'm confused.

Bhavay-2001 avatar Feb 29 '24 15:02 Bhavay-2001

Find a/several (0)self.something: 1. Used/Called in TensorRTStableDiffusionPipeline 2. Haven't been defined in TensorRTStableDiffusionPipeline 3. Defined in StableDiffusionPipeline 4. Haven't been defined in DiffusionPipeline

Then (5)copy its/their definition into TensorRTStableDiffusionPipeline from StableDiffusionPipeline and (6)add the "# Copied from ..." phrase on top of its/their definition.

tolgacangoz avatar Feb 29 '24 15:02 tolgacangoz

Hi @standardAI, I have made the changes. Should I try to run the colab notebook again? Why is the memory expanding in that case?

Bhavay-2001 avatar Feb 29 '24 16:02 Bhavay-2001

Hi @standardAI, done!

Bhavay-2001 avatar Feb 29 '24 18:02 Bhavay-2001

Good work! For now, I need to be AFK. I will further review them tomorrow if needed.

tolgacangoz avatar Feb 29 '24 18:02 tolgacangoz

Hi @standardAI, thank you for your kind suggestions. I have made the necessary changes and I think it will be good to go. @yiyixuxu could you please start the tests to merge it? I would like to see if its merges now properly. Thanks

Bhavay-2001 avatar Feb 29 '24 18:02 Bhavay-2001

Hi:

Can you make sure the code example runs as expected? main/examples/community/README.md#tensorrt-text2image-stable-diffusion-pipeline

Try this example to see if it works.

tolgacangoz avatar Feb 29 '24 20:02 tolgacangoz

Hi @standardAI can you please review?

Bhavay-2001 avatar Mar 03 '24 17:03 Bhavay-2001

Hi @asfiyab-nvidia, thank you again for your TensorRT-version contribution! Could you examine this refactor being done by @Bhavay-2001? Now, your implementation is supposed to be inherited from DiffusionPipeline rather than StableDiffusionPipeline. Unfortunately, I know nothing about TensorRT.

tolgacangoz avatar Mar 05 '24 08:03 tolgacangoz

A few comments

  1. Please update the example usage in the README as well.
  2. Based on my testing, the pipeline needs a few more updates in the init call. The first error I'm encountering is below. Fixing it leads to other issues that need to be addressed as well
ValueError: Pipeline <class 'diffusers_modules.local.stable_diffusion_tensorrt_txt2img.TensorRTStableDiffusionPipeline'> expected {'scheduler', 'tokenizer', 'unet', 'text_encoder', 'vae', 'safety_checker', 'feature_extractor', 'image_encoder'}, but only {'scheduler', 'tokenizer', 'unet', 'text_encoder', 'vae'} were passed.

Script I'm using to test (adapted from the example in the community readme)

import torch
from diffusers import DDIMScheduler
from diffusers.pipelines import DiffusionPipeline

# Use the DDIMScheduler scheduler here instead
scheduler = DDIMScheduler.from_pretrained("stabilityai/stable-diffusion-2-1",
                                            subfolder="scheduler")

pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1",
                                                custom_pipeline="/workspace/diffusers/examples/community/stable_diffusion_tensorrt_txt2img.py",
                                                revision='fp16',
                                                torch_dtype=torch.float16,
                                                scheduler=scheduler,)

# re-use cached folder to save ONNX models and TensorRT Engines
pipe.set_cached_folder("stabilityai/stable-diffusion-2-1", revision='fp16',)

pipe = pipe.to("cuda")

prompt = "a beautiful photograph of Mt. Fuji during cherry blossom"
image = pipe(prompt).images[0]
image.save('tensorrt_mt_fuji.png')

asfiyab-nvidia avatar Mar 05 '24 21:03 asfiyab-nvidia

Thank you very much for your feedback @asfiyab-nvidia!

tolgacangoz avatar Mar 06 '24 08:03 tolgacangoz

A few comments

  1. Please update the example usage in the README as well.
  2. Based on my testing, the pipeline needs a few more updates in the init call. The first error I'm encountering is below. Fixing it leads to other issues that need to be addressed as well
ValueError: Pipeline <class 'diffusers_modules.local.stable_diffusion_tensorrt_txt2img.TensorRTStableDiffusionPipeline'> expected {'scheduler', 'tokenizer', 'unet', 'text_encoder', 'vae', 'safety_checker', 'feature_extractor', 'image_encoder'}, but only {'scheduler', 'tokenizer', 'unet', 'text_encoder', 'vae'} were passed.

Script I'm using to test (adapted from the example in the community readme)

import torch
from diffusers import DDIMScheduler
from diffusers.pipelines import DiffusionPipeline

# Use the DDIMScheduler scheduler here instead
scheduler = DDIMScheduler.from_pretrained("stabilityai/stable-diffusion-2-1",
                                            subfolder="scheduler")

pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1",
                                                custom_pipeline="/workspace/diffusers/examples/community/stable_diffusion_tensorrt_txt2img.py",
                                                revision='fp16',
                                                torch_dtype=torch.float16,
                                                scheduler=scheduler,)

# re-use cached folder to save ONNX models and TensorRT Engines
pipe.set_cached_folder("stabilityai/stable-diffusion-2-1", revision='fp16',)

pipe = pipe.to("cuda")

prompt = "a beautiful photograph of Mt. Fuji during cherry blossom"
image = pipe(prompt).images[0]
image.save('tensorrt_mt_fuji.png')

Hi @asfiyab-nvidia, should I delete these parameters from the init class or provide a default value for them? Because those parameters are not defined in the DiffusionPipeline class

Bhavay-2001 avatar Mar 06 '24 15:03 Bhavay-2001

Hi @asfiyab-nvidia, should I delete these parameters from the init class or provide a default value for them? Because those parameters are not defined in the DiffusionPipeline class

When you add a proper class attribute, this error disappears. Please examine other pipelines.

tolgacangoz avatar Mar 06 '24 15:03 tolgacangoz

@Bhavay-2001 nice progress! @standardAI @asfiyab-nvidia thank you all for helping out! 😇😇😇

yiyixuxu avatar Mar 07 '24 05:03 yiyixuxu

if hasattr(scheduler.config, "steps_offset") and scheduler.config.steps_offset != 1:

Hi @standardAI, are you saying adding these lines of code in order to add logger, deprecate and ValueError?

Sorry, I was not able to quote the exact same line from your file but I tried my best to make it easier for you.

Bhavay-2001 avatar Mar 09 '24 02:03 Bhavay-2001

Right 👍.

tolgacangoz avatar Mar 09 '24 07:03 tolgacangoz

I have added for all the parameters but image_encoder because I couldn't find its usage beyond tensorrt.

Also, the other 2 tensorrt files like inpaint and img2img doesn't have these class attributes written. Do we have to change in them as well?

Thanks

Bhavay-2001 avatar Mar 09 '24 11:03 Bhavay-2001

PR has been shifted here

Bhavay-2001 avatar Mar 13 '24 15:03 Bhavay-2001