Prerequisites
- Choose one of the models supported for fine-tuning.
- **Create a dataset **for training. You can optionally create an additional dataset for validation. Split the data between two datasets as 80–90% for training and 10–20% for validation. Requirements for validation datasets are the same as for training datasets.
- Create an API key for authentication.
-
Save the API key to an environment variable:
How to fine-tune a model
-
Using Python : Install openai package:
-
Import essential librarires
-
Set up Nebius API key
-
Upload a training and a validation dataset. The validation dataset is optional.
-
Configure Fine-tuning parameters.
For more information about the tuning job parameters, see the specification of the fine-tuning job object.
-
Create and run the finetuning job.
-
Checks that the job status.
The status of a freshly started job is
running
. The script polls the status periodically to make sure that the job status has changed tosucceeded
. The minimum time window between subsequent polls is 15 seconds. If the status isfailed
, examine the output. It describes the error and how to fix it. If the error code is500
, resubmit the job. Checks that the training has been successful. To do this, check the job events. They are created when the job status changes. You can consider the training as finished if the response contains either theDataset processed successfully
orTraining completed successfully
message. -
Retrieves the contents of the files with the fine-tuned model.
You get the files for every fine-tuning checkpoint. A checkpoint is created after every epoch of training a model, so you get intermediate results of the training. If you need final results, use the files from the last checkpoint. Saves the contents to files. The script creates a directory per checkpoint and saves the files into these directories.
API specification for a fine-tuning job
The object below represents the fine-tuning job specification used in the API.-
model
(string, required): Model to fine-tune. -
suffix
(string, optional): Suffix added to the model name (for example,my-model
ormy-experiment
). It helps you differentiate between fine-tuned models in their list. -
training_file
(string, required): ID of the file with the training dataset. For more information about how to prepare and upload datasets and how to get their IDs, see the following instructions: -
validation_file
(string, optional): ID of the file with the validation dataset. -
hyperparameters
(object, optional): Fine-tuning parameters:-
batch_size
(integer, optional): Number of training examples used in a batch for fine-tuning. A bigger batch size works better with bigger datasets. From8
to32
. Default:8
. -
learning_rate
(float, optional): Learning rate for training. If you train a model in a domain in which the model has not been trained before, you may need a higher learning rate. Greater or equal to0
. Default:0.00001
. -
n_epochs
(integer, optional): Number of epochs to train on the dataset. An epoch is a cycle of going through the whole dataset for training. For example, if the number of epochs is10
, the model is trained on a given dataset 10 times. From1
to20
. Default:3
. -
warmup_ratio
(float, optional): Percentage by which the learning rate should increase from the beginning of training. From0
to1
. Default:0
. -
weight_decay
(float, optional): Weight decay value. Weight decay is a regularization technique that adds a penalty to the loss function and keeps fine-tuning weights small. This approach prevents overfitting and preserves generalization, so it is better suited for larger models or more complex tasks. Greater or equal to0
. Default:0
. -
lora
(boolean, optional): Whether to enable LoRA (Low-Rank Adaptation) for training. The LoRA method presumes that low-rank matrices are inserted into a pre-trained model. These matrices catch task-specific data during the training. As a result, you only train these matrices; you do not need to retrain the whole model and modify any preset fine-tuning parameters. Iffalse
, full fine-tuning is performed. Default:false
. -
lora_r
(integer, optional): Rank for weights of LoRA adapters. A larger rank captures more pre-existing model weights for training. Eventually, the model is trained better, especially if it is trained for a task for which it has not been trained before. However, ranking too high can cause overfitting. From8
to128
. Default:8
. -
lora_alpha
(integer, optional): Alpha value for training LoRA adapters. This parameter balances the influence of low-rank LoRA matrices on pre-existing model weights. If only a slight adjustment of a model is required, use a lower value. Greater or equal to8
. Default:8
. -
lora_dropout
(float, optional): LoRA dropout rate. LoRA dropout is a regularization technique that randomly omits a fraction of the model’s LoRA parameters during training. As a result, this technique helps avoid overfitting on the dataset, especially in cases when the dataset is small and the model should suit more general tasks. From0
to1
. Default:0
. -
packing
(boolean, optional): Whether to use packing for training. With packing enabled, you can combine multiple small samples in a batch instead of having one sample per batch. This increases training efficiency. Default:true
. -
max_grad_norm
(float, optional): Maximum gradient norm value used for gradient clipping. Make sure that the value is not too large or small:- A value that is too large causes the vanishing gradient problem. It happens when weight gradients become too small during backpropagation. As a result, the model cannot learn quickly enough.
- A value that is too small causes the exploding gradient problem, which is the opposite to the vanishing gradient problem. Explosion happens when weight gradients get large. As a result, it leads to unstable and unoptimized training.
0
. Default:1
.
-
-
seed
(integer, optional): Control of the LLM output reproduction. If you pass along the sameseed
in different requests, you achieve approximately the same results. If you use the same seed but different values of other parameters, the results of your requests might differ. -
integrations
(array, optional): Integrations that Nebius AI Studio supports for fine-tuning:-
type
(string, optional): Integration type. The possible values are the following:wandb
: Integration with Weights & Biases.
-
wandb
(object, optional): Settings for the export to a project in Weights & Biases:api_key
(string, optional): API key from Weights & Biases. The key should be 40 characters long.project
(string, optional): Name of the project in Weights & Biases.
-