Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: Remove of --no-half cause errors under MacOS with any Torch version, but almost all samplers produce only noise with it and latest nightly builds
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

I've tested different versions of Torch to possibly find one that works with _--no-half_ but no luck.

1.14.0.dev20221025 I'm currently using works fine but throws errors without the _--ho-half_ argument. The latest nightly version 2.1.0.dev20230312 seems to work with this argument and gives a really noticeable performance boost, but almost all samplers break on it.

**My results**

With --no-half:
There are no errors, but all samplers apart from DDIM and PLMS produce only noise as final results, these two gives out normal pictures. Also new UniPC produce something that looks like a bit less then noise, but still really messy.

Without --no-half: 
Errors while using everything except DDIM and PLMS. They also works around 40% faster then with _--no-half_.

Without --no-half and with --disable-nan-check:
Just black images instead of noise.

### Steps to reproduce the problem

I was just changing startup arguments

### What should have happened?

Other samplers should work too, I guess

### Commit where the problem happens

3c922d98

### What platforms do you use to access the UI ?

MacOS

### What browsers do you use to access the UI ?

Mozilla Firefox

### Command Line Arguments

```Shell
--opt-sub-quad-attention --skip-torch-cuda-test --upcast-sampling --use-cpu interrogate --no-half
```


### List of extensions

No

### Console logs

```Shell
Error completing request
Arguments: ('task(9rr6te8wtxyte2o)', 'watermelon', '', [], 20, 16, False, False, 1, 1, 7, -1.0, -1.0, 0, 0, 0, False, 512, 512, False, 0.7, 2, 'Latent', 0, 0, 0, [], 0, False, False, 'positive', 'comma', 0, False, False, '', 1, '', 0, '', 0, '', True, False, False, False, 0) {}
Traceback (most recent call last):
  File "/stable-diffusion-webui/modules/call_queue.py", line 56, in f
    res = list(func(*args, **kwargs))
  File "/stable-diffusion-webui/modules/call_queue.py", line 37, in f
    res = func(*args, **kwargs)
  File "/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
    processed = process_images(p)
  File "/stable-diffusion-webui/modules/processing.py", line 486, in process_images
    res = process_images_inner(p)
  File "/stable-diffusion-webui/modules/processing.py", line 635, in process_images_inner
    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts)
  File "/stable-diffusion-webui/modules/processing.py", line 835, in sample
    samples = self.sampler.sample(self, x, conditioning, unconditional_conditioning, image_conditioning=self.txt2img_image_conditioning(x))
  File "/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 351, in sample
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  File "/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 227, in launch_sampling
    return func()
  File "/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 351, in <lambda>
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  File "/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/stable-diffusion-webui/repositories/k-diffusion/k_diffusion/sampling.py", line 553, in sample_dpmpp_sde
    denoised = model(x, sigmas[i] * s_in, **extra_args)
  File "/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 145, in forward
    devices.test_for_nans(x_out, "unet")
  File "/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
    raise NansException(message)

modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.
```


### Additional information

Intel Mac with RX 6600XT, MacOS 13.2.1


Discussion:
I'm getting similar on my 32GB M1 Max - MacOS 13.2. 
Torch v1.12.1
Commit a9fed7c

Default Command Line Arguments:  --upcast-sampling --no-half-vae --use-cpu interrogate

Problem is with v2-1_768 (2.0, 1.5 & 1.4 work fine). With SD2.1 it errors at 0% with all sampling methods (except DDIM, PLMS & UniPC):

```
Error completing request
Arguments: ('task(ou61msdjo5m7nj1)', 'photo of a man', '', [], 10, 15, False, False, 1, 1, 7, -1.0, -1.0, 0, 0, 0, False, 768, 768, False, 0.7, 2, 'Latent', 0, 0, 0, [], 0, False, False, 'positive', 'comma', 0, False, False, '', 1, '', 0, '', 0, '', True, False, False, False, 0) {}
Traceback (most recent call last):
  File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 56, in f
    res = list(func(*args, **kwargs))
  File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 37, in f
    res = func(*args, **kwargs)
  File "/Users/js/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
    processed = process_images(p)
  File "/Users/js/stable-diffusion-webui/modules/processing.py", line 486, in process_images
    res = process_images_inner(p)
  File "/Users/js/stable-diffusion-webui/modules/processing.py", line 636, in process_images_inner
    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts)
  File "/Users/js/stable-diffusion-webui/modules/processing.py", line 836, in sample
    samples = self.sampler.sample(self, x, conditioning, unconditional_conditioning, image_conditioning=self.txt2img_image_conditioning(x))
  File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 351, in sample
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 227, in launch_sampling
    return func()
  File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 351, in <lambda>
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  File "/Users/js/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/Users/js/stable-diffusion-webui/repositories/k-diffusion/k_diffusion/sampling.py", line 594, in sample_dpmpp_2m
    denoised = model(x, sigmas[i] * s_in, **extra_args)
  File "/Users/js/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 145, in forward
    devices.test_for_nans(x_out, "unet")
  File "/Users/js/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
    raise NansException(message)
modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.
```

With DDIM, PLMS & UniPC it gets to 100% with black square and no saved image:

```
Error completing request:41,  1.36s/it]
Arguments: ('task(ao35ifugi48be7m)', 'photo of a man', '', [], 10, 19, False, False, 1, 1, 7, -1.0, -1.0, 0, 0, 0, False, 768, 768, False, 0.7, 2, 'Latent', 0, 0, 0, [], 0, False, False, 'positive', 'comma', 0, False, False, '', 1, '', 0, '', 0, '', True, False, False, False, 0) {}
Traceback (most recent call last):
  File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 56, in f
    res = list(func(*args, **kwargs))
  File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 37, in f
    res = func(*args, **kwargs)
  File "/Users/js/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
    processed = process_images(p)
  File "/Users/js/stable-diffusion-webui/modules/processing.py", line 486, in process_images
    res = process_images_inner(p)
  File "/Users/js/stable-diffusion-webui/modules/processing.py", line 640, in process_images_inner
    devices.test_for_nans(x, "vae")
  File "/Users/js/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
    raise NansException(message)
modules.devices.NansException: A tensor with all NaNs was produced in VAE. Use --disable-nan-check commandline argument to disable this check.
```
Have tried with all extensions off and have deleted /venv directory and get same results.

Have been absorbed in controlnet with SD1.5 for a while so not sure how long has been a problem.

Thanks.


> I'm getting similar on my 32GB M1 Max - MacOS 13.2. Torch v1.12.1 Commit [a9fed7c](https://github.com/AUTOMATIC1111/stable-diffusion-webui/commit/a9fed7c364061ae6efb37f797b6b522cb3cf7aa2)
> 
> Default Command Line Arguments: --upcast-sampling --no-half-vae --use-cpu interrogate
> 
> Problem is with v2-1_768 (2.0, 1.5 & 1.4 work fine). With SD2.1 it errors at 0% with all sampling methods (except DDIM, PLMS & UniPC):
> 
> ```
> Error completing request
> Arguments: ('task(ou61msdjo5m7nj1)', 'photo of a man', '', [], 10, 15, False, False, 1, 1, 7, -1.0, -1.0, 0, 0, 0, False, 768, 768, False, 0.7, 2, 'Latent', 0, 0, 0, [], 0, False, False, 'positive', 'comma', 0, False, False, '', 1, '', 0, '', 0, '', True, False, False, False, 0) {}
> Traceback (most recent call last):
>   File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 56, in f
>     res = list(func(*args, **kwargs))
>   File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 37, in f
>     res = func(*args, **kwargs)
>   File "/Users/js/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
>     processed = process_images(p)
>   File "/Users/js/stable-diffusion-webui/modules/processing.py", line 486, in process_images
>     res = process_images_inner(p)
>   File "/Users/js/stable-diffusion-webui/modules/processing.py", line 636, in process_images_inner
>     samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts)
>   File "/Users/js/stable-diffusion-webui/modules/processing.py", line 836, in sample
>     samples = self.sampler.sample(self, x, conditioning, unconditional_conditioning, image_conditioning=self.txt2img_image_conditioning(x))
>   File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 351, in sample
>     samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
>   File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 227, in launch_sampling
>     return func()
>   File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 351, in <lambda>
>     samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
>   File "/Users/js/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
>     return func(*args, **kwargs)
>   File "/Users/js/stable-diffusion-webui/repositories/k-diffusion/k_diffusion/sampling.py", line 594, in sample_dpmpp_2m
>     denoised = model(x, sigmas[i] * s_in, **extra_args)
>   File "/Users/js/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
>     return forward_call(*input, **kwargs)
>   File "/Users/js/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 145, in forward
>     devices.test_for_nans(x_out, "unet")
>   File "/Users/js/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
>     raise NansException(message)
> modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.
> ```
> 
> With DDIM, PLMS & UniPC it gets to 100% with black square and no saved image:
> 
> ```
> Error completing request:41,  1.36s/it]
> Arguments: ('task(ao35ifugi48be7m)', 'photo of a man', '', [], 10, 19, False, False, 1, 1, 7, -1.0, -1.0, 0, 0, 0, False, 768, 768, False, 0.7, 2, 'Latent', 0, 0, 0, [], 0, False, False, 'positive', 'comma', 0, False, False, '', 1, '', 0, '', 0, '', True, False, False, False, 0) {}
> Traceback (most recent call last):
>   File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 56, in f
>     res = list(func(*args, **kwargs))
>   File "/Users/js/stable-diffusion-webui/modules/call_queue.py", line 37, in f
>     res = func(*args, **kwargs)
>   File "/Users/js/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
>     processed = process_images(p)
>   File "/Users/js/stable-diffusion-webui/modules/processing.py", line 486, in process_images
>     res = process_images_inner(p)
>   File "/Users/js/stable-diffusion-webui/modules/processing.py", line 640, in process_images_inner
>     devices.test_for_nans(x, "vae")
>   File "/Users/js/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
>     raise NansException(message)
> modules.devices.NansException: A tensor with all NaNs was produced in VAE. Use --disable-nan-check commandline argument to disable this check.
> ```
> 
> Have tried with all extensions off and have deleted /venv directory and get same results.
> 
> Have been absorbed in controlnet with SD1.5 for a while so not sure how long has been a problem.
> 
> Thanks.

Totally , the same issue on my device.
I have the same problem after installing the fresh torch 2.0 GA, without `--no-halfs` I get the 
`input types 'tensor<1x77x1xf16>' and 'tensor<1xf32>' are not broadcast compatible`
and with `--no-halfs` most of the samplers just produce some gibberish images with noise.
This is on an non M1 Mac running macOS 12.6.3. 
Before the torch upgrade everything worked quite well aside from some random MPS problems regarding memory watermarks.
> 1.14.0.dev20221025 I'm currently using works fine but throws errors without the _--ho-half_ argument.

~~So all samplers work correctly with that build? Would you be able to test builds from the dates in between that and 2.1.0.dev20230312 to determine the latest build that works correctly? I haven’t been able to reproduce this issue but if I knew the exact date of the last working PyTorch build then I could likely determine more about this issue and make a workaround.~~

Edit: Actually, if you could give me the traceback from the error you get from using 1.14.0.dev20221025 without `--no-half`, that may give me a better idea of what is going wrong.
@brkirch I'm not entirely sure how to use it with 1.14.0 now because it's installed in my Conda environment and the WebUI always tries to create its own venv and install dependencies there. Maybe I can somehow force WebUI to skip creating another venv? Before that I used the old version, which I deleted
So just tested some more torch versions from nightly and the earliest versio (torch-2.0.0.dev20230128 torchvision-0.15.0.dev20230128) showed the same effects for me, as the latest one (torch-2.1.0.dev20230327 torchvision-0.16.0.dev2023032). 
Without --no-halfs I get for the latest nightly: `RuntimeError: "upsample_nearest2d_channels_last" not implemented for 'Half'` and with --no-halfs only noise is generated as image e.g. when using the euler sampler.
@brkirch
Same for me, just pulled master branch and updated dependencies.
webui config is default
2.1_768 model, stable torch 2.0, M1 Max.

For DPM++ 2M/SDE Karras/non-Karras:
```
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
  |    processed = process_images(p)
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/processing.py", line 503, in process_images
  |    res = process_images_inner(p)
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/processing.py", line 653, in process_images_inner
  |    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts)
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/processing.py", line 869, in sample
  |    samples = self.sampler.sample(self, x, conditioning, unconditional_conditioning, image_conditioning=self.txt2img_image_conditioning(x))
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 358, in sample
  |    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 234, in launch_sampling
  |    return func()
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 358, in <lambda>
  |    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  |  File "/Users/username/Documents/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
  |    return func(*args, **kwargs)
  |  File "/Users/username/Documents/stable-diffusion-webui/repositories/k-diffusion/k_diffusion/sampling.py", line 128, in sample_euler
  |    denoised = model(x, sigma_hat * s_in, **extra_args)
  |  File "/Users/username/Documents/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
  |    return forward_call(*args, **kwargs)
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 152, in forward
  |    devices.test_for_nans(x_out, "unet")
  |  File "/Users/username/Documents/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
  |    raise NansException(message)
  |modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.
```

For DDIM:
```
  File "/Users/username/Documents/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
    processed = process_images(p)
  File "/Users/username/Documents/stable-diffusion-webui/modules/processing.py", line 503, in process_images
    res = process_images_inner(p)
  File "/Users/username/Documents/stable-diffusion-webui/modules/processing.py", line 657, in process_images_inner
    devices.test_for_nans(x, "vae")
  File "/Users/username/Documents/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
    raise NansException(message)
modules.devices.NansException: A tensor with all NaNs was produced in VAE. Use --disable-nan-check commandline argument to disable this check.
```
> I have the same problem after installing the fresh torch 2.0 GA, without `--no-halfs` I get the `input types 'tensor<1x77x1xf16>' and 'tensor<1xf32>' are not broadcast compatible` and with `--no-halfs` most of the samplers just produce some gibberish images with noise. This is on an non M1 Mac running macOS 12.6.3. Before the torch upgrade everything worked quite well aside from some random MPS problems regarding memory watermarks.

me too
> So just tested some more torch versions from nightly and the earliest versio (torch-2.0.0.dev20230128 torchvision-0.15.0.dev20230128) showed the same effects for me, as the latest one (torch-2.1.0.dev20230327 torchvision-0.16.0.dev2023032). Without --no-halfs I get for the latest nightly: `RuntimeError: "upsample_nearest2d_channels_last" not implemented for 'Half'` and with --no-halfs only noise is generated as image e.g. when using the euler sampler.

have you solved this  ? i also have the problem
> RuntimeError

I also met the same problem, can you tell me you solved? thanks
Macos M1
> > So just tested some more torch versions from nightly and the earliest versio (torch-2.0.0.dev20230128 torchvision-0.15.0.dev20230128) showed the same effects for me, as the latest one (torch-2.1.0.dev20230327 torchvision-0.16.0.dev2023032). Without --no-halfs I get for the latest nightly: `RuntimeError: "upsample_nearest2d_channels_last" not implemented for 'Half'` and with --no-halfs only noise is generated as image e.g. when using the euler sampler.
> 
> have you solved this ? i also have the problem

me too!! how to solve this?
```
Error completing request
Arguments: ('task(vim8g0n4kh0utdt)', 'create a classic woman with the pearl necklace', '', [], 20, 0, False, False, 1, 1, 7, -1.0, -1.0, 0, 0, 0, False, 512, 512, False, 0.7, 2, 'Latent', 0, 0, 0, [], 0, False, False, 'positive', 'comma', 0, False, False, '', 1, '', 0, '', 0, '', True, False, False, False, 0) {}
Traceback (most recent call last):
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/call_queue.py", line 56, in f
    res = list(func(*args, **kwargs))
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/call_queue.py", line 37, in f
    res = func(*args, **kwargs)
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/txt2img.py", line 56, in txt2img
    processed = process_images(p)
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/processing.py", line 503, in process_images
    res = process_images_inner(p)
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/processing.py", line 653, in process_images_inner
    samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts)
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/processing.py", line 869, in sample
    samples = self.sampler.sample(self, x, conditioning, unconditional_conditioning, image_conditioning=self.txt2img_image_conditioning(x))
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 358, in sample
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 234, in launch_sampling
    return func()
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 358, in <lambda>
    samples = self.launch_sampling(steps, lambda: self.func(self.model_wrap_cfg, x, extra_args={
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/repositories/k-diffusion/k_diffusion/sampling.py", line 145, in sample_euler_ancestral
    denoised = model(x, sigmas[i] * s_in, **extra_args)
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1194, in _call_impl
    return forward_call(*input, **kwargs)
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/sd_samplers_kdiffusion.py", line 152, in forward
    devices.test_for_nans(x_out, "unet")
  File "/Users/hstk/code_ground/source_code/stable-diffusion-webui/modules/devices.py", line 152, in test_for_nans
    raise NansException(message)
modules.devices.NansException: A tensor with all NaNs was produced in Unet. This could be either because there's not enough precision to represent the picture, or because your video card does not support half type. Try setting the "Upcast cross attention layer to float32" option in Settings > Stable Diffusion or using the --no-half commandline argument to fix this. Use --disable-nan-check commandline argument to disable this check.
```

Me too! In my Apple M1 Pro 16G.
Try torch 1.12.1
Someone said that the 1.12.1 is the only version can work on Mac, other version all have issue.
> Try torch 1.12.1 Someone said that the 1.12.1 is the only version can work on Mac, other version all have issue.

Still not work for me.

Resolving patch:
diff --git a/modules/mac_specific.py b/modules/mac_specific.py
--- a/modules/mac_specific.py
+++ b/modules/mac_specific.py
@@ -54,6 +54,11 @@ def cumsum_fix(input, cumsum_func, *args, **kwargs):
         CondFunc('torch.cumsum', cumsum_fix_func, None)
         CondFunc('torch.Tensor.cumsum', cumsum_fix_func, None)
         CondFunc('torch.narrow', lambda orig_func, *args, **kwargs: orig_func(*args, **kwargs).clone(), None)
-    if version.parse(torch.__version__) == version.parse("2.0"):
+
         # MPS workaround for https://github.com/pytorch/pytorch/issues/96113
-        CondFunc('torch.nn.functional.layer_norm', lambda orig_func, x, normalized_shape, weight, bias, eps, **kwargs: orig_func(x.float(), normalized_shape, weight.float() if weight is not None else None, bias.float() if bias is not None else bias, eps).to(x.dtype), lambda *args, **kwargs: len(args) == 6)
+        CondFunc('torch.nn.functional.layer_norm', lambda orig_func, x, normalized_shape, weight, bias, eps, **kwargs: orig_func(x.float(), normalized_shape, weight.float() if weight is not None else None, bias.float() if bias is not None else bias, eps).to(x.dtype), lambda _, input, *args, **kwargs: len(args) == 4 and input.device.type == 'mps')
+
+        # MPS workaround for https://github.com/pytorch/pytorch/issues/92311
+        if platform.processor() == 'i386':
+            for funcName in ['torch.argmax', 'torch.Tensor.argmax']:
+                CondFunc(funcName, lambda _, input, *args, **kwargs: torch.max(input.float() if input.dtype == torch.int64 else input, *args, **kwargs)[1], lambda _, input, *args, **kwargs: input.device.type == 'mps')
\ No newline at end of file
diff --git a/modules/sd_hijack_optimizations.py b/modules/sd_hijack_optimizations.py
--- a/modules/sd_hijack_optimizations.py
+++ b/modules/sd_hijack_optimizations.py
@@ -256,6 +256,9 @@ def sub_quad_attention_forward(self, x, context=None, mask=None):
     k = k.unflatten(-1, (h, -1)).transpose(1,2).flatten(end_dim=1)
     v = v.unflatten(-1, (h, -1)).transpose(1,2).flatten(end_dim=1)
 
+    if q.device.type == 'mps':
+        q, k, v = q.contiguous(), k.contiguous(), v.contiguous()
+
     dtype = q.dtype
     if shared.opts.upcast_attn:
         q, k = q.float(), k.float()


Test patch: