Download scripts/test_dsv41.py from nvidia/aisimulate-fpm-dataset: direct link, hf CLI and curl.
- Browser
- Download file 10 kB
-
https://huggingface.co/datasets/nvidia/aisimulate-fpm-dataset/resolve/main/scripts/test_dsv41.py
- Command line
-
hf download hf://datasets/nvidia/aisimulate-fpm-dataset/scripts/test_dsv41.py
-
curl -L -o test_dsv41.py https://huggingface.co/datasets/nvidia/aisimulate-fpm-dataset/resolve/main/scripts/test_dsv41.py
10 kB
| """Negative admission controls against published DSV4.1 source artifacts.""" | |
| import copy | |
| import json | |
| import shutil | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| import dsv41 | |
| import manage_dataset | |
| ROOT = Path(__file__).resolve().parents[1] | |
| class AdmissionTests(unittest.TestCase): | |
| def setUp(self): | |
| records = json.loads((ROOT / "catalog/fpm.json").read_text())["records"] | |
| self.records = [ | |
| r for r in records if r["sha256"] in dsv41.REVIEWED_SOURCES | |
| ] | |
| self.assertTrue(self.records) | |
| def test_every_reviewed_payload(self): | |
| for row in self.records: | |
| self.assertEqual(dsv41.validate_catalog_record(ROOT, row), 7) | |
| profiles = { | |
| dsv41.identity(ROOT / r["path"])["execution_profile"] for r in self.records | |
| } | |
| self.assertEqual(profiles, {"full", "decoder_bounded"}) | |
| def test_native_source_artifact_not_git_revision(self): | |
| row = next(copy.deepcopy(r) for r in self.records if dsv41.REVIEWED_SOURCES[r['sha256']]['source_pr'] == 304) | |
| for key, value in [('source_revision_kind', 'aisimulate_producer_git_commit'), ('source_kind', 'committed_calibration')]: | |
| changed = copy.deepcopy(row) | |
| changed[key] = value | |
| with self.assertRaises(ValueError): | |
| dsv41.validate_catalog_record(ROOT, changed) | |
| def test_manifests_match_published_schemas(self): | |
| index = json.loads((ROOT / "catalog/index.json").read_text()) | |
| checked = 0 | |
| for relative in [*index["configuration_manifests"], *index["history_manifests"]]: | |
| manifest = json.loads((ROOT / relative).read_text()) | |
| if not manifest["fpm"] or not all(e["sha256"] in dsv41.REVIEWED_SOURCES for e in manifest["fpm"]): | |
| continue | |
| bundle = json.loads((ROOT / manifest["measurements"]["manifest_path"]).read_text()) | |
| self.assertEqual(manage_dataset.schema_violations(ROOT, "configuration_manifest", manifest), [], relative) | |
| self.assertEqual(manage_dataset.schema_violations(ROOT, "measurement_manifest", bundle), [], relative) | |
| checked += 1 | |
| self.assertEqual(checked, 10) | |
| def test_schema_rejects_nonconforming_manifest(self): | |
| path = next( | |
| ROOT / r["configuration_path"] / "manifest.json" | |
| for r in self.records if r["system"] == "h200_sxm" | |
| ) | |
| manifest = json.loads(path.read_text()) | |
| for mutate in ( | |
| lambda m: m.update(snapshot_id="native-6f01f78b10be-pr304"), | |
| lambda m: m.pop("collected_by"), | |
| lambda m: m.pop("producer_revisions"), | |
| lambda m: m["provenance"].update(unreviewed_field="x"), | |
| ): | |
| changed = copy.deepcopy(manifest) | |
| mutate(changed) | |
| self.assertTrue(manage_dataset.schema_violations(ROOT, "configuration_manifest", changed)) | |
| def test_changed_source_digest(self): | |
| row = copy.deepcopy(self.records[0]) | |
| row["sha256"] = "0" * 64 | |
| with self.assertRaises(ValueError): | |
| dsv41.validate_catalog_record(ROOT, row) | |
| def test_wrong_schema_and_topology(self): | |
| for key, value in [("schema_version", 6), ("tp", 128)]: | |
| row = copy.deepcopy(self.records[0]) | |
| row[key] = value | |
| with self.assertRaises(ValueError): | |
| dsv41.validate_catalog_record(ROOT, row) | |
| def test_execution_profile_cannot_be_swapped(self): | |
| row = self.records[0] | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| for key in ("path", "metadata_path"): | |
| dest = root / row[key] | |
| dest.parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copyfile(ROOT / row[key], dest) | |
| dest = root / row["metadata_path"] | |
| meta = json.loads(dest.read_text()) | |
| meta["configuration_selector"]["execution_profile"] = "another-profile" | |
| dest.write_text(json.dumps(meta)) | |
| with self.assertRaises(ValueError): | |
| dsv41.validate_catalog_record(root, row) | |
| def test_archive_cannot_be_promoted(self): | |
| archives = [r for r in self.records if dsv41.REVIEWED_SOURCES[r["sha256"]]["source_pr"] == 158] | |
| for record in archives: | |
| row = copy.deepcopy(record) | |
| row.update(snapshot_status="current", role="primary") | |
| with self.assertRaises(ValueError): | |
| dsv41.validate_catalog_record(ROOT, row) | |
| class ProducerProvenanceTests(unittest.TestCase): | |
| def test_producer_git_package_missing_and_mixed(self): | |
| commit = "40c9541bb0bb593f403345efde01eafba436543c" | |
| package = "installed:aisimulate==0.12.0:record-sha256:" + "a" * 64 | |
| for originals, expected, status in [ | |
| ([{"aic_revision": commit}], commit, "recorded"), | |
| ([{"aic_revision": package}], None, "unknown"), | |
| ([{}], None, "unknown"), | |
| ([{"aic_revision": commit}, {}], None, "unknown"), | |
| ([{"aic_revision": commit}, {"aic_revision": "b" * 40}], None, "mixed"), | |
| ([{"aic_revision": commit}, {"aic_revision": package}], None, "mixed"), | |
| ]: | |
| identity = dsv41.producer_identity(originals) | |
| self.assertEqual(identity["aisim_commit"], expected) | |
| self.assertEqual(identity["aisim_commit_status"], status) | |
| self.assertEqual(dsv41.producer_identity([{"aic_revision": package}])["producer_revisions"], [package]) | |
| def test_published_snapshot_distinguishes_archive_and_producer(self): | |
| index = dsv41.read(ROOT / "catalog/index.json") | |
| for path in index["history_manifests"] + index["configuration_manifests"]: | |
| manifest = dsv41.read(ROOT / path) | |
| if not manifest["fpm"] or not all(e["sha256"] in dsv41.REVIEWED_SOURCES for e in manifest["fpm"]): | |
| continue | |
| dsv41.validate_snapshot(ROOT, manifest) | |
| self.assertNotEqual(manifest["aisim_commit"], manifest["provenance"]["source_revision"]) | |
| changed = copy.deepcopy(manifest) | |
| changed["aisim_commit"] = manifest["provenance"]["source_revision"] | |
| changed["aisim_commit_status"] = "recorded" | |
| with self.assertRaisesRegex(ValueError, "Producer identity mismatch"): | |
| dsv41.validate_snapshot(ROOT, changed) | |
| def test_runtime_projection_preserves_collection_not_predictions(self): | |
| from ingest_dsv41 import collection_projection | |
| source = {"collection_provenance": { | |
| "runtime": {"image_manifest_digest": "sha256:" + "a" * 64, | |
| "model_revision": "b" * 40, "source_qualification_sha256": "c" * 64}, | |
| "observed_packages": {"torch": "2.13.0+cu130"}, | |
| "selection_policy": "first geometry-qualified fixed attempt by index; no latency selection", | |
| "calibration": {"planned_attempts": 1450, "qualified_attempts": 1224, | |
| "exact_static_self_queries": 145, "mape_percent": 0.5}, | |
| "heldout_validation": {"mape_percent": 1.0}, | |
| "prediction_validation": {"predictor_commit": "d" * 40}, | |
| }, "private_publication_provenance": { | |
| "runtime_pair": {"calibration_runtime_identity": {"model_revision": "b" * 40}}, | |
| "selection_policy": "first qualified", "reader": {"head": "d" * 40}, | |
| "planned_geometries": 145, | |
| }} | |
| projected = collection_projection(source) | |
| c = projected["collection_provenance"] | |
| for key in ("runtime", "observed_packages", "selection_policy"): | |
| self.assertEqual(c[key], source["collection_provenance"][key]) | |
| self.assertEqual(c["calibration"], {"planned_attempts": 1450, "qualified_attempts": 1224}) | |
| self.assertNotIn("prediction_validation", c) | |
| self.assertNotIn("heldout_validation", c) | |
| self.assertNotIn("reader", projected["private_publication_provenance"]) | |
| self.assertEqual(dsv41.recorded_model_revision(projected), "b" * 40) | |
| projected["private_publication_provenance"]["runtime_pair"]["calibration_runtime_identity"]["model_revision"] = "c" * 40 | |
| with self.assertRaisesRegex(ValueError, "Conflicting recorded model"): | |
| dsv41.recorded_model_revision(projected) | |
| def test_published_runtime_provenance(self): | |
| records = dsv41.read(ROOT / "catalog/fpm.json")["records"] | |
| for row in records: | |
| if dsv41.REVIEWED_SOURCES.get(row["sha256"],{}).get("source_pr") != 232: | |
| continue | |
| meta = dsv41.read(ROOT / row["metadata_path"]) | |
| self.assertEqual(row["model_revision"], "fb2764a5cf321eaa5070ca8f9e892818f477c16d") | |
| collection = meta.get("collection_provenance") | |
| if collection: | |
| self.assertTrue(collection["runtime"]["image_manifest_digest"].startswith("sha256:")) | |
| self.assertTrue(collection["runtime"]["source_qualification_sha256"]) | |
| self.assertEqual(collection["observed_packages"]["nvidia-nccl-cu13"], "2.30.7") | |
| self.assertEqual(collection["calibration"]["planned_attempts"], 1450) | |
| self.assertGreater(collection["calibration"]["qualified_attempts"], 0) | |
| self.assertIn("first geometry-qualified", collection["selection_policy"]) | |
| else: | |
| publication = meta["private_publication_provenance"] | |
| self.assertEqual(publication["planned_geometries"], 145) | |
| self.assertTrue(publication["runtime_pair"]["calibration_runtime_identity"]["image_manifest_digest"]) | |
| self.assertIn("first geometry-qualified", publication["selection_policy"]) | |
| serialized = json.dumps(meta) | |
| for forbidden in ('"mape_percent"', '"prediction_validation"', '"predicted_geometries"', '"exact_static_self_queries"'): | |
| self.assertNotIn(forbidden, serialized) | |
| if __name__ == "__main__": | |
| unittest.main() | |