Implementing llms litgpt
/SKILLImplements and trains LLMs using Lightning AI's LitGPT with 20+ pretrained architectures (Llama, Gemma, Phi, Qwen, Mistral).
--- name: implementing-llms-litgpt description: Implements and trains LLMs using Lightning AI's LitGPT with over 20 pretrained architectures (Llama, Gemma, Phi, Qwen, Mistral). Use when you need clean model implementations, an educational understanding of architectures, or production fine-tuning with LoRA/QLoRA. Single-file implementations, no abstraction layers. version: 1.0.0 author: Orchestra Research license: MIT tags: [Model Architecture, LitGPT, Lightning AI, LLM Implementation, LoRA, QLoRA, Fine-Tuning, Llama, Gemma, Phi, Mistral, Educational] dependencies: [litgpt, torch, transformers] --- # LitGPT - Clean LLM Implementations ## Quick Start LitGPT provides over 20 pretrained LLM implementations with clean, readable code and production-ready training workflows. Installation: ``bash pip install 'litgpt[extra]' **Load and use any model**: python from 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**: bash litgpt download list ## Common workflows ### Workflow 1: Fine-tune on custom dataset Copy this checklist: Fine-Tuning Setup: - [ ] Step 1: Download pretrained model - [ ] Step 2: Prepare dataset - [ ] Step 3: Configure training - [ ] Step 4: Run fine-tuning **Step 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 ### Workflow 2: LoRA fine-tuning on single GPU 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): - **Phi-2** (2.7B) - Best quality/size tradeoff - **Llama 3 1B** - Smallest, fastest - **Gemma 2B** - Good reasoning **Step 2: Configure LoRA parameters** bash litgpt 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 adapters - r=16 : Standard, good quality - r=32 : High capacity, use for complex tasks - r=64`: Maximum quality, 4× larger adapters **