Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Implements and trains LLMs using Lightning AI's LitGPT with 20+ pretrained architectures (Llama, Gemma, Phi, Qwen, Mistral). Use when need clean model implementations, educational understanding of architectures, or production fine-tuning with LoRA/QLoRA. Single-file implementations, no abstraction layers.
.claude/skills/openlair-implementing-llms-litgpt/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 45% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 121% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 120% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 72% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 42% | 0% |
LitGPT provides 20+ pretrained LLM implementations with clean, readable code and production-ready training workflows.
Installation:
bashpip install 'litgpt[extra]'
Load and use any model:
pythonfrom litgpt import LLM # Load pretrained model llm = LLM.load("microsoft/phi-2") # Generate text result = llm.generate( "What is the capital of France?", max_new_tokens=50, temperature=0.7 ) print(result)
List available models:
bashlitgpt download list
Copy this checklist:
Fine-Tuning Setup:
- [ ] Step 1: Download pretrained model
- [ ] Step 2: Prepare dataset
- [ ] Step 3: Configure training
- [ ] Step 4: Run fine-tuningStep 1: Download pretrained model
bash# Download Llama 3 8B litgpt download meta-llama/Meta-Llama-3-8B # Download Phi-2 (smaller, faster) litgpt download microsoft/phi-2 # Download Gemma 2B litgpt download google/gemma-2b
Models are saved to checkpoints/ directory.
Step 2: Prepare dataset
LitGPT supports multiple formats:
Alpaca format (instruction-response):
json[ { "instruction": "What is the capital of France?", "input": "", "output": "The capital of France is Paris." }, { "instruction": "Translate to Spanish: Hello, how are you?", "input": "", "output": "Hola, ¿cómo estás?" } ]
Save as data/my_dataset.json.
Step 3: Configure training
bash# Full fine-tuning (requires 40GB+ GPU for 7B models) litgpt finetune \ meta-llama/Meta-Llama-3-8B \ --data JSON \ --data.json_path data/my_dataset.json \ --train.max_steps 1000 \ --train.learning_rate 2e-5 \ --train.micro_batch_size 1 \ --train.global_batch_size 16 # LoRA fine-tuning (efficient, 16GB GPU) litgpt finetune_lora \ microsoft/phi-2 \ --data JSON \ --data.json_path data/my_dataset.json \ --lora_r 16 \ --lora_alpha 32 \ --lora_dropout 0.05 \ --train.max_steps 1000 \ --train.learning_rate 1e-4
Step 4: Run fine-tuning
Training saves checkpoints to out/finetune/ automatically.
Monitor training:
bash# View logs tail -f out/finetune/logs.txt # TensorBoard (if using --train.logger_name tensorboard) tensorboard --logdir out/finetune/lightning_logs
Most memory-efficient option.
LoRA Training:
- [ ] Step 1: Choose base model
- [ ] Step 2: Configure LoRA parameters
- [ ] Step 3: Train with LoRA
- [ ] Step 4: Merge LoRA weights (optional)Step 1: Choose base model
For limited GPU memory (12-16GB):
Step 2: Configure LoRA parameters
bashlitgpt finetune_lora \ microsoft/phi-2 \ --data JSON \ --data.json_path data/my_dataset.json \ --lora_r 16 \ # LoRA rank (8-64, higher=more capacity) --lora_alpha 32 \ # LoRA scaling (typically 2×r) --lora_dropout 0.05 \ # Prevent overfitting --lora_query true \ # Apply LoRA to query projection --lora_key false \ # Usually not needed --lora_value true \ # Apply LoRA to value projection --lora_projection true \ # Apply LoRA to output projection --lora_mlp false \ # Usually not needed --lora_head false # Usually not needed
LoRA rank guide:
r=8: Lightweight, 2-4MB adaptersr=16: Standard, good qualityr=32: High capacity, use for complex tasksr=64: Maximum quality, 4× larger adaptersStep 3: Train with LoRA
bashlitgpt finetune_lora \ microsoft/phi-2 \ --data JSON \ --data.json_path data/my_dataset.json \ --lora_r 16 \ --train.epochs 3 \ --train.learning_rate 1e-4 \ --train.micro_batch_size 4 \ --train.global_batch_size 32 \ --out_dir out/phi2-lora # Memory usage: ~8-12GB for Phi-2 with LoRA
Step 4: Merge LoRA weights (optional)
Merge LoRA adapters into base model for deployment:
bashlitgpt merge_lora \ out/phi2-lora/final \ --out_dir out/phi2-merged
Now use merged model:
pythonfrom litgpt import LLM llm = LLM.load("out/phi2-merged")
Train new model on your domain data.
Pretraining:
- [ ] Step 1: Prepare pretraining dataset
- [ ] Step 2: Configure model architecture
- [ ] Step 3: Set up multi-GPU training
- [ ] Step 4: Launch pretrainingStep 1: Prepare pretraining dataset
LitGPT expects tokenized data. Use prepare_dataset.py:
bashpython scripts/prepare_dataset.py \ --source_path data/my_corpus.txt \ --checkpoint_dir checkpoints/tokenizer \ --destination_path data/pretrain \ --split train,val
Step 2: Configure model architecture
Edit config file or use existing:
python# config/pythia-160m.yaml model_name: pythia-160m block_size: 2048 vocab_size: 50304 n_layer: 12 n_head: 12 n_embd: 768 rotary_percentage: 0.25 parallel_residual: true bias: true
Step 3: Set up multi-GPU training
bash# Single GPU litgpt pretrain \ --config config/pythia-160m.yaml \ --data.data_dir data/pretrain \ --train.max_tokens 10_000_000_000 # Multi-GPU with FSDP litgpt pretrain \ --config config/pythia-1b.yaml \ --data.data_dir data/pretrain \ --devices 8 \ --train.max_tokens 100_000_000_000
Step 4: Launch pretraining
For large-scale pretraining on cluster:
bash# Using SLURM sbatch --nodes=8 --gpus-per-node=8 \ pretrain_script.sh # pretrain_script.sh content: litgpt pretrain \ --config config/pythia-1b.yaml \ --data.data_dir /shared/data/pretrain \ --devices 8 \ --num_nodes 8 \ --train.global_batch_size 512 \ --train.max_tokens 300_000_000_000
Export LitGPT models for production.
Model Deployment:
- [ ] Step 1: Test inference locally
- [ ] Step 2: Quantize model (optional)
- [ ] Step 3: Convert to GGUF (for llama.cpp)
- [ ] Step 4: Deploy with APIStep 1: Test inference locally
pythonfrom litgpt import LLM llm = LLM.load("out/phi2-lora/final") # Single generation print(llm.generate("What is machine learning?")) # Streaming for token in llm.generate("Explain quantum computing", stream=True): print(token, end="", flush=True) # Batch inference prompts = ["Hello", "Goodbye", "Thank you"] results = [llm.generate(p) for p in prompts]
Step 2: Quantize model (optional)
Reduce model size with minimal quality loss:
bash# 8-bit quantization (50% size reduction) litgpt convert_lit_checkpoint \ out/phi2-lora/final \ --dtype bfloat16 \ --quantize bnb.nf4 # 4-bit quantization (75% size reduction) litgpt convert_lit_checkpoint \ out/phi2-lora/final \ --quantize bnb.nf4-dq # Double quantization
Step 3: Convert to GGUF (for llama.cpp)
bashpython scripts/convert_lit_checkpoint.py \ --checkpoint_path out/phi2-lora/final \ --output_path models/phi2.gguf \ --model_name microsoft/phi-2
Step 4: Deploy with API
pythonfrom fastapi import FastAPI from litgpt import LLM app = FastAPI() llm = LLM.load("out/phi2-lora/final") @app.post("/generate") def generate(prompt: str, max_tokens: int = 100): result = llm.generate( prompt, max_new_tokens=max_tokens, temperature=0.7 ) return {"response": result} # Run: uvicorn api:app --host 0.0.0.0 --port 8000
Use LitGPT when:
Use alternatives instead:
Issue: Out of memory during fine-tuning
Use LoRA instead of full fine-tuning:
bash# Instead of litgpt finetune (requires 40GB+) litgpt finetune_lora # Only needs 12-16GB
Or enable gradient checkpointing:
bashlitgpt finetune_lora \ ... \ --train.gradient_accumulation_iters 4 # Accumulate gradients
Issue: Training too slow
Enable Flash Attention (built-in, automatic on compatible hardware):
python# Already enabled by default on Ampere+ GPUs (A100, RTX 30/40 series) # No configuration needed
Use smaller micro-batch and accumulate:
bash--train.micro_batch_size 1 \ --train.global_batch_size 32 \ --train.gradient_accumulation_iters 32 # Effective batch=32
Issue: Model not loading
Check model name:
bash# List all available models litgpt download list # Download if not exists litgpt download meta-llama/Meta-Llama-3-8B
Verify checkpoints directory:
bashls checkpoints/ # Should see: meta-llama/Meta-Llama-3-8B/
Issue: LoRA adapters too large
Reduce LoRA rank:
bash--lora_r 8 # Instead of 16 or 32
Apply LoRA to fewer layers:
bash--lora_query true \ --lora_value true \ --lora_projection false \ # Disable this --lora_mlp false # And this
Supported architectures: See references/supported-models.md for complete list of 20+ model families with sizes and capabilities.
Training recipes: See references/training-recipes.md for proven hyperparameter configurations for pretraining and fine-tuning.
FSDP configuration: See references/distributed-training.md for multi-GPU training with Fully Sharded Data Parallel.
Custom architectures: See references/custom-models.md for implementing new model architectures in LitGPT style.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 16,512 | 6,904 | -58% | 1 | 1 | 0% | 3,421 | 4,969 | +45% | 0 | 0 | — |
case-06 | fail→pass | 10,611 | 4,475 | -58% | 1 | 1 | 0% | 1,980 | 4,376 | +121% | 0 | 0 | — |
case-07 | fail→pass | 25,840 | 3,666 | -86% | 1 | 1 | 0% | 1,882 | 4,136 | +120% | 0 | 0 | — |
case-02 | fail→pass | 11,432 | 3,701 | -68% | 1 | 1 | 0% | 2,446 | 4,209 | +72% | 0 | 0 | — |
case-03 | pass→pass | 5,660 | 3,870 | -32% | 1 | 1 | 0% | 1,103 | 4,227 | +283% | 0 | 0 | — |
case-04 | fail→pass | 14,762 | 4,177 | -72% | 1 | 1 | 0% | 3,092 | 4,378 | +42% | 0 | 0 | — |
case-05 | fail→pass | 15,198 | 3,815 | -75% | 1 | 1 | 0% | 2,394 | 4,286 | +79% | 0 | 0 | — |
case-08 | pass→pass | 3,292 | 1,720 | -48% | 1 | 1 | 0% | 691 | 3,775 | +446% | 0 | 0 | — |
case-09 | pass→pass | 6,381 | 4,231 | -34% | 1 | 1 | 0% | 1,298 | 4,246 | +227% | 0 | 0 | — |
case-10 | fail→pass | 7,954 | 2,640 | -67% | 1 | 1 | 0% | 1,510 | 3,947 | +161% | 0 | 0 | — |
case-11 | fail→pass | 9,185 | 4,744 | -48% | 1 | 1 | 0% | 1,833 | 4,531 | +147% | 0 | 0 | — |
case-17 | pass→pass | 15,531 | 3,601 | -77% | 1 | 1 | 0% | 2,830 | 4,172 | +47% | 0 | 0 | — |
case-12 | pass→pass | 6,368 | 4,525 | -29% | 1 | 1 | 0% | 1,323 | 4,436 | +235% | 0 | 0 | — |
case-13 | pass→pass | 5,296 | 6,805 | +28% | 1 | 1 | 0% | 992 | 4,853 | +389% | 0 | 0 | — |
case-14 | fail→pass | 10,039 | 6,289 | -37% | 1 | 1 | 0% | 1,974 | 4,822 | +144% | 0 | 0 | — |
case-15 | pass→pass | 10,904 | 4,975 | -54% | 1 | 1 | 0% | 2,196 | 4,462 | +103% | 0 | 0 | — |
case-16 | pass→pass | 5,051 | 2,737 | -46% | 1 | 1 | 0% | 884 | 4,032 | +356% | 0 | 0 | — |
case-18 | fail→pass | 13,876 | 2,034 | -85% | 1 | 1 | 0% | 2,364 | 3,774 | +60% | 0 | 0 | — |
case-19 | pass→pass | 8,941 | 4,174 | -53% | 1 | 1 | 0% | 1,809 | 4,276 | +136% | 0 | 0 | — |
case-20 | pass→pass | 11,833 | 6,309 | -47% | 1 | 1 | 0% | 2,343 | 4,691 | +100% | 0 | 0 | — |
case-21 | pass→pass | 15,252 | 9,216 | -40% | 1 | 1 | 0% | 2,639 | 5,279 | +100% | 0 | 0 | — |
case-22 | pass→pass | 15,475 | 9,643 | -38% | 1 | 1 | 0% | 2,968 | 5,390 | +82% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted, and 21 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +45 percentage points is the difference between those two pass rates over the 21 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.