Introduction

This guide will cover the general basics of how to use Google Colab with a focus on what's necessary to know when using it to run AUTOMATIC1111's Web UI, as well as explaining how to utilise one of the most popular Colab notebooks (TheLastBen's fast stable diffusion notebook) to run said Web UI. What this won't cover is how to use AUTOMATIC1111's Web UI itself, but I imagine there are plenty of tutorials on that subject.

Using Google Colab

Honestly, Google's own "Welcome to Colaboratory" notebook that shows up when you first open the site is already a good breakdown of all of the site's features, and I do recommend browsing that if you're interested. Rather than trying to re-explain all of that, the focus here will be on cutting down all the information to just what you need to know to be able to use Colab to run AUTOMATIC1111's Web UI.

What is Google Colab (Colaboratory)?

Google Colab (or Colaboratory, as it's actually called) is an online service that allows people to write, share, and run code remotely on Google's hardware. The benefit for our case specifically is that this available hardware includes access to good GPUs, allowing anyone to use GPU-demanding AI image generation regardless of their own hardware.

Anyone can prepare blocks of code together with instructions, and share those setups with others to make it easier for them to do whatever they designed it to do. People will sometimes refer to these prepared blocks as "Colabs" (myself included), but "notebooks" are the actual term. Remember when running into difficulties and looking for help that you should link to what notebook you're using, as there can be many notebooks, written by different people, made for the same purpose (there are multiple notebooks to set up and run AUTOMATIC1111's Web UI, for example).

The service comes with paid and free tiers, with the free service being subject to usage limits on how long you are allowed GPU time (time spent in a runtime with access to a GPU - which is essentially required for AI image generation). There's no explicit and fixed rules on how GPU time is allocated for free users - just know that if you use GPUs frequently you'll be denied access to GPUs more often (for example, having to wait days), and only be allowed shorter spans of times with them. So long as you use the free service casually, you should be fine. Some people recommend using multiple accounts to try and bypass those limitations, but in addition to that being against the service's usage rules, Google are also still likely to know it's you, and adjust things accordingly.

Runtimes

When you begin running a notebook, you'll be connected to a runtime - a virtual machine running on Google's hardware. Now connected to a runtime, you'll be able to execute code by running cells in your notebook (we'll get to cells a bit later). Every runtime starts off as a clean slate, which is why most notebooks you'll find include code to install the additional things necessary for it to use. Runtimes are only temporary, and any change you make - like files you install or add - will be gone when the runtime eventually ends. That means you'll have to go through any installation steps outlined in your notebook every time you start a new runtime. If you disconnect from a runtime, you should still have several minutes to reconnect to that same runtime (with all the changes you've made to it preserved) before it gets terminated for being idle. To reconnect, open the notebook and click "Reconnect" near the top right (if you can't find the notebook, you should be able to find it in Runtime -> Manage sessions).

A runtime will end when:

  • It's reached the maximum allowed time that a runtime can run for (generally 12 hours).
  • It's been idle for too long.
  • It's using a GPU (as you will be for AI image generation), and you have run out of GPU time.
  • You manually end the runtime with Runtime -> Disconnect and delete runtime or through the Runtime -> Manage sessions menu.

Remember to clean up!

Once you've finished with your runtime, try to remember to manually end it (see the last bullet point for how). While the runtime will eventually end itself for being idle, that's precious GPU time being eaten up until then.

Cells

Everything inside a notebook is split into blocks - of text, code, images, or all of them together - called "cells". The most important ones for our purposes, since they're the ones that actually do anything functional, are code cells. Run a code cell by pressing the ▶️ button at its top left, which will then execute whatever code was contained inside of it. Most code cells you encounter will be ones that will automatically stop when they've finished doing what they're doing. However, some code blocks, like the one that'll be running the A1111 Web UI, will continue running indefinitely until manually stopped, which you can do on any currently running code block by pressing the ⏹️ button that replaces the start button in the top left while the cell is running.

Code cells will often have configuration options, like checkboxes, dropdown menus, and textboxes to enter information into, to easily configure the ways in which it works, for example offering different ways to load an AI model. Any good notebook should hopefully make it clear what the options do and how to use them through the text and labels that go with them. When using a cell that has configuration options, you should make your configurations before running the cell!

Oftentimes code cells will output information about their current status in a section beneath them as they run, known as the log, which can contain useful information like installation progress, links to connect to your A1111 Web UI, or any errors. Error messages can be useful information to have when trying to find out what went wrong, either by yourself or when searching for help.

There can only be one

Only one cell can be running at a time! If you need to run another cell while one is already running - for example, you have the A1111 Web UI running and realise you'd like to use a code block to install some ControlNet models - you will have to stop the running cell first (with the ⏹️ button).

Advanced: Adding code cells

Sometimes it may be necessary to add custom code cells to a notebook. You can do this in a number of ways:

  • Going to Insert -> Code cell
  • Pressing Ctrl+M B
  • Mousing over the space between existing cells, and pressing the + Code button that appears.

Files

You may never have to actually manually access your runtime's files, but it's worth at least explaining it. The files tab can be opened by pressing the folder icon on the sidebar labelled "Files". It contains a list of all the files stored in your current runtime, which you can move, rename, delete, etc. like you can with any file navigator. You can upload files by dragging and dropping the files into that section, but the process can be slow, or outright not allowed for larger files. It is usually better to get files into your runtime by other means, such as using code to download the files directly from a link, or connecting your Google Drive (as explained later).

Annoyingly, the Files tab doesn't allow you to copy files from one place to the other, only move. In order to copy a file, you'll need a code cell to do it. Should you need one, I've written one that's available here.

Remember, as mentioned before, that all these files listed here will be deleted when your runtime ends (the exception to this is anything inside a mounted Google Drive, which we'll get to soon).

The file listings in the Files tab can be slow to update - press the "Refresh" button in the Files tab (it looks like a folder with a circular arrow) to update the listings.

Using Your Google Drive

Google Drive is a cloud file hosting service that you can use to store files remotely, with the free tier allowing your to store up to 15GB of data. Colab has a feature that allows notebooks to directly connect to your Google Drive, so it can easily access any files you store there. The term for connecting a runtime to your Google Drive is called "mounting" your Google Drive. Doing this has the benefits of being able to easily store any files you regularly use - like AI models, or configuration settings - without having to seek out download links / write code cells to add them to your runtimes all the time.

Usually when a notebook is specifically designed to support Google Drive, it will provide its own methods to mount your Google Drive. To mount your Google Drive in notebooks without their own methods, open the "Files" tab, and find one of the options near the top labelled "Mount Drive". Pressing that button will add a code cell that includes the code necessary to mount your drive. Run the code, and give the notebook permission to access your Google Drive when asked. With your drive mounted, you should see the contents of your Google Drive available in the "Files" tab in /content/drive/MyDrive (note: that's the path where it'll be available when using that default code block - the location may differ when using mounting code that somebody else has written for their notebook).

Anything removed, altered, or added to your mounted Google Drive folder while using a notebook will change the actual files in your Google Drive. Be wary about accidentally deleting something, or letting a notebook fill your drive over capacity with files.

Troubleshooting & Things to Note

Here are a few common problems you may run into when using Colab to run AUTOMATIC1111's Web UI:

  • Usage Limits: Remember there are limits to how long you can use GPUs for. If you've reached your limit, you won't be able to use GPUs for an indeterminate number of hours/days.
  • Check-ins: Colab will do checks to ensure your runtime isn't idle. Make sure to check your notebook tab semi-regularly, especially if nothing seems to be happening.
  • ^C error: If your runtime runs out of memory, it can crash, with ^C output in the code cell's log. Try not to do whatever taxing thing you were doing next time (for example, using loads of models at once).
  • Unresponsive Web UI: Sometimes it seems like the A1111 Web UI isn't responding - you press the "Generate" button, but nothing happens. There's two main causes for this:
    • Latency: Sometimes it can take several seconds after pressing the generate button before anything starts happening in the runtime (the worst I've had is something ~30 seconds) - but it is still working! The cell's log in Colab should report any image generation progress if it's working on something, so check there and wait if you're not sure.
    • Locked Up: The tab/UI has legitimately locked up. Personally, I experience this when receiving an image output beyond a certain size. The only solution is to refresh the Web UI's tab (not the Colab one). This will lose any of the generation settings you've input (including prompts), so remember to set everything again, and note them down before refreshing if they're important.

If you're having a problem that's particular to a notebook, you could always try asking the person who wrote the notebook about it!

Absolute tl;dr

Ideally you should read everything to be well informed, but I guess I can break things down into the absolute minimal basics.

  • The files you use in colab are called notebooks. Follow the instructions listed in the notebook to use them.
  • Run a code cell by pressing ️▶️. Stop it by pressing ⏹️.
  • When you start running code, you connect to a runtime, where the code is running on Google's machines. Runtimes are temporary, and will be deleted after a while.
  • If you use runtimes that require GPUs (which you will, for AI generation) too often, you will be made to have to wait before you can use them again/have shorter runtimes.
  • You can open the "Files" tab on the sidebar to access all the files in your runtime.

Using TheLastBen's Notebook

Introduction

TheLastBen's fast stable diffusion notebook is a popular notebook for setting up and running AUTOMATIC1111's Web UI in Google Colab. Being popular, it tends to get fixed quickly when something goes wrong, so is usually up to date. It has it's downsides in it's lack of inbuilt support for some things, but it's fast, and well-supported.

While the notebook is fairly well explained with its cells, we'll go through each one step by step here. This guide assumes you're familiar with the basics of using Google Colab, which you should be if you've read the above guide ;D

This guide is accurate at time of writing, but the notebook is subject to change at any time. If something changes, hopefully you'll be able to translate the information here and apply it to a newer version (or hopefully I'll actually update the guide!).

If you run into issues with the notebook, try searching the github issues page.

Step 0: Connect Google Drive (optional)

The very first code cell is an optional cell to mount your Google Drive. Choosing to run this will change how the rest of the notebook works slightly, which I'll explain.

If you choose to mount your Google Drive, the notebook will install AUTOMATIC1111's Web UI into your Google Drive. This has several helpful advantages: your Web UI settings will be saved between sessions, all your models are available in the same place and don't need to be downloaded again, you can easily use multiple models and addons, and any extensions you choose to download will remain installed rather than having to be reinstalled with every new runtime. The main disadvantage to this is that it necessitates installing everything onto there, which can quickly fill up a free tier Google Drive's 15GB limit, especially if that Google Drive is being used to hold other things!

Not mounting your Google Drive avoids both that disadvantage, but also the advantages too. Every new runtime you'll have to download the model you want to use, configure your Web UI settings, reinstall extensions, and if you want to use more models or addons you have to resort to adding in some custom code.

To make your choice, do one of the following:

  • To mount your drive, run the cell.
  • To mount your drive and use a shared drive, enter the folder's path into Shared_Drive, then run the cell.
  • To not mount your drive, skip running this cell.

If any of the following steps need to change based on your choice, it'll be mentioned.

Step 1: Install/Update AUTOMATIC1111 repo

Run this cell as part of the install process. If the current version of the A1111 Web UI is broken (like it is at time of writing), you can check the Use_Latest_Working_Commit option before running to download an earlier working version instead.

Remember that every new runtime starts off from the same blank slate! You'll need to go through each of the install steps every time you're using a new runtime.

Step 2: Requirements

Run this cell as the next step in the install process.

Step 3: Model Download/Load

Here, we make a choice about what model we want to install / start using. There are 3 different options (separated by "Or" in the cell), which we'll go through in order.

Once you've made your choice and made the appropriate configurations, remember to then run the cell. You can return to this cell again later if you'd like to download / change to another model.

Option 1: Use a Standard Model

This option will download and use one of the base Stable Diffusion models, which you can select from the Model_Version dropdown menu.

This is the default option that'll be used if you don't enter anything in Path_to_MODEL or MODEL_LINK.

Option 2: Provide a Path

This option is only usable if you've mounted your Google Drive (or done some other hackery to load a model into the runtime). You provide it with a file path to the model you want to use, by pasting the file path into Path_to_MODEL. The easiest way to get the file path is to open the file browser (the folder button names "Files" in the sidebar). From there, navigate to the model you want to use, right click it, and select "Copy path".

Default models file location

If you've used this notebook with your Google Drive mounted, then the "official" location for storing models can be found at /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion/.

This option will download a model straight from a download link. Find the direct download link for the model you want to use, and paste it into MODEL_LINK. If it's a safetensors file, you must also check the safetensors box (why doesn't the code handle this automatically? I don't know).

Use_temp_storage is only really relevant if you've mounted your Google Drive. With that option checked, the downloaded model won't be stored in your Google Drive. Without it checked, the model will be stored in your Google Drive, though annoyingly it's always renamed to model, so it's not clear what the model is.

Civitai downloads

You can get direct download links to models hosted on Civitai by right clicking the blue download link and selecting "Copy link address".

Extra

Checking the Redownload_the_original_model checkbox will forcibly delete a model previously loaded using Option 1 or Option 3, so you can install a different model via those options during the same runtime.

Step 4: ControlNet (optional)

This cell is optional, and can be skipped if you're not interested in using ControlNet. Even if you do skip it, you should be able to come back and run this later in the runtime to install everything. It requires an A1111 Web UI extension to utilise (sd-webui-controlnet), however if you run this cell the notebook will automatically install the extension (you may still have to enable it in the Web UI's extensions tab before you can use it, however).

ControlNet is an optional addon that provides different ways to guide your generations - like putting characters into particular poses - based on images your provide it. I've got a quick and basic guide about ControlNet's basic features and settings if you want to learn more about it.

To install the ControlNet models (not to be confused with the regular models you use for image generation), choose an option from the Model dropdown list, then run the cell (you can repeat these two steps to install multiple ControlNet models). Most options in the dropdown list are for single ControlNet models, with two exceptions:

  • T2iadapter_Models will install all the alternative T2iadapter models. They fulfil basically the same uses as the ControlNet models, and can be used the same way as them. For the most part, consider them alternative options to the ControlNet models, however there are some things they can do that the normal ControlNet models don't have options for - most interesting is the style adaptor.
  • All will install all of the ControlNet models, and T2iadapter_Models models.

Google Drive

If you have mounted your Google Drive, do be aware of the models taking up space. Each ControlNet model is ~730MB in size, and each T2iadapter model is ~155MB. Unlike previous steps, this notebook doesn't allow you to use temporary storage to store these models - they must go into your Google Drive if you've mounted it.

ControlNet models have different versions

There are different versions of the ControlNet models, depending on if the model you intend to use them with is based on Stable Diffusion 1, or Stable Diffusion 2! If you are intending to use a Stable Diffusion 2 based model, you should download ControlNet models made for that version via the v2_Model dropdown instead of the Model dropdown (or both, if you're planning on using multiple models, some of which are based on Stable Diffusion 1, while others on 2).

Step 5: Start Stable-Diffusion

Running this cell will start AUTOMATIC1111's Web UI. When it's ready, it'll post a link in the cell's log. Click on that link to access the Web UI and get generating! If for any reason you need to stop this cell and run it again, do remember that running this again will give you a new, different link that you must use.

In order to be able to connect to the Web UI that's being run in your runtime, notebooks needs to use a special service to do so. This notebook has three options for those services (that you don't really need to know any of the nitty-gritty details about): Gradio, Cloudflare, and Ngrok. Generally the only reason you'll need to consider switching between them is when you're currently experiencing problems with one of the services (which can happen from time to time).

There are 3 options, but again, you'll only really have to consider switching from the default option if something isn't working:

  • Cloudflare: The service that runs when Use_Cloudflare_Tunnel is checked, which it is by default.
  • Gradio: The service that runs if you're not using the others. In my experience, the least reliable.
  • Ngrok: In order to use Ngrok, you need to create an Ngrok account. Once you have an account, you can copy your Authtoken from here into Ngrok_token. When you run the cell after inputting an Ngrok_token, the service you'll be using is Ngrok.

Always add credentials!

User and Password are for setting up extra security when using any of these services (don't be confused by the notebook mentioning Gradio specifically). Don't treat this as optional! Without this, anyone who finds or guesses your link will be able to access your runtime, use it to generate images, and potentially access the files stored there (which is especially relevant if you've mounted your Google Drive!). Add a username and password here, which you will then use to access the Web UI after following the link the cell gives you.

Remember that only one cell can be running at a time! If you need to go back and run a previous cell (for instance, if you wanted to add more ControlNet models), you will need to stop this cell running. Once you have run the other cells you needed to, return to this once and run it again. The link to reconnect to the Web UI will be different than the one you used before.

Bonus Tips: Using Addons (VAEs, LoRAs, Textual Inversions, etc.)

While the notebook doesn't provide any proper interface for installing additional things like VAEs and LoRAs, it isn't that difficult to install them yourself. VAEs are especially important to know how to install, as some models are dependant on them! We'll go through a few different ways that you could add them to your runtime.

Custom Code Cell

I've prepared a block of custom code that you can add in as a code cell to the notebook. Create a new code block and paste the contents of the file into it. Enter the details of what you want to add to your runtime and run the code block, repeating for each thing you want to add. As with the code blocks before, we'll go through the configuration options, in case they aren't obvious.

Mounted Google Drives

If you've mounted your Google Drive, anything you download with this will go into there! This means that they'll take up your storage space, but will be available for any future sessions without you having to download them again.

Set Paths

Usually, you'd write out the paths for where each sort of file needs to be installed, but for this notebook you don't have to - the paths already written in there are for this notebook!

Load Element

This is where you choose what you want to load into your runtime, by giving it an internet download link to the file you want to load. In the file_type_to_dl dropdown, select the type of file you're planning to load, and then in link_to_file paste in the direct download link to that file. Run the cell, and it'll download the file into your runtime, ready to be used!

Repeat this step for each file you want to add.

Manually Installing

This method tends to be slower and less reliable than the other method, but I'll explain it regardless. You can manually download the files yourself, then make them available to your runtime using either Colab's file browser, or placing them into your Google Drive that you've mounted to your runtime. Here is a list of where you should place files of each type:

  • Models: /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Stable-diffusion
  • VAEs: /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/VAE
  • LoRAs: /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/Lora
  • Textual Inversion Embeddings: /content/gdrive/MyDrive/sd/stable-diffusion-webui/embeddings
  • Hypernetworks: /content/gdrive/MyDrive/sd/stable-diffusion-webui/models/hypernetworks

When manually uploading files in the Colab's file browser, some types of files may be too big to use. If you encounter that problem, you'll have to use other methods.

Edit
Pub: 17 Mar 2023 14:52 UTC
Edit: 02 Apr 2023 08:25 UTC
Views: 2377