Utils Compat#

compat #

FUNCTION DESCRIPTION
jit_compile

Conditionally apply torch.jit.script or torch.compile based on

Functions#

jit_compile(func) #

Conditionally apply torch.jit.script or torch.compile based on availability.

Python 3.14+ deprecates torch.jit.script in favor of torch.compile. This decorator provides backward and forward compatibility by:

  • Using torch.compile if available (PyTorch 2.0+) and Python >= 3.14
  • Using torch.jit.script on Python < 3.14
  • Falling back to eager mode if neither is available or compilation fails

Can disable torch.compile by setting the environment variable: - TORCH_COMPILE_DISABLE=1

Version requirements:

  • torch.jit.script: PyTorch 1.0+ (all versions)
  • torch.compile: PyTorch 2.0+ only
Note

On Python 3.14+, torch.compile may still emit deprecation warnings about torch.jit.script_method internally. These warnings are from PyTorch's own implementation.

PARAMETER DESCRIPTION
func

Function to potentially compile.

TYPE: callable

RETURNS DESCRIPTION
callable

Compiled function or original function if compilation is unavailable.

Source code in spectre/utils/compat.py
def jit_compile(func):
    """
    Conditionally apply torch.jit.script or torch.compile based on
    availability.

    Python 3.14+ deprecates torch.jit.script in favor of torch.compile.
    This decorator provides backward and forward compatibility by:

    - Using torch.compile if available (PyTorch 2.0+) and Python >= 3.14
    - Using torch.jit.script on Python < 3.14
    - Falling back to eager mode if neither is available or compilation fails

    Can disable torch.compile by setting the environment variable:
    - TORCH_COMPILE_DISABLE=1

    Version requirements:

    - torch.jit.script: PyTorch 1.0+ (all versions)
    - torch.compile: PyTorch 2.0+ only

    Note
    ----
    On Python 3.14+, torch.compile may still emit deprecation warnings about
    torch.jit.script_method internally. These warnings are from PyTorch's own
    implementation.

    Parameters
    ----------
    func : callable
        Function to potentially compile.

    Returns
    -------
    callable
        Compiled function or original function if compilation is unavailable.
    """
    if os.environ.get("TORCH_COMPILE_DISABLE", "0") == "1":
        return func

    # Python 3.14+ prefers torch.compile (avoids JIT deprecation warning)
    if sys.version_info >= (3, 14):
        # Check if torch.compile is available (PyTorch 2.0+)
        if hasattr(torch, "compile"):
            try:
                return torch.compile(func)
            except Exception:
                # Silently fall back if compilation fails (e.g., unsupported
                # GPU, compilation errors)
                return func
        else:
            # PyTorch < 2.0 doesn't have torch.compile
            # Fall back (JIT would trigger deprecation warning)
            return func
    else:
        # Python < 3.14 - use torch.jit.script
        try:
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", message=".*torch.jit.script.*")
                return torch.jit.script(func)
        except Exception:
            return func