MACE Fine-tuning

SkillDatabases & data

Fine-tune MACE machine learning interatomic potentials on custom datasets.

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the MACE Fine-tuning skill

What this skill tells your AI

The instructions your AI receives, as published by learningmatter-mit/atomisticskills in .agents/skills/ml-mace-finetune/SKILL.md and read by ahel’s review.

Goal

To evaluate and improve the accuracy of a foundation MACE potential for a specific chemical system or physical property using the provided Python fine-tuning script and data-augmentation.

Instructions

  1. Prepare Labeled Dataset: Obtain diverse structures with high-fidelity labels (energy, forces, stress). See the /benchmark-finetuning workflow for details.
  2. Custom Data Conversion: Read the source data format and write a customized conversion script if needed, formatting it for the subsequent preparation step.
  3. Benchmarking: Predict results on the new labels and benchmark the foundation model using ml-mlip-benchmark.
  4. Data Preparation: Execute scripts/prepare_mace_data.py to convert JSON structures to .xyz data files.
  5. Config Generation: Execute scripts/generate_mace_config.py using the .xyz data to produce finetune_config.yaml.
  6. Fine-Tuning: Execute mace_run_train --config /path/to/finetune_config.yaml to begin fine-tuning natively on the GPU.
  7. Validation: Verify convergence and compare against the benchmarked foundation metrics.
  8. Registration: Use the register_model tool to register the newly fine-tuned model checkpoint into the local registry so future research tasks can discover and reuse it.

Training Configuration

MACE fine-tuning is divided into a data preparation step, a configuration generation step, and a standard native training run. The script scripts/prepare_mace_data.py generates .xyz files, and scripts/generate_mace_config.py converts arguments into a fully-formed finetune_config.yaml configuration compatible with the MACE default parser.

Basic Arguments (Data Prep Script)

KeyTypeDefaultDescription
--datastr(Required)Path to JSON file containing ASE/pymatgen structure dictionaries
--output-dirstr./fine_tuning_dataDirectory to save the converted .xyz data
--val-splitfloat0.1Fraction of data to set aside for validation
--seedint42Random seed for validation splitting
--vasp-stress-conversionflag-If set, multiplies stress values by -1/160.2x to convert VASP raw kB to eV/ų

Basic Arguments (Configuration Generation Script)

KeyTypeDefaultDescription
--train-filestr(Required)Path to the converted train.xyz data
--valid-filestrNonePath to the valid.xyz data
--modelstrMACE-MP-smallBase model name or path to a checkpoint
--epochsint100Number of training epochs
--lrfloat0.01Peak learning rate for training
--batch-sizeint2Training batch size
--output-dirstr./fine_tuningDirectory to save the fine-tuned model and logs
--devicestrcudaTarget compute device (cuda or cpu)

[!NOTE] If you have created a dedicated research directory for your current workflow (e.g. using the create_research_dir tool), you should set the --output-dir argument to a folder within that active research directory to keep all artifacts and models organized.

Model Freezing and Heads (Configuration Generation Script)

KeyTypeDefaultChoicesDescription
--freeze-backboneflagN/AAdd flagFreeze backbone (interaction blocks); only readout heads are trained.
--reinit-headflagN/AAdd flagRe-initialize readout weights. When absent (default), pre-trained readout is preserved.
--multiheadsflagN/AAdd flagEnable multi-head fine-tuning (adds new head while keeping existing ones).

Advanced Parameters (Manual YAML Injection)

[!IMPORTANT] The following parameters govern Optimizer, Regularization, and Scheduling. They are NOT exposed via the prepare_mace_data.py CLI. Note that prepare_mace_data.py exposes --energy-weight, --forces-weight (default 10.0), and --stress-weight which inject cleanly into the initial YAML. For all other properties below, you must manually append the keys to the generated finetune_config.yaml file prior to running mace_run_train.

Optimizer & Regularization
KeyTypeDefaultChoices / RangeDescription
optimizerstr"adam""adam", "adamw", "schedulefree"Optimizer type.
weight_decayfloat5e-7≥0L2 weight decay.
amsgradboolTrueTrue, FalseUse AMSGrad variant of Adam.
clip_gradfloat10.0>0 or NoneMaximum gradient norm for clipping. Set to None to disable.
emaboolTrueTrue, FalseEnable exponential moving average of model weights.
ema_decayfloat0.990–1EMA decay rate. Higher = more smoothing.

LR Scheduler

KeyTypeDefaultChoices / RangeDescription
schedulerstr"ReduceLROnPlateau""ReduceLROnPlateau", "ExponentialLR"LR scheduler type.
lr_factorfloat0.80–1Factor by which LR is reduced on plateau (for ReduceLROnPlateau).
scheduler_patienceint50≥1Epochs without improvement before reducing LR (for ReduceLROnPlateau).
lr_scheduler_gammafloat0.99930–1Per-epoch multiplicative decay factor (for ExponentialLR).

ReduceLROnPlateau (default): Monitors validation loss. When loss stops improving for scheduler_patience epochs, LR is multiplied by lr_factor.

ExponentialLR: LR decays every epoch by lr_scheduler_gamma. At epoch n: LR = learning_rate × lr_scheduler_gamma^n.

Early Stopping

KeyTypeDefaultChoices / RangeDescription
patienceint2048≥1Stop training after this many epochs without improvement.

[!NOTE] Default patience=2048 effectively disables early stopping. Set lower (e.g., 100–200) if you want to stop early.

Loss Function

KeyTypeDefaultChoicesDescription
lossstr"weighted""weighted", "universal", etc.Loss function type. "universal" is auto-set by data script when stress data is detected.
energy_weightfloat1.0≥0Weight for energy loss (exposed via CLI).
forces_weightfloat10.0≥0Weight for forces loss (exposed via CLI).
stress_weightfloat1.0≥0Weight for stress loss (exposed via CLI).
compute_forcesboolTrueTrue, FalseInclude forces in training.
compute_stressboolFalseTrue, FalseInclude stress in training (auto-enabled by data script).

[!WARNING] Stress Units: MACE expects stress in eV/ų. Raw VASP stress obtained directly via some JSON files may be in kilo-Bar (kB), which is ~160x larger and will cause catastrophic training divergence. The Atomate2 MCP tool handles this conversion automatically when convert_units=True. However, if your JSON labels contain raw kB stress, you MUST pass the --vasp-stress-conversion flag to scripts/prepare_mace_data.py to automatically scale them by -1/160.2x. For more details on unit standardization, see @[.agents/skills/general-property-units/SKILL.md].

[!WARNING] Learning rate sensitivity for MACE-OMAT: The official MACE docs recommend lr=0.01 for MACE-MP-0, but MACE-OMAT-0-small requires lr=1e-4 to avoid divergence. Higher values (1e-3, 0.01) cause catastrophic forgetting even with frozen backbone + EMA.

[!IMPORTANT] The generated finetune_config.yaml maps exactly to mace_run_train arguments. You can open and manually modify the YAML file before running mace_run_train to inject any advanced parameter.

Usage:

# 1. Prepare Data
conda run -n mace-agent python .agents/skills/ml-mace-finetune/scripts/prepare_mace_data.py \
    --data /path/to/training_data.json \
    --output-dir ./mace_finetuned_data

# 2. Generate Configuration
conda run -n mace-agent python .agents/skills/ml-mace-finetune/scripts/generate_mace_config.py \
    --train-file ./mace_finetuned_data/train.xyz \
    --valid-file ./mace_finetuned_data/valid.xyz \
    --model MACE-OMAT-0-small \
    --epochs 10 \
    --lr 1e-4 \
    --batch-size 2 \
    --freeze-backbone \
    --output-dir ./mace_finetuned

# 3. Run Training
conda run -n mace-agent mace_run_train --config ./mace_finetuned/finetune_config.yaml

# 4. Extract Training Logs (Optional, to create standard training_history.json)
conda run -n base-agent python .agents/skills/ml-mace-finetune/scripts/extract_mace_logs.py \
    --results-dir ./mace_finetuned/results

Examples

See the mace-wbm-finetune directory for a complete, runnable example of 10-epoch fine-tuning on high-energy crystal structures (WBM dataset), including exact usage of the --vasp-stress-conversion flag and diagnostic output artifacts.

Constraints

  • Data Size: For small datasets (<500 structures), freeze_backbone=True is strongly recommended.
  • Reference Energies (E0s): If your fine-tuning data is computed using the same DFT functional (e.g., PBE) as the foundation model's original training data, you should reuse the foundation model's original isolated atom reference energies (E0s) instead of re-fitting them. This maintains thermodynamic compatibility across the periodic table for elements not in your fine-tuning set.
  • Units (input): Stress labels must be in eV/ų as per project standards.
  • Units (output): All training_history.json files use meV units: energy MAE in meV/atom, force MAE in meV/Å, stress MAE in meV/ų.

Author: Bowen Deng Contact: GitHub @learningmatter-mit

Signals

GitHub stars
164
Forks
24
Last commit
Sep 2026
Advanced
Catalog kind
skill
Gateway key
ml-mace-finetune
Source
github.com/learningmatter-mit/atomisticskills