Skip to main content
Rejection sampling is an iterative fine-tuning strategy that generates molecules, scores them with an oracle, and fine-tunes the model on high-scoring examples.

Main Function

Source: rejection_sampling_ft.py:57

Parameters

str
required
Path to pretrained model checkpoint or Hugging Face model ID.
str
required
Model configuration name (e.g., “125m”, “1.3b”).
str
required
Directory containing validation data.
int
required
Number of rejection sampling rounds to perform.
int
required
Training steps per rejection sampling round.
int
required
Evaluate model every N steps.
int
required
Save checkpoint every N steps.
int
required
Training batch size per device.
float
required
Maximum learning rate for polynomial decay schedule.
str
required
Name for this training run.
str
required
Root directory for saving checkpoints.
int
required
Number of workers for data loading.
bool
required
Whether to use Flash Attention for efficient training.
int
required
Number of gradient accumulation steps.
bool
required
Whether to use gradient checkpointing to save memory.
str
required
Device to use for training (e.g., “cuda”, “cpu”).
int
required
Random seed for reproducibility.
bool
default:"False"
Whether to enable experiment tracking (Aim).
str
default:"None"
Directory for experiment tracking logs.

Rejection Sampling Workflow

The rejection sampling process follows these steps:
1

Generate candidates

Generate molecules using the current model with specified prompts.
2

Score with oracle

Evaluate generated molecules using a scoring function (e.g., QED, docking score).
3

Select top performers

Filter molecules based on score thresholds and similarity constraints.
4

Fine-tune on selected data

Train the model on high-scoring molecules for N steps.
5

Repeat

Continue for the specified number of rounds.

Generation Configuration

GenerationConfig Parameters

int
default:"128"
Maximum number of new tokens to generate.
float
default:"1.0"
Sampling temperature for diversity.
bool
default:"True"
Whether to use sampling (vs greedy decoding).
int
default:"1"
Number of sequences to generate per prompt.
float
default:"1.0"
Penalty for repeating tokens (greater than 1.0 discourages repetition).

Scoring Configuration

ScoringConfig Parameters

str
required
Scoring metric to use:
  • "qed": Quantitative Estimate of Drug-likeness
  • "tanimoto": Tanimoto similarity to reference
  • "custom": Custom scoring function
float
required
Minimum score threshold for acceptance.
float
Minimum Tanimoto similarity to reference molecule (when using similarity constraints).
str
Reference SMILES for similarity-constrained optimization.

Example Usage

Custom Trainer

The rejection sampling uses CustomIterativeSFTTrainer which extends the standard trainer:

Learning Rate Schedule

Rejection sampling uses polynomial decay with warmup:

Data Format

Generated data is stored in JSONL format:

Monitoring Progress

Track rejection sampling with Aim:
View results:

Notes

Rejection sampling can significantly improve molecule generation quality by focusing the model on high-scoring examples.
Use appropriate score thresholds to avoid overfitting to a narrow region of chemical space. Monitor diversity metrics during training.
Start with a smaller number of rounds (5-10) and evaluate before scaling up. Excessive rounds can lead to mode collapse.

See Also