$ ls ./menu

© 2025 ESSA MAMDANI

cd ../blog
6 min read

Fine tuning an SLM or an LLM — a practical example using ...

Audio version coming soon
Fine tuning an SLM or an LLM — a practical example using ...
Verified by Essa Mamdani

Fine-Tuning SLMs and LLMs: From Penguins to Production with Llama 2

The exponential growth of Large Language Models (LLMs) and their smaller counterparts, Small Language Models (SLMs), has democratized access to powerful AI. But leveraging these models effectively isn't just about deploying pre-trained behemoths. Fine-tuning offers a crucial pathway to adapting these general-purpose engines to specific tasks, unlocking unparalleled precision and efficiency. This article delves into the practicalities of fine-tuning, using an unconventional, yet illustrative example: creating a model specialized in vertical woodworking with "penguin wood," mirroring the playful approach outlined by Martin Keywood, but pushing the technical envelope with Llama 2.

The Imperative of Fine-Tuning: Beyond General Purpose

Pre-trained LLMs, while impressive, are inherently generalists. They possess vast knowledge across domains but lack the nuanced understanding required for specialized applications. Consider, for instance, a customer support chatbot for a niche woodworking company. While a general LLM can answer basic questions, it would struggle with jargon, specific product details, or troubleshooting unusual scenarios involving specialized materials like, well, penguin wood.

Fine-tuning allows us to inject domain-specific knowledge and tailor model behavior. This results in:

  • Increased Accuracy: Reduced hallucination and improved factual correctness within the targeted domain.
  • Enhanced Efficiency: Faster inference times and lower computational costs compared to running inference on a full-sized, untuned LLM.
  • Customized Behavior: The ability to mold the model's response style, tone, and problem-solving approach.
  • Data Privacy: Fine-tuning on internal datasets ensures data stays within your control, addressing privacy concerns associated with using external, publicly available models.

Penguin Woodworking: An Absurdly Practical Example

Let's embrace the absurdity. Imagine we're building an AI assistant for carpenters specializing in "penguin wood" – a hypothetical, rare, and incredibly challenging material sourced ethically (of course!). This requires the model to understand:

  • The Properties of Penguin Wood: Its unique grain, strength, workability, and potential challenges.
  • Specialized Tools: Tools designed for vertical surfaces and the specific handling requirements of the material.
  • Construction Techniques: Joinery methods, finishing techniques, and safety protocols unique to penguin wood construction.

A pre-trained LLM wouldn't have this knowledge. Fine-tuning is the only path to building a functional "penguin wood AI."

Choosing the Right Model: SLM vs. LLM for the Task

The first critical decision is selecting the base model for fine-tuning. LLMs offer superior performance but demand significantly more computational resources. SLMs, on the other hand, are more efficient and cost-effective but may sacrifice some accuracy.

For our penguin wood example, let's consider Llama 2 (either the 7B or 13B parameter version) for its balance of performance and accessibility. Other viable options include alternatives such as Falcon or smaller variants of OPT. The choice depends on the desired accuracy and available resources.

Dataset Creation: The Foundation of Fine-Tuning

The quality of the fine-tuning dataset directly impacts the model's performance. For our penguin wood project, we need a dataset consisting of:

  • Q&A Pairs: Questions about penguin wood and their expert-level answers.
  • Instruction-Following Examples: Instructions on performing specific tasks, such as cutting or joining penguin wood.
  • Dialogue Examples: Conversations between carpenters discussing challenges and solutions related to penguin wood construction.

Creating this dataset can involve:

  • Expert Interviews: Gathering knowledge from woodworking professionals with (hypothetical) experience in penguin wood.
  • Synthetic Data Generation: Using a pre-trained LLM to generate realistic scenarios and dialogues based on a seed knowledge base.
  • Data Augmentation: Expanding the dataset by paraphrasing existing examples and adding variations.

Example Dataset Snippet (JSON format):

json
1[
2  {
3    "instruction": "How should I prepare penguin wood for planing?",
4    "output": "Penguin wood is prone to tearout. Use a very sharp plane iron, set the mouth extremely fine, and plane with the grain. Consider a skew angle for added smoothness."
5  },
6  {
7    "instruction": "What's the best adhesive for joining penguin wood?",
8    "output": "Traditional hide glue offers excellent adhesion and reversibility for penguin wood. Epoxy can also be used, but be mindful of potential discoloration."
9  },
10  {
11    "instruction": "Describe the optimal method for sanding penguin wood.",
12    "output": "Begin with a coarser grit (e.g., 120) and gradually increase to finer grits (e.g., 320). Use a sanding block to maintain even pressure and prevent rounding of edges. Vacuum frequently to remove dust."
13  }
14]

Fine-Tuning Techniques: Parameter-Efficient Methods

Fine-tuning large models from scratch is computationally expensive. Parameter-Efficient Fine-Tuning (PEFT) techniques offer a more practical alternative by only updating a small subset of the model's parameters. Popular PEFT methods include:

  • LoRA (Low-Rank Adaptation): Adds trainable rank decomposition matrices to existing weights, significantly reducing the number of trainable parameters.
  • Prefix-Tuning: Adds a trainable prefix to the input sequence, influencing the model's output without modifying the original parameters.
  • P-Tuning: Optimizes continuous prompts to guide the model's behavior.

For our Llama 2 penguin wood project, LoRA is a suitable choice due to its simplicity and effectiveness.

Implementation with Hugging Face Transformers

The Hugging Face Transformers library provides a powerful and user-friendly interface for fine-tuning LLMs. Here's a simplified code snippet demonstrating LoRA fine-tuning:

python
1from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
2from peft import LoraConfig, get_peft_model
3
4# 1. Load the pre-trained model and tokenizer
5model_name = "meta-llama/Llama-2-7b-chat-hf"  # Or your chosen model
6model = AutoModelForCausalLM.from_pretrained(model_name)
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8tokenizer.pad_token = tokenizer.eos_token #Crucial for Llama 2
9
10# 2. Configure LoRA
11lora_config = LoraConfig(
12    r=8, # Rank of the LoRA matrices
13    lora_alpha=32,
14    lora_dropout=0.05,
15    bias="none",
16    task_type="CAUSAL_LM",
17    target_modules=["q_proj", "v_proj"] #Specify layers for LoRA to be applied
18)
19model = get_peft_model(model, lora_config)
20model.print_trainable_parameters()
21
22# 3. Prepare the dataset (assuming 'dataset' is a Hugging Face Dataset object)
23def tokenize_function(examples):
24    return tokenizer(examples["instruction"] + examples["output"], truncation=True, padding="max_length", max_length=512)
25
26tokenized_datasets = dataset.map(tokenize_function, batched=True)
27
28# 4. Define training arguments
29training_args = TrainingArguments(
30    output_dir="./penguin_wood_model",
31    per_device_train_batch_size=4,
32    gradient_accumulation_steps=4,
33    learning_rate=2e-4,
34    logging_steps=10,
35    max_steps=1000,
36    save_steps=200,
37    push_to_hub=False #Set to True and configure for model upload
38)
39
40# 5. Train the model
41trainer = Trainer(
42    model=model,
43    args=training_args,
44    train_dataset=tokenized_datasets["train"],
45    tokenizer=tokenizer,
46)
47
48trainer.train()

This code snippet demonstrates the core steps of fine-tuning with LoRA: loading the model, configuring LoRA, preparing the dataset, defining training arguments, and training the model. This is a basic example, and further experimentation with hyperparameters and dataset engineering is often necessary to achieve optimal performance. The key line, target_modules=["q_proj", "v_proj"], specifically targets the query and value projection layers within the attention mechanism for modification, which is a common and effective approach for LoRA fine-tuning.

Evaluation and Deployment

After fine-tuning, it's crucial to evaluate the model's performance on a held-out dataset. Metrics such as perplexity, BLEU score, and human evaluation can be used to assess accuracy and fluency.

Deployment options depend on the application. The fine-tuned model can be deployed:

  • Locally: On a server or desktop for private use.
  • On the Cloud: Using platforms like AWS SageMaker, Google Cloud AI Platform, or Azure Machine Learning.
  • Through an API: Exposing the model as an API endpoint for integration with other applications.

Actionable Takeaways

  • Embrace Domain-Specific Fine-Tuning: Don't rely solely on pre-trained LLMs for specialized tasks. Invest in fine-tuning to achieve superior accuracy and efficiency.
  • Prioritize Dataset Quality: A well-curated dataset is the foundation of successful fine-tuning. Focus on collecting high-quality data that reflects the target domain.
  • Explore Parameter-Efficient Techniques: LoRA, Prefix-Tuning, and P-Tuning offer practical solutions for fine-tuning large models with limited resources.
  • Experiment and Iterate: Fine-tuning is an iterative process. Experiment with different hyperparameters, datasets, and training techniques to optimize performance.
  • Continuous Monitoring and Improvement: Monitor the model's performance in production and retrain it periodically with new data to maintain accuracy and relevance.

By mastering the art of fine-tuning, we can unlock the full potential of LLMs and SLMs, transforming them from general-purpose tools into highly specialized and impactful AI solutions, even if it involves the (hypothetical) world of penguin woodworking.

Source

https://medium.com/@martinkeywood/fine-tuning-an-slm-or-an-llm-a-practical-example-using-an-impractical-topic-8c1d6fe6d14a