Skip to content

Commit a293a56

Browse files
craymichaelfacebook-github-bot
authored andcommitted
Make Captum log_usage universal by supporting async functions (#1614)
Summary: Pull Request resolved: #1614 As title. Checks if function being wrapped is a coroutine and uses an async wrapper instead of sync when logging. Reviewed By: vivekmig Differential Revision: D76837949 fbshipit-source-id: 38893ed0404ae3b0f7140cb32055f0500dc8f9eb
1 parent ce452a2 commit a293a56

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

captum/log/dummy_log.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# pyre-strict
44

55
from functools import wraps
6+
from inspect import iscoroutinefunction
67
from types import TracebackType
78
from typing import Any, List, Optional, Union
89

@@ -29,16 +30,30 @@ def __exit__(
2930

3031
# pyre-fixme[3]: Return type must be annotated.
3132
def log_usage(*log_args: Any, **log_kwargs: Any):
33+
3234
# pyre-fixme[3]: Return type must be annotated.
3335
# pyre-fixme[2]: Parameter must be annotated.
3436
def _log_usage(func):
35-
@wraps(func)
36-
# pyre-fixme[53]: Captured variable `func` is not annotated.
37-
# pyre-fixme[3]: Return type must be annotated.
38-
def wrapper(*args: Any, **kwargs: Any):
39-
return func(*args, **kwargs)
40-
41-
return wrapper
37+
if iscoroutinefunction(func):
38+
39+
@wraps(func)
40+
# pyre-fixme[53]: Captured variable `func` is not annotated.
41+
# pyre-fixme[3]: Return type must be annotated.
42+
# pyre-fixme[2]: Parameter must be annotated.
43+
async def async_wrapper(*args, **kwargs):
44+
return await func(*args, **kwargs)
45+
46+
return async_wrapper
47+
else:
48+
49+
@wraps(func)
50+
# pyre-fixme[53]: Captured variable `func` is not annotated.
51+
# pyre-fixme[3]: Return type must be annotated.
52+
# pyre-fixme[2]: Parameter must be annotated.
53+
def wrapper(*args, **kwargs):
54+
return func(*args, **kwargs)
55+
56+
return wrapper
4257

4358
return _log_usage
4459

0 commit comments

Comments
 (0)