Skip to content

Commit 92e1164

Browse files
authored
mps: remove warmup passes (#2771)
* Remove warmup passes in mps tests. * Update mps docs: no warmup pass in PyTorch 2 * Update imports.
1 parent ca1a222 commit 92e1164

File tree

8 files changed

+11
-63
lines changed

8 files changed

+11
-63
lines changed

docs/source/en/optimization/mps.mdx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ specific language governing permissions and limitations under the License.
1919
- Mac computer with Apple silicon (M1/M2) hardware.
2020
- macOS 12.6 or later (13.0 or later recommended).
2121
- arm64 version of Python.
22-
- PyTorch 1.13. You can install it with `pip` or `conda` using the instructions in https://pytorch.org/get-started/locally/.
22+
- 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/.
2323

2424

2525
## Inference Pipeline
2626

2727
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.
2828

29-
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.
29+
<Tip warning={true}>
30+
31+
**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.
32+
33+
</Tip>
34+
35+
We strongly recommend you use PyTorch 2 or better, as it solves a number of problems like the one described in the previous tip.
3036

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

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

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

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

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

6065
## Known Issues
6166

62-
- As mentioned above, we are investigating a strange [first-time inference issue](https://github.com/huggingface/diffusers/issues/372).
6367
- 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.

tests/models/test_models_vae.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from parameterized import parameterized
2121

2222
from diffusers import AutoencoderKL
23-
from diffusers.models import ModelMixin
2423
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
2524

2625
from ..test_modeling_common import ModelTesterMixin
@@ -124,12 +123,7 @@ def test_output_pretrained(self):
124123
model = model.to(torch_device)
125124
model.eval()
126125

127-
# One-time warmup pass (see #372)
128-
if torch_device == "mps" and isinstance(model, ModelMixin):
129-
image = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size)
130-
image = image.to(torch_device)
131-
with torch.no_grad():
132-
_ = model(image, sample_posterior=True).sample
126+
if torch_device == "mps":
133127
generator = torch.manual_seed(0)
134128
else:
135129
generator = torch.Generator(device=torch_device).manual_seed(0)

tests/models/test_models_vq.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ def test_output_pretrained(self):
8585
image = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size)
8686
image = image.to(torch_device)
8787
with torch.no_grad():
88-
# Warmup pass when using mps (see #372)
89-
if torch_device == "mps":
90-
_ = model(image)
9188
output = model(image).sample
9289

9390
output_slice = output[0, -1, -3:, -3:].flatten().cpu()

tests/pipelines/ddpm/test_ddpm.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ def test_inference_predict_sample(self):
7474
ddpm.to(torch_device)
7575
ddpm.set_progress_bar_config(disable=None)
7676

77-
# Warmup pass when using mps (see #372)
78-
if torch_device == "mps":
79-
_ = ddpm(num_inference_steps=1)
80-
8177
generator = torch.manual_seed(0)
8278
image = ddpm(generator=generator, num_inference_steps=2, output_type="numpy").images
8379

tests/pipelines/latent_diffusion/test_latent_diffusion_uncond.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ def test_inference_uncond(self):
7979
ldm.to(torch_device)
8080
ldm.set_progress_bar_config(disable=None)
8181

82-
# Warmup pass when using mps (see #372)
83-
if torch_device == "mps":
84-
generator = torch.manual_seed(0)
85-
_ = ldm(generator=generator, num_inference_steps=1, output_type="numpy").images
86-
8782
generator = torch.manual_seed(0)
8883
image = ldm(generator=generator, num_inference_steps=2, output_type="numpy").images
8984

tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,6 @@ def test_dict_tuple_outputs_equivalent(self):
265265
pipe.to(torch_device)
266266
pipe.set_progress_bar_config(disable=None)
267267

268-
# Warmup pass when using mps (see #372)
269-
if torch_device == "mps":
270-
_ = pipe(**self.get_dummy_inputs(torch_device))
271-
272268
output = pipe(**self.get_dummy_inputs(torch_device))[0]
273269
output_tuple = pipe(**self.get_dummy_inputs(torch_device), return_dict=False)[0]
274270

tests/test_modeling_common.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import torch
2525
from requests.exceptions import HTTPError
2626

27-
from diffusers.models import ModelMixin, UNet2DConditionModel
27+
from diffusers.models import UNet2DConditionModel
2828
from diffusers.models.attention_processor import AttnProcessor
2929
from diffusers.training_utils import EMAModel
3030
from diffusers.utils import torch_device
@@ -119,11 +119,6 @@ def test_from_save_pretrained(self):
119119
new_model.to(torch_device)
120120

121121
with torch.no_grad():
122-
# Warmup pass when using mps (see #372)
123-
if torch_device == "mps" and isinstance(model, ModelMixin):
124-
_ = model(**self.dummy_input)
125-
_ = new_model(**self.dummy_input)
126-
127122
image = model(**inputs_dict)
128123
if isinstance(image, dict):
129124
image = image.sample
@@ -161,11 +156,6 @@ def test_from_save_pretrained_variant(self):
161156
new_model.to(torch_device)
162157

163158
with torch.no_grad():
164-
# Warmup pass when using mps (see #372)
165-
if torch_device == "mps" and isinstance(model, ModelMixin):
166-
_ = model(**self.dummy_input)
167-
_ = new_model(**self.dummy_input)
168-
169159
image = model(**inputs_dict)
170160
if isinstance(image, dict):
171161
image = image.sample
@@ -203,10 +193,6 @@ def test_determinism(self):
203193
model.eval()
204194

205195
with torch.no_grad():
206-
# Warmup pass when using mps (see #372)
207-
if torch_device == "mps" and isinstance(model, ModelMixin):
208-
model(**self.dummy_input)
209-
210196
first = model(**inputs_dict)
211197
if isinstance(first, dict):
212198
first = first.sample
@@ -377,10 +363,6 @@ def recursive_check(tuple_object, dict_object):
377363
model.eval()
378364

379365
with torch.no_grad():
380-
# Warmup pass when using mps (see #372)
381-
if torch_device == "mps" and isinstance(model, ModelMixin):
382-
model(**self.dummy_input)
383-
384366
outputs_dict = model(**inputs_dict)
385367
outputs_tuple = model(**inputs_dict, return_dict=False)
386368

tests/test_pipelines_common.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ def test_save_load_local(self):
121121
pipe.to(torch_device)
122122
pipe.set_progress_bar_config(disable=None)
123123

124-
# Warmup pass when using mps (see #372)
125-
if torch_device == "mps":
126-
_ = pipe(**self.get_dummy_inputs(torch_device))
127-
128124
inputs = self.get_dummy_inputs(torch_device)
129125
output = pipe(**inputs)[0]
130126

@@ -327,10 +323,6 @@ def test_dict_tuple_outputs_equivalent(self):
327323
pipe.to(torch_device)
328324
pipe.set_progress_bar_config(disable=None)
329325

330-
# Warmup pass when using mps (see #372)
331-
if torch_device == "mps":
332-
_ = pipe(**self.get_dummy_inputs(torch_device))
333-
334326
output = pipe(**self.get_dummy_inputs(torch_device))[0]
335327
output_tuple = pipe(**self.get_dummy_inputs(torch_device), return_dict=False)[0]
336328

@@ -402,10 +394,6 @@ def test_save_load_optional_components(self):
402394
pipe.to(torch_device)
403395
pipe.set_progress_bar_config(disable=None)
404396

405-
# Warmup pass when using mps (see #372)
406-
if torch_device == "mps":
407-
_ = pipe(**self.get_dummy_inputs(torch_device))
408-
409397
# set all optional components to None
410398
for optional_component in pipe._optional_components:
411399
setattr(pipe, optional_component, None)
@@ -477,10 +465,6 @@ def _test_attention_slicing_forward_pass(
477465
pipe.to(torch_device)
478466
pipe.set_progress_bar_config(disable=None)
479467

480-
# Warmup pass when using mps (see #372)
481-
if torch_device == "mps":
482-
_ = pipe(**self.get_dummy_inputs(torch_device))
483-
484468
inputs = self.get_dummy_inputs(torch_device)
485469
output_without_slicing = pipe(**inputs)[0]
486470

0 commit comments

Comments
 (0)