Repository: stable-diffusion-webui
Issue: [Bug]: CUDA error: HIPBLAS_STATUS_INVALID_VALUE when running Hires fix on AMD GPU
Labels: bug-report

### Is there an existing issue for this?

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

### What happened?

When attempting to upscale an image in txt2img using hires fix, using any upscaler model, the generation and upscale will appear to be processing, but will fail at the very end prior to producing the final image, with the error `CUDA error: HIPBLAS_STATUS_INVALID_VALUE` (see below for full stacktrace).

This seems to occur 100% of the time when running with `--medvram`. It occurs sporadically when not using `--medvram`, and produces a different stack trace depending on the setting. Tested with both SD1.5 and SDXL, 512x512 and 1024x1024 upscaling to 2048x2048, these combinations all produce the error.

### Steps to reproduce the problem

1. Go to txt2img
2. Enter any prompt
3. Enable hires fix
4. Generate the image

### What should have happened?

The image should generate completely and produce a final, upscaled output.

### Sysinfo

[sysinfo-2023-09-06-17-00.txt](https://github.com/AUTOMATIC1111/stable-diffusion-webui/files/12540554/sysinfo-2023-09-06-17-00.txt)


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

Mozilla Firefox, Google Chrome

### Console logs

```Shell
~/build/stable-diffusion-webui master* 18m 9s
stable-diffusion-webui ❯ nice ./webui.sh

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################

################################################################
Running on soichiro user
################################################################

################################################################
Repo already cloned, using it as install directory
################################################################

################################################################
python venv already activate or run without venv: /home/soichiro/build/stable-diffusion-webui/venv
################################################################

################################################################
Launching launch.py...
################################################################
Using TCMalloc: libtcmalloc_minimal.so.4
Python 3.10.13 (main, Sep  4 2023, 03:31:29) [GCC 12.3.0]
Version: v1.6.0
Commit hash: 5ef669de080814067961f28357256e8fe27544f4
#######################################################################################################
Initializing Civitai Link
If submitting an issue on github, please provide the below text for debugging purposes:

Python revision: 3.10.13 (main, Sep  4 2023, 03:31:29) [GCC 12.3.0]
Civitai Link revision: 763e8aedfab68e8933c3efbfa568961beeaa3def
SD-WebUI revision: 5ef669de080814067961f28357256e8fe27544f4

Checking Civitai Link requirements...
[+] python-socketio[client] version 5.7.2 installed.

#######################################################################################################
Pulled changes for repository in '/home/soichiro/build/stable-diffusion-webui/extensions/adetailer':
Already up to date.

Pulled changes for repository in '/home/soichiro/build/stable-diffusion-webui/extensions/ultimate-upscale-for-automatic1111':
Already up to date.

Pulled changes for repository in '/home/soichiro/build/stable-diffusion-webui/extensions/sd_civitai_extension':
Already up to date.

Launching Web UI with arguments: --opt-split-attention --enable-insecure-extension-access --update-all-extensions --theme dark --medvram-sdxl
/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/cuda/__init__.py:611: UserWarning: Can't initialize NVML
  warnings.warn("Can't initialize NVML")
no module 'xformers'. Processing without...
no module 'xformers'. Processing without...
No module 'xformers'. Proceeding without it.
[-] ADetailer initialized. version: 23.9.1, num models: 9
Civitai: API loaded
Loading weights [9a0157cad2] from /home/soichiro/build/stable-diffusion-webui/models/Stable-diffusion/SD_XL/counterfeitxl_v10.safetensors
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Civitai: Check resources for missing info filesCivitai: Check resources for missing preview images

Startup time: 15.2s (prepare environment: 7.2s, import torch: 2.9s, import gradio: 0.7s, setup paths: 0.7s, other imports: 0.7s, load scripts: 2.0s, create ui: 0.6s, gradio launch: 0.3s).
QObject::moveToThread: Current thread (0x55e9f0db6710) is not the object's thread (0x55e9f0db6870).
Cannot move to target thread (0x55e9f0db6710)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx.

/usr/bin/xdg-open: line 634: 15233 Aborted                 (core dumped) kde-open${KDE_SESSION_VERSION} "$1"
Creating model from config: /home/soichiro/build/stable-diffusion-webui/repositories/generative-models/configs/inference/sd_xl_base.yaml
Civitai: Found 4 resources missing preview images
Civitai: Found 2 resources missing info files
Civitai: No info found on Civitai
Loading VAE weights specified in settings: /home/soichiro/build/stable-diffusion-webui/models/VAE/sdxl_vae.safetensors
Applying attention optimization: Doggettx... done.
No Image data blocks found.
Model loaded in 8.2s (load weights from disk: 1.3s, create model: 0.4s, apply weights to model: 2.2s, load textual inversion embeddings: 3.2s, calculate empty prompt: 0.8s).
Civitai: No preview images found on Civitai
100%|████████████████████████████████████████████████████████████████████████████████████████| 30/30 [00:13<00:00,  2.27it/s]
==========================================================================================   | 30/50 [00:13<00:07,  2.67it/s]
A tensor with all NaNs was produced in VAE.
Web UI will now convert VAE into 32-bit float and retry.
To disable this behavior, disable the 'Automatically revert VAE to 32-bit floats' setting.
To always start with 32-bit VAE, use --no-half-vae commandline flag.
==========================================================================================
100%|████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:45<00:00,  2.25s/it]
*** Error completing request█████████████████████████████████████████████████████████████████| 50/50 [01:03<00:00,  2.19s/it]
*** Arguments: ('task(za9th5rgf2trtvm)', 'masterpiece, best quality, an anime girl', 'negativeXL_D, negativeXL_C', [], 30, 'DPM++ 2M Karras', 1, 1, 9, 1024, 1024, True, 0.4, 2, '4x-NMKD-YandereNeo', 20, 0, 0, 'Use same checkpoint', 'Use same sampler', '', '', [], <gradio.routes.Request object at 0x7f2dfd177580>, 0, False, '', 0.8, -1, False, -1, 0, 0, 0, False, {'ad_model': 'face_yolov8n.pt', 'ad_prompt': '', 'ad_negative_prompt': '', 'ad_confidence': 0.3, 'ad_mask_k_largest': 0, 'ad_mask_min_ratio': 0, 'ad_mask_max_ratio': 1, 'ad_x_offset': 0, 'ad_y_offset': 0, 'ad_dilate_erode': 4, 'ad_mask_merge_invert': 'None', 'ad_mask_blur': 4, 'ad_denoising_strength': 0.4, 'ad_inpaint_only_masked': True, 'ad_inpaint_only_masked_padding': 32, 'ad_use_inpaint_width_height': False, 'ad_inpaint_width': 512, 'ad_inpaint_height': 512, 'ad_use_steps': False, 'ad_steps': 28, 'ad_use_cfg_scale': False, 'ad_cfg_scale': 7, 'ad_use_checkpoint': False, 'ad_checkpoint': 'Use same checkpoint', 'ad_use_sampler': False, 'ad_sampler': 'DPM++ 2M Karras', 'ad_use_noise_multiplier': False, 'ad_noise_multiplier': 1, 'ad_use_clip_skip': False, 'ad_clip_skip': 1, 'ad_restore_face': False, 'ad_controlnet_model': 'None', 'ad_controlnet_module': 'inpaint_global_harmonious', 'ad_controlnet_weight': 1, 'ad_controlnet_guidance_start': 0, 'ad_controlnet_guidance_end': 1, 'is_api': ()}, {'ad_model': 'None', 'ad_prompt': '', 'ad_negative_prompt': '', 'ad_confidence': 0.3, 'ad_mask_k_largest': 0, 'ad_mask_min_ratio': 0, 'ad_mask_max_ratio': 1, 'ad_x_offset': 0, 'ad_y_offset': 0, 'ad_dilate_erode': 4, 'ad_mask_merge_invert': 'None', 'ad_mask_blur': 4, 'ad_denoising_strength': 0.4, 'ad_inpaint_only_masked': True, 'ad_inpaint_only_masked_padding': 32, 'ad_use_inpaint_width_height': False, 'ad_inpaint_width': 512, 'ad_inpaint_height': 512, 'ad_use_steps': False, 'ad_steps': 28, 'ad_use_cfg_scale': False, 'ad_cfg_scale': 7, 'ad_use_checkpoint': False, 'ad_checkpoint': 'Use same checkpoint', 'ad_use_sampler': False, 'ad_sampler': 'DPM++ 2M Karras', 'ad_use_noise_multiplier': False, 'ad_noise_multiplier': 1, 'ad_use_clip_skip': False, 'ad_clip_skip': 1, 'ad_restore_face': False, 'ad_controlnet_model': 'None', 'ad_controlnet_module': 'inpaint_global_harmonious', 'ad_controlnet_weight': 1, 'ad_controlnet_guidance_start': 0, 'ad_controlnet_guidance_end': 1, 'is_api': ()}, False, False, 'positive', 'comma', 0, False, False, '', 1, '', [], 0, '', [], 0, '', [], True, False, False, False, 0, False) {}
    Traceback (most recent call last):
      File "/home/soichiro/build/stable-diffusion-webui/modules/call_queue.py", line 57, in f
        res = list(func(*args, **kwargs))
      File "/home/soichiro/build/stable-diffusion-webui/modules/call_queue.py", line 36, in f
        res = func(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/modules/txt2img.py", line 55, in txt2img
        processed = processing.process_images(p)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 732, in process_images
        res = process_images_inner(p)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 867, in process_images_inner
        samples_ddim = p.sample(conditioning=p.c, unconditional_conditioning=p.uc, seeds=p.seeds, subseeds=p.subseeds, subseed_strength=p.subseed_strength, prompts=p.prompts)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 1156, in sample
        return self.sample_hr_pass(samples, decoded_samples, seeds, subseeds, subseed_strength, prompts)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 1249, in sample_hr_pass
        decoded_samples = decode_latent_batch(self.sd_model, samples, target_device=devices.cpu, check_for_nans=True)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 594, in decode_latent_batch
        sample = decode_first_stage(model, batch[i:i + 1])[0]
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_samplers_common.py", line 76, in decode_first_stage
        return samples_to_images_tensor(x, approx_index, model)
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_samplers_common.py", line 58, in samples_to_images_tensor
        x_sample = model.decode_first_stage(sample.to(model.first_stage_model.dtype))
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
        return func(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/repositories/generative-models/sgm/models/diffusion.py", line 121, in decode_first_stage
        out = self.first_stage_model.decode(z)
      File "/home/soichiro/build/stable-diffusion-webui/modules/lowvram.py", line 71, in first_stage_model_decode_wrap
        return first_stage_model_decode(z)
      File "/home/soichiro/build/stable-diffusion-webui/repositories/generative-models/sgm/models/autoencoder.py", line 315, in decode
        dec = self.decoder(z, **decoder_kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
        return forward_call(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/repositories/generative-models/sgm/modules/diffusionmodules/model.py", line 722, in forward
        h = self.mid.attn_1(h, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
        return forward_call(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_hijack_optimizations.py", line 586, in cross_attention_attnblock_forward
        w1 = torch.bmm(q[:, i:end], k)     # b,hw,hw    w[b,i,j]=sum_c q[b,i,c]k[b,c,j]
    RuntimeError: CUDA error: HIPBLAS_STATUS_INVALID_VALUE when calling `hipblasSgemmStridedBatched( handle, opa, opb, m, n, k, &alpha, a, lda, stridea, b, ldb, strideb, &beta, c, ldc, stridec, num_batches)`

---
```

Note that the backtrace is different when running without `--medvram`:

```
    Traceback (most recent call last):
      File "/home/soichiro/build/stable-diffusion-webui/modules/call_queue.py", line 57, in f
        res = list(func(*args, **kwargs))
      File "/home/soichiro/build/stable-diffusion-webui/modules/call_queue.py", line 36, in f
        res = func(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/modules/txt2img.py", line 55, in txt2img
        processed = processing.process_images(p)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 732, in process_images
        res = process_images_inner(p)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 867, in process_images_inner
        samples_ddim = p.sample(conditioning=p.c, unconditional_conditioning=p.uc, seeds=p.seeds, subseeds=p.subseeds, subseed_strength=p.subseed_strength, prompts=p.prompts)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 1156, in sample
        return self.sample_hr_pass(samples, decoded_samples, seeds, subseeds, subseed_strength, prompts)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 1249, in sample_hr_pass
        decoded_samples = decode_latent_batch(self.sd_model, samples, target_device=devices.cpu, check_for_nans=True)
      File "/home/soichiro/build/stable-diffusion-webui/modules/processing.py", line 594, in decode_latent_batch
        sample = decode_first_stage(model, batch[i:i + 1])[0]
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_samplers_common.py", line 76, in decode_first_stage
        return samples_to_images_tensor(x, approx_index, model)
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_samplers_common.py", line 58, in samples_to_images_tensor
        x_sample = model.decode_first_stage(sample.to(model.first_stage_model.dtype))
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_hijack_utils.py", line 17, in <lambda>
        setattr(resolved_obj, func_path[-1], lambda *args, **kwargs: self(*args, **kwargs))
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_hijack_utils.py", line 28, in __call__
        return self.__orig_func(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
        return func(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/models/diffusion/ddpm.py", line 826, in decode_first_stage
        return self.first_stage_model.decode(z)
      File "/home/soichiro/build/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/models/autoencoder.py", line 90, in decode
        dec = self.decoder(z)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
        return forward_call(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/ldm/modules/diffusionmodules/model.py", line 631, in forward
        h = self.mid.attn_1(h)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
        return forward_call(*args, **kwargs)
      File "/home/soichiro/build/stable-diffusion-webui/modules/sd_hijack_optimizations.py", line 586, in cross_attention_attnblock_forward
        w1 = torch.bmm(q[:, i:end], k)     # b,hw,hw    w[b,i,j]=sum_c q[b,i,c]k[b,c,j]
    RuntimeError: CUDA error: HIPBLAS_STATUS_INVALID_VALUE when calling `hipblasSgemmStridedBatched( handle, opa, opb, m, n, k, &alpha, a, lda, stridea, b, ldb, strideb, &beta, c, ldc, stridec, num_batches)`
```
```


### Additional information

_No response_