Download code/evaluate.py from yang29/afd-causal-forcing-baselines-vbench: direct link, hf CLI and curl.
- Browser
- Download file 6.37 kB
-
https://huggingface.co/datasets/yang29/afd-causal-forcing-baselines-vbench/resolve/main/code/evaluate.py
- Command line
-
hf download hf://datasets/yang29/afd-causal-forcing-baselines-vbench/code/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/datasets/yang29/afd-causal-forcing-baselines-vbench/resolve/main/code/evaluate.py
6.37 kB
| """Run all original VBench v1 dimensions and the original weighted total.""" | |
| import argparse | |
| import importlib | |
| import json | |
| import math | |
| import os | |
| from pathlib import Path | |
| import subprocess | |
| import sys | |
| from scripts.causal_baselines_b200.eval_common import dimension_entries, load_plan, selected_plan, records, write_json | |
| def complete_generation(plan): | |
| root = Path(plan["output_dir"]) | |
| count, fingerprints = 0, set() | |
| for rank in range(plan["generation_world_size"]): | |
| record = json.loads((root / f"generation-rank{rank}.json").read_text()) | |
| if not record["passed"] or record["checkpoint_step"] != plan["checkpoint_step"]: | |
| raise ValueError("Generation has not completed for the intended checkpoint") | |
| if record["method"] != plan["method"] or record["rank"] != rank or record["world_size"] != plan["generation_world_size"]: | |
| raise ValueError("Generation manifest has a different method or shard topology") | |
| fingerprints.add(record["checkpoint_sha256"]) | |
| count += record["assigned_videos"] | |
| if len(fingerprints) != 1 or count != plan["expected_videos"]: | |
| raise ValueError("Incomplete/mixed-checkpoint generation") | |
| if "selected_checkpoint_sha256" in plan and fingerprints != {plan["selected_checkpoint_sha256"]}: | |
| raise ValueError("Generated videos belong to a different selected checkpoint") | |
| for item in records(plan): | |
| if not Path(item["video"]).is_file() or Path(item["video"]).stat().st_size == 0: | |
| raise FileNotFoundError(item["video"]) | |
| return fingerprints.pop() | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--plan", type=Path, required=True) | |
| parser.add_argument("--dimension") | |
| parser.add_argument("--imports-only", action="store_true") | |
| args = parser.parse_args() | |
| plan = load_plan(args.plan) if args.imports_only else selected_plan(args.plan) | |
| os.chdir(plan["vbench_root"]) | |
| sys.path.insert(0, plan["vbench_root"]) | |
| sys.path.insert(0, str(Path(plan["vbench_root"]) / "scripts")) | |
| from scripts.vbench_b200.compat import install | |
| install() | |
| if args.imports_only: | |
| for dimension in plan["dimensions"]: | |
| importlib.import_module("vbench." + dimension) | |
| print(f"import OK: {dimension}", flush=True) | |
| return | |
| checkpoint_sha = complete_generation(plan) | |
| output = Path(plan["output_dir"]) / "results" | |
| output.mkdir(parents=True, exist_ok=True) | |
| if args.dimension: | |
| if args.dimension not in plan["dimensions"]: | |
| raise ValueError("Dimension is not included in the plan") | |
| import torch | |
| from vbench.utils import init_submodules | |
| entries = dimension_entries(plan, args.dimension) | |
| info = output / (args.dimension + "_full_info.json") | |
| write_json(info, entries) | |
| modules = init_submodules([args.dimension], local=True, read_frame=False) | |
| compute = getattr(importlib.import_module("vbench." + args.dimension), "compute_" + args.dimension) | |
| value = compute(str(info), torch.device("cuda:0"), modules[args.dimension], | |
| imaging_quality_preprocessing_mode="longer") | |
| if not math.isfinite(float(value[0])): | |
| raise FloatingPointError(f"Non-finite VBench score: {args.dimension}") | |
| write_json(output / (args.dimension + "_eval_results.json"), {args.dimension: value}) | |
| return | |
| failures = [] | |
| for dimension in plan["dimensions"]: | |
| target = output / (dimension + "_eval_results.json") | |
| if target.exists(): | |
| try: | |
| saved = json.loads(target.read_text()) | |
| if dimension in saved and math.isfinite(float(saved[dimension][0])): | |
| continue | |
| except (OSError, ValueError, KeyError, TypeError, IndexError): | |
| # Recompute a damaged cache entry; valid dimensions remain intact. | |
| import time | |
| target.rename(target.with_suffix(f'.invalid-{time.time_ns()}')) | |
| command = [sys.executable, str(Path(__file__).resolve()), "--plan", str(args.plan.resolve()), | |
| "--dimension", dimension] | |
| if subprocess.run(command).returncode: | |
| failures.append(dimension) | |
| if failures: | |
| write_json(output / "status.json", {"stage": "failed", "failed_dimensions": failures}) | |
| raise RuntimeError(f"Failed dimensions (completed results preserved): {failures}") | |
| from cal_final_score import get_nomalized_score, get_quality_score, get_semantic_score, get_final_score | |
| raw = {dimension.replace("_", " "): json.loads((output / (dimension + "_eval_results.json")).read_text())[dimension][0] | |
| for dimension in plan["dimensions"]} | |
| normalized = get_nomalized_score(raw) | |
| quality, semantic = get_quality_score(normalized), get_semantic_score(normalized) | |
| total = get_final_score(quality, semantic) | |
| summary = {"method": plan["method"], "checkpoint_step": plan["checkpoint_step"], | |
| "training_target_step": plan.get("training_target_step", 1600), | |
| "training_target_reached": plan["checkpoint_step"] >= plan.get("training_target_step", 1600), | |
| "checkpoint_sha256": checkpoint_sha, "generator_family": "causal_forcing", | |
| "protocol": "VBench v1 standard 16 dimensions, five samples per raw unique prompt" if not plan["smoke"] | |
| else "diagnostic subset only; not a benchmark result", | |
| "smoke": plan["smoke"], "samples_per_prompt": len(plan["seeds"]), "seeds": plan["seeds"], | |
| "unique_prompts": plan["expected_videos"] // len(plan["seeds"]), "videos": plan["expected_videos"], | |
| "metadata_rows": plan["metadata_rows"], "dimensions": raw, | |
| "quality_score": quality, "semantic_score": semantic, "total_score": total, | |
| "quality_score_percent": quality * 100, "semantic_score_percent": semantic * 100, | |
| "total_score_percent": total * 100, "sampler": plan["sampler"], | |
| "vbench_source": plan["vbench_source"]} | |
| write_json(output / "summary.json", summary) | |
| write_json(output / "status.json", {"stage": "complete", "summary": str(output / "summary.json")}) | |
| print(json.dumps(summary, ensure_ascii=False, indent=2), flush=True) | |
| if __name__ == "__main__": | |
| main() | |