|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Benchmark the released SolvAI artifact without fitting or model selection.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import os |
| 9 | +import platform |
| 10 | +import statistics |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import tempfile |
| 14 | +import time |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import chemprop |
| 18 | +import joblib |
| 19 | +import lightgbm |
| 20 | +import numpy |
| 21 | +import pandas |
| 22 | +import pandas as pd |
| 23 | +import rdkit |
| 24 | +import sklearn |
| 25 | +import torch |
| 26 | + |
| 27 | +from solv_ai import predict_smiles |
| 28 | + |
| 29 | +ROOT = Path(__file__).resolve().parents[1] |
| 30 | + |
| 31 | + |
| 32 | +def percentile(values: list[float], percentile_value: float) -> float: |
| 33 | + ordered = sorted(values) |
| 34 | + position = (len(ordered) - 1) * percentile_value / 100.0 |
| 35 | + lower = int(position) |
| 36 | + upper = min(lower + 1, len(ordered) - 1) |
| 37 | + fraction = position - lower |
| 38 | + return ordered[lower] * (1 - fraction) + ordered[upper] * fraction |
| 39 | + |
| 40 | + |
| 41 | +def timed_prediction(smiles: list[str]) -> float: |
| 42 | + start = time.perf_counter() |
| 43 | + predict_smiles(smiles) |
| 44 | + return time.perf_counter() - start |
| 45 | + |
| 46 | + |
| 47 | +def cpu_model() -> str: |
| 48 | + for line in Path("/proc/cpuinfo").read_text().splitlines(): |
| 49 | + if line.startswith("model name"): |
| 50 | + return line.split(":", 1)[1].strip() |
| 51 | + return platform.processor() or "unknown" |
| 52 | + |
| 53 | + |
| 54 | +def gpu_model() -> str: |
| 55 | + try: |
| 56 | + return subprocess.check_output( |
| 57 | + ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], text=True |
| 58 | + ).strip() |
| 59 | + except (FileNotFoundError, subprocess.CalledProcessError): |
| 60 | + return "not used / unavailable" |
| 61 | + |
| 62 | + |
| 63 | +def measured_peak_rss_kib(smiles: str) -> int | None: |
| 64 | + executable = Path("/usr/bin/time") |
| 65 | + if not executable.is_file(): |
| 66 | + return None |
| 67 | + with tempfile.TemporaryDirectory(prefix="solvai_benchmark_") as directory: |
| 68 | + output = Path(directory) / "time.txt" |
| 69 | + subprocess.run( |
| 70 | + [ |
| 71 | + str(executable), |
| 72 | + "-v", |
| 73 | + "-o", |
| 74 | + str(output), |
| 75 | + sys.executable, |
| 76 | + "-m", |
| 77 | + "solv_ai.cli", |
| 78 | + smiles, |
| 79 | + ], |
| 80 | + cwd=ROOT, |
| 81 | + check=True, |
| 82 | + capture_output=True, |
| 83 | + text=True, |
| 84 | + ) |
| 85 | + for line in output.read_text().splitlines(): |
| 86 | + if "Maximum resident set size" in line: |
| 87 | + return int(line.rsplit(":", 1)[1].strip()) |
| 88 | + return None |
| 89 | + |
| 90 | + |
| 91 | +def main() -> None: |
| 92 | + parser = argparse.ArgumentParser() |
| 93 | + parser.add_argument("--warm-runs", type=int, default=5) |
| 94 | + parser.add_argument("--batch-runs", type=int, default=3) |
| 95 | + parser.add_argument("--batch-size", type=int, default=32) |
| 96 | + args = parser.parse_args() |
| 97 | + if args.warm_runs < 3 or args.batch_runs < 3: |
| 98 | + raise ValueError("Use at least three repeated measurements") |
| 99 | + |
| 100 | + benchmark = pd.read_parquet(ROOT / "data/benchmark/arrow_solvation_master.parquet") |
| 101 | + smiles = benchmark.loc[benchmark.solvent.eq("water"), "canonical_smiles"].tolist() |
| 102 | + batch = smiles[: args.batch_size] |
| 103 | + |
| 104 | + cold = timed_prediction(["CCO"]) |
| 105 | + warm = [timed_prediction(["CCO"]) for _ in range(args.warm_runs)] |
| 106 | + batch_times = [timed_prediction(batch) for _ in range(args.batch_runs)] |
| 107 | + batch_per_molecule = [value / len(batch) for value in batch_times] |
| 108 | + peak_rss = measured_peak_rss_kib("CCO") |
| 109 | + |
| 110 | + # ARROW Supplementary Table 2: 16–18 h per 1 ns PIMD8 window on one |
| 111 | + # RTX 2080 Ti + two CPU cores; the published hydration protocol uses 15 windows. |
| 112 | + published_total_hours = [15 * 16, 15 * 18] |
| 113 | + median_single_seconds = statistics.median(warm) |
| 114 | + median_batch_per_molecule_seconds = statistics.median(batch_per_molecule) |
| 115 | + result = { |
| 116 | + "schema_version": 1, |
| 117 | + "artifact": "models/final", |
| 118 | + "input": "SMILES", |
| 119 | + "hardware": { |
| 120 | + "cpu": cpu_model(), |
| 121 | + "logical_cpus": os.cpu_count(), |
| 122 | + "gpu_present_but_not_used": gpu_model(), |
| 123 | + "platform": platform.platform(), |
| 124 | + }, |
| 125 | + "software": { |
| 126 | + "python": platform.python_version(), |
| 127 | + "chemprop": chemprop.__version__, |
| 128 | + "torch": torch.__version__, |
| 129 | + "rdkit": rdkit.__version__, |
| 130 | + "numpy": numpy.__version__, |
| 131 | + "pandas": pandas.__version__, |
| 132 | + "scikit_learn": sklearn.__version__, |
| 133 | + "lightgbm": lightgbm.__version__, |
| 134 | + "joblib": joblib.__version__, |
| 135 | + }, |
| 136 | + "single_molecule": { |
| 137 | + "cold_seconds": cold, |
| 138 | + "warm_runs_seconds": warm, |
| 139 | + "warm_median_seconds": median_single_seconds, |
| 140 | + "warm_p95_seconds": percentile(warm, 95), |
| 141 | + "peak_rss_kib": peak_rss, |
| 142 | + }, |
| 143 | + "batch": { |
| 144 | + "size": len(batch), |
| 145 | + "runs_seconds": batch_times, |
| 146 | + "median_seconds": statistics.median(batch_times), |
| 147 | + "p95_seconds": percentile(batch_times, 95), |
| 148 | + "median_seconds_per_molecule": median_batch_per_molecule_seconds, |
| 149 | + "molecules_per_second": 1.0 / median_batch_per_molecule_seconds, |
| 150 | + }, |
| 151 | + "published_arrow_pimd8_reference": { |
| 152 | + "hardware": "NVIDIA GeForce RTX 2080 Ti + 2 CPU cores", |
| 153 | + "hours_per_1ns_window_range": [16, 18], |
| 154 | + "windows": 15, |
| 155 | + "total_serial_gpu_hours_range": published_total_hours, |
| 156 | + "ideal_parallel_wall_hours_range_on_15_gpus": [16, 18], |
| 157 | + "source": "Pereyaslavets et al., Nature Communications 13, 414 (2022), Supplementary Table 2", |
| 158 | + "doi": "10.1038/s41467-022-28041-0", |
| 159 | + }, |
| 160 | + "derived_comparison": { |
| 161 | + "single_warm_total_work_speedup_range": [ |
| 162 | + published_total_hours[0] * 3600 / median_single_seconds, |
| 163 | + published_total_hours[1] * 3600 / median_single_seconds, |
| 164 | + ], |
| 165 | + "batch_amortized_total_work_speedup_range": [ |
| 166 | + published_total_hours[0] * 3600 / median_batch_per_molecule_seconds, |
| 167 | + published_total_hours[1] * 3600 / median_batch_per_molecule_seconds, |
| 168 | + ], |
| 169 | + "caveat": ( |
| 170 | + "The numerator is a published RTX 2080 Ti simulation estimate and the " |
| 171 | + "denominator is measured CPU inference on the release host; this compares " |
| 172 | + "total per-molecule work, not same-hardware kernel throughput." |
| 173 | + ), |
| 174 | + }, |
| 175 | + } |
| 176 | + output = ROOT / "results/runtime/runtime_benchmark.json" |
| 177 | + output.write_text(json.dumps(result, indent=2) + "\n") |
| 178 | + print(json.dumps(result, indent=2)) |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == "__main__": |
| 182 | + main() |
0 commit comments