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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""E1: does a NON-redundant physics channel help on an engine-independent,
|
| 2 |
+
physics-causal task?
|
| 3 |
+
|
| 4 |
+
Background (E2 finding): the original 2-channel physics signal (x_liquid,
|
| 5 |
+
log10 OAV) is largely REDUNDANT with the model's molecular-weight input
|
| 6 |
+
(volatility ~76% MW-recoverable), which predicts the frozen-task ablation null.
|
| 7 |
+
The genuinely non-redundant physics is the UNIFAC blend-interaction term
|
| 8 |
+
(gamma), which depends on the whole mixture and is not in any single-molecule
|
| 9 |
+
embedding.
|
| 10 |
+
|
| 11 |
+
This job trains two arms on the gamma-augmented corpus and evaluates the
|
| 12 |
+
substantivity head against MEASURED Poucher evaporation-duration coefficients
|
| 13 |
+
(engine-independent, physics-causal) on a molecule-disjoint holdout:
|
| 14 |
+
|
| 15 |
+
arm physics_off : state_dim=2, FiLM receives no gamma channel (baseline)
|
| 16 |
+
arm physics_gamma : state_dim=3, FiLM receives the gamma interaction channel
|
| 17 |
+
|
| 18 |
+
Metric: substantivity masked-MSE + Pearson/Spearman vs measured Poucher on the
|
| 19 |
+
held-out molecules. A real physics contribution shows as physics_gamma beating
|
| 20 |
+
physics_off on this engine-independent target.
|
| 21 |
+
|
| 22 |
+
Self-chains via PINO_SKIP_DONE to survive the ~90 min t4 wall-clock.
|
| 23 |
+
"""
|
| 24 |
+
from __future__ import annotations
|
| 25 |
+
|
| 26 |
+
import json
|
| 27 |
+
import os
|
| 28 |
+
import shutil
|
| 29 |
+
import subprocess
|
| 30 |
+
import sys
|
| 31 |
+
import time
|
| 32 |
+
from pathlib import Path
|
| 33 |
+
|
| 34 |
+
TOKEN = os.environ.get("HF_TOKEN")
|
| 35 |
+
WORKSPACE = Path("/workspace")
|
| 36 |
+
SOURCE_DIR = WORKSPACE / "pino-fresh"
|
| 37 |
+
DATASET_DIR = WORKSPACE / "pino-dataset"
|
| 38 |
+
OUT_DIR = WORKSPACE / "e1_out"
|
| 39 |
+
|
| 40 |
+
SOURCE_REPO = os.environ.get("PINO_SOURCE_REPO", "mattbitzesty/pino-source-code")
|
| 41 |
+
DATASET_REPO = os.environ.get("PINO_DATASET_REPO", "mattbitzesty/pino-synthetic-dataset-v3")
|
| 42 |
+
E1_REPO = os.environ.get("PINO_E1_REPO", "mattbitzesty/pino-pimt-physics-gamma-e1")
|
| 43 |
+
|
| 44 |
+
EPOCHS = int(os.environ.get("PINO_EPOCHS", "20"))
|
| 45 |
+
BATCH = int(os.environ.get("PINO_BATCH", "32"))
|
| 46 |
+
LR = os.environ.get("PINO_LR", "3e-4")
|
| 47 |
+
TRAIN_RATIO = os.environ.get("PINO_TRAIN_RATIO", "0.85")
|
| 48 |
+
SEEDS = [int(s) for s in os.environ.get("PINO_SEEDS", "42,123,2026").split(",")]
|
| 49 |
+
DATASET_NAME = "empirical_dataset_v9_gamma.jsonl"
|
| 50 |
+
|
| 51 |
+
# (arm_label, structural_source, use_gamma)
|
| 52 |
+
ARMS = (
|
| 53 |
+
("physics_off", "morgan", False),
|
| 54 |
+
("physics_gamma", "morgan", True),
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def log(msg: str) -> None:
|
| 59 |
+
print(f"{time.strftime('%Y-%m-%dT%H:%M:%SZ')} {msg}", flush=True)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def run(cmd: list[str]) -> None:
|
| 63 |
+
log(f"$ {' '.join(cmd)}")
|
| 64 |
+
subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def main() -> int:
|
| 68 |
+
run([sys.executable, "-m", "pip", "install", "--quiet",
|
| 69 |
+
"huggingface-hub", "scipy", "tensorboard", "rdkit", "numpy"])
|
| 70 |
+
from huggingface_hub import HfApi, snapshot_download
|
| 71 |
+
|
| 72 |
+
SOURCE_DIR.mkdir(parents=True, exist_ok=True)
|
| 73 |
+
DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
| 74 |
+
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 75 |
+
|
| 76 |
+
log(f"[E1] downloading source {SOURCE_REPO}")
|
| 77 |
+
snapshot_download(repo_id=SOURCE_REPO, repo_type="model", local_dir=str(SOURCE_DIR), token=TOKEN)
|
| 78 |
+
log(f"[E1] downloading dataset {DATASET_REPO}")
|
| 79 |
+
snapshot_download(repo_id=DATASET_REPO, repo_type="dataset", local_dir=str(DATASET_DIR), token=TOKEN)
|
| 80 |
+
|
| 81 |
+
reg_src = DATASET_DIR / "registry.db"
|
| 82 |
+
if reg_src.exists():
|
| 83 |
+
shutil.copy2(reg_src, WORKSPACE / "registry.db")
|
| 84 |
+
os.environ["PINO_REGISTRY_PATH"] = str(WORKSPACE / "registry.db")
|
| 85 |
+
ss = DATASET_DIR / "scalar_stats_v1.json"
|
| 86 |
+
if ss.exists():
|
| 87 |
+
(SOURCE_DIR / "artifacts").mkdir(exist_ok=True)
|
| 88 |
+
shutil.copy2(ss, SOURCE_DIR / "artifacts" / "scalar_stats_v1.json")
|
| 89 |
+
|
| 90 |
+
run([sys.executable, "-m", "pip", "install", "--quiet", "-e", str(SOURCE_DIR)])
|
| 91 |
+
sys.path.insert(0, str(SOURCE_DIR / "src"))
|
| 92 |
+
|
| 93 |
+
import torch # noqa: E402
|
| 94 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 95 |
+
log(f"[E1] device={device} cuda_devices={torch.cuda.device_count()}")
|
| 96 |
+
|
| 97 |
+
data_path = str(DATASET_DIR / DATASET_NAME)
|
| 98 |
+
api = HfApi(token=TOKEN)
|
| 99 |
+
try:
|
| 100 |
+
api.create_repo(E1_REPO, repo_type="model", exist_ok=True, private=False)
|
| 101 |
+
except Exception as e:
|
| 102 |
+
log(f"[E1] warn: create_repo ({e})")
|
| 103 |
+
|
| 104 |
+
skip_done = os.environ.get("PINO_SKIP_DONE", "1") == "1"
|
| 105 |
+
have = set()
|
| 106 |
+
if skip_done:
|
| 107 |
+
try:
|
| 108 |
+
have = set(api.list_repo_files(E1_REPO, repo_type="model"))
|
| 109 |
+
except Exception as e:
|
| 110 |
+
log(f"[E1] warn: could not list E1 repo ({e})")
|
| 111 |
+
|
| 112 |
+
results: dict = {}
|
| 113 |
+
for seed in SEEDS:
|
| 114 |
+
for arm_label, structural_source, use_gamma in ARMS:
|
| 115 |
+
tag = f"{arm_label}_s{seed}"
|
| 116 |
+
ckpt_name = f"pimt_e1_{tag}.pt"
|
| 117 |
+
eval_name = f"substantivity_eval_{tag}.json"
|
| 118 |
+
if skip_done and eval_name in have:
|
| 119 |
+
log(f"[E1] skipping {tag} (already uploaded)")
|
| 120 |
+
continue
|
| 121 |
+
logdir = OUT_DIR / "runs" / tag
|
| 122 |
+
log(f"[E1] ===== training {tag} (gamma={use_gamma}, {EPOCHS} epochs, seed {seed}) =====")
|
| 123 |
+
cmd = [sys.executable, "-m", "pino.train",
|
| 124 |
+
"--data", data_path,
|
| 125 |
+
"--structural-source", structural_source,
|
| 126 |
+
"--epochs", str(EPOCHS),
|
| 127 |
+
"--batch-size", str(BATCH),
|
| 128 |
+
"--lr", LR,
|
| 129 |
+
"--seed", str(seed),
|
| 130 |
+
"--train-ratio", str(TRAIN_RATIO),
|
| 131 |
+
"--split-strategy", "formula",
|
| 132 |
+
"--output-dir", str(OUT_DIR),
|
| 133 |
+
"--checkpoint-name", ckpt_name,
|
| 134 |
+
"--log-dir", str(logdir)]
|
| 135 |
+
if use_gamma:
|
| 136 |
+
cmd.append("--use-gamma")
|
| 137 |
+
run(cmd)
|
| 138 |
+
ckpt = OUT_DIR / ckpt_name
|
| 139 |
+
log(f"[E1] ===== substantivity eval {tag} =====")
|
| 140 |
+
eval_out = OUT_DIR / eval_name
|
| 141 |
+
run([sys.executable, str(SOURCE_DIR / "scripts" / "evaluate_substantivity.py"),
|
| 142 |
+
"--checkpoint", str(ckpt),
|
| 143 |
+
"--data", data_path,
|
| 144 |
+
"--structural-source", structural_source,
|
| 145 |
+
"--train-ratio", str(TRAIN_RATIO),
|
| 146 |
+
"--seed", str(seed),
|
| 147 |
+
"--split", "formula",
|
| 148 |
+
"--arm-label", tag,
|
| 149 |
+
"--output", str(eval_out)] + (["--use-gamma"] if use_gamma else []))
|
| 150 |
+
api.upload_file(path_or_fileobj=str(ckpt), path_in_repo=ckpt_name,
|
| 151 |
+
repo_id=E1_REPO, repo_type="model")
|
| 152 |
+
api.upload_file(path_or_fileobj=str(eval_out), path_in_repo=eval_name,
|
| 153 |
+
repo_id=E1_REPO, repo_type="model")
|
| 154 |
+
log(f"[E1] uploaded {tag} checkpoint + substantivity eval")
|
| 155 |
+
results[tag] = {"checkpoint": ckpt_name, "eval": eval_name, "use_gamma": use_gamma, "seed": seed}
|
| 156 |
+
|
| 157 |
+
summary = {"experiment": "E1 physics-gamma on measured Poucher substantivity",
|
| 158 |
+
"epochs": EPOCHS, "batch": BATCH, "lr": LR, "seeds": SEEDS,
|
| 159 |
+
"train_ratio": TRAIN_RATIO, "device": device, "dataset": DATASET_NAME,
|
| 160 |
+
"rationale": "E2 showed x_liq/logOAV physics redundant w/ MW; gamma is the "
|
| 161 |
+
"non-redundant blend-interaction channel; measured Poucher is "
|
| 162 |
+
"engine-independent + physics-causal",
|
| 163 |
+
"arms": results}
|
| 164 |
+
api.upload_file(path_or_fileobj=json.dumps(summary, indent=2).encode(),
|
| 165 |
+
path_in_repo="e1_run_summary.json", repo_id=E1_REPO, repo_type="model")
|
| 166 |
+
log("[E1] DONE")
|
| 167 |
+
return 0
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
try:
|
| 172 |
+
raise SystemExit(main())
|
| 173 |
+
except Exception as exc: # noqa: BLE001
|
| 174 |
+
import traceback
|
| 175 |
+
traceback.print_exc()
|
| 176 |
+
print(f"[E1] FAILED: {exc}", flush=True)
|
| 177 |
+
raise SystemExit(1)
|