Fine-tune large language models effectively. Covers when to fine-tune vs prompt engineer, LoRA/QLoRA, training data preparation, evaluation methodology, and cost optimization.
This page describes specific, executable techniques for adapting large language models to domain-specific tasks. These strategies apply when you have a pre-trained model and need to adjust its behavior using labeled data or instruction examples, typically in preparation for inference or deployment in production systems.
Fine-tuning requires structured training data in JSONL format, where each line is a valid JSON object. The minimum required fields are prompt and completion.
{"prompt": "Translate to French: Hello world", "completion": "Bonjour le monde"}
{"prompt": "Summarize: The quick brown fox jumps over the lazy dog.", "completion": "A fox leaps over a sleeping dog."}
Avoid mixing multiple task types in a single dataset. If you need to train on both translation and summarization, create separate datasets for each task.
The prompt field should not include the model's response token; it must contain only the input text that precedes the expected output. For example, do not include "Assistant:" or "User:" prefixes in the prompt unless they are part of your instruction format.
Use the standard --learning-rate flag with values between 1e-5 and 1e-4 for most fine-tuning scenarios.
python train.py --learning-rate 3e-5 --epochs 3 --batch-size 4
The --warmup-steps parameter should be set to 10% of total training steps. For a dataset of 10,000 examples with 4 batch size and 3 epochs, this means about 750 warmup steps.
Do not use --gradient-clip unless you observe loss spikes or gradient explosion. Most LLMs handle gradient clipping automatically through their internal optimization logic.
Use AdamW optimizer with default parameters unless otherwise specified. Do not set --beta1 or --beta2 explicitly unless your model requires specific momentum settings.
python train.py --optimizer adamw --weight-decay 0.01
Avoid --adam-betas unless you're working with a custom implementation that supports them. The standard library implementations use beta1=0.9, beta2=0.999 by default.
The cross-entropy loss function is the only supported option for text generation tasks. Do not attempt to use focal loss or contrastive loss for instruction-following fine-tuning.
Loss values are typically reported as tokens-per-word (TPW) when using standard tokenizers. A TPW of 2.0–3.0 indicates good convergence for most instruction-following tasks.
Set batch size to 4, 8, or 16 depending on GPU memory. Larger batch sizes can cause training instability without proper gradient accumulation.
python train.py --batch-size 8 --gradient-accumulation-steps 2
Gradient accumulation steps should be set so that effective batch size equals 16. If your GPU memory limits you to batch size 4, use --gradient-accumulation-steps 4.
Load checkpoints using the standard --model-path flag. Do not assume automatic checkpoint restoration unless explicitly configured.
python train.py --model-path ./checkpoints/epoch_2 --resume-from-checkpoint
Checkpoints should be saved every 10% of training steps. If your training runs for 1000 steps, save a checkpoint at step 100, 200, 300, etc.
Train for a fixed number of epochs unless using validation loss monitoring. Do not train beyond 5 epochs without re-evaluation. Most instruction-following tasks converge within 3 epochs.
python train.py --epochs 3 --early-stopping-patience 2
If --early-stopping-patience is not supported, monitor training loss manually and stop after two consecutive epochs with no improvement.
Validation should occur at least once per epoch. Use the same data format as training for validation sets.
python evaluate.py --validation-data ./val.jsonl --model-path ./final_model
Evaluation metrics are reported as perplexity or token accuracy. Perplexity values below 10 indicate good performance for instruction-following tasks.
The most impactful hyperparameter is --learning-rate. Do not vary --weight-decay unless you observe overfitting. Do not adjust --dropout during fine-tuning unless using a model that explicitly supports it.
For low-resource scenarios, reduce --epochs to 1 or 2 and increase learning rate to 5e-5.
If training loss drops while validation loss increases, reduce learning rate by half. Do not apply early stopping based on training loss alone.
python train.py --learning-rate 1e-5 --max-steps 500
Use --max-steps instead of epochs when overfitting occurs to prevent extended training runs.
Remove duplicate prompts from training data. Duplicates cause the model to learn redundant patterns without new information.
Filter out examples with completion length under 3 tokens or over 500 tokens. Short completions often lack semantic content, while long ones may exceed token limits.
Silently failing to load checkpoints results in full retraining without indication. Always verify checkpoint integrity by checking model weights against expected shapes.
Incorrect prompt formatting causes the model to ignore instructions entirely. The prompt field must not include the response prefix unless explicitly required.
Using incorrect loss function (e.g., trying to use focal loss) leads to training failure or poor convergence. Cross-entropy is the only valid choice.
Training without proper validation leads to overfitting with no detection. Always validate on held-out data before deployment.
Log training loss every 100 steps. If logging interval is not configurable, log at least once per epoch.
python train.py --logging-steps 100 --save-steps 500
Ensure logs include actual token counts per batch. This helps detect data corruption or misconfigured batching.
After training, convert the model to a format compatible with your deployment infrastructure. Use standard --output-dir flag for final model serialization.
python train.py --output-dir ./final_model --save-final-model
Do not assume automatic quantization. Some models require explicit conversion steps before serving in production.
If a training run fails mid-epoch, the system should automatically resume from the last saved checkpoint. If no checkpoint exists, re-start from the original base model.
python train.py --resume-from-checkpoint --checkpoint-path ./last_checkpoint
Checkpoints must be complete and consistent. Partially written checkpoints cause silent failures during resume.
GPU memory usage scales with batch size and sequence length. Monitor memory consumption and adjust accordingly.
For 7B parameter models, use batch size 8 with sequence length 512 on a single A100 GPU. Exceeding this causes out-of-memory errors.
Track training data versions using Git or similar tools. Changes in data format or content should be clearly marked.
git add training_data_v2.jsonl
git commit -m "Update training data with new examples"
Do not assume that model weights will automatically align with data versioning. Always maintain explicit links between datasets and trained models.
Ensure that the fine-tuned model uses the same architecture as the base model. Do not attempt to fine-tune a 1B parameter model using 7B parameter training scripts.
If your base model is based on LLaMA architecture, do not apply BERT-specific training parameters. The two architectures have different optimization requirements.
Use --debug flag for detailed logging of loss computation and gradient updates. This flag should be available in most training implementations.
python train.py --debug --log-level debug
If debugging is not supported, inspect intermediate model outputs manually using the saved checkpoints.
When training data contains mixed formats, preprocess it into a single consistent structure before training begins. Do not rely on runtime format detection.
python preprocess.py --input-data ./raw_data.jsonl --output-data ./cleaned_data.jsonl
Data preprocessing should validate token counts and ensure all examples fit within maximum sequence length limits.
This page was rewritten on 15 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy