Create a fine-tuning job for image generation models. Datasets can be uploaded via the API or mounted from OSS.
Prerequisites
- Supported regions: The features described in this document are only available in the Singapore region. You must use an API Key from this region.
- Account permissions: If you use an Alibaba Cloud sub-account (RAM user), you need to grant the sub-account model invocation, training, and deployment permissions.
- Configure environment variables: You have successfully obtained an API Key and configured it as an environment variable.
- Preparation: You have read Fine-tune image generation models to understand the models that support fine-tuning, fine-tuning steps, data formats, and billing instructions.
Create a Fine-tuning Job
- Singapore
POST https://dashscope-intl.aliyuncs.com/api/v1/fine-tunesFor Windows CMD, replace$DASHSCOPE_API_KEYwith%DASHSCOPE_API_KEY%. For PowerShell, replace it with$env:DASHSCOPE_API_KEY
Request parametersHeadersContent-Typestring (Required)Fixed value: application/jsonAuthorization string (Required)API Key authentication. Format: Bearer sk-xxxx.Request Bodymodelstring (Required)Specifies the base model for fine-tuning.
array[string] (Conditionally required)Array of training dataset file IDs. Multiple IDs can be passed. Mutually exclusive with training_datasets . If training_datasets is used, this parameter is not required. File IDs are obtained through the Upload File API.validation_file_ids array[string] (Optional)Array of validation dataset file IDs. Multiple IDs can be passed. Mutually exclusive with validation_datasets . If validation_datasets is used, this parameter is not required. If neither is provided, the system will automatically split from the training set. File IDs are obtained through the Upload File API.training_type string (Required)Fine-tuning type. Currently only supports efficient_sft (LoRA efficient fine-tuning).hyper_parameters object (Optional)Hyperparameter configuration. For initial training, it is recommended to use the default hyperparameters. If the model performance is poor or training does not converge, try adjusting parameters such as max_steps or learning_rate.
Hyperparameter properties max_steps int (Required)Total training steps. The core parameter that controls training duration. Recommended value: 800.max_steps determines the number of training iterations, while max_token_length determines the data volume processed per step.It is recommended to use at least 500 steps to ensure the model converges sufficiently. You can increase the steps for larger datasets.eval_steps int (Required)Validation interval. Recommended value: 200. The value must be >= 0. Specifies how many steps between each validation evaluation during training, used for periodic assessment of model training progress. The model file at the current step is also saved.learning_rate float (Required)Learning rate. Recommended value: 3e-5. Controls the magnitude of model weight updates. Too high may degrade the model, while too low may result in insignificant changes.generation_type string (Required)Generation mode. Determines the training data format and inference method. Valid values:
string (Required)Maximum resolution of training images. Sets the upper limit for the total pixel count (width x height) of images in the training set. The system only scales images that exceed this value; images within the limit remain unchanged. It is recommended to keep the three resolution parameters (max_pixels, max_token_length, val_img_size) consistent.
string (Required)Validation image generation resolution. The target resolution for images generated during validation evaluation in the training process. Valid values are the same as max_pixels. Recommended value: 2k for text-to-image, 1k for image-to-image.max_token_length string (Required)Maximum token length per training step, affecting per-step computation and billing. Together with max_steps, it controls the training process: max_steps determines the number of iterations, while max_token_length determines the data volume per step. Valid values:
float (Required)Gradient clipping. Recommended value: 0.5. The threshold for global gradient norm clipping on all trainable parameters to prevent gradient explosion. Set to -1 to disable clipping.weight_decay float (Required)Weight decay. Recommended value: 0.02. The AdamW decoupled weight decay coefficient, applied to all trainable parameters for regularization to prevent overfitting.lora_rank int (Required)The rank of LoRA low-rank matrices. Recommended value: 32. This value determines the number of fine-tuning parameters. A larger value provides stronger model fitting capability but slower training speed. The value must be a power of 2n (e.g., 16, 32, 64).save_total_limit int (Optional)Maximum number of checkpoints to save. Recommended value: 10. Limits the maximum number of saved models. The system will always keep only the last N checkpoints generated during training.split float (Optional)Training set split ratio. Recommended value: 0.9. Value range: (0, 1). Only takes effect when neither validation_file_ids nor validation_datasets is specified. This parameter is used to automatically split a portion from the training set as the validation set. For example, 0.9 means 90% training set and 10% validation set.Array of Dataset (Conditionally required)Training dataset file list. Mutually exclusive with training_file_ids . If training_file_ids is used, this parameter is not required.
Dataset structure data_source_type string (Required)Data source type. Valid values:
object (Conditionally required)Required when data_source_type is oss_mount . OSS mount information.
Properties region string (Required)The region of the OSS Bucket to mount. Supports Beijing (cn-beijing) and Singapore (ap-southeast-1).bucket string (Required)The name of the OSS Bucket to mount.file_path string (Required)The OSS file path (object key) to mount. For datasets containing multiple files, use the file path of data.jsonl. Unlike the file_id approach, you need to upload the entire uncompressed dataset folder to OSS. Zip files are not supported.string (Conditionally required)Required when data_source_type is file_id . File ID, generated by the Upload File API.Array of Dataset (Optional)Validation dataset file list. Structure is the same as training_datasets.Mutually exclusive with validation_file_ids. If neither is provided, the system will automatically split from the training set.job_name string (Optional)The name of the tuning job.model_name string (Optional)The name of the model after tuning is complete. |
Response parametersrequest_idstringThe unique identifier of the request.output objectJob details.
Properties job_id stringThe unique identifier of the fine-tuning job, used to query job details, logs, cancel, or delete the job. Generation rule: ft-{yyyyMMddHHmm}-{4-digit uuid}.job_name stringThe name of the fine-tuning job.status stringThe status of the fine-tuning job:
stringThe ID of the new model produced after fine-tuning, used for deployment and invocation. Returned when the job status is SUCCEEDED.model stringThe base model used.base_model stringThe base model used.training_file_ids arrayA legacy field for backward compatibility. New jobs always return an empty array. Please use training_datasets.training_datasets Array of DatasetThe list of training datasets.validation_file_ids arrayA legacy field for backward compatibility. New jobs always return an empty array. Please use validation_datasets.validation_datasets Array of DatasetThe list of validation datasets. Returns an empty array if no validation set is specified.hyper_parameters objectThe hyperparameters actually used.training_type stringThe training method for model fine-tuning.create_time stringThe job creation time.end_time stringThe job end time. Returned when the job status is SUCCEEDED, FAILED, or CANCELED.usage integerThe number of tokens consumed by the fine-tuning job. Returned when the job status is SUCCEEDED or CANCELED.workspace_id stringThe workspace ID associated with the Alibaba Cloud Model Studio API Key. See Get the Workspace ID.user_identity stringUser identity, Alibaba Cloud account ID.creator stringThe Alibaba Cloud account ID of the creator.modifier stringThe Alibaba Cloud account ID of the modifier.group stringThe fine-tuning job group.max_output_cnt integerThe maximum number of checkpoints saved during training. Equals the value of the save_total_limit hyperparameter.stringError code. Returned when the call fails. See the error code table below.message stringDetailed error description. Returned when the call fails. |
Key fields: output.job_id (Job ID), output.finetuned_output (The name of the new model produced after fine-tuning). |
Error codes
If the call fails and returns an error message, refer to the following table for troubleshooting.
HTTP status code | Error code | Solution |
|---|---|---|
400 | InvalidParameter | Parameter error, missing parameter or parameter format issue. Correct your parameters based on the error message. |
400 | UnsupportedOperation | The resource cannot be operated on when it is in a specific state. Wait until the resource reaches an operable state before performing the operation. |
404 | NotFound | The resource to query/operate does not exist. Check if the resource ID is incorrect. |
409 | Conflict | A deployment instance with the same name already exists. Specify a suffix to differentiate. |
429 | Throttling | Resource creation has triggered a platform limit. Delete models that are no longer in use. |
500 | InternalError | Internal error. Record the request_id and contact Alibaba Cloud engineers via a ticket for troubleshooting. |