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
from kups.application.potential.filter import POSITIONS_AND_CELL
from kups.application.potential.mliap.torch import make_torch_mliap_from_state
from kups.potential.mliap.torch import load_mace
model = load_mace("mace.model", compute_cell_gradients=True)
potential = make_torch_mliap_from_state(state_lens, gradient=POSITIONS_AND_CELL)
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
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 168 | |
__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 168 | |
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 subtract the (fully known) position virial and apply the h^-T
factor to recover the raw lattice gradient ∂E/∂h see
lattice_gradient_from_virial.
Source code in src/kups/potential/mliap/torch/uma.py
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 | |
__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
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 | |
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. In kUPS's row convention
(r = frac @ h; lattice vectors are the rows of h) that virial is
virial = pos_virial + cell_virial (exactly symmetric)
where
pos_virial[s, j, k] = Σ_{b∈s} (∂E/∂r_b)_j · (r_b)_k
cell_virial = (∂E/∂h)^T @ h
Rotational invariance makes the total symmetric, but cell_virial on its
own is not. Its antisymmetric part is pinned by pos_virial, known exactly
from forces and positions, so the raw lattice gradient (antisymmetric part
included) is recovered by
∂E/∂h = h^-T @ (virial - pos_virial^T).
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
|
Literal['cpu', 'cuda']
|
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
|
UMAInferenceSettings
|
|
'default'
|
Returns:
| Type | Description |
|---|---|
TorchMliap
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in src/kups/potential/mliap/torch/uma.py
255 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
make_torch_mliap_potential(particles_view, systems_view, neighborlist_view, model, patch_idx_view=None, out_cache_lens=None, gradient=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,
patch_idx_view: View[
State, PotentialOut[PositionsAndCell, EmptyType]
]
| None = None,
out_cache_lens: Lens[
State, PotentialOut[PositionsAndCell, EmptyType]
]
| None = None,
) -> Potential[
State, PositionsAndCell, 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,
patch_idx_view: View[
State, PotentialOut[PositionsAndCell, EmptyType]
]
| None = None,
out_cache_lens: Lens[
State, PotentialOut[PositionsAndCell, EmptyType]
]
| None = None,
*,
gradient: Lens[Geometry, PositionsAndCell],
) -> Potential[
State, PositionsAndCell, EmptyType, Patch[State]
]
Create a kUPS Potential from a TorchMliap.
Forces and stress are computed inside the torch module; the kUPS side just
routes the precomputed PositionsAndCell gradients through
DirectPotential. Without a gradient the raw PositionsAndCell
gradients pass through; with one they are pulled back through gradient.set
into ∂E/∂u — the pullback is hooked here, where the gradients are concretely
PositionsAndCell.
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 a cutoff-bound neighbor list from state. |
required |
model
|
Any
|
|
required |
patch_idx_view
|
Any | None
|
Cached output index structure (optional). |
None
|
out_cache_lens
|
Any | None
|
Cache location lens (optional). |
None
|
gradient
|
Lens[Geometry, PositionsAndCell] | None
|
Relaxation filter |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Configured |
Source code in src/kups/potential/mliap/torch/interface.py
torch_mliap_model_fn(inp)
¶
Run a TorchMliap on a graph input and package the result.
Always packages "cell_gradients" into a PositionsAndCell gradients
structure (the module must produce them); downstream consumers that only
need forces let XLA prune the unused cell-gradient ops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
TorchMliapInput[P, S]
|
Graph potential input bundling the model and graph. |
required |
Returns:
| Type | Description |
|---|---|
WithPatch[PotentialOut[PositionsAndCell, EmptyType], IdPatch[Any]]
|
|
WithPatch[PotentialOut[PositionsAndCell, EmptyType], IdPatch[Any]]
|
gradients, and an identity patch. |