KernelBench-compatible correctness checks. Generate n_trials random input sets, run both models, compare within (atol, rtol).
(
model_new,
model_ref,
get_inputs_fn: Callable,
n_trials: int = DEFAULT_N_CORRECTNESS,
atol: float = DEFAULT_ATOL,
rtol: float = DEFAULT_RTOL,
device: str = "cuda",
)
| 253 | # --------------------------------------------------------------------------- |
| 254 | |
| 255 | def run_correctness( |
| 256 | model_new, |
| 257 | model_ref, |
| 258 | get_inputs_fn: Callable, |
| 259 | n_trials: int = DEFAULT_N_CORRECTNESS, |
| 260 | atol: float = DEFAULT_ATOL, |
| 261 | rtol: float = DEFAULT_RTOL, |
| 262 | device: str = "cuda", |
| 263 | ) -> Dict[str, Any]: |
| 264 | """ |
| 265 | KernelBench-compatible correctness checks. |
| 266 | |
| 267 | Generate n_trials random input sets, run both models, compare within (atol, rtol). |
| 268 | """ |
| 269 | import torch |
| 270 | |
| 271 | results: Dict[str, Any] = { |
| 272 | "correctness": "FAIL", |
| 273 | "trials_passed": 0, |
| 274 | "trials_total": n_trials, |
| 275 | "worst_max_abs_error": 0.0, |
| 276 | "worst_mean_abs_error": 0.0, |
| 277 | "details": [], |
| 278 | } |
| 279 | |
| 280 | for trial in range(n_trials): |
| 281 | trial_info: Dict[str, Any] = {"trial": trial, "status": "FAIL"} |
| 282 | try: |
| 283 | with _Timeout(TIMEOUT_SECONDS, f"trial {trial} timed out"): |
| 284 | inputs = get_inputs_fn() |
| 285 | inputs_dev = [ |
| 286 | inp.to(device) if isinstance(inp, torch.Tensor) else inp |
| 287 | for inp in inputs |
| 288 | ] |
| 289 | |
| 290 | with torch.no_grad(): |
| 291 | expected = model_ref(*inputs_dev) |
| 292 | with torch.no_grad(): |
| 293 | output = model_new(*inputs_dev) |
| 294 | |
| 295 | cmp = _compare_outputs(output, expected, atol, rtol) |
| 296 | trial_info["max_abs_error"] = cmp.get("max_abs_error", float("inf")) |
| 297 | trial_info["mean_abs_error"] = cmp.get("mean_abs_error", float("inf")) |
| 298 | |
| 299 | if cmp["match"]: |
| 300 | trial_info["status"] = "PASS" |
| 301 | results["trials_passed"] += 1 |
| 302 | results["worst_max_abs_error"] = max( |
| 303 | results["worst_max_abs_error"], cmp.get("max_abs_error", 0) |
| 304 | ) |
| 305 | results["worst_mean_abs_error"] = max( |
| 306 | results["worst_mean_abs_error"], cmp.get("mean_abs_error", 0) |
| 307 | ) |
| 308 | else: |
| 309 | trial_info["reason"] = cmp["reason"] |
| 310 | |
| 311 | except TimeoutError as e: |
| 312 | trial_info["status"] = "TIMEOUT" |
no test coverage detected