CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
huggingface

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: huggingface/notebooks
Path: blob/main/course/en/chapter11/section3.ipynb
Views: 2935
Kernel: py310

Supervised Fine-Tuning with SFTTrainer

This notebook demonstrates how to fine-tune the HuggingFaceTB/SmolLM2-135M model using the SFTTrainer from the trl library. The notebook cells run and will finetune the model. You can select your difficulty by trying out different datasets.

Exercise: Fine-Tuning SmolLM2 with SFTTrainer

Take a dataset from the Hugging Face hub and finetune a model on it.

Difficulty Levels

🐢 Use the `HuggingFaceTB/smoltalk` dataset

🐕 Try out the `bigcode/the-stack-smol` dataset and finetune a code generation model on a specific subset `data/python`.

🦁 Select a dataset that relates to a real world use case your interested in

# Install the requirements in Google Colab # !pip install transformers datasets trl huggingface_hub # Authenticate to Hugging Face from huggingface_hub import login login() # for convenience you can create an environment variable containing your hub token as HF_TOKEN
# Import necessary libraries from transformers import AutoModelForCausalLM, AutoTokenizer from datasets import load_dataset from trl import SFTConfig, SFTTrainer, setup_chat_format import torch device = ( "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" ) # Load the model and tokenizer model_name = "HuggingFaceTB/SmolLM2-135M" model = AutoModelForCausalLM.from_pretrained( pretrained_model_name_or_path=model_name ).to(device) tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=model_name) # Set up the chat format model, tokenizer = setup_chat_format(model=model, tokenizer=tokenizer) # Set our name for the finetune to be saved &/ uploaded to finetune_name = "SmolLM2-FT-MyDataset" finetune_tags = ["smol-course", "module_1"]

Generate with the base model

Here we will try out the base model which does not have a chat template.

# Let's test the base model before training prompt = "Write a haiku about programming" # Format with template messages = [{"role": "user", "content": prompt}] formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False) # Generate response inputs = tokenizer(formatted_prompt, return_tensors="pt").to(device) outputs = model.generate(**inputs, max_new_tokens=100) print("Before training:") print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Dataset Preparation

We will load a sample dataset and format it for training. The dataset should be structured with input-output pairs, where each input is a prompt and the output is the expected response from the model.

TRL will format input messages based on the model's chat templates. They need to be represented as a list of dictionaries with the keys: role and content,.

# Load a sample dataset from datasets import load_dataset # TODO: define your dataset and config using the path and name parameters ds = load_dataset(path="HuggingFaceTB/smoltalk", name="everyday-conversations")
# TODO: 🦁 If your dataset is not in a format that TRL can convert to the chat template, you will need to process it. Refer to the [module](../chat_templates.md)

Configuring the SFTTrainer

The SFTTrainer is configured with various parameters that control the training process. These include the number of training steps, batch size, learning rate, and evaluation strategy. Adjust these parameters based on your specific requirements and computational resources.

# Configure the SFTTrainer sft_config = SFTConfig( output_dir="./sft_output", max_steps=1000, # Adjust based on dataset size and desired training duration per_device_train_batch_size=4, # Set according to your GPU memory capacity learning_rate=5e-5, # Common starting point for fine-tuning logging_steps=10, # Frequency of logging training metrics save_steps=100, # Frequency of saving model checkpoints evaluation_strategy="steps", # Evaluate the model at regular intervals eval_steps=50, # Frequency of evaluation use_mps_device=( True if device == "mps" else False ), # Use MPS for mixed precision training hub_model_id=finetune_name, # Set a unique name for your model ) # Initialize the SFTTrainer trainer = SFTTrainer( model=model, args=sft_config, train_dataset=ds["train"], tokenizer=tokenizer, eval_dataset=ds["test"], ) # TODO: 🦁 🐕 align the SFTTrainer params with your chosen dataset. For example, if you are using the `bigcode/the-stack-smol` dataset, you will need to choose the `content` column`

Training the Model

With the trainer configured, we can now proceed to train the model. The training process will involve iterating over the dataset, computing the loss, and updating the model's parameters to minimize this loss.

# Train the model trainer.train() # Save the model trainer.save_model(f"./{finetune_name}")
trainer.push_to_hub(tags=finetune_tags)

Bonus Exercise: Generate with fine-tuned model

🐕 Use the fine-tuned to model generate a response, just like with the base example..

# Test the fine-tuned model on the same prompt # Let's test the base model before training prompt = "Write a haiku about programming" # Format with template messages = [{"role": "user", "content": prompt}] formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False) # Generate response inputs = tokenizer(formatted_prompt, return_tensors="pt").to(device) # TODO: use the fine-tuned to model generate a response, just like with the base example.

💐 You're done!

This notebook provided a step-by-step guide to fine-tuning the HuggingFaceTB/SmolLM2-135M model using the SFTTrainer. By following these steps, you can adapt the model to perform specific tasks more effectively. If you want to carry on working on this course, here are steps you could try out:

  • Try this notebook on a harder difficulty

  • Review a colleagues PR

  • Improve the course material via an Issue or PR.