|
| 1 | +#!/usr/bin/env python |
| 2 | +import argparse |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import jax.numpy as jnp |
| 7 | + |
| 8 | +from rubix.core.data import Galaxy, GasData, RubixData, StarsData |
| 9 | +from rubix.inference import ( |
| 10 | + OptimizationObjectiveThresholds, |
| 11 | + RuntimeThresholds, |
| 12 | + benchmark_ifu_cube_optimization, |
| 13 | + benchmark_result_to_dict, |
| 14 | + check_ifu_optimization_guardrails, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class _SyntheticPipeline: |
| 19 | + """Small synthetic pipeline for guardrail smoke checks.""" |
| 20 | + |
| 21 | + def __init__(self, template: jnp.ndarray): |
| 22 | + self.template = template |
| 23 | + |
| 24 | + def run_sharded(self, rubixdata: RubixData) -> jnp.ndarray: |
| 25 | + return rubixdata.stars.age[0] * self.template |
| 26 | + |
| 27 | + |
| 28 | +def _make_data() -> RubixData: |
| 29 | + return RubixData( |
| 30 | + galaxy=Galaxy(), |
| 31 | + stars=StarsData( |
| 32 | + coords=jnp.zeros((1, 3)), |
| 33 | + velocity=jnp.zeros((1, 3)), |
| 34 | + mass=jnp.ones(1), |
| 35 | + age=jnp.array([0.0]), |
| 36 | + metallicity=jnp.array([0.01]), |
| 37 | + ), |
| 38 | + gas=GasData( |
| 39 | + coords=jnp.zeros((1, 3)), |
| 40 | + velocity=jnp.zeros((1, 3)), |
| 41 | + mass=jnp.ones(1), |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def parse_args() -> argparse.Namespace: |
| 47 | + parser = argparse.ArgumentParser( |
| 48 | + description="Run optimization benchmark and assert runtime/loss guardrails." |
| 49 | + ) |
| 50 | + parser.add_argument("--nx", type=int, default=8) |
| 51 | + parser.add_argument("--ny", type=int, default=8) |
| 52 | + parser.add_argument("--nw", type=int, default=64) |
| 53 | + parser.add_argument("--max-steps", type=int, default=120) |
| 54 | + parser.add_argument("--repeats", type=int, default=2) |
| 55 | + parser.add_argument("--max-mean-runtime-s", type=float, default=3.0) |
| 56 | + parser.add_argument("--max-median-runtime-s", type=float, default=3.0) |
| 57 | + parser.add_argument("--max-final-loss", type=float, default=1e-3) |
| 58 | + parser.add_argument("--max-best-loss", type=float, default=1e-3) |
| 59 | + parser.add_argument("--output-json", type=str, default="") |
| 60 | + return parser.parse_args() |
| 61 | + |
| 62 | + |
| 63 | +def main() -> None: |
| 64 | + args = parse_args() |
| 65 | + |
| 66 | + cube = jnp.ones((args.nx, args.ny, args.nw), dtype=jnp.float32) |
| 67 | + target = 1.5 * cube |
| 68 | + |
| 69 | + benchmark_result = benchmark_ifu_cube_optimization( |
| 70 | + pipeline=_SyntheticPipeline(cube), |
| 71 | + params_init={"stars": {"age": jnp.array([0.2])}}, |
| 72 | + static_data=_make_data(), |
| 73 | + target=target, |
| 74 | + learning_rate=0.1, |
| 75 | + max_steps=args.max_steps, |
| 76 | + tol=1e-8, |
| 77 | + repeats=args.repeats, |
| 78 | + warmup=True, |
| 79 | + ) |
| 80 | + |
| 81 | + runtime_thresholds = RuntimeThresholds( |
| 82 | + max_mean_runtime_s=args.max_mean_runtime_s, |
| 83 | + max_median_runtime_s=args.max_median_runtime_s, |
| 84 | + ) |
| 85 | + objective_thresholds = OptimizationObjectiveThresholds( |
| 86 | + max_final_loss=args.max_final_loss, |
| 87 | + max_best_loss=args.max_best_loss, |
| 88 | + ) |
| 89 | + |
| 90 | + check = check_ifu_optimization_guardrails( |
| 91 | + benchmark_result, |
| 92 | + runtime_thresholds, |
| 93 | + objective_thresholds, |
| 94 | + ) |
| 95 | + |
| 96 | + payload = { |
| 97 | + "benchmark": benchmark_result_to_dict(benchmark_result), |
| 98 | + "guardrail": { |
| 99 | + "passed": check.passed, |
| 100 | + "message": check.message, |
| 101 | + "failed_conditions": check.failed_conditions, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + if args.output_json: |
| 106 | + output_path = Path(args.output_json) |
| 107 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 108 | + output_path.write_text(json.dumps(payload, indent=2), encoding="utf-8") |
| 109 | + |
| 110 | + if not check.passed: |
| 111 | + raise SystemExit(check.message) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments