- Resources
- Deprecated features
- Fine-tuning
Text & Vision Fine-tuning
Text & Vision Fine-tuning
Deprecated
This feature is deprecated and is no longer actively supported.
Fine-tuning allows you to tailor a pre-trained language model to your specific needs by training it on your dataset. This guide explains how to fine-tune text and vision models, from preparing your data to training, whether you aim to improve domain-specific understanding or adapt to a unique conversational style.
You can both finetune directly in the AI Studio or via our API.
Before You Start
Dataset
To fine-tune a model, you need to provide a dataset that contains the data you want to train on, it is also recommended to have a validation dataset and a test dataset.
The dataset must be in a specific format, and you can upload it to the Mistral Cloud before launching the fine-tuning job.
Data must be stored in JSON Lines (.jsonl) files, which allow storing multiple JSON objects, each on a new line.
SFT Datasets should follow an instruction-following format representing a user-assistant conversation. Each JSON data sample should either consist of only user and assistant messages or include function-calling logic.
Conversational text only data between user and assistant, which can be one-turn or multi-turn.
{
"messages": [
{
"role": "user",
"content": "User interaction n°1"
},
{
"role": "assistant",
"content": "Bot interaction n°1"
},
{
"role": "user",
"content": "User interaction n°2"
},
{
"role": "assistant",
"content": "Bot interaction n°2"
}
]
}
- Conversational data must be stored under the "messages" key as a list.
- Each list item is a dictionary containing the "content" and "role" keys. "role" is a string: "system", "user", "assistant" or "tool".
- Loss computation is performed only on tokens corresponding to assistant messages ("role" == "assistant").
While text-only fine-tuning covers multiple use cases, you can also fine-tune the vision capabilities of our models. This allows you to create models that can understand and generate responses based on both text and image inputs.
Note that the files must be in JSONL format, meaning every JSON object must be flattened into a single line, and each JSON object is on a new line.
Raw .jsonl file example:
{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
...
Jobs Management
Create and Manage Fine-tuning Jobs
To create your custom model, you need to create a fine-tuning job. You can fully manage jobs via our API, from creation, to starting, monitoring and cancellation.
A fine-tuning job corresponds to a single training run. You can create a fine-tuning job with the following parameters:
Create a fine-tuning job
- model: the specific model you would like to fine-tune. The choices are:
- Text Only:
- open-mistral-7b
- mistral-small-latest
- codestral-latest
- open-mistral-nemo
- mistral-large-latest
- ministral-8b-latest
- ministral-3b-latest
- Vision:
- pixtral-12b-latest
- Text Only:
- training_files: a collection of training file IDs, which can consist of a single file or multiple files
- validation_files: a collection of validation file IDs, which can consist of a single file or multiple files
- hyperparameters: two adjustable hyperparameters, "training_steps" and "learning_rate", that users can modify.
- auto_start:
- auto_start=True: Your job will be launched immediately after validation.
- auto_start=False (default): You can manually start the training after validation by sending a POST request to /fine_tuning/jobs/<uuid>/start.
- integrations: external integrations we support such as Weights and Biases for metrics tracking during training.
# create a fine-tuning job
created_jobs = client.fine_tuning.jobs.create(
model="open-mistral-7b",
training_files=[{"file_id": training_data.id, "weight": 1}],
validation_files=[validation_data.id],
hyperparameters={
"training_steps": 10,
"learning_rate":0.0001
},
auto_start=False,
# integrations=[
# {
# "project": "finetuning",
# "api_key": "WANDB_KEY",
# }
# ]
)
Job Status
After creating a fine-tuning job, you can check the job status using:
client.fine_tuning.jobs.get(job_id = created_jobs.id)
Start a fine-tuning job
Initially, the job status will be "QUEUED". After a brief period, the status will update to "VALIDATED". At this point, you can proceed to start the fine-tuning job:
# start a fine-tuning job
client.fine_tuning.jobs.start(job_id = created_jobs.id)
created_jobs
Fine-tuned Model
Use and Delete Fine-tuned Models
Once your fine-tuning job is done, you can use your fine-tuned custom model in your applications.
Use a fine-tuned model
Below is an example of how to use a fine-tuned model to classify your data.
# You will be able to see the fine-tuned model name via `retrieved_job.fine_tuned_model`
chat_response = client.chat.complete(
model=retrieved_job.fine_tuned_model,
messages = [{"role":'user', "content":'What is the best French cheese?'}]
)
Delete a fine-tuned model
You can delete a fine-tuned model if you no longer need it.
client.models.delete(model_id=retrieved_job.fine_tuned_model)
FAQ
-
Mistral API: We currently validate each file when you upload the dataset.
-
mistral-finetune: You can run the data validation script to validate the data and run the reformat data script to reformat the data to the right format:
# download the reformat script
wget https://raw.githubusercontent.com/mistralai/mistral-finetune/main/utils/reformat_data.py
# download the validation script
wget https://raw.githubusercontent.com/mistralai/mistral-finetune/main/utils/validate_data.py
# reformat data
python reformat_data.py data.jsonl
# validate data
python validate_data.py data.jsonl
However, it's important to note that these scripts might not detect all problematic cases. Therefore, you may need to manually validate and correct any unique edge cases in your data.
While the size limit for an individual training data file is 512MB, there's no limitation on the number of files you can upload. You can upload multiple files and reference them when creating the job.
The size limit for the validation data is 1MB. As a rule of thumb:
validation_set_max_size = min(1MB, 5% of training data)
At job creation, you will receive a 409 Conflict error in case a similar job is already running / validated / queued. This mechanism helps avoid inadvertently creating duplicate jobs, saving resources and preventing redundancy.
If a file is uploaded and matches an existing file in both content and name, the pre-existing file is returned instead of creating a new one.
A general rule of thumb is: Num epochs = max_steps / file_of_training_jsonls_in_MB. For instance, if your training file is 100MB and you set max_steps=1000, the training process will roughly perform 10 epochs.
Mistral API: When you create a fine-tuning job, you should automatically see these info with the default auto_start=False argument.
Note that the dry_run=True argument will be removed in September.
mistral-finetune: You can use the following script to find out: https://github.com/mistralai/mistral-finetune/blob/main/utils/validate_data.py. This script accepts a .yaml training file as input and returns the number of tokens the model is being trained on.
For Mistral API, you can use the auto_start=False argument as mentioned in the previous question.
For LoRA fine-tuning, we recommend 1e-4 (default) or 1e-5.
Note that the learning rate we define is the peak learning rate, instead of a flat learning rate. The learning rate follows a linear warmup and cosine decay schedule. During the warmup phase, the learning rate is linearly increased from a small initial value to a larger value over a certain number of training steps. After the warmup phase, the learning rate is decayed using a cosine function.
Yes, we support OpenAI format.
You can split your data file into chunks. Here is an example:
import json
from datasets import load_dataset
# get data from hugging face
ds = load_dataset("HuggingFaceH4/ultrachat_200k",split="train_gen")
# save data into .jsonl. This file is about 1.3GB
with open('train.jsonl', 'w') as f:
for line in ds:
json.dump(line, f)
f.write('\n')
# reformat data
!wget https://raw.githubusercontent.com/mistralai/mistral-finetune/main/utils/reformat_data.py
!python reformat_data.py train.jsonl
# Split file into three chunks
input_file = "train.jsonl"
output_files = ["train_1.jsonl", "train_2.jsonl", "train_3.jsonl"]
# open the output files
output_file_objects = [open(file, "w") for file in output_files]
# counter for output files
counter = 0
with open(input_file, "r") as f_in: # read the input file line by line
for line in f_in: # parse the line as JSON
data = json.loads(line) # write the data to the current output file
output_file_objects[counter].write(json.dumps(data) + "\n") # increment the counter
counter = (counter + 1) % 3
# close the output files
for file in output_file_objects:
file.close()
# now you should see three jsonl files under 500MB