| # Fine-Tuning Model Recommendation |
|
|
| ## Current Model Performance Comparison |
|
|
| | Metric | Llama-3 | Qwen | Winner | |
| |--------|---------|------|--------| |
| | **Validity Rate** | 86.00% | 90.00% | Qwen (+4%) | |
| | **Tanimoto Similarity** | 0.1089 | 0.0634 | **Llama-3 (1.7x better)** | |
| | **Mass Error (Da)** | 314.28 | 957.96 | **Llama-3 (3.0x better)** | |
| | **Size Ratio** | 0.585 | 2.390 | **Llama-3 (much closer to 1.0)** | |
| | **Failed Predictions** | 14% | 10% | Qwen (4% better) | |
|
|
| ## Analysis |
|
|
| ### Llama-3 Advantages |
| - ✅ **Much better structural similarity** (0.109 vs 0.063) - 72% better |
| - ✅ **Much better mass accuracy** (314 Da vs 958 Da) - 3x better |
| - ✅ **Better size prediction** (0.585 vs 2.390 ratio) - closer to target |
| - ✅ **More consistent behavior** - understands molecular complexity better |
| - ✅ **Better foundation for fine-tuning** - already has good understanding |
|
|
| ### Qwen Disadvantages |
| - ❌ **Very poor structural similarity** (0.063) - worse than Llama-3 |
| - ❌ **Very large mass errors** (958 Da) - 3x worse than Llama-3 |
| - ❌ **Generates molecules that are too large** (2.4x target size) |
| - ❌ **Poor understanding of molecular complexity** - generates long C chains |
|
|
| ## Recommendation: **Fine-tune Llama-3** |
|
|
| ### Reasons: |
| 1. **Better baseline performance**: Llama-3 already shows much better understanding of molecular structure |
| 2. **More room for improvement**: With fine-tuning, can improve from 0.109 to potentially 0.3+ Tanimoto |
| 3. **Better mass accuracy**: Already at 314 Da, fine-tuning can bring it down to <50 Da |
| 4. **Proven track record**: Llama models have excellent fine-tuning support and community resources |
| 5. **Better prompt following**: Llama-3 better understands complexity requirements |
|
|
| ### Fine-Tuning Strategy: |
|
|
| #### Option 1: Llama-3-8B-Instruct (Recommended) |
| - **Model**: `meta-llama/Meta-Llama-3-8B-Instruct` |
| - **Method**: LoRA (QLoRA with 4-bit quantization) |
| - **Target modules**: `q_proj`, `k_proj`, `v_proj`, `o_proj` (attention layers) |
| - **Training data**: Use `prepare_finetune_data.py` to create chat-formatted data |
| - **Expected improvements**: |
| - Tanimoto: 0.109 → 0.25-0.35 (2-3x improvement) |
| - Mass Error: 314 Da → 50-100 Da (3-6x improvement) |
| - Validity: 86% → 95%+ (with better prompt following) |
| - Size Ratio: 0.585 → 0.8-0.9 (closer to target) |
|
|
| #### Option 2: Llama-2-13B-Chat (Alternative) |
| - **Model**: `meta-llama/Llama-2-13B-Chat` |
| - **Advantage**: Larger model, may have better performance |
| - **Disadvantage**: Requires more GPU memory (~26GB for 4-bit) |
|
|
| ### Fine-Tuning Implementation: |
|
|
| ```bash |
| # 1. Prepare fine-tuning data |
| python scripts/prepare_finetune_data.py \ |
| --train-jsonl runs/rag_molt5_train.jsonl \ |
| --spec-embeddings runs/spec_embeddings_train.npy \ |
| --faiss-index runs/index_all \ |
| --smiles-path data/pubchem_1k.smi \ |
| --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_train.mgf \ |
| --output-jsonl runs/finetune_data.jsonl \ |
| --top-k 5 |
| |
| # 2. Fine-tune with LoRA |
| python scripts/train_llama_lora.py \ |
| --model-name meta-llama/Meta-Llama-3-8B-Instruct \ |
| --train-jsonl runs/finetune_data.jsonl \ |
| --output-dir runs/llama3_finetuned \ |
| --load-in-4bit \ |
| --use-chat-template \ |
| --batch-size 4 \ |
| --epochs 3 \ |
| --lr 2e-4 \ |
| --max-length 2048 |
| ``` |
|
|
| ### Expected Training Time: |
| - **Data preparation**: ~10-30 minutes (depending on dataset size) |
| - **Fine-tuning**: ~2-6 hours (depending on GPU and dataset size) |
| - **GPU requirements**: ~16GB VRAM (with 4-bit quantization) |
|
|
| ### Success Metrics: |
| After fine-tuning, expect: |
| - **Tanimoto Similarity**: >0.25 (vs current 0.109) |
| - **Mass Error**: <100 Da (vs current 314 Da) |
| - **Validity Rate**: >95% (vs current 86%) |
| - **Size Ratio**: 0.8-0.9 (vs current 0.585) |
|
|
| ## Conclusion |
|
|
| **Recommendation: Fine-tune Llama-3-8B-Instruct** |
|
|
| Llama-3 shows significantly better baseline performance in structural similarity and mass accuracy, making it the better foundation for fine-tuning. With proper fine-tuning, we can expect 2-3x improvement in Tanimoto similarity and 3-6x improvement in mass accuracy. |
|
|