-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Add
is_tracing
to wrap torch.jit.is_tracing
in differen…
…t versions. (#1187) * Add `is_tracing` to wrap `torch.jit.is_tracing` in different versions. * Remame `is_tracing` to `is_jit_tracing` * Ignore `is_jit_tracing` tests in CI.
- Loading branch information
Showing
4 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import warnings | ||
from distutils.version import LooseVersion | ||
|
||
import torch | ||
|
||
|
||
def is_jit_tracing() -> bool: | ||
if LooseVersion(torch.__version__) >= LooseVersion('1.6.0'): | ||
on_trace = torch.jit.is_tracing() | ||
# In PyTorch 1.6, torch.jit.is_tracing has a bug. | ||
# Refers to https://github.com/pytorch/pytorch/issues/42448 | ||
if isinstance(on_trace, bool): | ||
return on_trace | ||
else: | ||
return torch._C._is_tracing() | ||
else: | ||
warnings.warn( | ||
'torch.jit.is_tracing is only supported after v1.6.0. ' | ||
'Therefore is_tracing returns False automatically. Please ' | ||
'set on_trace manually if you are using trace.', UserWarning) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from distutils.version import LooseVersion | ||
|
||
import pytest | ||
import torch | ||
|
||
from mmcv.utils import is_jit_tracing | ||
|
||
|
||
@pytest.mark.skipif( | ||
LooseVersion(torch.__version__) < LooseVersion('1.6.0'), | ||
reason='torch.jit.is_tracing is not available before 1.6.0') | ||
def test_is_jit_tracing(): | ||
|
||
def foo(x): | ||
if is_jit_tracing(): | ||
return x | ||
else: | ||
return x.tolist() | ||
|
||
x = torch.rand(3) | ||
# test without trace | ||
assert isinstance(foo(x), list) | ||
|
||
# test with trace | ||
traced_foo = torch.jit.trace(foo, (torch.rand(1), )) | ||
assert isinstance(traced_foo(x), torch.Tensor) |