kempnerforge.resilience.signal_handler¶
Graceful shutdown via signal handling.
Handles SIGTERM (SLURM preemption / graceful shutdown) and SIGUSR1 (SLURM requeue) by setting a flag that the training loop checks after each step. On signal, the loop saves an emergency checkpoint and exits.
Timeout protection ensures the process exits even if graceful shutdown stalls (e.g., stuck in NCCL collective).
Module Attributes
Seconds a launch script gives its ranks from the shutdown SIGTERM until they may be killed. |
|
Share of that lead the forced-exit timer of a SIGTERM defaults to, so it fires first. |
|
The forced-exit timeout when neither the config nor the launch script sets one. |
Functions
|
|
|
The forced-exit timeouts, in seconds, for |
Classes
Cooperative shutdown handler for long-running training jobs. |
- kempnerforge.resilience.signal_handler.SHUTDOWN_LEAD_ENV = 'KEMPNERFORGE_SHUTDOWN_LEAD_SEC'¶
Seconds a launch script gives its ranks from the shutdown SIGTERM until they may be killed.
- kempnerforge.resilience.signal_handler.SHUTDOWN_LEAD_FRACTION = 0.75¶
Share of that lead the forced-exit timer of a SIGTERM defaults to, so it fires first.
- kempnerforge.resilience.signal_handler.DEFAULT_SHUTDOWN_TIMEOUT_SEC = 600.0¶
The forced-exit timeout when neither the config nor the launch script sets one.
- kempnerforge.resilience.signal_handler.resolve_shutdown_timeouts(configured)[source]¶
The forced-exit timeouts, in seconds, for
train.shutdown_timeout_sec.The launch scripts under
scripts/slurmexportKEMPNERFORGE_SHUTDOWN_LEAD_SEC, the seconds their ranks have from the shutdownSIGTERMuntil they may be killed: the--signallead of the scripts that launch the ranks withsrun, since at the time limit SLURM’sSIGTERMcan makesrunkill them at once, and the 30 s thattorchrungives its workers by default insinglenode.sh. Left unset, aSIGTERMgets three quarters of that lead, so a rank whose emergency save, flush or teardown hangs exits through the timer, which logsGraceful shutdown timed out, before the launcher kills it; without the variable it gets 600 s. ASIGUSR1, the manual stop, which no kill follows, and a shutdown another rank started get 600 s.0disables the timer. Any other value applies to both, with a warning when it is not below the lead, since the launcher can then kill the rank before the timer fires.- Returns:
(timeout_sec, sigterm_timeout_sec), asShutdownHandlertakes them.- Raises:
ValueError –
KEMPNERFORGE_SHUTDOWN_LEAD_SECis set to something other than a positive number of seconds.- Parameters:
configured (float | None)
- Return type:
- kempnerforge.resilience.signal_handler.ignore_shutdown_signals_in_worker(worker_id)[source]¶
worker_init_fnthat makes a DataLoader worker ignore shutdown signals.SLURM preemption and
torchrunteardown signal the whole process group, not a single PID, so SIGTERM reaches every DataLoader worker as well as the rank’s main process. Workers install no handler, take the default action and die at once. The main process — which handles the signal cooperatively, by setting a flag and finishing its step — then fails fetching its next micro-batch, and that failure escapes before the loop reaches itsshould_shutdown()check, so no emergency checkpoint is written.Ignoring the shutdown signals in the worker keeps it serving long enough for the step to finish, which puts the emergency save back on the loop’s normal path rather than on an exception path.
The cost:
multiprocessing.Process.terminate()is itself SIGTERM, so a shielded worker cannot be reaped that way, andmultiprocessing’s exit hook, which terminates leftover workers and then joins them, would block on one forever. Workers are therefore shut down through the loader’s sentinel path:StatefulDataLoader.closeandStreamingDataLoader.close, which the training loop calls on every exit path and a process calls at exit for every loader whose workers are still alive. Nothing closes a plainDataLoaderat exit: its caller has to callshutdown_workersitself.- Parameters:
worker_id (int) – Worker index, supplied by
DataLoader. Unused.- Return type:
None
- class kempnerforge.resilience.signal_handler.ShutdownHandler[source]¶
Bases:
objectCooperative shutdown handler for long-running training jobs.
Register this handler before the training loop. The training loop checks
should_shutdown()after each step and takes appropriate action (save checkpoint, clean up, exit).If the graceful shutdown exceeds its timeout, a forced exit is triggered via
os._exitto avoid hanging on stuck collectives.Usage:
handler = ShutdownHandler(timeout_sec=120) handler.register() for step in range(max_steps): train_step() handler.reinstall() if handler.should_shutdown(): save_checkpoint() break wait_for_checkpoint() handler.finish()
- Parameters:
timeout_sec – Seconds a shutdown that a
SIGUSR1orrequest()starts may take before the forced exit; 0 disables it.Noneresolves it, andsigterm_timeout_secwith it, as training does for an unsettrain.shutdown_timeout_sec(seeresolve_shutdown_timeouts).sigterm_timeout_sec – The same for a
SIGTERM, which the launcher may follow with a kill;Nonetakestimeout_sec, or the resolved value. ASIGTERMthat arrives once a longer timer is running brings the forced exit forward to its own timeout.
- should_shutdown()[source]¶
Check if the training loop should exit.
Call this after each training step.
- Return type:
- request()[source]¶
Request a shutdown without a signal, as when another rank received one.
Sets the flag and starts the forced-exit timer with
timeout_sec, as aSIGUSR1does, so a rank that stops because another rank was signaled is bounded by its own timer too; its ownSIGTERM, if one follows, brings the exit forward. Calling it again changes nothing.- Return type:
None
- register()[source]¶
Register signal handlers for SIGTERM and SIGUSR1.
Must be called from the main thread.
- Return type:
None
- reinstall()[source]¶
Install the handlers again; a no-op unless registered, or off the main thread.
Native code can replace a handler below Python, where
signal.getsignalcannot see it: the LLVM inside Triton does on its first in-process compile, after which SIGUSR1 does nothing. Only the main thread can install a handler, so on any other thread, such as a training loop run off the main thread, this does nothing.- Return type:
None
- unregister()[source]¶
Restore the original signal handlers; the forced-exit timer stays armed.
- Return type:
None
- cancel_watchdog()[source]¶
Cancel the forced-exit timer, leaving the signal handlers installed.
- Return type:
None
- finish()[source]¶
Call once the emergency checkpoint is durable and teardown is done.
Restores the signal handlers and cancels the forced-exit timer, so it must come after the last step that a second signal or a hang could cut short: the checkpoint flush, and tearing down the process group.
- Return type:
None