Skip to content

Commit

Permalink
[feat] Add exception-free mode to Aim (#2561)
Browse files Browse the repository at this point in the history
  • Loading branch information
alberttorosyan authored Mar 2, 2023
1 parent 58c1535 commit 8ff63a8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## 3.16.2
- Add exception-free mode to Aim (alberttorosyan)
- Expose `capture_terminal_logs` argument for `aim.sdk.adapters` classes (mihran113)
- Handle inconsistency between Sequence data and metadata (alberttorosyan)

Expand Down
40 changes: 40 additions & 0 deletions aim/ext/exception_resistant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import logging

from functools import wraps


logger = logging.getLogger(__name__)


def exception_resistant(silent: bool):
def inner(func):
if not silent:
Expand Down Expand Up @@ -30,3 +35,38 @@ def wrapper(*args, **kwargs):
pass
return wrapper
return inner


class _SafeModeConfig:
@staticmethod
def log_exception(e: Exception, func: callable):
logger.warning(f'Exception "{str(e)}" raised in function "{func.__name__}"')

@staticmethod
def reraise_exception(e: Exception, func: callable):
raise e

exception_callback = reraise_exception


def enable_safe_mode():
_SafeModeConfig.exception_callback = _SafeModeConfig.log_exception


def disable_safe_mode():
_SafeModeConfig.exception_callback = _SafeModeConfig.reraise_exception


def set_exception_callback(callback: callable):
_SafeModeConfig.exception_callback = callback


def noexcept(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
_SafeModeConfig.exception_callback(e, func)

return wrapper
5 changes: 5 additions & 0 deletions aim/sdk/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
get_installed_packages,
get_git_info,
)
from aim.ext.exception_resistant import noexcept

from typing import Any, Dict, Iterator, Optional, Tuple, Union
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -334,6 +335,7 @@ def __hash__(self) -> int:
def idx_to_ctx(self, idx: int) -> Context:
return self._tracker.idx_to_ctx(idx)

@noexcept
def __setitem__(self, key: str, val: Any):
"""Set Run top-level meta-parameter.
Expand Down Expand Up @@ -364,6 +366,7 @@ def __getitem__(self, key):
"""
return self._collect(key)

@noexcept
def set(self, key, val: Any, strict: bool = True):
self.meta_run_attrs_tree.set(key, val, strict)
self.meta_attrs_tree.set(key, val, strict)
Expand All @@ -385,6 +388,7 @@ def __delitem__(self, key: str):
del self.meta_attrs_tree[key]
del self.meta_run_attrs_tree[key]

@noexcept
def track(
self,
value,
Expand Down Expand Up @@ -812,6 +816,7 @@ class Run(BasicRun):
git info, environment variables, etc.
"""

@noexcept
def __init__(self, run_hash: Optional[str] = None, *,
repo: Optional[Union[str, 'Repo']] = None,
read_only: bool = False,
Expand Down
15 changes: 15 additions & 0 deletions aim/sdk/training_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ def training_successfully_finished(
):
"""Is called after the training phase is successfully finished."""

@event
def exception_raised(
self, *,
exception: Exception,
**kwargs
):
"""Is called when exception is raised from Aim codebase. """

def handle_exceptions(self):
from aim.ext.exception_resistant import set_exception_callback

def callback(e: Exception, func: callable):
self.exception_raised(exception=e, function=func)
set_exception_callback(callback)

# @event
# def run_available_experimental(
# self, *,
Expand Down
1 change: 1 addition & 0 deletions aim/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from aim.ext.exception_resistant import enable_safe_mode, disable_safe_mode # noqa
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[build-system]
requires = ["setuptools", "cython == 3.0.0a11", "aimrocks == 0.3.1"]
requires = ["setuptools", "cython == 3.0.0a11", "aimrocks == 0.4.0"]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def package_files(directory):
REQUIRED = [
f'aim-ui=={__version__}',
'aimrecords==0.0.7',
'aimrocks==0.3.1',
'aimrocks==0.4.0',
'cachetools>=4.0.0',
'click>=7.0',
'cryptography>=3.0',
Expand Down

0 comments on commit 8ff63a8

Please sign in to comment.