[3.13t] use sysconfig to check for Python nogil builds (#144361)

`sys._is_gil_enabled()` wasn't working in certain cases, according to @atalman

Pull Request resolved: https://github.com/pytorch/pytorch/pull/144361
Approved by: https://github.com/atalman
This commit is contained in:
William Wen
2025-01-07 17:17:26 -08:00
committed by PyTorch MergeBot
parent a5051a9521
commit f700035090
2 changed files with 12 additions and 5 deletions

View File

@ -2491,11 +2491,15 @@ def compile(
return torch.sin(x) + torch.cos(x) return torch.sin(x) + torch.cos(x)
""" """
import sysconfig
_C._log_api_usage_once("torch.compile") _C._log_api_usage_once("torch.compile")
if sys.version_info >= (3, 14): if sys.version_info >= (3, 14):
raise RuntimeError("Dynamo is not supported on Python 3.14+") raise RuntimeError("torch.compile is not supported on Python 3.14+")
elif sys.version_info >= (3, 13) and not sys._is_gil_enabled(): elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1:
raise RuntimeError("Dynamo is not supported on Python with GIL disabled") raise RuntimeError(
"torch.compile is not supported on Python built with GIL disabled"
)
# Decorator mode # Decorator mode
if model is None: if model is None:

View File

@ -17,6 +17,7 @@ import inspect
import logging import logging
import os import os
import sys import sys
import sysconfig
import textwrap import textwrap
import threading import threading
import traceback import traceback
@ -813,8 +814,10 @@ class _NullDecorator(contextlib.nullcontext): # type: ignore[type-arg]
def check_if_dynamo_supported(): def check_if_dynamo_supported():
if sys.version_info >= (3, 14): if sys.version_info >= (3, 14):
raise RuntimeError("Python 3.14+ not yet supported for torch.compile") raise RuntimeError("Python 3.14+ not yet supported for torch.compile")
elif sys.version_info >= (3, 13) and not sys._is_gil_enabled(): elif sysconfig.get_config_var("Py_GIL_DISABLED") == 1:
raise RuntimeError("Dynamo is not supported on Python with GIL disabled") raise RuntimeError(
"torch.compile is not supported on Python built with GIL disabled"
)
def is_dynamo_supported(): def is_dynamo_supported():