skills/AI/AI-llm-architecture/7.0.-lora-improvements-in-fine-tuning/SKILL.md
How to implement LoRA (Low-Rank Adaptation) for efficient fine-tuning of large language models. Use this skill whenever the user wants to fine-tune an LLM, reduce training memory/compute requirements, implement parameter-efficient fine-tuning, or adapt a pre-trained model to a new task without retraining all parameters. Make sure to use this skill when users mention fine-tuning, LoRA, PEFT, parameter-efficient training, or want to train on limited hardware.
npx skillsauth add abelrguezr/hacktricks-skills lora-fine-tuningInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This skill helps you implement LoRA (Low-Rank Adaptation) for efficient fine-tuning of large language models. LoRA reduces training costs by only updating small adapter matrices instead of the entire model.
Use LoRA when you need to:
LoRA replaces standard linear layers with a combination of:
The forward pass becomes: output = original_linear(x) + alpha * (x @ A @ B)
Use the scripts/lora_layers.py module which provides:
LoRALayer: The low-rank adapter with matrices A and BLinearWithLoRA: Wrapper combining original linear layer with LoRAreplace_linear_with_lora(): Recursive function to convert all linear layersfrom scripts.lora_layers import replace_linear_with_lora
# Choose rank and alpha (typical values)
rank = 8 # Lower rank = fewer parameters, higher compression
alpha = 16 # Scaling factor (often 2x rank)
# Apply LoRA to your model
model = replace_linear_with_lora(model, rank=rank, alpha=alpha)
Only the LoRA parameters need gradients:
# Freeze original model parameters
for param in model.parameters():
param.requires_grad = False
# Enable gradients only for LoRA matrices
for name, param in model.named_parameters():
if 'lora' in name:
param.requires_grad = True
Your training loop remains the same. The optimizer will only update LoRA parameters.
| Rank | Use Case | Trainable Params (approx) | |------|----------|---------------------------| | 4-8 | Small tasks, limited data | ~0.1% of model | | 8-16 | Standard fine-tuning | ~0.2-0.4% of model | | 16-32 | Complex tasks, more data | ~0.4-0.8% of model |
Alpha: Typically set to 2x rank (e.g., rank=8, alpha=16)
import torch
from transformers import AutoModelForCausalLM
from scripts.lora_layers import replace_linear_with_lora
# Load pre-trained model
model = AutoModelForCausalLM.from_pretrained("your-model")
# Apply LoRA
rank = 8
alpha = 16
model = replace_linear_with_lora(model, rank, alpha)
# Freeze and enable LoRA gradients
for param in model.parameters():
param.requires_grad = False
for name, param in model.named_parameters():
if 'lora' in name:
param.requires_grad = True
# Count trainable parameters
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"Trainable: {trainable_params:,} ({100*trainable_params/total_params:.2f}%)")
# Train normally
optimizer = torch.optim.AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-4)
After training, you can merge LoRA weights back into the original model for inference:
def merge_lora_weights(model):
for name, module in model.named_modules():
if isinstance(module, LinearWithLoRA):
# Merge: W_merged = W_original + alpha * B @ A
with torch.no_grad():
merged_weight = module.linear.weight + module.lora.alpha * (module.lora.B @ module.lora.A)
module.linear.weight = torch.nn.Parameter(merged_weight)
# Replace with plain linear
setattr(model, name.split('.')[-1], module.linear)
Issue: Out of memory during training
Issue: Poor fine-tuning quality
Issue: LoRA not being applied
replace_linear_with_lora was called before training, check that LoRA parameters have requires_grad=Truetesting
How to perform a House of Lore (small bin attack) heap exploitation. Use this skill whenever the user mentions heap exploitation, small bin attacks, fake chunks, glibc heap vulnerabilities, or needs to insert fake chunks into small bins for arbitrary read/write. Trigger for CTF challenges involving heap corruption, glibc 2.31+ exploitation, or when the user needs to bypass malloc sanity checks using fake chunk linking.
testing
How to perform House of Force heap exploitation attacks. Use this skill whenever the user mentions heap exploitation, House of Force, top chunk manipulation, arbitrary memory allocation, malloc manipulation, or wants to allocate chunks at specific addresses. Also trigger for CTF challenges involving heap overflows, top chunk size overwrites, or when the user needs to calculate evil_size for heap attacks. Make sure to use this skill for any binary exploitation task involving glibc heap manipulation, even if they don't explicitly say "House of Force".
tools
How to perform House of Einherjar heap exploitation to allocate memory at arbitrary addresses. Use this skill whenever the user mentions heap exploitation, glibc heap attacks, arbitrary memory allocation, off-by-one overflow exploitation, tcache poisoning, fast bin attacks, or any CTF challenge involving heap manipulation. This is essential for binary exploitation tasks where you need to control malloc() return addresses.
testing
How to identify, analyze, and exploit heap overflow vulnerabilities in binary exploitation challenges and real-world scenarios. Use this skill whenever the user mentions heap overflows, memory corruption, heap grooming, tcache poisoning, fast-bin attacks, or any heap-related vulnerability in CTF challenges, binary analysis, or security research. This skill covers heap overflow fundamentals, exploitation techniques, heap grooming strategies, and real-world CVE analysis.