Skip to content

Conversation

@manuelbrack
Copy link
Contributor

As discussed in isuse #1242 this PR adds a new Pipeline implementing the paper on Safe Latent Diffusion.
SLD mitigates the well known issue that models like Stable Diffusion that are trained on unfiltered, web-crawled datasets tend to suffer from inappropriate degeneration. For instance SD may unexpectedly generate nudity, violence, images depicting self-harm, or otherwise offensive content.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Nov 10, 2022

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

Ran make fix-copies after adding new pipline
@manuelbrack
Copy link
Contributor Author

@patrickvonplaten I'd appreciate some pointers on the check that is currently failing. I'm not really sure how this is something I can fix.

Error message:

Bad request for commit endpoint:
[37](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:38)
KeyError: 'endpoint'
[38](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:39)

[39](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:40)
Traceback (most recent call last):
[40](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:41)
  File "/usr/local/lib/python3.8/site-packages/huggingface_hub/utils/_errors.py", line 213, in hf_raise_for_status
[41](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:42)
    response.raise_for_status()
[42](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:43)
  File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 1021, in raise_for_status
[43](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:44)
    raise HTTPError(http_error_msg, response=self)
[44](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:45)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://huggingface.co/api/datasets/hf-doc-build/doc-build-dev/commit/main
[45](https://github.com/huggingface/diffusers/actions/runs/3438139061/jobs/5733811656#step:11:46)

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@manuelbrack
Copy link
Contributor Author

Alright as it appears the issue from before was not related to the PR itself.
@patrickvonplaten just let me know how we should proceed from here.

@patrickvonplaten
Copy link
Contributor

Hey @manuelbrack,

Sorry for replying so late here. The PR looks to be in a great shape :-)
Could you maybe add some fast and one slow test? Think it could be very similar to those ones here: https://github.com/huggingface/diffusers/blob/main/tests/pipelines/altdiffusion/test_alt_diffusion.py (you can probably copy paste most of it :-))

Also should we maybe upload a stable diffusion checkpoint under your repo on the Hub clearly advertising it as a "safety stable diffusion" model and also so that DiffusionPipeline will automatically detect the correct class?

Finally, it could be nice to add your model to the docs with a nice doc page linking to your original repo and your paper. Maybe you can have a look at this PR: #1299 to see what things you could all add to the doc (pipeline table, pipeline page, ...)

Let me know if something is not clear! Also very happy to dive into the PR next week if you're stuck somewhere :-) Very exciting - the first really safety driven model in diffusers 🥳

@manuelbrack
Copy link
Contributor Author

manuelbrack commented Nov 17, 2022

Hi @patrickvonplaten,

Thanks for the feedback, we are already working on the refactoring, docs and test.

Adding a checkpoint to the our labs huggingface page sounds like a great idea. Should we get that going, or is this something that we should leave up to you guys?
We would also be interested in hosting a demo space on hugging face for the safe stable diffusion model. Feel free to contact me on this.

@manuelbrack
Copy link
Contributor Author

manuelbrack commented Nov 17, 2022

TODOs

  • Refactor code in Pipeline
  • Add our pipeline to docs
  • Add some fast test
  • Add a slow test
  • Host Safe Stable Diffusion checkpoint at https://huggingface.co/AIML-TUDA

@manuelbrack manuelbrack changed the title [WIP] Add Safe Stable Diffusion Pipeline Add Safe Stable Diffusion Pipeline Nov 18, 2022
```python
>>> from diffusers import StableDiffusionPipelineSafe

>>> pipeline = StableDiffusionPipelineSafe.from_pretrained("AIML-TUDA/stable-diffusion-safe")
Copy link
Contributor

Choose a reason for hiding this comment

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

Very cool examples!

image = self.decode_latents(latents)

# 9. Run safety checker
image, has_nsfw_concept = self.run_safety_checker(image, device, text_embeddings.dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering whether we really need the safety checker here - given that the model is already guided towards safe images before does this final CLIP check help?

The advantage of removing it would be that we would save 1GB on RAM and potentially have much fewer false positives (e.g. cases where the images is incorrectly classified as nfsw).
But also happy to leave it in if you think it adds another important layer of "safety"

@patrickvonplaten
Copy link
Contributor

Hey @manuelbrack,

Great work! The tests and doc examples are on point and it's also very nice that you additionally hold the checkpoint here: https://huggingface.co/AIML-TUDA/stable-diffusion-safe

Just two two final things before merging:

    1. Could you allow me to make changes to your PR (e.g. enable this: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork). The reason is that the slow gpu tests are very hardware dependent for diffusion models and the tests as currently written fail on our target hardware (V100), I could quickly correct the expected values to make the tests pass, but I'd need right to push to your PR :-)
    1. I'm wondering whether we should leave the safety checker module in or not. Or whether we should maybe remove the warning if no safety checker is used. The reason is that the safety checker is >1GB in model size and make the pipeline heavy. For your pipeline, I'm wondering if the safety checker is that useful. => totally up to you - for me it makes perfect sense to leave as is right now, but was just wondering if the safety checker was maybe just copy-pasted and doesn't really add functionality to this pipeline as the guided images are already "safe".

Once I can update the tests after 1. , we can merge this one :-)

Wasn't able to find your email address online - could you maybe write me an email at patrick[at]huggingface.co so that we could maybe connect with our ethics / safety group at Hugging Face and think about how to best promote this model together? :-)

@manuelbrack
Copy link
Contributor Author

Hi @patrickvonplaten,

regarding the safety checker we discussed the issue and opted to have it enabled by default.
However, if safety was enabled the warning message of flagged images will be adjusted and the user may look at the generated images at their own discretion in a dedicated variable of the output class.
Reasoning being that weak hyper parameter configurations for safety guidance are unlikely to overcome strong biases of the model.
So we thought it best to have the extra layer in there.

@patrickvonplaten
Copy link
Contributor

Cool merging this PR then :-)

@patrickvonplaten patrickvonplaten merged commit e50c25d into huggingface:main Nov 22, 2022
@patrickvonplaten patrickvonplaten deleted the safe-latent-diffusion branch November 22, 2022 10:51
@patrickvonplaten
Copy link
Contributor

@manuelbrack let me know if you'd like to get in touch with our diffusers team to promote your work here. We could think about writing a blog post about how this model mitigates known problems of stable diffusion here: https://huggingface.co/blog or create a nice space.

Feel free to send me an email :-)

yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* Add pipeline_stable_diffusion_safe.py to pipelines

* Fix repository consistency

Ran make fix-copies after adding new pipline

* Add Paper/Equation reference for parameters to doc string

* Ensure code style and quality

* Perform code refactoring

* Fix copies inherited from merge with huggingface/main

* Add docs

* Fix code style

* Fix errors in documentation

* Fix refactoring error

* remove debugging print statement

* added Safe Latent Diffusion tests

* Fix style

* Fix style

* Add pre-defined safety configurations

* Fix line-break

* fix some tests

* finish

* Change safety checker

* Add missing safety_checker.py file

* Remove unused imports

Co-authored-by: PatrickSchrML <[email protected]>
Co-authored-by: Patrick von Platen <[email protected]>
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.

4 participants