Overview
Rejection sampling is an advanced fine-tuning strategy that iteratively generates and filters molecular candidates based on desired properties. This approach is particularly effective for molecular optimization tasks where you want to improve specific properties while maintaining structural similarity.How It Works
1
Generate Candidates
The model generates multiple molecular candidates based on a lead molecule and similar structures
2
Filter by Criteria
Candidates are filtered based on:
- QED (drug-likeness) score
- Tanimoto similarity to lead molecule
- Other custom properties
3
Create Training Samples
Accepted molecules are formatted into training samples with their properties
4
Fine-tune Model
The model is fine-tuned on the filtered samples
5
Iterate
Process repeats for multiple rounds, progressively improving the model
Quick Start
Command-Line Arguments
Required Arguments
string
required
Path to pretrained model or model identifier
string
required
Model configuration name (e.g.,
125m, 1.3b)string
required
Directory containing validation data in JSONL format
integer
required
Number of rejection sampling rounds to perform
integer
required
Number of training steps per round (also number of samples generated)
integer
required
Training batch size
float
required
Maximum learning rate for training
integer
required
Steps between evaluation runs
integer
required
Steps between checkpoint saves
string
required
Root directory for saving checkpoints
string
required
Device to use for generation (e.g.,
cuda:0, cpu)Optional Arguments
string
default:"none"
Name for the experiment
integer
Validation batch size (defaults to training batch size)
integer
default:"0"
Number of dataloader workers
integer
default:"1"
Gradient accumulation steps
boolean
default:"false"
Enable gradient checkpointing
boolean
default:"false"
Use Flash Attention
boolean
default:"true"
Enable Aim tracking
string
Directory for tracking data
integer
default:"42"
Random seed for reproducibility
Generation Configuration
The rejection sampling process uses two sets of generation parameters defined inrejection_sampling_configs.py:
Similar Molecule Generation
integer
default:"50"
Maximum tokens to generate for similar molecules
float
default:"1.0"
Sampling temperature (higher = more diverse)
float
default:"1.0"
Penalty for repeating tokens
Target Molecule Generation
integer
default:"20"
Number of candidate molecules to generate per prompt
integer
default:"300"
Maximum tokens for target molecule generation
Molecular Scoring
Molecules are evaluated using multiple metrics:QED Score
Tanimoto Similarity
Combined Score
Optimization Criteria
The default optimization targets molecules with:- QED ≥ 0.9: High drug-likeness
- Tanimoto similarity ≥ 0.4: Maintains structural features of lead
Prompt Format
The rejection sampling generates prompts in this format:Example
Training Process
Round-by-Round Workflow
For each round (fromrejection_sampling_ft.py:235-299):
1
Load Generator Model
2
Generate Samples
3
Filter and Score
4
Train on Samples
5
Save Checkpoint
Learning Rate Schedule
Rejection sampling uses polynomial decay with warmup:Example: Lead Optimization
Define Lead Molecule
Run Optimization
Monitor Progress
The training logs will show:Advanced Configuration
Custom Similarity Thresholds
Modify the prompt generation ingenerate_dataset:
Custom QED Targets
Multiple Lead Molecules
Modify the training loop to alternate between different leads:Data Storage
Generated samples are saved as CSV files:samples: Full formatted training samplesmiles: Generated SMILES stringqed: QED scoremorgan_sim_to_lead: Tanimoto similarity (Morgan fingerprint)
Performance Tips
Generation Speed
Generation Speed
- Use
--flash_attnfor faster generation - Adjust
num_return_sequencesto balance diversity and speed - Use GPU for generation (
--device cuda:0) - Consider batch generation for multiple leads
Sample Quality
Sample Quality
- Increase
max_similars_in_prompt(default 5) for more context - Adjust temperature for more/less diversity
- Filter invalid SMILES early to save computation
- Use canonical and Kekulé SMILES for consistency
Memory Management
Memory Management
Troubleshooting
No Optimized Molecules Found
- Relax optimization criteria (lower QED/similarity thresholds)
- Increase
num_return_sequencesfor more candidates - Try different lead molecules
- Increase temperature for more diversity
Invalid SMILES Generated
- Check tokenizer is properly configured
- Verify model was trained on SMILES data
- Ensure proper formatting of start/end tokens
- Consider adding SMILES validation callback
Training Instability
- Reduce learning rate
- Increase warmup steps
- Use gradient clipping (controlled by
global_gradient_norm) - Monitor gradient norms in Aim
Next Steps
Pretraining
Learn about pretraining models from scratch
Configuration
Explore all training configuration options