π Adapting Pre-Trained Foundation Models to Domain-Specific Tasks
Training large language models from scratch requires immense computational budgets and massive data corpora. Instead, modern AI engineering relies on Transfer Learning and Fine-Tuning: taking a pre-trained foundation model (such as BERT for encoder tasks or LLaMA/GPT for generative tasks) and adapting its weights on specialized downstream datasets. While full fine-tuning updates all model parameters, modern Parameter-Efficient Fine-Tuning (PEFT) techniques like LoRA (Low-Rank Adaptation) freeze base weights and inject trainable rank decomposition matrices, drastically reducing GPU memory footprints. This masterclass covers 10 real-world production examples of BERT and LLM fine-tuning.
import torch
def simulate_bert_tokenizer():
print("=" * 65)
print("π€ EXAMPLE 1: BERT TOKENIZATION & TENSOR PADDING PIPELINE")
print("=" * 65)
# Simulating Hugging Face tokenizer output for batch of 2 sentences
batch_sentences = [
"Fine-tuning BERT on custom enterprise data.",
"Low-Rank Adaptation saves GPU VRAM."
]
# Mock tokenized tensors (input_ids, attention_mask)
max_length = 8
input_ids = torch.tensor([
[101, 2045, 1000, 1024, 4932, 2305, 1012, 102], # Sentence 1 with [CLS] and [SEP]
[101, 2128, 1000, 3982, 1001, 7842, 4510, 102] # Sentence 2
])
attention_mask = torch.tensor([
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
])
print(f"Sentences Processed : {len(batch_sentences)}")
print(f"Input IDs Tensor :\n{input_ids}")
print(f"Attention Mask Tensor:\n{attention_mask}")
print("=" * 65)
if __name__ == "__main__":
simulate_bert_tokenizer()
import torch
import torch.nn as nn
class BERTForFinancialSentiment(nn.Module):
def __init__(self, hidden_dim=768, num_classes=3):
super().__init__()
# Simulating base BERT encoder output mapping [CLS] token to sentiment classes
self.classifier = nn.Linear(hidden_dim, num_classes)
def forward(self, cls_embeddings):
logits = self.classifier(cls_embeddings)
return logits
def simulate_financial_bert():
print("=" * 65)
print("π EXAMPLE 2: BERT FINANCIAL SENTIMENT CLASSIFICATION HEAD")
print("=" * 65)
model = BERTForFinancialSentiment(num_classes=3)
model.eval()
# Batch=4 financial news headlines [CLS] representations (dim = 768)
cls_representations = torch.randn(4, 768)
with torch.no_grad():
logits = model(cls_representations)
probabilities = torch.softmax(logits, dim=1)
print(f"CLS Representation Shape: {cls_representations.shape}")
print(f"Sentiment Logits Shape : {logits.shape}")
print(f"Sample Probabilities : {probabilities[0].round(3).tolist()} (Classes: Bearish, Neutral, Bullish)")
print("=" * 65)
if __name__ == "__main__":
simulate_financial_bert()
import torch
import torch.nn as nn
def simulate_bert_ner():
print("=" * 65)
print("𧬠EXAMPLE 3: BERT TOKEN-LEVEL NER CLASSIFIER")
print("=" * 65)
# Sequence of 12 tokens, hidden dimension 768 from BERT encoder
sequence_hidden_states = torch.randn(2, 12, 768) # Batch=2, SeqLen=12
# Token classifier predicting BIO tags (Begin, Inside, Outside for Gene/Disease)
num_ner_tags = 5 # O, B-Gene, I-Gene, B-Disease, I-Disease
ner_classifier = nn.Linear(768, num_ner_tags)
token_logits = ner_classifier(sequence_hidden_states)
print(f"Hidden States Shape : {sequence_hidden_states.shape}")
print(f"Token Logits Shape : {token_logits.shape}")
print(f"Task Objective : Classify every token in the clinical sentence independently")
print("=" * 65)
if __name__ == "__main__":
simulate_bert_ner()
import torch
import torch.nn as nn
def simulate_legal_summarization():
print("=" * 65)
print("βοΈ EXAMPLE 4: LEGAL ENCODER-DECODER SUMMARIZATION HEAD")
print("=" * 65)
# Encoder output for long legal document [Batch=1, DocLen=512, Hidden=1024]
encoder_hidden = torch.randn(1, 512, 1024)
# Decoder projection head mapping hidden states to vocabulary logits for summary generation
vocab_size = 32000
summarizer_head = nn.Linear(1024, vocab_size)
summary_logits = summarizer_head(encoder_hidden)
print(f"Encoder Document State: {encoder_hidden.shape}")
print(f"Summary Vocab Logits : {summary_logits.shape}")
print(f"Production Utility : Condensing 50-page contracts into 3-bullet executive briefs")
print("=" * 65)
if __name__ == "__main__":
simulate_legal_summarization()
import torch
import torch.nn as nn
class LoRALinearLayer(nn.Module):
def __init__(self, in_features, out_features, rank=8, alpha=16):
super().__init__()
# Frozen base weight matrix W0
self.weight = nn.Parameter(torch.randn(out_features, in_features), requires_grad=False)
# Trainable low-rank adaptation matrices A and B
self.lora_A = nn.Parameter(torch.randn(rank, in_features) * 0.01)
self.lora_B = nn.Parameter(torch.zeros(out_features, rank))
self.scaling = alpha / rank
def forward(self, x):
# Base linear pass + Low-rank adaptation delta pass
base_out = torch.matmul(x, self.weight.t())
lora_out = torch.matmul(torch.matmul(x, self.lora_A.t()), self.lora_B.t()) * self.scaling
return base_out + lora_out
def simulate_lora():
print("=" * 65)
print("β‘ EXAMPLE 5: LOW-RANK ADAPTATION (LoRA) FOR LLMs")
print("=" * 65)
in_dim, out_dim = 4096, 4096
lora_layer = LoRALinearLayer(in_features=in_dim, out_features=out_dim, rank=8, alpha=16)
# Count parameters
total_params = in_dim * out_dim
trainable_params = (8 * in_dim) + (out_dim * 8)
x = torch.randn(2, 64, in_dim) # Batch=2, SeqLen=64
output = lora_layer(x)
print(f"Base Weight Parameters : {total_params:,} (Frozen)")
print(f"LoRA Trainable Params : {trainable_params:,} ({trainable_params/total_params*100:.2f}% of total)")
print(f"Output Tensor Shape : {output.shape}")
print("=" * 65)
if __name__ == "__main__":
simulate_lora()
import torch
import torch.nn as nn
class LLMRewardModel(nn.Module):
def __init__(self, hidden_dim=4096):
super().__init__()
# Score head mapping final hidden state to a scalar reward value
self.score_head = nn.Linear(hidden_dim, 1)
def forward(self, response_hidden_states):
# Extract last token hidden state and compute scalar reward score
last_token_hidden = response_hidden_states[:, -1, :]
reward_score = self.score_head(last_token_hidden)
return reward_score
def simulate_reward_model():
print("=" * 65)
print("π― EXAMPLE 6: RLHF REWARD MODEL SCORING HEAD")
print("=" * 65)
reward_model = LLMRewardModel(hidden_dim=4096)
reward_model.eval()
# Batch=2 response sequences [Batch=2, SeqLen=128, Hidden=4096]
chosen_response = torch.randn(1, 128, 4096)
rejected_response = torch.randn(1, 128, 4096)
with torch.no_grad():
score_chosen = reward_model(chosen_response)
score_rejected = reward_model(rejected_response)
print(f"Chosen Response Reward : {score_chosen.item():.4f} (Preferred)")
print(f"Rejected Response Reward : {score_rejected.item():.4f}")
print(f"Loss Objective : Margin ranking loss maximizing chosen vs rejected score gap")
print("=" * 65)
if __name__ == "__main__":
simulate_reward_model()
import torch
import torch.nn.functional as F
def simulate_instruction_tuning():
print("=" * 65)
print("π€ EXAMPLE 7: CAUSAL LANGUAGE MODELING INSTRUCTION LOSS")
print("=" * 65)
vocab_size = 32000
batch_size = 2
seq_len = 32
# Simulated logits predicted by LLM [Batch, SeqLen, VocabSize]
logits = torch.randn(batch_size, seq_len, vocab_size)
# Ground truth target token IDs shifted by 1
targets = torch.randint(0, vocab_size, (batch_size, seq_len))
# Compute Cross-Entropy Loss ignoring prompt tokens (masking prompt loss)
loss = F.cross_entropy(logits.view(-1, vocab_size), targets.view(-1))
print(f"Logits Tensor Shape : {logits.shape}")
print(f"Targets Tensor Shape : {targets.shape}")
print(f"Next-Token Loss : {loss.item():.4f}")
print(f"Training Objective : Autoregressive next-token prediction across instruction datasets")
print("=" * 65)
if __name__ == "__main__":
simulate_instruction_tuning()
import torch
def simulate_code_completion():
print("=" * 65)
print("π» EXAMPLE 8: ENTERPRISE CODE LLM FINE-TUNING")
print("=" * 65)
# Prompt: "def calculate_tax(income):" -> Target completion: " return income * 0.25"
prompt_tokens = torch.tensor([[15, 342, 8921, 23, 492, 7]], dtype=torch.long) # Batch=1, Len=6
# Simulating LLM generation step with temperature and top-k sampling
vocab_size = 50000
generation_head = torch.nn.Linear(768, vocab_size)
# Mock hidden states for prompt ending
hidden_state = torch.randn(1, 1, 768)
logits = generation_head(hidden_state)
next_token_id = torch.argmax(logits, dim=-1)
print(f"Prompt Token Shape : {prompt_tokens.shape}")
print(f"Predicted Token ID : {next_token_id.item()}")
print(f"Engineering Goal : Domain adaptation for specialized programming languages")
print("=" * 65)
if __name__ == "__main__":
simulate_code_completion()
def simulate_qlora_memory():
print("=" * 65)
print("πΎ EXAMPLE 9: QLoRA 4-BIT QUANTIZATION VRAM SAVINGS")
print("=" * 65)
# 7B Model Parameters
params_7b = 7_000_000_000
# VRAM footprint calculations
fp16_vram = (params_7b * 2) / (1024 ** 3) # 2 bytes per param in FP16
nf4_vram = (params_7b * 0.5) / (1024 ** 3) # 0.5 bytes per param in 4-bit NF4
lora_vram = 1.5 # Approximate VRAM overhead for FP16 adapters and optimizer states
total_qlora_vram = nf4_vram + lora_vram
print(f"Standard FP16 VRAM (7B) : {fp16_vram:.2f} GB (Requires A100 GPU)")
print(f"QLoRA 4-bit NF4 VRAM : {total_qlora_vram:.2f} GB (Fits on 24GB RTX 4090)")
print(f"Memory Reduction : {(1 - total_qlora_vram/fp16_vram)*100:.1f}% less VRAM required")
print("=" * 65)
if __name__ == "__main__":
simulate_qlora_memory()
BERT & LLM FINE-TUNING REFERENCE MATRIX:
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β Fine-Tuning Strategy β Base Architecture β Trainable Parameter % β Primary Production Application β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββ€
β Full Fine-Tuning β BERT, RoBERTa (Encoder) β 100% (All layers updated) β Classification, NER, QA β
β Feature Extraction β BERT / Frozen Encoder β 0% (Only classification head)β Low-resource text embedding β
β LoRA (Low-Rank Adapters) β LLaMA, Mistral, GPT (Decoder)β < 1% (Rank decomposition) β Custom instruction chatbots β
β QLoRA (Quantized LoRA) β 4-bit NF4 Base + LoRA β < 1% (4-bit base + FP16 adapters)β Cost-effective GPU trainingβ
β RLHF / DPO β Instruction LLM β 100% Policy / Reward models β Human preference alignment β
ββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ€
β FINE-TUNING ENGINEERING BEST PRACTICES: β
β 1. Use PEFT (LoRA/QLoRA) for models with > 7B parameters to avoid multi-GPU cluster hardware bottlenecks. β
β 2. Set learning rates between 1e-4 and 3e-4 for LoRA adapters, significantly higher than full fine-tuning rates. β
β 3. Always evaluate fine-tuned checkpoints on held-out evaluation benchmarks to prevent catastrophic forgetting. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ