nvalchemi.models.uma.UMAWrapper#
- class nvalchemi.models.uma.UMAWrapper(predict_unit, task_name='omol', train=False)[source]#
Wrapper for fairchem’s UMA (Universal Models for Atoms).
Wraps a
fairchem.core.units.mlip_unit.MLIPPredictUnit— the level at which energy / forces / stress are computed by fairchem (the raw backbone only produces node embeddings). Task is fixed at construction;active_outputsreflects what that task supports.- Parameters:
predict_unit (fairchem.core.units.mlip_unit.MLIPPredictUnit) – Pre-loaded UMA prediction unit. Use
from_checkpoint()for the typical construction path that resolves a registered checkpoint name and downloads via HuggingFace Hub.task_name (str) – UMA task: one of
_UMA_TASKS. Determines which per-task head in the multi-task model is used and which inputs (charge, spin) must be populated.train (bool) –
False(default) freezes all weights for inference (lossless for autograd forces);Truekeeps fairchem’s trainable/frozen split so weights stay exposed for fine-tuning.
- model_config#
Task-dependent outputs + autograd + neighbor config.
- Type:
- task_name#
The UMA task this wrapper is pinned to.
- Type:
str
- adapt_input(data, **kwargs)[source]#
Convert an nvalchemi
AtomicData/Batchto a fairchem graph.Tensor-native (no ASE round trip): tensors stay on
data.positions.device, preserving GPU residency and autograd.edge_indexis left empty(2, 0)so fairchem’sMLIPPredictUnitrebuilds the graph internally, matching the defaultFAIRChemCalculatorpath (r_edges=False), so outputs are equivalent. Charge/spin default per the ASE-calculator convention (per-system LongTensors; spin defaults to the closed-shell singlet for OMol, 0 for periodic tasks) unless the caller provides them on the batch.- Parameters:
data (AtomicData | Batch) – The input system; an
AtomicDatais promoted to a single-graphBatch.**kwargs – Unused; accepted for interface compatibility.
- Returns:
The fairchem graph:
pos[N, 3],atomic_numbers[N],cell[B, 3, 3],pbc[B, 3], per-systemcharge/spin[B], and emptyedge_index[2, 0].- Return type:
fairchem.core.datasets.atomic_data.AtomicData
- adapt_output(raw, data=None)[source]#
Map fairchem’s prediction dict to nvalchemi’s output keys.
- Parameters:
raw (dict) – fairchem’s prediction dict:
"energy"(per-system),"forces"(per-atom[N, 3]), and optionally"stress"(per-system).data (AtomicData | Batch | None, optional) – The input system the outputs were computed for. Unused here.
- Returns:
The active subset of
energy[B, 1],forces[N, 3], andstress[B, 3, 3].- Return type:
ModelOutputs
- compute_embeddings(data, **kwargs)[source]#
Run the backbone only and attach node embeddings.
UMA/eSEN backbones return
{"embedding": [N, sph, ch], "batch": [N]}; the embedding is attached as a node property so pipelines can consume it without re-running the heads.- Parameters:
data (AtomicData | Batch) – The input system; an
AtomicDatais promoted to aBatch.**kwargs – Forwarded to
adapt_input().
- Returns:
data, with
node_embeddings[N, sph, ch]attached when the backbone returns an embedding.- Return type:
- property cutoff: float#
Radial cutoff (Å) for neighbor-list construction.
- distribution_spec(strategy=None)[source]#
MLIPSpec for UMA under domain decomposition.
Each rank computes a full forward over its
owned + ghostatoms on plain tensors; DD happens only at the boundaries: per-block ghost-row feature refresh, owned-only + all-reduce per-system energy reduction, and forces/stress through fairchem’s autograd (ghost contributions routed to owners in consolidation). Nothing is sharded (shard_fieldsis empty); the spec carries the fixed-shape-caps_UMAGraphPadder.- Returns:
The memoized halo spec: boundary adapters, empty
shard_fields, and aCompilePolicycarrying the graph padder.- Return type:
- Parameters:
strategy (Any)
- property embedding_shapes: dict[str, tuple[int, ...]]#
Shape of the per-node backbone embedding.
eSCN-MD (and eSEN) backbones produce
[N, (lmax+1)^2, sphere_channels], read off the backbone’ssph_feature_size/sphere_channelsattrs.- Returns:
{"node_embeddings": (sph_feature_size, sphere_channels)}.- Return type:
dict[str, tuple[int, …]]
- Raises:
RuntimeError – If the predict unit’s module exposes no
backbone.
- extra_repr()#
Format the model config for
nn.Module.__repr__.- Parameters:
self (Any)
- Return type:
str
- forward(data, **kwargs)[source]#
Run the UMA predict unit on
data.Pipeline:
adapt_input->MLIPPredictUnit.predict->adapt_output. The single distribution touchpoint isctx.maybe_pad_graph, which under compiled domain decomposition pads the fairchem graph to stable per-rank shapes (a no-op single-process). The two blocks below handle fairchem’s own compile requirements: CPU routing for the first-call MoLE merge, and forcing static shapes.- Parameters:
data (AtomicData | Batch) – Input structure(s); promoted to a
Batchbyadapt_input.kwargs (Any)
- Returns:
energy(per system) plusforces/stresspermodel_config.active_outputs.- Return type:
ModelOutputs
- classmethod from_checkpoint(name_or_path, task_name='omol', device='cpu', inference_settings='default', overrides=None, train=False)[source]#
Resolve and load a UMA checkpoint.
Accepts either a registered model name (
"uma-s-1p1"/"uma-s-1p2"/"uma-m-1p1") or a local filesystem path to a.ptfile. The multi-task checkpoints ship all five task heads;task_namepicks which one the wrapper exposes viamodel_config.active_outputs.- Parameters:
name_or_path (str | Path) – Registered model name (see
fairchem.core.calculate.pretrained_mlip.available_models) or a local file path.task_name (str) – One of
omol,omat,oc20,odac,omc. Defaults toomol(molecular chemistry) — the most common entry point; override explicitly for crystals / catalysis.device (str | torch.device) – Target device for inference. Defaults to
"cpu".inference_settings (InferenceSettings | str) – fairchem inference configuration. Either a preset name (
"default"or"turbo") or afairchem.core.units.mlip_unit.api.inference.InferenceSettingsinstance.torch.compileis reached through this argument — see the module docstring’s torch.compile section. Defaults to"default".overrides (dict | None) – Optional overrides forwarded to fairchem’s inference-settings builder.
train (bool) – If
False(default), freeze all weights for inference — lossless, since conservative forces come from autograd on positions. IfTrue, keep fairchem’s loaded trainable/frozen split so weights remain exposed for fine-tuning. Note: theforwardpath goes through fairchem’s inferencepredict(eval mode, detached forces); gradient-based training requires a separate path through the raw model.
- Returns:
A wrapper pinned to
task_nameover the loaded predict unit.- Return type:
- Raises:
ValueError – If
name_or_pathis neither a registered model name nor a local file path.