Skip to content

Commit

Permalink
Adding support to ExceptionHandling (Pointcept#292)
Browse files Browse the repository at this point in the history
* Update events.py

Added ExceptionHandler class.
The purpose is to save the error traceback to the log_file.

* Update train.py

Integrate ExceptionHandler Context Manager into the training pipeline.

* Imported necessar libraries

* Rename ExceptionHandler to ExceptionWriter

---------

Co-authored-by: Xiaoyang Wu <[email protected]>
  • Loading branch information
SpeedyGonzales949 and Gofinge authored Jul 6, 2024
1 parent 9a3f603 commit 7b37078
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pointcept/engines/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 27,7 @@
from pointcept.utils.logger import get_root_logger
from pointcept.utils.optimizer import build_optimizer
from pointcept.utils.scheduler import build_scheduler
from pointcept.utils.events import EventStorage
from pointcept.utils.events import EventStorage, ExceptionWriter
from pointcept.utils.registry import Registry


Expand Down Expand Up @@ -145,7 145,7 @@ def __init__(self, cfg):
self.register_hooks(self.cfg.hooks)

def train(self):
with EventStorage() as self.storage:
with EventStorage() as self.storage, ExceptionWriter():
# => before train
self.before_train()
self.logger.info(">>>>>>>>>>>>>>>> Start Training >>>>>>>>>>>>>>>>")
Expand Down
19 changes: 19 additions & 0 deletions pointcept/utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 14,8 @@
import time
import torch
import numpy as np
import traceback
import sys

from typing import List, Optional, Tuple
from collections import defaultdict
Expand All @@ -25,6 27,7 @@
"TensorboardXWriter",
"CommonMetricPrinter",
"EventStorage",
"ExceptionWriter",
]

_CURRENT_STORAGE_STACK = []
Expand Down Expand Up @@ -591,3 863,19 @@ def values(self) -> List[Tuple[float, float]]:
list[(number, iteration)]: content of the current buffer.
"""
return self._data


class ExceptionWriter:

def __init__(self):
self.logger = logging.getLogger(__name__)

def __enter__(self):
pass

def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type:
tb = traceback.format_exception(exc_type, exc_val, exc_tb)
formatted_tb_str = "".join(tb)
self.logger.error(formatted_tb_str)
sys.exit(1) # This prevents double logging the error to the console

0 comments on commit 7b37078

Please sign in to comment.