Skip to main content

Explain Axolotl with examples to fine tune Models

Axolotl is becoming popular in the fine-tuning space, especially for LLMs (Large Language Models).


🦎 What is Axolotl?

Axolotl is an open-source framework designed to make fine-tuning large language models (LLMs) simple, efficient, and flexible.
It's built to handle LoRA (Low-Rank Adaptation) and other parameter-efficient fine-tuning techniques with ease.

It is inspired by libraries like Hugging Face's Transformers, but adds convenient tools specifically for customizing, training, and managing LLM fine-tuning.


🔑 Key Features of Axolotl:

  1. Supports various fine-tuning methods:

    • Full fine-tuning.
    • LoRA (most commonly used!)
    • QLoRA (Quantized LoRA).
  2. Supports multiple model families:

    • LLaMA, Mistral, Falcon, GPT-NeoX, etc.
  3. Highly configurable with YAML files.

  4. Integrated with Hugging Face, so you can load pre-trained models easily.


🚀 Fine-tuning Example Workflow:

Objective:

Fine-tune a pre-trained LLaMA-2-7B model on a custom dataset of Q&A pairs using LoRA.


1️⃣ Install Axolotl

pip install axolotl

2️⃣ Prepare Data (JSONL Format)

Example dataset.jsonl:

{"instruction": "What is the capital of France?", "input": "", "output": "Paris"}
{"instruction": "Who wrote Hamlet?", "input": "", "output": "William Shakespeare"}

3️⃣ Create Config File (YAML)

Example config.yaml:

base_model: 'meta-llama/Llama-2-7b-hf'
dataset:
  path: './dataset.jsonl'
  format: 'alpaca'  # Supports various formats, alpaca-style Q&A is common
lora:
  r: 8
  alpha: 16
  dropout: 0.05
  target_modules: 'all-linear'
training:
  epochs: 3
  batch_size: 8
  learning_rate: 2e-4
output:
  output_dir: './llama2-finetuned'

4️⃣ Run Fine-tuning

axolotl train config.yaml

✅ That’s it! It handles:

  • Loading the base model.
  • Applying LoRA adapters.
  • Preparing the dataset.
  • Training loop & saving the output.

5️⃣ Inference Using Fine-tuned Model:

from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('./llama2-finetuned')
tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-hf')

input_text = "What is the capital of France?"
inputs = tokenizer(input_text, return_tensors='pt')
outputs = model.generate(**inputs)
print(tokenizer.decode(outputs[0]))

🌟 Advantages of Axolotl:

  • Simplifies fine-tuning for large models.
  • No need to write complex training scripts.
  • Easy LoRA integration to save GPU memory.
  • Supports advanced techniques like QLoRA, 4-bit quantization, etc.
  • YAML-based configs make experiments reproducible.

🔥 Use Cases:

Use Case How Axolotl Helps
Fine-tuning chatbots Quickly fine-tune Llama or Mistral on Q&A datasets.
Domain adaptation Train models on specific industry datasets (e.g., legal, medical).
Language-specific models Fine-tune multilingual models for a specific language.
Low-resource environments Use LoRA/QLoRA to fine-tune big models with limited hardware.

Here's a ready-to-run Axolotl fine-tuning setup tailored for you.


🎯 Objective:

Fine-tune a pre-trained LLaMA-2-7B model using LoRA on a custom Q&A dataset.


🟢 Folder Structure:

project/
├── dataset.jsonl        # Your Q&A dataset
├── config.yaml          # Axolotl config
└── run.sh               # Script to start training

1️⃣ Prepare Dataset (dataset.jsonl):

Each line is a JSON object:

{"instruction": "What is the capital of France?", "input": "", "output": "Paris"}
{"instruction": "Who developed Python?", "input": "", "output": "Guido van Rossum"}

2️⃣ Create Config File (config.yaml):

base_model: 'meta-llama/Llama-2-7b-hf'
dataset:
  path: './dataset.jsonl'
  format: 'alpaca'
lora:
  r: 8
  alpha: 16
  dropout: 0.05
  target_modules: 'all-linear'
training:
  epochs: 3
  batch_size: 4
  learning_rate: 2e-4
  save_every_n_steps: 500
output:
  output_dir: './llama2-finetuned'
tokenizer:
  truncation_side: 'left'
  padding_side: 'right'
  max_length: 2048

3️⃣ Create Training Script (run.sh):

#!/bin/bash

# Install dependencies
pip install axolotl transformers datasets

# Start training
axolotl train config.yaml

Make it executable:

chmod +x run.sh

4️⃣ Run Fine-tuning:

./run.sh

5️⃣ Inference After Fine-tuning:

from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained('./llama2-finetuned')
tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-hf')

input_text = "Who developed Python?"
inputs = tokenizer(input_text, return_tensors='pt')
outputs = model.generate(**inputs, max_length=50)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

🚀 Quick Customization:

  • Different Model:
    Change base_model in config.yaml to any supported Hugging Face model, e.g., mistralai/Mistral-7B-v0.1.

  • Dataset Format:
    Supports alpaca, vicuna, oasst, etc.
    You can adjust based on dataset style.

  • LoRA Parameters:
    Tune r, alpha, dropout depending on memory budget.


Hardware Requirements:

  • Minimum: 1 GPU (24GB VRAM recommended for 7B models with LoRA)
  • For QLoRA: You can even run with lower VRAM (12-16GB) using quantization!


Summary:

Axolotl = Lightweight, configurable, and efficient framework to fine-tune large language models (LLMs), especially with LoRA/QLoRA.



Comments

Popular posts from this blog

Simple Linear Regression - and Related Regression Loss Functions

Today's Topics: a. Regression Algorithms  b. Outliers - Explained in Simple Terms c. Common Regression Metrics Explained d. Overfitting and Underfitting e. How are Linear and Non Linear Regression Algorithms used in Neural Networks [Future study topics] Regression Algorithms Regression algorithms are a category of machine learning methods used to predict a continuous numerical value. Linear regression is a simple, powerful, and interpretable algorithm for this type of problem. Quick Example: These are the scores of students vs. the hours they spent studying. Looking at this dataset of student scores and their corresponding study hours, can we determine what score someone might achieve after studying for a random number of hours? Example: From the graph, we can estimate that 4 hours of daily study would result in a score near 80. It is a simple example, but for more complex tasks the underlying concept will be similar. If you understand this graph, you will understand this blog. Sim...

What problems can AI Neural Networks solve

How does AI Neural Networks solve Problems? What problems can AI Neural Networks solve? Based on effectiveness and common usage, here's the ranking from best to least suitable for neural networks (Classification Problems, Regression Problems and Optimization Problems.) But first some Math, background and related topics as how the Neural Network Learn by training (Supervised Learning and Unsupervised Learning.)  Background Note - Mathematical Precision vs. Practical AI Solutions. Math can solve all these problems with very accurate results. While Math can theoretically solve classification, regression, and optimization problems with perfect accuracy, such calculations often require impractical amounts of time—hours, days, or even years for complex real-world scenarios. In practice, we rarely need absolute precision; instead, we need actionable results quickly enough to make timely decisions. Neural networks excel at this trade-off, providing "good enough" solutions in seco...

Activation Functions in Neural Networks

  A Guide to Activation Functions in Neural Networks 🧠 Question: Without activation function can a neural network with many layers be non-linear? Answer: Provided at the end of this document. Activation functions are a crucial component of neural networks. Their primary purpose is to introduce non-linearity , which allows the network to learn the complex, winding patterns found in real-world data. Without them, a neural network, no matter how deep, would just be a simple linear model. In the diagram below the f is the activation function that receives input and send output to next layers. Commonly used activation functions. 1. Sigmoid Function 2. Tanh (Hyperbolic Tangent) 3. ReLU (Rectified Linear Unit - Like an Electronic Diode) 4. Leaky ReLU & PReLU 5. ELU (Exponential Linear Unit) 6. Softmax 7. GELU, Swish, and SiLU 1. Sigmoid Function                       The classic "S-curve," Sigmoid squashes any input value t...