[来源关键摘录] 官方开源 LoRA 微调代码库,支持科研社区对 Mathstral 7B 等模型进行领域定制。
[正文前段节选] Skip to content
Navigation Menu
Appearance settings
You signed in with another tab or window. Reload to refresh your session.
You signed out in another tab or window. Reload to refresh your session.
You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 16, 2026. It is now read-only.
Repository navigation
mistral-finetune
Public archive
NotificationsYou must be signed in to change notification settings
Fork316 (316)
Star3.1k (3.1k)You must be signed in to star a repository
No description, website, or topics provided.
Apache-2.0 license
Custom properties
Report repository
BranchesTags
Open more actions menu
Latest commit
Folders and files
Last commit message
Last commit date
.github/ISSUE_TEMPLATE
pyproject.toml
requirements.dev.txt
requirements.txt
View all files
Repository files navigation
Mistral-finetune
⚠️ Archived — no longer maintained
This repository is archived and no longer actively maintained. We may launch a new library or a major update in the future if the community needs it or we believe we can bring value to the fine-tuning ecosystem.
mistral-finetune is a light-weight codebase that enables memory-efficient and performant finetuning of Mistral's models.
It is based on LoRA, a training paradigm where most weights are frozen and only 1-2% of additional weights in the form of low-rank matrix perturbations are trained.
For maximum efficiency it is recommended to use an A100 or H100 GPU. The codebase is optimized
for multi-GPU-single-node training setups, but for smaller models, such as the 7B a single GPU suffices.
The goal of this repository is to provide a simple, guided entrypoint to finetune Mistral models.
As such, it is fairly opinionated (especially around data formatting) and does not aim at being exhaustive
across multiple model architectures or hardware types.
For more generic approaches, you can check out some other great projects like
13.08.2024: Mistral Large v2 is now compatible with mistral-finetune!
Download the 123B Instruct here and set model_id_or_path to the downloaded checkpoint dir.
Fine-tuning Mistral-Large v2 requires significantly more memory due to a larger model size. For now set seq_len to <= 8192
It is recommended to use a lower learning rate as compared to other models, e.g. lr=1e-6 should work well for most cases.
19.07.2024: Mistral Nemo is now compatible with mistral-finetune!
Download the 12B Base or Instruct here and set model_id_or_path to the downloaded checkpoint dir.
Run pip install --upgrade mistral-common to have a version that supports the Tekkenizer (>=1.3.1).
Fine-tuning Mistral-Nemo requires currently much more memory due to a larger vocabulary size which spikes the peak memory requirement of the CE loss (we'll soon add an improved CE loss here). For now set seq_len to <= 16384
It is recommended to use the same hyperparameters as for the 7B v3.
Installation
To get started with Mistral LoRA fine-tuning, follow these steps:
Clone this repository:
cd $HOME && git clone https://github.com/mistralai/mistral-finetune.git
Install all required dependencies:
cd mistral-finetune
pip install -r requirements.txt
Model download
We recommend fine-tuning one of the official Mistral models which you can download here:
0663b293810d7571dad25dae2f2a5806
7B Instruct v3
80b71fcb6416085bcb4efad86dfb4d52
8x7B Base V1
8x7B Instruct V1
8x7B Instruct
8e2d3930145dc43d3084396f49d38a3f
8x22 Instruct V3
8x22 Instruct
471a02a6902706a2f1e44a693813855b
8x22B Base V3
a2fa75117174f87d1197e3a4eb50371a
12B Instruct
12B Instruct (Mistral-Nemo)
296fbdf911cb88e6f0be74cd04827fe7
12 Base (Mistral-Nemo)
c5d079ac4b55fc1ae35f51f0a3c0eb83
Mistral Large 2
123B Instruct (Large v2)
fc602155f9e39151fba81fcaab2fa7c4
Important Notice: For 8x7B Base V1 and 8x7B Instruct V1, it is necessary to use our v3 tokenizer and extend the vocabulary size to 32768 prior to fine-tuning. For detailed instructions on this process, please refer to the "Model extension" section.
E.g., to download the 7B-base model you can run the following command:
mkdir -p ~/${HOME}/mistral_models
cd ${HOME} && wget https://models.mistralcdn.com/mistral-7b-v0-3/mistral-7B-v0.3.tar
tar -xf mistral-7B-v0.3.tar -C mistral_models
Make sure to modify your training script and add the path to the downloaded
folder as model_id_or_path.
E.g., modify example/7B.yaml to include the absolute path to $HOME/mistral_models/7B:
model_id_or_path: "/Users/johndoe/mistral_models/7B"
Prepare dataset
To ensure effective training, mistral-finetune has strict
requirements for how the training data has to be formatted.
All data files must be stored in jsonl format files.
You can build two types of data files:
Pretrain data corresponds to plain text data stored in the "text" key. E.g:
{"text": "Text contained in document n°1"}
{"text": "Text contained in document n°2"}
Currently two different types of instruction following data are supported:
Instruct: conversational data stored in the "messages" key in the form of a list. Each list item is a dictionary containing the "content" and "role" keys. "role" is a string being one of "user", "assistant" or "system". The loss will only be computed if "role" == "assistant". E.g.:
"messages": [
"role": "user",
"content": "User interaction n°1 contained in document n°1"
"role": "assistant",
"content": "Bot interaction n°1 contained in document n°1"
"content": "User interaction n°2 contained in document n°1"
"content": "Bot interaction n°2 contained in document n°1"
"content": "User interaction n°1 contained in document n°2"
"content": "Bot interaction n°1 contained in document n°2"
"content": "User interaction n°2 contained in document n°2"
"content": "Bot interaction n°2 contained in document n°2",
"weight": 0, # don't train on n°2
"content": "User interaction n°3 contained in document n°2"
"content": "Bot interaction n°3 contained in document n°2"
Function calling: conversational data stored in the "messages" key in the form of a list. Each list item is a dictionary containing the "role" and "content" or "tool_calls" keys. "role" is a string being one of "user", "assistant", "system", or "tool". The loss will only be computed if "role" == "assistant".
Note: In function calling the "id" of "tool_calls" and the "tool_call_id" are randomly generated strings of exactly 9 chars. We recommend to generate this automatically
in a data preparation script as is done here.
"role": "system",
"content": "You are a helpful assistant who has access to the following functions to help the user, you can use the functions if needed"
"content": "Can you help me generate an anagram of the word \"listen\"?"
"tool_calls": [
"id": "TX92Jm8Zi",
"type": "function",
"function": {
"name": "generate_anagram",
"arguments": "{\"word\": \"listen\"}"
"role": "tool",
"content": "{\"anagram\": \"silent\"}",
"tool_call_id": "TX92Jm8Zi"
"content": "The anagram of the word \"listen\" is \"silent\"."
"content": "That's amazing! Can you generate an anagram for the word \"race\"?"
"id": "3XhQnxLsT",
"arguments": "{\"word\": \"race\"}"
"description": "Generate an anagram of a given word",
"parameters": {
"type": "object",
"properties": {
"type": "string",
"description": "The word to generate an anagram of"
"required": [
Verify dataset
Before starting a training run you should verify that your dataset is correctly formatted and get an
estimation of the training time. You can do so by using the ./utils/validate_data script.
Note that this step is crucial to ensure that the data is correctly formatted.
Instruction following
Let's go over a simple example to train a model in instruction following:
Load a chunk of Ultachat_200k
Create the data folder and navigate to the folder.
cd $HOME && mkdir -p data && cd $HOME/data
Load the data into a Pandas Dataframe.
[...内容已截断,完整版请查看原文或 Wayback 快照]