fix(upload,train): use load_dataset select and fingerprint-based leakage check
Browse filesAvoid materializing 20k records via Dataset.from_list. Use load_dataset +
select(indices) for fast formula-disjoint partitioning. Verify isolation by
exact ingredient+weight fingerprint, not formula_id (which is reused across
different recipes).
- src/pino/train.py +11 -4
- src/pino/upload_data.py +11 -12
src/pino/train.py
CHANGED
|
@@ -294,10 +294,17 @@ def main():
|
|
| 294 |
print("⏳ Initiating formula-disjoint dataset partitioning...")
|
| 295 |
dataset_dict = create_formula_disjoint_split(args.data, train_ratio=0.8)
|
| 296 |
|
| 297 |
-
# Verify no identical formula fingerprint appears in both splits.
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
print(f"🔒 Formula isolation check: {len(overlap)} identical recipes shared between splits.")
|
| 302 |
assert len(overlap) == 0, f"Formula leakage detected: {overlap}"
|
| 303 |
|
|
|
|
| 294 |
print("⏳ Initiating formula-disjoint dataset partitioning...")
|
| 295 |
dataset_dict = create_formula_disjoint_split(args.data, train_ratio=0.8)
|
| 296 |
|
| 297 |
+
# Verify no identical formula fingerprint (ingredients + weights) appears in both splits.
|
| 298 |
+
def _formula_fingerprint(r):
|
| 299 |
+
components = sorted(
|
| 300 |
+
r.get("metadata", {}).get("initial_components") or r.get("formula", []),
|
| 301 |
+
key=lambda x: x.get("cas", ""),
|
| 302 |
+
)
|
| 303 |
+
return "".join(f"{c.get('cas', '')}_{c.get('weight_fraction', 0):.4f}" for c in components)
|
| 304 |
+
|
| 305 |
+
train_fps = {_formula_fingerprint(r) for r in dataset_dict["train"]}
|
| 306 |
+
val_fps = {_formula_fingerprint(r) for r in dataset_dict["validation"]}
|
| 307 |
+
overlap = train_fps & val_fps
|
| 308 |
print(f"🔒 Formula isolation check: {len(overlap)} identical recipes shared between splits.")
|
| 309 |
assert len(overlap) == 0, f"Formula leakage detected: {overlap}"
|
| 310 |
|
src/pino/upload_data.py
CHANGED
|
@@ -8,7 +8,7 @@ import os
|
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Any
|
| 10 |
|
| 11 |
-
from datasets import ClassLabel,
|
| 12 |
|
| 13 |
logger = logging.getLogger("pino.upload_data")
|
| 14 |
|
|
@@ -43,13 +43,12 @@ def create_formula_disjoint_split(
|
|
| 43 |
"""
|
| 44 |
jsonl_path = Path(jsonl_path)
|
| 45 |
logger.info("Loading raw corpus from %s", jsonl_path)
|
| 46 |
-
|
| 47 |
-
records = [json.loads(line) for line in f]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
for r in
|
| 53 |
# Build a deterministic structural fingerprint from the exact recipe.
|
| 54 |
# Sorting ensures identical ingredient combinations hash identically
|
| 55 |
# regardless of component order.
|
|
@@ -63,17 +62,17 @@ def create_formula_disjoint_split(
|
|
| 63 |
|
| 64 |
hash_val = int(hashlib.sha256(formula_string.encode("utf-8")).hexdigest(), 16)
|
| 65 |
if (hash_val % 100) < (train_ratio * 100):
|
| 66 |
-
|
| 67 |
else:
|
| 68 |
-
|
| 69 |
|
| 70 |
print("✅ Formula-Disjoint Split Complete.")
|
| 71 |
-
print(f" - Training Recipes: {len(
|
| 72 |
-
print(f" - Validation Recipes: {len(
|
| 73 |
|
| 74 |
return DatasetDict({
|
| 75 |
-
"train":
|
| 76 |
-
"validation":
|
| 77 |
})
|
| 78 |
|
| 79 |
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Any
|
| 10 |
|
| 11 |
+
from datasets import ClassLabel, DatasetDict, load_dataset
|
| 12 |
|
| 13 |
logger = logging.getLogger("pino.upload_data")
|
| 14 |
|
|
|
|
| 43 |
"""
|
| 44 |
jsonl_path = Path(jsonl_path)
|
| 45 |
logger.info("Loading raw corpus from %s", jsonl_path)
|
| 46 |
+
raw_dataset = load_dataset("json", data_files=str(jsonl_path), split="train")
|
|
|
|
| 47 |
|
| 48 |
+
train_indices = []
|
| 49 |
+
val_indices = []
|
| 50 |
|
| 51 |
+
for idx, r in enumerate(raw_dataset):
|
| 52 |
# Build a deterministic structural fingerprint from the exact recipe.
|
| 53 |
# Sorting ensures identical ingredient combinations hash identically
|
| 54 |
# regardless of component order.
|
|
|
|
| 62 |
|
| 63 |
hash_val = int(hashlib.sha256(formula_string.encode("utf-8")).hexdigest(), 16)
|
| 64 |
if (hash_val % 100) < (train_ratio * 100):
|
| 65 |
+
train_indices.append(idx)
|
| 66 |
else:
|
| 67 |
+
val_indices.append(idx)
|
| 68 |
|
| 69 |
print("✅ Formula-Disjoint Split Complete.")
|
| 70 |
+
print(f" - Training Recipes: {len(train_indices)}")
|
| 71 |
+
print(f" - Validation Recipes: {len(val_indices)}")
|
| 72 |
|
| 73 |
return DatasetDict({
|
| 74 |
+
"train": raw_dataset.select(train_indices),
|
| 75 |
+
"validation": raw_dataset.select(val_indices),
|
| 76 |
})
|
| 77 |
|
| 78 |
|