From 0993272b9eab76bbb385cc964bec7ced65e0cdbf Mon Sep 17 00:00:00 2001 From: Drake Panzer <64224601+Cyberes@users.noreply.github.com> Date: Sat, 15 Apr 2023 17:56:01 -0600 Subject: [PATCH] fix model downloader, other minor changes --- StableDiffusionUI_Voldemort_paperspace.ipynb | 169 ++++++++++--------- 1 file changed, 91 insertions(+), 78 deletions(-) diff --git a/StableDiffusionUI_Voldemort_paperspace.ipynb b/StableDiffusionUI_Voldemort_paperspace.ipynb index 32ce276..d90b627 100644 --- a/StableDiffusionUI_Voldemort_paperspace.ipynb +++ b/StableDiffusionUI_Voldemort_paperspace.ipynb @@ -81,7 +81,6 @@ "\n", "# Optional path settings\n", "repo_storage_dir = '/storage/stable-diffusion' # Where to store your Stable Diffusion-related files.\n", - " # The WebUI will be installed to repo_storage_dir/stable-diffusion-webui\n", "\n", "export_storage_dir = '/notebooks/exports' # Where the generated images will be exported to.\n", "\n", @@ -125,9 +124,11 @@ "\n", "gradio_queue = False # Uses gradio queue; experimental option; breaks restart UI button.\n", "\n", + "install_pip_xformers = False # Install xformers through pip. Probably won't work because it needs Torch 2.0\n", + "\n", "# ===================================================================================================\n", "# Save variables to Jupiter's temp storage so we can access it even if the kernel restarts.\n", - "%store symlink_to_notebooks model_storage_dir repo_storage_dir export_storage_dir activate_xformers link_novelai_anime_vae activate_deepdanbooru activate_medvram disable_pickle_check gradio_port gradio_auth search_paperspace_datasets ui_theme insecure_extension_access pip_cache_dir gradio_queue" + "%store symlink_to_notebooks model_storage_dir repo_storage_dir export_storage_dir activate_xformers link_novelai_anime_vae activate_deepdanbooru activate_medvram disable_pickle_check gradio_port gradio_auth search_paperspace_datasets ui_theme insecure_extension_access pip_cache_dir gradio_queue install_pip_xformers" ] }, { @@ -217,32 +218,26 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "tags": [] + }, "source": [ "## Python 3.10\n", "\n", - "Python 3.10 is the recommended Python version for running the WebUI. If you encounter any issues, maybe running the WebUI in Python 3.10 will help? Paperspace uses Python 3.9 so you must run a custom container.\n", + "Python 3.10 is the recommended Python version for running the WebUI. Paperspace uses Python 3.9 for their containers so you must use a custom container. Luckily, I've created a container for you to use.\n", "\n", "First, delete your current notebook and create a new one following these instructions: https://docs.paperspace.com/gradient/notebooks/runtimes/#how-to-specify-a-custom-container\n", "\n", - "Make sure to use this container image: `cyberes/gradient-base-py3.10:latest`\n", + "Make sure to use this container image: `cyberes/gradient-base-py3.10`\n", "\n", "You can use the block below to test your Python version." ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Your Python version is less than 3.10 -> 3.9.13\n" - ] - } - ], + "outputs": [], "source": [ "import sys\n", "version = sys.version.split(' ')[0]\n", @@ -253,6 +248,15 @@ " print('Your Python version is good:', version)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you are using the special Python 3.10 container *cyberes/gradient-base-py3.10:latest* you need to either install xformers through pip or build them yourself.\n", + "\n", + "First, try installing it through pip by setting `install_pip_xformers = True` in the settings block above. If that fails or you encounter issues, use the block in the Tools section to build xformers for your machine." + ] + }, { "cell_type": "markdown", "metadata": { @@ -268,14 +272,13 @@ "execution_count": null, "metadata": { "id": "SaAJk33ppFw1", - "scrolled": true, "tags": [] }, "outputs": [], "source": [ "try:\n", - " %store -r symlink_to_notebooks model_storage_dir repo_storage_dir activate_xformers activate_deepdanbooru pip_cache_dir\n", - " test = [symlink_to_notebooks, model_storage_dir, repo_storage_dir, activate_xformers, activate_deepdanbooru, pip_cache_dir]\n", + " %store -r symlink_to_notebooks model_storage_dir repo_storage_dir activate_xformers activate_deepdanbooru pip_cache_dir install_pip_xformers\n", + " test = [symlink_to_notebooks, model_storage_dir, repo_storage_dir, activate_xformers, activate_deepdanbooru, pip_cache_dir, install_pip_xformers]\n", "except NameError as e:\n", " print(\"There is an issue with your variables.\")\n", " print(\"Please go back to the first block and make sure your settings are correct, then run the cell.\")\n", @@ -314,54 +317,59 @@ "\n", "# We need to install xformers first so that the WebUI installer can install the correct version of PyTorch afterwards\n", "if activate_xformers:\n", - " print('Installing xformers...')\n", - " import subprocess\n", - " from glob import glob\n", - " def download_release(url, binary_name='xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl'):\n", - " tmp_dir = subprocess.check_output(['mktemp', '-d']).decode('ascii').strip('\\n')\n", - " !wget \"{url}\" -O \"{tmp_dir}/{binary_name}\"\n", - " return os.path.join(tmp_dir, binary_name)\n", - " \n", - " xformers_whl = None\n", - " found_xformers_whls = glob('/notebooks/xformers-*')\n", - " if len(found_xformers_whls) == 1:\n", - " xformers_whl = found_xformers_whls[0]\n", - " delete_whl = False\n", - " elif len(found_xformers_whls) > 1:\n", - " print('Found more than one Xformers wheel in /notebooks so not doing anything!')\n", + " if install_pip_xformers:\n", + " print('Installing xformers through pip...')\n", + " !pip install --no-dependencies xformers\n", " else:\n", - " delete_whl = True\n", - " # Set up pip packages\n", - " # !pip uninstall -y torch torchvision torchaudio # Remove existing pytorch install.\n", - " # !pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113 # Install pytorch for cuda 11.3\n", - " s = subprocess.getoutput('nvidia-smi')\n", - " if 'A4000' in s:\n", - " xformers_whl = download_release('https://raw.githubusercontent.com/Cyberes/xformers-compiled/main/a4000/xformers-0.0.16%2B6f3c20f.d20230127-cp39-cp39-linux_x86_64.whl')\n", - " elif 'A5000' in s:\n", - " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A5000-Nov-1-2022/a5000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", - " elif 'A6000' in s:\n", - " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A6000-Nov-1-2022/a6000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", - " elif 'P5000' in s:\n", - " xformers_whl = download_release('https://raw.githubusercontent.com/Cyberes/xformers-compiled/main/p5000/xformers-0.0.16%2B6f3c20f.d20230127-cp39-cp39-linux_x86_64.whl')\n", - " elif 'RTX 4000' in s:\n", - " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/RTX-4000-Nov-1-2022/rtx4000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", - " elif 'RTX 5000' in s:\n", - " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/RTX-5000-Nov-1-2022/rtx5000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", - " elif 'A100' in s:\n", - " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A100-Nov-1-2022/a100-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", - " elif 'M4000' in s:\n", - " print('xformers for M4000 hasn\\'t been built yet.')\n", - " # xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A100-Nov-1-2022/a100-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", - " else:\n", - " print('GPU not matched to xformers binary so a one-size-fits-all binary was installed. If you have any issues, please build xformers using the Tools block below.')\n", - " xformers_whl = download_release('https://raw.githubusercontent.com/Cyberes/xformers-compiled/main/various/xformers-0.0.14.dev0-cp37-cp37m-linux_x86_64.whl')\n", - " if xformers_whl:\n", - " # We're going to install xformers without installing any of its dependencies since they should already be installed.\n", - " # If you have issues with this step then try replacing --no-dependencies with --force-reinstall\n", - " !pip install --no-dependencies \"{xformers_whl}\"\n", - " if delete_whl:\n", - " !rm -rf \"{xformers_whl}\"\n", + " import subprocess\n", + " from glob import glob\n", + " def download_release(url, binary_name='xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl'):\n", + " tmp_dir = subprocess.check_output(['mktemp', '-d']).decode('ascii').strip('\\n')\n", + " !wget \"{url}\" -O \"{tmp_dir}/{binary_name}\"\n", + " return os.path.join(tmp_dir, binary_name)\n", "\n", + " xformers_whl = None\n", + " found_xformers_whls = glob('/notebooks/xformers-*')\n", + " if len(found_xformers_whls) == 1:\n", + " print('Installing xformers using your pre-built wheel...')\n", + " xformers_whl = found_xformers_whls[0]\n", + " delete_whl = False\n", + " elif len(found_xformers_whls) > 1:\n", + " print('Found more than one Xformers wheel in /notebooks so not doing anything!')\n", + " else:\n", + " print('Installing xformers from wheels on Github...')\n", + " delete_whl = True\n", + " # Set up pip packages\n", + " # !pip uninstall -y torch torchvision torchaudio # Remove existing pytorch install.\n", + " # !pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113 # Install pytorch for cuda 11.3\n", + " s = subprocess.getoutput('nvidia-smi')\n", + " if 'A4000' in s:\n", + " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/raw/main/a4000/xformers-0.0.18%2Bda27862.d20230413-cp39-cp39-linux_x86_64.whl')\n", + " elif 'A5000' in s:\n", + " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A5000-Nov-1-2022/a5000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", + " elif 'A6000' in s:\n", + " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A6000-Nov-1-2022/a6000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", + " elif 'P5000' in s:\n", + " xformers_whl = download_release('https://raw.githubusercontent.com/Cyberes/xformers-compiled/main/p5000/xformers-0.0.16%2B6f3c20f.d20230127-cp39-cp39-linux_x86_64.whl')\n", + " elif 'RTX 4000' in s:\n", + " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/RTX-4000-Nov-1-2022/rtx4000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", + " elif 'RTX 5000' in s:\n", + " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/RTX-5000-Nov-1-2022/rtx5000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", + " elif 'A100' in s:\n", + " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A100-Nov-1-2022/a100-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", + " elif 'M4000' in s:\n", + " print('xformers for M4000 hasn\\'t been built yet.')\n", + " # xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A100-Nov-1-2022/a100-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n", + " else:\n", + " print('GPU not matched to xformers binary so a one-size-fits-all binary was installed. If you have any issues, please build xformers using the Tools block below.')\n", + " xformers_whl = download_release('https://raw.githubusercontent.com/Cyberes/xformers-compiled/main/various/xformers-0.0.14.dev0-cp37-cp37m-linux_x86_64.whl')\n", + " if xformers_whl:\n", + " !pip uninstall -y xformers\n", + " # We're going to install xformers without installing any of its dependencies since they should already be installed.\n", + " # If you have any issues then replacing --no-dependencies with --force-reinstall\n", + " !pip install --no-dependencies \"{xformers_whl}\"\n", + " if delete_whl:\n", + " !rm -rf \"{xformers_whl}\"\n", "# Make sure important directories exists\n", "!mkdir -p \"{model_storage_dir}/hypernetworks\"\n", "!mkdir -p \"{model_storage_dir}/vae\"\n", @@ -377,6 +385,7 @@ "cell_type": "markdown", "metadata": { "id": "F0EINk5M0s-w", + "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1208,16 +1217,12 @@ { "cell_type": "markdown", "metadata": { + "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ - "# Export Generations" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ + "# Export Generations\n", + "\n", "This block will rename and compress the outputs with 7zip max compression. It expects you to have `log/` and `outputs/` in `/notebooks/stable-diffusion-webui/`." ] }, @@ -1256,6 +1261,7 @@ "!cd \"{repo_storage_dir / 'stable-diffusion-webui' / 'log'}\" && mv * \"{export_storage_dir / datetime_str / 'log'}\"\n", "!cd \"{repo_storage_dir / 'stable-diffusion-webui' / 'outputs'}\" && mv * \"{export_storage_dir / datetime_str}\"\n", "s = subprocess.run(f'find \"{Path(export_storage_dir, datetime_str)}\" -type d -name .ipynb_checkpoints -exec rm -rv {{}} +', shell=True)\n", + "print('Compressing folder:', export_storage_dir / datetime_str)\n", "!7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on \"{datetime_str}.7z\" \"{export_storage_dir / datetime_str}\"" ] }, @@ -1619,11 +1625,7 @@ "source": [ "If you're still encountering issues then try building it yourself using the next block. You can also try [my old script](https://github.com/Engineer-of-Stuff/stable-diffusion-paperspace/blob/master/other/build-xformers.sh).\n", "\n", - "This will take over 25 minutes but you should only have to do this once. Leave the Xformers `.whl` in `/notebooks/` and it will automatically be installed by the notebook's installer.\n", - "\n", - "To build a minimal version of the xformers library, add this in front of `MAX_JOBS=$(nproc)`\n", - "\n", - "`XFORMERS_DISABLE_FLASH_ATTN=1 NVCC_FLAGS=\"--use_fast_math -DXFORMERS_MEM_EFF_ATTENTION_DISABLE_BACKWARD\"`" + "This will take over 25 minutes but you should only have to do this once. Leave the Xformers `.whl` in `/notebooks/` and it will automatically be installed by the notebook's installer." ] }, { @@ -1643,18 +1645,29 @@ "LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1`)\n", "echo \"Building version: $LATEST_TAG\"\n", "\n", - "MAX_JOBS=$(nproc) pip wheel --no-dependencies --wheel-dir=\"$TMP\" \"git+https://github.com/facebookresearch/xformers.git@$LATEST_TAG#egg=xformers\"\n", + "XFORMERS_DISABLE_FLASH_ATTN=1 NVCC_FLAGS=\"--use_fast_math -DXFORMERS_MEM_EFF_ATTENTION_DISABLE_BACKWARD\" MAX_JOBS=$(nproc) pip wheel --no-dependencies --wheel-dir=\"$TMP\" \"git+https://github.com/facebookresearch/xformers.git@$LATEST_TAG#egg=xformers\"\n", "if [[ $? -eq 0 ]]; then\n", " echo -e \"Finished!\\nMoving .whl to /notebooks/\"\n", " cp \"$TMP\"/xformers-* /notebooks/\n", " echo \"Here is your wheel file:\"\n", " find /notebooks -name xformers-*.whl\n", " echo \"Installing your new Xformers wheel...\"\n", - " pip install \"$TMP\"/xformers-*\n", + " pip install --force-reinstall --no-dependencies \"$TMP\"/xformers-*\n", "fi\n", "rm -rf \"$TMP\"" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Restart the kernel\n", + "import os\n", + "os.kill(os.getpid(), 9)" + ] + }, { "cell_type": "markdown", "metadata": {