InvokeAI icon indicating copy to clipboard operation
InvokeAI copied to clipboard

Stable Diffusion PR optimizes VRAM, generate 576x1280 images with 6 GB VRAM

Open mgcrea opened this issue 4 years ago • 180 comments

Seen on HN, might be interesting to pull in this repo? (PR looks a bit dirty with a lot of extra changes though).

https://github.com/basujindal/stable-diffusion/pull/103

mgcrea avatar Sep 04 '22 09:09 mgcrea

check out my other compvis PR https://github.com/CompVis/stable-diffusion/pull/177 might be more suitable for you

neonsecret avatar Sep 04 '22 10:09 neonsecret

Thanks for the tip! I'll check them both out.

lstein avatar Sep 04 '22 10:09 lstein

Would be cool to get this implemented! ❤️ I got two users with only 4 GB VRAM and the model won't even load. If I saw it correctly, that should work with basujindal's version.

sunija-dev avatar Sep 04 '22 11:09 sunija-dev

Don't I know it!

lstein avatar Sep 04 '22 11:09 lstein

By the looks of it without all the white space changes we get...

diff ldm/modules/attention.py ldm/modules/attention.py.opt
181a182
>         del context, x
187a189
>         del q, k
193a196
>             del mask
196c199,200
<         attn = sim.softmax(dim=-1)
---
>         sim[4:] = sim[4:].softmax(dim=-1)
>         sim[:4] = sim[:4].softmax(dim=-1)
198,200c202,204
<         out = einsum('b i j, b j d -> b i d', attn, v)
<         out = rearrange(out, '(b h) n d -> b n (h d)', h=h)
<         return self.to_out(out)
---
>         sim = einsum('b i j, b j d -> b i d', sim, v)
>         sim = rearrange(sim, '(b h) n d -> b n (h d)', h=h)
>         return self.to_out(sim)

attached in in diff -u format for patching

attn.patch.txt

Vargol avatar Sep 04 '22 11:09 Vargol

Oh thank you very much for that! I actually just did the same thing with @neonsecret 's attention optimization and it works amazingly. Without any change to execution speed my test prompt now uses 3.60G of VRAM. Previously it was using 4.42G.

Now I'm looking to see if image quality is affected.

Any reason to prefer basunjal's attention.py optimization?

lstein avatar Sep 04 '22 11:09 lstein

nothing to add to this conversation, except to say that i'm excited for this lol ;)

smoke2007 avatar Sep 04 '22 11:09 smoke2007

I've merged @neonsecret 's optimizations into the development branch "refactoring-simplet2i" and would welcome people testing it and sending feedback. This branch probably still has major bugs in it, but I refactored the code to make it much easier to add optimizations and new features (particularly inpainting, which I'd hoped to have done by today).

lstein avatar Sep 04 '22 11:09 lstein

Hmmm....on my barely coping 8G M1 its not so hot, the image is different and it took twice as long, but its an old clone, let me try in on a fresher one

Vargol avatar Sep 04 '22 11:09 Vargol

Darn. I'd hoped that there was such a thing as a free lunch.

I'm on an atypical system with 32G of VRAM, so maybe my results aren't representative. I did timing and peak VRAM usage, and then looked at two images generated with the same seed and they were indistinguishable to the eye. Let me know what you find out.

Are you on an Apple? I didn't know there were clones. The M1 MPS support in this fork is really new, and I wouldn't be surprised it needs additional tweaking to get it to work properly with the optimization.

lstein avatar Sep 04 '22 11:09 lstein

Sorry meant its an old local clone of your repo as I didn't want to make changes in my local clone of the current one as hot works quite nicely :-) , but yes I'm not surprised mps is breaking things and pytorch is pretty buggy too, I raised a few mps related issues over there that stable diffusion hits.

Vargol avatar Sep 04 '22 12:09 Vargol

hey guys see https://www.reddit.com/r/StableDiffusion/comments/x56e8x/the_optimized_stable_diffusion_repo_got_a_pr_that/in032db

neonsecret avatar Sep 04 '22 12:09 neonsecret

okay, on the main branch the images are the same, but it is really slow, even compared to my normal times...

10/10 [06:49<00:00, 40.94s/it] compared 10/10 [02:14<00:00, 13.44s/it] (spot the man with the 8Gb M1)

I'll do some more digging

@magnusviri any chance you can check this out on a bigger M1 ?

Vargol avatar Sep 04 '22 12:09 Vargol

Ok, this is @neonsecret 's PR, which I just tested and merged into the refactor branch. I'm seeing a 20% reduction in memory footprint, but unfortunately not the 35% reduction reported in the Reddit post. Presumably this is due to the earlier optimizations in basunjal's branch. I haven't really wanted to use those opts because the code is complex and I hear it has a performance hit. Advice?

lstein avatar Sep 04 '22 12:09 lstein

Last I looks at basunjal's there where loads of assumption about using cuda, a big chunk of the memory saving seemed to come from forcing half. Was a week ago things might have changed

Vargol avatar Sep 04 '22 12:09 Vargol

I've got half precision on already as the default. I think what I'm missing is basunjal's optimization of splitting the task into several chunks and loading them into the GPU sequentially.

lstein avatar Sep 04 '22 12:09 lstein

Frankly, I'm happy with the 20% savings for now.

lstein avatar Sep 04 '22 12:09 lstein

Seems the speed loss is coming from the twin calls to softmax.

<         attn = sim.softmax(dim=-1)
---
>         sim[4:] = sim[4:].softmax(dim=-1)
>         sim[:4] = sim[:4].softmax(dim=-1)

If I change it to use sim = sim.softmax(dim=-1)

instead I get all my speed back (I Assume more memory usage though I need better diagnostic stuff than Activity Monitor).

I can do 640x512 images now so there do appear to be some memory saving even reverting that change, be interesting to see what happens on a larger box and if is it worth wrapping them into one variation in a "if mps" statement

EDIT:

seems I can also now do 384 x320 without it using swap.

Vargol avatar Sep 04 '22 12:09 Vargol

No measurable slowdown at all on CUDA. Maybe we make the twin softmax conditional on not MPS?

lstein avatar Sep 04 '22 13:09 lstein

test run:

751283a2de81bee4bb571fbabe4adb19f1d85b97 (main) EDIT: corrected hash "test" -s50 -W512 -H512 -C7.5 -Ak_lms -S42 00:15<00:00, 3.30it/s Max VRAM used for this generation: 4.44G

89a762203449a1efc1d34632fe76bf942669031d (refactoring-simplet2i) "test" -s50 -W512 -H512 -C7.5 -Ak_lms -S42 00:13<00:00, 3.77it/s Max VRAM used for this generation: 3.61G

XOR of both images gave a pitch black result -> seems to be no difference

maybe this helps. Inference even seems to be a little faster in this test run.

veprogames avatar Sep 04 '22 14:09 veprogames