Skip to content

LoupGYM CLI and Python API

Command line (loupgym)

Invoke via loupgym COMMAND or python -m loupgym COMMAND.

loupgym finetune

Fine-tune any LoupMD foundation MACE model and output a verified .loup bundle.

Option Default Description
--model MODEL (required) Foundation label (omat0-small, mh1, polar1-s, …), .model path, or .loup bundle path
--train FILE (required, repeatable) Training frames (.extxyz, .xyz, .traj)
--valid FILE auto-split Validation frames (repeatable)
--test FILE auto-split Held-out test frames for zero-shot vs fine-tuned evaluation (repeatable)
--replay-data FILE none Replay frames mixed into training batches to prevent forgetting (repeatable)
-o, --output PATH ./finetune_out Output directory, .loup path, or .model path
--backend {cuda,pytorch} cuda cuda (pure-CUDA libloupmd_ft.so, default) or pytorch
--strategy {auto,full,top,readout,lora} auto Fine-tuning strategy (lora requires --backend pytorch)
--head NAME none Head name for multi-head models (e.g. matpes_r2scan)
--epochs N 30 Number of training epochs
--lr FLOAT 5e-4 Peak learning rate for readout/head parameters
--trunk-lr-mult FLOAT 0.25 Learning-rate multiplier for trunk parameters
--anchor-weight FLOAT 1e-2 L2 anchor regularization toward pretrained weights
--replay-weight FLOAT 0.25 Relative loss weight on --replay-data
--energy-weight FLOAT 1.0 Energy loss weight (boosted 20× in final 20% of epochs)
--force-weight FLOAT 100.0 Force loss weight
--stress-weight FLOAT auto Stress loss weight (--backend pytorch only)
--batch-size N auto Training batch size
--grad-accum N auto Gradient accumulation steps
--keep-e0 off Skip least-squares per-species E0 alignment
--charge INT none Total cell charge (polar1-* and omol0-xl)
--spin-multiplicity INT none Spin multiplicity 2S + 1 (polar1-* and omol0-xl)
--energy-key, --forces-key, --stress-key auto Custom ASE info/arrays keys
--no-verify off Skip post-training parity / gate verification
--device N 0 CUDA GPU index (-1 for CPU with --backend pytorch)
--seed N 42 Random seed
--quiet off Suppress progress output

loupgym distill

Automatically distill a fast FastLR-SK (arch=ilmlip) student potential.

Option Default Description
--teacher MODEL none Teacher foundation label, .model path, or .loup bundle
--head NAME none Teacher head name for multi-head models
--structure FILE none Starting structure(s) for automatic teacher MD sampling (repeatable)
--md-data FILE none Pre-existing MD trajectory frames to label/train on (repeatable)
--dft-data FILE none Reference DFT frames with energy/forces (repeatable)
-o, --output PATH ./distill_out Output directory or .loup path
--speed {fast50,balanced,accurate} fast50 Target MD speed preset
--budget {quick,standard,thorough} standard Compute budget for candidate search & training
--temps K [K ...] 300 600 Temperatures (K) for automatic teacher MD sampling
--dispersion {off,d3bj,d3bj-cn,d3bj-far} off Built-in force-shifted D3(BJ): Option B (d3bj), Option C (d3bj-cn), or Option D (d3bj-far)
--dispersion-functional NAME pbe D3(BJ) functional parameters (pbe, r2scan, wb97m, b3lyp, pbe0)
--dispersion-rcut FLOAT 10.0 Explicit far-pair cutoff in Å for --dispersion d3bj-far
--contact-frames N 0 Teacher-labelled close-contact frames added to the training set (needs --teacher; Short range)
--contact-weight W 0.03 Per-frame loss weight of the contact frames
--contact-sites N 3 Close contacts per contact frame
--contact-range KIND=LO:HI from the data Distance range (Å) of one contact kind (inter:A-B, bond:A-B, angle:A-B); repeatable
--zbl-switch EV 10.0 Per-atom ZBL energy (eV) at which the learned energy is switched off
--zbl-radii SPEC covalent ZBL radius per element pair: covalent (0.65 × the covalent radii's sum), data (0.85 × the closest pair in the data) or A-B=R,... (Å)
--active-learning-rounds N 0 Optional student MD active-learning refinement rounds
--device N 0 CUDA GPU index
--seed N 42 Random seed
--quiet off Suppress progress output

loupgym repair

Check a fine-tuned bundle for layer-0 radial-bias weights, which make its energy jump at the cutoff (bundles of loupgym finetune before 0.0.5; Bundles from 0.0.4), and write a copy with them set to zero.

Argument Default Meaning
BUNDLE (required) The .loup file to check
-o, --output FIXED.loup none Where to write the repaired copy; without it the command only reports

Python API

import loupgym as lg exposes the functions and classes below lazily (importing loupgym does not import torch).

Fine-tuning

finetune

finetune(*, model: str | Path, train_data: str | Path | Atoms | Sequence, valid_data: str | Path | Atoms | Sequence | None = None, test_data: str | Path | Atoms | Sequence | None = None, replay_data: str | Path | Atoms | Sequence | None = None, output: str | Path = './finetune_out', head: str | None = None, checkpoint: str | Path | None = None, sha256: str | None = None, trusted: bool = False, download: bool = False, backend: str = 'cuda', strategy: str = 'auto', epochs: int = 30, lr: float = 0.0005, trunk_lr_mult: float = 0.25, lora_rank: int = 8, lora_alpha: float = 16.0, anchor_weight: float = 0.01, replay_weight: float = 0.25, energy_weight: float = 1.0, force_weight: float = 100.0, stress_weight: float | None = None, batch_size: int | None = None, grad_accum: int | None = None, dtype: str = 'float32', use_cueq: bool = True, keep_e0: bool = False, charge: int | None = None, spin_multiplicity: int | None = None, energy_key: str | None = None, forces_key: str | None = None, stress_key: str | None = None, export_loup: bool = True, verify: bool = True, device: int = 0, seed: int = 42, verbose: bool = True) -> FinetuneResult

Fine-tune any LoupMD foundation MACE checkpoint and export a verified .loup bundle.

FinetuneResult dataclass

finetune_cuda

finetune_cuda(*, model: str | Path, train_data, valid_data=None, test_data=None, replay_data=None, output: str | Path = './finetune_out', head: str | None = None, checkpoint: str | Path | None = None, sha256: str | None = None, trusted: bool = False, download: bool = False, strategy: str = 'auto', epochs: int = 30, lr: float = 0.0005, trunk_lr_mult: float = 0.25, anchor_weight: float = 0.01, replay_weight: float = 0.25, energy_weight: float = 1.0, force_weight: float = 100.0, batch_size: int | None = None, grad_accum: int | None = None, keep_e0: bool = False, charge: int | None = None, spin_multiplicity: int | None = None, energy_key: str | None = None, forces_key: str | None = None, stress_key: str | None = None, verify: bool = True, device: int = 0, seed: int = 42, verbose: bool = True)

100% pure-CUDA foundation MACE fine-tuning (zero PyTorch in the training loop).

CudaMaceTrainer

Thin ctypes wrapper around libloupmd_ft.so (zero PyTorch).

cutoff_bias

cutoff_bias(path) -> dict

{"bias_max": largest |weight| on the bias rows, "column_at_rmax": the bias column's largest |value| at r_max, "discontinuous": whether the energy steps at the cutoff} of a bundle.

repair

repair(path, output) -> dict

Write output: path with the layer-0 bias rows zeroed (atomically). Returns :func:cutoff_bias of the input plus "output".

Auto-distillation

auto_distill

auto_distill(*, teacher: str | Path | None = None, head: str | None = None, checkpoint: str | Path | None = None, sha256: str | None = None, trusted: bool = False, structure: str | Path | Atoms | Sequence | None = None, md_data: str | Path | Sequence | None = None, dft_data: str | Path | Sequence | None = None, output: str | Path = './distill_out', speed: str = 'fast50', max_speed_us: float | None = None, budget: str = 'standard', epochs: int | None = None, n_md_frames: int | None = None, temps_k: Sequence[float] | None = None, no_teacher_aug: bool = False, dft_weight: float = 3.0, dispersion: str | None = None, dispersion_functional: str = 'pbe', dispersion_rcut: float | None = None, energy_key: str | None = None, forces_key: str | None = None, stress_key: str | None = None, device: int = 0, seed: int = 42, verbose: bool = True, contact_frames: int = 0, contact_weight: float = 0.03, contact_sites: int = 3, contact_ranges: Mapping | Sequence[str] | str | None = None, zbl_switch_ev: float = 10.0, zbl_radii: Mapping | str | None = None) -> DistillResult

Automatically analyze a system, select a speed-compliant student, train via pure CUDA, and verify.

Short range (the defaults keep the behaviour of earlier versions):

  • contact_frames: number of teacher-labelled close-contact frames added to the training set (loupgym.distill.contacts; 0 = none). A fifth as many are made from the test frames for the report.
  • contact_weight: per-frame loss weight of the contact frames (the other frames have 1, DFT frames dft_weight). They are left out of the E0 fits and of the force and molecular loss scales.
  • contact_sites: contact sites per frame; contact_ranges: KIND=LO:HI (A) overriding the ranges taken from the data, e.g. inter:O-O=1.8:2.4.
  • zbl_switch_ev: per-atom ZBL energy (eV) at which the learned energy is switched off (z_c).
  • zbl_radii: None / "covalent" (0.65 (r_cov,a + r_cov,b)), "data" (0.85 times the data's closest pair) or explicit radii such as "H-H=1.1,O-H=0.8,O-O=2.0".

DistillResult dataclass

Shared utilities

find_native_lib

find_native_lib(filename: str, *, env_var: str | None = None, make_target: str | None = None, auto_build: bool = False, extra_candidates: tuple[Path, ...] = ()) -> Path

Locate a compiled LoupGYM .so across env override, wheel _lib/, repo root, and build/.

fit_fixed_d3bj

fit_fixed_d3bj(frames: Sequence[Atoms], Z: Sequence[int], r_cut: float, *, functional: str = 'pbe', parameters: dict[str, float] | None = None, force_shift: bool = True, use_tail: bool = True, method: str = 'd3bj_fixed') -> FixedD3BJSpec

Derive a fixed-C6 D3(BJ) specification from training frames and loupmd/data/d3_tables.npz.

fit_cn_d3bj

fit_cn_d3bj(frames: Sequence[Atoms], Z: Sequence[int], r_cut: float, *, functional: str = 'pbe', parameters: dict[str, float] | None = None, force_shift: bool = True, use_tail: bool = True) -> CnD3BJSpec

Derive an Option C (coordination-dependent C6) D3(BJ) specification.

transform_labels_for_d3

transform_labels_for_d3(frames: Sequence[Atoms], spec: FixedD3BJSpec | CnD3BJSpec, *, labels_have_d3: bool = False, ref_d3_fn: Callable[[Atoms], dict] | None = None) -> list[Atoms]

Transform REF_energy, REF_forces, and REF_stress for Option B, C, or D training.

  • If labels_have_d3=False (e.g. PBE labels without D3): if ref_d3_fn is provided, adds the small local remainder (D3_ref_in - V_builtin); if ref_d3_fn is None, labels are returned unchanged (exact for saturated-CN crystals/salts where fixed-C6 matches exact D3 inside r_cut).
  • If labels_have_d3=True (e.g. ωB97M-D3BJ or OFF24 labels): subtracts V_builtin + tail (or D3_ref_out + V_builtin when ref_d3_fn is given) so student + V_builtin + tail reproduces the target labels cleanly without double-counting.