"""MACE-small + MLP(E) + sigmoid -> hybrid mixing coefficient alpha.""" from __future__ import annotations import warnings import numpy as np import torch import torch.nn as nn from ase import Atoms import mace.data as mace_data from mace.calculators.foundations_models import mace_mp from mace.tools import torch_geometric from mace.tools.utils import AtomicNumberTable def load_weights_filtered(model, ckpt_path, device, *, verbose=False): try: ckpt = torch.load(ckpt_path, map_location=device, weights_only=True) except Exception as e1: warnings.warn("falling back to weights_only=False for checkpoint load") ckpt = torch.load(ckpt_path, map_location=device, weights_only=False) state = model.state_dict() filtered = {k: v for k, v in ckpt.items() if k in state and state[k].shape == v.shape} if verbose: for k in ckpt: if k not in filtered: print("skip", k) state.update(filtered) model.load_state_dict(state, strict=False) return model def _build_readout_head(hidden, n_layers, device, dtype): layers = [] in_dim = 1 for _ in range(n_layers): layers.append(nn.Linear(in_dim, hidden)) layers.append(nn.SiLU()) in_dim = hidden layers.append(nn.Linear(in_dim, 1)) return nn.Sequential(*layers).to(device=device, dtype=dtype) def init_readout_last_linear_small(seq, *, weight_std=1e-6, bias=0.0): last = seq[-1] with torch.no_grad(): nn.init.normal_(last.weight, mean=0.0, std=weight_std) if last.bias is not None: nn.init.constant_(last.bias, bias) class MACEAlphaHFReadout(nn.Module): def __init__( self, r_max=None, device="cuda", dtype=torch.float32, foundation="small", readout_hidden=256, readout_layers=3, last_layer_weight_std=1e-6, ): super().__init__() self.device = torch.device(device) self.dtype = dtype raw = mace_mp( model=foundation, return_raw_model=True, device=str(self.device), default_dtype="float32", ) self.mace_model = raw.float().to(device=self.device) self.readout = _build_readout_head(readout_hidden, readout_layers, self.device, dtype) init_readout_last_linear_small(self.readout, weight_std=last_layer_weight_std) zs = [int(z) for z in self.mace_model.atomic_numbers.detach().cpu().tolist()] self.z_table = AtomicNumberTable(sorted(zs)) self.r_max = float(r_max if r_max is not None else getattr(self.mace_model, "r_max", 6.0)) self.to(self.device) def _make_batch(self, elements, coords): atoms = Atoms(symbols=elements, positions=np.asarray(coords, dtype=np.float32)) atoms.set_pbc([False, False, False]) atoms.set_cell(np.eye(3, dtype=np.float32) * 100.0) config = mace_data.config_from_atoms(atoms) data = mace_data.AtomicData.from_config(config, z_table=self.z_table, cutoff=self.r_max) loader = torch_geometric.dataloader.DataLoader( dataset=[data], batch_size=1, shuffle=False, drop_last=False ) batch = next(iter(loader)).to(self.device) batch.positions = batch.positions.float() batch.node_attrs = batch.node_attrs.float() batch.shifts = batch.shifts.float() batch.cell = batch.cell.float() return batch def forward(self, elements, coords): batch = self._make_batch(elements, coords) return self.forward_atomic_batch(batch).reshape(-1)[0] def forward_atomic_batch(self, batch): batch = batch.to(self.device) batch.positions = batch.positions.float() batch.node_attrs = batch.node_attrs.float() batch.shifts = batch.shifts.float() batch.cell = batch.cell.float() out = self.mace_model(batch.to_dict(), compute_force=False, training=self.training) e = out["energy"].reshape(-1, 1) return torch.sigmoid(self.readout(e).reshape(-1))