-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log exception on inactivity callback (#1194)
- Loading branch information
1 parent
983234d
commit 994209c
Showing
3 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2024 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import logging | ||
import os | ||
import signal | ||
import threading | ||
from typing import Optional | ||
|
||
from composer import Callback, Logger, State | ||
from composer.loggers import MosaicMLLogger | ||
|
||
from llmfoundry.utils.exceptions import RunTimeoutError | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def _timeout(timeout: int, mosaicml_logger: Optional[MosaicMLLogger] = None): | ||
log.error(f'Timeout after {timeout} seconds of inactivity after fit_end.',) | ||
if mosaicml_logger is not None: | ||
mosaicml_logger.log_exception(RunTimeoutError(timeout=timeout)) | ||
os.kill(os.getpid(), signal.SIGINT) | ||
|
||
|
||
class RunTimeoutCallback(Callback): | ||
|
||
def __init__( | ||
self, | ||
timeout: int = 1800, | ||
): | ||
self.timeout = timeout | ||
self.mosaicml_logger: Optional[MosaicMLLogger] = None | ||
self.timer: Optional[threading.Timer] = None | ||
|
||
def init(self, state: State, logger: Logger): | ||
for callback in state.callbacks: | ||
if isinstance(callback, MosaicMLLogger): | ||
self.mosaicml_logger = callback | ||
|
||
def _reset(self): | ||
if self.timer is not None: | ||
self.timer.cancel() | ||
self.timer = None | ||
|
||
def _timeout(self): | ||
self._reset() | ||
self.timer = threading.Timer( | ||
self.timeout, | ||
_timeout, | ||
[self.timeout, self.mosaicml_logger], | ||
) | ||
self.timer.daemon = True | ||
self.timer.start() | ||
|
||
def fit_end(self, state: State, logger: Logger): | ||
del state | ||
del logger | ||
self._timeout() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters