-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apple MPS Support #179
Apple MPS Support #179
Conversation
I can confirm txt2img works on my m1, for some reason pip fails installing from requirements, with
Anyway, thanks. |
If merging to main is too disruptive, could you make a branch for apple-mps-support? I can resubmit the pull request to that branch. Someone said this works on Intel using MPS, that's why I dropped "silicon" from the other branch name. It's basically Apple's MPS support, not Apple Silicon support. |
I'll work on dream.py maybe later today, like when I'm done with work in 8 hours or something. Probably later so I can eat lol. |
I do not have a MPS system to test on. I'm reviewing the code now, so could I get confirmation from someone (other than the pull author) that it is working on the appropriate Apple silicon? I will do some timing tests to confirm that there is no performance hit on CUDA systems. |
The PR merges cleanly and does not have a measurable effect on either model loading time or image generation time when running on CUDA Linux. I did five timings and could not detect a statistically significant difference. If there is an effect it is no more than a fraction of a second. |
Testing now ./scripts/orig_scripts/txt2img.py works beautifully, nice! dream.py still needs some work. OP indicated that he's going to work on it. So we probably should either update the README.md to make that just a little more obvious to eliminate confusion or wait until OP gets a chance. |
Sorry, don't recognize the "OP" acronym. Who is this? I would much prefer to wait until the whole package (dream.py, webUI, and the method calls) all work on MPS before rolling this out. There will be jubilation in the streets when SD comes to the M1 chips. Up above, @Birch-san requested that we turn this into a public feature branch, and I'm happy to do so as a marker for what is coming. Any objections? |
Apologies, meaning magnusviri
sounds like a plan |
New public branch magnusviri-apple-mps-support is now available for people to work on. |
Just a data point: starting with commit 8b9f2c7, was able to compile and run txt2img on a MB Pro (M1 chip, 16 GB) running 12.5.1. From a fairly stock configuration with XCode tools installed, had to install Rust and Anaconda, update pip, tweak the PYTORCH_ENABLE_MPS_FALLBACK env variable, and symlink openai/clip-vit-large-patch14. With default settings txt2img.py completed 2 iterations in 43:33. |
I was actually hoping for a feature branch. However, this has now exceeded my GitHub experience. I've never worked on a group project like this before. I'm actually not sure how to proceed at all now. Now that the lstein branch is the upstream branch I want to work on getting things like dream.py working, but actually first I want to get other Mac users to switch from the main repo to this repo so that hopefully all Mac users are working on the same thing. |
Regarding |
Right, found the model. Dream.py now throws the following when run:
|
As in 43 minutes? On my m1 max txt2image with
for me I had to use the sd-v1-4.ckpt directly, and just directly copy it to |
Can some kind people one check if the seed issue is still occurring, that not using fixed_code but using a seed [EDIT: Actually I see this repo doesn't have the fix for seed in, just the fix for fixed_code, Not sure if this is a MPS or general thing, but only MPS users have complained so far. There's a work around to move the seed_everything after the model load, I'm jut a DBA with no pytorch skills so I've no idea why the model load would affect the random number generator or why the affect would be different for different users. I assume hoping for randn to come up with the same order of random values on different architectures would be a bit of a pipe dream ? |
I get an error with the tokenizer when trying to run
|
See this error on M1 16GB, when running a prompt under
This error has nothing to do with the 'k_heun' sampler. |
@lstein I believe the new module (ldm.dream.devices) seems to be missing from the repo presently |
With the addition of my suggested fix I was able to reproduce this example from magnusviri's original fork: python scripts/orig_scripts/txt2img.py \
--prompt "Anubis riding a motorbike in Grand Theft Auto cover, palm trees, cover art by Stephen Bliss, artstation, high quality" \
--ddim_steps=50 \
--n_samples=1 \
--n_rows=1 \
--n_iter=1 \
--seed 1805504473 \
--fixed_code |
@lstein I don't believe the |
It looks like that file ( import torch
def choose_torch_device():
if torch.cuda.is_available():
return 'cuda'
elif torch.backends.mps.is_available():
return 'mps'
return 'cpu' |
My bad. Forgot to push. |
Make --fixed-code work again Co-authored-by: Beau Gunderson <[email protected]>
I'm having issues with this branch working (where magnusviri's fork was working).
Maybe it just didn't get added with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've checked that dream.py works with both txt2img and img2img functionality, and that images produced by previous versions are the same now when using the original seed. Not surprisingly, images produced on macs are different, even when same seed is used.
@Birch-san I tried replacing my fork of k-diffusion with yours, and unfortunately the result was that dream.py crashed with:
What version of typing is required? |
I did a squash merge from the magnusviri-apple-mps-support branch. Everything went in cleanly. I will leave the branch up so that the commit chain can be followed. This was a bear! Thank you everyone for the thorough conversations. Let me know if any assistance is required. |
@hemmer Thanks for the suggestion, but I can't install the osx-arm64 version, since it requires python <3.10.0a0.
|
@sclausen I forgot to mention that I also downgraded to python 3.9 without issue ( in the yml) |
@hemmer I didn't tried that 🤦♂️ Thank you very much! What I ask myself: Why does it seem that I'm the only one (or one of few) that had this issue? Shouldn't this be a problem for every M1 user? |
btw there is another fork, running with GPU on m1, making it run a lot faster for me: Actually maybe it's the export PYTORCH_ENABLE_MPS_FALLBACK=1 that I had that made it run slower? EDIT2: Yep it's the PYTORCH_ENABLE_MPS_FALLBACK=1 - if you had to turn it on, turn it of and run: |
You guys moved so fast my head is still spinning. I'm trying to figure out everything that's just happened. I just replied here that we need to make one change regarding the mps support. I had made this on the repo on my hard drive but I haven't had time to push it. I'm not sure what the state of the code is anymore. I've never worked on a project where multiple people contribute, so I'm having a hard time understanding what all of the GitHub messages mean. I can't even keep up with all the messages! |
I believe everyone is using txt2img, and img2img, as it's the only currently supported on m1, as it's written in docs in this PR |
This works! I've only tested txt2img.py so far. One problem, it's really slow at 18.24s/it. Before I was getting somewhere between 4-8s/it (it kept changing as we made changes). Even with PYTORCH_ENABLE_MPS_FALLBACK=1 before it wasn't this slow, it was closer to 8-9s/it. I will test more tomorrow. |
@magnusviri I'm running a clean installation of the main branch from this repo using a MacBook Pro M1 Max with 24 GPU cores and getting an average of 2.23 s/it |
I was under 1.5 s/it with same M1 Max on the forked branch. Let me try fresh main and report back. Edit: |
Ugh! I forgot to set n_samples to 1! Yeah, on my M1 MacBook Air I'm now getting 4.39s/it. This might even be faster than before. False alarm! |
@cgodley Thanks for your work. It works for me, however returns blank as you mentioned. Did anyone figure out how to make deam.py functional in m.ps? Seems like the conversations are about both original and dream.py. |
@junukwon7 Yes
I think the reason for k-diffusion producing black squares is this issue: Edit: I was using |
@cgodley yes, I have a workaround to fix the k-diffusion samplers on MPS in my k-diffusion branch. I recommended it as a change on this review a few days ago but it wasn't incorporated. |
@Birch-san I tried your k-diffusion fork but I'm getting very blurry images like this:
Same thing using DDIM: I'm using Edit: I'm using MacBook Pro M1 14" 16GB, macOS 12.5 |
@cgodley thanks for trying it out… hm, I haven't tried it in the context of the lstein fork, but certainly when using the I'm using the same commit |
These are all of the changes from my repo that gets this working on Apple Mac's with MPS (Apple Silicon and Intel). I am aware of at least one problem with the scripts that are new in lstein, but I'm hoping I can get those fixed in time. The main scripts old original txt2img and img2img work.