perf(train): avoid Hugging Face Dataset round-trip during formula-disjoint split
Browse files- src/pino/train.py +5 -6
- src/pino/upload_data.py +28 -14
src/pino/train.py
CHANGED
|
@@ -12,14 +12,14 @@ import torch
|
|
| 12 |
import torch.nn as nn
|
| 13 |
import torch.nn.functional as F
|
| 14 |
import torch.optim as optim
|
| 15 |
-
from datasets import
|
| 16 |
from torch.utils.data import DataLoader
|
| 17 |
from torch.utils.tensorboard import SummaryWriter
|
| 18 |
|
| 19 |
from pino.heads import PIMTHeads
|
| 20 |
from pino.pimt_model import FragranceTrajectoryDataset, PhysicsInformedMixtureTransformer
|
| 21 |
from pino.thermo.losses import AdaptiveLossBalancer
|
| 22 |
-
from pino.upload_data import
|
| 23 |
|
| 24 |
logger = logging.getLogger("pino.train")
|
| 25 |
|
|
@@ -281,12 +281,11 @@ def main():
|
|
| 281 |
print(f"📊 Extracted {len(control_records)} pure single-molecule anchors.")
|
| 282 |
|
| 283 |
# Apply formula-disjoint split only to mixture records.
|
| 284 |
-
|
| 285 |
-
mixture_split = create_formula_disjoint_split_from_dataset(mixture_dataset, train_ratio=0.8)
|
| 286 |
|
| 287 |
# Pin all control records to the training pool.
|
| 288 |
-
train_records =
|
| 289 |
-
val_records =
|
| 290 |
|
| 291 |
# Verify no identical formula fingerprint (ingredients + weights) appears in both splits.
|
| 292 |
def _formula_fingerprint(r):
|
|
|
|
| 12 |
import torch.nn as nn
|
| 13 |
import torch.nn.functional as F
|
| 14 |
import torch.optim as optim
|
| 15 |
+
from datasets import DatasetDict
|
| 16 |
from torch.utils.data import DataLoader
|
| 17 |
from torch.utils.tensorboard import SummaryWriter
|
| 18 |
|
| 19 |
from pino.heads import PIMTHeads
|
| 20 |
from pino.pimt_model import FragranceTrajectoryDataset, PhysicsInformedMixtureTransformer
|
| 21 |
from pino.thermo.losses import AdaptiveLossBalancer
|
| 22 |
+
from pino.upload_data import create_formula_disjoint_split_from_records
|
| 23 |
|
| 24 |
logger = logging.getLogger("pino.train")
|
| 25 |
|
|
|
|
| 281 |
print(f"📊 Extracted {len(control_records)} pure single-molecule anchors.")
|
| 282 |
|
| 283 |
# Apply formula-disjoint split only to mixture records.
|
| 284 |
+
mixture_train, mixture_val = create_formula_disjoint_split_from_records(mixture_records, train_ratio=0.8)
|
|
|
|
| 285 |
|
| 286 |
# Pin all control records to the training pool.
|
| 287 |
+
train_records = mixture_train + control_records
|
| 288 |
+
val_records = mixture_val
|
| 289 |
|
| 290 |
# Verify no identical formula fingerprint (ingredients + weights) appears in both splits.
|
| 291 |
def _formula_fingerprint(r):
|
src/pino/upload_data.py
CHANGED
|
@@ -46,30 +46,44 @@ def create_formula_disjoint_split_from_dataset(
|
|
| 46 |
) -> DatasetDict:
|
| 47 |
"""
|
| 48 |
Partition a Hugging Face Dataset of formula records at the formula level.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
for
|
| 58 |
formula_string = _formula_fingerprint(r)
|
| 59 |
hash_val = int(hashlib.sha256(formula_string.encode("utf-8")).hexdigest(), 16)
|
| 60 |
if (hash_val % 100) < (train_ratio * 100):
|
| 61 |
-
|
| 62 |
else:
|
| 63 |
-
|
| 64 |
|
| 65 |
print("✅ Formula-Disjoint Split Complete.")
|
| 66 |
-
print(f" - Training Recipes: {len(
|
| 67 |
-
print(f" - Validation Recipes: {len(
|
| 68 |
|
| 69 |
-
return
|
| 70 |
-
"train": dataset.select(train_indices),
|
| 71 |
-
"validation": dataset.select(val_indices),
|
| 72 |
-
})
|
| 73 |
|
| 74 |
|
| 75 |
def create_formula_disjoint_split(
|
|
|
|
| 46 |
) -> DatasetDict:
|
| 47 |
"""
|
| 48 |
Partition a Hugging Face Dataset of formula records at the formula level.
|
| 49 |
+
"""
|
| 50 |
+
records = [dict(r) for r in dataset]
|
| 51 |
+
train_records, val_records = create_formula_disjoint_split_from_records(
|
| 52 |
+
records, train_ratio=train_ratio
|
| 53 |
+
)
|
| 54 |
+
return DatasetDict({
|
| 55 |
+
"train": Dataset.from_list(train_records),
|
| 56 |
+
"validation": Dataset.from_list(val_records),
|
| 57 |
+
})
|
| 58 |
|
| 59 |
+
|
| 60 |
+
def create_formula_disjoint_split_from_records(
|
| 61 |
+
records: list[dict[str, Any]],
|
| 62 |
+
train_ratio: float = 0.8,
|
| 63 |
+
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
|
| 64 |
"""
|
| 65 |
+
Partition a list of formula records at the formula level.
|
| 66 |
+
|
| 67 |
+
Avoids row-by-row iteration over a Hugging Face Arrow Dataset and avoids the
|
| 68 |
+
expensive Python-list <-> Arrow round-trip. Returns plain Python lists ready for
|
| 69 |
+
FragranceTrajectoryDataset(records=...).
|
| 70 |
+
"""
|
| 71 |
+
train_records = []
|
| 72 |
+
val_records = []
|
| 73 |
|
| 74 |
+
for r in records:
|
| 75 |
formula_string = _formula_fingerprint(r)
|
| 76 |
hash_val = int(hashlib.sha256(formula_string.encode("utf-8")).hexdigest(), 16)
|
| 77 |
if (hash_val % 100) < (train_ratio * 100):
|
| 78 |
+
train_records.append(r)
|
| 79 |
else:
|
| 80 |
+
val_records.append(r)
|
| 81 |
|
| 82 |
print("✅ Formula-Disjoint Split Complete.")
|
| 83 |
+
print(f" - Training Recipes: {len(train_records)}")
|
| 84 |
+
print(f" - Validation Recipes: {len(val_records)}")
|
| 85 |
|
| 86 |
+
return train_records, val_records
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
def create_formula_disjoint_split(
|