Download atomtoken/geometry.py from Scinetics-ai/all-atom-structure-tokenizer: direct link, hf CLI and curl.
- Browser
- Download file 4.33 kB
-
https://huggingface.co/Scinetics-ai/all-atom-structure-tokenizer/resolve/main/atomtoken/geometry.py
- Command line
-
hf download hf://Scinetics-ai/all-atom-structure-tokenizer/atomtoken/geometry.py
-
curl -L -o geometry.py https://huggingface.co/Scinetics-ai/all-atom-structure-tokenizer/resolve/main/atomtoken/geometry.py
4.33 kB
| """Relative geometric features. Directed edges are indexed as receiver, sender.""" | |
| import torch | |
| from torch import nn | |
| from torch.nn import functional as F | |
| from .chemistry import FEATURE_SIZES | |
| def reduce_sum(values, index, size): | |
| return values.new_zeros(size, *values.shape[1:]).index_add_(0, index, values) | |
| def radial(distance): | |
| centers = torch.linspace(0, 16, 16, device=distance.device, dtype=distance.dtype) | |
| return torch.exp(-((distance[..., None] - centers) / 1.2).square()) | |
| def signed_volumes(x, triplets): | |
| a, b, c, d = triplets.T | |
| p, q, r = [F.normalize(x[i] - x[a], dim=-1, eps=1e-7) for i in (b, c, d)] | |
| return (p * torch.linalg.cross(q, r)).sum(-1) | |
| def neighbors(chem, x, valid, *, within_unit=False, cutoff=12., caps=(32, 16), chunk=128): | |
| n = len(x) | |
| bonds = torch.cat((chem.bonds, chem.bonds.flip(1))) | |
| orders = chem.order.repeat(2) | |
| bonds_ok = valid[bonds].all(dim=1) | |
| dst, src = bonds[bonds_ok].T | |
| bond_dst, bond_src = dst, src | |
| target, source = [dst], [src] | |
| relation, order = [torch.zeros_like(dst)], [orders[bonds_ok]] | |
| atoms = torch.arange(n, device=x.device) | |
| mol = chem.molecule[chem.unit] | |
| for rows in atoms.split(chunk): | |
| distances = torch.cdist(x[rows], x, compute_mode="donot_use_mm_for_euclid_dist") | |
| allowed = valid[rows, None] & valid[None, :] & (rows[:, None] != atoms) | |
| local = (bond_dst >= rows[0]) & (bond_dst <= rows[-1]) | |
| allowed[bond_dst[local] - rows[0], bond_src[local]] = False | |
| if within_unit: | |
| internal = allowed & (chem.unit[rows, None] == chem.unit) | |
| i, j = internal.nonzero(as_tuple=True) | |
| target.append(rows[i]); source.append(j) | |
| relation.append(torch.ones_like(j)); order.append(torch.zeros_like(j)) | |
| allowed = allowed & ~internal | |
| same = mol[rows, None] == mol | |
| for code, selection, cap in ((1, same, caps[0]), (2, ~same, caps[1])): | |
| eligible = allowed & selection & (distances <= cutoff) | |
| ds, js = distances.masked_fill(~eligible, torch.inf).topk(min(cap, n), largest=False) | |
| i, k = ds.isfinite().nonzero(as_tuple=True) | |
| j = js[i, k] | |
| target.append(rows[i]); source.append(j) | |
| relation.append(torch.full_like(j, code)); order.append(torch.zeros_like(j)) | |
| return tuple(torch.cat(parts) for parts in (target, source, relation, order)) | |
| class ChemicalEmbedding(nn.Module): | |
| def __init__(self, width): | |
| super().__init__() | |
| self.tables = nn.ModuleList(nn.Embedding(n, width) for n in FEATURE_SIZES) | |
| def forward(self, features): | |
| return sum(table(features[:, i]) for i, table in enumerate(self.tables)) | |
| class AtomBlock(nn.Module): | |
| def __init__(self, width, vectors): | |
| super().__init__() | |
| self.message = nn.Sequential(nn.Linear(2*width + 3*vectors + 25, width), | |
| nn.SiLU(), nn.Linear(width, width + 2*vectors)) | |
| self.mix = nn.Linear(vectors, vectors, bias=False) | |
| self.scalar_norm = nn.LayerNorm(width) | |
| self.vector_gate = nn.Linear(width + vectors, vectors) | |
| self.width, self.vectors = width, vectors | |
| def forward(self, h, v, x, edges, chem): | |
| dst, src, relation, order = edges | |
| delta = x[src] - x[dst] | |
| distance = delta.norm(dim=-1) | |
| direction = F.normalize(delta, dim=-1, eps=1e-7) | |
| aligned = direction.unsqueeze(1) | |
| same_chain = (chem.chain[chem.unit[dst]] == chem.chain[chem.unit[src]]).to(x.dtype) | |
| inputs = torch.cat((h[dst], h[src], (v[dst]*aligned).sum(-1), (v[src]*aligned).sum(-1), | |
| (v[dst]*v[src]).sum(-1), radial(distance), | |
| F.one_hot(relation, 3), F.one_hot(order, 5), same_chain[:, None]), -1) | |
| scalar, g_r, g_v = self.message(inputs).split((self.width, self.vectors, self.vectors), -1) | |
| mixed = self.mix(v[src].transpose(1, 2)).transpose(1, 2) | |
| vector = g_r[..., None] * aligned + g_v[..., None] * mixed | |
| h = self.scalar_norm(h + reduce_sum(scalar, dst, len(h))) | |
| v = v + reduce_sum(vector, dst, len(v)) | |
| norm = v.norm(dim=-1) | |
| v = v / (1. + norm[..., None]) * self.vector_gate(torch.cat((h, norm), -1)).sigmoid()[..., None] | |
| return h, v | |