File size: 4,327 Bytes
4c231da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
"""One-assembly flow matching and auxiliary chemistry objectives."""
import torch
from torch.nn import functional as F

from .geometry import signed_volumes


def center(x, mask):
    return x - x[mask].mean(0)


def aligned_endpoint(x, noise, mask):
    clean = center(torch.where(mask[:, None], x, 0.), mask) / 10.
    reference_noise = center(noise, mask)
    with torch.no_grad():
        left, _, right = torch.linalg.svd(clean[mask].T @ reference_noise[mask])
        parity = torch.linalg.det(left @ right).sign()
        left[:, -1] *= parity
        rotation = left @ right
    return torch.where(mask[:, None], clean @ rotation + noise[mask].mean(0), noise)


def mean_square(residual):
    return residual.square().sum() / max(residual.numel(), 1)


def angular_cosine(x, angles):
    a, b, c = angles.T
    return (F.normalize(x[b]-x[a]) * F.normalize(x[c]-x[a])).sum(-1)


def evaluation_pairs(chem, mask, budget=2048):
    """Bound memory for long assemblies while including intra- and inter-molecular pairs."""
    idx = mask.nonzero().flatten()
    if len(idx)**2 <= 2*budget:
        return torch.combinations(idx, r=2)
    pairs = idx[torch.randint(len(idx), (budget*4, 2), device=idx.device)]
    pairs = pairs[pairs[:, 0] != pairs[:, 1]]
    mol = chem.molecule[chem.unit[pairs]]
    cross = mol[:, 0] != mol[:, 1]
    intra, inter = pairs[~cross], pairs[cross]
    n_inter = min(len(inter), max(budget//2, budget-len(intra)))
    return torch.cat((inter[:n_inter], intra[:budget-n_inter]))


def tokenizer_loss(model, chem, xyz, mask, *, weights=None):
    """Returns total and component losses. Coordinates and length penalties use angstroms."""
    lambdas = dict(bond=.1, angle=.1, clash=.05, chiral=.1, distance=.05, usage=.001)
    if weights:
        if set(weights) - lambdas.keys():
            raise ValueError("Unknown auxiliary loss weight.")
        lambdas.update(weights)
    if model.training:
        rotation, triangular = torch.linalg.qr(torch.randn(3, 3, device=xyz.device, dtype=xyz.dtype))
        rotation = rotation * torch.diagonal(triangular).sign()
        rotation[:, -1] *= torch.linalg.det(rotation).sign()
        xyz = xyz @ rotation
    _, code, raw, present = model(chem, xyz, mask)
    noise = torch.randn_like(xyz)
    noise -= noise.mean(0)
    target = aligned_endpoint(xyz, noise, mask)
    t = torch.rand((), device=xyz.device, dtype=xyz.dtype)
    state = torch.lerp(noise, target, t)
    # Give an entirely unobserved unit an ordinary random code for this training example.
    # The decoder API only receives chemistry, code vectors, time, and the noisy state.
    filler = model.fsq.from_ids(torch.randint(8192, (chem.units,), device=xyz.device))
    velocity = model.decoder(chem, torch.where(present[:, None], code, filler), state, t)
    losses = {"flow": ((velocity - (target-noise))[mask]).square().sum(-1).mean()}
    pred = (state + (1.-t)*velocity) * 10.
    bonds = chem.bonds[mask[chem.bonds].all(1)]
    angles = chem.angles[mask[chem.angles].all(1)]
    triplets = chem.triplets[mask[chem.triplets].all(1)]
    def lengths(x, pairs):
        return (x[pairs[:, 0]]-x[pairs[:, 1]]).norm(dim=-1)
    losses["bond"] = mean_square(lengths(pred, bonds)-lengths(xyz, bonds))
    losses["angle"] = mean_square(angular_cosine(pred, angles)-angular_cosine(xyz, angles))
    losses["chiral"] = mean_square(signed_volumes(pred, triplets)-signed_volumes(xyz, triplets))
    pairs = evaluation_pairs(chem, mask)
    losses["distance"] = mean_square((lengths(pred, pairs)-lengths(xyz, pairs))/10.)
    def keys(pairs):
        p, q = pairs.sort(dim=-1).values.T
        return p*len(xyz)+q
    exclusions = torch.cat((chem.bonds, chem.angles[:, 1:]))
    distant = pairs[~torch.isin(keys(pairs), keys(exclusions))]
    losses["clash"] = mean_square(F.relu(2.-lengths(pred, distant)))
    usage = xyz.new_zeros(())
    for i, n in enumerate((8, 8, 8, 4, 4)):
        grid = torch.arange(n, device=xyz.device)
        p = (-(raw[present, i, None]-grid).square()/.5).softmax(-1).mean(0)
        usage = usage + (p*(p*n).clamp_min(1e-8).log()).sum()/5
    losses["usage"] = usage
    total = losses["flow"] + lambdas["usage"]*usage
    total = total + t.square()*sum(lambdas[k]*losses[k] for k in ("bond","angle","clash","chiral","distance"))
    return total, losses