AUTOMATIC1111 / stable-diffusion-webui

Stable Diffusion web UI
GNU Affero General Public License v3.0
139.41k stars 26.43k forks source link

[Bug]: launch.py cannot find module `xformers` although it is installed #12142

Open sanromd opened 1 year ago

sanromd commented 1 year ago

Is there an existing issue for this?

What happened?

I am running webUI from the command line inside an environment with all prerequisites installed, i.e., pip install -r requirements.txt; python launch.py. During ststartup, the script complains that xfomers cannot be found. However, the module is installed.

Steps to reproduce the problem

  1. Clone webui's repo
  2. install requirements (pip install -r requirements.txt)
  3. launch webui from the command line (python launch.py)

What should have happened?

The script should load without issues since all modules are available in the environment. I can load xformersfrom a jupyter notebook using the diffusers pipeline.

Version or Commit where the problem happens

Master

What Python version are you running on ?

Python 3.10.x

What platforms do you use to access the UI ?

Linux

What device are you running WebUI on?

Nvidia GPUs (RTX 20 above)

Cross attention optimization

xformers

What browsers do you use to access the UI ?

Microsoft Edge

Command Line Arguments

Launch webui from the command line (`python launch.py`)

List of extensions

No

Console logs

Launching Web UI with arguments: 
No module 'xformers'. Proceeding without it.


### Additional information

_No response_
w-e-w commented 1 year ago

expected behavior you need the arg --xformers to enable xformers

the message "No module 'xformers'. Proceeding without it." is not from us (webui) irrc from stability repo

w-e-w commented 1 year ago

also I recommend you start from the beginning as probably accidentally install stuff that you shouldn't don't install stuff manually, Just run the script for your platform

sanromd commented 1 year ago

Thanks, @w-e-w I tried running launch.py with the options --xformers and --force-enable-xformers. I also tested --reinstall-xformers. None of these options fixed the problem. If I try to force loading xformers I get the following error:

No module 'xformers'. Proceeding without it.
*** Cannot import xformers
    Traceback (most recent call last):
      File "$PATH_TO_AUTOMATIC!111/modules/sd_hijack_optimizations.py", line 156, in <module>
        import xformers.ops
    ModuleNotFoundError: import of xformers halted; None in sys.modules

However, xformers is installed in the environment

sanromd commented 1 year ago

@w-e-w see bug report. This issue persists across platforms/users https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/5993

w-e-w commented 1 year ago

ah Linux, not too familiar webui on linux

sanromd commented 1 year ago

It is strange because I can find xformers.ops in the env's site-packages. Below is the output of my sys.modules The error originates in lines 154:160 of _/modules/sd_hijackoptimizations.py

(note: I must load xformers.ops first, otherwise sys.modules cannot see the package)

python -c "import xformers.ops, sys; print(sys.modules['xformers.ops'])" | grep xformers 
<module 'xformers.ops' from '$PYTHON_ROOT/envs/diffusers/lib/python3.10/site-packages/xformers/ops/__init__.py'>
sanromd commented 1 year ago

@w-e-w, @liuzimo proposed a solution in https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/6594#discussioncomment-5216054. I tested it; it works.

Solution: modify modules/import_hook.py

import sys

# solution to load xformers by @liuzimo https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/6594#discussioncomment-5216054
# sd_hijack will fail to load xformers because sys.modules cannot find i. This solution solves the problem. 
if "--xformers" not in "".join(sys.argv):
    import xformers
sys.modules["xformers"] = xformers