Molecular Verifier
The MolecularVerifier is the main orchestrator for reward computation across all molecular tasks. It automatically routes completions to the appropriate task-specific verifier based on metadata.
Overview
The MolecularVerifier provides:
- Unified Interface: Single entry point for all reward computations
- Automatic Routing: Routes completions to the correct verifier based on metadata
- Parallel Processing: Uses Ray for efficient batch processing
- GPU Support: Supports GPU-accelerated molecular docking
Architecture
MolecularVerifier
├── GenerationVerifier → De novo molecular generation
├── MolPropVerifier → Property prediction (regression/classification)
└── ReactionVerifier → Chemical reactions & retro-synthesis
Usage
Basic Example
from mol_gen_docking.reward import (
MolecularVerifier,
MolecularVerifierConfigModel,
)
from mol_gen_docking.reward.verifiers import (
GenerationVerifierConfigModel,
ReactionVerifierConfigModel,
MolPropVerifierConfigModel
)
# Configure the verifier
config = MolecularVerifierConfigModel(
generation_verifier_config=GenerationVerifierConfigModel(
path_to_mappings="data/molgendata",
reward="property"
),
mol_prop_verifier_config=MolPropVerifierConfigModel(),
reaction_verifier_config=ReactionVerifierConfigModel()
)
# Create verifier
verifier = MolecularVerifier(config)
# Compute rewards for generation task
completions = ["<answer>CCO</answer>"]
metadata = [{"properties": ["QED"], "objectives": ["maximize"], "target": [0.0]}]
results = verifier(completions, metadata)
print(f"Rewards: {results.rewards}")
# Output:
# >>> Rewards: [0.1127273579103326]
Mixed Batch Processing
The verifier can handle batches with different task types:
completions = [
"<answer>CCO</answer>",
"<answer>0.75</answer>",
"<answer>CCN(CC)C(N)=S + NNC(=O)Cc1ccc(F)cc1 -> C[CH]N(CC)c1nnc(Cc2ccc(F)cc2)[nH]1</answer>"
]
metadata = [
{"properties": ["QED"], "objectives": ["maximize"], "target": [0.0]},
{"objectives": ["regression"], "target": [0.8], "norm_var": 0.1},
{"objectives": ["full_path"], "target": ["C[CH]N(CC)c1nnc(Cc2ccc(F)cc2)[nH]1"]}
]
results = verifier(completions, metadata)
print(results.rewards)
# Output:
# >>> Rewards: [0.1127273579103326, 0.7499999999999996, 1.0]
MolecularVerifierConfigModel
Bases: BaseModel
Pydantic model for molecular verifier configuration.
This model defines the configuration parameters for the MolecularVerifier class, providing validation and documentation for all configuration options.
The reward field is automatically propagated to all sub-verifier configurations, ensuring consistent reward type usage across all verifiers.
Attributes:
| Name | Type | Description |
|---|---|---|
parsing_method |
Literal['none', 'answer_tags', 'boxed']
|
Method to parse model completions:
|
reward |
Literal['valid_smiles', 'property']
|
Type of reward to compute. Affects all sub-verifiers:
|
generation_verifier_config |
Optional[GenerationVerifierConfigModel]
|
Configuration for de novo molecular generation tasks. Required if handling generation tasks. Contains settings for molecular property optimization, docking oracle configuration, and SMILES extraction. |
mol_prop_verifier_config |
Optional[MolPropVerifierConfigModel]
|
Configuration for molecular property prediction tasks. Required if handling property prediction tasks. Contains settings for regression and classification objective handling. |
reaction_verifier_config |
Optional[ReactionVerifierConfigModel]
|
Configuration for chemical reaction and retro-synthesis tasks. Required if handling reaction tasks. Contains settings for synthesis path validation and reaction SMARTS matching. |
Notes
- At least one sub-verifier configuration should be provided for the verifier to work
- The propagate_reward_to_subconfigs validator automatically sets the reward type and parsing_method flag in all sub-verifier configurations
- Invalid configurations will raise Pydantic ValidationError with detailed messages
Example
from mol_gen_docking.reward import MolecularVerifierConfigModel
from mol_gen_docking.reward.verifiers import (
GenerationVerifierConfigModel,
MolPropVerifierConfigModel,
ReactionVerifierConfigModel
)
# Minimal configuration with generation verifier
config = MolecularVerifierConfigModel(
generation_verifier_config=GenerationVerifierConfigModel(
path_to_mappings="data/molgendata"
)
)
# Full configuration with all verifiers
config = MolecularVerifierConfigModel(
parsing_method="answer_tags",
reward="property",
generation_verifier_config=GenerationVerifierConfigModel(
path_to_mappings="data/molgendata",
rescale=True,
oracle_kwargs={
"exhaustiveness": 8,
"n_cpu": 8,
"docking_oracle": "autodock_gpu",
"vina_mode": "autodock_gpu_256wi"
},
docking_concurrency_per_gpu=2
),
mol_prop_verifier_config=MolPropVerifierConfigModel(
reward="property"
),
reaction_verifier_config=ReactionVerifierConfigModel(
reaction_matrix_path="data/rxn_matrix.pkl",
reaction_reward_type="tanimoto"
)
)
See Also
- GenerationVerifierConfigModel: Configuration for generation tasks
- MolPropVerifierConfigModel: Configuration for property prediction tasks
- ReactionVerifierConfigModel: Configuration for reaction tasks
- MolecularVerifier: Main orchestrator class using this config
Source code in mol_gen_docking/reward/molecular_verifier_pydantic_model.py
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
Config
Pydantic configuration for the MolecularVerifierConfigModel.
Source code in mol_gen_docking/reward/molecular_verifier_pydantic_model.py
propagate_fields_to_subconfigs()
Propagate the fields shared to all sub-verifier configurations.
Source code in mol_gen_docking/reward/molecular_verifier_pydantic_model.py
BatchMolecularVerifierOutputModel
Bases: BaseModel
Output model for molecular verifier batch results.
This model encapsulates the results from batch verification across multiple tasks and completions. It provides both the computed rewards and detailed metadata about each verification process, enabling comprehensive analysis of model performance.
The output is structured as parallel lists where each index corresponds to a single completion from the input batch, allowing easy correlation between inputs and outputs.
Attributes:
| Name | Type | Description |
|---|---|---|
rewards |
list[float]
|
List of computed reward scores, one per input completion. Each reward is a float value typically in the range [0.0, 1.0], though values may exceed 1.0 for maximize objectives depending on the input values. The reward value depends on the task type and objective:
|
parsed_answer |
list[float]
|
The parsed answer extracted from the model completion. |
verifier_metadatas |
list[MolecularVerifierOutputMetadataModel]
|
List of metadata objects from each verification process. Each element corresponds to the reward at the same index. The metadata type depends on which verifier processed the completion:
|
Notes
- The length of rewards and verifier_metadatas must always match the number of input completions
- The order of outputs corresponds to the order of inputs
- Metadata provides detailed diagnostic information about failures (extraction errors, invalid molecules, validation failures, etc.)
- All numeric reward values are finite (not NaN or inf)
Example
from mol_gen_docking.reward import MolecularVerifier, MolecularVerifierConfigModel
from mol_gen_docking.reward.verifiers import GenerationVerifierConfigModel
config = MolecularVerifierConfigModel(
generation_verifier_config=GenerationVerifierConfigModel(
path_to_mappings="data/molgendata"
)
)
verifier = MolecularVerifier(config)
# Verify multiple completions
completions = [
"<answer>CCO</answer>",
"<answer>c1ccccc1</answer>",
"<answer>invalid_smiles</answer>"
]
metadata = [
{"properties": ["QED"], "objectives": ["maximize"], "target": [0.0]},
{"properties": ["QED"], "objectives": ["maximize"], "target": [0.0]},
{"properties": ["QED"], "objectives": ["maximize"], "target": [0.0]}
]
results = verifier(completions, metadata)
# Access results
print(f"Number of completions: {len(results.rewards)}")
for i, (reward, meta) in enumerate(zip(results.rewards, results.verifier_metadatas)):
print(f"Completion {i}: reward={reward:.3f}")
print(f" Properties: {meta.properties}")
print(f" All SMILES: {meta.all_smi}")
if meta.smiles_extraction_failure:
print(f" Failure reason: {meta.smiles_extraction_failure}")
# Output:
# >>> Number of completions: 3
# Completion 0: reward=0.113
# Properties: ['QED']
# All SMILES: ['CCO']
# Completion 1: reward=0.173
# Properties: ['QED']
# All SMILES: ['c1ccccc1']
# Completion 2: reward=0.000
# Properties: []
# All SMILES: []
# Failure reason: no_smiles
See Also
- MolecularVerifier: Main class that returns this model
- GenerationVerifierMetadataModel: Metadata for generation tasks
- MolPropVerifierMetadataModel: Metadata for property prediction tasks
- ReactionVerifierMetadataModel: Metadata for reaction tasks
Source code in mol_gen_docking/reward/molecular_verifier_pydantic_model.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
Molecular verifier module for computing rewards across different task types.
This module provides the MolecularVerifier class which orchestrates reward computation for molecular generation, property prediction, and chemical reaction tasks. It uses Ray for parallel processing and supports GPU-accelerated docking calculations.
MolecularVerifier
Main orchestrator for molecular verification and reward computation.
This class provides a unified interface for computing rewards across different molecular tasks: de novo generation, property prediction, and chemical reactions. It automatically routes inputs to the appropriate verifier based on metadata.
The verifier uses Ray for parallel processing and supports GPU-accelerated molecular docking calculations when configured.
Attributes:
| Name | Type | Description |
|---|---|---|
verifier_config |
Configuration model containing settings for all verifiers. |
|
logger |
Logger instance for the verifier. |
Example
from mol_gen_docking.reward import MolecularVerifier, MolecularVerifierConfigModel
from mol_gen_docking.reward.verifiers import GenerationVerifierConfigModel
config = MolecularVerifierConfigModel(
generation_verifier_config=GenerationVerifierConfigModel(
path_to_mappings="data/molgendata",
reward="property"
)
)
verifier = MolecularVerifier(config)
# Compute rewards
completions = ["<answer>CCO</answer>", "<answer>c1ccccc1</answer>"]
metadata = [
{"properties": ["QED"], "objectives": ["maximize"], "target": [0.0]},
{"properties": ["SA"], "objectives": ["minimize"], "target": [0.0]}
]
results = verifier(completions, metadata)
print(f"Rewards: {results.rewards}")
Source code in mol_gen_docking/reward/molecular_verifier.py
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
generation_verifier
property
Lazy-loaded generation verifier instance.
Returns:
| Name | Type | Description |
|---|---|---|
GenerationVerifier |
GenerationVerifier
|
The generation verifier for de novo molecular generation tasks. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If generation_verifier_config is not set in the config. |
mol_prop_verifier
property
Lazy-loaded molecular property verifier instance.
Returns:
| Name | Type | Description |
|---|---|---|
MolPropVerifier |
MolPropVerifier
|
The verifier for molecular property prediction tasks. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If mol_prop_verifier_config is not set in the config. |
reaction_verifier
property
Lazy-loaded reaction verifier instance.
Returns:
| Name | Type | Description |
|---|---|---|
ReactionVerifier |
ReactionVerifier
|
The verifier for chemical reaction and retro-synthesis tasks. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If reaction_verifier_config is not set in the config. |
__call__(completions, metadata, debug=False, use_pbar=False)
Call the verifier to compute rewards.
This is a convenience method that calls get_score().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
completions
|
List[Any]
|
List of model completions. |
required |
metadata
|
List[Dict[str, Any]]
|
List of metadata dictionaries. |
required |
debug
|
bool
|
If True, enables debug mode. |
False
|
use_pbar
|
bool
|
If True, displays a progress bar. |
False
|
Returns:
| Type | Description |
|---|---|
BatchMolecularVerifierOutputModel
|
BatchMolecularVerifierOutputModel with rewards and metadata. |
Source code in mol_gen_docking/reward/molecular_verifier.py
__init__(verifier_config)
Initialize the MolecularVerifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verifier_config
|
MolecularVerifierConfigModel
|
Configuration model containing settings for all sub-verifiers (generation, property prediction, reaction). |
required |
Source code in mol_gen_docking/reward/molecular_verifier.py
get_score(completions, metadata, debug=False, use_pbar=False)
Compute rewards for a batch of completions.
This method automatically routes each completion to the appropriate verifier based on its metadata. It supports mixed batches containing generation, property prediction, and reaction tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
completions
|
List[Any]
|
List of model completions (strings or structured outputs). |
required |
metadata
|
List[Dict[str, Any]]
|
List of metadata dictionaries, one per completion. The metadata determines which verifier is used for each completion. |
required |
debug
|
bool
|
If True, enables debug mode with additional logging. |
False
|
use_pbar
|
bool
|
If True, displays a progress bar during computation. |
False
|
Returns:
| Type | Description |
|---|---|
BatchMolecularVerifierOutputModel
|
BatchMolecularVerifierOutputModel containing: - rewards: List of float rewards for each completion - verifier_metadatas: List of metadata from each verification |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If completions and metadata have different lengths. |
Example
Source code in mol_gen_docking/reward/molecular_verifier.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
Task Routing
The verifier automatically determines the task type based on metadata structure:
| Metadata Fields | Task Type | Verifier |
|---|---|---|
properties, objectives (maximize/minimize/above/below), target |
Generation | GenerationVerifier |
objectives (regression/classification), target, norm_var |
Property Prediction | MolPropVerifier |
objectives (full_path/smarts/...), target, reactants |
Reaction | ReactionVerifier |
Performance Considerations
- Lazy Loading: Verifiers are instantiated on first use
- Ray Parallelization: Long-running computations (docking) are parallelized
- GPU Allocation: Docking uses fractional GPU allocation for concurrency
- Caching: Oracle results are cached to avoid redundant computations