How cvignac/midi works
1. Overall shape
- This is a research codebase for mixed discrete + 3D diffusion on molecules.
- The runtime center is midi/main.py, which wires together configuration, dataset loading, model construction, training, checkpoint resume, and sampling/evaluation.
- The repository is organized around four main flows: data preprocessing/loading, diffusion model training, metric computation, and post-hoc chemical analysis/visualization.
2. Configuration and experiment composition
- The configs/ tree is a Hydra-style config system.
- configs/config.yaml and the general/, train/, model/, dataset/, and experiment/ subfolders separate:
- global runtime settings,
- dataset-specific choices,
- model variant parameters,
- training defaults,
- experiment presets like qm9_with_h_uniform or geom_no_h_adaptive.
- This is the main boundary for switching between QM9 vs GEOM, explicit vs implicit hydrogens, and uniform vs adaptive sampling.
3. Dataset subsystem
- midi/datasets/ contains the dataset layer:
- qm9_dataset.py and geom_dataset.py define the two supported molecular datasets.
- abstract_dataset.py defines shared dataset/datamodule/infos abstractions.
- dataset_utils.py holds shared helpers.
- adaptive_loader.py suggests a custom batching/loading path for variable-size molecular graphs.
- The data pipeline is built to produce graph-structured molecules with 3D coordinates, along with metadata needed by the model and metrics.
4. GEOM preprocessing boundary
- geom_preprocessing.py is a distinct preprocessing stage for GEOM raw files.
- The README confirms GEOM starts as raw pickles placed under data/geom/raw/, so the architecture separates raw chemistry data ingestion from training-time loading.
- test_geom.py indicates dataset validation/test utilities live alongside preprocessing rather than in the training loop.
5. Core diffusion model
- midi/diffusion_model.py is the high-level diffusion model wrapper.
- The midi/diffusion/ package implements the denoising process:
- noise_model.py for noise schedules / corruption process,
- diffusion_utils.py for shared math and sampling helpers,
- distributions.py for probability distributions used in the process,
- extra_features.py and extra_features_molecular.py for conditioning / structural features.
- The architectural emphasis is on jointly denoising molecular graph structure and 3D geometry, not just one modality.
6. Neural network backbone
- midi/models/transformer_model.py and midi/models/layers.py define the learnable network.
- The model appears to be a transformer-style graph architecture operating over molecular entities and their relations, which is consistent with diffusion on structured molecular states.
- This layer sits between the diffusion process and the raw dataset: it learns to predict denoising updates from corrupted molecular graphs/coordinates.
7. Training and runtime control
- The repository’s main execution path is training or sampling via main.py.
- The README shows support for:
- normal training,
- resuming from checkpoints,
- test-only / evaluation-only runs.
- Outputs and checkpoints are managed externally from the code modules, and the config-driven entrypoint chooses dataset/model/experiment combinations.
8. Metrics subsystem
- midi/metrics/ separates evaluation from training:
- abstract_metrics.py and metrics_utils.py provide shared metric scaffolding,
- train_metrics.py covers training-time reporting,
- molecular_metrics.py implements molecule-specific sampling metrics.
- The README’s sampling_metrics(...) entry point shows this layer evaluates generated molecules rather than generic ML loss alone.
9. Chemical analysis and conversion
- midi/analysis/ is the post-processing layer for turning generated outputs into chemically meaningful results.
- rdkit_functions.py is the bridge from graph outputs to RDKit molecule objects.
- visualization.py handles inspection/plotting.
- analyse_charges.py and baselines_evaluation.py suggest auxiliary studies on molecular properties and baseline comparisons.
- This module is where generated samples become analyzable chemistry artifacts rather than just tensors.
10. External chemistry tooling
- RDKit is a hard dependency and materially shapes the architecture because molecule parsing, validity checks, and metric computation depend on it.
- The README also describes OpenBabel as an external baseline tool for bond inference from XYZ files; that sits outside the learned model but inside the evaluation workflow.
11. Data flow from raw molecules to metrics
- Raw QM9/GEOM data enters through dataset-specific loaders and preprocessing.
- The loaders produce molecular graph states and metadata.
- The diffusion model corrupts and denoises these states during training.
- At sampling time, the model generates new molecular graphs/coordinates.
- Generated outputs are converted to RDKit molecules and passed into molecular metrics and visualization utilities.
12. Key architectural boundary
- The biggest boundary is between chemical graph representation and evaluation/analysis representation.
- Inside the model, molecules are treated as structured tensors and graph states.
- Outside the model, they are converted into RDKit molecules, SMILES, or files for downstream chemistry evaluation.
- That boundary is what makes the repo suitable for both model research and chemical benchmarking.
13. Extension points
- To add a new dataset, the repository expects a new dataset module under midi/datasets/ plus a matching config under configs/dataset/.
- New experiment behavior is usually expressed by composing config files rather than editing core training logic.
- This indicates a clean separation between research variation and core diffusion architecture.
Main entry points: config.yaml Base config · main.py Main entry · diffusion_model.py Diffusion wrapper · abstract_dataset.py Dataset base
Components 22
Config system
Hydra-style experiment composition and runtime defaults
- Base config · Top-level config composition for runs
configs/config.yaml - General defaults · Shared experiment/runtime settings
configs/general/general_default.yaml - Train defaults · Optimization and training defaults
configs/train/train_default.yaml - Model config · Discrete/model variant parameters
configs/model/discrete.yaml - Dataset configs · QM9 and GEOM dataset selection and settings
configs/dataset/qm9.yaml - Experiments · Preset compositions for QM9/GEOM, hydrogen mode, and sampling strategy
configs/experiment/qm9_with_h_uniform.yaml
Data pipeline
Raw molecular data, preprocessing, and training-time loaders
- QM9 loader · QM9 molecular dataset handling
midi/datasets/qm9_dataset.py - GEOM loader · GEOM molecular dataset handling
midi/datasets/geom_dataset.py - Dataset base · Shared dataset/datamodule/infos interfaces
midi/datasets/abstract_dataset.py - Adaptive loader · Custom loading path for variable-size molecular graphs
midi/datasets/adaptive_loader.py - GEOM prep · Raw GEOM preprocessing stage
midi/datasets/geom_preprocessing.py
Diffusion core
Model, diffusion process, and neural backbone
- Main entry · Wires config, data, model, training, resume, and sampling
midi/main.py - Noise model · Corruption schedule and denoising process
midi/diffusion/noise_model.py - Diffusion utils · Shared diffusion math and sampling helpers
midi/diffusion/diffusion_utils.py - Extra features · Structural and molecular conditioning features
midi/diffusion/extra_features_molecular.py - Diffusion wrapper · High-level diffusion model used by training and sampling
midi/diffusion_model.py - Transformer model · Graph transformer backbone for denoising predictions
midi/models/transformer_model.py - Model layers · Reusable network layers for the backbone
midi/models/layers.py
Evaluation & analysis
Metrics, RDKit conversion, and post-hoc inspection
- Metrics · Training-time and molecule-level metric scaffolding
midi/metrics/molecular_metrics.py - RDKit bridge · Converts generated graphs into RDKit molecules
midi/analysis/rdkit_functions.py - Visualization · Plots and inspection utilities for generated molecules
midi/analysis/visualization.py - Chemical analysis · Auxiliary analysis, charges, and baseline comparison workflows
midi/analysis/baselines_evaluation.py
Connections 22
- Main entry → Base config: loads · Composes the run configuration
- Base config → General defaults: includes · Shares global defaults
- Base config → Train defaults: includes · Adds training defaults
- Base config → Model config: includes · Selects model variant
- Base config → Dataset configs: includes · Selects dataset family
- Base config → Experiments: includes · Applies experiment preset
- Main entry → Dataset base: builds from · Creates dataset/datamodule objects
- Dataset base → QM9 loader: specializes · QM9-specific implementation
- Dataset base → GEOM loader: specializes · GEOM-specific implementation
- GEOM loader → GEOM prep: depends on · Uses preprocessing artifacts
- Dataset base → Adaptive loader: uses · Variable-size molecular batching
- Main entry → Diffusion wrapper: instantiates · Constructs training/sampling model
- Diffusion wrapper → Noise model: drives · Applies corruption and denoising schedule
- Diffusion wrapper → Diffusion utils: uses · Sampling and diffusion math helpers
- Diffusion wrapper → Extra features: conditions on · Adds molecular structural features
- Diffusion wrapper → Transformer model: runs · Delegates prediction to backbone
- Transformer model → Model layers: composes · Built from reusable layers
- Main entry → Metrics: reports · Uses metrics during training and sampling
- Main entry → RDKit bridge: converts to · Turns generated structures into molecules
- RDKit bridge → Visualization: feeds · Visualization operates on RDKit outputs
- RDKit bridge → Chemical analysis: feeds · Baseline and property analysis use chemistry objects
- Metrics → RDKit bridge: evaluates via · Molecular metrics depend on chemical conversion