AI Dictionary of Terms

LoRA (Low-Rank Adaptation)

A parameter-efficient fine-tuning technique that adapts large language models by training only small, low-rank matrices instead of all model parameters, dramatically reducing the computational cost and memory requirements of fine-tuning.

The Simple Version

Imagine you have a massive, expensive sports car that is really fast but not quite right for driving on snowy roads. You could buy a whole new car designed for snow, but that would cost hundreds of thousands of dollars. Or, you could just put snow tires on your existing car — much cheaper, and it works great!

LoRA is like putting snow tires on an AI model. Instead of retraining the entire massive model (which costs a fortune in computing power), LoRA adds small, lightweight adapters that teach the model new tricks. The original model stays frozen, and only these tiny adapters get trained.

The result? You can customize a giant AI model for your specific needs at a fraction of the cost — sometimes 100x cheaper — while keeping almost all of the original model capabilities.

Detailed Explanation

LoRA is based on the hypothesis that the change in weights during adaptation also has a low intrinsic rank. Instead of updating the full weight matrix W during fine-tuning, LoRA decomposes the update into two smaller matrices.

Mathematical formulation:

Key parameters:

How it works:

  1. Freeze pre-trained weights: Original model parameters do not change
  2. Inject trainable matrices: Add low-rank decomposition matrices to specific layers
  3. Train only adapters: Only the small LoRA matrices are updated during training
  4. Merge at inference: LoRA weights can be merged with base model for zero inference overhead

Variants:

Key Characteristics

Business Context

LoRA has revolutionized enterprise AI by making model customization accessible to organizations without massive ML infrastructure.

Business impact:

Enterprise use cases:

Implementation considerations:

When to use LoRA:

Real-World Analogy

Adding a specialized lens to a camera. Your camera (the base model) is already excellent at taking photos. But for macro photography, you add a macro lens (LoRA adapter). The lens is small and inexpensive compared to buying a whole new camera system, but it gives you specialized capabilities for close-up shots. You can swap lenses for different photography styles without buying multiple cameras.

Code Example

# LoRA fine-tuning using Hugging Face PEFT library
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments
from peft import LoraConfig, get_peft_model, TaskType

# Load base model and tokenizer
model_name = "meta-llama/Llama-2-7b-hf"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Configure LoRA
lora_config = LoraConfig(
    task_type=TaskType.CAUSAL_LM,
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
)

# Apply LoRA to model
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()

# Training arguments
training_args = TrainingArguments(
    output_dir="./lora-model",
    num_train_epochs=3,
    per_device_train_batch_size=4,
    learning_rate=2e-4,
    fp16=True,
)

# Save and merge for deployment
model.save_pretrained("./lora-adapter")
merged_model = model.merge_and_unload()
merged_model.save_pretrained("./merged-model")

Common Misconceptions

Sources & Further Reading