All Products
Search
Document Center

Platform For AI:Quickly deploy Stable Diffusion for text-to-image generation in EAS

Last Updated:May 28, 2024

Stable Diffusion is an open source deep learning model that can generate images based on text prompts with high-performance at low computing costs. Elastic Algorithm Service (EAS) of Platform for AI (PAI) provides the stable-diffusion-webui image that allows you to deploy a Stable Diffusion model as an AI-powered web application and easily operate the model service on the web application interface. This topic describes how to deploy Stable Diffusion as a web application in EAS.

Deploy the Stable Diffusion model

  1. Go to the EAS-Online Model Services page.

    1. Log on to the PAI console.

    2. In the left-side navigation pane, click Workspaces. On the Workspaces page, click the name of the workspace to which you want to deploy the model.

    3. In the left-side navigation pane, choose Model Deployment>Elastic Algorithm Service (EAS) to go to the Elastic Algorithm Service (EAS) page. image

  2. On the Elastic Algorithm Service (EAS) page, click Deploy Service. In the dialog box that appears, select Custom Deployment and click OK.

  3. On the Create Service page, configure the parameters. The following table describes the parameters. Use the default settings for other parameters.

    Parameter

    Example

    Description

    Model Service Information

    Service Name

    sdwebui_demo

    The name of the service. The name must be unique in the region.

    Deployment Method

    Deploy Web App by Using Image

    The deployment mode of the service.

    Select Image

    • Image type: PAI Image

    • Image name: stable-diffusion-webui

    • Image version: 4.2-standard

    Select a stable-diffusion-webui image. In this example, the image version 4.2-standard is used. You can select an image based on your business requirements.

    Select I have read and agree to Machine Learning Platform for AI Terms of Service.

    Command to Run

    • Command: ./webui.sh --listen --port 8000 --skip-version-check --no-hashing --no-download-sd-model --skip-prepare-environment --api --filebrowser

    • Port number: 8000

    After you complete the preceding configurations, the system automatically generates the command.

    Resource Deployment Information

    Resource Group Type

    Public Resource Group

    Select the resources used to deploy and run the service.

    Resource Configuration Mode

    General

    In this example, General is selected.

    Resource Configuration

    • Resource type: GPU

    • Instance type: ml.gu7i.c16m60.1-gu30

    Select a GPU-accelerated instance type. In this example, ml.gu7i.c16m60.1-gu30 is used. This instance type can meet the requirements in this example and achieve cost-effectiveness.

    Extra System Storage

    Additional System Disk: 100 GB

    The memory size of the system disk that you want to add.

    After you complete the preceding configurations, a JSON file is automatically generated in the Configuration Editor section.

  4. Click Deploy. The model deployment requires approximately five minutes.

Use the web application to perform model inference

  1. Find the service that you deployed and click View Web App in the Service Type column.

  2. Perform model inference.

    In the Prompt section of the txt2img tab, enter your prompt and click Generate. In this example, cute dog is used as a prompt. The following figure shows the inference result.

    image

FAQ

What do I do if the image requires a long period of time to generate?

  1. On the Elastic Algorithm Service (EAS) page, find the service that you deployed and click Update Service in the Actions column.

  2. To accelerate the model service, modify the Command to Run parameter to enable Blade or xFormers.

    image

    Acceleration method

    Description

    Blade

    Blade is an acceleration tool provided by PAI that can accelerate model service by up to 3.06 times. The actual acceleration varies based on the image size and the number of iteration steps. You can enable Blade to improve performance and reduce latency.

    Sample command: ./webui.sh --listen --port=8000 --blade

    xFormers

    xFormers is an open-source acceleration tool provided by the Stable Diffusion web application that can be used to accelerate various models.

    Sample command: ./webui.sh --listen --port=8000 --xformers

  3. Click Update.

How do I use my model and output directory?

If you want to use your model or Object Storage Service (OSS) bucket, such as when downloading a model from the open source community or training a model such as Low-Rank Adaptation of Large Language Models (LoRA) or Stable Diffusion for use in the Stable Diffusion web application, you can mount your file by using the following method. You may also want to save output data to your OSS bucket directory, configure specific third-party library files, or install plug-ins.

  1. Create an OSS bucket. For more information, see Get started by using the OSS console.

  2. On the Elastic Algorithm Service (EAS) page, find the service that you deployed and click Update Service in the Actions column.

  3. In the Model Service Information section, click Specify Model Settings, and then configure the Model Settings and Command to Run parameters.

    image

    Parameter

    Example

    Description

    Model Settings

    • Mount type: Mount OSS Path

    • OSS path: the path that you created in Step 1. Example: oss://bucket-test/data-oss/

    • Mount Path: /code/stable-diffusion-webui/data-oss

    • Enable Read-only Mode: turn off the switch.

    In the Model Settings section, specify an OSS path and a mount path.

    Command to Run

    • Command: ./webui.sh --listen --port=8000 --data-dir data-oss

    • Port number: 8000

    Append the following option to the command:

    -- data-dir: data-oss. The directory must be the same as the last-level directory of the Mount Path in the Model Settings section.

  4. Click Update. The following figure shows the directory automatically created by PAI in the empty OSS directory you specified. After the service is started, we recommend that you upload data to the specified directory.

    image

  5. After the OSS directory is created, you can upload the model that you downloaded or trained to the specified directory in the models section. Click image>Restart Service in the Actions column. The configuration takes effect after the service is restarted.

  6. After the configuration takes effect, select the model from the Stable Diffusion Model (ckpt) drop-down list on the web application interface. Then, you can use the selected model to perform model inference.

    image

What do I do if the service freezes for a long period of time?

  • You can reopen the Stable Diffusion web application interface or restart the EAS service.

    • Find the service that you want to manage and click View Web Application in the Service Type column to reopen the web application page.

    • Click image>Restart Service in the Actions column of the service to restart the EAS service.

  • If the issue persists, the service may need to download models or plug-ins from the Internet. By default, EAS is not connected to the Internet. You can load an image or mount a model offline. However, you may need Internet connection to download specific plug-ins. In this case, we recommend that you find the download path of the model or plug-in by viewing the service logs, download the model, and then upload and mount the model to OSS. For more information, see How do I use my model and output directory?. If you require Internet connection, you can configure network settings. For more information, see Configure network connectivity and Configure Internet access and a whitelist.

How do I enable multiple users to call the service by using the same web application at the same time?

The preceding deployment method supports only an individual user. If multiple users access the Stable Diffusion web application at the same time, incompatibility issues may occur. If you want to enable multiple users to access the Stable Diffusion web application at the same time, you can deploy the cluster edition of the Stable Diffusion web application in EAS. You need to only select the stable-diffusion-webui:4.2-cluster-webui image when you deploy the service. To ensure the speed and stability of AI image generation in multi-user scenarios, we recommend that you use multiple service instances.

image

The cluster edition has the following benefits:

  • Multiple users can independently access the web application by using the same URL.

  • Multiple users can share one GPU, and the service scales based on the actual usage. This improves cluster utilization and cost-effectiveness.

  • The application can integrate with the enterprise account system to distinguish the image generation models, image results, and plug-ins of each user.

How do I change the default language of the web application interface to English?

  1. On the web application interface, click 设置.

  2. In the left-side navigation pane, click 用户界面. In the lower part of the 本地化 page, select .

  3. In the upper part of the web application interface, click 保存设置 to save the configuration. Then, click 重启前端 to restart the web application.

    Refresh the web application interface. The page is displayed in English.

How do I manage my file system?

  1. Make sure that you mounted an OSS path. For more information, see the How do I use my model and output directory? section of this topic. When you create or update a service, you can append the --filebrowser option to the Command to Run field. Example: ./webui.sh --listen --port=8000 --blade --data-dir data-oss --filebrowser.

    image

  2. After the service is deployed, click View Web App in the Service Type column.

  3. On the web application interface, click the FileBrowser tab. The file system page is displayed. You can view the file system, upload on-premises files to the file system, or download files to your on-premises computer.

    image

References