mlff_attack.grad_based package¶
This package contains gradient-based adversarial attack implementations for MLFF models.
Submodules¶
mlff_attack.grad_based.fgsm module¶
Fast Gradient Sign Method (FGSM) attack implementation for MLFF models.
This module implements the FGSM attack specifically for MACE models, extending the base MLFFAttack class.
- class mlff_attack.grad_based.fgsm.FGSM_MACE(model: Any, epsilon: float = 0.01, device: str = 'cpu', track_history: bool = True, target_energy: float | None = None)[source]¶
Bases:
MLFFAttackFGSM attack implementation for MACE force field models.
The Fast Gradient Sign Method computes the gradient of the loss with respect to atomic positions and perturbs them in the direction that maximizes the loss.
- model¶
MACE calculator attached to atoms
- Type:
Any
- __init__(model: Any, epsilon: float = 0.01, device: str = 'cpu', track_history: bool = True, target_energy: float | None = None)[source]¶
Initialize FGSM , clip_radius: Optional[float] = Noneattack for MACE models.
- Parameters:
model (Any) – MACE calculator (will be attached to atoms)
epsilon (float, optional) – Perturbation step size in Angstroms, by default 0.01
device (str, optional) – Device for computations (‘cpu’ or ‘cuda’), by default ‘cpu’
track_history (bool, optional) – Whether to track attack progression, by default True
target_energy (Optional[float], optional) – Optional target energy (if None, maximize energy), by default None
- attack(atoms: Any, n_steps: int = 1, clip: bool = True) Any[source]¶
Execute FGSM attack.
For standard FGSM, n_steps=1. For iterative FGSM (I-FGSM), use n_steps>1.
- Parameters:
- Returns:
Perturbed atoms object
- Return type:
Any
- attack_step(atoms: Any, step: int = 0) Any[source]¶
Perform one step of FGSM attack.
- Parameters:
atoms (Any) – Current atomic structure with MACE calculator
step (int, optional) – Current iteration number, by default 0
- Returns:
Updated atoms object with perturbed positions
- Return type:
Any
- compute_gradient(atoms: Any, loss_fn: Callable | None = None) ndarray[source]¶
Compute gradient of loss with respect to atomic positions.
- Parameters:
atoms (Any) – ASE Atoms object with MACE calculator
loss_fn (Optional[Callable], optional) – Optional custom loss function. If None, uses default (maximize energy or target energy loss), by default None
- Returns:
Gradient array with shape (n_atoms, 3)
- Return type:
np.ndarray
- get_attack_summary() dict[source]¶
Get a summary of the attack results.
- Returns:
Dictionary with attack summary statistics
- Return type:
- save_perturbation(filepath: str, atoms_original: Any | None = None, atoms_perturbed: Any | None = None, include_metadata: bool = True) None[source]¶
Save perturbation data with additional FGSM-specific information.
- Parameters:
filepath (str) – Output file path (.npz format)
atoms_original (Optional[Any], optional) – Optional original atoms (for saving chemical symbols, cell), by default None
atoms_perturbed (Optional[Any], optional) – Optional perturbed atoms, by default None
include_metadata (bool, optional) – Whether to include attack parameters, by default True
mlff_attack.grad_based.mlff_attack_class module¶
- class mlff_attack.grad_based.mlff_attack_class.MLFFAttack(model: Any, epsilon: float, device: str = 'cpu', track_history: bool = True)[source]¶
Bases:
ABCBase class for adversarial attacks on Machine Learning Force Fields.
This abstract class defines the interface for implementing various attack strategies on MLFF models like MACE, ALIGNN, etc.
- model¶
The MLFF model (e.g., MACE calculator, ALIGNN ForceField)
- Type:
Any
- attack_history¶
Dictionary storing attack trajectory information including energies, forces, perturbations, and gradients
- Type:
dict or None
- __init__(model: Any, epsilon: float, device: str = 'cpu', track_history: bool = True)[source]¶
Initialize the attack.
- abstractmethod attack_step(atoms: Any, step: int = 0) Any[source]¶
Perform one step of the adversarial attack.
- Parameters:
atoms (Any) – Current atomic structure
step (int, optional) – Current iteration number, by default 0
- Returns:
Updated atoms object with perturbed positions
- Return type:
Any
- abstractmethod compute_gradient(atoms: Any, loss_fn: Callable | None = None) ndarray[source]¶
Compute gradient of loss with respect to atomic positions.
- Parameters:
atoms (Any) – ASE Atoms object or equivalent structure
loss_fn (Optional[Callable], optional) – Optional custom loss function (default: maximize energy), by default None
- Returns:
Gradient array with shape (n_atoms, 3)
- Return type:
np.ndarray
mlff_attack.grad_based.pgd module¶
Projected Gradient Descent (PGD) attack implementation for MLFF models.
This module implements the PGD attack specifically for MACE models, extending the base MLFFAttack class.
- class mlff_attack.grad_based.pgd.PGD_MACE(model: Any, epsilon: float, alpha: float, num_iter: int, device: str = 'cpu', track_history: bool = True)[source]¶
Bases:
MLFFAttack- __init__(model: Any, epsilon: float, alpha: float, num_iter: int, device: str = 'cpu', track_history: bool = True)[source]¶
Initialize the PGD attack.
- Parameters:
model – MLFF model with calculator interface
epsilon – Maximum perturbation magnitude
alpha – Step size for each iteration
num_iter – Number of attack iterations
device – Device for PyTorch computations
track_history – Whether to track attack progression
- attack() Any[source]¶
Execute the full PGD attack over the specified number of iterations.
- Returns:
Final perturbed atomic structure after all attack iterations
- attack_step(atoms: Any, step: int = 0) Any[source]¶
Perform one step of the PGD adversarial attack.
- Parameters:
atoms – Current atomic structure
step – Current iteration number
- Returns:
Updated atomic structure after one attack step
- compute_gradient(atoms: Any, loss_fn: Callable | None = None) ndarray[source]¶
Compute gradient of loss with respect to atomic positions.
- Parameters:
atoms – ASE Atoms object or equivalent structure
loss_fn – Optional custom loss function (default: maximize energy)
- Returns:
Gradient array with shape (n_atoms, 3)