|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Token Merging for Stable Diffusion running with OpenVINO demo\n", |
| 9 | + "This notebook demonstrates how to use Token Merging method to accelerate Stable Diffusion model running with OpenVINO. The method is applied to PyTorch model before exporting to OpenVINO representation." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import tomeov\n", |
| 19 | + "from diffusers import StableDiffusionPipeline, DDPMScheduler\n", |
| 20 | + "from diffusers.training_utils import set_seed\n", |
| 21 | + "from optimum.intel.openvino import OVStableDiffusionPipeline\n", |
| 22 | + "from IPython.display import display" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": null, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "scheduler = DDPMScheduler(beta_start=0.00085, beta_end=0.012,\n", |
| 32 | + " beta_schedule=\"scaled_linear\", num_train_timesteps=1000)\n", |
| 33 | + "pipe = StableDiffusionPipeline.from_pretrained(\"runwayml/stable-diffusion-v1-5\", scheduler=scheduler)\n", |
| 34 | + "pipe.safety_checker = lambda images, clip_input: (images, False)\n" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "attachments": {}, |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "* Create a pipiline with Token Merging applied to a Stable Diffusion model and export it to OpenVINO representation." |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "# Apply ToMe with a 30% merging ratio\n", |
| 52 | + "tomeov.patch_stable_diffusion(pipe, ratio=0.3) # Can also use pipe.unet in place of pipe here" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "save_dir = \"stable_diffusion_optimized\"\n", |
| 62 | + "tomeov.export_diffusion_pipeline(pipe, save_dir)" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "attachments": {}, |
| 67 | + "cell_type": "markdown", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "* Create OpenVINO-based pipeline. We fix image size for faster inference." |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": null, |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "set_seed(42)\n", |
| 80 | + "ov_pipe = OVStableDiffusionPipeline.from_pretrained(save_dir, compile=False)\n", |
| 81 | + "ov_pipe.reshape(batch_size=1, height=512, width=512, num_images_per_prompt=1)\n", |
| 82 | + "ov_pipe.compile()" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "attachments": {}, |
| 87 | + "cell_type": "markdown", |
| 88 | + "metadata": {}, |
| 89 | + "source": [ |
| 90 | + "* Generate and display the image." |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": null, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "set_seed(42)\n", |
| 100 | + "output = ov_pipe(prompt, num_inference_steps=50, output_type=\"pil\")\n", |
| 101 | + "display(output.images[0])" |
| 102 | + ] |
| 103 | + } |
| 104 | + ], |
| 105 | + "metadata": { |
| 106 | + "kernelspec": { |
| 107 | + "display_name": "Python 3.8.10 ('stable_diffusion')", |
| 108 | + "language": "python", |
| 109 | + "name": "python3" |
| 110 | + }, |
| 111 | + "language_info": { |
| 112 | + "codemirror_mode": { |
| 113 | + "name": "ipython", |
| 114 | + "version": 3 |
| 115 | + }, |
| 116 | + "file_extension": ".py", |
| 117 | + "mimetype": "text/x-python", |
| 118 | + "name": "python", |
| 119 | + "nbconvert_exporter": "python", |
| 120 | + "pygments_lexer": "ipython3", |
| 121 | + "version": "3.8.10" |
| 122 | + }, |
| 123 | + "vscode": { |
| 124 | + "interpreter": { |
| 125 | + "hash": "7918409a64d3d4275e0103fc4443d9be5863d1df136c02ed032407c7ae821339" |
| 126 | + } |
| 127 | + } |
| 128 | + }, |
| 129 | + "nbformat": 4, |
| 130 | + "nbformat_minor": 2 |
| 131 | +} |
0 commit comments