@@ -351,6 +351,7 @@ def __init__(self, kernel: _AutotunableKernel, args: Sequence[object]) -> None:
351351 self ._precompile_tmpdir : tempfile .TemporaryDirectory [str ] | None = None
352352 self ._precompile_args_path : str | None = None
353353 self ._precompile_result_counter = count ()
354+ self ._crashed_config_strs : set [str ] = set ()
354355
355356 def _prepare (self ) -> None :
356357 """Some initialization deferred until autotuning actually runs.
@@ -494,6 +495,32 @@ def _try_load_checkpoint(self) -> bool:
494495
495496 def _recompile_after_checkpoint (self ) -> None :
496497 """Recompile after loading a checkpoint. Override in subclasses."""
498+
499+ def _load_crashed_configs (self ) -> None :
500+ """Load crashed configs from {hash}.crashed_configs (written by crash-recovery script)."""
501+ checkpoint_dir_str = self .settings .autotune_checkpoint_dir
502+ if checkpoint_dir_str is None :
503+ return
504+ crashed_configs_path = (
505+ Path (checkpoint_dir_str ) / f"{ self ._get_stable_hash ()} .crashed_configs"
506+ )
507+ if crashed_configs_path .exists ():
508+ self ._crashed_config_strs |= {
509+ line .strip ()
510+ for line in crashed_configs_path .read_text ().splitlines ()
511+ if line .strip ()
512+ }
513+ if self ._crashed_config_strs :
514+ self .log (
515+ f"Loaded { len (self ._crashed_config_strs )} crashed config(s) to skip"
516+ )
517+
518+ def _get_pending_config_path (self ) -> Path | None :
519+ """Get path for pending-config sentinel, or None if checkpointing disabled."""
520+ checkpoint_dir_str = self .settings .autotune_checkpoint_dir
521+ if checkpoint_dir_str is None :
522+ return None
523+ return Path (checkpoint_dir_str ) / f"{ self ._get_stable_hash ()} .pending_config"
497524 def _compute_baseline (
498525 self ,
499526 ) -> tuple [object , Sequence [int ], Sequence [object ] | None ]:
@@ -716,6 +743,12 @@ def benchmark_function(self, config: Config, fn: CompiledConfig) -> float:
716743 Returns:
717744 The performance of the configuration in ms.
718745 """
746+ # Skip configs that previously crashed the subprocess
747+ config_str = str (config )
748+ if config_str in self ._crashed_config_strs :
749+ self .log .warning (f"Skipping known-crashed config: { config } " )
750+ return inf
751+
719752 self ._autotune_metrics .num_configs_tested += 1
720753 self .counters ["benchmark" ] += 1
721754 self .log .debug (lambda : f"Running benchmark for { config !r} " )
@@ -996,13 +1029,36 @@ def _benchmark(
9961029 A list of BenchmarkResult entries containing the configuration, compiled
9971030 callable, measured performance, status, and compilation time.
9981031 """
1032+ # Filter out known-crashed configs before compilation
1033+ if self ._crashed_config_strs :
1034+ original_len = len (configs )
1035+ configs = [c for c in configs if str (c ) not in self ._crashed_config_strs ]
1036+ skipped = original_len - len (configs )
1037+ if skipped :
1038+ self .log .warning (
1039+ f"Skipped { skipped } known-crashed config(s) before compilation"
1040+ )
1041+ if not configs :
1042+ return []
1043+
9991044 fns : list [Callable [..., object ]] = []
10001045 valid_configs : list [Config ] = []
10011046 futures : list [PrecompileFuture ] | None = None
1047+ pending_path = self ._get_pending_config_path ()
10021048 for i , config in enumerate (configs ):
1049+ # Write sentinel before compile so a hard crash (SIGKILL /
1050+ # CUDA IMA) leaves a trace the crash recovery script can find.
1051+ if pending_path is not None :
1052+ pending_path .write_text (str (config ))
10031053 try :
10041054 fn = self .kernel .compile_config (config , allow_print = False )
1005- except Exception :
1055+ except Exception as e :
1056+ if match_unrecoverable_runtime_error (e ):
1057+ # Leave sentinel for crash recovery — CUDA context is
1058+ # corrupted and the process cannot continue.
1059+ raise
1060+ if pending_path is not None :
1061+ pending_path .unlink (missing_ok = True )
10061062 # If all configs failed, raise error
10071063 if not valid_configs and i == len (configs ) - 1 :
10081064 raise
@@ -1012,9 +1068,14 @@ def _benchmark(
10121068 exc_info = True ,
10131069 )
10141070 continue
1071+ if pending_path is not None :
1072+ pending_path .unlink (missing_ok = True )
10151073 fns .append (fn )
10161074 valid_configs .append (config )
10171075 configs = valid_configs
1076+ # NOTE: precompile runs in separate subprocesses with isolated CUDA
1077+ # contexts; crashes there are caught via is_working checks, not
1078+ # sentinels.
10181079 if self .settings .autotune_precompile :
10191080 futures = list (
10201081 starmap (
@@ -1076,7 +1137,14 @@ def _benchmark(
10761137 )
10771138 )
10781139 # benchmark one-by-one to avoid noisy results
1140+ # Write pending-config sentinel; cleared after benchmark.
1141+ # On crash the file stays so the crash recovery script can
1142+ # detect which config caused the failure.
1143+ if pending_path is not None :
1144+ pending_path .write_text (str (config ))
10791145 perf = self .benchmark_function (config , fn )
1146+ if pending_path is not None :
1147+ pending_path .unlink (missing_ok = True )
10801148 status = "ok" if math .isfinite (perf ) else "error"
10811149 # Log completion after benchmarking
10821150 self .log .record_autotune_entry (
@@ -1181,6 +1249,7 @@ def autotune(self, *, skip_cache: bool = False) -> Config:
11811249
11821250 if not self ._try_load_checkpoint ():
11831251 self ._init_search ()
1252+ self ._load_crashed_configs ()
11841253 try :
11851254 best = self ._autotune ()
11861255 self ._cleanup_checkpoint ()
0 commit comments