Team Ai
Apppublic

Arulkumar03/Fox_Sheep_Detector_Computer_Vision_model

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
tracing.py72 linesDownload Raw Back to utils
1import inspect2import torch3 4from detectron2.utils.env import TORCH_VERSION5 6try:7    from torch.fx._symbolic_trace import is_fx_tracing as is_fx_tracing_current8 9    tracing_current_exists = True10except ImportError:11    tracing_current_exists = False12 13try:14    from torch.fx._symbolic_trace import _orig_module_call15 16    tracing_legacy_exists = True17except ImportError:18    tracing_legacy_exists = False19 20 21@torch.jit.ignore22def is_fx_tracing_legacy() -> bool:23    """24    Returns a bool indicating whether torch.fx is currently symbolically tracing a module.25    Can be useful for gating module logic that is incompatible with symbolic tracing.26    """27    return torch.nn.Module.__call__ is not _orig_module_call28 29 30@torch.jit.ignore31def is_fx_tracing() -> bool:32    """Returns whether execution is currently in33    Torch FX tracing mode"""34    if TORCH_VERSION >= (1, 10) and tracing_current_exists:35        return is_fx_tracing_current()36    elif tracing_legacy_exists:37        return is_fx_tracing_legacy()38    else:39        # Can't find either current or legacy tracing indication code.40        # Enabling this assert_fx_safe() call regardless of tracing status.41        return False42 43 44@torch.jit.ignore45def assert_fx_safe(condition: bool, message: str) -> torch.Tensor:46    """An FX-tracing safe version of assert.47    Avoids erroneous type assertion triggering when types are masked inside48    an fx.proxy.Proxy object during tracing.49    Args: condition - either a boolean expression or a string representing50    the condition to test. If this assert triggers an exception when tracing51    due to dynamic control flow, try encasing the expression in quotation52    marks and supplying it as a string."""53    # Must return a concrete tensor for compatibility with PyTorch <=1.8.54    # If <=1.8 compatibility is not needed, return type can be converted to None55    if not is_fx_tracing():56        try:57            if isinstance(condition, str):58                caller_frame = inspect.currentframe().f_back59                torch._assert(60                    eval(condition, caller_frame.f_globals, caller_frame.f_locals), message61                )62                return torch.ones(1)63            else:64                torch._assert(condition, message)65                return torch.ones(1)66        except torch.fx.proxy.TraceError as e:67            print(68                "Found a non-FX compatible assertion. Skipping the check. Failure is shown below"69                + str(e)70            )71    return torch.zeros(1)72