Unleashing the Power of Tiny-Llama: Fine-Tuning with Unsloth for Efficiency and Performance

Introduction

In the rapidly evolving landscape of large language models (LLMs), the quest for improved efficiency and accessibility has given rise to a new breed of compact yet powerful models. Among these emerging stars, Tiny-Llama stands out as a game-changer, packing an impressive 1.1 billion parameters while delivering performance comparable to larger models. Fine-tuning plays a crucial role in unlocking the full potential of these base models for specific downstream tasks. In this article, we‘ll dive into the world of Tiny-Llama, explore efficient fine-tuning techniques like LoRA and QLoRA, and walk through the process of fine-tuning a Tiny-Llama-1B model on the Alpaca dataset using the Unsloth platform.

The Rise of Tiny-Llama and Efficient LLMs

Traditional LLMs, with their billions of parameters, have revolutionized natural language processing tasks. However, their massive size poses challenges in terms of computational resources and deployment on resource-constrained devices. This has led to a growing interest in developing smaller, more efficient models that can maintain high performance while being accessible to a wider range of users and applications.

Tiny-Llama, a model trained on an impressive 3 trillion tokens, has emerged as a frontrunner in this space. With just 1.1 billion parameters, Tiny-Llama achieves results comparable to larger models like GPT-3.5 Turbo and Claude, making it an attractive choice for applications where model size and efficiency are critical factors. The success of Tiny-Llama has paved the way for other sub-7B parameter models like Microsoft‘s Phi-2 and Alibaba‘s Qwen-3b, showcasing the immense potential of compact LLMs.

Fine-Tuning: Adapting Models to Specific Tasks

While pre-trained LLMs excel at general language understanding tasks, they often require fine-tuning to perform optimally on specific downstream tasks. Fine-tuning involves adapting the pre-trained model to learn new knowledge and specialize in a particular domain or task. This process is essential for molding base models into different avatars tailored to specific use cases, such as chatbots, content generation, or code completion.

The quality of fine-tuned models depends on several factors, including the quality of the training data, the capabilities of the base model, and the fine-tuning techniques employed. Two popular fine-tuning techniques that have gained traction are LoRA (Low-rank Adaptation) and QLoRA (Quantized LoRA).

LoRA focuses on updating a subset of trainable parameters through a low-rank approximation of the original weight matrices. By selectively updating these parameters, LoRA enables faster fine-tuning on less compute-intensive hardware. QLoRA takes this a step further by quantizing the model weights to lower floating-point precision before applying LoRA. Quantization involves downcasting higher bit values to lower values, resulting in a substantial reduction in model size while maintaining comparable accuracy to the original model.

Unsloth: Accelerating Fine-Tuning with Triton Optimizations

To harness the full potential of fine-tuning techniques like LoRA and QLoRA, efficient tooling and optimizations are essential. This is where Unsloth comes into play. Unsloth is an open-source platform that enables faster fine-tuning of popular LLMs, including Llama-2, Mistral, and their derivatives. By leveraging custom Triton kernels and a manual back-propagation engine, Unsloth significantly improves the speed of model training.

In the following sections, we‘ll walk through the process of fine-tuning a 4-bit quantized Tiny-Llama model on the Alpaca dataset using Unsloth. We‘ll also explore how to log training metrics using Weights and Biases (WandB) for better tracking and visualization of the fine-tuning process.

Fine-Tuning Tiny-Llama with Unsloth

To get started with fine-tuning Tiny-Llama using Unsloth, we‘ll need a machine with sufficient VRAM (around 10-15 GB) or access to a cloud-based GPU runtime like Google Colab‘s free Tesla T4 GPU.

Step 1: Installation and Setup

First, install Unsloth and WandB using the following commands:

%%capture
import torch
major_version, minor_version = torch.cuda.get_device_capability()
!pip install wandb
if major_version >= 8:
    # Use this for new GPUs like Ampere, Hopper GPUs (RTX 30xx, RTX 40xx, A100, H100, L40)
    !pip install "unsloth[colab_ampere] @ git+https://github.com/unslothai/unsloth.git"
else:
    # Use this for older GPUs (V100, Tesla T4, RTX 20xx)
    !pip install "unsloth[colab] @ git+https://github.com/unslothai/unsloth.git"

Next, load the 4-bit quantized pre-trained Tiny-Llama model using Unsloth:

from unsloth import FastLanguageModel
import torch

max_seq_length = 4096
dtype = None
load_in_4bit = True

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name="unsloth/tinyllama-bnb-4bit",
    max_seq_length=max_seq_length,
    dtype=dtype,
    load_in_4bit=load_in_4bit,
)

The 4-bit quantized model size will be around 760 MB, making it memory-efficient.

Step 2: Applying PEFT

Apply PEFT (Parameter-Efficient Fine-Tuning) to the 4-bit Tiny-Llama model using the following code:

model = FastLanguageModel.get_peft_model(
    model,
    r=32,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
    lora_alpha=32,
    lora_dropout=0,
    bias="none",
    use_gradient_checkpointing=True,
    random_state=3407,
    use_rslora=False,
    loftq_config=None,
)

This step prepares the model for efficient fine-tuning using techniques like LoRA.

Step 3: Preparing the Dataset

We‘ll use the cleaned Alpaca dataset for fine-tuning. The dataset follows an instruction-input-response format. Prepare the dataset using the following code:

from datasets import load_dataset

EOS_TOKEN = tokenizer.eos_token

def formatting_prompts_func(examples):
    instructions = examples["instruction"]
    inputs = examples["input"]
    outputs = examples["output"]
    texts = []
    for instruction, input, output in zip(instructions, inputs, outputs):
        text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
        texts.append(text)
    return {"text": texts}

dataset = load_dataset("yahma/alpaca-cleaned", split="train")
dataset = dataset.map(formatting_prompts_func, batched=True,)
dataset_dict = dataset.train_test_split(test_size=0.004)

Step 4: Configuring WandB

Configure Weights and Biases (WandB) for logging training metrics:

import wandb
wandb.login()
%env WANDB_WATCH=all
%env WANDB_SILENT=true

Provide your WandB API key when prompted.

Step 5: Training the Model

Define the training arguments and initialize the SFTTrainer from the Trl library:

from trl import SFTTrainer
from transformers import TrainingArguments

args = TrainingArguments(
    per_device_train_batch_size=2,
    per_device_eval_batch_size=2,
    gradient_accumulation_steps=4,
    evaluation_strategy="steps",
    warmup_ratio=0.1,
    num_train_epochs=1,
    learning_rate=2e-5,
    fp16=not torch.cuda.is_bf16_supported(),
    bf16=torch.cuda.is_bf16_supported(),
    optim="adamw_8bit",
    weight_decay=0.1,
    lr_scheduler_type="linear",
    seed=3407,
    output_dir="outputs",
    report_to="wandb",
    logging_steps=1,
    logging_strategy="steps",
    save_total_limit=2,
)

trainer = SFTTrainer(
    model=model,
    tokenizer=tokenizer,
    train_dataset=dataset_dict["train"],
    eval_dataset=dataset_dict["test"],
    dataset_text_field="text",
    max_seq_length=max_seq_length,
    dataset_num_proc=2,
    packing=True,
    args=args,
)

Start the training process:

trainer_stats = trainer.train()
wandb.finish()

During training, WandB will track and visualize the training and evaluation metrics in real-time on the provided dashboard link.

Step 6: Inferencing

After training, you can save the LoRA adapters locally or push them to a Hugging Face repository. To perform inference using the fine-tuned model, load the saved model and generate responses:

from transformers import TextStreamer

inputs = tokenizer([
    alpaca_prompt.format(
        "capital of France?",
        "",
        "",
    )
]*1, return_tensors="pt").to("cuda")

outputs = model.generate(**inputs, max_new_tokens=64, use_cache=True)
tokenizer.batch_decode(outputs)

text_streamer = TextStreamer(tokenizer)
_ = model.generate(**inputs, streamer=text_streamer, max_new_tokens=64)

Advantages of Tiny-Llama and Future Potential

The success of Tiny-Llama and other sub-7B parameter models highlights the immense potential of compact LLMs. By achieving high performance while maintaining a smaller model size, these models open up new possibilities for deploying language models on resource-constrained devices like personal computers, mobile phones, and wearables.

Fine-tuning plays a crucial role in adapting these models to specific tasks and domains, enabling them to excel in various applications such as chatbots, content generation, and code completion. The availability of efficient fine-tuning techniques like LoRA and QLoRA, along with optimized tooling like Unsloth, has made the fine-tuning process more accessible and streamlined.

As the field of LLMs continues to evolve, we can expect further advancements in developing compact models that push the boundaries of efficiency and performance. The success of Tiny-Llama serves as an inspiration for researchers and developers to explore new architectures, training techniques, and optimizations that can unlock the full potential of sub-7B parameter models.

Conclusion

In this article, we explored the world of Tiny-Llama, a powerful and efficient LLM that has captured the attention of the AI community. We discussed the importance of fine-tuning in adapting pre-trained models to specific tasks and walked through the process of fine-tuning a Tiny-Llama-1B model on the Alpaca dataset using the Unsloth platform.

By leveraging efficient fine-tuning techniques like LoRA and QLoRA, along with the optimizations provided by Unsloth, we can unlock the full potential of Tiny-Llama and other compact LLMs. The ability to log training metrics using tools like Weights and Biases further enhances the fine-tuning experience by providing valuable insights and visualizations.

As the field of LLMs continues to evolve, the emergence of models like Tiny-Llama showcases the immense potential of compact models in pushing the boundaries of efficiency and accessibility. By harnessing the power of these models through fine-tuning, we can develop sophisticated language applications that cater to a wide range of use cases and deployments.

The future of LLMs looks bright, with the promise of even more efficient and powerful models on the horizon. As researchers and developers continue to innovate and explore new techniques, we can expect to see a proliferation of compact models that deliver impressive performance while being accessible to a broader audience.

So, whether you‘re a researcher, developer, or enthusiast, embracing the potential of Tiny-Llama and other sub-7B parameter models opens up a world of possibilities. By mastering the art of fine-tuning and leveraging powerful tools like Unsloth, you can unleash the full potential of these models and contribute to the exciting future of language AI.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts