Text Generation
Transformers
TensorBoard
ONNX
Safetensors
Transformers.js
English
llama
conversational
text-generation-inference
Instructions to use HuggingFaceTB/SmolLM2-1.7B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use HuggingFaceTB/SmolLM2-1.7B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-1.7B-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct") model = AutoModelForCausalLM.from_pretrained("HuggingFaceTB/SmolLM2-1.7B-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Transformers.js
How to use HuggingFaceTB/SmolLM2-1.7B-Instruct with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('text-generation', 'HuggingFaceTB/SmolLM2-1.7B-Instruct'); - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use HuggingFaceTB/SmolLM2-1.7B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "HuggingFaceTB/SmolLM2-1.7B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "HuggingFaceTB/SmolLM2-1.7B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/HuggingFaceTB/SmolLM2-1.7B-Instruct
- SGLang
How to use HuggingFaceTB/SmolLM2-1.7B-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "HuggingFaceTB/SmolLM2-1.7B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "HuggingFaceTB/SmolLM2-1.7B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "HuggingFaceTB/SmolLM2-1.7B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "HuggingFaceTB/SmolLM2-1.7B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use HuggingFaceTB/SmolLM2-1.7B-Instruct with Docker Model Runner:
docker model run hf.co/HuggingFaceTB/SmolLM2-1.7B-Instruct
Transformers.js improvements (#25)
Browse files- [WIP] Transformers.js improvements (b86e3eda672bc29427317d7d6753fbf808730d94)
- Update README.md (26cd37002254a59f114c509de0b80ef80aaf90d6)
- README.md +29 -2
- config.json +5 -0
README.md
CHANGED
|
@@ -40,7 +40,7 @@ For more details refer to: https://github.com/huggingface/smollm. You will find
|
|
| 40 |
|
| 41 |
### How to use
|
| 42 |
|
| 43 |
-
### Transformers
|
| 44 |
```bash
|
| 45 |
pip install transformers
|
| 46 |
```
|
|
@@ -62,13 +62,40 @@ print(tokenizer.decode(outputs[0]))
|
|
| 62 |
```
|
| 63 |
|
| 64 |
|
| 65 |
-
### Chat in TRL
|
| 66 |
You can also use the TRL CLI to chat with the model from the terminal:
|
| 67 |
```bash
|
| 68 |
pip install trl
|
| 69 |
trl chat --model_name_or_path HuggingFaceTB/SmolLM2-1.7B-Instruct --device cpu
|
| 70 |
```
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
## Evaluation
|
| 73 |
|
| 74 |
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.
|
|
|
|
| 40 |
|
| 41 |
### How to use
|
| 42 |
|
| 43 |
+
#### Transformers
|
| 44 |
```bash
|
| 45 |
pip install transformers
|
| 46 |
```
|
|
|
|
| 62 |
```
|
| 63 |
|
| 64 |
|
| 65 |
+
#### Chat in TRL
|
| 66 |
You can also use the TRL CLI to chat with the model from the terminal:
|
| 67 |
```bash
|
| 68 |
pip install trl
|
| 69 |
trl chat --model_name_or_path HuggingFaceTB/SmolLM2-1.7B-Instruct --device cpu
|
| 70 |
```
|
| 71 |
|
| 72 |
+
#### Transformers.js
|
| 73 |
+
|
| 74 |
+
```bash
|
| 75 |
+
npm i @huggingface/transformers
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
```js
|
| 79 |
+
import { pipeline } from "@huggingface/transformers";
|
| 80 |
+
|
| 81 |
+
// Create a text generation pipeline
|
| 82 |
+
const generator = await pipeline(
|
| 83 |
+
"text-generation",
|
| 84 |
+
"HuggingFaceTB/SmolLM2-1.7B-Instruct",
|
| 85 |
+
);
|
| 86 |
+
|
| 87 |
+
// Define the list of messages
|
| 88 |
+
const messages = [
|
| 89 |
+
{ role: "system", content: "You are a helpful assistant." },
|
| 90 |
+
{ role: "user", content: "Tell me a joke." },
|
| 91 |
+
];
|
| 92 |
+
|
| 93 |
+
// Generate a response
|
| 94 |
+
const output = await generator(messages, { max_new_tokens: 128 });
|
| 95 |
+
console.log(output[0].generated_text.at(-1).content);
|
| 96 |
+
// "Why don't scientists trust atoms?\n\nBecause they make up everything!"
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
## Evaluation
|
| 100 |
|
| 101 |
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.
|
config.json
CHANGED
|
@@ -25,9 +25,14 @@
|
|
| 25 |
"torch_dtype": "bfloat16",
|
| 26 |
"transformers_version": "4.42.3",
|
| 27 |
"transformers.js_config": {
|
|
|
|
| 28 |
"kv_cache_dtype": {
|
| 29 |
"q4f16": "float16",
|
| 30 |
"fp16": "float16"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
},
|
| 33 |
"use_cache": true,
|
|
|
|
| 25 |
"torch_dtype": "bfloat16",
|
| 26 |
"transformers_version": "4.42.3",
|
| 27 |
"transformers.js_config": {
|
| 28 |
+
"dtype": "q4",
|
| 29 |
"kv_cache_dtype": {
|
| 30 |
"q4f16": "float16",
|
| 31 |
"fp16": "float16"
|
| 32 |
+
},
|
| 33 |
+
"use_external_data_format": {
|
| 34 |
+
"model.onnx": true,
|
| 35 |
+
"model_fp16.onnx": true
|
| 36 |
}
|
| 37 |
},
|
| 38 |
"use_cache": true,
|