Files

92 lines
3.3 KiB
Python

from typing import TYPE_CHECKING, Dict
from ray.train.v2._internal.exceptions import RayTrainError
from ray.util.annotations import PublicAPI
if TYPE_CHECKING:
from ray.train.v2.api.preemption import PreemptionInfo
@PublicAPI(stability="alpha")
class TrainingFailedError(RayTrainError):
"""Exception raised when training fails from a `trainer.fit()` call.
This is either :class:`ray.train.WorkerGroupError` or :class:`ray.train.ControllerError`.
"""
@PublicAPI(stability="alpha")
class WorkerGroupError(TrainingFailedError):
"""Exception raised from the worker group during training.
Args:
error_message: A human-readable error message describing the training worker failures.
worker_failures: A mapping from worker rank to the exception that
occurred on that worker during training.
"""
def __init__(self, error_message: str, worker_failures: Dict[int, Exception]):
super().__init__("Training failed due to worker errors:\n" + error_message)
self._error_message = error_message
self.worker_failures = worker_failures
def __reduce__(self):
return (self.__class__, (self._error_message, self.worker_failures))
@PublicAPI(stability="alpha")
class ControllerError(TrainingFailedError):
"""Exception raised when training fails due to a controller error.
Args:
controller_failure: The exception that occurred on the controller.
"""
def __init__(self, controller_failure: Exception):
super().__init__(
"Training failed due to controller error:\n" + str(controller_failure)
)
self.controller_failure = controller_failure
self.with_traceback(controller_failure.__traceback__)
def __reduce__(self):
return (self.__class__, (self.controller_failure,))
@PublicAPI(stability="alpha")
class PreemptionError(TrainingFailedError):
"""Exception raised when training is interrupted by node preemption.
Distinct from :class:`WorkerGroupError` so that a planned preemption
consumes a separate retry budget (``FailureConfig.max_preemption_failures``,
default -1 = unlimited) rather than ``max_failures``, which is reserved for
real failures (OOM, hardware faults, user-code bugs).
Args:
preemption_info: Which nodes / world ranks were preempted and the
reclaim deadline, for logging and debugging which preemption caused
the restart.
drain_timed_out: True when Ray Train stopped waiting because the reclaim
deadline passed while workers were still running (and tore them down
itself); False when every worker had already exited. Distinguishes
a forced teardown from an observed one when debugging.
"""
def __init__(
self,
preemption_info: "PreemptionInfo",
drain_timed_out: bool = False,
):
self.preemption_info = preemption_info
self.drain_timed_out = drain_timed_out
super().__init__(
"Training was interrupted by node preemption "
f"(preempted_ranks={preemption_info.preempted_ranks}, "
f"drain_timed_out={drain_timed_out})."
)
def __reduce__(self):
return (
self.__class__,
(self.preemption_info, self.drain_timed_out),
)