Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: validate each dataset snapshot once per provenance attestation (#16790)

Daniel Han18b4c0d1b5e4
diff --git a/studio/backend/core/training/provenance.py b/studio/backend/core/training/provenance.py
index 541608e3c45..f22e0940c11 100644
--- a/studio/backend/core/training/provenance.py
+++ b/studio/backend/core/training/provenance.py
@@ -3,11 +3,12 @@
from __future__ import annotations
+import functools
import json
import os
import re
from pathlib import Path, PureWindowsPath
-from typing import Any, Optional
+from typing import Any, Callable, Optional
from urllib.parse import unquote, urlsplit
from hub.utils.paths import is_valid_repo_id
@@ -380,7 +381,9 @@ def exact_dataset_snapshot_for_commit(repo_id: Any, commit: Any) -> Optional[str
return None
-def _local_dataset_source_snapshot(path_value: str, repo_id: str) -> Optional[tuple[str, str]]:
+def _local_dataset_source_snapshot(
+ path_value: str, repo_id: str, snapshot_for_path: Callable[[str, str], Optional[str]]
+) -> Optional[tuple[str, str]]:
if len(path_value) > 4096 or "\x00" in path_value:
return None
path = Path(path_value).expanduser()
@@ -391,7 +394,7 @@ def _local_dataset_source_snapshot(path_value: str, repo_id: str) -> Optional[tu
for parent in path.parents:
if parent.parent.name != "snapshots":
continue
- snapshot = exact_dataset_snapshot_path(str(parent), repo_id)
+ snapshot = snapshot_for_path(str(parent), repo_id)
try:
source_path = path.relative_to(parent).as_posix()
except ValueError:
@@ -479,6 +482,9 @@ def attest_loaded_dataset(repo_id: Any, *datasets: Any) -> tuple[Optional[str],
snapshots: set[str] = set()
found_dataset = False
+ # Per call only: whole-snapshot validation once per snapshot, not per data file (K^2 file checks).
+ snapshot_for_path = functools.lru_cache(maxsize = None)(exact_dataset_snapshot_path)
+ snapshot_for_commit = functools.lru_cache(maxsize = None)(exact_dataset_snapshot_for_commit)
for value in datasets:
for dataset in _loaded_dataset_objects(value):
found_dataset = True
@@ -496,17 +502,14 @@ def attest_loaded_dataset(repo_id: Any, *datasets: Any) -> tuple[Optional[str],
or expected_size < 0
):
return None, "dataset_revision_unattested"
- local_source = _local_dataset_source_snapshot(source, repo_id)
+ local_source = _local_dataset_source_snapshot(source, repo_id, snapshot_for_path)
if local_source is not None:
snapshot, source_path = local_source
else:
source_ref = _hf_dataset_source_ref(source)
if source_ref is None or source_ref[0].casefold() != repo_id.casefold():
return None, "dataset_source_unattested"
- snapshot = exact_dataset_snapshot_for_commit(
- repo_id,
- source_ref[1],
- )
+ snapshot = snapshot_for_commit(repo_id, source_ref[1])
source_path = source_ref[2]
resolved_source = (
_dataset_snapshot_file(snapshot, source_path) if snapshot is not None else None
diff --git a/studio/backend/tests/test_training_provenance.py b/studio/backend/tests/test_training_provenance.py
index 69ad830e671..3fa8a01072b 100644
--- a/studio/backend/tests/test_training_provenance.py
+++ b/studio/backend/tests/test_training_provenance.py
@@ -1038,6 +1038,46 @@ def test_loaded_hub_dataset_rejects_cross_snapshot_symlink(tmp_path):
assert attest_loaded_dataset("org/dataset", loaded) == (None, "dataset_snapshot_unavailable")
[email protected]("local", [False, True])
+def test_loaded_hub_dataset_validates_each_snapshot_once_per_call(tmp_path, monkeypatch, local):
+ files = 60
+ snapshot = tmp_path / "datasets--org--dataset" / "snapshots" / "dataset-commit"
+ snapshot.mkdir(parents = True)
+ for index in range(files):
+ (snapshot / f"train-{index:03d}.parquet").write_bytes(b"dataset")
+ sources = [
+ str(snapshot / f"train-{index:03d}.parquet")
+ if local
+ else f"hf://datasets/org/dataset@dataset-commit/train-{index:03d}.parquet"
+ for index in range(files)
+ ]
+ original = dataset_cache.resolved_dataset_snapshot_file
+ calls = []
+
+ def counting(snapshot_path, source_path):
+ calls.append(source_path)
+ return original(snapshot_path, source_path)
+
+ monkeypatch.setattr(dataset_cache, "resolved_dataset_snapshot_file", counting)
+ train = _loaded_dataset(*sources[: files // 2])
+ evaluation = _loaded_dataset(*sources[files // 2 :])
+
+ for _ in range(2):
+ calls.clear()
+ assert attest_loaded_dataset("org/dataset", train, evaluation) == (
+ str(snapshot.resolve()),
+ None,
+ )
+ per_source = 2 if local else 1
+ assert len(calls) == files + per_source * files
+
+ (snapshot / "train-059.parquet").write_bytes(b"datase")
+ assert attest_loaded_dataset("org/dataset", train, evaluation) == (
+ None,
+ "dataset_snapshot_unavailable",
+ )
+
+
def test_loaded_hub_dataset_rejects_mixed_train_eval_commits(tmp_path):
_dataset_snapshot(tmp_path, "org/dataset", "train-commit")
_dataset_snapshot(tmp_path, "org/dataset", "eval-commit")