File size: 12,182 Bytes
3e3ee14
 
 
 
 
 
 
 
 
 
 
 
 
0b0dafb
3e3ee14
 
 
 
 
baf571e
 
3e3ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1adadbe
3e3ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b0dafb
 
 
3e3ee14
 
6e09236
3e3ee14
 
 
 
0b0dafb
 
3e3ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b0dafb
 
 
 
 
 
3e3ee14
 
0b0dafb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e3ee14
 
0b0dafb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e3ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b0dafb
 
 
 
 
 
 
3e3ee14
 
 
 
 
 
0b0dafb
 
3e3ee14
0b0dafb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""Dataset-free inference for the public Morph playground."""

from __future__ import annotations

import csv
import hashlib
import json
import math
import os
from pathlib import Path
import random
import sys
import tempfile
import zipfile

ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT / "runtime"))
os.environ.setdefault("MORPH_COMPILE", "0")
os.environ.setdefault("WANDB_MODE", "disabled")
os.environ.setdefault("MORPH_FUSED_KERNELS", "0")
os.environ.setdefault("PYTORCH_NVML_BASED_CUDA_CHECK", "1")

import hydra
import numpy as np
import torch
from huggingface_hub import hf_hub_download
from omegaconf import OmegaConf
from PIL import Image, ImageDraw
from rdkit import Chem, RDLogger
from rdkit.Chem import Crippen, Draw, rdDepictor

import morph.config  # noqa: F401 -- registers the saved configuration resolvers
from morph.dataset.core.atomic_data import chembatch_from_graphs
from morph.dataset.core.flow_matching_wrapper import uniform_n_atoms_range
from morph.flow_matching.prior import sample_prior_graph
from morph.inference.module import InferenceModule, load_inference_weights
from morph.model.build import prepare_model_data
from morph.utils.rdkit_utils import mol_is_valid

REGISTRY = json.loads((ROOT / "models.json").read_text())
MODELS = {}
RDLogger.DisableLog("rdApp.*")
torch.set_num_threads(2)


def load_models(device="cuda", keys=None):
    """Preload weights before ZeroGPU forks its inference worker."""
    local = json.loads(os.environ.get("MORPH_LOCAL_CHECKPOINTS", "{}"))
    for key in keys or REGISTRY:
        spec = REGISTRY[key]
        assets = ROOT / "assets" / key
        cfg = OmegaConf.load(assets / "config.yaml")
        OmegaConf.set_struct(cfg, False)
        cfg.data.preprocessing.root = str(assets)
        cfg.data.preprocessing.train_dataset = None
        cfg.data.preprocessing.num_workers = 0
        for name in ("atom_tokens", "edge_tokens", "charge_tokens", "distributions"):
            suffix = ".pt" if name == "distributions" else ".txt"
            cfg.data.preprocessing[name + "_path"] = str(assets / (name + suffix))
        preprocessing = hydra.utils.instantiate(cfg.data.preprocessing)
        data = prepare_model_data(cfg, preprocessing.vocab, preprocessing.distributions)
        cfg.model.module.integrator.device = device
        module = InferenceModule(cfg, data)
        module.integrator.num_integration_steps = 100
        if spec.get("max_atoms"):
            module.integrator.max_atoms = spec["max_atoms"]
        checkpoint = local.get(key) or hf_hub_download(
            "maltefranke/morph", spec["checkpoint"], revision=spec["revision"]
        )
        with open(checkpoint, "rb") as handle:
            digest = hashlib.file_digest(handle, "sha256").hexdigest()
        if digest != spec["sha256"]:
            raise RuntimeError(f"Checkpoint integrity check failed: {key}")
        load_inference_weights(module.model, checkpoint, use_ema=True)
        prior = spec.get("prior") or uniform_n_atoms_range(data.empirical)
        MODELS[key] = (module.eval().to(device), data, tuple(prior))
        print(f"Loaded {key}: EMA, initial count {prior}, {device}", flush=True)


def validate_request(key, target_logp, target_n, count, seed):
    """Validate API inputs independently of the browser controls."""
    if key not in REGISTRY:
        raise ValueError("Choose a listed model.")
    if int(count) != count or not 1 <= count <= 8:
        raise ValueError("Generate between 1 and 8 molecules at a time.")
    if int(seed) != seed or not 0 <= seed <= 2147483647:
        raise ValueError("Seed must be an integer from 0 to 2147483647.")
    kind = REGISTRY[key]["kind"]
    if kind == "logp" and (
        not math.isfinite(target_logp) or not -8 <= target_logp <= 9
    ):
        raise ValueError("Choose a logP target between -8 and 9.")
    if kind == "count" and (int(target_n) != target_n or not 3 <= target_n <= 120):
        raise ValueError("Choose an atom-count target between 3 and 120.")


def generate_graphs(
    key, target_logp, target_n, count, seed, *, trajectory=False, require_gpu=False
):
    """Run the same 100-step EMA sampler used for evaluation."""
    validate_request(key, target_logp, target_n, count, seed)
    torch.set_float32_matmul_precision("high")
    random.seed(int(seed))
    np.random.seed(int(seed))
    torch.manual_seed(int(seed))
    module, data, prior = MODELS[key]
    if require_gpu and module.device.type != "cuda":
        raise RuntimeError("This endpoint requires real CUDA inference.")
    kind = REGISTRY[key]["kind"]
    graphs = []
    for n in torch.randint(prior[0], prior[1] + 1, (int(count),)):
        graph = sample_prior_graph(data.prior, n_atoms=int(n))
        if kind == "logp":
            graph.add_system_property("target_logp", torch.tensor([float(target_logp)]))
        elif kind == "count":
            graph.add_system_property("target_n_atoms", torch.tensor([int(target_n)]))
        graphs.append(graph)
    batch = chembatch_from_graphs(graphs).to_inplace(module.device)
    with (
        torch.inference_mode(),
        torch.autocast(
            module.device.type,
            dtype=torch.bfloat16,
            enabled=module.device.type == "cuda",
        ),
    ):
        generated = module.sample((batch, batch), 0, return_traj=trajectory)
    if trajectory:
        outputs = [frames[-1] for frames in generated]
    else:
        generated.to_inplace("cpu", non_blocking=False)
        outputs = generated.to_data_list()
    assert len(outputs) == int(count)
    assert all(torch.isfinite(graph.positions).all() for graph in outputs)
    if not trajectory:
        return outputs
    times = [0.0, *np.cumsum(module.integrator.get_time_steps()).tolist()]
    assert all(len(frames) == len(times) == 101 for frames in generated)
    runtime = dict(
        device=str(module.device),
        gpu=torch.cuda.get_device_name() if module.device.type == "cuda" else None,
        torch=torch.__version__,
        precision="bfloat16" if module.device.type == "cuda" else "float32",
        model=key,
        checkpoint_sha256=REGISTRY[key]["sha256"],
        initial_count_prior=list(prior),
        integration_steps=100,
        frames_per_molecule=101,
        coordinate_units="angstrom",
        time_grid="log; final integration boundary 0.999",
    )
    return dict(graphs=outputs, trajectories=generated, times=times, runtime=runtime)


def frame_atoms(graph, vocab):
    """Export raw graph geometry, including chemically invalid intermediate states."""
    positions = graph.positions.tolist()
    atom_types = graph.atom_types.tolist()
    charges = graph.charge_types.tolist()
    atoms = [
        dict(
            elem=vocab.atom_tokens[a],
            x=round(p[0], 5),
            y=round(p[1], 5),
            z=round(p[2], 5),
            charge=int(vocab.charge_tokens[c]),
            bonds=[],
            bondOrder=[],
        )
        for a, c, p in zip(atom_types, charges, positions, strict=True)
    ]
    edge_types, edge_index = graph.get_edge_types_triu(
        no_bond_idx=list(vocab.edge_tokens).index("<NO_BOND>")
    )
    for edge, (a, b) in zip(edge_types.tolist(), edge_index.T.tolist(), strict=True):
        token = vocab.edge_tokens[edge]
        if token == "<NO_BOND>":
            continue
        order = 1.5 if str(token) == "4" else float(token)
        atoms[a]["bonds"].append(b)
        atoms[a]["bondOrder"].append(order)
        atoms[b]["bonds"].append(a)
        atoms[b]["bondOrder"].append(order)
    return atoms


def present(
    graphs,
    key,
    target_logp,
    target_n,
    seed,
    *,
    trajectories=None,
    times=None,
    runtime=None,
):
    """Keep every attempt in the table; export sanitized structures to SDF."""
    _, data, _ = MODELS[key]
    vocab = data.vocab
    directory = Path(tempfile.mkdtemp(prefix="morph-"))
    sdf_path, csv_path = directory / "molecules.sdf", directory / "results.csv"
    writer = Chem.SDWriter(str(sdf_path))
    gallery, rows, records = [], [], []
    valid_count = 0
    kind = REGISTRY[key]["kind"]
    for i, graph in enumerate(graphs, 1):
        mol = graph.to_rdkit_mol(
            vocab.atom_tokens, vocab.edge_tokens, vocab.charge_tokens
        )
        valid = mol is not None and mol_is_valid(mol, allow_charged=data.allow_charged)
        valid_count += int(valid)
        logp = float(Crippen.MolLogP(mol)) if mol is not None else None
        smiles = Chem.MolToSmiles(Chem.RemoveHs(mol)) if mol is not None else ""
        row = dict(
            sample=i,
            valid=bool(valid),
            atoms=int(graph.num_nodes),
            logP=round(logp, 4) if logp is not None else None,
            target_logP=float(target_logp) if kind == "logp" else None,
            target_N=int(target_n) if kind == "count" else None,
            seed=int(seed),
            model=key,
            smiles=smiles,
        )
        rows.append(row)
        if mol is not None:
            mol.SetProp("_Name", f"Morph sample {i}")
            for name, value in row.items():
                if value is not None:
                    mol.SetProp(name, str(value))
            writer.write(mol)
            block = Chem.MolToMolBlock(mol)
            display = Chem.RemoveHs(Chem.Mol(mol))
            rdDepictor.Compute2DCoords(display)
            thumbnail = Draw.MolToImage(display, size=(440, 320))
        else:
            thumbnail = Image.new("RGB", (440, 320), "#f4f6f8")
            ImageDraw.Draw(thumbnail).text(
                (125, 150), "Could not sanitize this sample", fill="#566477"
            )
        caption = f"{i} 路 N={graph.num_nodes}"
        if logp is not None:
            caption += f" 路 logP={logp:.2f}"
        caption += " 路 valid" if valid else " 路 invalid"
        gallery.append((thumbnail, caption))
        record = dict(index=i, molblock=block if mol is not None else None)
        if trajectories is not None:
            record["frames"] = [
                dict(time=float(t), atoms=frame_atoms(frame, vocab))
                for t, frame in zip(times, trajectories[i - 1], strict=True)
            ]
        records.append(record)
    writer.close()
    with csv_path.open("w", newline="") as handle:
        csv_writer = csv.DictWriter(handle, fieldnames=rows[0])
        csv_writer.writeheader()
        csv_writer.writerows(rows)
    status = f"**{valid_count} / {len(graphs)} chemically valid** 路 Seed {int(seed)}"
    if runtime is not None and runtime["device"].startswith("cuda"):
        status += " 路 GPU"
    status += "\n\nAll attempts are shown. SDF includes sanitized structures; CSV includes every attempt."
    if trajectories is None:
        return gallery, rows, records, str(sdf_path), str(csv_path), status
    archive = directory / "trajectories.zip"
    with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED) as zip_file:
        zip_file.writestr(
            "generation.json",
            json.dumps(
                dict(
                    **runtime,
                    seed=int(seed),
                    target_logp=float(target_logp) if kind == "logp" else None,
                    target_n=int(target_n) if kind == "count" else None,
                ),
                indent=2,
            ),
        )
        for record in records:
            zip_file.writestr(
                f"molecule_{record['index']}.json", json.dumps(record["frames"])
            )
            xyz = []
            for frame in record["frames"]:
                xyz.extend(
                    [
                        str(len(frame["atoms"])),
                        f"t={frame['time']:.9f}; coordinates in angstrom",
                    ]
                )
                xyz.extend(
                    f"{a['elem']} {a['x']:.5f} {a['y']:.5f} {a['z']:.5f}"
                    for a in frame["atoms"]
                )
            zip_file.writestr(f"molecule_{record['index']}.xyz", "\n".join(xyz) + "\n")
    return (
        gallery,
        rows,
        records,
        str(sdf_path),
        str(csv_path),
        status,
        str(archive),
        runtime,
    )