world-v2 / example_inference.py
AdagioBiology's picture
MolJEPA-World v2: multi-horizon displacement heads (11-21% better than the v1 Koopman operator, 100% win rate on 23 held-out domains)
5e2a55e verified
Raw History Blame Contribute Delete
2.42 kB
"""MolJEPA-World v2 -- minimal inference demo.
Run from this folder (the model loads itself from the local files):
pip install torch numpy safetensors huggingface_hub
python example_inference.py
"""
import os
import numpy as np
import torch
from modeling_moljepa import load_pretrained, prepare_coords, advance
HERE = os.path.dirname(os.path.abspath(__file__))
demo = np.load(os.path.join(HERE, "examples", "demo_1a0hA01.npz"))
device = "cuda" if torch.cuda.is_available() else "cpu"
model = load_pretrained() # local folder; or "AdagioBiology/world-v2"
x = prepare_coords(torch.from_numpy(demo["coords_angstrom"])) # (T,N,3)
atom_types = torch.from_numpy(demo["atom_types"]).long() # (N,)
esm = torch.from_numpy(demo["esm2_emb"]).float() # (N,480)
temperature = torch.from_numpy(demo["temperature"]).float() # (T,)
mask = torch.ones(1, x.shape[1])
with torch.no_grad():
def encode(frame):
return model.encode(atom_types.unsqueeze(0).to(device),
x[frame:frame + 1].to(device),
atom_mask=mask.to(device),
esm_emb=esm.unsqueeze(0).to(device),
sim_temp=temperature[frame:frame + 1].to(device))
z0 = encode(0)
z_true = encode(20) # 20 steps x 275 ps = 5.5 ns ahead
for mode in ("direct", "rollout"): # rollout = free-running 1-step, drifts
z_pred = advance(model, z0, frames=20, mode=mode)
mse = float((z_pred - z_true).pow(2).mean())
per = float((z0 - z_true).pow(2).mean())
cos = float(torch.nn.functional.cosine_similarity(z_pred, z_true, dim=1))
print(" %-8s MSE %8.3f | vs persistence %5.2fx | cosine %.4f"
% (mode, mse, mse / per, cos))
tau = model.koop.timescales(dt_ps=model.config["frame_dt_ps"])
print("\nKoopman timescales of the v1 encoder (ns):",
np.round(tau[:6].detach().cpu().numpy() / 1000, 1))
# 3D decode of the rolled latent (distogram -> SMACOF). This path is the
# known weak link of the release; the latent forecast is the measured part.
coords = model.generate_3d(atom_types.unsqueeze(0).to(device),
x[0:1].to(device), z=z0, atom_mask=mask.to(device))
print("\ndecoded CA trace:", np.asarray(coords).shape,
"(multiply by model.coord_scale for angstrom)")