kups.potential.mliap.torch
¶
PyTorch ML interatomic potentials.
A universal interface mirroring tojax:
each torch MLFF backend only fills in a torch.nn.Module whose forward
consumes AtomGraphInput
and returns {"energy", "position_gradients", "cell_gradients"}. All graph
extraction, padding, and kUPS Potential wiring is shared.
Example
Requires the torch_dev dependency group: uv sync --group torch_dev.
AtomGraphInput
¶
Bases: TypedDict
Universal input schema shared by all torch MLFF backends.
Mirrors the JAX AtomGraphInput.
Shapes use N atoms, B systems, and E edges (each padded by one
extra atom/system to work around backends that cannot handle empty graphs).
Source code in src/kups/potential/mliap/torch/interface.py
IsTorchMliapParticles
¶
Bases: IsRadiusGraphPoints, HasAtomicNumbers, Protocol
Particle protocol for torch MLFF models.
Source code in src/kups/potential/mliap/torch/interface.py
IsTorchMliapState
¶
Bases: Protocol
Protocol for states providing all inputs for a torch MLFF potential.
Source code in src/kups/potential/mliap/torch/interface.py
MACEModule
¶
Bases: Module
Adapter: AtomGraphInput → MACE PyG-style input → energy + gradients.
Wraps a MACE nn.Module and translates the universal graph input into
the (node_attrs, positions, edge_index, batch, ptr,
shifts, cell) tuple that MACE expects. Returns gradients of energy
w.r.t. positions (and optionally cell vectors).
Attributes:
| Name | Type | Description |
|---|---|---|
mace |
Underlying MACE |
|
species_to_index |
Tensor
|
Buffer mapping atomic number |
num_species |
Number of species the MACE model was trained on. |
|
compute_cell_gradients |
Whether to compute cell gradients (stress). |
Source code in src/kups/potential/mliap/torch/mace.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
__init__(mace_model, species_to_index, num_species, compute_cell_gradients=False)
¶
Initialise MACEModule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mace_model
|
Module
|
Underlying MACE |
required |
species_to_index
|
Tensor
|
Tensor mapping |
required |
num_species
|
int
|
Number of species the MACE model was trained on. |
required |
compute_cell_gradients
|
bool
|
Whether to compute cell gradients. |
False
|
Source code in src/kups/potential/mliap/torch/mace.py
forward(input)
¶
Run MACE on a universal AtomGraphInput and return gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
dict[str, Tensor]
|
Dict matching the universal |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dict with |
dict[str, Tensor]
|
and optionally |
Source code in src/kups/potential/mliap/torch/mace.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
TorchMliap
¶
Container for a torch MLFF wired into JAX.
Attributes:
| Name | Type | Description |
|---|---|---|
cutoff |
Table[SystemId, Array]
|
Per-system cutoff radius [Å]. |
wrapper |
TorchModuleWrapper
|
|
compute_cell_gradients |
bool
|
Whether the module returns |
Source code in src/kups/potential/mliap/torch/interface.py
call(input)
¶
from_module(module, cutoff, compute_cell_gradients=False)
staticmethod
¶
Wrap a torch.nn.Module that returns energy and gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
torch |
required |
cutoff
|
float
|
Interaction cutoff radius [Å]. |
required |
compute_cell_gradients
|
bool
|
Whether the module returns
|
False
|
Returns:
| Type | Description |
|---|---|
'TorchMliap'
|
Configured |
Source code in src/kups/potential/mliap/torch/interface.py
TorchMliapForward
¶
Bases: Protocol
Forward contract for a torch MLFF module.
The module must accept an AtomGraphInput dict and return a dict with:
"energy":(B,)per-system total energies."position_gradients":(N, 3):math:\partial E / \partial r."cell_gradients":(B, 3, 3):math:\partial E / \partial h, required only whencompute_cell_gradients=True.
Outputs are gradients (not forces); adapters around models that natively produce forces/virials negate appropriately inside the module.
Source code in src/kups/potential/mliap/torch/interface.py
UMAModule
¶
Bases: Module
Adapter: AtomGraphInput → fairchem AtomicData → energy + gradients.
Wraps a fairchem MLIPPredictUnit and translates the universal graph
input into the AtomicData object UMA expects. Returns gradients of
energy w.r.t. positions (and optionally w.r.t. cell vectors).
The wrapped predict-unit holds its own torch module and manages its own
device placement; this adapter intentionally does not register it as a
submodule (it is not an nn.Module).
Attributes:
| Name | Type | Description |
|---|---|---|
predict_unit |
fairchem |
|
task_name |
UMA inference head to route every system to. |
|
compute_cell_gradients |
Whether to also return |
Note
UMA's stress is the symmetrized strain virial V_ij /
volume from a joint symmetric strain on positions and cell
(cf. fairchem.core.models.uma.outputs.compute_forces_and_stress).
We invert the position contribution and the cell^T factor to
recover the raw lattice gradient ∂E/∂h — see
lattice_gradient_from_virial. The antisymmetric part of
cell^T @ ∂E/∂h is unrecoverable from a symmetric-strain virial
alone; for physical models with rotational invariance it is zero,
so the recovered ∂E/∂h is exact.
Source code in src/kups/potential/mliap/torch/uma.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
__init__(predict_unit, task_name='omat', compute_cell_gradients=False)
¶
Initialise UMAModule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predict_unit
|
Any
|
fairchem |
required |
task_name
|
UMATaskName | str
|
UMA inference head (e.g. |
'omat'
|
compute_cell_gradients
|
bool
|
Whether to compute cell gradients (stress). |
False
|
Source code in src/kups/potential/mliap/torch/uma.py
forward(input)
¶
Run UMA on a universal AtomGraphInput and return gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
dict[str, Tensor]
|
Dict matching the universal |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dict with |
dict[str, Tensor]
|
and optionally |
Source code in src/kups/potential/mliap/torch/uma.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
lattice_gradient_from_virial(forces, positions, batch, cell, virial)
¶
Recover ∂E/∂h from a symmetric-strain virial.
Many torch MLFF backends (MACE, UMA, …) return a virial or stress quantity that encodes the gradient of energy under a symmetric infinitesimal strain applied jointly to positions and cell:
r_b → r_b + r_b @ ε (per atom b)
h_s → h_s + h_s @ ε (per system s)
The virial returned by the backend is then
virial = sym(pos_virial + cell_virial) where:
pos_virial[s, j, k] = Σ_{b∈s} (∂E/∂r_b)_j · (r_b)_k
cell_virial = cell^T @ ∂E/∂h
sym(M) = (M + M^T) / 2
Given forces (= -∂E/∂r), positions, batch, cell, and the virial, this
function reconstructs pos_virial from -forces ⊗ positions, subtracts
it, and solves cell^T @ (∂E/∂h) = cell_virial for the raw lattice
gradient. Assumes cell^T @ ∂E/∂h is symmetric (rotational invariance
of the energy); the antisymmetric part is unrecoverable from the
symmetric-strain virial alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forces
|
'torch.Tensor'
|
|
required |
positions
|
'torch.Tensor'
|
|
required |
batch
|
'torch.Tensor'
|
|
required |
cell
|
'torch.Tensor'
|
|
required |
virial
|
'torch.Tensor'
|
|
required |
Returns:
| Type | Description |
|---|---|
'torch.Tensor'
|
|
Source code in src/kups/potential/mliap/torch/interface.py
load_mace(model_path, device='cuda', dtype='float32', compute_cell_gradients=False, cutoff=None)
¶
Load a PyTorch MACE .model into a universal TorchMliap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str | Path
|
Path to a MACE |
required |
device
|
str
|
Device to load the model onto. |
'cuda'
|
dtype
|
Literal['float32', 'float64']
|
Model precision — |
'float32'
|
compute_cell_gradients
|
bool
|
Whether to also compute virials/stress. |
False
|
cutoff
|
float | None
|
Cutoff radius [Å]. When |
None
|
Returns:
| Type | Description |
|---|---|
TorchMliap
|
|
Source code in src/kups/potential/mliap/torch/mace.py
load_uma(model_path, device='cuda', task_name='omat', compute_cell_gradients=False, cutoff=6.0, inference_settings='default')
¶
Load a Meta FAIR Chemistry UMA checkpoint into a TorchMliap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str | Path
|
Path to a UMA |
required |
device
|
str
|
Device to load the model onto. |
'cuda'
|
task_name
|
UMATaskName | str
|
UMA inference head — |
'omat'
|
compute_cell_gradients
|
bool
|
Whether to also return cell gradients
(stress). See |
False
|
cutoff
|
float
|
Cutoff radius [Å]. UMA-s-1.2 defaults to 6.0. |
6.0
|
inference_settings
|
str
|
Forwarded to
|
'default'
|
Returns:
| Type | Description |
|---|---|
TorchMliap
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in src/kups/potential/mliap/torch/uma.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
make_torch_mliap_from_state(state, *, compute_position_and_cell_gradients=False)
¶
Create a torch MLFF potential from a typed state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Any
|
Lens into a sub-state providing particles, systems, neighbor list, and torch MLFF model. |
required |
compute_position_and_cell_gradients
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
Configured torch MLFF |
Source code in src/kups/potential/mliap/torch/interface.py
make_torch_mliap_potential(particles_view, systems_view, neighborlist_view, model, cutoffs_view, compute_cell_gradients=False, patch_idx_view=None, out_cache_lens=None)
¶
make_torch_mliap_potential(
particles_view: View[State, Table[ParticleId, P]],
systems_view: View[State, Table[SystemId, S]],
neighborlist_view: View[State, NNList],
model: View[State, TorchMliap] | TorchMliap,
cutoffs_view: View[State, Table[SystemId, Array]],
compute_cell_gradients: Literal[False] = False,
patch_idx_view: View[
State, PotentialOut[Array, EmptyType]
]
| None = None,
out_cache_lens: Lens[
State, PotentialOut[Array, EmptyType]
]
| None = None,
) -> Potential[State, Array, EmptyType, Patch[State]]
make_torch_mliap_potential(
particles_view: View[State, Table[ParticleId, P]],
systems_view: View[State, Table[SystemId, S]],
neighborlist_view: View[State, NNList],
model: View[State, TorchMliap] | TorchMliap,
cutoffs_view: View[State, Table[SystemId, Array]],
compute_cell_gradients: Literal[True],
patch_idx_view: View[
State, PotentialOut[PositionAndCell, EmptyType]
]
| None = None,
out_cache_lens: Lens[
State, PotentialOut[PositionAndCell, EmptyType]
]
| None = None,
) -> Potential[
State, PositionAndCell, EmptyType, Patch[State]
]
Create a kUPS Potential from a TorchMliap.
Forces (and optionally stress) are computed inside the torch module; the
kUPS side just routes the precomputed gradients through DirectPotential.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles_view
|
Any
|
Extracts particle data from state. |
required |
systems_view
|
Any
|
Extracts system data (cell) from state. |
required |
neighborlist_view
|
Any
|
Extracts neighbor list from state. |
required |
model
|
Any
|
|
required |
cutoffs_view
|
Any
|
Extracts cutoffs as |
required |
compute_cell_gradients
|
bool
|
When |
False
|
patch_idx_view
|
Any | None
|
Cached output index structure (optional). |
None
|
out_cache_lens
|
Any | None
|
Cache location lens (optional). |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Configured |
Source code in src/kups/potential/mliap/torch/interface.py
torch_mliap_model_fn(inp, *, compute_cell_gradients=False)
¶
Run a TorchMliap on a graph input and package the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
TorchMliapInput[P, S]
|
Graph potential input bundling the model and graph. |
required |
compute_cell_gradients
|
bool
|
Whether to wrap |
False
|
Returns:
| Type | Description |
|---|---|
WithPatch[PotentialOut[Array, EmptyType], IdPatch] | WithPatch[PotentialOut[PositionAndCell, EmptyType], IdPatch]
|
|
WithPatch[PotentialOut[Array, EmptyType], IdPatch] | WithPatch[PotentialOut[PositionAndCell, EmptyType], IdPatch]
|
an identity patch. |