From 12bc5f76242c62de2152cde74591cb640c933a18 Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Tue, 21 Mar 2023 18:23:50 +0000 Subject: [PATCH 1/3] Remove warmup passes in mps tests. --- tests/models/test_models_vae.py | 7 +------ tests/models/test_models_vq.py | 3 --- tests/pipelines/ddpm/test_ddpm.py | 4 ---- .../test_latent_diffusion_uncond.py | 5 ----- .../test_stable_diffusion_depth.py | 4 ---- tests/test_modeling_common.py | 18 ------------------ tests/test_pipelines_common.py | 16 ---------------- 7 files changed, 1 insertion(+), 56 deletions(-) diff --git a/tests/models/test_models_vae.py b/tests/models/test_models_vae.py index 3eb7ce861592..784e04d2ed30 100644 --- a/tests/models/test_models_vae.py +++ b/tests/models/test_models_vae.py @@ -124,12 +124,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) diff --git a/tests/models/test_models_vq.py b/tests/models/test_models_vq.py index 733b51d2f158..66c33e07371e 100644 --- a/tests/models/test_models_vq.py +++ b/tests/models/test_models_vq.py @@ -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() diff --git a/tests/pipelines/ddpm/test_ddpm.py b/tests/pipelines/ddpm/test_ddpm.py index c2fc4fddc1bd..5e3e47cb74fb 100644 --- a/tests/pipelines/ddpm/test_ddpm.py +++ b/tests/pipelines/ddpm/test_ddpm.py @@ -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 diff --git a/tests/pipelines/latent_diffusion/test_latent_diffusion_uncond.py b/tests/pipelines/latent_diffusion/test_latent_diffusion_uncond.py index c8ee4b1ba5f4..aa7b33730d18 100644 --- a/tests/pipelines/latent_diffusion/test_latent_diffusion_uncond.py +++ b/tests/pipelines/latent_diffusion/test_latent_diffusion_uncond.py @@ -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 diff --git a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py index 12e7113399a8..c2ad239f6888 100644 --- a/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py +++ b/tests/pipelines/stable_diffusion_2/test_stable_diffusion_depth.py @@ -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] diff --git a/tests/test_modeling_common.py b/tests/test_modeling_common.py index 9d891207f9a1..9f92ea48397e 100644 --- a/tests/test_modeling_common.py +++ b/tests/test_modeling_common.py @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/tests/test_pipelines_common.py b/tests/test_pipelines_common.py index 1ab6baeb81a3..7c87aa888ce6 100644 --- a/tests/test_pipelines_common.py +++ b/tests/test_pipelines_common.py @@ -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] @@ -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] @@ -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) @@ -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] From acc9d103c1381e5da477b2f6a743a00d4eac3033 Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Tue, 21 Mar 2023 18:37:20 +0000 Subject: [PATCH 2/3] Update mps docs: no warmup pass in PyTorch 2 --- docs/source/en/optimization/mps.mdx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/source/en/optimization/mps.mdx b/docs/source/en/optimization/mps.mdx index cada8ce8b4ce..3750724bce57 100644 --- a/docs/source/en/optimization/mps.mdx +++ b/docs/source/en/optimization/mps.mdx @@ -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. + + +**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. + + + +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") @@ -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. @@ -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. From 95e47e224882a371ba6e8bac4161f952a4c0d400 Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Tue, 21 Mar 2023 18:51:06 +0000 Subject: [PATCH 3/3] Update imports. --- tests/models/test_models_vae.py | 1 - tests/test_modeling_common.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/models/test_models_vae.py b/tests/models/test_models_vae.py index 784e04d2ed30..abd4a078e692 100644 --- a/tests/models/test_models_vae.py +++ b/tests/models/test_models_vae.py @@ -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 diff --git a/tests/test_modeling_common.py b/tests/test_modeling_common.py index 9f92ea48397e..e880950a7914 100644 --- a/tests/test_modeling_common.py +++ b/tests/test_modeling_common.py @@ -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