Download tests/test_consistency.py from HYUKI5/nvidia__SOL-ExecBench: direct link, hf CLI and curl.
- Browser
- Download file 9.24 kB
-
https://huggingface.co/datasets/HYUKI5/nvidia__SOL-ExecBench/resolve/main/tests/test_consistency.py
- Command line
-
hf download hf://datasets/HYUKI5/nvidia__SOL-ExecBench/tests/test_consistency.py
-
curl -L -o test_consistency.py https://huggingface.co/datasets/HYUKI5/nvidia__SOL-ExecBench/resolve/main/tests/test_consistency.py
9.24 kB
| import json | |
| import pathlib | |
| import uuid | |
| import pandas as pd | |
| import pytest | |
| REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent | |
| BENCHMARK_DIR = REPO_ROOT / "benchmark" | |
| DATA_DIR = REPO_ROOT / "data" | |
| PROBLEM_DIRS = sorted(BENCHMARK_DIR.rglob("definition.json")) | |
| SUBSETS = ["FlashInfer-Bench", "L1", "L2", "Quant"] | |
| def _problem_id(path: pathlib.Path) -> str: | |
| """Return a short id like 'L1/rmsnorm_h128' for test parametrization.""" | |
| return "/".join(path.parent.relative_to(BENCHMARK_DIR).parts) | |
| def compute_workload_uuid(workload: dict, reference: str | None = None) -> str: | |
| """Compute a deterministic UUID for a workload dict, ignoring the 'uuid' field. | |
| When *reference* is provided (the ``definition.reference`` source code), | |
| it is included in the hash so that problems with identical workloads but | |
| different reference implementations receive distinct UUIDs. | |
| """ | |
| filtered = {k: v for k, v in workload.items() if k != "uuid"} | |
| serialized = json.dumps(filtered, sort_keys=True) | |
| if reference is not None: | |
| serialized += "\n" + reference | |
| return str(uuid.uuid5(uuid.NAMESPACE_DNS, serialized)) | |
| def test_definition_name_matches_directory(defn_path: pathlib.Path): | |
| """definition.json 'name' field must match the containing directory name.""" | |
| definition = json.loads(defn_path.read_text()) | |
| dir_name = defn_path.parent.name | |
| assert definition["name"] == dir_name, ( | |
| f"definition.json name {definition['name']!r} does not match " | |
| f"directory name {dir_name!r} in {_problem_id(defn_path)}" | |
| ) | |
| def test_reference_matches_file(defn_path: pathlib.Path): | |
| """definition.json 'reference' field must match reference.py contents.""" | |
| definition = json.loads(defn_path.read_text()) | |
| ref_path = defn_path.parent / "reference.py" | |
| assert ref_path.exists(), f"reference.py missing in {defn_path.parent}" | |
| assert "reference" in definition, f"'reference' key missing in {defn_path}" | |
| expected = ref_path.read_text() | |
| actual = definition["reference"] | |
| # Normalise trailing whitespace so a single trailing newline difference | |
| # doesn't cause a false failure. | |
| assert actual.rstrip() == expected.rstrip(), ( | |
| f"reference.py and definition.json['reference'] differ in {_problem_id(defn_path)}" | |
| ) | |
| def test_workload_uuids_unique_within_problem(defn_path: pathlib.Path): | |
| """All UUIDs within a single workload.jsonl must be unique.""" | |
| workload_path = defn_path.parent / "workload.jsonl" | |
| assert workload_path.exists(), f"workload.jsonl missing in {defn_path.parent}" | |
| seen: dict[str, int] = {} | |
| for line_no, line in enumerate(workload_path.read_text().splitlines(), 1): | |
| if not line.strip(): | |
| continue | |
| entry = json.loads(line) | |
| uid = entry.get("uuid") | |
| assert uid is not None, ( | |
| f"Missing 'uuid' at line {line_no} in {_problem_id(defn_path)}/workload.jsonl" | |
| ) | |
| assert uid not in seen, ( | |
| f"Duplicate uuid {uid!r} at lines {seen[uid]} and {line_no} " | |
| f"in {_problem_id(defn_path)}/workload.jsonl" | |
| ) | |
| seen[uid] = line_no | |
| def test_workload_uuid_consistency(defn_path: pathlib.Path): | |
| """Stored UUIDs must match the deterministic compute_workload_uuid output.""" | |
| if "FlashInfer-Bench" in defn_path.parts: | |
| pytest.skip("FlashInfer-Bench is not included in the consistency tests because its UUID matches the original dataset") | |
| definition = json.loads(defn_path.read_text()) | |
| reference = definition.get("reference") | |
| workload_path = defn_path.parent / "workload.jsonl" | |
| assert workload_path.exists(), f"workload.jsonl missing in {defn_path.parent}" | |
| for line_no, line in enumerate(workload_path.read_text().splitlines(), 1): | |
| if not line.strip(): | |
| continue | |
| entry = json.loads(line) | |
| stored = entry.get("uuid") | |
| assert stored is not None, ( | |
| f"Missing 'uuid' at line {line_no} in {_problem_id(defn_path)}/workload.jsonl" | |
| ) | |
| expected = compute_workload_uuid(entry, reference=reference) | |
| assert stored == expected, ( | |
| f"UUID mismatch at line {line_no} in {_problem_id(defn_path)}/workload.jsonl: " | |
| f"stored {stored!r} != computed {expected!r}" | |
| ) | |
| def test_workload_uuids_globally_unique(): | |
| """All workload UUIDs across the entire benchmark must be globally unique.""" | |
| seen: dict[str, str] = {} # uuid -> "subset/kernel:line_no" | |
| for defn_path in PROBLEM_DIRS: | |
| problem_id = _problem_id(defn_path) | |
| workload_path = defn_path.parent / "workload.jsonl" | |
| if not workload_path.exists(): | |
| continue | |
| for line_no, line in enumerate(workload_path.read_text().splitlines(), 1): | |
| if not line.strip(): | |
| continue | |
| entry = json.loads(line) | |
| uid = entry.get("uuid") | |
| if uid is None: | |
| continue | |
| location = f"{problem_id}/workload.jsonl:{line_no}" | |
| assert uid not in seen, ( | |
| f"Duplicate UUID {uid!r} found in {location} " | |
| f"and {seen[uid]}" | |
| ) | |
| seen[uid] = location | |
| # --------------------------------------------------------------------------- | |
| # Parquet ↔ benchmark consistency | |
| # --------------------------------------------------------------------------- | |
| def test_parquet_exists(subset: str): | |
| """Each subset must have a corresponding Parquet file.""" | |
| assert (DATA_DIR / f"{subset}.parquet").exists(), ( | |
| f"data/{subset}.parquet is missing — run scripts/convert_to_parquet.py" | |
| ) | |
| def test_parquet_kernel_count_matches(subset: str): | |
| """Parquet file must contain exactly as many rows as kernel directories.""" | |
| subset_dir = BENCHMARK_DIR / subset | |
| expected = len([d for d in subset_dir.iterdir() if d.is_dir()]) | |
| df = pd.read_parquet(DATA_DIR / f"{subset}.parquet") | |
| assert len(df) == expected, ( | |
| f"{subset}: parquet has {len(df)} rows but benchmark has {expected} kernels" | |
| ) | |
| def test_parquet_kernel_names_match(subset: str): | |
| """Kernel names in Parquet must match definition.json names.""" | |
| subset_dir = BENCHMARK_DIR / subset | |
| expected_names = sorted( | |
| json.loads((d / "definition.json").read_text())["name"] | |
| for d in subset_dir.iterdir() | |
| if d.is_dir() | |
| ) | |
| df = pd.read_parquet(DATA_DIR / f"{subset}.parquet") | |
| actual_names = sorted(df["name"].tolist()) | |
| assert actual_names == expected_names, ( | |
| f"{subset}: kernel name mismatch between parquet and definition.json files" | |
| ) | |
| def test_parquet_reference_matches_source(defn_path: pathlib.Path): | |
| """Reference code in Parquet must match reference.py on disk.""" | |
| rel = defn_path.parent.relative_to(BENCHMARK_DIR) | |
| subset = rel.parts[0] | |
| definition = json.loads(defn_path.read_text()) | |
| kernel_name = definition["name"] | |
| df = pd.read_parquet(DATA_DIR / f"{subset}.parquet") | |
| row = df[df["name"] == kernel_name] | |
| assert len(row) == 1, f"{subset}/{kernel_name} not found in parquet" | |
| expected = (defn_path.parent / "reference.py").read_text() | |
| actual = row.iloc[0]["reference"] | |
| assert actual.rstrip() == expected.rstrip(), ( | |
| f"{subset}/{kernel_name}: reference code differs between parquet and reference.py" | |
| ) | |
| def test_parquet_workloads_match_source(defn_path: pathlib.Path): | |
| """Workloads in Parquet must match workload.jsonl on disk.""" | |
| rel = defn_path.parent.relative_to(BENCHMARK_DIR) | |
| subset = rel.parts[0] | |
| definition = json.loads(defn_path.read_text()) | |
| kernel_name = definition["name"] | |
| df = pd.read_parquet(DATA_DIR / f"{subset}.parquet") | |
| row = df[df["name"] == kernel_name] | |
| assert len(row) == 1, f"{subset}/{kernel_name} not found in parquet" | |
| workload_path = defn_path.parent / "workload.jsonl" | |
| expected = [] | |
| for line in workload_path.read_text().splitlines(): | |
| if line.strip(): | |
| expected.append(json.loads(line)) | |
| actual = json.loads(row.iloc[0]["workloads"]) | |
| assert len(actual) == len(expected), ( | |
| f"{subset}/{kernel_name}: workload count mismatch " | |
| f"(parquet={len(actual)}, source={len(expected)})" | |
| ) | |
| expected_uuids = [w["uuid"] for w in expected] | |
| actual_uuids = [w["uuid"] for w in actual] | |
| assert actual_uuids == expected_uuids, ( | |
| f"{subset}/{kernel_name}: workload UUIDs differ between parquet and source" | |
| ) | |