Repository navigation
Description
It's really difficult to test stable diffusion due to the following:
-
- Continous output: Diffusion models take float values as input and output float values. This is different from NLP models which tend to take int64 as inputs and int64 as outputs.
-
- Output dimensions are huge. If an image has a output size of
(1, 512, 512, 3)this means that there are 512 * 512 * 3 ~ 800,000 values that need to be within a given range. Say if you want to test for a max difference of(pred - ref).abs() < 1e-3we have roughly a million values where this has to hold true. This is quite different in NLP where we rather test things like text generation or final logit layers which usually aren't bigger then a dozen or so tensors of size 768 or 1024.
- Output dimensions are huge. If an image has a output size of
-
- Error propagation: We cannot simple test one forward pass for stable diffusion because in practice people use 50 forward passes. Error propagation becomes a real problem in this case. This again is different from say generation in NLP because in generation at every generation step errors can be somewhat "smoothed" out since a "argmax" of "softmax" operation is used after each step
-
- Composite systems: Stable Diffusion has three main components for inference: A Unet, a scheduler and a VAE decoder. The UNet and Scheduler are very entangled during the forward pass. Just because we know the forward pass of both the scheduler and unet work independently, it doesn't mean that using them together works.
=> Therefore, we need to do full integration tests, meaning we need to make sure that the output of a full denoising process stays within a given error range. At the moment, we're having quite some problems though to get full reproducible of results on different GPUs, CUDA versions etc... (especially for FP16).
That being said, it is extremely important to test stable diffusion to avoid issues like this in the future: #902 whereas we should still be able to improve speed with PRs like this: #371
At the moment, we're running multiple integration tests for all 50 diffusion steps every time a PR is merged to master, see:
class PipelineIntegrationTests(unittest.TestCase): class PipelineIntegrationTests(unittest.TestCase): class PipelineIntegrationTests(unittest.TestCase):
Nevertheless, the tests weren't sufficient to detect: #902
Testing Puzzle 🧩: How can we find the best trade-off between fast & in-expensive test and best possible test coverage taking into account the above points?
We already looked quite a bit into: https://pytorch.org/docs/stable/notes/randomness.html
Activity
patrickvonplaten commentedon Oct 21, 2022
Some ideas for improvement:
At the moment, we're not comparing against numpy float outputs but against the generated images:
diffusers/tests/pipelines/stable_diffusion/test_stable_diffusion_inpaint.py
Line 282 in 31af4d1
We should probably set up a better "layered" testing structure:
1e-5or1e-4), same for VAEpatrickvonplaten commentedon Oct 21, 2022
Pinging some people that might have good ideas (maybe big NLP models have similar problems) - cc @stas00 @sgugger @ydshieh @thomasw21 @anton-l
anton-l commentedon Oct 21, 2022
Unit-testing individual parts of the pipeline + nightly CPU integration tests seems like a solid path to me at the moment.
For now we can check how spotty our line coverage is when disabling the slow integration tests: lots of room for improvement there.
ydshieh commentedon Oct 21, 2022
Regarding 2), I see in
transformers, we test just a slice of the outputs in some cases, something like(well, this approach is probably not what
diffuserswant to take)diffusers, is it possible to test against smaller input images (i.e. creating smaller models that accept smaller image inputs)?Regarding different GPUs: I think we can focus on one type of GPU at this moment. In
transformers, we only run onT4so far. Once the testing become better/complete (i.e. when we are happy enough), we can enhance it.ydshieh commentedon Oct 21, 2022
If the pipeline used in that issue is tested in
diffusers, it's probably a good idea to run that related test in the 2 mentioneddiffusersversions and see if both pass. If so, we can try to figure out what the testing lacks to detect the regression in #902.thomasw21 commentedon Oct 22, 2022
Maybe I'm naive, but in theory everything in discretized to bits. So if you can guarantee determinism (probably a bunch of flags in pytorch + blacklisting some operations), output can be compared bitwise, ie have perfect equality. That does mean that your test become somewhat setup specific (torch version, type of hardware etc ...) We have things like that on some unit tests in Megatron-DeepSpeed: https://github.com/bigscience-workshop/Megatron-DeepSpeed/blob/4255845d0685dd59cb70c0026db45f92b7ae4848/tests/test_activations.py#L30
If you have a very strict test (
typically torch_assert_equal), unless you have some inductive bias where some values are not affected, you could randomly sample elements (like 0.1% for example) and run those assertions there? Though I'm not sure I understand what's the issue with comparing 1M values, those operations are pretty fast on GPUs for example.I think the best comparison with NLP is rather depth of a model, and indeed in this case NLP is much shallower usually. However concerning the argument of 50 passes instead of 1, I'd say it's just a matter of making your test more strict. If you expect that after 50 forward passes the result is EXACTLY the same as explained #902 then you should probably make sure that a single pass is EXACTLY the same. Concerning the tradeoff with speed, I guess it's fine to update the test if you're willing to accept the breaking change that the increase speed implies. In fact it's usually the thing you would do manually:
Is the issue that in the case where one of your test fails, it's hard to figure out where the bug is?
Concerning the issue where you're seeing issues with FP16. Maybe the issue is you're trying to ensure the model returns the same output regardless of setups (frameworks, hardware etc ...) I don't see how that's easily doable (how can you guarantee that
LayerNormimplementations are the same across frameworks). My personal opinion is that you should rely on the APIs you're trying to compute the same thing?msaroufim commentedon Oct 22, 2022
My 2c on this, ideally you can have some fast unit tests that will cover some baseline correctness issues but for the vast majority of issues you're highlighting you need time-consuming runs so there's 2 things that help dramatically in my experience.
Finally, a lot of these problems become easier to handle if the models are faster either by changing the models or by using training compilers so focus on speed and test time will be crucial to make this process sane.
patrickvonplaten commentedon Oct 25, 2022
Thanks for the great feedback here!
keturn commentedon Nov 14, 2022
And testing is logistically difficult for another reason that I'm rediscovering with this PR for InvokeAI: we have to do secret token management in order for tests to be able to run with the only known useful diffusion model.
and secrets management + public PRs really don't mix well on GitHub.
I suspect you've worked around that problem here by self-hosting your test runners, but are there any options for other projects that would like to avoid that burden?
Are there any releases of text-to-image models under a creative commons or open source license, so we can pass them around without worrying about redistribution clauses?
They wouldn't need to have the breadth of subjects of the big Stable Diffusion models, or as refined. Just something that works well enough that we can tell if schedulers and guidance functions and whatnot are behaving.
keturn commentedon Nov 14, 2022
Ah, crowsonkb pointed out that earlier latent diffusion models were released under MIT or Apache licenses, e.g. https://huggingface.co/CompVis/ldm-text2im-large-256
That might be close enough for some purposes. Though I see LDMTextToImagePipeline uses an entirely different text encoder, which our app is sensitive to, so it'd still really help to have a release compatible with the StableDiffusionPipeline.
patrickvonplaten commentedon Nov 15, 2022
That's a very good question - we also ran into this problem with @anton-l 😅
In short, for public PRs we don't test any models that require the auth_token verification. The reason here is that
Public PRs cannot have access to the GitHub secret token of our github repo which means that the PRs fail (please correct me if I'm wrong @anton-l)
when we merge to "main", we always have access to our secret GitHub token and then can run the tests on the models.
patrickvonplaten commentedon Nov 15, 2022
Honestly what I would do in your case is to clone the weights of stable diffusion under your repository on the Hub hf.co/invokeAI (you can do this easily with https://huggingface.co/spaces/osanseviero/repo_duplicator) and then not add a request access requirement there. Note that the CreativeML license doesn't force you to add a request access and 90% of stable diffusion models on the Hub don't have a request access feature: https://huggingface.co/models?library=diffusers
If the repo has no access feature, no need to pass a token :-)
keturn commentedon Nov 15, 2022
Well, this is what it says about redistribution:
I Am Not A Lawyer, but I'm not sure how I can claim I include an "enforceable provision" on anything I make available by a publicly-advertised unauthenticated URL.
But hey, if 🤗 is saying that's fine, and it's
huggingface.codoing the redistributing, then I'd be happy for you to take that liability!Are you willing to do that repository clone so I don't have to put my name on it? 😇
CarlosMFerr commentedon Nov 21, 2022
thanks for the comment @keturn !
The license does not require the licensor nor the licensee to set gated access/tokens as a compulsory mechanism to comply with it (that's a complementary tool on top of the license to ensure users will read it and/or accept it). The license works as an open source or CC license, by using/distributing the model you accept the license and the license applies to you.
github-actions commentedon Jan 13, 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.
print to stdout for test visibility (huggingface#937)