"""The two specialists on one GPU: the Free Fall model predicts drops, the Dragging model drags (the live demo's engine, pwm-live-drop livedrop/engine.py). One call = one 16-frame window from the last 4 frames, exactly the protocol's windowed rollout, with the demo's sampler settings (config.STEPS, bf16, full_sequence). The library comes from the model repository's pwm_infer package: call pwm_infer.install() before building an Engine. The model input is built by the library's own predict.batch_from_scene, from a scene dict in predict.py's format. """ from __future__ import annotations import time import numpy as np import torch from demo import config class Model: def __init__(self, name, weights, device): from data.normalization import resolve_normalization from data.recording_adapters import normalizers from inference import load_model_and_diffusion self.name, self.device = name, device self.domain = {"freefall": "mpm_fresh", "dragging": "mpm_drag"}[name] self.model, self.diffusion, self.cfg, self.own_steps = load_model_and_diffusion(str(weights), device) self.model.eval() self.C, self.F = int(self.cfg.data.context_len), int(self.cfg.data.future_len) assert (self.C, self.F) == (config.CONTEXT, config.WINDOW_FRAMES), (self.C, self.F) self.uscale = float(self.cfg.get("df_uncertainty_scale", 1.0)) self.stats = normalizers(self.domain, resolve_normalization(self.cfg.normalization))[:2] class Engine: def __init__(self, weights, device="cuda", steps=config.STEPS, precision=config.PRECISION): """weights: {"freefall": path, "dragging": path}.""" self.device = torch.device(device) self.models = {name: Model(name, weights[name], self.device) for name in ("freefall", "dragging")} self.steps, self.precision = int(steps), precision def gpu(self): return torch.cuda.get_device_name(self.device) if self.device.type == "cuda" and torch.cuda.is_available() else "cpu" def window(self, scene, model="freefall", seed=0, frames=None): """One window: scene (predict.py format; positions = the C given frames) -> (frames [F, N, 3] float32, ms).""" from evaluation.evaluator import sampler_precision from inference import windowed_rollout from predict import batch_from_scene, check_drag from utils.reproducibility import seed_for_device m = self.models[model] horizon = int(frames or m.F) if "dragged" in scene: # the library's own checks and per-particle expansion of a drag scene = check_drag(dict(scene)) t = time.perf_counter() batch = batch_from_scene(scene, m.C, horizon, m.stats, m.domain).to(self.device) seed_for_device(int(seed), self.device) with sampler_precision(self.precision, self.device): out = windowed_rollout(m.model, m.diffusion, batch, horizon=horizon, future_len=m.F, num_inference_steps=self.steps, schedule="full_sequence", uncertainty_scale=m.uscale) frames = out["positions"][0, m.C:].float().cpu().numpy() return frames, (time.perf_counter() - t) * 1000.0