├── README.md └── stable_fast_colab.ipynb /README.md: -------------------------------------------------------------------------------- 1 | 🐣 Please follow me for new updates https://twitter.com/camenduru
2 | 🔥 Please join our discord server https://discord.gg/k5BwmmvJJU
3 | 🥳 Please join my patreon community https://patreon.com/camenduru
4 | 5 | ## 🦒 Colab 6 | 7 | | Colab | Info 8 | | --- | --- | 9 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/chengzeyi/stable-fast-colab/blob/main/stable_fast_colab.ipynb) | stable_fast_colab (Author Colab) (New) 10 | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-fast-colab/blob/main/stable_fast_colab.ipynb) | stable_fast_colab (Old) 11 | 12 | ## Main Repo 13 | https://github.com/chengzeyi/stable-fast 14 | 15 | ## Output 16 | 17 | https://github.com/camenduru/stable-fast-colab/assets/54370274/d72e09ee-815a-4e09-8e2f-32e9fca12a1d 18 | 19 | -------------------------------------------------------------------------------- /stable_fast_colab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "view-in-github" 7 | }, 8 | "source": [ 9 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/stable-fast-colab/blob/main/stable_fast_colab.ipynb)" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# !pip install -q https://download.pytorch.org/whl/cu118/torchaudio-2.1.0%2Bcu118-cp310-cp310-linux_x86_64.whl\n", 19 | "# !pip install -q https://download.pytorch.org/whl/torchdata-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n", 20 | "# !pip install -q https://download.pytorch.org/whl/torchtext-0.16.0%2Bcpu-cp310-cp310-linux_x86_64.whl\n", 21 | "# !pip install -q https://download.pytorch.org/whl/cu118/torchvision-0.16.0%2Bcu118-cp310-cp310-linux_x86_64.whl\n", 22 | "# !pip install -q fastai==2.7.13\n", 23 | "!pip install -q diffusers transformers accelerate\n", 24 | "!pip install -q https://download.pytorch.org/whl/cu118/torch-2.1.0%2Bcu118-cp310-cp310-linux_x86_64.whl\n", 25 | "!pip install -q https://download.pytorch.org/whl/triton-2.1.0-0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl\n", 26 | "!pip install -q https://download.pytorch.org/whl/cu121/xformers-0.0.22.post7-cp310-cp310-manylinux2014_x86_64.whl\n", 27 | "!pip install -q https://github.com/camenduru/wheels/releases/download/colab/stable_fast-0.0.2-cp310-cp310-linux_x86_64.whl\n", 28 | "\n", 29 | "# !pip install -q ninja\n", 30 | "# !pip install -q git+https://github.com/chengzeyi/stable-fast.git@main#egg=stable-fast\n", 31 | "\n", 32 | "# !apt -y update -qq\n", 33 | "# !wget https://github.com/camenduru/gperftools/releases/download/v1.0/libtcmalloc_minimal.so.4 -O /content/libtcmalloc_minimal.so.4\n", 34 | "# %env LD_PRELOAD=/content/libtcmalloc_minimal.so.4\n", 35 | "\n", 36 | "!ldconfig /usr/lib64-nvidia" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "import torch, xformers, triton\n", 46 | "from diffusers import StableDiffusionPipeline\n", 47 | "from sfast.compilers.stable_diffusion_pipeline_compiler import (compile, CompilationConfig)\n", 48 | "torch.backends.cuda.matmul.allow_tf32 = True\n", 49 | "model = StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5', torch_dtype=torch.float16, safety_checker=None).to('cuda')\n", 50 | "config = CompilationConfig.Default()\n", 51 | "config.enable_xformers = True\n", 52 | "config.enable_triton = True\n", 53 | "config.enable_cuda_graph = True # After capturing, the model only accepts one fixed image size. If you want the model to be dynamic, don't enable it.\n", 54 | "compiled_model = compile(model, config)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "kwarg_inputs = dict(\n", 64 | " prompt=\n", 65 | " '(masterpiece:1,2), best quality, masterpiece, best detail face, lineart, monochrome, a beautiful girl',\n", 66 | " height=512,\n", 67 | " width=512,\n", 68 | " num_inference_steps=50,\n", 69 | " num_images_per_prompt=1,\n", 70 | ")\n", 71 | "output_image = compiled_model(**kwarg_inputs).images[0] # The first call will trigger compilation and might be very slow. # After the first call, it should be very fast.\n", 72 | "output_image" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "output_image = compiled_model(**kwarg_inputs).images[0]\n", 82 | "output_image" 83 | ] 84 | } 85 | ], 86 | "metadata": { 87 | "accelerator": "GPU", 88 | "colab": { 89 | "gpuType": "T4", 90 | "provenance": [] 91 | }, 92 | "kernelspec": { 93 | "display_name": "Python 3", 94 | "name": "python3" 95 | }, 96 | "language_info": { 97 | "name": "python" 98 | } 99 | }, 100 | "nbformat": 4, 101 | "nbformat_minor": 0 102 | } 103 | --------------------------------------------------------------------------------