kups.potential.mliap.interface
¶
Unified interface for graph-based machine learning interatomic potentials (MLIAPs).
This module provides generic protocols and factory functions for integrating graph-based ML potentials (MACE, NequIP, Allegro, etc.) into kUPS.
Models can return: - Energy only (Gradients=EmptyType): autodiff computes forces and Hessians - Energy + forces (Gradients=Array): model-provided forces, optional autodiff for Hessians - Energy + forces + virials (Gradients=VirialTheoremGradients): full stress support
Example (autodiff forces):
from kups.potential.mliap.interface import make_mliap_potential
def my_energy_fn(inp: MLIAPInput) -> WithPatch[PotentialOut[EmptyType, EmptyType], IdPatch]:
energy = model(inp.graph)
return WithPatch(PotentialOut(energy, EMPTY, EMPTY), IdPatch())
potential = make_mliap_potential(my_energy_fn, gradient_lens=..., ...)
Example (precomputed forces):
def my_forces_fn(inp: MLIAPInput) -> WithPatch[PotentialOut[Array, EmptyType], IdPatch]:
energy, forces = model(inp.graph)
return WithPatch(PotentialOut(energy, -forces, EMPTY), IdPatch())
potential = make_mliap_potential(my_forces_fn, ...) # No gradient_lens needed
ModelFunction
¶
Bases: Protocol
Protocol for MLIAP model functions.
Type parameter semantics: - Gradients=EmptyType, Hessians=EmptyType: energy only (autodiff required for forces) - Gradients=Array, Hessians=EmptyType: energy + forces (precomputed) - Gradients=VirialTheoremGradients, Hessians=EmptyType: energy + forces + virials
Source code in src/kups/potential/mliap/interface.py
make_mliap_potential(model_fn, particles_view, systems_view, neighborlist_view, model_view, cutoffs_view=None, gradient_lens=None, hessian_lens=None, hessian_idx_view=None, patch_idx_view=None, out_cache_lens=None)
¶
make_mliap_potential(
model_fn: ModelFunction[
Model, EmptyType, EmptyType, P, S, Ptch
],
particles_view: View[State, Table[ParticleId, P]],
systems_view: View[State, Table[SystemId, S]],
neighborlist_view: View[State, NearestNeighborList],
model_view: View[State, Model],
cutoffs_view: View[State, Table[SystemId, Array]],
gradient_lens: Lens[MLIAPInput[Model, P, S], Gradients],
hessian_lens: Lens[Gradients, Hessians],
hessian_idx_view: View[State, Hessians],
*,
patch_idx_view: View[
State, PotentialOut[Gradients, Hessians]
]
| None = None,
out_cache_lens: Lens[
State, PotentialOut[Gradients, Hessians]
]
| None = None,
) -> Potential[State, Gradients, Hessians, Patch[State]]
make_mliap_potential(
model_fn: ModelFunction[
Model, EmptyType, EmptyType, P, S, Ptch
],
particles_view: View[State, Table[ParticleId, P]],
systems_view: View[State, Table[SystemId, S]],
neighborlist_view: View[State, NearestNeighborList],
model_view: View[State, Model],
cutoffs_view: View[State, Table[SystemId, Array]],
gradient_lens: Lens[MLIAPInput[Model, P, S], Gradients],
*,
patch_idx_view: View[
State, PotentialOut[Gradients, EmptyType]
]
| None = None,
out_cache_lens: Lens[
State, PotentialOut[Gradients, EmptyType]
]
| None = None,
) -> Potential[State, Gradients, EmptyType, Patch[State]]
make_mliap_potential(
model_fn: ModelFunction[
Model, Gradients, Hessians, P, S, Ptch
],
particles_view: View[State, Table[ParticleId, P]],
systems_view: View[State, Table[SystemId, S]],
neighborlist_view: View[State, NearestNeighborList],
model_view: View[State, Model],
cutoffs_view: View[State, Table[SystemId, Array]],
*,
patch_idx_view: View[
State, PotentialOut[Gradients, Hessians]
]
| None = None,
out_cache_lens: Lens[
State, PotentialOut[Gradients, Hessians]
]
| None = None,
) -> Potential[State, Gradients, Hessians, Patch[State]]
Create a graph-based MLIAP potential.
Three modes based on arguments: - gradient_lens + hessian_lens + hessian_idx_view: autodiff with hessians - gradient_lens only: autodiff without hessians - neither: model provides gradients directly via DirectPotential
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_fn
|
Any
|
Function returning PotentialOut with energy (and optionally gradients). |
required |
particles_view
|
Any
|
View to extract particles from state. |
required |
systems_view
|
Any
|
View to extract systems (unit cell) from state. |
required |
neighborlist_view
|
Any
|
View to extract neighbor list from state. |
required |
model_view
|
Any
|
View to extract model from state. |
required |
cutoffs_view
|
Any
|
View to extract cutoffs as |
None
|
gradient_lens
|
Any
|
Lens for gradient computation (None = use model's gradients directly via DirectPotential). |
None
|
hessian_lens
|
Any
|
Lens selecting gradients for Hessian computation. |
None
|
hessian_idx_view
|
Any
|
View for Hessian row/column indices from state. |
None
|
patch_idx_view
|
Any
|
View for cached output indices (optional). |
None
|
out_cache_lens
|
Any
|
Lens for output cache (optional). |
None
|