Skip to main content

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 in rejection_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

Quantitative Estimation of Drug-likeness (0-1 scale, higher is better).

Tanimoto Similarity

Measures structural similarity between molecules (0-1 scale).

Combined Score

Molecules are ranked by the sum of similarity and QED scores.

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 (from rejection_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:
This creates a linear decay from max LR to 10% of max LR over all rounds.

Example: Lead Optimization

Define Lead Molecule

Run Optimization

Monitor Progress

The training logs will show:
Successful rounds find molecules meeting the optimization criteria (QED ≥ 0.9, similarity ≥ 0.4).

Advanced Configuration

Custom Similarity Thresholds

Modify the prompt generation in generate_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:
Each CSV contains:
  • samples: Full formatted training sample
  • smiles: Generated SMILES string
  • qed: QED score
  • morgan_sim_to_lead: Tanimoto similarity (Morgan fingerprint)

Performance Tips

  • Use --flash_attn for faster generation
  • Adjust num_return_sequences to balance diversity and speed
  • Use GPU for generation (--device cuda:0)
  • Consider batch generation for multiple leads
  • 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
Clear cache between rounds to prevent OOM

Troubleshooting

No Optimized Molecules Found

  • Relax optimization criteria (lower QED/similarity thresholds)
  • Increase num_return_sequences for 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