# fung-lab/mattertune architecture

> Architecture diagram of the GitHub repository fung-lab/mattertune, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fung-lab/mattertune
- Repository: https://github.com/fung-lab/mattertune

## Overview

#### Purpose and entry points
MatterTune is a Python library for fine-tuning pretrained atomistic foundation models to predict energies, forces, stresses, and custom properties, then exposing trained models to simulation workflows. The primary programmatic orchestration point is `src/mattertune/main.py`; package exports are rooted at `src/mattertune/__init__.py`. Users construct typed configs, instantiate `MatterTune`, and call tuning; examples provide executable entry points such as `examples/water-thermodynamics/water-finetune.py`.

#### Configuration-driven composition
The architecture is configuration-first: model/backbone, data, training, callbacks, logging, normalization, and wrappers are assembled from config objects. Configuration namespaces mirror runtime modules, enabling declarative selection without binding user code directly to a specific backbone.
- `src/mattertune/configs/main/__init__.py`
- `src/mattertune/configs/backbones`
- `src/mattertune/configs/data`

#### Backbone adaptation boundary
Each supported foundation model has an adapter module that normalizes pretrained-model loading, graph/model invocation, property heads, and fine-tuning behavior behind MatterTune’s common interface. Current adapters cover EqV2, JMP, M3GNet, MACE, MatterSim, NequIP/Allegro, ORB, and UMA; optional upstream model packages and their licensing remain external dependencies.
- `src/mattertune/backbones/mace_foundation/model.py`
- `src/mattertune/backbones/nequip_foundation/nequip_model.py`
- `src/mattertune/backbones/uma/model.py`

#### Data ingestion and batching
Dataset adapters convert atomistic sources into the common training-data path, including XYZ, ASE databases, JSON, Matbench, Materials Project, MP trajectories, and OMAT24. A datamodule owns loading and batching; splitting utilities support automated train/validation partitioning. This is the boundary between external datasets/services and model-ready atomistic structures.
- `src/mattertune/data/datamodule.py`
- `src/mattertune/data/xyz.py`
- `src/mattertune/data/mp.py`

#### Fine-tuning execution
The fine-tuning layer defines trainable property objectives, loss and metric calculation, optimizer/scheduler construction, loaders, and parameter-freezing behavior. It drives backbone modules through a trainer-oriented lifecycle, with normalization applied as part of target/model handling.
- `src/mattertune/finetune/base.py`
- `src/mattertune/finetune/properties.py`
- `src/mattertune/normalization.py`

#### Training operations and lifecycle hooks
Training is designed around PyTorch/Lightning-style accelerator execution, with callbacks for early stopping, exponential moving averages, checkpoint persistence, and multi-GPU result writing. Logging integrations are centralized separately. GPU and distributed execution are material runtime concerns.
- `src/mattertune/callbacks/ema.py`
- `src/mattertune/callbacks/model_checkpoint.py`
- `src/mattertune/callbacks/multi_gpu_writer.py`

#### Inference and simulation integration
Fine-tuned backbone modules are exposed through wrappers: an ASE calculator allows atomistic optimization/MD tools to request energy, force, and stress predictions; property-predictor and ensemble wrappers support direct prediction use cases. Multi-GPU and parallel inference utilities separate serving/execution concerns from model adapters. Graph partitioning is a compiled Cython acceleration path for large atomistic graphs.
- `src/mattertune/wrappers/ase_calculator.py`
- `src/mattertune/wrappers/property_predictor.py`
- `src/mattertune/wrappers/utils/graph_partition.pyx`

#### Extension and acceleration mechanisms
Recipes provide composable model/training transformations, notably LoRA and EMA, while the registry supports discoverable component selection. Pruning/partitioning workflows are demonstrated for molecular-dynamics-scale workloads, rather than forming a separate service.
- `src/mattertune/recipes/lora.py`
- `src/mattertune/registry.py`
- `examples/prune_and_partition/Li3PO4/train.py`

Main entry points: [`src/mattertune/finetune/base.py`](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/finetune/base.py) (Fine-tuning module), [`src/mattertune/configs/main/__init__.py`](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/configs/main/__init__.py) (Main configs), [`src/mattertune/main.py`](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/main.py) (MatterTune orchestrator), [`src/mattertune/data/datamodule.py`](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/data/datamodule.py) (Data module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_config["Configuration"]
  node_main_config["Main configs<br/>config namespace<br/>[__init__.py]"]
  node_backbone_configs["Backbone configs<br/>config namespace"]
  node_data_configs["Data configs<br/>config namespace"]
  node_registry["Component registry<br/>[registry.py]"]
end

subgraph group_model["Model Adaptation"]
  node_backbone_adapters{{"Foundation adapters<br/>adapter layer"}}
  node_mace_adapter["MACE adapter<br/>backbone adapter<br/>[model.py]"]
  node_nequip_adapter["NequIP/Allegro adapter<br/>backbone adapter<br/>[nequip_model.py]"]
  node_uma_adapter["UMA adapter<br/>backbone adapter<br/>[model.py]"]
  node_external_models["Upstream foundation models<br/>external dependency"]
end

subgraph group_training["Fine-tuning"]
  node_dataset_sources[("Atomistic datasets &amp; services<br/>external data")]
  node_datamodule["Data module<br/>data pipeline<br/>[datamodule.py]"]
  node_finetune["Fine-tuning module<br/>training engine<br/>[base.py]"]
  node_properties["Property objectives<br/>objective definitions<br/>[properties.py]"]
  node_normalization["Target normalization<br/>[normalization.py]"]
  node_callbacks["Training callbacks<br/>lifecycle hooks<br/>[ema.py]"]
  node_recipes["LoRA recipes<br/>training transformation<br/>[lora.py]"]
end

subgraph group_serving["Inference &amp; Simulation"]
  node_predictor["Property predictor<br/>inference wrapper"]
  node_ase_calculator["ASE calculator<br/>simulation wrapper<br/>[ase_calculator.py]"]
  node_graph_partition["Graph partitioning<br/>Cython acceleration"]
end

node_user_code["User code &amp; examples<br/>entry point<br/>[water-finetune.py]"]
node_package["Package exports<br/>Python package<br/>[__init__.py]"]
node_orchestrator["MatterTune orchestrator<br/>Python API<br/>[main.py]"]

node_user_code -->|"imports"| node_package
node_package -->|"exports"| node_orchestrator
node_user_code -->|"configures tuning"| node_orchestrator
node_orchestrator -->|"reads"| node_main_config
node_main_config -->|"selects"| node_backbone_configs
node_main_config -->|"selects"| node_data_configs
node_main_config -->|"resolves components"| node_registry
node_backbone_configs -->|"instantiates"| node_backbone_adapters
node_backbone_adapters -.->|"includes"| node_mace_adapter
node_backbone_adapters -.->|"includes"| node_nequip_adapter
node_backbone_adapters -.->|"includes"| node_uma_adapter
node_backbone_adapters -->|"loads"| node_external_models
node_dataset_sources -->|"ingests"| node_datamodule
node_data_configs -->|"configures"| node_datamodule
node_orchestrator -->|"runs"| node_finetune
node_datamodule -->|"batches training data"| node_finetune
node_finetune -->|"trains"| node_backbone_adapters
node_properties -->|"defines targets"| node_finetune
node_normalization -->|"normalizes targets"| node_finetune
node_recipes -->|"modifies training"| node_finetune
node_callbacks -->|"hooks lifecycle"| node_finetune
node_finetune -->|"produces trained model"| node_predictor
node_finetune -->|"produces trained model"| node_ase_calculator
node_graph_partition -->|"accelerates large graphs"| node_predictor
node_graph_partition -->|"accelerates large graphs"| node_ase_calculator

click node_user_code "https://github.com/fung-lab/mattertune/blob/main/examples/water-thermodynamics/water-finetune.py"
click node_package "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/__init__.py"
click node_orchestrator "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/main.py"
click node_main_config "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/configs/main/__init__.py"
click node_backbone_configs "https://github.com/fung-lab/mattertune/tree/main/src/mattertune/configs/backbones"
click node_data_configs "https://github.com/fung-lab/mattertune/tree/main/src/mattertune/configs/data"
click node_registry "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/registry.py"
click node_mace_adapter "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/backbones/mace_foundation/model.py"
click node_nequip_adapter "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/backbones/nequip_foundation/nequip_model.py"
click node_uma_adapter "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/backbones/uma/model.py"
click node_datamodule "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/data/datamodule.py"
click node_finetune "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/finetune/base.py"
click node_properties "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/finetune/properties.py"
click node_normalization "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/normalization.py"
click node_callbacks "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/callbacks/ema.py"
click node_recipes "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/recipes/lora.py"
click node_predictor "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/wrappers/property_predictor.py"
click node_ase_calculator "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/wrappers/ase_calculator.py"
click node_graph_partition "https://github.com/fung-lab/mattertune/blob/main/src/mattertune/wrappers/utils/graph_partition.pyx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main_config,node_backbone_configs,node_data_configs,node_registry toneBlue
class node_backbone_adapters,node_mace_adapter,node_nequip_adapter,node_uma_adapter,node_external_models toneAmber
class node_dataset_sources,node_datamodule,node_finetune,node_properties,node_normalization,node_callbacks,node_recipes toneMint
class node_predictor,node_ase_calculator,node_graph_partition toneRose
class node_user_code,node_package,node_orchestrator toneNeutral
```

## Components

### Configuration

Declarative component selection.

- [Main configs](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/configs/main/__init__.py): config namespace · `src/mattertune/configs/main/__init__.py`
- [Backbone configs](https://github.com/fung-lab/mattertune/tree/main/src/mattertune/configs/backbones): config namespace · `src/mattertune/configs/backbones`
- [Data configs](https://github.com/fung-lab/mattertune/tree/main/src/mattertune/configs/data): config namespace · `src/mattertune/configs/data`
- [Component registry](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/registry.py): registry · Supports discoverable component selection. · `src/mattertune/registry.py`

### Model Adaptation

Common interface over foundation models.

- **Foundation adapters**: adapter layer · Normalizes loading, invocation, heads, and fine-tuning across backbones.
- [MACE adapter](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/backbones/mace_foundation/model.py): backbone adapter · `src/mattertune/backbones/mace_foundation/model.py`
- [NequIP/Allegro adapter](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/backbones/nequip_foundation/nequip_model.py): backbone adapter · `src/mattertune/backbones/nequip_foundation/nequip_model.py`
- [UMA adapter](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/backbones/uma/model.py): backbone adapter · `src/mattertune/backbones/uma/model.py`
- **Upstream foundation models**: external dependency · Optional model packages remain outside MatterTune.

### Fine-tuning

Data, objectives, and training lifecycle.

- **Atomistic datasets & services**: external data · XYZ, ASE DB, JSON, Materials Project, and benchmark sources.
- [Data module](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/data/datamodule.py): data pipeline · Loads, splits, and batches model-ready atomistic structures. · `src/mattertune/data/datamodule.py`
- [Fine-tuning module](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/finetune/base.py): training engine · Owns property objectives, losses, metrics, optimization, and freezing. · `src/mattertune/finetune/base.py`
- [Property objectives](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/finetune/properties.py): objective definitions · `src/mattertune/finetune/properties.py`
- [Target normalization](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/normalization.py): normalization · `src/mattertune/normalization.py`
- [Training callbacks](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/callbacks/ema.py): lifecycle hooks · EMA, checkpointing, early stopping, and multi-GPU result handling. · `src/mattertune/callbacks/ema.py`
- [LoRA recipes](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/recipes/lora.py): training transformation · `src/mattertune/recipes/lora.py`

### Inference & Simulation

Prediction and atomistic simulation integration.

- [Property predictor](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/wrappers/property_predictor.py): inference wrapper · Provides direct property prediction and ensemble use. · `src/mattertune/wrappers/property_predictor.py`
- [ASE calculator](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/wrappers/ase_calculator.py): simulation wrapper · Exposes energy, force, and stress predictions to ASE workflows. · `src/mattertune/wrappers/ase_calculator.py`
- [Graph partitioning](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/wrappers/utils/graph_partition.pyx): Cython acceleration · Compiled partitioning for large atomistic graphs. · `src/mattertune/wrappers/utils/graph_partition.pyx`

### Other components

- [User code & examples](https://github.com/fung-lab/mattertune/blob/main/examples/water-thermodynamics/water-finetune.py): entry point · `examples/water-thermodynamics/water-finetune.py`
- [Package exports](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/__init__.py): Python package · `src/mattertune/__init__.py`
- [MatterTune orchestrator](https://github.com/fung-lab/mattertune/blob/main/src/mattertune/main.py): Python API · Builds and runs a tuning workflow. · `src/mattertune/main.py`

## Connections

- User code & examples → Package exports: imports
- Package exports → MatterTune orchestrator: exports
- User code & examples → MatterTune orchestrator: configures tuning
- MatterTune orchestrator → Main configs: reads
- Main configs → Backbone configs: selects
- Main configs → Data configs: selects
- Main configs → Component registry: resolves components
- Backbone configs → Foundation adapters: instantiates
- Foundation adapters → MACE adapter: includes
- Foundation adapters → NequIP/Allegro adapter: includes
- Foundation adapters → UMA adapter: includes
- Foundation adapters → Upstream foundation models: loads
- Atomistic datasets & services → Data module: ingests
- Data configs → Data module: configures
- MatterTune orchestrator → Fine-tuning module: runs
- Data module → Fine-tuning module: batches training data
- Fine-tuning module → Foundation adapters: trains
- Property objectives → Fine-tuning module: defines targets
- Target normalization → Fine-tuning module: normalizes targets
- LoRA recipes → Fine-tuning module: modifies training
- Training callbacks → Fine-tuning module: hooks lifecycle
- Fine-tuning module → Property predictor: produces trained model
- Fine-tuning module → ASE calculator: produces trained model
- Graph partitioning → Property predictor: accelerates large graphs
- Graph partitioning → ASE calculator: accelerates large graphs
