Spaces:
Running on Zero
Running on Zero
Download infer/benchmark.py from akashch1512/SingleViewHeigthEstimation: direct link, hf CLI and curl.
- Browser
- Download file 4.41 kB
-
https://huggingface.co/spaces/akashch1512/SingleViewHeigthEstimation/resolve/main/infer/benchmark.py
- Command line
-
hf download hf://spaces/akashch1512/SingleViewHeigthEstimation/infer/benchmark.py
-
curl -L -o benchmark.py https://huggingface.co/spaces/akashch1512/SingleViewHeigthEstimation/resolve/main/infer/benchmark.py
4.41 kB
| """Bounded benchmark of the Space's real sliding-window inference path.""" | |
| from __future__ import annotations | |
| import time | |
| import numpy as np | |
| import torch | |
| from config import SEG_IGNORE_INDEX | |
| from infer.engine import count_tiles, predict_scene | |
| def benchmark_model(model, spec, cfg, rgb, gsd_m, overlap, batch_tiles): | |
| if not torch.cuda.is_available(): | |
| raise RuntimeError("This benchmark requires the Space GPU.") | |
| device = torch.device("cuda") | |
| started = time.perf_counter() | |
| props = torch.cuda.get_device_properties(device) | |
| report = { | |
| "gpu": props.name, | |
| "vram_gib": props.total_memory / 2**30, | |
| "torch": torch.__version__, | |
| "checkpoint_epoch": cfg.checkpoint_epoch, | |
| "checkpoint_tensors": cfg.checkpoint_tensor_count, | |
| "detail_branch": cfg.detail_branch, | |
| "preproc": spec.to_dict(), | |
| "source_shape": list(rgb.shape), | |
| "gsd_m": float(gsd_m), | |
| "overlap": overlap, | |
| "batch_tiles": batch_tiles, | |
| "cases": [], | |
| } | |
| model.to(device).eval() | |
| torch.cuda.synchronize() | |
| report["model_to_gpu_s"] = time.perf_counter() - started | |
| amp_dtype = torch.bfloat16 if cfg.amp_dtype == "bf16" else torch.float16 | |
| modes = (("baseline", False, 1.0), ("plain_1.5", False, 1.5), | |
| ("d4_native", True, 1.0), ("d4_1.5", True, 1.5)) | |
| # Bound work and return partial measurements before the 180-second lease. | |
| # Each size uses the same actual RGB pixels across all four modes. | |
| try: | |
| for side in (512, 1024): | |
| scene = np.ascontiguousarray(rgb[:side, :side]) | |
| tiles = count_tiles(*scene.shape[:2], gsd_m, spec, overlap) | |
| for name, tta, scale in modes: | |
| for repeat in range(2): | |
| previous = [c["scene_wall_s"] * tiles / c["tiles"] for c in report["cases"] | |
| if c["mode"] == name] | |
| estimate = max(previous, default=10.0) * 1.3 | |
| if time.perf_counter() - started + estimate > 150: | |
| report["stopped_before_lease_deadline"] = True | |
| return report | |
| torch.cuda.reset_peak_memory_stats() | |
| torch.cuda.synchronize() | |
| begin, end = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True) | |
| wall = time.perf_counter() | |
| begin.record() | |
| height, seg, std = predict_scene( | |
| model, scene, gsd_m, spec, device, | |
| tta=tta, tta_scales=(scale,), amp_dtype=amp_dtype, | |
| overlap=overlap, batch_tiles=batch_tiles, | |
| want_seg=True, want_std=True, | |
| ) | |
| end.record() | |
| torch.cuda.synchronize() | |
| elapsed = time.perf_counter() - wall | |
| assert height.shape == seg.shape == std.shape == scene.shape[:2] | |
| assert np.isfinite(height).all() and np.isfinite(std).all() | |
| assert (std >= 0).all() and seg.min() >= 0 and seg.max() < SEG_IGNORE_INDEX | |
| case = { | |
| "mode": name, "scale": scale, "d4": tta, | |
| "size_px": list(scene.shape[:2]), "tiles": tiles, | |
| "repeat": repeat, "scene_wall_s": elapsed, | |
| "cuda_elapsed_s": begin.elapsed_time(end) / 1000, | |
| "peak_allocated_gib": torch.cuda.max_memory_allocated() / 2**30, | |
| "peak_reserved_gib": torch.cuda.max_memory_reserved() / 2**30, | |
| "height_min_m": float(height.min()), "height_max_m": float(height.max()), | |
| "height_mean_m": float(height.mean()), "std_mean_m": float(std.mean()), | |
| "class_ids": np.unique(seg).tolist(), | |
| "outputs_finite_and_same_grid": True, | |
| } | |
| report["cases"].append(case) | |
| print(f"[benchmark] {name} {scene.shape[:2]} repeat={repeat} " | |
| f"tiles={tiles} seconds={elapsed:.3f}", flush=True) | |
| del height, seg, std | |
| return report | |
| finally: | |
| model.to("cpu") | |
| torch.cuda.empty_cache() | |
| report["total_gpu_worker_s"] = time.perf_counter() - started | |