Skip to content
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 silicon mps support #47

Merged
merged 0 commits into from
Aug 23, 2022
Merged

Apple silicon mps support #47

merged 0 commits into from
Aug 23, 2022

Conversation

magnusviri
Copy link

I don't have access to the models so I haven't tested this. But I have tested the torch.device code and it does work.

To get this to work, the only thing that needs to be done differently is that the conda environment needs to use environment-mac.yaml instead of environment.yaml. That is because the cudatoolkit dependency in environment.yaml generates an error. I also believe (but haven't verified) that torch and torchvision need to be updated, so I updated them.

Raymonf added a commit to Raymonf/stable-diffusion that referenced this pull request Aug 21, 2022
@Raymonf
Copy link

Raymonf commented Aug 21, 2022

There's a bit of duplication for the device selection logic since I didn't intend on PRing these changes, I'm not sure if you want to clean that up or not. 😅

Comment on lines 251 to 255
precision_scope = autocast if opt.precision == "autocast" else nullcontext
if device.type == 'mps':
precision_scope = nullcontext # have to use f32 on mps
with torch.no_grad():
with precision_scope("cuda"):
with precision_scope(device.type):
Copy link

@illeatmyhat illeatmyhat Aug 21, 2022

Choose a reason for hiding this comment

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

This is 90% of the way to being able to inference on CPU, however you will get a type mismatch error, expecting a BFloat16 but getting a Float.

To fix this, it should be more like if device.type in ['mps', 'cpu']:
because CPUs don't have BFloat16 operations either.
Alternatively, people can run the model with --precision full when stuff doesn't work.

I am not sure if this is a bug in PyTorch or not, because my impression is that autocast() should not be offering BFloat16 optimizations to anything that is not a recent Nvidia GPU.
Intel apparently has BF16 operations in recent Xeon products. Maybe this is a better way?

Copy link

@whyboris whyboris Sep 1, 2022

Choose a reason for hiding this comment

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

Thank you for sharing the solution 🙇
I was running txt2img.py and it kept failing with:

`RuntimeError: expected scalar type BFloat16 but found Float`

But adding --precision full when running the script worked 🎉

Choose a reason for hiding this comment

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

Thank you for sharing the solution 🙇 I was running txt2img.py and it kept failing with:

`RuntimeError: expected scalar type BFloat16 but found Float`

But adding --precision full when running the script worked 🎉

Wow this perfectly solves my same problem.

Comment on lines 20 to 21
elif(torch.backends.mps.is_available()):
self.device_available = "mps"

Choose a reason for hiding this comment

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

torch.backends.mps is not available in all installations of PyTorch. This needs to be either checked or surrounded with a try-catch to avoid a crash.

corajr pushed a commit to corajr/stable-diffusion that referenced this pull request Aug 22, 2022
- numpy=1.19.2
- pip:
- albumentations==0.4.3
- opencv-python==4.1.2.30
Copy link

@HenkPoley HenkPoley Aug 23, 2022

Choose a reason for hiding this comment

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

FYI, this opencv-python version (from November 2019) is longer offered on PIP

ERROR: Could not find a version that satisfies the requirement opencv-python==4.1.2.30 (from versions: 3.4.0.14, 3.4.10.37, 3.4.11.39, 3.4.11.41, 3.4.11.43, 3.4.11.45, 3.4.13.47, 3.4.15.55, 3.4.16.57, 3.4.16.59, 3.4.17.61, 3.4.17.63, 3.4.18.65, 4.3.0.38, 4.4.0.40, 4.4.0.42, 4.4.0.44, 4.4.0.46, 4.5.1.48, 4.5.3.56, 4.5.4.58, 4.5.4.60, 4.5.5.62, 4.5.5.64, 4.6.0.66)

- pytorch
- defaults
dependencies:
- python=3.8.5

Choose a reason for hiding this comment

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

Probably should be python=3.8.11

- defaults
dependencies:
- python=3.8.5
- pip=20.3
Copy link

@HenkPoley HenkPoley Aug 23, 2022

Choose a reason for hiding this comment

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

Current pip is more like 22.2.2

@magnusviri
Copy link
Author

I closed this on accident (I didn't know that bringing my branch up-to-date would close it). I'm going to try to implement what @illeatmyhat and then resubmit it.

@HenkPoley
Copy link

HenkPoley commented Aug 24, 2022

It says you merged (0, zero) commits into CompVis:main. Hence this one would get closed.

You might have wanted to use pull from main into magnusviri:apple-silicon-mps-support ?

@magnusviri
Copy link
Author

I don't have permission to merge into their repo and I don't see my changes in their repo but I have their changes in mine. The wording always confuses me, and I've never done what I did, so I didn't realize it would close the pull request. IDK. Anyway, they haven't done a merge since they released this 3 days ago. I've been pretty busy playing with this, so I haven't done much coding to improve my code yet. I am kind of waiting to see how the developers interact with the community before I fix the merge request (I've done a lot of work for other projects to have it flat out rejected, so I'm not too eager to work on it unless I know it will be merged). If anything, I might do a pull request for the lstein fork because he has responded.

enzymezoo-code added a commit to enzymezoo-code/stable-diffusion that referenced this pull request Sep 2, 2022
colemickens pushed a commit to colemickens/stable-diffusion that referenced this pull request Sep 15, 2022
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.

6 participants