Download simulation/scripts/bench_sonic_batch.py from hk239/v2d: direct link, hf CLI and curl.
- Browser
- Download file 3.3 kB
-
https://huggingface.co/datasets/hk239/v2d/resolve/main/simulation/scripts/bench_sonic_batch.py
- Command line
-
hf download hf://datasets/hk239/v2d/simulation/scripts/bench_sonic_batch.py
-
curl -L -o bench_sonic_batch.py https://huggingface.co/datasets/hk239/v2d/resolve/main/simulation/scripts/bench_sonic_batch.py
3.3 kB
| #!/usr/bin/env python3 | |
| """Measure batched SONIC inference throughput, CPU vs CUDA. | |
| An RL rollout evaluates SONIC once per environment per control tick, so the | |
| number that matters is env-steps/s = batch / seconds-per-tick. Isaac Lab at 50 Hz | |
| with N envs needs 50*N env-steps/s just to keep up with physics. | |
| python scripts/bench_sonic_batch.py # CPU and CUDA if available | |
| python scripts/bench_sonic_batch.py --batch 128 512 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import importlib.util | |
| import os | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| _ROOT = Path(__file__).resolve().parents[1] | |
| _DEFAULT_CKPT = _ROOT.parent / "checkpoints" / "sonic" / "sonic_v1_1" | |
| def _load_policy_module(): | |
| """Import ``sonic/policy.py`` alone; ``v2d_sim`` itself needs Isaac Lab.""" | |
| path = _ROOT / "source" / "v2d_sim" / "sonic" / "policy.py" | |
| spec = importlib.util.spec_from_file_location("sonic_policy", path) | |
| module = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(module) | |
| return module | |
| def bench(ckpt: Path, batches: list[int], repeats: int, use_cuda: bool) -> None: | |
| os.environ["SONIC_ORT_CUDA"] = "1" if use_cuda else "0" | |
| SonicOnnxAgent = _load_policy_module().SonicOnnxAgent | |
| try: | |
| agent = SonicOnnxAgent(ckpt, batched=True) | |
| except Exception as exc: # noqa: BLE001 | |
| print(f" could not load ({exc})") | |
| return | |
| providers = agent.encoder.get_providers() | |
| print(f" providers in use: {providers}") | |
| if use_cuda and not any("CUDA" in p or "Tensorrt" in p for p in providers): | |
| print(" CUDA provider did not attach; skipping") | |
| return | |
| rng = np.random.default_rng(0) | |
| for n in batches: | |
| enc = rng.normal(size=(n, 1751)).astype(np.float32) | |
| enc[:, 0] = 1.0 # teleop | |
| dec = rng.normal(size=(n, 994)).astype(np.float32) | |
| for _ in range(3): | |
| dec[:, :64] = agent.encode(enc) | |
| agent.decode(dec) | |
| t0 = time.perf_counter() | |
| for _ in range(repeats): | |
| dec[:, :64] = agent.encode(enc) | |
| agent.decode(dec) | |
| dt = (time.perf_counter() - t0) / repeats | |
| envs_at_50hz = int(1.0 / dt / 50 * n) if dt > 0 else 0 | |
| print( | |
| f" N={n:5d} {dt * 1000:8.2f} ms/tick {n / dt:10.0f} env-steps/s" | |
| f" -> sustains {envs_at_50hz:6d} envs at 50 Hz" | |
| ) | |
| def main() -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--checkpoint", type=Path, default=_DEFAULT_CKPT) | |
| ap.add_argument("--batch", type=int, nargs="+", default=[64, 256, 1024, 4096]) | |
| ap.add_argument("--repeats", type=int, default=10) | |
| args = ap.parse_args() | |
| if not (args.checkpoint / "model_encoder_batch.onnx").is_file(): | |
| raise SystemExit( | |
| f"no dynamic-batch graphs in {args.checkpoint}; " | |
| "run scripts/export_sonic_dynamic_batch.py first" | |
| ) | |
| import onnxruntime as ort | |
| print(f"onnxruntime {ort.__version__} built-in providers: {ort.get_available_providers()}\n") | |
| print("CPU:") | |
| bench(args.checkpoint, args.batch, args.repeats, use_cuda=False) | |
| print("\nCUDA:") | |
| bench(args.checkpoint, args.batch, args.repeats, use_cuda=True) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |