Parameter-Efficient Fine-Tuning with LoRA
Parameter-efficient fine-tuning with LoRA (Low-Rank Adaptation) is a technique designed to adapt a pretrained model to a specific task by adjusting only a small subset of the model’s weight parameters. This approach significantly reduces the computational costs and resources required for fine-tuning, making it an efficient alternative to traditional methods.
Overview
The core idea behind LoRA is to approximate the weight update matrix using two smaller matrices, A and B. By doing so, the number of parameters that need to be adjusted during fine-tuning is minimized, which in turn reduces the computational overhead. This method is particularly useful when working with large language models, where full fine-tuning can be resource-intensive.
Preparing the Dataset
Before applying LoRA, it is essential to prepare the dataset that will be used for fine-tuning. The following code snippet demonstrates how to download and prepare a dataset for this purpose:
Listing E.1 Downloading and preparing the dataset
from pathlib import Path
import pandas as pd
from ch06 import (
download_and_unzip_spam_data,
create_balanced_dataset,
random_split
)
url = \
"https://archive.ics.uci.edu/static/public/228/sms+spam+collection.zip"
zip_path = "sms_spam_collection.zip"
extracted_path = "sms_spam_collection"
data_file_path = Path(extracted_path) / "SMSSpamCollection.tsv"
download_and_unzip_spam_data(url, zip_path, extracted_path, data_file_path)
df = pd.read_csv(
data_file_path, sep="\t", header=None, names=["Label", "Text"]
)
balanced_df = create_balanced_dataset(df)
balanced_df["Label"] = balanced_df["Label"].map({"ham": 0, "spam": 1})
train_df, validation_df, test_df = random_split(balanced_df, 0.7, 0.1)
train_df.to_csv("train.csv", index=None)
validation_df.to_csv("validation.csv", index=None)
test_df.to_csv("test.csv", index=None)
This code downloads a dataset from the UCI Machine Learning Repository, processes it to create a balanced dataset, and splits it into training, validation, and test sets.
Initializing the Model
Once the dataset is ready, the next step is to initialize a pretrained model. The following code snippet shows how to load a pretrained GPT model:
Listing E.4 Loading a pretrained GPT model
from gpt_download import download_and_load_gpt2
from chapter04 import GPTModel
from chapter05 import load_weights_into_gpt
CHOOSE_MODEL = "gpt2-small (124M)"
INPUT_PROMPT = "Every effort moves"
BASE_CONFIG = {
"vocab_size": 50257, #1
"context_length": 1024, #2
"drop_rate": 0.0, #3
"qkv_bias": True #4
}
model_configs = {
"gpt2-small (124M)": {"emb_dim": 768, "n_layers": 12, "n_heads": 12},
"gpt2-medium (355M)": {"emb_dim": 1024, "n_layers": 24, "n_heads": 16},
"gpt2-large (774M)": {"emb_dim": 1280, "n_layers": 36, "n_heads": 20},
"gpt2-xl (1558M)": {"emb_dim": 1600, "n_layers": 48, "n_heads": 25},
}
BASE_CONFIG.update(model_configs[CHOOSE_MODEL])
model_size = CHOOSE_MODEL.split(" ")[-1].lstrip("(").rstrip(")")
settings, params = download_and_load_gpt2(
model_size=model_size, models_dir="gpt2"
)
model = GPTModel(BASE_CONFIG)
load_weights_into_gpt(model, params)
model.eval()
#1 Vocabulary size
#2 Context length
#3 Dropout rate
#4 Query-key-value bias
This code snippet demonstrates how to download and load a GPT-2 model, configure its parameters, and prepare it for evaluation.
Applying LoRA
To apply parameter-efficient fine-tuning with LoRA, the Linear layers in the model are replaced with LinearWithLoRA layers. This involves creating LoRALayer components that adjust a small subset of the model’s weight parameters. By doing so, the model can be fine-tuned efficiently with reduced computational costs. This approach allows for the adaptation of large models to specific tasks without the need for extensive computational resources.