Skip to content

mps: remove warmup passes #2771

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

Merged
merged 3 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions docs/source/en/optimization/mps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ specific language governing permissions and limitations under the License.
- Mac computer with Apple silicon (M1/M2) hardware.
- macOS 12.6 or later (13.0 or later recommended).
- arm64 version of Python.
- PyTorch 1.13. You can install it with `pip` or `conda` using the instructions in https://pytorch.org/get-started/locally/.
- PyTorch 2.0 (recommended) or 1.13 (minimum version supported for `mps`). You can install it with `pip` or `conda` using the instructions in https://pytorch.org/get-started/locally/.


## Inference Pipeline

The snippet below demonstrates how to use the `mps` backend using the familiar `to()` interface to move the Stable Diffusion pipeline to your M1 or M2 device.

We recommend to "prime" the pipeline using an additional one-time pass through it. This is a temporary workaround for a weird issue we have detected: the first inference pass produces slightly different results than subsequent ones. You only need to do this pass once, and it's ok to use just one inference step and discard the result.
<Tip warning={true}>

**If you are using PyTorch 1.13** you need to "prime" the pipeline using an additional one-time pass through it. This is a temporary workaround for a weird issue we detected: the first inference pass produces slightly different results than subsequent ones. You only need to do this pass once, and it's ok to use just one inference step and discard the result.

</Tip>

We strongly recommend you use PyTorch 2 or better, as it solves a number of problems like the one described in the previous tip.

```python
# make sure you're logged in with `huggingface-cli login`
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
Expand All @@ -40,7 +45,7 @@ pipe.enable_attention_slicing()

prompt = "a photo of an astronaut riding a horse on mars"

# First-time "warmup" pass (see explanation above)
# First-time "warmup" pass if PyTorch version is 1.13 (see explanation above)
_ = pipe(prompt, num_inference_steps=1)

# Results match those from the CPU device after the warmup pass.
Expand All @@ -59,5 +64,4 @@ pipeline.enable_attention_slicing()

## Known Issues

- As mentioned above, we are investigating a strange [first-time inference issue](https://github.com/huggingface/diffusers/issues/372).
- Generating multiple prompts in a batch [crashes or doesn't work reliably](https://github.com/huggingface/diffusers/issues/363). We believe this is related to the [`mps` backend in PyTorch](https://github.com/pytorch/pytorch/issues/84039). This is being resolved, but for now we recommend to iterate instead of batching.
8 changes: 1 addition & 7 deletions tests/models/test_models_vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from parameterized import parameterized

from diffusers import AutoencoderKL
from diffusers.models import ModelMixin
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device

from ..test_modeling_common import ModelTesterMixin
Expand Down Expand Up @@ -124,12 +123,7 @@ def test_output_pretrained(self):
model = model.to(torch_device)
model.eval()

# One-time warmup pass (see #372)
if torch_device == "mps" and isinstance(model, ModelMixin):
image = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size)
image = image.to(torch_device)
with torch.no_grad():
_ = model(image, sample_posterior=True).sample
if torch_device == "mps":
generator = torch.manual_seed(0)
else:
generator = torch.Generator(device=torch_device).manual_seed(0)
Expand Down
3 changes: 0 additions & 3 deletions tests/models/test_models_vq.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ def test_output_pretrained(self):
image = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size)
image = image.to(torch_device)
with torch.no_grad():
# Warmup pass when using mps (see #372)
if torch_device == "mps":
_ = model(image)
output = model(image).sample

output_slice = output[0, -1, -3:, -3:].flatten().cpu()
Expand Down
4 changes: 0 additions & 4 deletions tests/pipelines/ddpm/test_ddpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ def test_inference_predict_sample(self):
ddpm.to(torch_device)
ddpm.set_progress_bar_config(disable=None)

# Warmup pass when using mps (see #372)
if torch_device == "mps":
_ = ddpm(num_inference_steps=1)

generator = torch.manual_seed(0)
image = ddpm(generator=generator, num_inference_steps=2, output_type="numpy").images

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ def test_inference_uncond(self):
ldm.to(torch_device)
ldm.set_progress_bar_config(disable=None)

# Warmup pass when using mps (see #372)
if torch_device == "mps":
generator = torch.manual_seed(0)
_ = ldm(generator=generator, num_inference_steps=1, output_type="numpy").images

generator = torch.manual_seed(0)
image = ldm(generator=generator, num_inference_steps=2, output_type="numpy").images

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,6 @@ def test_dict_tuple_outputs_equivalent(self):
pipe.to(torch_device)
pipe.set_progress_bar_config(disable=None)

# Warmup pass when using mps (see #372)
if torch_device == "mps":
_ = pipe(**self.get_dummy_inputs(torch_device))

output = pipe(**self.get_dummy_inputs(torch_device))[0]
output_tuple = pipe(**self.get_dummy_inputs(torch_device), return_dict=False)[0]

Expand Down
20 changes: 1 addition & 19 deletions tests/test_modeling_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import torch
from requests.exceptions import HTTPError

from diffusers.models import ModelMixin, UNet2DConditionModel
from diffusers.models import UNet2DConditionModel
from diffusers.models.attention_processor import AttnProcessor
from diffusers.training_utils import EMAModel
from diffusers.utils import torch_device
Expand Down Expand Up @@ -119,11 +119,6 @@ def test_from_save_pretrained(self):
new_model.to(torch_device)

with torch.no_grad():
# Warmup pass when using mps (see #372)
if torch_device == "mps" and isinstance(model, ModelMixin):
_ = model(**self.dummy_input)
_ = new_model(**self.dummy_input)

image = model(**inputs_dict)
if isinstance(image, dict):
image = image.sample
Expand Down Expand Up @@ -161,11 +156,6 @@ def test_from_save_pretrained_variant(self):
new_model.to(torch_device)

with torch.no_grad():
# Warmup pass when using mps (see #372)
if torch_device == "mps" and isinstance(model, ModelMixin):
_ = model(**self.dummy_input)
_ = new_model(**self.dummy_input)

image = model(**inputs_dict)
if isinstance(image, dict):
image = image.sample
Expand Down Expand Up @@ -203,10 +193,6 @@ def test_determinism(self):
model.eval()

with torch.no_grad():
# Warmup pass when using mps (see #372)
if torch_device == "mps" and isinstance(model, ModelMixin):
model(**self.dummy_input)

first = model(**inputs_dict)
if isinstance(first, dict):
first = first.sample
Expand Down Expand Up @@ -377,10 +363,6 @@ def recursive_check(tuple_object, dict_object):
model.eval()

with torch.no_grad():
# Warmup pass when using mps (see #372)
if torch_device == "mps" and isinstance(model, ModelMixin):
model(**self.dummy_input)

outputs_dict = model(**inputs_dict)
outputs_tuple = model(**inputs_dict, return_dict=False)

Expand Down
16 changes: 0 additions & 16 deletions tests/test_pipelines_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ def test_save_load_local(self):
pipe.to(torch_device)
pipe.set_progress_bar_config(disable=None)

# Warmup pass when using mps (see #372)
if torch_device == "mps":
_ = pipe(**self.get_dummy_inputs(torch_device))

inputs = self.get_dummy_inputs(torch_device)
output = pipe(**inputs)[0]

Expand Down Expand Up @@ -320,10 +316,6 @@ def test_dict_tuple_outputs_equivalent(self):
pipe.to(torch_device)
pipe.set_progress_bar_config(disable=None)

# Warmup pass when using mps (see #372)
if torch_device == "mps":
_ = pipe(**self.get_dummy_inputs(torch_device))

output = pipe(**self.get_dummy_inputs(torch_device))[0]
output_tuple = pipe(**self.get_dummy_inputs(torch_device), return_dict=False)[0]

Expand Down Expand Up @@ -395,10 +387,6 @@ def test_save_load_optional_components(self):
pipe.to(torch_device)
pipe.set_progress_bar_config(disable=None)

# Warmup pass when using mps (see #372)
if torch_device == "mps":
_ = pipe(**self.get_dummy_inputs(torch_device))

# set all optional components to None
for optional_component in pipe._optional_components:
setattr(pipe, optional_component, None)
Expand Down Expand Up @@ -470,10 +458,6 @@ def _test_attention_slicing_forward_pass(
pipe.to(torch_device)
pipe.set_progress_bar_config(disable=None)

# Warmup pass when using mps (see #372)
if torch_device == "mps":
_ = pipe(**self.get_dummy_inputs(torch_device))

inputs = self.get_dummy_inputs(torch_device)
output_without_slicing = pipe(**inputs)[0]

Expand Down