Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log exception on inactivity callback #1194

Merged
merged 11 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llmfoundry/callbacks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from llmfoundry.callbacks.eval_output_logging_callback import EvalOutputLogging
from llmfoundry.callbacks.fdiff_callback import FDiffMetrics
from llmfoundry.callbacks.hf_checkpointer import HuggingFaceCheckpointer
from llmfoundry.callbacks.inactivity_callback import InactivityCallback
from llmfoundry.callbacks.log_mbmoe_tok_per_expert_callback import \
MegaBlocksMoE_TokPerExpert
from llmfoundry.callbacks.monolithic_ckpt_callback import \
Expand Down Expand Up @@ -47,6 +48,7 @@
callbacks.register('oom_observer', func=OOMObserver)
callbacks.register('eval_output_logging', func=EvalOutputLogging)
callbacks.register('mbmoe_tok_per_expert', func=MegaBlocksMoE_TokPerExpert)
callbacks.register('inactivity', func=InactivityCallback)

callbacks_with_config.register('async_eval', func=AsyncEval)
callbacks_with_config.register('curriculum_learning', func=CurriculumLearning)
Expand Down
56 changes: 56 additions & 0 deletions llmfoundry/callbacks/inactivity_callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 no Trainer events were triggered for {timeout} seconds.',
jjanezhang marked this conversation as resolved.
Show resolved Hide resolved
)
if mosaicml_logger is not None:
mosaicml_logger.log_exception(RunTimeoutError(timeout=timeout))
os.kill(os.getpid(), signal.SIGINT)


class InactivityCallback(Callback):
jjanezhang marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
timeout: int = 1800,
mosaicml_logger: Optional[MosaicMLLogger] = None,
jjanezhang marked this conversation as resolved.
Show resolved Hide resolved
):
self.timeout = timeout
self.mosaicml_logger = mosaicml_logger
self.timer: Optional[threading.Timer] = None

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()
9 changes: 9 additions & 0 deletions llmfoundry/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,12 @@ def __init__(self, dataset_name: str, split: str) -> None:
self.split = split
message = f'Your dataset (name={dataset_name}, split={split}) is misconfigured. Please check your dataset format and make sure you can load your dataset locally.'
super().__init__(message)


class RunTimeoutError(RuntimeError):
"""Error thrown when a run times out."""

def __init__(self, timeout: int) -> None:
self.timeout = timeout
message = f'Run timed out after {timeout} seconds.'
super().__init__(message)
5 changes: 5 additions & 0 deletions scripts/train/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ def main(cfg: DictConfig) -> Trainer:
callback_configs = train_cfg.callbacks or {}

# Callbacks
if callback_configs is not None:
jjanezhang marked this conversation as resolved.
Show resolved Hide resolved
for name, callback_cfg in callback_configs.items():
if name == 'inactivity':
callback_cfg['mosaicml_logger'] = mosaicml_logger

callbacks: List[Callback] = [
build_callback(
name=str(name),
Expand Down
Loading