File size: 4,058 Bytes
db32e07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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.