From 79953e9b8b0d04868b719452a5b250c450f757b6 Mon Sep 17 00:00:00 2001 From: brkirch Date: Thu, 1 Dec 2022 03:38:13 -0500 Subject: [PATCH 1/3] Add support for macOS (Darwin) in launch.py --- launch.py | 19 ++++++++++++++++++- webui-user.sh | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/launch.py b/launch.py index ad9ddd5a9..48314264e 100644 --- a/launch.py +++ b/launch.py @@ -193,6 +193,20 @@ def prepare_enviroment(): xformers = '--xformers' in sys.argv ngrok = '--ngrok' in sys.argv + if platform.system() == 'Darwin': + os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1" + torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.12.1 torchvision==0.13.1") + k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/brkirch/k-diffusion.git') + k_diffusion_commit_hash = os.environ.get('K_DIFFUSION_COMMIT_HASH', "51c9778f269cedb55a4d88c79c0246d35bdadb71") + if os.environ.get('COMMANDLINE_ARGS') == None: + if '--use-cpu' in sys.argv: + idx = sys.argv.index('--use-cpu') + if idx < len(sys.argv) and sys.argv[idx+1][0] != '-': + sys.argv.insert(idx+1, 'interrogate') + else: + sys.argv += ['--use-cpu', 'interrogate'] + sys.argv.append('--no-half') + try: commit = run(f"{git} rev-parse HEAD").strip() except Exception: @@ -204,7 +218,7 @@ def prepare_enviroment(): if not is_installed("torch") or not is_installed("torchvision"): run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch") - if not skip_torch_cuda_test: + if not skip_torch_cuda_test and platform.system() != 'Darwin': run_python("import torch; assert torch.cuda.is_available(), 'Torch is not able to use GPU; add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check'") if not is_installed("gfpgan"): @@ -231,6 +245,9 @@ def prepare_enviroment(): if not is_installed("pyngrok") and ngrok: run_pip("install pyngrok", "ngrok") + if platform.system() == 'Darwin' and not is_installed("psutil"): + run_pip("install psutil", "psutil") + os.makedirs(dir_repos, exist_ok=True) git_clone(stable_diffusion_repo, repo_dir('stable-diffusion-stability-ai'), "Stable Diffusion", stable_diffusion_commit_hash) diff --git a/webui-user.sh b/webui-user.sh index 16e427597..bfa53cb7c 100644 --- a/webui-user.sh +++ b/webui-user.sh @@ -10,7 +10,7 @@ #clone_dir="stable-diffusion-webui" # Commandline arguments for webui.py, for example: export COMMANDLINE_ARGS="--medvram --opt-split-attention" -export COMMANDLINE_ARGS="" +#export COMMANDLINE_ARGS="" # python3 executable #python_cmd="python3" From bef36597cc46671eeb2041b0343bd5e183883eb7 Mon Sep 17 00:00:00 2001 From: brkirch Date: Thu, 1 Dec 2022 04:04:14 -0500 Subject: [PATCH 2/3] Fix run as root flag Even though -f enables running webui.sh as root, the -f flag will also be passed to launch.py, causing it to exit with a usage message. This adds a line to launch.py to remove the -f flag if present. In addition to the above, all the letters in the command line arguments after each '-' were being processed for 'f' and "illegal option" was displayed for each letter that didn't match. Instead, this commit silences those errors and stops processing if the first flag doesn't start with '-f'. --- launch.py | 1 + webui.sh | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/launch.py b/launch.py index 48314264e..1661b569e 100644 --- a/launch.py +++ b/launch.py @@ -186,6 +186,7 @@ def prepare_enviroment(): parser.add_argument("--ui-settings-file", type=str, help="filename to use for ui settings", default='config.json') args, _ = parser.parse_known_args(sys.argv) + sys.argv, _ = extract_arg(sys.argv, '-f') sys.argv, skip_torch_cuda_test = extract_arg(sys.argv, '--skip-torch-cuda-test') sys.argv, reinstall_xformers = extract_arg(sys.argv, '--reinstall-xformers') sys.argv, update_check = extract_arg(sys.argv, '--update-check') diff --git a/webui.sh b/webui.sh index 6d4f09922..5f48741fc 100755 --- a/webui.sh +++ b/webui.sh @@ -51,10 +51,11 @@ fi can_run_as_root=0 # read any command line flags to the webui.sh script -while getopts "f" flag +while getopts "f" flag > /dev/null 2>&1 do case ${flag} in f) can_run_as_root=1;; + *) break;; esac done From 5ec8981df46ff6e678c09dd2c1bf4d873ac22a46 Mon Sep 17 00:00:00 2001 From: brkirch Date: Sat, 3 Dec 2022 02:28:53 -0500 Subject: [PATCH 3/3] Revert most launch.py changes, add mac user script Adds an addition file to read environment variables from when the webui.sh is run from macOS. --- launch.py | 19 +------------------ webui-macos-env.sh | 13 +++++++++++++ webui.sh | 8 ++++++++ 3 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 webui-macos-env.sh diff --git a/launch.py b/launch.py index 1661b569e..0e1bbaf22 100644 --- a/launch.py +++ b/launch.py @@ -194,20 +194,6 @@ def prepare_enviroment(): xformers = '--xformers' in sys.argv ngrok = '--ngrok' in sys.argv - if platform.system() == 'Darwin': - os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1" - torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.12.1 torchvision==0.13.1") - k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/brkirch/k-diffusion.git') - k_diffusion_commit_hash = os.environ.get('K_DIFFUSION_COMMIT_HASH', "51c9778f269cedb55a4d88c79c0246d35bdadb71") - if os.environ.get('COMMANDLINE_ARGS') == None: - if '--use-cpu' in sys.argv: - idx = sys.argv.index('--use-cpu') - if idx < len(sys.argv) and sys.argv[idx+1][0] != '-': - sys.argv.insert(idx+1, 'interrogate') - else: - sys.argv += ['--use-cpu', 'interrogate'] - sys.argv.append('--no-half') - try: commit = run(f"{git} rev-parse HEAD").strip() except Exception: @@ -219,7 +205,7 @@ def prepare_enviroment(): if not is_installed("torch") or not is_installed("torchvision"): run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch") - if not skip_torch_cuda_test and platform.system() != 'Darwin': + if not skip_torch_cuda_test: run_python("import torch; assert torch.cuda.is_available(), 'Torch is not able to use GPU; add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check'") if not is_installed("gfpgan"): @@ -246,9 +232,6 @@ def prepare_enviroment(): if not is_installed("pyngrok") and ngrok: run_pip("install pyngrok", "ngrok") - if platform.system() == 'Darwin' and not is_installed("psutil"): - run_pip("install psutil", "psutil") - os.makedirs(dir_repos, exist_ok=True) git_clone(stable_diffusion_repo, repo_dir('stable-diffusion-stability-ai'), "Stable Diffusion", stable_diffusion_commit_hash) diff --git a/webui-macos-env.sh b/webui-macos-env.sh new file mode 100644 index 000000000..68d1f7549 --- /dev/null +++ b/webui-macos-env.sh @@ -0,0 +1,13 @@ +#!/bin/bash +#################################################################### +# macOS defaults # +# Please modify webui-user.sh to change these instead of this file # +#################################################################### + +export COMMANDLINE_ARGS="--skip-torch-cuda-test --no-half --use-cpu interrogate" +export TORCH_COMMAND="pip install torch==1.12.1 torchvision==0.13.1" +export K_DIFFUSION_REPO="https://github.com/brkirch/k-diffusion.git" +export K_DIFFUSION_COMMIT_HASH="51c9778f269cedb55a4d88c79c0246d35bdadb71" +export PYTORCH_ENABLE_MPS_FALLBACK=1 + +#################################################################### diff --git a/webui.sh b/webui.sh index 5f48741fc..683c97d3a 100755 --- a/webui.sh +++ b/webui.sh @@ -4,6 +4,14 @@ # change the variables in webui-user.sh instead # ################################################# +# If run from macOS, load defaults from webui-macos-env.sh +if [[ "$OSTYPE" == "darwin"* ]]; then + if [[ -f webui-macos-env.sh ]] + then + source ./webui-macos-env.sh + fi +fi + # Read variables from webui-user.sh # shellcheck source=/dev/null if [[ -f webui-user.sh ]]