How do you fine-tune language models for offline deployment?
Fine-tuning adapts a pre-trained model to specific tasks or domains. Methods: full fine-tuning (updates all model weights, requires significant GPU memory), LoRA (Low-Rank Adaptation, trains small adapter matrices, reduces memory by 10x), QLoRA (quantized LoRA, fine-tunes a 4-bit quantized model, enables 7B model fine-tuning on a single 24GB GPU). Process: prepare training data (instruction-response pairs in JSON/JSONL format, minimum 500-1000 high-quality examples for task-specific tuning), choose a base model (Llama 3.1, Mistral, or Phi-3), configure hyperparameters (learning rate 1e-4 to 2e-5, batch size limited by VRAM, 1-3 epochs to avoid overfitting), and train using frameworks like Hugging Face Transformers with PEFT library, Axolotl, or LLaMA-Factory. LoRA configuration: target attention layers (qproj, kproj, vproj, oproj), rank 8-64 (higher rank captures more task-specific knowledge but costs more memory), alpha typically 2x rank. Evaluation: hold out 10-20% for validation, measure task-specific metrics (accuracy, F1, BLEU for generation), run human evaluation on a sample, and check for catastrophic forgetting on general capabilities. Export for offline use: merge LoRA adapters into base model, convert to GGUF format with quantization, test inference quality at target quantization level, and benchmark performance on target hardware. Data quality matters more than quantity: 1000 high-quality examples often outperform 10000 noisy ones.