Update README.md
Browse files
README.md
CHANGED
@@ -1,92 +1,229 @@
|
|
1 |
---
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
-
|
6 |
-
- dpo
|
7 |
-
- generated_from_trainer
|
8 |
-
- trl
|
9 |
-
- dpo
|
10 |
-
- generated_from_trainer
|
11 |
-
datasets:
|
12 |
-
- HuggingFaceH4/ultrafeedback_binarized
|
13 |
-
model-index:
|
14 |
-
- name: smollm2-1.7B-8k-mix7-ep2-v2-dpo-ultraf-ep3
|
15 |
-
results: []
|
16 |
---
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
[
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
79 |
-
|
80 |
-
|
|
81 |
-
|
|
82 |
-
|
|
83 |
-
|
|
84 |
-
|
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
library_name: transformers
|
3 |
+
license: apache-2.0
|
4 |
+
language:
|
5 |
+
- en
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
---
|
7 |
|
8 |
+
|
9 |
+
# SmolLM2
|
10 |
+
|
11 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/61c141342aac764ce1654e43/7IzejwZJ62MfRwvDYvQXY.png)
|
12 |
+
|
13 |
+
## Table of Contents
|
14 |
+
|
15 |
+
1. [Model Summary](#model-summary)
|
16 |
+
2. [Evaluation](#evaluation)
|
17 |
+
3. [Examples](#examples)
|
18 |
+
4. [Limitations](#limitations)
|
19 |
+
5. [Training](#training)
|
20 |
+
6. [License](#license)
|
21 |
+
7. [Citation](#citation)
|
22 |
+
|
23 |
+
## Model Summary
|
24 |
+
|
25 |
+
SmolLM2 is a family of compact language models available in three size: 135M, 360M, and 1.7B parameters. They are capable of solving a wide range of tasks while being lightweight enough to run on-device.
|
26 |
+
|
27 |
+
The 1.7B variant demonstrates significant advances over its predecessor SmolLM1-1.7B, particularly in instruction following, knowledge, reasoning, and mathematics. It was trained on 11 trillion tokens using a diverse dataset combination: FineWeb-Edu, DCLM, The Stack, along with new mathematics and coding datasets that we curated and will release soon. We developed the instruct version through supervised fine-tuning (SFT) using a combination of public datasets and our own curated datasets. We then applied Direct Preference Optimization (DPO) using [UltraFeedback](https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized).
|
28 |
+
|
29 |
+
The instruct model additionally supports tasks such as text rewriting, summarization and function calling thanks to datasets developed by [Argilla](https://huggingface.co/argilla) such as [Synth-APIGen-v0.1](https://huggingface.co/datasets/argilla/Synth-APIGen-v0.1).
|
30 |
+
|
31 |
+
### How to use
|
32 |
+
|
33 |
+
### Transformers
|
34 |
+
```bash
|
35 |
+
pip install transformers
|
36 |
+
```
|
37 |
+
|
38 |
+
```python
|
39 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
40 |
+
checkpoint = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
41 |
+
|
42 |
+
device = "cuda" # for GPU usage or "cpu" for CPU usage
|
43 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
44 |
+
# for multiple GPUs install accelerate and do `model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto")`
|
45 |
+
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
|
46 |
+
|
47 |
+
messages = [{"role": "user", "content": "What is the capital of France."}]
|
48 |
+
input_text=tokenizer.apply_chat_template(messages, tokenize=False)
|
49 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
50 |
+
outputs = model.generate(inputs, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
|
51 |
+
print(tokenizer.decode(outputs[0]))
|
52 |
+
```
|
53 |
+
|
54 |
+
|
55 |
+
### Chat in TRL
|
56 |
+
You can also use the TRL CLI to chat with the model from the terminal:
|
57 |
+
```bash
|
58 |
+
pip install trl
|
59 |
+
trl chat --model_name_or_path HuggingFaceTB/SmolLM2-1.7B-Instruct --device cpu
|
60 |
+
```
|
61 |
+
|
62 |
+
## Evaluation
|
63 |
+
|
64 |
+
In this section, we report the evaluation results of SmolLM2. All evaluations are zero-shot unless stated otherwise, and we use [lighteval](https://github.com/huggingface/lighteval) to run them.
|
65 |
+
|
66 |
+
## Base Pre-Trained Model
|
67 |
+
|
68 |
+
| Metric | SmolLM2-1.7B | Llama-1B | Qwen2.5-1.5B | SmolLM1-1.7B |
|
69 |
+
|------------------|--------------|-------------|---------------|--------------|
|
70 |
+
| HellaSwag | **68.7** | 61.2 | 66.4 | 62.9 |
|
71 |
+
| ARC (Average) | **60.5** | 49.2 | 58.5 | 59.9 |
|
72 |
+
| PIQA | **77.6** | 74.8 | 76.1 | 76.0 |
|
73 |
+
| MMLU-Pro (MCF) | **19.4** | 11.7 | 13.7 | 10.8 |
|
74 |
+
| CommonsenseQA | **43.6** | 41.2 | 34.1 | 38.0 |
|
75 |
+
| TriviaQA | **36.7** | 28.1 | 20.9 | 22.5 |
|
76 |
+
| Winogrande | **59.4** | 57.8 | 59.3 | 54.7 |
|
77 |
+
| OpenBookQA | 42.2 | 38.4 | 40.0 | **42.4** |
|
78 |
+
| GSM8K (5-shot) | 31.0 | 7.2 | **61.3** | 5.5 |
|
79 |
+
|
80 |
+
## Instruction Model
|
81 |
+
|
82 |
+
| Metric | SmolLM2-1.7B-Instruct | Llama-1B-Instruct | Qwen2.5-1.5B-Instruct | SmolLM1-1.7B-Instruct |
|
83 |
+
|:-----------------------------|:---------------------:|:-----------------:|:----------------------:|:----------------------:|
|
84 |
+
| IFEval (Average prompt/inst) | **56.7** | 53.5 | 47.4 | 23.1 |
|
85 |
+
| MT-Bench | 6.13 | 5.48 | **6.52** | 4.33 |
|
86 |
+
| OpenRewrite-Eval (micro_avg RougeL) | 44.9 | 39.2 | **46.9** | NaN |
|
87 |
+
| HellaSwag | **66.1** | 56.1 | 60.9 | 55.5 |
|
88 |
+
| ARC (Average) | **51.7** | 41.6 | 46.2 | 43.7 |
|
89 |
+
| PIQA | **74.4** | 72.3 | 73.2 | 71.6 |
|
90 |
+
| MMLU-Pro (MCF) | 19.3 | 12.7 | **24.2** | 11.7 |
|
91 |
+
| BBH (3-shot) | 32.2 | 27.6 | **35.3** | 25.7 |
|
92 |
+
| GSM8K (5-shot) | **48.2** | 26.8 | 42.8 | 4.62 |
|
93 |
+
|
94 |
+
|
95 |
+
## Examples
|
96 |
+
Below are some system and instruct prompts that work well for special tasks
|
97 |
+
|
98 |
+
### Text rewriting
|
99 |
+
|
100 |
+
```python
|
101 |
+
system_prompt_rewrite = "You are an AI writing assistant. Your task is to rewrite the user's email to make it more professional and approachable while maintaining its main points and key message. Do not return any text other than the rewritten message."
|
102 |
+
user_prompt_rewrite = "Rewrite the message below to make it more friendly and approachable while maintaining its main points and key message. Do not add any new information or return any text other than the rewritten message\nThe message:"
|
103 |
+
messages = [{"role": "system", "content": system_prompt_rewrite}, {"role": "user", "content":f"{user_prompt_rewrite} The CI is failing after your last commit!}"]
|
104 |
+
input_text=tokenizer.apply_chat_template(messages, tokenize=False)
|
105 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
106 |
+
outputs = model.generate(inputs, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
|
107 |
+
print(tokenizer.decode(outputs[0]))
|
108 |
+
```
|
109 |
+
```
|
110 |
+
Hey there! I noticed that the CI isn't passing after your latest commit. Could you take a look and let me know what's going on? Thanks so much for your help!
|
111 |
+
```
|
112 |
+
|
113 |
+
### Summarization
|
114 |
+
|
115 |
+
```python
|
116 |
+
system_prompt_summarize = "Provide a concise, objective summary of the input text in up to three sentences, focusing on key actions and intentions without using second or third person pronouns."
|
117 |
+
messages = [{"role": "system", "content": system_prompt_rewrite}, {"role": "user", "content": INSERT_LONG_EMAIL]
|
118 |
+
input_text=tokenizer.apply_chat_template(messages, tokenize=False)
|
119 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
120 |
+
outputs = model.generate(inputs, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
|
121 |
+
print(tokenizer.decode(outputs[0]))
|
122 |
+
```
|
123 |
+
|
124 |
+
### Function calling
|
125 |
+
|
126 |
+
SmolLM2-1.7B-Instruct can handle function calling, it scores 27% on the [BFCL Leaderboard](https://gorilla.cs.berkeley.edu/blogs/8_berkeley_function_calling_leaderboard.html). Here's how you can leverage it:
|
127 |
+
|
128 |
+
```python
|
129 |
+
import json
|
130 |
+
import re
|
131 |
+
from typing import Optional
|
132 |
+
|
133 |
+
from jinja2 import Template
|
134 |
+
import torch
|
135 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
136 |
+
from transformers.utils import get_json_schema
|
137 |
+
|
138 |
+
|
139 |
+
system_prompt = Template("""You are an expert in composing functions. You are given a question and a set of possible functions.
|
140 |
+
Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
|
141 |
+
If none of the functions can be used, point it out and refuse to answer.
|
142 |
+
If the given question lacks the parameters required by the function, also point it out.
|
143 |
+
|
144 |
+
You have access to the following tools:
|
145 |
+
<tools>{{ tools }}</tools>
|
146 |
+
|
147 |
+
The output MUST strictly adhere to the following format, and NO other text MUST be included.
|
148 |
+
The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make the tool calls an empty list '[]'.
|
149 |
+
<tool_call>[
|
150 |
+
{"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
|
151 |
+
... (more tool calls as required)
|
152 |
+
]</tool_call>""")
|
153 |
+
|
154 |
+
|
155 |
+
def prepare_messages(
|
156 |
+
query: str,
|
157 |
+
tools: Optional[dict[str, any]] = None,
|
158 |
+
history: Optional[list[dict[str, str]]] = None
|
159 |
+
) -> list[dict[str, str]]:
|
160 |
+
"""Prepare the system and user messages for the given query and tools.
|
161 |
+
|
162 |
+
Args:
|
163 |
+
query: The query to be answered.
|
164 |
+
tools: The tools available to the user. Defaults to None, in which case if a
|
165 |
+
list without content will be passed to the model.
|
166 |
+
history: Exchange of messages, including the system_prompt from
|
167 |
+
the first query. Defaults to None, the first message in a conversation.
|
168 |
+
"""
|
169 |
+
if tools is None:
|
170 |
+
tools = []
|
171 |
+
if history:
|
172 |
+
messages = history.copy()
|
173 |
+
messages.append({"role": "user", "content": query})
|
174 |
+
else:
|
175 |
+
messages = [
|
176 |
+
{"role": "system", "content": system_prompt.render(tools=json.dumps(tools))},
|
177 |
+
{"role": "user", "content": query}
|
178 |
+
]
|
179 |
+
return messages
|
180 |
+
|
181 |
+
|
182 |
+
def parse_response(text: str) -> str | dict[str, any]:
|
183 |
+
"""Parses a response from the model, returning either the
|
184 |
+
parsed list with the tool calls parsed, or the
|
185 |
+
model thought or response if couldn't generate one.
|
186 |
+
|
187 |
+
Args:
|
188 |
+
text: Response from the model.
|
189 |
+
"""
|
190 |
+
pattern = r"<tool_call>(.*?)</tool_call>"
|
191 |
+
matches = re.findall(pattern, text, re.DOTALL)
|
192 |
+
if matches:
|
193 |
+
return json.loads(matches[0])
|
194 |
+
return text
|
195 |
+
```
|
196 |
+
|
197 |
+
## Limitations
|
198 |
+
|
199 |
+
SmolLM2 models primarily understand and generate content in English. They can produce text on a variety of topics, but the generated content may not always be factually accurate, logically consistent, or free from biases present in the training data. These models should be used as assistive tools rather than definitive sources of information. Users should always verify important information and critically evaluate any generated content.
|
200 |
+
|
201 |
+
## Training
|
202 |
+
|
203 |
+
### Model
|
204 |
+
|
205 |
+
- **Architecture:** Transformer decoder
|
206 |
+
- **Pretraining tokens:** 11T
|
207 |
+
- **Precision:** bfloat16
|
208 |
+
|
209 |
+
### Hardware
|
210 |
+
|
211 |
+
- **GPUs:** 256 H100
|
212 |
+
|
213 |
+
### Software
|
214 |
+
|
215 |
+
- **Training Framework:** [nanotron](https://github.com/huggingface/nanotron/tree/main)
|
216 |
+
- **Alignement Handbook** [alignement-handbook](https://github.com/huggingface/alignment-handbook/)
|
217 |
+
|
218 |
+
## License
|
219 |
+
|
220 |
+
[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)
|
221 |
+
|
222 |
+
## Citation
|
223 |
+
```bash
|
224 |
+
@misc{allal2024SmolLM2,
|
225 |
+
title={SmolLM2 - with great data, comes great performance},
|
226 |
+
author={Loubna Ben Allal and Anton Lozhkov and Elie Bakouch and Gabriel Martín Blázquez and Lewis Tunstall and Agustín Piqueres and Andres Marafioti and Cyril Zakka and Leandro von Werra and Thomas Wolf},
|
227 |
+
year={2024},
|
228 |
+
}
|
229 |
+
```
|