Spaces:
Sleeping
Sleeping
Delete ReadMe.md
Browse files
ReadMe.md
DELETED
|
@@ -1,133 +0,0 @@
|
|
| 1 |
-
# Lab 2 - Fine-Tuning a Large Language Model
|
| 2 |
-
|
| 3 |
-
## Overview
|
| 4 |
-
|
| 5 |
-
In this lab we fine-tuned the open-source base model `unsloth/Llama-3.2-3B-Instruct` on Maxime Labonne's FineTome-100k instruction dataset using LoRA (Low-Rank Adaptation) with QLoRA quantization via Unsloth and `trl.SFTTrainer`. The training used the following hyperparameters:
|
| 6 |
-
|
| 7 |
-
| Hyperparameter | Value |
|
| 8 |
-
|----------------|-------|
|
| 9 |
-
| `per_device_train_batch_size` | 2 |
|
| 10 |
-
| `gradient_accumulation_steps` | 4 |
|
| 11 |
-
| `num_train_epochs` | 1 |
|
| 12 |
-
| `learning_rate` | 2e-4 |
|
| 13 |
-
| `warmup_steps` | 5 |
|
| 14 |
-
| `weight_decay` | 0.01 |
|
| 15 |
-
| `optimizer` | adamw_8bit |
|
| 16 |
-
|
| 17 |
-
---
|
| 18 |
-
|
| 19 |
-
## Evaluation Methodology
|
| 20 |
-
|
| 21 |
-
We evaluated both the base model and the fine-tuned model on **100 held-out examples** from a 15% test split of FineTome-100k. For each example we used the conversation history as input and treated the last assistant turn as the reference answer.
|
| 22 |
-
|
| 23 |
-
### Metrics Used
|
| 24 |
-
|
| 25 |
-
We used **ROUGE scores** (Recall-Oriented Understudy for Gisting Evaluation) to measure the overlap between model-generated responses and reference answers:
|
| 26 |
-
|
| 27 |
-
- **ROUGE-1**: Measures unigram (single word) overlap between the generated text and reference. Higher scores indicate better word-level similarity.
|
| 28 |
-
- **ROUGE-2**: Measures bigram (two consecutive words) overlap. This captures phrase-level similarity and is more sensitive to word order.
|
| 29 |
-
- **ROUGE-L**: Measures the longest common subsequence between generated and reference text. This captures sentence-level structure and fluency.
|
| 30 |
-
|
| 31 |
-
All scores use the F-measure (harmonic mean of precision and recall) and range from 0 to 1, where higher is better.
|
| 32 |
-
|
| 33 |
-
### Generation Settings
|
| 34 |
-
|
| 35 |
-
We used **deterministic generation** (greedy decoding with `do_sample=False`) for both models to ensure reproducible and comparable results. Each response was limited to 256 new tokens.
|
| 36 |
-
|
| 37 |
-
---
|
| 38 |
-
|
| 39 |
-
## Results
|
| 40 |
-
|
| 41 |
-
The fine-tuned model clearly outperformed the base model across all ROUGE metrics:
|
| 42 |
-
|
| 43 |
-
| Metric | Base Model | Fine-Tuned Model | Improvement |
|
| 44 |
-
|--------|------------|------------------|-------------|
|
| 45 |
-
| ROUGE-1 | 0.4732 | 0.5323 | **+12.5%** |
|
| 46 |
-
| ROUGE-2 | 0.2255 | 0.2849 | **+26.4%** |
|
| 47 |
-
| ROUGE-L | 0.2856 | 0.3521 | **+23.3%** |
|
| 48 |
-
|
| 49 |
-
These results demonstrate that even a single epoch of LoRA fine-tuning on FineTome-100k produces substantial improvements in response quality. The largest gain was in ROUGE-2 (+26.4%), indicating that the fine-tuned model better captures phrase-level patterns from the training data.
|
| 50 |
-
|
| 51 |
-
### Response Length Analysis
|
| 52 |
-
|
| 53 |
-
| Metric | Base Model | Fine-Tuned Model | Reference |
|
| 54 |
-
|--------|------------|------------------|-----------|
|
| 55 |
-
| Mean length (words) | 165.0 | 155.7 | 216.3 |
|
| 56 |
-
| Median length (words) | 176.0 | 165.5 | 199.0 |
|
| 57 |
-
|
| 58 |
-
The fine-tuned model produces slightly more concise responses while achieving higher ROUGE scores, suggesting improved information density.
|
| 59 |
-
|
| 60 |
-
---
|
| 61 |
-
|
| 62 |
-
## Improving Model Performance
|
| 63 |
-
|
| 64 |
-
### (a) Model-Centric Approach
|
| 65 |
-
|
| 66 |
-
A model-centric approach keeps the data fixed and focuses on changing the model architecture, training configuration, or optimization procedure. Below are concrete strategies for further improvement:
|
| 67 |
-
|
| 68 |
-
#### Hyperparameter Tuning
|
| 69 |
-
|
| 70 |
-
- **Learning rate**: Sweep over values such as `1e-4`, `2e-4`, `5e-4` to find the optimal learning rate. Our current setting of `2e-4` is a reasonable default but may not be optimal.
|
| 71 |
-
- **Learning rate schedule**: Experiment with cosine decay or cosine with warm restarts instead of constant learning rate.
|
| 72 |
-
- **Training epochs**: Train for 2–3 epochs with early stopping based on validation loss to potentially improve convergence.
|
| 73 |
-
- **Batch size**: Increase effective batch size (via `gradient_accumulation_steps`) within memory constraints for more stable gradients.
|
| 74 |
-
- **Warmup steps**: Adjust warmup duration (e.g., 10–100 steps) to improve training stability.
|
| 75 |
-
- **Weight decay**: Test different regularization strengths (e.g., `0.001`, `0.01`, `0.1`) to control overfitting.
|
| 76 |
-
|
| 77 |
-
#### LoRA Configuration
|
| 78 |
-
|
| 79 |
-
- **Rank (r)**: Increase LoRA rank (e.g., from 16 to 32 or 64) to allow more expressive adapter updates, at the cost of increased memory.
|
| 80 |
-
- **Alpha scaling**: Adjust the LoRA alpha parameter to control the magnitude of adapter contributions.
|
| 81 |
-
- **Target modules**: Experiment with applying LoRA to different layer types (attention only, MLP layers, or both) and different layer ranges.
|
| 82 |
-
|
| 83 |
-
#### Model Architecture
|
| 84 |
-
|
| 85 |
-
- **Base model selection**: Compare different foundation models such as `Llama-3.2-1B-Instruct` (faster inference) or `Llama-3.1-8B-Instruct` (potentially higher quality but slower).
|
| 86 |
-
- **Quantization**: Compare 4-bit (QLoRA) vs 8-bit quantization to understand the quality-speed tradeoff.
|
| 87 |
-
|
| 88 |
-
#### Training Procedure
|
| 89 |
-
|
| 90 |
-
- **Gradient clipping**: Add gradient clipping to prevent exploding gradients and improve training stability.
|
| 91 |
-
- **Mixed precision**: Ensure optimal use of mixed precision training for faster iteration.
|
| 92 |
-
|
| 93 |
-
### (b) Data-Centric Approach
|
| 94 |
-
|
| 95 |
-
A data-centric approach keeps the model and training loop mostly fixed and focuses on improving or extending the training data. Below are concrete strategies:
|
| 96 |
-
|
| 97 |
-
#### Data Quality Improvements
|
| 98 |
-
|
| 99 |
-
- **Filter low-quality examples**: Remove very short, unclear, or noisy instruction-response pairs from FineTome-100k to increase average signal per batch.
|
| 100 |
-
- **Deduplicate**: Remove near-duplicate examples that may cause the model to overfit to specific patterns.
|
| 101 |
-
- **Balance task types**: If the target application focuses on specific capabilities (e.g., reasoning, coding, explanation), up-sample those categories and down-sample less relevant ones.
|
| 102 |
-
|
| 103 |
-
#### Additional Data Sources
|
| 104 |
-
|
| 105 |
-
Augment FineTome-100k with other high-quality open-source instruction datasets:
|
| 106 |
-
|
| 107 |
-
| Dataset | Focus Area | Potential Benefit |
|
| 108 |
-
|---------|------------|-------------------|
|
| 109 |
-
| OpenAssistant Conversations | Multi-turn dialogue | Improved conversational ability |
|
| 110 |
-
| GSM8K / MetaMath | Math reasoning | Better mathematical problem-solving |
|
| 111 |
-
| CodeAlpaca / Code-Feedback | Programming tasks | Improved code generation |
|
| 112 |
-
| FLAN Collection | Diverse NLP tasks | Broader task coverage |
|
| 113 |
-
| UltraChat | Long-form dialogue | Better handling of extended conversations |
|
| 114 |
-
|
| 115 |
-
#### Domain-Specific Fine-Tuning
|
| 116 |
-
|
| 117 |
-
- **Curriculum learning**: Start training on general instructions, then gradually shift to more specialized or difficult examples.
|
| 118 |
-
- **Task-specific adapters**: Train separate LoRA adapters for different domains (math, code, creative writing) and select the appropriate adapter at inference time.
|
| 119 |
-
|
| 120 |
-
#### Data Alignment
|
| 121 |
-
|
| 122 |
-
- **Match UI format**: If the final application expects specific output formats (e.g., step-by-step reasoning, JSON responses), construct or filter training examples that demonstrate these formats.
|
| 123 |
-
- **User feedback loop**: In production, log anonymized user interactions (if permitted) to create a fine-tuning set that reflects real usage patterns.
|
| 124 |
-
|
| 125 |
-
---
|
| 126 |
-
|
| 127 |
-
## Conclusion
|
| 128 |
-
|
| 129 |
-
Our fine-tuning pipeline demonstrates measurable improvements over the base model, with ROUGE scores increasing by 12–26% on a held-out test set. The model-centric and data-centric strategies outlined above provide clear directions for further performance gains. The most promising next steps would be:
|
| 130 |
-
|
| 131 |
-
1. **Hyperparameter sweep** on learning rate and number of epochs
|
| 132 |
-
2. **Increase LoRA rank** to allow more expressive updates
|
| 133 |
-
3. **Mix in domain-specific datasets** (e.g., math reasoning or code) to improve performance on specialized tasks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|