Fix #954 by bringing traceable _assert into timm to allow compat w/ PyTorch < 1.8
parent
a41de1f666
commit
4f0f9cb348
@ -0,0 +1,23 @@
|
|||||||
|
import torch
|
||||||
|
try:
|
||||||
|
from torch.overrides import has_torch_function, handle_torch_function
|
||||||
|
except ImportError:
|
||||||
|
from torch._overrides import has_torch_function, handle_torch_function
|
||||||
|
|
||||||
|
|
||||||
|
def _assert(condition, message):
|
||||||
|
r"""A wrapper around Python's assert which is symbolically traceable.
|
||||||
|
This is based on _assert method in torch.__init__.py but brought here to avoid reliance
|
||||||
|
on internal torch fn and allow compatibility with PyTorch < 1.8.
|
||||||
|
"""
|
||||||
|
if type(condition) is not torch.Tensor and has_torch_function((condition,)):
|
||||||
|
return handle_torch_function(_assert, (condition,), condition, message)
|
||||||
|
assert condition, message
|
||||||
|
|
||||||
|
|
||||||
|
def _float_to_int(x: float) -> int:
|
||||||
|
"""
|
||||||
|
Symbolic tracing helper to substitute for inbuilt `int`.
|
||||||
|
Hint: Inbuilt `int` can't accept an argument of type `Proxy`
|
||||||
|
"""
|
||||||
|
return int(x)
|
Loading…
Reference in new issue