This topic shows how to use DLC compute resources for a hyperparameter tuning experiment to train a LoRA model and find its optimal configuration.
Prerequisites
-
If you are using AutoML for the first time, grant the required permissions. For more information, see Cloud product dependencies and authorization: AutoML.
-
You have granted the required permissions to DLC. For more information, see Cloud product dependencies and authorization: DLC.
-
You have created a workspace and associated it with a public resource group. For more information, see Create and manage workspaces.
-
You have activated Object Storage Service (OSS) and created an OSS bucket. For more information, see Quick start.
Step 1: Create a dataset
Create an OSS dataset to mount an OSS directory to a DLC path. This path stores the data files from the hyperparameter tuning experiment. Configure the following key parameters and keep the default values for others. For more information, see Create and manage datasets.
-
Dataset Name: Enter a custom name for the dataset.
-
Select Data Storage: Select the OSS directory that contains the script file.
-
Property: Select Property.
Step 2: Create an experiment
Go to the New Experiment page and configure the key parameters. For more information about how to configure other parameters, see New Experiment. After you configure the parameters, click Submit.
-
Configure execution settings.
Parameter
Description
Job type
Select DLC.
Resource Group
Select Public Resource Group.
Framework
Select Tensorflow.
Datasets
Select the dataset created in Step 1.
Node Image
Select Image Address and enter
registry.cn-shanghai.aliyuncs.com/mybigpai/nni:diffusersin the text box.The image contains the following pre-configured data:
-
Pre-trained foundation model: The Stable-Diffusion-V1-5 model is pre-configured in the image path
/workspace/diffusers_model_data/model. -
LoRA training data: The pokemon dataset is pre-configured in the image path
/workspace/diffusers_model_data/data. -
Training code: The diffusers library is pre-configured in the image path
/workspace/diffusers.
Instace Type
Select GPU >
12vCPU+92GB Mem+1*NVIDIA V100 ecs.gn6e-c12g1.3xlarge.Nodes
Set the value to 1.
Startup Command
cd /workspace/diffusers/examples/text_to_image && accelerate launch --mixed_precision="fp16" train_text_to_image_lora_eval.py \ --pretrained_model_name_or_path="/workspace/diffusers_model_data/model" \ --dataset_name="/workspace/diffusers_model_data/data" \ --caption_column="text" \ --resolution=512 --random_flip \ --train_batch_size=8 \ --val_batch_size=8 \ --num_train_epochs=100 --checkpointing_steps=100 \ --learning_rate=${lr} --lr_scheduler=${lr_scheduler} --lr_warmup_steps=0 \ --rank=${rank} --adam_beta1=${adam_beta1} --adam_beta2=${adam_beta2} --adam_weight_decay=${adam_weight_decay} \ --max_grad_norm=${max_grad_norm} \ --seed=42 \ --output_dir="/mnt/data/diffusers/pokemon/sd-pokemon_${exp_id}_${trial_id}" \ --validation_prompts "a cartoon pikachu pokemon with big eyes and big ears" \ --validation_metrics ImageRewardPatched \ --save_by_metric val_lossHyperparameter
Configure each hyperparameter with the following constraint type and search space:
-
lr:
-
Constraint type: choice.
-
Search space: Click
and add three enumeration values: 1e-4, 1e-5, and 2e-5.
-
-
lr_scheduler:
-
Constraint type: choice.
-
Search space: Click
and add three enumeration values: constant, cosine, and polynomial.
-
-
rank:
-
Constraint type: choice.
-
Search space: Click
and add three enumeration values: 4, 32, and 64.
-
-
adam_beta1:
-
Constraint type: choice.
-
Search space: Click
and add two enumeration values: 0.9 and 0.95.
-
-
adam_beta2:
-
Constraint type: choice.
-
Search space: Click
and add two enumeration values: 0.99 and 0.999.
-
-
adam_weight_decay:
-
Constraint type: choice.
-
Search space: Click
and add two enumeration values: 1e-2 and 1e-3.
-
-
max_grad_norm:
-
Constraint type: choice.
-
Search space: Click
and add three enumeration values: 1, 5, and 10.
-
This configuration generates 648 hyperparameter combinations. The experiment creates a trial for each combination and runs the training script with that specific set.
-
-
Configure trial settings.
Parameter
Description
Metric type
Select stdout.
Calculation method
Select best.
Metric weight
-
key: val_loss=([0-9\\.]+).
-
Value: 1.
Metric source
Set the value to cmd1.
Optimization direction
Select Larger is better.
-
-
Configure search settings.
Parameter
Description
Search algorithm
Select TPE.
Maximum number of searches
Set the value to 5.
Maximum concurrency
Set the value to 2.
Enable earlystop
Turn on the switch.
Start step
5
Step 3: View experiment details and results
-
On the Experiment List page, click the name of your experiment to go to the Experiment Details page. On the Basic information tab, you can view the Basic settings, Trial status, Trial configuration, and Search configurations of the experiment.
On this page, you can view the progress and status of the trials. The experiment automatically creates five trials based on the configured search algorithm and maximum number of searches.
-
Click the Trials tab. On this tab, you can view a list of all trials automatically generated for the experiment, along with the execution status, final metric, and hyperparameter combination of each trial. This page displays the hyperparameter values and final metric values for each trial in a table. This helps you compare the effectiveness of different hyperparameter combinations.
Each trial runs for approximately five hours. Based on the configured optimization direction (Larger is better), the hyperparameter combination that yields a final metric of 0.087655 is optimal.
Step 4: Deploy service and run inference
-
Download the LoRA model and convert its file format.
-
After the experiment is complete, the model file is generated in the
output_dirdirectory specified in the startup command. You can go to thecheckpoint-bestdirectory in the OSS path that is mounted to the dataset configured for the experiment to view and download the model file. For more information, see Get started with the console. Thepytorch_model.binfile is the model weight file to be converted in the next step. -
Run the following commands to convert
pytorch_model.bintopytorch_model_converted.safetensors.wget http://automl-nni.oss-cn-beijing.aliyuncs.com/aigc/convert.py python convert.py --file pytorch_model.bin
-
-
Deploy the Stable Diffusion WebUI service.
-
Go to the Elastic Algorithm Service (EAS) page. For more information, see Custom Deployment.
-
On the Elastic Algorithm Service (EAS) page, click Deploy Service. In the Custom Model Deployment section, click Custom Deployment.
-
On the Create Service page, configure the following key parameters and click Deploy.
Parameter
Description
Service Name
Enter a custom service name. For example, sdwebui_demo.
Deployment Method
Select Deploy Web App by Using Image.
Select Image
In the PAI Image list, select stable-diffusion-webui and set Image Version to 4.2-standard.
NoteDue to frequent version updates, we recommend selecting the latest image version during deployment.
Model Settings
Click Specify Model Settings to configure the model.
-
In the Model Settings section, select OSS, and set the OSS path to the OSS Bucket path you created in Step 1. For example:
oss://bucket-test/data-oss/. -
Mount Path: Mount the configured OSS file directory under the
/code/stable-diffusion-webuipath in the image. For example, set the path to/code/stable-diffusion-webui/data-oss. -
Enable Read-Only: Turn off the switch.
Command to Run
The system automatically populates the command after you configure the image. You must then add
--data-dir <mount_directory>to the command, where<mount_directory>must be the same as the last directory name of the Mount Path in the Model Settings section. In this example, add--data-dir data-ossto the end of the command.Resource Configuration Mode
Select General.
Resource Configuration
Select a GPU type. For optimal cost-effectiveness, set Instance Type to ml.gu7i.c16m60.1-gu30.
Extra System Storage
Set the extra system disk to 100 GB.
-
-
Click Deploy.
PAI automatically creates a directory structure and copies necessary data to the empty OSS directory that you specified. In the data-oss directory, PAI creates the configs, extensions, log, models, outputs, and progress_images subdirectories, as well as the cache.json and ui-config.json configuration files.
-
-
Upload the model files to the specified paths. Then, in the Actions column of the target service, click the Actions icon and choose Restart Service to apply the changes.

-
Upload the
pytorch_model_converted.safetensorsmodel file that you generated in the previous steps to themodels/lora/directory in OSS. -
Upload the revAnimated_v122 foundation model to the
models/Stable-diffusion/directory in OSS.
-
-
In the Service Type column of the target service, click View Web App to open the WebUI and validate the model by running inference.