Skip to content

Commit

Permalink
[MISC] add decorator for logging exceptions (dmlc#1512)
Browse files Browse the repository at this point in the history
* add decorator for logging exceptions

Signed-off-by: Sheng Zha <[email protected]>

* make dev dependencies available in CI

Signed-off-by: Sheng Zha <[email protected]>

* limit spacy<3

Signed-off-by: Sheng Zha <[email protected]>
  • Loading branch information
szha authored Feb 2, 2021
1 parent 8d31297 commit 302865c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/unittests-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defaults:
jobs:
unittest-gpu:
runs-on: ubuntu-latest
strategy:
strategy:
fail-fast: false
steps:
- name: Checkout repository
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
--save-path coverage.xml \
--remote https://github.com/${{ github.repository }} \
--command "python3 -m pip install pytest-forked && python3 -m pytest --forked --cov=. --cov-config=./.coveragerc --cov-report=xml --durations=50 --device="gpu" --runslow ./tests/" \
--wait | tee batch_job.log
--wait | tee batch_job.log
- name: Test project on AWS Batch(For pull request)
Expand Down Expand Up @@ -88,11 +88,11 @@ jobs:
bash codecov.sh -f ./coverage.xml -n -F; \
else \
bash codecov.sh -f ./coverage.xml -n -F -C ${{ github.event.pull_request.head.sha }} -P ${{ github.event.pull_request.number }}; \
fi
fi
env:
EVENT_NAME: ${{ github.event_name }}
- name: Upload Cloud Watch Log Stream

- name: Upload Cloud Watch Log Stream
if: ${{ failure() || success() }}
uses: actions/upload-artifact@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
python -m pip install setuptools pytest pytest-cov contextvars
python -m pip install --upgrade cython
python -m pip install --pre "mxnet>=2.0.0b20210121" -f https://dist.mxnet.io/python
python -m pip install -U -e .[extras]
python -m pip install -U -e .[extras,dev]
- name: Build and Install TVM
if: matrix.os == 'ubuntu-latest'
run: |
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def find_version(*file_paths):
'tqdm',
'jieba',
'subword_nmt',
'spacy>=2.3.0',
'spacy>=2.3.0,<3',
'langid==1.1.6',
'nltk',
'h5py>=2.10',
Expand All @@ -92,6 +92,7 @@ def find_version(*file_paths):
'dev': [
'pytest',
'pytest-env',
'pytest-mock',
'pylint',
'pylint_quotes',
'flake8',
Expand Down
26 changes: 24 additions & 2 deletions src/gluonnlp/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__all__ = ['glob', 'file_line_number', 'md5sum', 'sha1sum', 'naming_convention',
'logging_config', 'set_seed', 'sizeof_fmt', 'grouper', 'repeat',
'parse_ctx', 'load_checksum_stats', 'download', 'check_version',
'init_comm', 'get_mxnet_visible_ctx']
'init_comm', 'get_mxnet_visible_ctx', 'logerror']

import os
import sys
Expand All @@ -11,7 +11,7 @@
import functools
import uuid
from types import ModuleType
from typing import Optional, Tuple
from typing import Optional
import numpy as np
import hashlib
import requests
Expand Down Expand Up @@ -184,6 +184,28 @@ def logging_config(folder: Optional[str] = None,
return folder


def logerror(logger: logging.Logger = logging.root):
"""A decorator that wraps the passed in function and logs exceptions.
Parameters
----------
logger: logging.Logger
The logger to which to log the error.
"""
def log_wrapper(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
try:
return function(*args, **kwargs)
except Exception as e:
# log the exception
logger.exception(
f'{function.__name__}(args={args}, kwargs={kwargs}) failed:\n{e}.')
raise e
return wrapper
return log_wrapper


def set_seed(seed):
import mxnet as mx
mx.random.seed(seed)
Expand Down
21 changes: 19 additions & 2 deletions tests/test_utils_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
import mxnet as mx
from gluonnlp.utils.misc import download, sha1sum, logging_config,\
get_mxnet_visible_ctx
get_mxnet_visible_ctx, logerror
mx.npx.set_np()


Expand Down Expand Up @@ -109,4 +109,21 @@ def test_get_mxnet_visible_ctx(ctx):
ctx_l = get_mxnet_visible_ctx()
for ele_ctx in ctx_l:
arr = mx.np.array(1.0, ctx=ele_ctx)
arr_np = arr.asnumpy()
arr.asnumpy()


@pytest.mark.parametrize('a,err', [
(None, TypeError),
(range(2), IndexError)])
def test_logerror(mocker, a, err):
logger = logging.getLogger(__name__)
spy = mocker.spy(logger, 'exception')

@logerror(logger=logger)
def test_fn(a):
return a[3]

with pytest.raises(err):
test_fn(a)

assert spy.call_count == 1
2 changes: 1 addition & 1 deletion tools/docker/gluon_nlp_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ else
python3 -m pip install -U --quiet --pre "mxnet-cu102>=2.0.0b20210121" -f https://dist.mxnet.io/python/cu102 --user
fi

python3 -m pip install --quiet -e .[extras]
python3 -m pip install --quiet -e .[extras,dev]

cd $WORK_DIR
/bin/bash -o pipefail -c "$COMMAND"
Expand Down
2 changes: 1 addition & 1 deletion tools/docker/ubuntu18.04-cpu.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ RUN cd ${WORKDIR} \
&& git clone https://github.com/dmlc/gluon-nlp \
&& cd gluon-nlp \
&& git checkout master \
&& python3 -m pip install -U -e ."[extras]"
&& python3 -m pip install -U -e ."[extras,dev]"


# Stage-CI
Expand Down
2 changes: 1 addition & 1 deletion tools/docker/ubuntu18.04-gpu.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RUN cd ${WORKDIR} \
&& git clone https://github.com/dmlc/gluon-nlp \
&& cd gluon-nlp \
&& git checkout master \
&& python3 -m pip install -U -e ."[extras]"
&& python3 -m pip install -U -e ."[extras,dev]"

# Stage-CI
FROM base as ci
Expand Down

0 comments on commit 302865c

Please sign in to comment.