nvalchemi.models.mace.MACEWrapper#
- class nvalchemi.models.mace.MACEWrapper(model, *, reconstruction_spec=None)[source]#
Wrapper for any MACE model implementing the
BaseModelMixininterface.Accepts any MACE model variant (
MACE,ScaleShiftMACE, cuEq-converted models,torch.compile-d models, etc.). The wrapper handles:One-hot
node_attrsencoding via a pre-built GPU lookup table (no CPU round-trip per step).Gradient enabling on
positionsfor conservative force / stress computation.PBC via both
neighbor_list_shifts(integer image indices) and pre-computedshifts(physical Å vectors fromneighbor_list_shifts @ cell) passed to MACE.shiftsis always required;neighbor_list_shiftsis additionally consumed whencompute_displacement=True(stress path).
- Parameters:
model (nn.Module) – An instantiated MACE model. Any subclass of
mace.modules.MACEis accepted.reconstruction_spec (BaseSpec | None)
- model#
The underlying MACE model.
- Type:
nn.Module
- model_config#
Mutable configuration controlling which outputs are computed.
- Type:
- adapt_input(data, **kwargs)[source]#
Build the input dict expected by
MACE.forward.Handles
AtomicData -> Batchpromotion,node_attrsencoding, gradient enabling onpositions, transposingedge_indexfrom nvalchemi’s[E, 2]to MACE’s[2, E]convention, zero-filling ofneighbor_list_shifts/cellfor non-PBC systems, and pre-computation of physicalshiftsvectors fromneighbor_list_shifts @ cell.Expects COO neighbor data (
neighbor_list, optionallyneighbor_list_shifts) to be present on the batch. When used in aPipelineModelWrapper, the pipeline handles format conversion and cutoff filtering before calling this model.- Parameters:
data (AtomicData | Batch) – The input system; an
AtomicDatais promoted to a single-graphBatch.**kwargs – Unused; accepted for interface compatibility.
- Returns:
MACE inputs:
positions,node_attrs,batch,ptr,edge_index[2, E],shifts[E, 3],cell[B, 3, 3].- Return type:
dict[str, Any]
Notes
Does not call
super().adapt_input()(Batchhas nomodel_dump); gradient enabling onpositionsis handled here.
- adapt_output(raw_output, data)[source]#
Map MACE raw outputs to nvalchemi standard keys.
Normalizes
energyshape, forwardsforces/stress/hessianwhen present, and exposes MACE’snode_energyasatomic_energies, then delegates to the base auto-mapper.- Parameters:
raw_output (dict[str, Any]) – The dict returned by
MACE.forward.data (AtomicData | Batch) – The input system the outputs were computed for.
- Returns:
The standardized outputs (subset of
energy,forces,stress,hessian,atomic_energies).- Return type:
ModelOutputs
- checkpoint_spec()[source]#
Return the factory spec used to reconstruct this wrapper, if known.
Wrappers created by
from_checkpoint()store a callable spec for that factory so strategy checkpoints can rebuild optimized MACE models without introspecting the transformed inner MACE module constructor. Wrappers around arbitrary live modules returnNoneand use the generic constructor-introspection fallback.- Return type:
BaseSpec | None
- compute_embeddings(data, **kwargs)[source]#
Compute node and graph embeddings without forces or stresses.
- Parameters:
data (AtomicData | Batch) – The input system; an
AtomicDatais promoted to aBatch.**kwargs – Forwarded to
adapt_input().
- Returns:
data, with
node_embeddings[N, hidden_dim]andgraph_embeddings[B, hidden_dim](sum-pooled) written in place.model_configis not mutated.- Return type:
- property cutoff: float#
Interaction cutoff in Angstroms, read from
model.r_max.
- distribution_spec(strategy=None)[source]#
MLIPSpec for MACE under domain decomposition.
MACE uses the MPNN halo spec: every message-passing layer scatters over edges into
node_feats(halo rows kept in sync), and a final per-graph scatter over node energies produces total energy (halo rows dropped, then all-reduced across ranks). For cueq-converted checkpoints the fusedconv_tpkernel hides that gather/scatter, so the spec installs a mode-dependent conv adapter for the DD scope (_cueq_conv_unfuse_adapters): under eager DD it unfuses to the external gather + scatter plain MACE uses (halo handlers fire); under compiled DD it keeps the conv fused for memory parity with single-GPU and relies on the refresh adapter’sscatter_to_ownersfor halo correctness.Memoized on first access. The per-checkpoint additions over the base spec are: the message-passing halo refresh (
neighbor_refresh_adaptersdiscovers the concrete InteractionBlocks and declares their per-node output halo-corrected under compile;NVALCHEMI_MACE_NO_REFRESH=1drops it, debug only) and, for cueq, the conv unfuse adapters.- Parameters:
strategy (Any)
- Return type:
Any
- property embedding_shapes: dict[str, tuple[int, ...]]#
Retrieves the expected shapes of the node, edge, and graph embeddings.
- export_model(path, as_state_dict=False)[source]#
Serialize the underlying MACE model without the wrapper.
The exported file can be reloaded as a plain MACE
nn.Moduleand used with the standard MACE / ASE interface.- Parameters:
path (Path) – Output path.
as_state_dict (bool, optional) – If
True, save only thestate_dict; otherwise pickle the full model object. Defaults toFalse.
- Return type:
None
- extra_repr()#
Format the model config for
nn.Module.__repr__.- Parameters:
self (Any)
- Return type:
str
- forward(data, **kwargs)[source]#
Run the MACE model for the active outputs.
Pure and distribution-agnostic: computes exactly what
model_config.active_outputsrequests. Forces/stress run MACE’s internal autograd; anatomic_energies-only request runs an energy-only forward returning per-atom energy.- Parameters:
data (AtomicData | Batch) – The input system (with neighbor data attached).
**kwargs – Forwarded to
adapt_input().
- Returns:
The standardized outputs for the active set.
- Return type:
ModelOutputs
- classmethod from_checkpoint(checkpoint_path, device=torch.device('cpu'), enable_cueq=False, dtype=None, compile_model=False, atomic_energies=None, atomic_energies_path=None, **compile_kwargs)[source]#
Load a MACE model from a checkpoint and return a
MACEWrapper.Accepts local file paths or named MACE-MP foundation-model checkpoints (e.g.
"medium-0b2"), which are downloaded automatically to the MACE cache directory.Operations are applied in this order:
Load —
torch.loadthe checkpoint to the specified device.dtype — cast model weights to the requested dtype.
cuEq — convert to cuEquivariance format for GPU speedup.
compile —
torch.compile; freezes parameters and sets eval mode. The model is inference-only after this step.
For best GPU throughput, use
device=torch.device("cuda"),enable_cueq=True,dtype=torch.float32, andcompile_model=True. Example:model = MACEWrapper.from_checkpoint( "medium-mpa-0", device=torch.device("cuda"), dtype=torch.float32, enable_cueq=True, compile_model=True, )
- Parameters:
checkpoint_path (Path | str) – Local path to a
.ptfile, or a named checkpoint string such as"medium-0b2".device (torch.device, optional) – Target device. Defaults to CPU.
enable_cueq (bool, optional) – Convert to cuEquivariance format for GPU speedup. Defaults to
False. Requires thecuequivariancepackage.dtype (torch.dtype | None, optional) – If set, cast model weights to this dtype before cuEq conversion.
compile_model (bool, optional) – Apply
torch.compile. Sets eval mode and freezes parameters; the model is inference-only after this step.atomic_energies (Mapping[int | str, float] | None, optional) – Per-element E0 overrides keyed by atomic number.
atomic_energies_path (Path | str | None, optional) – JSON file containing per-element E0 overrides keyed by atomic number.
**compile_kwargs – Forwarded to
torch.compile.
- Return type:
- Raises:
ImportError – If
mace-torchis not installed, or ifenable_cueq=Trueand eithercuequivarianceor thecuequivariance-ops-torchCUDA kernels (thecu12/cu13dependency groups) are missing.ValueError – If
enable_cueq=Trueanddeviceis not a CUDA device.
- modify_ema_methods()[source]#
Restore cuEquivariance methods discarded by EMA model copying.
torch.optim.swa_utils.AveragedModeldeep-copies its source model. cuEquivariance fused convolution modules attach their specializedforwardmethod at runtime, and that instance method is not retained by the copy. Reapply MACE’s fusion wrapper only when it is missing.- Return type:
None