All Products
Search
Document Center

Platform For AI:DLC LoRA training best practices

Last Updated:Sep 09, 2026

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

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.

  1. 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:diffusers in 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_loss 

    Hyperparameter

    Configure each hyperparameter with the following constraint type and search space:

    • lr:

      • Constraint type: choice.

      • Search space: Click image.png and add three enumeration values: 1e-4, 1e-5, and 2e-5.

    • lr_scheduler:

      • Constraint type: choice.

      • Search space: Click image.png and add three enumeration values: constant, cosine, and polynomial.

    • rank:

      • Constraint type: choice.

      • Search space: Click image.png and add three enumeration values: 4, 32, and 64.

    • adam_beta1:

      • Constraint type: choice.

      • Search space: Click image.png and add two enumeration values: 0.9 and 0.95.

    • adam_beta2:

      • Constraint type: choice.

      • Search space: Click image.png and add two enumeration values: 0.99 and 0.999.

    • adam_weight_decay:

      • Constraint type: choice.

      • Search space: Click image.png and add two enumeration values: 1e-2 and 1e-3.

    • max_grad_norm:

      • Constraint type: choice.

      • Search space: Click image.png 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.

  2. 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.

  3. 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

  1. 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.

  2. 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

  1. Download the LoRA model and convert its file format.

    1. After the experiment is complete, the model file is generated in the output_dir directory specified in the startup command. You can go to the checkpoint-best directory 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. The pytorch_model.bin file is the model weight file to be converted in the next step.

    2. Run the following commands to convert pytorch_model.bin to pytorch_model_converted.safetensors.

      wget http://automl-nni.oss-cn-beijing.aliyuncs.com/aigc/convert.py
      python convert.py --file pytorch_model.bin
  2. Deploy the Stable Diffusion WebUI service.

    1. Go to the Elastic Algorithm Service (EAS) page. For more information, see Custom Deployment.

    2. On the Elastic Algorithm Service (EAS) page, click Deploy Service. In the Custom Model Deployment section, click Custom Deployment.

    3. 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.

      Note

      Due 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-webui path 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-oss to 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.

    4. 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.

  3. 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.image

    • Upload the pytorch_model_converted.safetensors model file that you generated in the previous steps to the models/lora/ directory in OSS.

    • Upload the revAnimated_v122 foundation model to the models/Stable-diffusion/ directory in OSS.

  4. In the Service Type column of the target service, click View Web App to open the WebUI and validate the model by running inference.