Commit
·
7e2da68
1
Parent(s):
2193a60
update blog
Browse files
examples/fine-tune-smollm2-on-synthetic-data.ipynb
CHANGED
@@ -4,19 +4,19 @@
|
|
4 |
"cell_type": "markdown",
|
5 |
"metadata": {},
|
6 |
"source": [
|
7 |
-
"# Fine-tune a SmolLM on domain
|
8 |
"\n",
|
9 |
-
"Yes, smoll models can beat GPT4-like models on domain-specific tasks but don't expect miracles. When comparing smoll vs large, consider all costs and gains like difference performance and the value of using private and local models and data that you own
|
10 |
"\n",
|
11 |
-
"The [Hugging Face SmolLM models](https://github.com/huggingface/smollm) are blazingly fast and remarkably powerful. With its 135M, 360M and 1.7B parameter models, it is a great choice for a small and fast model. The great thing about SmolLM is that it is a general
|
12 |
"\n",
|
13 |
-
"A lack of domain
|
14 |
"\n",
|
15 |
"In this example, we will fine-tune a SmolLM2 model on a synthetic dataset generated from `meta-llama/Meta-Llama-3.1-8B-Instruct` with the `synthetic-data-generator`.\n",
|
16 |
"\n",
|
17 |
"## Install the dependencies\n",
|
18 |
"\n",
|
19 |
-
"We will
|
20 |
]
|
21 |
},
|
22 |
{
|
@@ -34,11 +34,11 @@
|
|
34 |
"source": [
|
35 |
"## The problem\n",
|
36 |
"\n",
|
37 |
-
"Reasoning data has proven to be a fundamental change
|
38 |
"\n",
|
39 |
"## Let's generate some data\n",
|
40 |
"\n",
|
41 |
-
"Let's go to the [hosted Hugging Face Space](https://huggingface.co/spaces/argilla/synthetic-data-generator) to generate the data. This is done in three steps 1) we come up with a dataset description, 2) iterate on the task configuration, and 3) generate and push the data to Hugging Face. A more detailed flow can be found in [this
|
42 |
"\n",
|
43 |
"<iframe\n",
|
44 |
"\tsrc=\"https://argilla-synthetic-data-generator.hf.space\"\n",
|
@@ -50,10 +50,10 @@
|
|
50 |
"For this example, we will generate 5000 chat data examples for a single turn in the conversation. All examples have been generated with a temperature of 1. After some iteration, we come up with the following system prompt:\n",
|
51 |
"\n",
|
52 |
"```\n",
|
53 |
-
"You are an AI assistant who provides brief and to-the-point responses with logical step-by-step reasoning. Your purpose is to offer straightforward explanations and answers
|
54 |
"```\n",
|
55 |
"\n",
|
56 |
-
"We press the \"Push to Hub\" button and wait for the data to be generated. This takes a few hours and we end up with a dataset with 5000 examples, which is the maximum examples we can generate in a single run. You can scale this by deploying a private instance of the Synthetic Data Generator. \n",
|
57 |
"\n",
|
58 |
"<iframe\n",
|
59 |
" src=\"https://huggingface.co/datasets/argilla/synthetic-concise-reasoning-sft-filtered/embed/viewer/default/train\"\n",
|
@@ -70,7 +70,7 @@
|
|
70 |
"\n",
|
71 |
"### Load the model\n",
|
72 |
"\n",
|
73 |
-
"We will first load the model and tokenizer
|
74 |
]
|
75 |
},
|
76 |
{
|
@@ -186,6 +186,15 @@
|
|
186 |
"ds = ds.shuffle()"
|
187 |
]
|
188 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
{
|
190 |
"cell_type": "code",
|
191 |
"execution_count": null,
|
@@ -220,11 +229,11 @@
|
|
220 |
"cell_type": "markdown",
|
221 |
"metadata": {},
|
222 |
"source": [
|
223 |
-
"For the example, we did not use a specific validation set but we can see the loss is decreasing, so we assume the model is
|
224 |
"\n",
|
225 |
"### Run inference\n",
|
226 |
"\n",
|
227 |
-
"We can now run inference with the fine-tuned model."
|
228 |
]
|
229 |
},
|
230 |
{
|
@@ -245,9 +254,9 @@
|
|
245 |
"\n",
|
246 |
"We have fine-tuned a SmolLM2 model on a synthetic dataset generated from a large language model. We have seen that the model performs well on the task and that the synthetic data is a great way to generate diverse and representative data for supervised fine-tuning. \n",
|
247 |
"\n",
|
248 |
-
"In
|
249 |
"\n",
|
250 |
-
"Overall, I think it is pretty cool for one of our generation and fine-tuning on
|
251 |
]
|
252 |
}
|
253 |
],
|
|
|
4 |
"cell_type": "markdown",
|
5 |
"metadata": {},
|
6 |
"source": [
|
7 |
+
"# Fine-tune a SmolLM on domain-specific synthetic data from a LLM\n",
|
8 |
"\n",
|
9 |
+
"Yes, smoll models can beat GPT4-like models on domain-specific tasks but don't expect miracles. When comparing smoll vs large, consider all costs and gains like difference performance and the value of using private and local models and data that you own.\n",
|
10 |
"\n",
|
11 |
+
"The [Hugging Face SmolLM models](https://github.com/huggingface/smollm) are blazingly fast and remarkably powerful. With its 135M, 360M and 1.7B parameter models, it is a great choice for a small and fast model. The great thing about SmolLM is that it is a general-purpose model that can be fine-tuned on domain-specific data.\n",
|
12 |
"\n",
|
13 |
+
"A lack of domain-specific datasets is a common problem for smaller and more specialized models. This is because it is difficult to find a dataset that is both representative and diverse enough for a specific task. We solve this problem by generating a synthetic dataset from an LLM using the `synthetic-data-generator`, which is available as a [Hugging Face Space](https://huggingface.co/spaces/argilla/synthetic-data-generator) or on [GitHub](https://github.com/argilla-io/synthetic-data-generator).\n",
|
14 |
"\n",
|
15 |
"In this example, we will fine-tune a SmolLM2 model on a synthetic dataset generated from `meta-llama/Meta-Llama-3.1-8B-Instruct` with the `synthetic-data-generator`.\n",
|
16 |
"\n",
|
17 |
"## Install the dependencies\n",
|
18 |
"\n",
|
19 |
+
"We will install some basic dependencies for the fine-tuning with `trl` but we will use the Synthetic Data Generator UI to generate the synthetic dataset."
|
20 |
]
|
21 |
},
|
22 |
{
|
|
|
34 |
"source": [
|
35 |
"## The problem\n",
|
36 |
"\n",
|
37 |
+
"Reasoning data has proven to be a fundamental change in the performance of generative models. Reasoning is amazing but it also means the model generates more \"chatty\" during the token generation process, causing the model to become slower and more expensive. For this reason, we want to create a model that can reason without being too chatty. Therefore, we will generate a concise reasoning dataset and fine-tune a SmolLM2 model on it.\n",
|
38 |
"\n",
|
39 |
"## Let's generate some data\n",
|
40 |
"\n",
|
41 |
+
"Let's go to the [hosted Hugging Face Space](https://huggingface.co/spaces/argilla/synthetic-data-generator) to generate the data. This is done in three steps 1) we come up with a dataset description, 2) iterate on the task configuration, and 3) generate and push the data to Hugging Face. A more detailed flow can be found in [this blog post](https://huggingface.co/blog/synthetic-data-generator). \n",
|
42 |
"\n",
|
43 |
"<iframe\n",
|
44 |
"\tsrc=\"https://argilla-synthetic-data-generator.hf.space\"\n",
|
|
|
50 |
"For this example, we will generate 5000 chat data examples for a single turn in the conversation. All examples have been generated with a temperature of 1. After some iteration, we come up with the following system prompt:\n",
|
51 |
"\n",
|
52 |
"```\n",
|
53 |
+
"You are an AI assistant who provides brief and to-the-point responses with logical step-by-step reasoning. Your purpose is to offer straightforward explanations and answers so that you can get to the heart of the issue. Respond with extremely concise, direct justifications and evidence-based conclusions. User questions are direct and concise.\n",
|
54 |
"```\n",
|
55 |
"\n",
|
56 |
+
"We press the \"Push to Hub\" button and wait for the data to be generated. This takes a few hours and we end up with a dataset with 5000 examples, which is the maximum number of examples we can generate in a single run. You can scale this by deploying a private instance of the Synthetic Data Generator. \n",
|
57 |
"\n",
|
58 |
"<iframe\n",
|
59 |
" src=\"https://huggingface.co/datasets/argilla/synthetic-concise-reasoning-sft-filtered/embed/viewer/default/train\"\n",
|
|
|
70 |
"\n",
|
71 |
"### Load the model\n",
|
72 |
"\n",
|
73 |
+
"We will first load the model and tokenizer and set up the chat format."
|
74 |
]
|
75 |
},
|
76 |
{
|
|
|
186 |
"ds = ds.shuffle()"
|
187 |
]
|
188 |
},
|
189 |
+
{
|
190 |
+
"cell_type": "markdown",
|
191 |
+
"metadata": {},
|
192 |
+
"source": [
|
193 |
+
"### Fine-tune the model\n",
|
194 |
+
"\n",
|
195 |
+
"We will now fine-tune the model. We will use the `SFTTrainer` from the `trl` library to fine-tune the model. We will use a batch size of 4 and a learning rate of 5e-5. We will also use the `use_mps_device` flag to use the MPS device if available."
|
196 |
+
]
|
197 |
+
},
|
198 |
{
|
199 |
"cell_type": "code",
|
200 |
"execution_count": null,
|
|
|
229 |
"cell_type": "markdown",
|
230 |
"metadata": {},
|
231 |
"source": [
|
232 |
+
"For the example, we did not use a specific validation set but we can see the loss is decreasing, so we assume the model is generalsing well to the training data. To get a better understanding of the model's performance, let's test it again with the same prompt. \n",
|
233 |
"\n",
|
234 |
"### Run inference\n",
|
235 |
"\n",
|
236 |
+
"We can now run inference with [the fine-tuned model](https://huggingface.co/argilla/SmolLM2-360M-synthetic-concise-reasoning/blob/main/README.md)."
|
237 |
]
|
238 |
},
|
239 |
{
|
|
|
254 |
"\n",
|
255 |
"We have fine-tuned a SmolLM2 model on a synthetic dataset generated from a large language model. We have seen that the model performs well on the task and that the synthetic data is a great way to generate diverse and representative data for supervised fine-tuning. \n",
|
256 |
"\n",
|
257 |
+
"In practice, you would likely want to spend more time on the data quality and fine-tuning the model but the flow shows the Synthetic Data Generator is a great tool to generate synthetic data for any task.\n",
|
258 |
"\n",
|
259 |
+
"Overall, I think it is pretty cool for one of our generation and twon hours of fine-tuning on "
|
260 |
]
|
261 |
}
|
262 |
],
|