Skip to content

Conversation

@ayushtues
Copy link

@ayushtues ayushtues commented May 22, 2023

Adding Unet model and conversion scripts, part of huggingface#3492

To-do

  • Add building blocks needed for the original repo Unet implementation
  • Conversion script for weights into diffusers + load pretrained weights into diffusers
  • Check if forward passes into pretrained model give same results as original repo

jongwooo and others added 30 commits May 16, 2023 12:51
Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
* add stable diffusion tensorrt img2img pipeline

Signed-off-by: Asfiya Baig <asfiyab@nvidia.com>

* update docstrings

Signed-off-by: Asfiya Baig <asfiyab@nvidia.com>

---------

Signed-off-by: Asfiya Baig <asfiyab@nvidia.com>
* refactor controlnet and add img2img and inpaint

* First draft to get pipelines to work

* make style

* Fix more

* Fix more

* More tests

* Fix more

* Make inpainting work

* make style and more tests

* Apply suggestions from code review

* up

* make style

* Fix imports

* Fix more

* Fix more

* Improve examples

* add test

* Make sure import is correctly deprecated

* Make sure everything works in compile mode

* make sure authorship is correctly attributed
* Add DPM-Solver Multistep Inverse Scheduler

* Add draft tests for DiffEdit

* Add inverse sde-dpmsolver steps to tune image diversity from inverted latents

* Fix tests

---------

Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
fix tiled vae bleand extent range
Small update to "Next steps" section:

- PyTorch 2 is recommended.
- Updated improvement figures.
…e#3298)

* Update pipeline_if_superresolution.py

Allow arbitrary aspect ratio in IFSuperResolutionPipeline by using the input image shape

* IFSuperResolutionPipeline: allow the user to override the height and width through the arguments

* update IFSuperResolutionPipeline width/height doc string to match StableDiffusionInpaintPipeline conventions

---------

Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
…gingface#3424)

* Added explanation of 'strength' parameter

* Added get_timesteps function which relies on new strength parameter

* Added `strength` parameter which defaults to 1.

* Swapped ordering so `noise_timestep` can be calculated before masking the image

this is required when you aren't applying 100% noise to the masked region, e.g. strength < 1.

* Added strength to check_inputs, throws error if out of range

* Changed `prepare_latents` to initialise latents w.r.t strength

inspired from the stable diffusion img2img pipeline, init latents are initialised by converting the init image into a VAE latent and adding noise (based upon the strength parameter passed in), e.g. random when strength = 1, or the init image at strength = 0.

* WIP: Added a unit test for the new strength parameter in the StableDiffusionInpaintingPipeline

still need to add correct regression values

* Created a is_strength_max to initialise from pure random noise

* Updated unit tests w.r.t new strength parameter + fixed new strength unit test

* renamed parameter to avoid confusion with variable of same name

* Updated regression values for new strength test - now passes

* removed 'copied from' comment as this method is now different and divergent from the cpy

* Update src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py

Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>

* Ensure backwards compatibility for prepare_mask_and_masked_image

created a return_image boolean and initialised to false

* Ensure backwards compatibility for prepare_latents

* Fixed copy check typo

* Fixes w.r.t backward compibility changes

* make style

* keep function argument ordering same for backwards compatibility in callees with copied from statements

* make fix-copies

---------

Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
Co-authored-by: William Berman <WLBberman@gmail.com>
…s partially downloaded (huggingface#3448)

Added bugfix using f strings.
…grad=False) (huggingface#3404)

* gradient checkpointing bug fix

* bug fix; changes for reviews

* reformat

* reformat

---------

Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
* Make dreambooth lora more robust to orig unet

* up
…y're unnecessary) (huggingface#3463)

Release large tensors in attention (as soon as they're no longer required). Reduces peak VRAM by nearly 2 GB for 1024x1024 (even after slicing), and the savings scale up with image size.
add min snr to text2img lora training script
* add inpaint lora scale support

* add inpaint lora scale test

---------

Co-authored-by: yueyang.hyy <yueyang.hyy@alibaba-inc.com>
* Correct from_ckpt

* make style
* dreambooth docs torch.compile note

* Update examples/dreambooth/README.md

Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>

* Update examples/dreambooth/README.md

Co-authored-by: Pedro Cuenca <pedro@huggingface.co>

---------

Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
* add textual inversion inference to docs

* add to toctree

---------

Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
* distributed inference

* move to inference section

* apply feedback

* update with split_between_processes

* apply feedback
…lattened indices (huggingface#3479)

explicit view kernel size as number elements in flattened indices
* Remove ONNX tests from PR.

They are already a part of push_tests.yml.

* Remove mps tests from PRs.

They are already performed on push.

* Fix workflow name for fast push tests.

* Extract mps tests to a workflow.

For better control/filtering.

* Remove --extra-index-url from mps tests

* Increase tolerance of mps test

This test passes in my Mac (Ventura 13.3) but fails in the CI hardware
(Ventura 13.2). I ran the local tests following the same steps that
exist in the CI workflow.

* Temporarily run mps tests on pr

So we can test.

* Revert "Temporarily run mps tests on pr"

Tests passed, go back to running on push.
…ocessor2_0` (huggingface#3457)

* add: debugging to enabling memory efficient processing

* add: better warning message.
add note on local directory path.

Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com>
@ayushtues
Copy link
Author

ayushtues commented May 23, 2023

@dg845 Was able to convert the Unet into diffusers, load pretrained checkpoints and get same outputs from forward passes:

ckpt : https://huggingface.co/ayushtues/consistency_models/tree/main

unet = UNet2DModel.from_pretrained("ayushtues/consistency_models", subfolder="diffusers_cd_imagenet64_l2")

@ayushtues
Copy link
Author

ayushtues commented May 23, 2023

Had to add two new blocks to unet_2d_blocks - AttnDownsampleBlock2D & AttnUpsampleBlock2D, because I needed a resnet based down/upsampler along with attention, and the existing AttnDownBlock2D &AttnUpBlock2D just used a simple conv downsampler.

Open to suggestions on how to design this better

@ayushtues ayushtues changed the title [WIP] Add Unet blocks for consistency models [WIP] Add Unet for consistency models May 23, 2023
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented May 23, 2023

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

@dg845
Copy link
Owner

dg845 commented May 24, 2023

Sorry, didn't get a chance to look at this until now.

Had to add two new blocks to unet_2d_blocks - AttnDownsampleBlock2D & AttnUpsampleBlock2D, because I needed a resnet based down/upsampler along with attention, and the existing AttnDownBlock2D &AttnUpBlock2D just used a simple conv downsampler.

Open to suggestions on how to design this better

The new blocks look fine to me. I guess I'm not the most knowledgeable about unet_2d_blocks so it might be worth it to ask the maintainers directly about the design.

Also, I have a quick question: does the conversion script support class-conditional models? I see for example in the consistency model scripts that there are some examples of training and sampling from them.

@dg845
Copy link
Owner

dg845 commented May 24, 2023

I took a closer look at the unet block implementations and it looks like SimpleCrossAttnDownBlock2D/SimpleCrossAttnUpBlock2D are pretty similar to AttnDownsampleBlock2D/AttnUpsampleBlock2D in having a ResnetBlock2D/Attention/ResnetBlock2D downsampler/upsampler pattern. Might it be possible to leverage some or all of the SimpleCrossAttnDownBlock2D/SimpleCrossAttnUpBlock2D implementation?

@ayushtues
Copy link
Author

ayushtues commented May 25, 2023

Sorry, I was blocked by Github for a while ( supposedly a VS code extension was generating infinite PRs 🙈 ).

Also, I have a quick question: does the conversion script support class-conditional models? I see for example in the consistency model scripts that there are some examples of training and sampling from them.

The current Unet2DModel already supports class conditioning : https://github.com/ayushtues/diffusers/blob/consistency_unet/src/diffusers/models/unet_2d.py#L223, and infact I tested this while making sure the forward passes are the same

@ayushtues
Copy link
Author

ayushtues commented May 25, 2023

I took a closer look at the unet block implementations and it looks like SimpleCrossAttnDownBlock2D/SimpleCrossAttnUpBlock2D are pretty similar to AttnDownsampleBlock2D/AttnUpsampleBlock2D in having a ResnetBlock2D/Attention/ResnetBlock2D downsampler/upsampler pattern. Might it be possible to leverage some or all of the SimpleCrossAttnDownBlock2D/SimpleCrossAttnUpBlock2D implementation?

These blocks use AttnAddedKVProcessor, which has a different Attention implementation, and would have been non-trivial to convert it into what we need without changing the Attention Processor. Also it would be better to keep the Cross-Attention implementations seperate from simple attention imo.

Also the new blocks are simply a copy of AttnDownBlock2D/AttnUpBlock2D, with self.downsamplers/self.upsamplers being a resnet instead of a simple down/upsampler.

So we can also instead parameterize AttnDownBlock2D to have a resnet downsampler instead based on an argument

@dg845
Copy link
Owner

dg845 commented May 25, 2023

Makes sense, looks good to me then :).

@dg845 dg845 merged commit d137d11 into dg845:consistency-models-pipeline May 25, 2023
dg845 pushed a commit that referenced this pull request Sep 19, 2023
* Implement `CustomDiffusionAttnProcessor2_0`

* Doc-strings and type annotations for `CustomDiffusionAttnProcessor2_0`. (#1)

* Update attnprocessor.md

* Update attention_processor.py

* Interops for `CustomDiffusionAttnProcessor2_0`.

* Formatted `attention_processor.py`.

* Formatted doc-string in `attention_processor.py`

* Conditional CustomDiffusion2_0 for training example.

* Remove unnecessary reference impl in comments.

* Fix `save_attn_procs`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.