Sponsored by
Vmake Labs Video Enhancer
-Vmake Labs Video Enhancer uses AI to improve video quality, restore details, reduce blur, and upscale videos to 8K.
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. Fine-tuning a language model like SmolLM involves several steps, from setting up the environment to training the model and saving the results. Below is a detailed step-by-step guide based on the provided notebook file
How to use
Transformers
pip install transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "prithivMLmods/SmolLM2-CoT-360M"
device = "cuda"# for GPU usage or "cpu" for CPU usage
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# for multiple GPUs install accelerate and do `model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto")`
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
messages = [{"role": "user", "content": "What is the capital of France."}]
input_text=tokenizer.apply_chat_template(messages, tokenize=False)
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
outputs = model.generate(inputs, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
print(tokenizer.decode(outputs[0]))
Step 1: Setting Up the Environment
Before diving into fine-tuning, you need to set up your environment with the necessary libraries and tools.
Install Required Libraries
:
Install the necessary Python libraries using
pip
. These include
transformers
,
datasets
,
trl
,
torch
,
accelerate
,
bitsandbytes
, and
wandb
.
These libraries are essential for working with Hugging Face models, datasets, and training loops.
Import the required modules from the installed libraries. These include
AutoModelForCausalLM
,
AutoTokenizer
,
TrainingArguments
,
pipeline
,
load_dataset
, and
SFTTrainer
.
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, pipeline
from datasets import load_dataset
from trl import SFTConfig, SFTTrainer, setup_chat_format
import torch
import os
Detect Device (GPU, MPS, or CPU)
:
Detect the available hardware (GPU, MPS, or CPU) to ensure the model runs on the most efficient device.
Chat template is already added to the tokenizer, indicates that the tokenizer already has a predefined chat template, which prevents the setup_chat_format() from modifying it again.
Define a tokenization function that processes the dataset in batches. This function applies the chat template to each prompt-response pair and tokenizes the text.
deftokenize_function(examples):
prompts = [p.strip() for p in examples["prompt"]]
responses = [r.strip() for r in examples["response"]]
texts = [
tokenizer.apply_chat_template(
[{"role": "user", "content": p}, {"role": "assistant", "content": r}],
tokenize=False
)
for p, r inzip(prompts, responses)
]
return tokenizer(texts, truncation=True, padding="max_length", max_length=512)
Apply Tokenization
:
Apply the tokenization function to the dataset.
ds = ds.map(tokenize_function, batched=True)
Step 4: Configure Training Arguments
Set up the training arguments to control the fine-tuning process.
Define Training Arguments
:
Use
TrainingArguments
to specify parameters like batch size, learning rate, number of steps, and optimization settings.
Fine-tuning SmolLM involves setting up the environment, loading the model and dataset, configuring training parameters, and running the training loop. By following these steps, you can adapt SmolLM to your specific use case, whether it’s for reasoning tasks, chat-based applications, or other NLP tasks.
This process is highly customizable, so feel free to experiment with different datasets, hyperparameters, and training strategies to achieve the best results for your project.
Runs of prithivMLmods SmolLM2-CoT-360M on huggingface.co
87
Total runs
2
24-hour runs
14
3-day runs
15
7-day runs
18
30-day runs
More Information About SmolLM2-CoT-360M huggingface.co Model
SmolLM2-CoT-360M huggingface.co is an AI model on huggingface.co that provides SmolLM2-CoT-360M's model effect (), which can be used instantly with this prithivMLmods SmolLM2-CoT-360M model. huggingface.co supports a free trial of the SmolLM2-CoT-360M model, and also provides paid use of the SmolLM2-CoT-360M. Support call SmolLM2-CoT-360M model through api, including Node.js, Python, http.
SmolLM2-CoT-360M huggingface.co is an online trial and call api platform, which integrates SmolLM2-CoT-360M's modeling effects, including api services, and provides a free online trial of SmolLM2-CoT-360M, you can try SmolLM2-CoT-360M online for free by clicking the link below.
prithivMLmods SmolLM2-CoT-360M online free url in huggingface.co:
SmolLM2-CoT-360M is an open source model from GitHub that offers a free installation service, and any user can find SmolLM2-CoT-360M on GitHub to install. At the same time, huggingface.co provides the effect of SmolLM2-CoT-360M install, users can directly use SmolLM2-CoT-360M installed effect in huggingface.co for debugging and trial. It also supports api for free installation.