Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor' torchvision 0.17 promblem
### Is there an existing issue for this?

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

### What happened?

ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor'
crash
https://github.com/pytorch/pytorch/issues/103973#issuecomment-1813303128

### Steps to reproduce the problem

1. Install
https://github.com/pytorch/pytorch/issues/103973#issuecomment-1813214452
2. Run webui

### What should have happened?

Run normaly

### Sysinfo

AMD Radeon VII
AMD® Fx(tm)-9590 eight-core processor × 8 (not supporting pci atomics)
32 gb ram
Ubuntu 22.04.3 LTS x64

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

Mozilla Firefox

### Console logs

```Shell
(venv) b_cansin@b-cansin-ubuntu:/media/b_cansin/ai/ai/stable-diffusion-webui$ ./webui.sh

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################

################################################################
Running on b_cansin user
################################################################

################################################################
Repo already cloned, using it as install directory
################################################################

################################################################
python venv already activate or run without venv: /media/b_cansin/ai/ai/stable-diffusion-webui/venv
################################################################

################################################################
Launching launch.py...
################################################################
Using TCMalloc: libtcmalloc_minimal.so.4
fatal: No names found, cannot describe anything.
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
Version: 1.6.1
Commit hash: 7ba3923d5b494b7756d0b12f33acb3716d830b9a
Launching Web UI with arguments: 
2023-11-16 00:38:11.762329: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
no module 'xformers'. Processing without...
no module 'xformers'. Processing without...
No module 'xformers'. Proceeding without it.
*** Error setting up CodeFormer
    Traceback (most recent call last):
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/modules/codeformer_model.py", line 30, in setup_model
        from modules.codeformer.codeformer_arch import CodeFormer
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/modules/codeformer/codeformer_arch.py", line 9, in <module>
        from modules.codeformer.vqgan_arch import VQAutoEncoder, ResBlock
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/modules/codeformer/vqgan_arch.py", line 11, in <module>
        from basicsr.utils import get_root_logger
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/__init__.py", line 4, in <module>
        from .data import *
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/__init__.py", line 22, in <module>
        _dataset_modules = [importlib.import_module(f'basicsr.data.{file_name}') for file_name in dataset_filenames]
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/__init__.py", line 22, in <listcomp>
        _dataset_modules = [importlib.import_module(f'basicsr.data.{file_name}') for file_name in dataset_filenames]
      File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/realesrgan_dataset.py", line 11, in <module>
        from basicsr.data.degradations import circular_lowpass_kernel, random_mixed_kernels
      File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/degradations.py", line 8, in <module>
        from torchvision.transforms.functional_tensor import rgb_to_grayscale
    ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor'

---
Traceback (most recent call last):
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/launch.py", line 48, in <module>
    main()
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/launch.py", line 44, in main
    start()
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/modules/launch_utils.py", line 436, in start
    webui.webui()
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/webui.py", line 52, in webui
    initialize.initialize()
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/modules/initialize.py", line 71, in initialize
    from modules import gfpgan_model
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/modules/gfpgan_model.py", line 4, in <module>
    import gfpgan
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/gfpgan/__init__.py", line 3, in <module>
    from .data import *
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/gfpgan/data/__init__.py", line 10, in <module>
    _dataset_modules = [importlib.import_module(f'gfpgan.data.{file_name}') for file_name in dataset_filenames]
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/gfpgan/data/__init__.py", line 10, in <listcomp>
    _dataset_modules = [importlib.import_module(f'gfpgan.data.{file_name}') for file_name in dataset_filenames]
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/gfpgan/data/ffhq_degradation_dataset.py", line 7, in <module>
    from basicsr.data import degradations as degradations
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/__init__.py", line 4, in <module>
    from .data import *
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/__init__.py", line 22, in <module>
    _dataset_modules = [importlib.import_module(f'basicsr.data.{file_name}') for file_name in dataset_filenames]
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/__init__.py", line 22, in <listcomp>
    _dataset_modules = [importlib.import_module(f'basicsr.data.{file_name}') for file_name in dataset_filenames]
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/realesrgan_dataset.py", line 11, in <module>
    from basicsr.data.degradations import circular_lowpass_kernel, random_mixed_kernels
  File "/media/b_cansin/ai/ai/stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/degradations.py", line 8, in <module>
    from torchvision.transforms.functional_tensor import rgb_to_grayscale
ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor'
(venv) b_cansin@b-cansin-ubuntu:/media/b_cansin/ai/ai/stable-diffusion-webui$
```


### Additional information

As @hongxiayang said in here:
https://github.com/pytorch/pytorch/issues/103973#issuecomment-1813380169
Need support torchvision 0.17 because of torchvision.transforms.functional deprecated and removed in 0.17. Could we get dev version that support 0.17?
After than we will test if this wheel fix non pci atomics problem of ROCM


Discussion:
This looks like it should probably be opened at [basicsr's repo](https://github.com/XPixelGroup/BasicSR),as I believe A1111 just pulls from that repo, it hasn't been updated in a while though. 

In the short term - try:

Open `./stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/degradations.py` and on line 8, simply change:

```python
from torchvision.transforms.functional_tensor import rgb_to_grayscale
```
to:
```python
from torchvision.transforms.functional import rgb_to_grayscale
```

Which should at least get you past this step.
Opened a PR on basicsr repo https://github.com/XPixelGroup/BasicSR/pull/650
> This looks like it should probably be opened at [basicsr's repo](https://github.com/XPixelGroup/BasicSR),as I believe A1111 just pulls from that repo, it hasn't been updated in a while though.
> 
> In the short term - try:
> 
> Open `./stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/degradations.py` and on line 8, simply change:
> 
> ```python
> from torchvision.transforms.functional_tensor import rgb_to_grayscale
> ```
> 
> to:
> 
> ```python
> from torchvision.transforms.functional import rgb_to_grayscale
> ```
> 
> Which should at least get you past this step.

Yes that fix my problem now I can finally gen with pytorch 2 without black image or pci atomics problem

![Screenshot from 2023-11-16 13-58-13](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/52115509/66ae618c-d132-43fc-b273-d1389d8dc96f)


> This looks like it should probably be opened at [basicsr's repo](https://github.com/XPixelGroup/BasicSR),as I believe A1111 just pulls from that repo, it hasn't been updated in a while though.
> 
> In the short term - try:
> 
> Open `./stable-diffusion-webui/venv/lib/python3.10/site-packages/basicsr/data/degradations.py` and on line 8, simply change:
> 
> ```python
> from torchvision.transforms.functional_tensor import rgb_to_grayscale
> ```
> 
> to:
> 
> ```python
> from torchvision.transforms.functional import rgb_to_grayscale
> ```
> 
> Which should at least get you past this step.

Lazy people (like me) can use sed (on Mac and Linux):

mac
```
sed -i '' 's/from torchvision.transforms.functional_tensor import rgb_to_grayscale/from torchvision.transforms.functional import rgb_to_grayscale/' venv/lib/python3.10/site-packages/basicsr/data/degradations.py
```

linux
```
sed -i 's/from torchvision.transforms.functional_tensor import rgb_to_grayscale/from torchvision.transforms.functional import rgb_to_grayscale/' venv/lib/python3.10/site-packages/basicsr/data/degradations.py
```

Mac requires an additional parameter for the backup file, but the rest is the same for both.

Resolving patch:
diff --git a/modules/import_hook.py b/modules/import_hook.py
--- a/modules/import_hook.py
+++ b/modules/import_hook.py
@@ -3,3 +3,14 @@
 # this will break any attempt to import xformers which will prevent stability diffusion repo from trying to use it
 if "--xformers" not in "".join(sys.argv):
     sys.modules["xformers"] = None
+
+# Hack to fix a changed import in torchvision 0.17+, which otherwise breaks
+# basicsr; see https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/13985
+try:
+    import torchvision.transforms.functional_tensor  # noqa: F401
+except ImportError:
+    try:
+        import torchvision.transforms.functional as functional
+        sys.modules["torchvision.transforms.functional_tensor"] = functional
+    except ImportError:
+        pass  # shrug...


Test patch: